@bdkinc/knex-ibmi 0.1.6 → 0.2.1

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.
@@ -1,249 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- // @ts-ignore
19
- var querycompiler_1 = require("knex/lib/query/querycompiler");
20
- var isObject_1 = require("lodash/isObject");
21
- var wrappingFormatter_1 = require("knex/lib/formatter/wrappingFormatter");
22
- var date_fns_1 = require("date-fns");
23
- var isEmpty_1 = require("lodash/isEmpty");
24
- var console = require("console");
25
- var IBMiQueryCompiler = /** @class */ (function (_super) {
26
- __extends(IBMiQueryCompiler, _super);
27
- function IBMiQueryCompiler() {
28
- return _super !== null && _super.apply(this, arguments) || this;
29
- }
30
- IBMiQueryCompiler.prototype.insert = function () {
31
- // @ts-ignore
32
- var insertValues = this.single.insert || [];
33
- // we need to return a value
34
- // we need to wrap the insert statement in a select statement
35
- // we use the "IDENTITY_VAL_LOCAL()" function to return the IDENTITY
36
- // unless specified in a return
37
- // @ts-ignore
38
- var sql = "SELECT ".concat(
39
- // @ts-ignore
40
- this.single.returning
41
- ? // @ts-ignore
42
- this.formatter.columnize(this.single.returning)
43
- : "IDENTITY_VAL_LOCAL()", " FROM FINAL TABLE(");
44
- // @ts-ignore
45
- sql += this.with() + "insert into ".concat(this.tableName, " ");
46
- // @ts-ignore
47
- var returning = this.single.returning;
48
- var returningSql = returning
49
- ? // @ts-ignore
50
- this._returning("insert", returning) + " "
51
- : "";
52
- if (Array.isArray(insertValues)) {
53
- if (insertValues.length === 0) {
54
- return "";
55
- }
56
- }
57
- else if (typeof insertValues === "object" && (0, isEmpty_1.default)(insertValues)) {
58
- return {
59
- // @ts-ignore
60
- sql: sql + returningSql + this._emptyInsertValue,
61
- returning: returning,
62
- };
63
- }
64
- // @ts-ignore
65
- sql += this._buildInsertData(insertValues, returningSql);
66
- sql += ")";
67
- return {
68
- sql: sql,
69
- returning: returning,
70
- };
71
- };
72
- IBMiQueryCompiler.prototype._buildInsertData = function (insertValues, returningSql) {
73
- var sql = "";
74
- var insertData = this._prepInsert(insertValues);
75
- if (typeof insertData === "string") {
76
- sql += insertData;
77
- }
78
- else {
79
- if (insertData.columns.length) {
80
- // @ts-ignore
81
- sql += "(".concat(this.formatter.columnize(insertData.columns));
82
- sql +=
83
- ") ".concat(returningSql, "values (") +
84
- // @ts-ignore
85
- this._buildInsertValues(insertData) +
86
- ")";
87
- }
88
- else if (insertValues.length === 1 && insertValues[0]) {
89
- // @ts-ignore
90
- sql += returningSql + this._emptyInsertValue;
91
- }
92
- else {
93
- return "";
94
- }
95
- }
96
- return sql;
97
- };
98
- IBMiQueryCompiler.prototype._prepInsert = function (data) {
99
- if ((0, isObject_1.default)(data)) {
100
- if (data.hasOwnProperty("migration_time")) {
101
- var parsed = new Date(data.migration_time);
102
- data.migration_time = (0, date_fns_1.format)(parsed, "yyyy-MM-dd HH:mm:ss");
103
- }
104
- }
105
- var isRaw = (0, wrappingFormatter_1.rawOrFn)(data, undefined,
106
- // @ts-ignore
107
- this.builder,
108
- // @ts-ignore
109
- this.client,
110
- // @ts-ignore
111
- this.bindingsHolder);
112
- if (isRaw)
113
- return isRaw;
114
- var columns = [];
115
- var values = [];
116
- if (!Array.isArray(data))
117
- data = data ? [data] : [];
118
- var i = -1;
119
- while (++i < data.length) {
120
- if (data[i] == null)
121
- break;
122
- if (i === 0)
123
- columns = Object.keys(data[i]).sort();
124
- var row = new Array(columns.length);
125
- var keys = Object.keys(data[i]);
126
- var j = -1;
127
- while (++j < keys.length) {
128
- var key = keys[j];
129
- var idx = columns.indexOf(key);
130
- if (idx === -1) {
131
- columns = columns.concat(key).sort();
132
- idx = columns.indexOf(key);
133
- var k = -1;
134
- while (++k < values.length) {
135
- values[k].splice(idx, 0, undefined);
136
- }
137
- row.splice(idx, 0, undefined);
138
- }
139
- row[idx] = data[i][key];
140
- }
141
- values.push(row);
142
- }
143
- return {
144
- columns: columns,
145
- values: values,
146
- };
147
- };
148
- IBMiQueryCompiler.prototype.update = function () {
149
- var _this = this;
150
- // @ts-ignore
151
- var withSQL = this.with();
152
- // @ts-ignore
153
- var updates = this._prepUpdate(this.single.update);
154
- // @ts-ignore
155
- var where = this.where();
156
- // @ts-ignore
157
- var order = this.order();
158
- // @ts-ignore
159
- var limit = this.limit();
160
- // @ts-ignore
161
- var returning = this.single.returning;
162
- // @ts-ignore
163
- var values = Object.values(this.single.update)
164
- .map(function (a) { return "".concat(a); })
165
- .join(", ");
166
- // @ts-ignore
167
- console.log({
168
- returning: returning,
169
- // @ts-ignore
170
- where: where,
171
- // @ts-ignore
172
- updates: updates,
173
- // @ts-ignore
174
- single: this.single.update,
175
- // @ts-ignore
176
- grouped: this.grouped.where,
177
- values: values,
178
- });
179
- // @ts-ignore
180
- var moreWheres =
181
- // @ts-ignore
182
- this.grouped.where && this.grouped.where.length > 0
183
- ? // @ts-ignore
184
- this.grouped.where.map(function (w) {
185
- // @ts-ignore
186
- if (_this.single.update.hasOwnProperty(w.column))
187
- return;
188
- if (!w.value)
189
- return;
190
- return "\"".concat(w.column, "\" ").concat(w.not ? "!" : "").concat(w.operator, " ").concat(w.value);
191
- })
192
- : [];
193
- var selectReturning = returning
194
- ? "select ".concat(returning.map(function (a) { return "\"".concat(a, "\""); }).join(", "), " from ").concat(
195
- // @ts-ignore
196
- this.tableName
197
- // @ts-ignore
198
- , " where ").concat(Object.entries(this.single.update)
199
- .map(function (_a) {
200
- var key = _a[0], value = _a[1];
201
- return "\"".concat(key, "\" = '").concat(value, "'");
202
- })
203
- .join(" and ")).concat(moreWheres.length > 0 && " and ").concat(moreWheres.join(" and "))
204
- : "";
205
- console.log({ selectReturning: selectReturning });
206
- var sql = withSQL +
207
- // @ts-ignore
208
- "update ".concat(this.single.only ? "only " : "").concat(this.tableName) +
209
- " set " +
210
- // @ts-ignore
211
- updates.join(", ") +
212
- (where ? " ".concat(where) : "") +
213
- (order ? " ".concat(order) : "") +
214
- (limit ? " ".concat(limit) : "");
215
- return { sql: sql, returning: returning, selectReturning: selectReturning };
216
- };
217
- IBMiQueryCompiler.prototype._returning = function (method, value, withTrigger) {
218
- // currently a placeholder in case I need to update return values
219
- console.log("_returning", value);
220
- switch (method) {
221
- case "update":
222
- case "insert":
223
- return value
224
- ? // @ts-ignore
225
- "".concat(withTrigger ? " into #out" : "")
226
- : "";
227
- case "del":
228
- return value
229
- ? // @ts-ignore
230
- "".concat(withTrigger ? " into #out" : "")
231
- : "";
232
- case "rowcount":
233
- return value ? "select @@rowcount" : "";
234
- }
235
- };
236
- IBMiQueryCompiler.prototype.columnizeWithPrefix = function (prefix, target) {
237
- var columns = typeof target === "string" ? [target] : target;
238
- var str = "", i = -1;
239
- while (++i < columns.length) {
240
- if (i > 0)
241
- str += ", ";
242
- // @ts-ignore
243
- str += prefix + this.wrap(columns[i]);
244
- }
245
- return str;
246
- };
247
- return IBMiQueryCompiler;
248
- }(querycompiler_1.default));
249
- exports.default = IBMiQueryCompiler;
@@ -1,219 +0,0 @@
1
- // @ts-ignore
2
- import QueryCompiler from "knex/lib/query/querycompiler";
3
- import isObject from "lodash/isObject";
4
- import { rawOrFn as rawOrFn_ } from "knex/lib/formatter/wrappingFormatter";
5
- import { format } from "date-fns";
6
- import isEmpty from "lodash/isEmpty";
7
-
8
- class IBMiQueryCompiler extends QueryCompiler {
9
- insert() {
10
- // @ts-ignore
11
- const insertValues = this.single.insert || [];
12
- // we need to return a value
13
- // we need to wrap the insert statement in a select statement
14
- // we use the "IDENTITY_VAL_LOCAL()" function to return the IDENTITY
15
- // unless specified in a return
16
- // @ts-ignore
17
- let sql = `select ${
18
- // @ts-ignore
19
- this.single.returning
20
- ? // @ts-ignore
21
- this.formatter.columnize(this.single.returning)
22
- : "IDENTITY_VAL_LOCAL()"
23
- } from FINAL TABLE(`;
24
- // @ts-ignore
25
- sql += this.with() + `insert into ${this.tableName} `;
26
- // @ts-ignore
27
- const { returning } = this.single;
28
- const returningSql = returning
29
- ? // @ts-ignore
30
- this._returning("insert", returning) + " "
31
- : "";
32
-
33
- if (Array.isArray(insertValues)) {
34
- if (insertValues.length === 0) {
35
- return "";
36
- }
37
- } else if (typeof insertValues === "object" && isEmpty(insertValues)) {
38
- return {
39
- // @ts-ignore
40
- sql: sql + returningSql + this._emptyInsertValue,
41
- returning,
42
- };
43
- }
44
-
45
- // @ts-ignore
46
- sql += this._buildInsertData(insertValues, returningSql);
47
- sql += ")";
48
-
49
- return {
50
- sql,
51
- returning,
52
- };
53
- }
54
-
55
- _buildInsertData(insertValues, returningSql) {
56
- let sql = "";
57
- const insertData = this._prepInsert(insertValues);
58
- if (typeof insertData === "string") {
59
- sql += insertData;
60
- } else {
61
- if (insertData.columns.length) {
62
- // @ts-ignore
63
- sql += `(${this.formatter.columnize(insertData.columns)}`;
64
- sql +=
65
- `) ${returningSql}values (` +
66
- // @ts-ignore
67
- this._buildInsertValues(insertData) +
68
- ")";
69
- } else if (insertValues.length === 1 && insertValues[0]) {
70
- // @ts-ignore
71
- sql += returningSql + this._emptyInsertValue;
72
- } else {
73
- return "";
74
- }
75
- }
76
- return sql;
77
- }
78
-
79
- _prepInsert(data) {
80
- if (isObject(data)) {
81
- if (data.hasOwnProperty("migration_time")) {
82
- const parsed = new Date(data.migration_time);
83
- data.migration_time = format(parsed, "yyyy-MM-dd HH:mm:ss");
84
- }
85
- }
86
-
87
- const isRaw = rawOrFn_(
88
- data,
89
- undefined,
90
- // @ts-ignore
91
- this.builder,
92
- // @ts-ignore
93
- this.client,
94
- // @ts-ignore
95
- this.bindingsHolder,
96
- );
97
- if (isRaw) return isRaw;
98
- let columns: any[] = [];
99
- const values: any[] = [];
100
- if (!Array.isArray(data)) data = data ? [data] : [];
101
- let i = -1;
102
- while (++i < data.length) {
103
- if (data[i] == null) break;
104
- if (i === 0) 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
-
130
- update() {
131
- // @ts-ignore
132
- const withSQL = this.with();
133
- // @ts-ignore
134
- const updates = this._prepUpdate(this.single.update);
135
- // @ts-ignore
136
- const where = this.where();
137
- // @ts-ignore
138
- const order = this.order();
139
- // @ts-ignore
140
- const limit = this.limit();
141
- // @ts-ignore
142
- const { returning } = this.single;
143
- // @ts-ignore
144
- const values = Object.values(this.single.update)
145
- .map((a) => `${a}`)
146
- .join(", ");
147
-
148
- // @ts-ignore
149
- const moreWheres =
150
- // @ts-ignore
151
- this.grouped.where && this.grouped.where.length > 0
152
- ? // @ts-ignore
153
- this.grouped.where.map((w) => {
154
- // @ts-ignore
155
- if (this.single.update.hasOwnProperty(w.column)) return;
156
- if (!w.value) return;
157
- return `"${w.column}" ${w.not ? "!" : ""}${w.operator} ${w.value}`;
158
- })
159
- : [];
160
-
161
- let selectReturning = returning
162
- ? `select ${returning.map((a) => `"${a}"`).join(", ")} from ${
163
- // @ts-ignore
164
- this.tableName
165
- // @ts-ignore
166
- } where ${Object.entries(this.single.update)
167
- .map(([key, value]) => `"${key}" = '${value}'`)
168
- .join(" and ")}${moreWheres.length > 0 && " and "}${moreWheres.join(
169
- " and ",
170
- )}`
171
- : "";
172
-
173
- const sql =
174
- withSQL +
175
- // @ts-ignore
176
- `update ${this.single.only ? "only " : ""}${this.tableName}` +
177
- " set " +
178
- // @ts-ignore
179
- updates.join(", ") +
180
- (where ? ` ${where}` : "") +
181
- (order ? ` ${order}` : "") +
182
- (limit ? ` ${limit}` : "");
183
-
184
- return { sql, returning, selectReturning };
185
- }
186
-
187
- _returning(method, value, withTrigger) {
188
- // currently a placeholder in case I need to update return values
189
- switch (method) {
190
- case "update":
191
- case "insert":
192
- return value
193
- ? // @ts-ignore
194
- `${withTrigger ? " into #out" : ""}`
195
- : "";
196
- case "del":
197
- return value
198
- ? // @ts-ignore
199
- `${withTrigger ? " into #out" : ""}`
200
- : "";
201
- case "rowcount":
202
- return value ? "select @@rowcount" : "";
203
- }
204
- }
205
-
206
- columnizeWithPrefix(prefix, target) {
207
- const columns = typeof target === "string" ? [target] : target;
208
- let str = "",
209
- i = -1;
210
- while (++i < columns.length) {
211
- if (i > 0) str += ", ";
212
- // @ts-ignore
213
- str += prefix + this.wrap(columns[i]);
214
- }
215
- return str;
216
- }
217
- }
218
-
219
- export default IBMiQueryCompiler;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- // @ts-ignore
19
- var columncompiler_1 = require("knex/lib/schema/columncompiler");
20
- var IBMiColumnCompiler = /** @class */ (function (_super) {
21
- __extends(IBMiColumnCompiler, _super);
22
- function IBMiColumnCompiler() {
23
- return _super !== null && _super.apply(this, arguments) || this;
24
- }
25
- IBMiColumnCompiler.prototype.increments = function (options) {
26
- if (options === void 0) { options = { primaryKey: true }; }
27
- return ("int not null generated always as identity (start with 1, increment by 1)" +
28
- // @ts-ignore
29
- (this.tableCompiler._canBeAddPrimaryKey(options) ? " primary key" : ""));
30
- };
31
- return IBMiColumnCompiler;
32
- }(columncompiler_1.default));
33
- exports.default = IBMiColumnCompiler;
@@ -1,14 +0,0 @@
1
- // @ts-ignore
2
- import ColumnCompiler from "knex/lib/schema/columncompiler";
3
-
4
- class IBMiColumnCompiler extends ColumnCompiler {
5
- increments(options = { primaryKey: true }) {
6
- return (
7
- "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
- }
13
-
14
- export default IBMiColumnCompiler;
@@ -1,67 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- // @ts-ignore
19
- var compiler_1 = require("knex/lib/schema/compiler");
20
- var IBMiSchemaCompiler = /** @class */ (function (_super) {
21
- __extends(IBMiSchemaCompiler, _super);
22
- function IBMiSchemaCompiler() {
23
- return _super !== null && _super.apply(this, arguments) || this;
24
- }
25
- IBMiSchemaCompiler.prototype.hasTable = function (tableName) {
26
- // @ts-ignore
27
- var formattedTable = this.client.parameter(
28
- // @ts-ignore
29
- prefixedTableName(this.schema, tableName),
30
- // @ts-ignore
31
- this.builder,
32
- // @ts-ignore
33
- this.bindingsHolder);
34
- var bindings = [tableName];
35
- var sql = "SELECT TABLE_NAME FROM QSYS2.SYSTABLES " +
36
- "where TYPE = 'T' and TABLE_NAME = ".concat(formattedTable);
37
- // @ts-ignore
38
- if (this.schema) {
39
- sql += " and TABLE_SCHEMA = ?";
40
- // @ts-ignore
41
- bindings.push(this.schema);
42
- }
43
- // @ts-ignore
44
- this.pushQuery({
45
- sql: sql,
46
- bindings: bindings,
47
- output: function (resp) {
48
- return resp.rowCount > 0;
49
- },
50
- });
51
- };
52
- IBMiSchemaCompiler.prototype.toSQL = function () {
53
- // @ts-ignore
54
- var sequence = this.builder._sequence;
55
- for (var i = 0, l = sequence.length; i < l; i++) {
56
- var query = sequence[i];
57
- this[query.method].apply(this, query.args);
58
- }
59
- // @ts-ignore
60
- return this.sequence;
61
- };
62
- return IBMiSchemaCompiler;
63
- }(compiler_1.default));
64
- function prefixedTableName(prefix, table) {
65
- return prefix ? "".concat(prefix, ".").concat(table) : table;
66
- }
67
- exports.default = IBMiSchemaCompiler;
@@ -1,52 +0,0 @@
1
- // @ts-ignore
2
- import SchemaCompiler from "knex/lib/schema/compiler";
3
-
4
- class IBMiSchemaCompiler extends SchemaCompiler {
5
- hasTable(tableName) {
6
- // @ts-ignore
7
- const formattedTable = this.client.parameter(
8
- // @ts-ignore
9
- prefixedTableName(this.schema, tableName),
10
- // @ts-ignore
11
- this.builder,
12
- // @ts-ignore
13
- this.bindingsHolder,
14
- );
15
- const bindings = [tableName];
16
- let sql =
17
- `select TABLE_NAME from QSYS2.SYSTABLES ` +
18
- `where TYPE = 'T' and TABLE_NAME = ${formattedTable}`;
19
- // @ts-ignore
20
- if (this.schema) {
21
- sql += " and TABLE_SCHEMA = ?";
22
- // @ts-ignore
23
- bindings.push(this.schema);
24
- }
25
-
26
- // @ts-ignore
27
- this.pushQuery({
28
- sql,
29
- bindings,
30
- output: (resp) => {
31
- return resp.rowCount > 0;
32
- },
33
- });
34
- }
35
-
36
- toSQL() {
37
- // @ts-ignore
38
- const sequence = this.builder._sequence;
39
- for (let i = 0, l = sequence.length; i < l; i++) {
40
- const query = sequence[i];
41
- this[query.method].apply(this, query.args);
42
- }
43
- // @ts-ignore
44
- return this.sequence;
45
- }
46
- }
47
-
48
- function prefixedTableName(prefix, table) {
49
- return prefix ? `${prefix}.${table}` : table;
50
- }
51
-
52
- export default IBMiSchemaCompiler;