@bdkinc/knex-ibmi 0.0.21 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/execution/ibmi-transaction.d.ts +7 -0
- package/dist/execution/ibmi-transaction.js +20 -0
- package/dist/execution/ibmi-transaction.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +217 -0
- package/dist/index.js.map +1 -0
- package/dist/query/ibmi-querycompiler.d.ts +17 -0
- package/dist/query/ibmi-querycompiler.js +226 -0
- package/dist/query/ibmi-querycompiler.js.map +1 -0
- package/dist/schema/ibmi-columncompiler.d.ts +7 -0
- package/dist/schema/ibmi-columncompiler.js +13 -0
- package/dist/schema/ibmi-columncompiler.js.map +1 -0
- package/dist/schema/ibmi-compiler.d.ts +6 -0
- package/dist/schema/ibmi-compiler.js +48 -0
- package/dist/schema/ibmi-compiler.js.map +1 -0
- package/dist/schema/ibmi-tablecompiler.d.ts +9 -0
- package/dist/schema/ibmi-tablecompiler.js +103 -0
- package/dist/schema/ibmi-tablecompiler.js.map +1 -0
- package/package.json +3 -2
- package/src/execution/ibmi-transaction.js +99 -0
- package/src/execution/ibmi-transaction.ts +21 -0
- package/src/index.d.ts +4 -0
- package/src/index.ts +281 -0
- package/src/query/ibmi-querycompiler.js +249 -0
- package/src/query/ibmi-querycompiler.ts +236 -0
- package/src/schema/ibmi-columncompiler.js +33 -0
- package/src/schema/ibmi-columncompiler.ts +14 -0
- package/src/schema/ibmi-compiler.js +67 -0
- package/src/schema/ibmi-compiler.ts +52 -0
- package/src/schema/ibmi-tablecompiler.js +166 -0
- package/src/schema/ibmi-tablecompiler.ts +114 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Transaction from "knex/lib/execution/transaction";
|
|
2
|
+
declare class IBMiTransaction extends Transaction {
|
|
3
|
+
begin(connection: any): Promise<any>;
|
|
4
|
+
rollback(connection: any): Promise<any>;
|
|
5
|
+
commit(connection: any): Promise<any>;
|
|
6
|
+
}
|
|
7
|
+
export default IBMiTransaction;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const transaction_1 = require("knex/lib/execution/transaction");
|
|
5
|
+
class IBMiTransaction extends transaction_1.default {
|
|
6
|
+
async begin(connection) {
|
|
7
|
+
await connection.beginTransaction();
|
|
8
|
+
return connection;
|
|
9
|
+
}
|
|
10
|
+
async rollback(connection) {
|
|
11
|
+
await connection.rollback();
|
|
12
|
+
return connection;
|
|
13
|
+
}
|
|
14
|
+
async commit(connection) {
|
|
15
|
+
await connection.commit();
|
|
16
|
+
return connection;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.default = IBMiTransaction;
|
|
20
|
+
//# sourceMappingURL=ibmi-transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibmi-transaction.js","sourceRoot":"","sources":["../../src/execution/ibmi-transaction.ts"],"names":[],"mappings":";;AAAA,aAAa;AACb,gEAAyD;AAEzD,MAAM,eAAgB,SAAQ,qBAAW;IACvC,KAAK,CAAC,KAAK,CAAC,UAAU;QACpB,MAAM,UAAU,CAAC,gBAAgB,EAAE,CAAC;QACpC,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,UAAU;QACvB,MAAM,UAAU,CAAC,QAAQ,EAAE,CAAC;QAC5B,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,UAAU;QACrB,MAAM,UAAU,CAAC,MAAM,EAAE,CAAC;QAC1B,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AAED,kBAAe,eAAe,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Connection } from "odbc";
|
|
2
|
+
import { knex, Knex } from "knex";
|
|
3
|
+
import * as odbc from "odbc";
|
|
4
|
+
import SchemaCompiler from "./schema/ibmi-compiler";
|
|
5
|
+
import TableCompiler from "./schema/ibmi-tablecompiler";
|
|
6
|
+
import ColumnCompiler from "./schema/ibmi-columncompiler";
|
|
7
|
+
import QueryCompiler from "./query/ibmi-querycompiler";
|
|
8
|
+
declare class DB2Client extends knex.Client {
|
|
9
|
+
constructor(config: any);
|
|
10
|
+
_driver(): typeof odbc;
|
|
11
|
+
printDebug(message: string): void;
|
|
12
|
+
acquireRawConnection(): Promise<any>;
|
|
13
|
+
destroyRawConnection(connection: Connection): Promise<void>;
|
|
14
|
+
_getConnectionString(connectionConfig: any): string;
|
|
15
|
+
_query(connection: any, obj: any): Promise<any>;
|
|
16
|
+
transaction(container: any, config: any, outerTx: any): Knex.Transaction;
|
|
17
|
+
schemaCompiler(): SchemaCompiler;
|
|
18
|
+
tableCompiler(): TableCompiler;
|
|
19
|
+
columnCompiler(): ColumnCompiler;
|
|
20
|
+
queryCompiler(): QueryCompiler;
|
|
21
|
+
processResponse(obj: any, runner: any): any;
|
|
22
|
+
}
|
|
23
|
+
interface DB2PoolConfig {
|
|
24
|
+
min: number;
|
|
25
|
+
max: number;
|
|
26
|
+
}
|
|
27
|
+
interface DB2ConnectionParams {
|
|
28
|
+
CMT?: number;
|
|
29
|
+
CONNTYPE?: number;
|
|
30
|
+
DBQ?: string;
|
|
31
|
+
MAXDECPREC?: 31 | 63;
|
|
32
|
+
MAXDECSCALE?: number;
|
|
33
|
+
MINDIVSCALE?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
34
|
+
NAM?: 0 | 1;
|
|
35
|
+
DFT?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
|
36
|
+
DSP?: 0 | 1 | 2 | 3 | 4;
|
|
37
|
+
DEC?: 0 | 1;
|
|
38
|
+
DECFLOATERROROPTION?: 0 | 1;
|
|
39
|
+
DECFLOATROUNDMODE?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
40
|
+
MAPDECIMALFLOATDESCRIBE?: 1 | 3;
|
|
41
|
+
}
|
|
42
|
+
interface DB2ConnectionConfig {
|
|
43
|
+
database: string;
|
|
44
|
+
host: string;
|
|
45
|
+
port: 50000 | number;
|
|
46
|
+
user: string;
|
|
47
|
+
password: string;
|
|
48
|
+
driver: "IBM i Access ODBC Driver" | string;
|
|
49
|
+
connectionStringParams?: DB2ConnectionParams;
|
|
50
|
+
pool?: DB2PoolConfig;
|
|
51
|
+
}
|
|
52
|
+
export interface DB2Config {
|
|
53
|
+
client: any;
|
|
54
|
+
connection: DB2ConnectionConfig;
|
|
55
|
+
pool?: DB2PoolConfig;
|
|
56
|
+
}
|
|
57
|
+
export declare const DB2Dialect: typeof DB2Client;
|
|
58
|
+
export default DB2Client;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DB2Dialect = void 0;
|
|
4
|
+
const process = require("process");
|
|
5
|
+
const knex_1 = require("knex");
|
|
6
|
+
const odbc = require("odbc");
|
|
7
|
+
const console = require("console");
|
|
8
|
+
const ibmi_compiler_1 = require("./schema/ibmi-compiler");
|
|
9
|
+
const ibmi_tablecompiler_1 = require("./schema/ibmi-tablecompiler");
|
|
10
|
+
const ibmi_columncompiler_1 = require("./schema/ibmi-columncompiler");
|
|
11
|
+
const ibmi_transaction_1 = require("./execution/ibmi-transaction");
|
|
12
|
+
const ibmi_querycompiler_1 = require("./query/ibmi-querycompiler");
|
|
13
|
+
class DB2Client extends knex_1.knex.Client {
|
|
14
|
+
constructor(config) {
|
|
15
|
+
super(config);
|
|
16
|
+
this.driverName = "odbc";
|
|
17
|
+
if (this.dialect && !this.config.client) {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
this.logger.warn(`Using 'this.dialect' to identify the client is deprecated and support for it will be removed in the future. Please use configuration option 'client' instead.`);
|
|
20
|
+
}
|
|
21
|
+
const dbClient = this.config.client || this.dialect;
|
|
22
|
+
if (!dbClient) {
|
|
23
|
+
throw new Error(`knex: Required configuration option 'client' is missing.`);
|
|
24
|
+
}
|
|
25
|
+
if (config.version) {
|
|
26
|
+
this.version = config.version;
|
|
27
|
+
}
|
|
28
|
+
if (this.driverName && config.connection) {
|
|
29
|
+
this.initializeDriver();
|
|
30
|
+
if (!config.pool || (config.pool && config.pool.max !== 0)) {
|
|
31
|
+
this.initializePool(config);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
this.valueForUndefined = this.raw("DEFAULT");
|
|
35
|
+
if (config.useNullAsDefault) {
|
|
36
|
+
this.valueForUndefined = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
_driver() {
|
|
40
|
+
return odbc;
|
|
41
|
+
}
|
|
42
|
+
printDebug(message) {
|
|
43
|
+
if (process.env.DEBUG === "true") {
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
this.logger.debug(message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Get a raw connection, called by the pool manager whenever a new
|
|
49
|
+
// connection needs to be added to the pool.
|
|
50
|
+
async acquireRawConnection() {
|
|
51
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
52
|
+
this.printDebug("acquiring raw connection");
|
|
53
|
+
const connectionConfig = this.config.connection;
|
|
54
|
+
console.log(this._getConnectionString(connectionConfig));
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
if ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.connection) === null || _b === void 0 ? void 0 : _b.pool) {
|
|
57
|
+
const poolConfig = {
|
|
58
|
+
connectionString: this._getConnectionString(connectionConfig),
|
|
59
|
+
connectionTimeout:
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
((_d = (_c = this.config) === null || _c === void 0 ? void 0 : _c.connection) === null || _d === void 0 ? void 0 : _d.acquireConnectionTimeout) || 60000,
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
initialSize: ((_g = (_f = (_e = this.config) === null || _e === void 0 ? void 0 : _e.connection) === null || _f === void 0 ? void 0 : _f.pool) === null || _g === void 0 ? void 0 : _g.min) || 2,
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
maxSize: ((_k = (_j = (_h = this.config) === null || _h === void 0 ? void 0 : _h.connection) === null || _j === void 0 ? void 0 : _j.pool) === null || _k === void 0 ? void 0 : _k.max) || 10,
|
|
66
|
+
reuseConnection: true,
|
|
67
|
+
};
|
|
68
|
+
const pool = await this.driver.pool(poolConfig);
|
|
69
|
+
return await pool.connect();
|
|
70
|
+
}
|
|
71
|
+
return await this.driver.connect(this._getConnectionString(connectionConfig));
|
|
72
|
+
}
|
|
73
|
+
// Used to explicitly close a connection, called internally by the pool manager
|
|
74
|
+
// when a connection times out or the pool is shutdown.
|
|
75
|
+
async destroyRawConnection(connection) {
|
|
76
|
+
console.log("destroy connection");
|
|
77
|
+
return await connection.close();
|
|
78
|
+
}
|
|
79
|
+
_getConnectionString(connectionConfig) {
|
|
80
|
+
const connectionStringParams = connectionConfig.connectionStringParams || {};
|
|
81
|
+
const connectionStringExtension = Object.keys(connectionStringParams).reduce((result, key) => {
|
|
82
|
+
const value = connectionStringParams[key];
|
|
83
|
+
return `${result}${key}=${value};`;
|
|
84
|
+
}, "");
|
|
85
|
+
return `${`DRIVER=${connectionConfig.driver};SYSTEM=${connectionConfig.host};HOSTNAME=${connectionConfig.host};` +
|
|
86
|
+
`PORT=${connectionConfig.port};DATABASE=${connectionConfig.database};` +
|
|
87
|
+
`UID=${connectionConfig.user};PWD=${connectionConfig.password};`}${connectionStringExtension}`;
|
|
88
|
+
}
|
|
89
|
+
// Runs the query on the specified connection, providing the bindings
|
|
90
|
+
// and any other necessary prep work.
|
|
91
|
+
async _query(connection, obj) {
|
|
92
|
+
if (!obj || typeof obj == "string")
|
|
93
|
+
obj = { sql: obj };
|
|
94
|
+
const method = (obj.hasOwnProperty("method") && obj.method !== "raw"
|
|
95
|
+
? obj.method
|
|
96
|
+
: obj.sql.split(" ")[0]).toLowerCase();
|
|
97
|
+
obj.sqlMethod = method;
|
|
98
|
+
// Different functions are used since query() doesn't return # of rows affected,
|
|
99
|
+
// which is needed for queries that modify the database
|
|
100
|
+
if (method === "select" || method === "first" || method === "pluck") {
|
|
101
|
+
const rows = await connection.query(obj.sql, obj.bindings);
|
|
102
|
+
if (rows) {
|
|
103
|
+
obj.response = { rows, rowCount: rows.length };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
await connection.beginTransaction();
|
|
108
|
+
console.log("transaction begun");
|
|
109
|
+
try {
|
|
110
|
+
const statement = await connection.createStatement();
|
|
111
|
+
await statement.prepare(obj.sql);
|
|
112
|
+
if (obj.bindings) {
|
|
113
|
+
await statement.bind(obj.bindings);
|
|
114
|
+
}
|
|
115
|
+
const result = await statement.execute();
|
|
116
|
+
// this is hacky we check the SQL for the ID column
|
|
117
|
+
// most dialects return the ID of the inserted
|
|
118
|
+
// we check for the IDENTITY scalar function
|
|
119
|
+
// if that function is present, then we just return the value of the
|
|
120
|
+
// IDENTITY column
|
|
121
|
+
if (result.statement.includes("IDENTITY_VAL_LOCAL()")) {
|
|
122
|
+
obj.response = {
|
|
123
|
+
rows: result.map((row) => {
|
|
124
|
+
var _a;
|
|
125
|
+
return result.columns && ((_a = result.columns) === null || _a === void 0 ? void 0 : _a.length) > 0
|
|
126
|
+
? row[result.columns[0].name]
|
|
127
|
+
: row;
|
|
128
|
+
}),
|
|
129
|
+
rowCount: result.count,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
else if (method === "update") {
|
|
133
|
+
if (obj.selectReturning) {
|
|
134
|
+
const returningSelect = await connection.query(obj.selectReturning);
|
|
135
|
+
obj.response = {
|
|
136
|
+
rows: returningSelect,
|
|
137
|
+
rowCount: result.count,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
obj.response = {
|
|
142
|
+
rows: result,
|
|
143
|
+
rowCount: result.count,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
obj.response = { rows: result, rowCount: result.count };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
console.error(err);
|
|
153
|
+
await connection.rollback();
|
|
154
|
+
throw new Error(err);
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
console.log("transaction committed");
|
|
158
|
+
await connection.commit();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
console.log({ obj });
|
|
162
|
+
return obj;
|
|
163
|
+
}
|
|
164
|
+
transaction(container, config, outerTx) {
|
|
165
|
+
// @ts-ignore
|
|
166
|
+
return new ibmi_transaction_1.default(this, ...arguments);
|
|
167
|
+
}
|
|
168
|
+
schemaCompiler() {
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
return new ibmi_compiler_1.default(this, ...arguments);
|
|
171
|
+
}
|
|
172
|
+
tableCompiler() {
|
|
173
|
+
// @ts-ignore
|
|
174
|
+
return new ibmi_tablecompiler_1.default(this, ...arguments);
|
|
175
|
+
}
|
|
176
|
+
columnCompiler() {
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
return new ibmi_columncompiler_1.default(this, ...arguments);
|
|
179
|
+
}
|
|
180
|
+
queryCompiler() {
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
return new ibmi_querycompiler_1.default(this, ...arguments);
|
|
183
|
+
}
|
|
184
|
+
processResponse(obj, runner) {
|
|
185
|
+
if (obj === null)
|
|
186
|
+
return null;
|
|
187
|
+
const resp = obj.response;
|
|
188
|
+
const method = obj.sqlMethod;
|
|
189
|
+
const { rows, rowCount } = resp;
|
|
190
|
+
if (obj.output)
|
|
191
|
+
return obj.output.call(runner, resp);
|
|
192
|
+
switch (method) {
|
|
193
|
+
case "select":
|
|
194
|
+
return rows;
|
|
195
|
+
case "pluck":
|
|
196
|
+
return rows.map(obj.pluck);
|
|
197
|
+
case "first":
|
|
198
|
+
return rows[0];
|
|
199
|
+
case "insert":
|
|
200
|
+
return rows;
|
|
201
|
+
case "del":
|
|
202
|
+
case "delete":
|
|
203
|
+
case "update":
|
|
204
|
+
if (obj.selectReturning) {
|
|
205
|
+
return rows;
|
|
206
|
+
}
|
|
207
|
+
return rowCount;
|
|
208
|
+
case "counter":
|
|
209
|
+
return rowCount;
|
|
210
|
+
default:
|
|
211
|
+
return rows;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.DB2Dialect = DB2Client;
|
|
216
|
+
exports.default = DB2Client;
|
|
217
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAmC;AAEnC,+BAAkC;AAClC,6BAA6B;AAC7B,mCAAmC;AACnC,0DAAoD;AACpD,oEAAwD;AACxD,sEAA0D;AAC1D,mEAAuD;AACvD,mEAAuD;AAEvD,MAAM,SAAU,SAAQ,WAAI,CAAC,MAAM;IACjC,YAAY,MAAM;QAChB,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC;QAEzB,IAAI,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YACvC,aAAa;YACb,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,+JAA+J,CAChK,CAAC;SACH;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC;QACpD,IAAI,CAAC,QAAQ,EAAE;YACb,MAAM,IAAI,KAAK,CACb,0DAA0D,CAC3D,CAAC;SACH;QAED,IAAI,MAAM,CAAC,OAAO,EAAE;YAClB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;SAC/B;QAED,IAAI,IAAI,CAAC,UAAU,I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;QAErD,QAAQ,MAAM,EAAE;YACd,KAAK,QAAQ;gBACX,OAAO,IAAI,CAAC;YACd,KAAK,OAAO;gBACV,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YAC7B,KAAK,OAAO;gBACV,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC;YACjB,KAAK,QAAQ;gBACX,OAAO,IAAI,CAAC;YACd,KAAK,KAAK,CAAC;YACX,KAAK,QAAQ,CAAC;YACd,KAAK,QAAQ;gBACX,IAAI,GAAG,CAAC,eAAe,EAAE;oBACvB,OAAO,IAAI,CAAC;iBACb;gBACD,OAAO,QAAQ,CAAC;YAClB,KAAK,SAAS;gBACZ,OAAO,QAAQ,CAAC;YAClB;gBACE,OAAO,IAAI,CAAC;SACf;IACH,CAAC;CACF;AAwCY,QAAA,UAAU,GAAG,SAAS,CAAC;AACpC,kBAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import QueryCompiler from "knex/lib/query/querycompiler";
|
|
2
|
+
declare class IBMiQueryCompiler extends QueryCompiler {
|
|
3
|
+
insert(): "" | {
|
|
4
|
+
sql: string;
|
|
5
|
+
returning: any;
|
|
6
|
+
};
|
|
7
|
+
_buildInsertData(insertValues: any, returningSql: any): string;
|
|
8
|
+
_prepInsert(data: any): any;
|
|
9
|
+
update(): {
|
|
10
|
+
sql: string;
|
|
11
|
+
returning: any;
|
|
12
|
+
selectReturning: string;
|
|
13
|
+
};
|
|
14
|
+
_returning(method: any, value: any, withTrigger: any): string | undefined;
|
|
15
|
+
columnizeWithPrefix(prefix: any, target: any): string;
|
|
16
|
+
}
|
|
17
|
+
export default IBMiQueryCompiler;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const querycompiler_1 = require("knex/lib/query/querycompiler");
|
|
5
|
+
const isObject_1 = require("lodash/isObject");
|
|
6
|
+
const wrappingFormatter_1 = require("knex/lib/formatter/wrappingFormatter");
|
|
7
|
+
const date_fns_1 = require("date-fns");
|
|
8
|
+
const isEmpty_1 = require("lodash/isEmpty");
|
|
9
|
+
const console = require("console");
|
|
10
|
+
class IBMiQueryCompiler extends querycompiler_1.default {
|
|
11
|
+
insert() {
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
const insertValues = this.single.insert || [];
|
|
14
|
+
// we need to return a value
|
|
15
|
+
// we need to wrap the insert statement in a select statement
|
|
16
|
+
// we use the "IDENTITY_VAL_LOCAL()" function to return the IDENTITY
|
|
17
|
+
// unless specified in a return
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
let sql = `SELECT ${
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
this.single.returning
|
|
22
|
+
? // @ts-ignore
|
|
23
|
+
this.formatter.columnize(this.single.returning)
|
|
24
|
+
: "IDENTITY_VAL_LOCAL()"} FROM FINAL TABLE(`;
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
sql += this.with() + `insert into ${this.tableName} `;
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
const { returning } = this.single;
|
|
29
|
+
const returningSql = returning
|
|
30
|
+
? // @ts-ignore
|
|
31
|
+
this._returning("insert", returning) + " "
|
|
32
|
+
: "";
|
|
33
|
+
if (Array.isArray(insertValues)) {
|
|
34
|
+
if (insertValues.length === 0) {
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (typeof insertValues === "object" && (0, isEmpty_1.default)(insertValues)) {
|
|
39
|
+
return {
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
sql: sql + returningSql + this._emptyInsertValue,
|
|
42
|
+
returning,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
sql += this._buildInsertData(insertValues, returningSql);
|
|
47
|
+
sql += ")";
|
|
48
|
+
return {
|
|
49
|
+
sql,
|
|
50
|
+
returning,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
_buildInsertData(insertValues, returningSql) {
|
|
54
|
+
let sql = "";
|
|
55
|
+
const insertData = this._prepInsert(insertValues);
|
|
56
|
+
if (typeof insertData === "string") {
|
|
57
|
+
sql += insertData;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
if (insertData.columns.length) {
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
sql += `(${this.formatter.columnize(insertData.columns)}`;
|
|
63
|
+
sql +=
|
|
64
|
+
`) ${returningSql}values (` +
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
this._buildInsertValues(insertData) +
|
|
67
|
+
")";
|
|
68
|
+
}
|
|
69
|
+
else if (insertValues.length === 1 && insertValues[0]) {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
sql += returningSql + this._emptyInsertValue;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return "";
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return sql;
|
|
78
|
+
}
|
|
79
|
+
_prepInsert(data) {
|
|
80
|
+
if ((0, isObject_1.default)(data)) {
|
|
81
|
+
if (data.hasOwnProperty("migration_time")) {
|
|
82
|
+
const parsed = new Date(data.migration_time);
|
|
83
|
+
data.migration_time = (0, date_fns_1.format)(parsed, "yyyy-MM-dd HH:mm:ss");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const isRaw = (0, wrappingFormatter_1.rawOrFn)(data, undefined,
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
this.builder,
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
this.client,
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
this.bindingsHolder);
|
|
93
|
+
if (isRaw)
|
|
94
|
+
return isRaw;
|
|
95
|
+
let columns = [];
|
|
96
|
+
const values = [];
|
|
97
|
+
if (!Array.isArray(data))
|
|
98
|
+
data = data ? [data] : [];
|
|
99
|
+
let i = -1;
|
|
100
|
+
while (++i < data.length) {
|
|
101
|
+
if (data[i] == null)
|
|
102
|
+
break;
|
|
103
|
+
if (i === 0)
|
|
104
|
+
columns = Object.keys(data[i]).sort();
|
|
105
|
+
const row = new Array(columns.length);
|
|
106
|
+
const keys = Object.keys(data[i]);
|
|
107
|
+
let j = -1;
|
|
108
|
+
while (++j < keys.length) {
|
|
109
|
+
const key = keys[j];
|
|
110
|
+
let idx = columns.indexOf(key);
|
|
111
|
+
if (idx === -1) {
|
|
112
|
+
columns = columns.concat(key).sort();
|
|
113
|
+
idx = columns.indexOf(key);
|
|
114
|
+
let k = -1;
|
|
115
|
+
while (++k < values.length) {
|
|
116
|
+
values[k].splice(idx, 0, undefined);
|
|
117
|
+
}
|
|
118
|
+
row.splice(idx, 0, undefined);
|
|
119
|
+
}
|
|
120
|
+
row[idx] = data[i][key];
|
|
121
|
+
}
|
|
122
|
+
values.push(row);
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
columns,
|
|
126
|
+
values,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
update() {
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
const withSQL = this.with();
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
const updates = this._prepUpdate(this.single.update);
|
|
134
|
+
// @ts-ignore
|
|
135
|
+
const where = this.where();
|
|
136
|
+
// @ts-ignore
|
|
137
|
+
const order = this.order();
|
|
138
|
+
// @ts-ignore
|
|
139
|
+
const limit = this.limit();
|
|
140
|
+
// @ts-ignore
|
|
141
|
+
const { returning } = this.single;
|
|
142
|
+
// @ts-ignore
|
|
143
|
+
const values = Object.values(this.single.update)
|
|
144
|
+
.map((a) => `${a}`)
|
|
145
|
+
.join(", ");
|
|
146
|
+
// @ts-ignore
|
|
147
|
+
console.log({
|
|
148
|
+
returning,
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
where,
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
updates,
|
|
153
|
+
// @ts-ignore
|
|
154
|
+
single: this.single.update,
|
|
155
|
+
// @ts-ignore
|
|
156
|
+
grouped: this.grouped.where,
|
|
157
|
+
values,
|
|
158
|
+
});
|
|
159
|
+
// @ts-ignore
|
|
160
|
+
const moreWheres =
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
this.grouped.where && this.grouped.where.length > 0
|
|
163
|
+
? // @ts-ignore
|
|
164
|
+
this.grouped.where.map((w) => {
|
|
165
|
+
// @ts-ignore
|
|
166
|
+
if (this.single.update.hasOwnProperty(w.column))
|
|
167
|
+
return;
|
|
168
|
+
if (!w.value)
|
|
169
|
+
return;
|
|
170
|
+
return `"${w.column}" ${w.not ? "!" : ""}${w.operator} ${w.value}`;
|
|
171
|
+
})
|
|
172
|
+
: [];
|
|
173
|
+
let selectReturning = returning
|
|
174
|
+
? `select ${returning.map((a) => `"${a}"`).join(", ")} from ${
|
|
175
|
+
// @ts-ignore
|
|
176
|
+
this.tableName
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
} where ${Object.entries(this.single.update)
|
|
179
|
+
.map(([key, value]) => `"${key}" = '${value}'`)
|
|
180
|
+
.join(" and ")}${moreWheres.length > 0 && " and "}${moreWheres.join(" and ")}`
|
|
181
|
+
: "";
|
|
182
|
+
console.log({ selectReturning });
|
|
183
|
+
const sql = withSQL +
|
|
184
|
+
// @ts-ignore
|
|
185
|
+
`update ${this.single.only ? "only " : ""}${this.tableName}` +
|
|
186
|
+
" set " +
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
updates.join(", ") +
|
|
189
|
+
(where ? ` ${where}` : "") +
|
|
190
|
+
(order ? ` ${order}` : "") +
|
|
191
|
+
(limit ? ` ${limit}` : "");
|
|
192
|
+
return { sql, returning, selectReturning };
|
|
193
|
+
}
|
|
194
|
+
_returning(method, value, withTrigger) {
|
|
195
|
+
// currently a placeholder in case I need to update return values
|
|
196
|
+
console.log("_returning", value);
|
|
197
|
+
switch (method) {
|
|
198
|
+
case "update":
|
|
199
|
+
case "insert":
|
|
200
|
+
return value
|
|
201
|
+
? // @ts-ignore
|
|
202
|
+
`${withTrigger ? " into #out" : ""}`
|
|
203
|
+
: "";
|
|
204
|
+
case "del":
|
|
205
|
+
return value
|
|
206
|
+
? // @ts-ignore
|
|
207
|
+
`${withTrigger ? " into #out" : ""}`
|
|
208
|
+
: "";
|
|
209
|
+
case "rowcount":
|
|
210
|
+
return value ? "select @@rowcount" : "";
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
columnizeWithPrefix(prefix, target) {
|
|
214
|
+
const columns = typeof target === "string" ? [target] : target;
|
|
215
|
+
let str = "", i = -1;
|
|
216
|
+
while (++i < columns.length) {
|
|
217
|
+
if (i > 0)
|
|
218
|
+
str += ", ";
|
|
219
|
+
// @ts-ignore
|
|
220
|
+
str += prefix + this.wrap(columns[i]);
|
|
221
|
+
}
|
|
222
|
+
return str;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
exports.default = IBMiQueryCompiler;
|
|
226
|
+
//# sourceMappingURL=ibmi-querycompiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibmi-querycompiler.js","sourceRoot":"","sources":["../../src/query/ibmi-querycompiler.ts"],"names":[],"mappings":";;AAAA,aAAa;AACb,gEAAyD;AACzD,8CAAuC;AACvC,4EAA2E;AAC3E,uCAAkC;AAClC,4CAAqC;AACrC,mCAAmC;AAEnC,MAAM,iBAAkB,SAAQ,uBAAa;IAC3C,MAAM;QACJ,aAAa;QACb,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;QAC9C,4BAA4B;QAC5B,6DAA6D;QAC7D,oEAAoE;QACpE,+BAA+B;QAC/B,aAAa;QACb,IAAI,GAAG,GAAG,UAAU;QAClB,aAAa;QACb,IAAI,CAAC,MAAM,CAAC,SAAS;YACnB,CAAC,CAAC,aAAa;gBACb,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;YACjD,CAAC,CAAC,sBAC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,CAAC;CACF;AAED,kBAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const columncompiler_1 = require("knex/lib/schema/columncompiler");
|
|
5
|
+
class IBMiColumnCompiler extends columncompiler_1.default {
|
|
6
|
+
increments(options = { primaryKey: true }) {
|
|
7
|
+
return ("int not null generated always as identity (start with 1, increment by 1)" +
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
(this.tableCompiler._canBeAddPrimaryKey(options) ? " primary key" : ""));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.default = IBMiColumnCompiler;
|
|
13
|
+
//# sourceMappingURL=ibmi-columncompiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibmi-columncompiler.js","sourceRoot":"","sources":["../../src/schema/ibmi-columncompiler.ts"],"names":[],"mappings":";;AAAA,aAAa;AACb,mEAA4D;AAE5D,MAAM,kBAAmB,SAAQ,wBAAc;IAC7C,UAAU,CAAC,OAAO,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE;QACvC,OAAO,CACL,0EAA0E;YAC1E,aAAa;YACb,CAAC,IAAI,CAAC,aAAa,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CACxE,CAAC;IACJ,CAAC;CACF;AAED,kBAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const compiler_1 = require("knex/lib/schema/compiler");
|
|
5
|
+
class IBMiSchemaCompiler extends compiler_1.default {
|
|
6
|
+
hasTable(tableName) {
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const formattedTable = this.client.parameter(
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
prefixedTableName(this.schema, tableName),
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
this.builder,
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
this.bindingsHolder);
|
|
15
|
+
const bindings = [tableName];
|
|
16
|
+
let sql = `SELECT TABLE_NAME FROM QSYS2.SYSTABLES ` +
|
|
17
|
+
`where TYPE = 'T' and TABLE_NAME = ${formattedTable}`;
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
if (this.schema) {
|
|
20
|
+
sql += " and TABLE_SCHEMA = ?";
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
bindings.push(this.schema);
|
|
23
|
+
}
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
this.pushQuery({
|
|
26
|
+
sql,
|
|
27
|
+
bindings,
|
|
28
|
+
output: (resp) => {
|
|
29
|
+
return resp.rowCount > 0;
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
toSQL() {
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
const sequence = this.builder._sequence;
|
|
36
|
+
for (let i = 0, l = sequence.length; i < l; i++) {
|
|
37
|
+
const query = sequence[i];
|
|
38
|
+
this[query.method].apply(this, query.args);
|
|
39
|
+
}
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
return this.sequence;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function prefixedTableName(prefix, table) {
|
|
45
|
+
return prefix ? `${prefix}.${table}` : table;
|
|
46
|
+
}
|
|
47
|
+
exports.default = IBMiSchemaCompiler;
|
|
48
|
+
//# sourceMappingURL=ibmi-compiler.js.map
|