tspace-mysql 1.8.2 → 1.8.3
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/README.md +76 -11
- package/build/lib/core/Abstracts/AbstractDB.d.ts +2 -2
- package/build/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/build/lib/core/Builder.d.ts +26 -26
- package/build/lib/core/Builder.js +1092 -1014
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/DB.d.ts +38 -38
- package/build/lib/core/DB.js +119 -115
- package/build/lib/core/DB.js.map +1 -1
- package/build/lib/core/Handlers/Relation.js +9 -7
- package/build/lib/core/Handlers/Relation.js.map +1 -1
- package/build/lib/core/Model.d.ts +138 -88
- package/build/lib/core/Model.js +1403 -1196
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/core/Nest/index.d.ts +3 -1
- package/build/lib/core/Nest/index.js +4 -2
- package/build/lib/core/Nest/index.js.map +1 -1
- package/build/lib/{connection/index.d.ts → core/Pool.d.ts} +2 -1
- package/build/lib/{connection/index.js → core/Pool.js} +74 -6
- package/build/lib/core/Pool.js.map +1 -0
- package/build/lib/core/Repository.d.ts +16 -16
- package/build/lib/core/Repository.js +73 -45
- package/build/lib/core/Repository.js.map +1 -1
- package/build/lib/core/index.d.ts +12 -12
- package/build/lib/core/index.js +3 -3
- package/build/lib/core/index.js.map +1 -1
- package/build/lib/types/index.d.ts +19 -3
- package/build/tests/01-Pool.test.js +0 -10
- package/build/tests/01-Pool.test.js.map +1 -1
- package/package.json +1 -1
- package/build/lib/connection/index.js.map +0 -1
|
@@ -27,7 +27,7 @@ const constants_1 = require("../constants");
|
|
|
27
27
|
const DB_1 = require("./DB");
|
|
28
28
|
const State_1 = require("./Handlers/State");
|
|
29
29
|
const Join_1 = require("./Join");
|
|
30
|
-
const
|
|
30
|
+
const Pool_1 = require("./Pool");
|
|
31
31
|
class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
32
32
|
constructor() {
|
|
33
33
|
super();
|
|
@@ -56,23 +56,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
56
56
|
*/
|
|
57
57
|
unset(options) {
|
|
58
58
|
if ((options === null || options === void 0 ? void 0 : options.select) != null && options.select)
|
|
59
|
-
this.$state.set(
|
|
59
|
+
this.$state.set("SELECT", []);
|
|
60
60
|
if ((options === null || options === void 0 ? void 0 : options.join) != null && options.join)
|
|
61
|
-
this.$state.set(
|
|
61
|
+
this.$state.set("JOIN", []);
|
|
62
62
|
if ((options === null || options === void 0 ? void 0 : options.where) != null && options.where)
|
|
63
|
-
this.$state.set(
|
|
63
|
+
this.$state.set("WHERE", []);
|
|
64
64
|
if ((options === null || options === void 0 ? void 0 : options.groupBy) != null && options.groupBy)
|
|
65
|
-
this.$state.set(
|
|
65
|
+
this.$state.set("GROUP_BY", []);
|
|
66
66
|
if ((options === null || options === void 0 ? void 0 : options.having) != null && options.having)
|
|
67
|
-
this.$state.set(
|
|
67
|
+
this.$state.set("HAVING", "");
|
|
68
68
|
if ((options === null || options === void 0 ? void 0 : options.orderBy) != null && options.orderBy)
|
|
69
|
-
this.$state.set(
|
|
69
|
+
this.$state.set("ORDER_BY", []);
|
|
70
70
|
if ((options === null || options === void 0 ? void 0 : options.limit) != null && options.limit)
|
|
71
|
-
this.$state.set(
|
|
71
|
+
this.$state.set("LIMIT", "");
|
|
72
72
|
if ((options === null || options === void 0 ? void 0 : options.offset) != null && options.offset)
|
|
73
|
-
this.$state.set(
|
|
73
|
+
this.$state.set("OFFSET", "");
|
|
74
74
|
if ((options === null || options === void 0 ? void 0 : options.alias) != null && options.alias)
|
|
75
|
-
this.$state.set(
|
|
75
|
+
this.$state.set("RAW_ALIAS", "");
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
@@ -82,9 +82,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
82
82
|
*/
|
|
83
83
|
CTEs(as, callback) {
|
|
84
84
|
const query = callback(new DB_1.DB().from(this.getTableName()));
|
|
85
|
-
this.$state.set(
|
|
86
|
-
...this.$state.get(
|
|
87
|
-
`${as} AS (${query.toSQL()})
|
|
85
|
+
this.$state.set("CTE", [
|
|
86
|
+
...this.$state.get("CTE"),
|
|
87
|
+
`${as} AS (${query.toSQL()})`,
|
|
88
88
|
]);
|
|
89
89
|
return this;
|
|
90
90
|
}
|
|
@@ -94,7 +94,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
94
94
|
* @returns {string} return sql query
|
|
95
95
|
*/
|
|
96
96
|
getQueries() {
|
|
97
|
-
return this.$state.get(
|
|
97
|
+
return this.$state.get("QUERIES");
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
100
|
* The 'distinct' method is used to apply the DISTINCT keyword to a database query.
|
|
@@ -103,7 +103,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
103
103
|
* @returns {this} this
|
|
104
104
|
*/
|
|
105
105
|
distinct() {
|
|
106
|
-
this.$state.set(
|
|
106
|
+
this.$state.set("DISTINCT", true);
|
|
107
107
|
return this;
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
@@ -115,22 +115,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
115
115
|
*/
|
|
116
116
|
select(...columns) {
|
|
117
117
|
if (!columns.length) {
|
|
118
|
-
this.$state.set(
|
|
118
|
+
this.$state.set("SELECT", ["*"]);
|
|
119
119
|
return this;
|
|
120
120
|
}
|
|
121
121
|
let select = columns.map((column) => {
|
|
122
|
-
if (column.includes(this.$constants(
|
|
123
|
-
return column === null || column === void 0 ? void 0 : column.replace(this.$constants(
|
|
122
|
+
if (column.includes(this.$constants("RAW"))) {
|
|
123
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "").replace(/'/g, "");
|
|
124
124
|
}
|
|
125
125
|
return this.bindColumn(column);
|
|
126
126
|
});
|
|
127
|
-
select = [...this.$state.get(
|
|
128
|
-
if (this.$state.get(
|
|
129
|
-
select[0] = String(select[0]).includes(this.$constants(
|
|
127
|
+
select = [...this.$state.get("SELECT"), ...select];
|
|
128
|
+
if (this.$state.get("DISTINCT") && select.length) {
|
|
129
|
+
select[0] = String(select[0]).includes(this.$constants("DISTINCT"))
|
|
130
130
|
? select[0]
|
|
131
|
-
: `${this.$constants(
|
|
131
|
+
: `${this.$constants("DISTINCT")} ${select[0]}`;
|
|
132
132
|
}
|
|
133
|
-
this.$state.set(
|
|
133
|
+
this.$state.set("SELECT", select);
|
|
134
134
|
return this;
|
|
135
135
|
}
|
|
136
136
|
/**
|
|
@@ -146,21 +146,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
146
146
|
if (!columns.length)
|
|
147
147
|
return this;
|
|
148
148
|
let select = columns.map((column) => {
|
|
149
|
-
if (column ===
|
|
149
|
+
if (column === "*")
|
|
150
150
|
return column;
|
|
151
|
-
if (column.includes(
|
|
152
|
-
return column.replace(
|
|
153
|
-
if (column.includes(this.$constants(
|
|
154
|
-
return column === null || column === void 0 ? void 0 : column.replace(this.$constants(
|
|
151
|
+
if (column.includes("`*`"))
|
|
152
|
+
return column.replace("`*`", "*");
|
|
153
|
+
if (column.includes(this.$constants("RAW")))
|
|
154
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "").replace(/'/g, "");
|
|
155
155
|
return column;
|
|
156
156
|
});
|
|
157
|
-
select = [...this.$state.get(
|
|
158
|
-
if (this.$state.get(
|
|
159
|
-
select[0] = String(select[0]).includes(this.$constants(
|
|
157
|
+
select = [...this.$state.get("SELECT"), ...select];
|
|
158
|
+
if (this.$state.get("DISTINCT") && select.length) {
|
|
159
|
+
select[0] = String(select[0]).includes(this.$constants("DISTINCT"))
|
|
160
160
|
? select[0]
|
|
161
|
-
: `${this.$constants(
|
|
161
|
+
: `${this.$constants("DISTINCT")} ${select[0]}`;
|
|
162
162
|
}
|
|
163
|
-
this.$state.set(
|
|
163
|
+
this.$state.set("SELECT", select);
|
|
164
164
|
return this;
|
|
165
165
|
}
|
|
166
166
|
/**
|
|
@@ -169,7 +169,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
169
169
|
* @returns {this} this
|
|
170
170
|
*/
|
|
171
171
|
select1() {
|
|
172
|
-
this.$state.set(
|
|
172
|
+
this.$state.set("SELECT", [..."1"]);
|
|
173
173
|
return this;
|
|
174
174
|
}
|
|
175
175
|
/**
|
|
@@ -188,10 +188,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
188
188
|
maping = [...maping, `'${key}'`, this.bindColumn(value)];
|
|
189
189
|
}
|
|
190
190
|
const json = [
|
|
191
|
-
`${this.$constants(
|
|
192
|
-
`${this.$constants(
|
|
193
|
-
].join(
|
|
194
|
-
this.$state.set(
|
|
191
|
+
`${this.$constants("JSON_OBJECT")}(${maping.join(", ")})`,
|
|
192
|
+
`${this.$constants("AS")} \`${alias}\``,
|
|
193
|
+
].join(" ");
|
|
194
|
+
this.$state.set("SELECT", [...this.$state.get("SELECT"), json]);
|
|
195
195
|
return this;
|
|
196
196
|
}
|
|
197
197
|
/**
|
|
@@ -208,22 +208,26 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
208
208
|
let maping = [];
|
|
209
209
|
for (const [key, value] of Object.entries(object)) {
|
|
210
210
|
if (/\./.test(value)) {
|
|
211
|
-
const [table, c] = value.split(
|
|
211
|
+
const [table, c] = value.split(".");
|
|
212
212
|
maping = [...maping, `'${key}'`, `\`${table}\`.\`${c}\``];
|
|
213
213
|
continue;
|
|
214
214
|
}
|
|
215
|
-
maping = [
|
|
215
|
+
maping = [
|
|
216
|
+
...maping,
|
|
217
|
+
`'${key}'`,
|
|
218
|
+
`\`${this.getTableName()}\`.\`${value}\``,
|
|
219
|
+
];
|
|
216
220
|
}
|
|
217
221
|
const json = `
|
|
218
|
-
${this.$constants(
|
|
219
|
-
${this.$constants(
|
|
220
|
-
${this.$constants(
|
|
221
|
-
${this.$constants(
|
|
222
|
+
${this.$constants("CASE")}
|
|
223
|
+
${this.$constants("WHEN")} COUNT(${Object.values(maping)[1]}) = 0 ${this.$constants("THEN")} ${this.$constants("JSON_ARRAY")}()
|
|
224
|
+
${this.$constants("ELSE")} ${this.$constants("JSON_ARRAYAGG")}(
|
|
225
|
+
${this.$constants("JSON_OBJECT")}(${maping.join(" , ")})
|
|
222
226
|
)
|
|
223
|
-
${this.$constants(
|
|
224
|
-
${this.$constants(
|
|
227
|
+
${this.$constants("END")}
|
|
228
|
+
${this.$constants("AS")} \`${alias}\`
|
|
225
229
|
`;
|
|
226
|
-
this.$state.set(
|
|
230
|
+
this.$state.set("SELECT", [...this.$state.get("SELECT"), json]);
|
|
227
231
|
return this;
|
|
228
232
|
}
|
|
229
233
|
/**
|
|
@@ -233,7 +237,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
233
237
|
* @returns {this} this
|
|
234
238
|
*/
|
|
235
239
|
table(table) {
|
|
236
|
-
this.$state.set(
|
|
240
|
+
this.$state.set("TABLE_NAME", `\`${table}\``);
|
|
237
241
|
return this;
|
|
238
242
|
}
|
|
239
243
|
/**
|
|
@@ -243,7 +247,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
243
247
|
* @returns {this} this
|
|
244
248
|
*/
|
|
245
249
|
from(table) {
|
|
246
|
-
this.$state.set(
|
|
250
|
+
this.$state.set("TABLE_NAME", `\`${table}\``);
|
|
247
251
|
return this;
|
|
248
252
|
}
|
|
249
253
|
/**
|
|
@@ -254,9 +258,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
254
258
|
* @returns {this} this
|
|
255
259
|
*/
|
|
256
260
|
fromRaw(alias, from) {
|
|
257
|
-
this.$state.set(
|
|
261
|
+
this.$state.set("ALIAS", alias);
|
|
258
262
|
if (from) {
|
|
259
|
-
this.$state.set(
|
|
263
|
+
this.$state.set("RAW_ALIAS", from);
|
|
260
264
|
}
|
|
261
265
|
return this;
|
|
262
266
|
}
|
|
@@ -268,9 +272,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
268
272
|
* @returns {this} this
|
|
269
273
|
*/
|
|
270
274
|
alias(alias, from) {
|
|
271
|
-
this.$state.set(
|
|
275
|
+
this.$state.set("ALIAS", alias);
|
|
272
276
|
if (from) {
|
|
273
|
-
this.$state.set(
|
|
277
|
+
this.$state.set("RAW_ALIAS", from);
|
|
274
278
|
}
|
|
275
279
|
return this;
|
|
276
280
|
}
|
|
@@ -282,9 +286,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
282
286
|
* @returns {this} this
|
|
283
287
|
*/
|
|
284
288
|
as(alias, from) {
|
|
285
|
-
this.$state.set(
|
|
289
|
+
this.$state.set("ALIAS", alias);
|
|
286
290
|
if (from) {
|
|
287
|
-
this.$state.set(
|
|
291
|
+
this.$state.set("RAW_ALIAS", from);
|
|
288
292
|
}
|
|
289
293
|
return this;
|
|
290
294
|
}
|
|
@@ -296,14 +300,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
296
300
|
*/
|
|
297
301
|
sleep(second) {
|
|
298
302
|
const sql = `(SELECT SLEEP(${second}) as sleep)`;
|
|
299
|
-
this.$state.set(
|
|
300
|
-
...this.$state.get(
|
|
301
|
-
|
|
302
|
-
`${
|
|
303
|
-
`${this.$constants(
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
303
|
+
this.$state.set("JOIN", [
|
|
304
|
+
...this.$state.get("JOIN"),
|
|
305
|
+
[
|
|
306
|
+
`${this.$constants("INNER_JOIN")}`,
|
|
307
|
+
`${sql} ${this.$constants("AS")} temp`,
|
|
308
|
+
`${this.$constants("ON")}`,
|
|
309
|
+
`1=1`,
|
|
310
|
+
].join(" "),
|
|
307
311
|
]);
|
|
308
312
|
return this;
|
|
309
313
|
}
|
|
@@ -314,7 +318,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
314
318
|
* @returns {this} this
|
|
315
319
|
*/
|
|
316
320
|
returnType(type) {
|
|
317
|
-
this.$state.set(
|
|
321
|
+
this.$state.set("RETURN_TYPE", type);
|
|
318
322
|
return this;
|
|
319
323
|
}
|
|
320
324
|
/**
|
|
@@ -326,7 +330,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
326
330
|
* @returns {this}
|
|
327
331
|
*/
|
|
328
332
|
pluck(column) {
|
|
329
|
-
this.$state.set(
|
|
333
|
+
this.$state.set("PLUCK", column);
|
|
330
334
|
return this;
|
|
331
335
|
}
|
|
332
336
|
/**
|
|
@@ -339,11 +343,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
339
343
|
except(...columns) {
|
|
340
344
|
if (!columns.length)
|
|
341
345
|
return this;
|
|
342
|
-
const exceptColumns = this.$state.get(
|
|
343
|
-
this.$state.set(
|
|
344
|
-
...columns,
|
|
345
|
-
...exceptColumns
|
|
346
|
-
]);
|
|
346
|
+
const exceptColumns = this.$state.get("EXCEPTS");
|
|
347
|
+
this.$state.set("EXCEPTS", [...columns, ...exceptColumns]);
|
|
347
348
|
return this;
|
|
348
349
|
}
|
|
349
350
|
/**
|
|
@@ -352,7 +353,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
352
353
|
* @returns {this} this
|
|
353
354
|
*/
|
|
354
355
|
exceptTimestamp() {
|
|
355
|
-
this.$state.set(
|
|
356
|
+
this.$state.set("EXCEPTS", ["created_at", "updated_at"]);
|
|
356
357
|
return this;
|
|
357
358
|
}
|
|
358
359
|
/**
|
|
@@ -361,7 +362,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
361
362
|
* @returns {this} this
|
|
362
363
|
*/
|
|
363
364
|
void() {
|
|
364
|
-
this.$state.set(
|
|
365
|
+
this.$state.set("VOID", true);
|
|
365
366
|
return this;
|
|
366
367
|
}
|
|
367
368
|
/**
|
|
@@ -373,7 +374,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
373
374
|
* @returns {this} this
|
|
374
375
|
*/
|
|
375
376
|
only(...columns) {
|
|
376
|
-
this.$state.set(
|
|
377
|
+
this.$state.set("ONLY", columns);
|
|
377
378
|
return this;
|
|
378
379
|
}
|
|
379
380
|
/**
|
|
@@ -386,7 +387,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
386
387
|
* @returns {this} this
|
|
387
388
|
*/
|
|
388
389
|
chunk(chunk) {
|
|
389
|
-
this.$state.set(
|
|
390
|
+
this.$state.set("CHUNK", chunk);
|
|
390
391
|
return this;
|
|
391
392
|
}
|
|
392
393
|
/**
|
|
@@ -414,7 +415,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
414
415
|
* @returns {this}
|
|
415
416
|
*/
|
|
416
417
|
where(column, operator, value) {
|
|
417
|
-
if (typeof column ===
|
|
418
|
+
if (typeof column === "object") {
|
|
418
419
|
return this.whereObject(column);
|
|
419
420
|
}
|
|
420
421
|
[value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
|
|
@@ -426,14 +427,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
426
427
|
if (Array.isArray(value)) {
|
|
427
428
|
return this.whereIn(column, value);
|
|
428
429
|
}
|
|
429
|
-
this.$state.set(
|
|
430
|
-
...this.$state.get(
|
|
430
|
+
this.$state.set("WHERE", [
|
|
431
|
+
...this.$state.get("WHERE"),
|
|
431
432
|
[
|
|
432
|
-
this.$state.get(
|
|
433
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
433
434
|
`${this.bindColumn(String(column))}`,
|
|
434
435
|
`${operator}`,
|
|
435
|
-
`${this._checkValueHasRaw(value)}
|
|
436
|
-
].join(
|
|
436
|
+
`${this._checkValueHasRaw(value)}`,
|
|
437
|
+
].join(" "),
|
|
437
438
|
]);
|
|
438
439
|
return this;
|
|
439
440
|
}
|
|
@@ -458,14 +459,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
458
459
|
if (Array.isArray(value)) {
|
|
459
460
|
return this.orWhereIn(column, value);
|
|
460
461
|
}
|
|
461
|
-
this.$state.set(
|
|
462
|
-
...this.$state.get(
|
|
462
|
+
this.$state.set("WHERE", [
|
|
463
|
+
...this.$state.get("WHERE"),
|
|
463
464
|
[
|
|
464
|
-
this.$state.get(
|
|
465
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
465
466
|
`${this.bindColumn(String(column))}`,
|
|
466
467
|
`${operator}`,
|
|
467
|
-
`${this._checkValueHasRaw(value)}
|
|
468
|
-
].join(
|
|
468
|
+
`${this._checkValueHasRaw(value)}`,
|
|
469
|
+
].join(" "),
|
|
469
470
|
]);
|
|
470
471
|
return this;
|
|
471
472
|
}
|
|
@@ -478,14 +479,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
478
479
|
* @returns {this}
|
|
479
480
|
*/
|
|
480
481
|
whereDay(column, day) {
|
|
481
|
-
this.$state.set(
|
|
482
|
-
...this.$state.get(
|
|
482
|
+
this.$state.set("WHERE", [
|
|
483
|
+
...this.$state.get("WHERE"),
|
|
483
484
|
[
|
|
484
|
-
this.$state.get(
|
|
485
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
485
486
|
`DAY(${this.bindColumn(String(column))})`,
|
|
486
487
|
`=`,
|
|
487
|
-
`'${`00${this.$utils.escape(day)}`.slice(-2)}'
|
|
488
|
-
].join(
|
|
488
|
+
`'${`00${this.$utils.escape(day)}`.slice(-2)}'`,
|
|
489
|
+
].join(" "),
|
|
489
490
|
]);
|
|
490
491
|
return this;
|
|
491
492
|
}
|
|
@@ -498,14 +499,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
498
499
|
* @returns {this}
|
|
499
500
|
*/
|
|
500
501
|
whereMonth(column, month) {
|
|
501
|
-
this.$state.set(
|
|
502
|
-
...this.$state.get(
|
|
502
|
+
this.$state.set("WHERE", [
|
|
503
|
+
...this.$state.get("WHERE"),
|
|
503
504
|
[
|
|
504
|
-
this.$state.get(
|
|
505
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
505
506
|
`MONTH(${this.bindColumn(String(column))})`,
|
|
506
507
|
`=`,
|
|
507
|
-
`'${`00${this.$utils.escape(month)}`.slice(-2)}'
|
|
508
|
-
].join(
|
|
508
|
+
`'${`00${this.$utils.escape(month)}`.slice(-2)}'`,
|
|
509
|
+
].join(" "),
|
|
509
510
|
]);
|
|
510
511
|
return this;
|
|
511
512
|
}
|
|
@@ -518,14 +519,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
518
519
|
* @returns {this}
|
|
519
520
|
*/
|
|
520
521
|
whereYear(column, year) {
|
|
521
|
-
this.$state.set(
|
|
522
|
-
...this.$state.get(
|
|
522
|
+
this.$state.set("WHERE", [
|
|
523
|
+
...this.$state.get("WHERE"),
|
|
523
524
|
[
|
|
524
|
-
this.$state.get(
|
|
525
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
525
526
|
`YEAR(${this.bindColumn(String(column))})`,
|
|
526
527
|
`=`,
|
|
527
|
-
`'${`0000${this.$utils.escape(year)}`.slice(-4)}'
|
|
528
|
-
].join(
|
|
528
|
+
`'${`0000${this.$utils.escape(year)}`.slice(-4)}'`,
|
|
529
|
+
].join(" "),
|
|
529
530
|
]);
|
|
530
531
|
return this;
|
|
531
532
|
}
|
|
@@ -538,12 +539,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
538
539
|
* @returns {this} this
|
|
539
540
|
*/
|
|
540
541
|
whereRaw(sql) {
|
|
541
|
-
this.$state.set(
|
|
542
|
-
...this.$state.get(
|
|
542
|
+
this.$state.set("WHERE", [
|
|
543
|
+
...this.$state.get("WHERE"),
|
|
543
544
|
[
|
|
544
|
-
this.$state.get(
|
|
545
|
-
`${sql}
|
|
546
|
-
].join(
|
|
545
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
546
|
+
`${sql}`,
|
|
547
|
+
].join(" "),
|
|
547
548
|
]);
|
|
548
549
|
return this;
|
|
549
550
|
}
|
|
@@ -556,12 +557,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
556
557
|
* @returns {this} this
|
|
557
558
|
*/
|
|
558
559
|
orWhereRaw(sql) {
|
|
559
|
-
this.$state.set(
|
|
560
|
-
...this.$state.get(
|
|
560
|
+
this.$state.set("WHERE", [
|
|
561
|
+
...this.$state.get("WHERE"),
|
|
561
562
|
[
|
|
562
|
-
this.$state.get(
|
|
563
|
-
`${sql}
|
|
564
|
-
].join(
|
|
563
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
564
|
+
`${sql}`,
|
|
565
|
+
].join(" "),
|
|
565
566
|
]);
|
|
566
567
|
return this;
|
|
567
568
|
}
|
|
@@ -576,7 +577,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
576
577
|
*/
|
|
577
578
|
whereObject(columns) {
|
|
578
579
|
for (const column in columns) {
|
|
579
|
-
const operator =
|
|
580
|
+
const operator = "=";
|
|
580
581
|
const value = this.$utils.escape(columns[column]);
|
|
581
582
|
const useOp = this._checkValueHasOp(value);
|
|
582
583
|
if (useOp == null) {
|
|
@@ -584,49 +585,49 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
584
585
|
continue;
|
|
585
586
|
}
|
|
586
587
|
switch (useOp.op) {
|
|
587
|
-
case
|
|
588
|
-
this.whereIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(
|
|
588
|
+
case "IN": {
|
|
589
|
+
this.whereIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(","));
|
|
589
590
|
break;
|
|
590
591
|
}
|
|
591
|
-
case
|
|
592
|
-
this.orWhereIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(
|
|
592
|
+
case "|IN": {
|
|
593
|
+
this.orWhereIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(","));
|
|
593
594
|
break;
|
|
594
595
|
}
|
|
595
|
-
case
|
|
596
|
+
case "QUERY": {
|
|
596
597
|
this.whereSubQuery(column, useOp.value);
|
|
597
598
|
break;
|
|
598
599
|
}
|
|
599
|
-
case
|
|
600
|
+
case "!QUERY": {
|
|
600
601
|
this.orWhereSubQuery(column, useOp.value);
|
|
601
602
|
break;
|
|
602
603
|
}
|
|
603
|
-
case
|
|
604
|
-
this.whereNotIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(
|
|
604
|
+
case "NOT IN": {
|
|
605
|
+
this.whereNotIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(","));
|
|
605
606
|
break;
|
|
606
607
|
}
|
|
607
|
-
case
|
|
608
|
-
this.orWhereNotIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(
|
|
608
|
+
case "|NOT IN": {
|
|
609
|
+
this.orWhereNotIn(column, Array.isArray(useOp.value) ? useOp.value : useOp.value.split(","));
|
|
609
610
|
break;
|
|
610
611
|
}
|
|
611
|
-
case
|
|
612
|
+
case "IS NULL": {
|
|
612
613
|
this.whereNull(column);
|
|
613
614
|
break;
|
|
614
615
|
}
|
|
615
|
-
case
|
|
616
|
+
case "|IS NULL": {
|
|
616
617
|
this.orWhereNull(column);
|
|
617
618
|
break;
|
|
618
619
|
}
|
|
619
|
-
case
|
|
620
|
+
case "IS NOT NULL": {
|
|
620
621
|
this.whereNotNull(column);
|
|
621
622
|
break;
|
|
622
623
|
}
|
|
623
|
-
case
|
|
624
|
+
case "|IS NOT NULL": {
|
|
624
625
|
this.orWhereNotNull(column);
|
|
625
626
|
break;
|
|
626
627
|
}
|
|
627
628
|
default: {
|
|
628
|
-
if (useOp.op.includes(
|
|
629
|
-
this.orWhere(column, useOp.op.replace(
|
|
629
|
+
if (useOp.op.includes("|")) {
|
|
630
|
+
this.orWhere(column, useOp.op.replace("|", ""), useOp.value);
|
|
630
631
|
break;
|
|
631
632
|
}
|
|
632
633
|
this.where(column, useOp.op, useOp.value);
|
|
@@ -649,14 +650,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
649
650
|
whereJSON(column, { key, value, operator }) {
|
|
650
651
|
value = this.$utils.escape(value);
|
|
651
652
|
value = this.$utils.covertBooleanToNumber(value);
|
|
652
|
-
this.$state.set(
|
|
653
|
-
...this.$state.get(
|
|
653
|
+
this.$state.set("WHERE", [
|
|
654
|
+
...this.$state.get("WHERE"),
|
|
654
655
|
[
|
|
655
|
-
this.$state.get(
|
|
656
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
656
657
|
`${this.bindColumn(column)}->>'$.${key}'`,
|
|
657
658
|
`${operator == null ? "=" : operator.toLocaleUpperCase()}`,
|
|
658
|
-
`${this._checkValueHasRaw(value)}
|
|
659
|
-
].join(
|
|
659
|
+
`${this._checkValueHasRaw(value)}`,
|
|
660
|
+
].join(" "),
|
|
660
661
|
]);
|
|
661
662
|
return this;
|
|
662
663
|
}
|
|
@@ -683,13 +684,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
683
684
|
* @returns {this}
|
|
684
685
|
*/
|
|
685
686
|
whereExists(sql) {
|
|
686
|
-
this.$state.set(
|
|
687
|
-
...this.$state.get(
|
|
687
|
+
this.$state.set("WHERE", [
|
|
688
|
+
...this.$state.get("WHERE"),
|
|
688
689
|
[
|
|
689
|
-
this.$state.get(
|
|
690
|
-
`${this.$constants(
|
|
691
|
-
`(${sql})
|
|
692
|
-
].join(
|
|
690
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
691
|
+
`${this.$constants("EXISTS")}`,
|
|
692
|
+
`(${sql})`,
|
|
693
|
+
].join(" "),
|
|
693
694
|
]);
|
|
694
695
|
return this;
|
|
695
696
|
}
|
|
@@ -702,13 +703,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
702
703
|
* @returns {this}
|
|
703
704
|
*/
|
|
704
705
|
whereNotExists(sql) {
|
|
705
|
-
this.$state.set(
|
|
706
|
-
...this.$state.get(
|
|
706
|
+
this.$state.set("WHERE", [
|
|
707
|
+
...this.$state.get("WHERE"),
|
|
707
708
|
[
|
|
708
|
-
this.$state.get(
|
|
709
|
-
`${this.$constants(
|
|
710
|
-
`(${sql})
|
|
711
|
-
].join(
|
|
709
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
710
|
+
`${this.$constants("NOT")} ${this.$constants("EXISTS")}`,
|
|
711
|
+
`(${sql})`,
|
|
712
|
+
].join(" "),
|
|
712
713
|
]);
|
|
713
714
|
return this;
|
|
714
715
|
}
|
|
@@ -717,13 +718,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
717
718
|
* @param {number} id
|
|
718
719
|
* @returns {this} this
|
|
719
720
|
*/
|
|
720
|
-
whereId(id, column =
|
|
721
|
-
this.$state.set(
|
|
722
|
-
...this.$state.get(
|
|
721
|
+
whereId(id, column = "id") {
|
|
722
|
+
this.$state.set("WHERE", [
|
|
723
|
+
...this.$state.get("WHERE"),
|
|
723
724
|
[
|
|
724
|
-
this.$state.get(
|
|
725
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
725
726
|
`${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
|
|
726
|
-
].join(
|
|
727
|
+
].join(" "),
|
|
727
728
|
]);
|
|
728
729
|
return this;
|
|
729
730
|
}
|
|
@@ -733,13 +734,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
733
734
|
* @returns {this}
|
|
734
735
|
*/
|
|
735
736
|
whereEmail(email) {
|
|
736
|
-
const column =
|
|
737
|
-
this.$state.set(
|
|
738
|
-
...this.$state.get(
|
|
737
|
+
const column = "email";
|
|
738
|
+
this.$state.set("WHERE", [
|
|
739
|
+
...this.$state.get("WHERE"),
|
|
739
740
|
[
|
|
740
|
-
this.$state.get(
|
|
741
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
741
742
|
`${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
|
|
742
|
-
].join(
|
|
743
|
+
].join(" "),
|
|
743
744
|
]);
|
|
744
745
|
return this;
|
|
745
746
|
}
|
|
@@ -749,13 +750,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
749
750
|
* @param {string?} column custom it *if column is not user_id
|
|
750
751
|
* @returns {this}
|
|
751
752
|
*/
|
|
752
|
-
whereUser(userId, column =
|
|
753
|
-
this.$state.set(
|
|
754
|
-
...this.$state.get(
|
|
753
|
+
whereUser(userId, column = "user_id") {
|
|
754
|
+
this.$state.set("WHERE", [
|
|
755
|
+
...this.$state.get("WHERE"),
|
|
755
756
|
[
|
|
756
|
-
this.$state.get(
|
|
757
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
757
758
|
`${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
|
|
758
|
-
].join(
|
|
759
|
+
].join(" "),
|
|
759
760
|
]);
|
|
760
761
|
return this;
|
|
761
762
|
}
|
|
@@ -771,16 +772,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
771
772
|
if (!Array.isArray(array))
|
|
772
773
|
array = [array];
|
|
773
774
|
const values = array.length
|
|
774
|
-
? `${array
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
775
|
+
? `${array
|
|
776
|
+
.map((value) => this._checkValueHasRaw(this.$utils.escape(value)))
|
|
777
|
+
.join(",")}`
|
|
778
|
+
: this.$constants(this.$constants("NULL"));
|
|
779
|
+
this.$state.set("WHERE", [
|
|
780
|
+
...this.$state.get("WHERE"),
|
|
778
781
|
[
|
|
779
|
-
this.$state.get(
|
|
782
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
780
783
|
`${this.bindColumn(column)}`,
|
|
781
|
-
`${this.$constants(
|
|
782
|
-
`(${values})
|
|
783
|
-
].join(
|
|
784
|
+
`${this.$constants("IN")}`,
|
|
785
|
+
`(${values})`,
|
|
786
|
+
].join(" "),
|
|
784
787
|
]);
|
|
785
788
|
return this;
|
|
786
789
|
}
|
|
@@ -796,16 +799,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
796
799
|
if (!Array.isArray(array))
|
|
797
800
|
array = [array];
|
|
798
801
|
const values = array.length
|
|
799
|
-
? `${array
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
802
|
+
? `${array
|
|
803
|
+
.map((value) => this._checkValueHasRaw(this.$utils.escape(value)))
|
|
804
|
+
.join(",")}`
|
|
805
|
+
: this.$constants(this.$constants("NULL"));
|
|
806
|
+
this.$state.set("WHERE", [
|
|
807
|
+
...this.$state.get("WHERE"),
|
|
803
808
|
[
|
|
804
|
-
this.$state.get(
|
|
809
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
805
810
|
`${this.bindColumn(column)}`,
|
|
806
|
-
`${this.$constants(
|
|
807
|
-
`(${values})
|
|
808
|
-
].join(
|
|
811
|
+
`${this.$constants("IN")}`,
|
|
812
|
+
`(${values})`,
|
|
813
|
+
].join(" "),
|
|
809
814
|
]);
|
|
810
815
|
return this;
|
|
811
816
|
}
|
|
@@ -822,15 +827,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
822
827
|
array = [array];
|
|
823
828
|
if (!array.length)
|
|
824
829
|
return this;
|
|
825
|
-
const values = `${array
|
|
826
|
-
|
|
827
|
-
|
|
830
|
+
const values = `${array
|
|
831
|
+
.map((value) => this._checkValueHasRaw(this.$utils.escape(value)))
|
|
832
|
+
.join(",")}`;
|
|
833
|
+
this.$state.set("WHERE", [
|
|
834
|
+
...this.$state.get("WHERE"),
|
|
828
835
|
[
|
|
829
|
-
this.$state.get(
|
|
836
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
830
837
|
`${this.bindColumn(column)}`,
|
|
831
|
-
`${this.$constants(
|
|
832
|
-
`(${values})
|
|
833
|
-
].join(
|
|
838
|
+
`${this.$constants("NOT_IN")}`,
|
|
839
|
+
`(${values})`,
|
|
840
|
+
].join(" "),
|
|
834
841
|
]);
|
|
835
842
|
return this;
|
|
836
843
|
}
|
|
@@ -847,15 +854,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
847
854
|
array = [array];
|
|
848
855
|
if (!array.length)
|
|
849
856
|
return this;
|
|
850
|
-
const values = `${array
|
|
851
|
-
|
|
852
|
-
|
|
857
|
+
const values = `${array
|
|
858
|
+
.map((value) => this._checkValueHasRaw(this.$utils.escape(value)))
|
|
859
|
+
.join(",")}`;
|
|
860
|
+
this.$state.set("WHERE", [
|
|
861
|
+
...this.$state.get("WHERE"),
|
|
853
862
|
[
|
|
854
|
-
this.$state.get(
|
|
863
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
855
864
|
`${this.bindColumn(column)}`,
|
|
856
|
-
`${this.$constants(
|
|
857
|
-
`(${values})
|
|
858
|
-
].join(
|
|
865
|
+
`${this.$constants("NOT_IN")}`,
|
|
866
|
+
`(${values})`,
|
|
867
|
+
].join(" "),
|
|
859
868
|
]);
|
|
860
869
|
return this;
|
|
861
870
|
}
|
|
@@ -870,14 +879,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
870
879
|
* @returns {this}
|
|
871
880
|
*/
|
|
872
881
|
whereSubQuery(column, subQuery) {
|
|
873
|
-
this.$state.set(
|
|
874
|
-
...this.$state.get(
|
|
882
|
+
this.$state.set("WHERE", [
|
|
883
|
+
...this.$state.get("WHERE"),
|
|
875
884
|
[
|
|
876
|
-
this.$state.get(
|
|
885
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
877
886
|
`${this.bindColumn(column)}`,
|
|
878
|
-
`${this.$constants(
|
|
879
|
-
`(${subQuery})
|
|
880
|
-
].join(
|
|
887
|
+
`${this.$constants("IN")}`,
|
|
888
|
+
`(${subQuery})`,
|
|
889
|
+
].join(" "),
|
|
881
890
|
]);
|
|
882
891
|
return this;
|
|
883
892
|
}
|
|
@@ -892,14 +901,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
892
901
|
* @returns {this}
|
|
893
902
|
*/
|
|
894
903
|
whereNotSubQuery(column, subQuery) {
|
|
895
|
-
this.$state.set(
|
|
896
|
-
...this.$state.get(
|
|
904
|
+
this.$state.set("WHERE", [
|
|
905
|
+
...this.$state.get("WHERE"),
|
|
897
906
|
[
|
|
898
|
-
this.$state.get(
|
|
907
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
899
908
|
`${this.bindColumn(column)}`,
|
|
900
|
-
`${this.$constants(
|
|
901
|
-
`(${subQuery})
|
|
902
|
-
].join(
|
|
909
|
+
`${this.$constants("NOT_IN")}`,
|
|
910
|
+
`(${subQuery})`,
|
|
911
|
+
].join(" "),
|
|
903
912
|
]);
|
|
904
913
|
return this;
|
|
905
914
|
}
|
|
@@ -914,14 +923,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
914
923
|
* @returns {this}
|
|
915
924
|
*/
|
|
916
925
|
orWhereSubQuery(column, subQuery) {
|
|
917
|
-
this.$state.set(
|
|
918
|
-
...this.$state.get(
|
|
926
|
+
this.$state.set("WHERE", [
|
|
927
|
+
...this.$state.get("WHERE"),
|
|
919
928
|
[
|
|
920
|
-
this.$state.get(
|
|
929
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
921
930
|
`${this.bindColumn(column)}`,
|
|
922
|
-
`${this.$constants(
|
|
923
|
-
`(${subQuery})
|
|
924
|
-
].join(
|
|
931
|
+
`${this.$constants("IN")}`,
|
|
932
|
+
`(${subQuery})`,
|
|
933
|
+
].join(" "),
|
|
925
934
|
]);
|
|
926
935
|
return this;
|
|
927
936
|
}
|
|
@@ -936,14 +945,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
936
945
|
* @returns {this}
|
|
937
946
|
*/
|
|
938
947
|
orWhereNotSubQuery(column, subQuery) {
|
|
939
|
-
this.$state.set(
|
|
940
|
-
...this.$state.get(
|
|
948
|
+
this.$state.set("WHERE", [
|
|
949
|
+
...this.$state.get("WHERE"),
|
|
941
950
|
[
|
|
942
|
-
this.$state.get(
|
|
951
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
943
952
|
`${this.bindColumn(column)}`,
|
|
944
|
-
`${this.$constants(
|
|
945
|
-
`(${subQuery})
|
|
946
|
-
].join(
|
|
953
|
+
`${this.$constants("NOT_IN")}`,
|
|
954
|
+
`(${subQuery})`,
|
|
955
|
+
].join(" "),
|
|
947
956
|
]);
|
|
948
957
|
return this;
|
|
949
958
|
}
|
|
@@ -959,30 +968,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
959
968
|
if (!Array.isArray(array))
|
|
960
969
|
throw new Error("Value is't array");
|
|
961
970
|
if (!array.length) {
|
|
962
|
-
this.$state.set(
|
|
963
|
-
...this.$state.get(
|
|
971
|
+
this.$state.set("WHERE", [
|
|
972
|
+
...this.$state.get("WHERE"),
|
|
964
973
|
[
|
|
965
|
-
this.$state.get(
|
|
974
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
966
975
|
`${this.bindColumn(column)}`,
|
|
967
|
-
`${this.$constants(
|
|
968
|
-
`${this.$constants(this.$constants(
|
|
969
|
-
`${this.$constants(
|
|
970
|
-
`${this.$constants(this.$constants(
|
|
971
|
-
].join(
|
|
976
|
+
`${this.$constants("BETWEEN")}`,
|
|
977
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
978
|
+
`${this.$constants("AND")}`,
|
|
979
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
980
|
+
].join(" "),
|
|
972
981
|
]);
|
|
973
982
|
return this;
|
|
974
983
|
}
|
|
975
984
|
const [value1, value2] = array;
|
|
976
|
-
this.$state.set(
|
|
977
|
-
...this.$state.get(
|
|
985
|
+
this.$state.set("WHERE", [
|
|
986
|
+
...this.$state.get("WHERE"),
|
|
978
987
|
[
|
|
979
|
-
this.$state.get(
|
|
988
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
980
989
|
`${this.bindColumn(column)}`,
|
|
981
|
-
`${this.$constants(
|
|
990
|
+
`${this.$constants("BETWEEN")}`,
|
|
982
991
|
`${this._checkValueHasRaw(this.$utils.escape(value1))}`,
|
|
983
|
-
`${this.$constants(
|
|
984
|
-
`${this._checkValueHasRaw(this.$utils.escape(value2))}
|
|
985
|
-
].join(
|
|
992
|
+
`${this.$constants("AND")}`,
|
|
993
|
+
`${this._checkValueHasRaw(this.$utils.escape(value2))}`,
|
|
994
|
+
].join(" "),
|
|
986
995
|
]);
|
|
987
996
|
return this;
|
|
988
997
|
}
|
|
@@ -998,30 +1007,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
998
1007
|
if (!Array.isArray(array))
|
|
999
1008
|
throw new Error("Value is't array");
|
|
1000
1009
|
if (!array.length) {
|
|
1001
|
-
this.$state.set(
|
|
1002
|
-
...this.$state.get(
|
|
1010
|
+
this.$state.set("WHERE", [
|
|
1011
|
+
...this.$state.get("WHERE"),
|
|
1003
1012
|
[
|
|
1004
|
-
this.$state.get(
|
|
1013
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1005
1014
|
`${this.bindColumn(column)}`,
|
|
1006
|
-
`${this.$constants(
|
|
1007
|
-
`${this.$constants(this.$constants(
|
|
1008
|
-
`${this.$constants(
|
|
1009
|
-
`${this.$constants(this.$constants(
|
|
1010
|
-
].join(
|
|
1015
|
+
`${this.$constants("BETWEEN")}`,
|
|
1016
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
1017
|
+
`${this.$constants("AND")}`,
|
|
1018
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
1019
|
+
].join(" "),
|
|
1011
1020
|
]);
|
|
1012
1021
|
return this;
|
|
1013
1022
|
}
|
|
1014
1023
|
const [value1, value2] = array;
|
|
1015
|
-
this.$state.set(
|
|
1016
|
-
...this.$state.get(
|
|
1024
|
+
this.$state.set("WHERE", [
|
|
1025
|
+
...this.$state.get("WHERE"),
|
|
1017
1026
|
[
|
|
1018
|
-
this.$state.get(
|
|
1027
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1019
1028
|
`${this.bindColumn(column)}`,
|
|
1020
|
-
`${this.$constants(
|
|
1029
|
+
`${this.$constants("BETWEEN")}`,
|
|
1021
1030
|
`${this._checkValueHasRaw(this.$utils.escape(value1))}`,
|
|
1022
|
-
`${this.$constants(
|
|
1023
|
-
`${this._checkValueHasRaw(this.$utils.escape(value2))}
|
|
1024
|
-
].join(
|
|
1031
|
+
`${this.$constants("AND")}`,
|
|
1032
|
+
`${this._checkValueHasRaw(this.$utils.escape(value2))}`,
|
|
1033
|
+
].join(" "),
|
|
1025
1034
|
]);
|
|
1026
1035
|
return this;
|
|
1027
1036
|
}
|
|
@@ -1037,30 +1046,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1037
1046
|
if (!Array.isArray(array))
|
|
1038
1047
|
throw new Error("Value is't array");
|
|
1039
1048
|
if (!array.length) {
|
|
1040
|
-
this.$state.set(
|
|
1041
|
-
...this.$state.get(
|
|
1049
|
+
this.$state.set("WHERE", [
|
|
1050
|
+
...this.$state.get("WHERE"),
|
|
1042
1051
|
[
|
|
1043
|
-
this.$state.get(
|
|
1052
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1044
1053
|
`${this.bindColumn(column)}`,
|
|
1045
|
-
`${this.$constants(
|
|
1046
|
-
`${this.$constants(this.$constants(
|
|
1047
|
-
`${this.$constants(
|
|
1048
|
-
`${this.$constants(this.$constants(
|
|
1049
|
-
].join(
|
|
1054
|
+
`${this.$constants("NOT_BETWEEN")}`,
|
|
1055
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
1056
|
+
`${this.$constants("AND")}`,
|
|
1057
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
1058
|
+
].join(" "),
|
|
1050
1059
|
]);
|
|
1051
1060
|
return this;
|
|
1052
1061
|
}
|
|
1053
1062
|
const [value1, value2] = array;
|
|
1054
|
-
this.$state.set(
|
|
1055
|
-
...this.$state.get(
|
|
1063
|
+
this.$state.set("WHERE", [
|
|
1064
|
+
...this.$state.get("WHERE"),
|
|
1056
1065
|
[
|
|
1057
|
-
this.$state.get(
|
|
1066
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1058
1067
|
`${this.bindColumn(column)}`,
|
|
1059
|
-
`${this.$constants(
|
|
1068
|
+
`${this.$constants("NOT_BETWEEN")}`,
|
|
1060
1069
|
`${this._checkValueHasRaw(this.$utils.escape(value1))}`,
|
|
1061
|
-
`${this.$constants(
|
|
1062
|
-
`${this._checkValueHasRaw(this.$utils.escape(value2))}
|
|
1063
|
-
].join(
|
|
1070
|
+
`${this.$constants("AND")}`,
|
|
1071
|
+
`${this._checkValueHasRaw(this.$utils.escape(value2))}`,
|
|
1072
|
+
].join(" "),
|
|
1064
1073
|
]);
|
|
1065
1074
|
return this;
|
|
1066
1075
|
}
|
|
@@ -1076,30 +1085,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1076
1085
|
if (!Array.isArray(array))
|
|
1077
1086
|
throw new Error("Value is't array");
|
|
1078
1087
|
if (!array.length) {
|
|
1079
|
-
this.$state.set(
|
|
1080
|
-
...this.$state.get(
|
|
1088
|
+
this.$state.set("WHERE", [
|
|
1089
|
+
...this.$state.get("WHERE"),
|
|
1081
1090
|
[
|
|
1082
|
-
this.$state.get(
|
|
1091
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1083
1092
|
`${this.bindColumn(column)}`,
|
|
1084
|
-
`${this.$constants(
|
|
1085
|
-
`${this.$constants(this.$constants(
|
|
1086
|
-
`${this.$constants(
|
|
1087
|
-
`${this.$constants(this.$constants(
|
|
1088
|
-
].join(
|
|
1093
|
+
`${this.$constants("NOT_BETWEEN")}`,
|
|
1094
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
1095
|
+
`${this.$constants("AND")}`,
|
|
1096
|
+
`${this.$constants(this.$constants("NULL"))}`,
|
|
1097
|
+
].join(" "),
|
|
1089
1098
|
]);
|
|
1090
1099
|
return this;
|
|
1091
1100
|
}
|
|
1092
1101
|
const [value1, value2] = array;
|
|
1093
|
-
this.$state.set(
|
|
1094
|
-
...this.$state.get(
|
|
1102
|
+
this.$state.set("WHERE", [
|
|
1103
|
+
...this.$state.get("WHERE"),
|
|
1095
1104
|
[
|
|
1096
|
-
this.$state.get(
|
|
1105
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1097
1106
|
`${this.bindColumn(column)}`,
|
|
1098
|
-
`${this.$constants(
|
|
1107
|
+
`${this.$constants("NOT_BETWEEN")}`,
|
|
1099
1108
|
`${this._checkValueHasRaw(this.$utils.escape(value1))}`,
|
|
1100
|
-
`${this.$constants(
|
|
1101
|
-
`${this._checkValueHasRaw(this.$utils.escape(value2))}
|
|
1102
|
-
].join(
|
|
1109
|
+
`${this.$constants("AND")}`,
|
|
1110
|
+
`${this._checkValueHasRaw(this.$utils.escape(value2))}`,
|
|
1111
|
+
].join(" "),
|
|
1103
1112
|
]);
|
|
1104
1113
|
return this;
|
|
1105
1114
|
}
|
|
@@ -1111,13 +1120,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1111
1120
|
* @returns {this}
|
|
1112
1121
|
*/
|
|
1113
1122
|
whereNull(column) {
|
|
1114
|
-
this.$state.set(
|
|
1115
|
-
...this.$state.get(
|
|
1123
|
+
this.$state.set("WHERE", [
|
|
1124
|
+
...this.$state.get("WHERE"),
|
|
1116
1125
|
[
|
|
1117
|
-
this.$state.get(
|
|
1126
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1118
1127
|
`${this.bindColumn(column)}`,
|
|
1119
|
-
`${this.$constants(
|
|
1120
|
-
].join(
|
|
1128
|
+
`${this.$constants("IS_NULL")}`,
|
|
1129
|
+
].join(" "),
|
|
1121
1130
|
]);
|
|
1122
1131
|
return this;
|
|
1123
1132
|
}
|
|
@@ -1129,13 +1138,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1129
1138
|
* @returns {this}
|
|
1130
1139
|
*/
|
|
1131
1140
|
orWhereNull(column) {
|
|
1132
|
-
this.$state.set(
|
|
1133
|
-
...this.$state.get(
|
|
1141
|
+
this.$state.set("WHERE", [
|
|
1142
|
+
...this.$state.get("WHERE"),
|
|
1134
1143
|
[
|
|
1135
|
-
this.$state.get(
|
|
1144
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1136
1145
|
`${this.bindColumn(column)}`,
|
|
1137
|
-
`${this.$constants(
|
|
1138
|
-
].join(
|
|
1146
|
+
`${this.$constants("IS_NULL")}`,
|
|
1147
|
+
].join(" "),
|
|
1139
1148
|
]);
|
|
1140
1149
|
return this;
|
|
1141
1150
|
}
|
|
@@ -1147,13 +1156,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1147
1156
|
* @returns {this}
|
|
1148
1157
|
*/
|
|
1149
1158
|
whereNotNull(column) {
|
|
1150
|
-
this.$state.set(
|
|
1151
|
-
...this.$state.get(
|
|
1159
|
+
this.$state.set("WHERE", [
|
|
1160
|
+
...this.$state.get("WHERE"),
|
|
1152
1161
|
[
|
|
1153
|
-
this.$state.get(
|
|
1162
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1154
1163
|
`${this.bindColumn(column)}`,
|
|
1155
|
-
`${this.$constants(
|
|
1156
|
-
].join(
|
|
1164
|
+
`${this.$constants("IS_NOT_NULL")}`,
|
|
1165
|
+
].join(" "),
|
|
1157
1166
|
]);
|
|
1158
1167
|
return this;
|
|
1159
1168
|
}
|
|
@@ -1165,13 +1174,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1165
1174
|
* @returns {this}
|
|
1166
1175
|
*/
|
|
1167
1176
|
orWhereNotNull(column) {
|
|
1168
|
-
this.$state.set(
|
|
1169
|
-
...this.$state.get(
|
|
1177
|
+
this.$state.set("WHERE", [
|
|
1178
|
+
...this.$state.get("WHERE"),
|
|
1170
1179
|
[
|
|
1171
|
-
this.$state.get(
|
|
1180
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1172
1181
|
`${this.bindColumn(column)}`,
|
|
1173
|
-
`${this.$constants(
|
|
1174
|
-
].join(
|
|
1182
|
+
`${this.$constants("IS_NOT_NULL")}`,
|
|
1183
|
+
].join(" "),
|
|
1175
1184
|
]);
|
|
1176
1185
|
return this;
|
|
1177
1186
|
}
|
|
@@ -1190,15 +1199,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1190
1199
|
[value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
|
|
1191
1200
|
value = this.$utils.escape(value);
|
|
1192
1201
|
value = this.$utils.covertBooleanToNumber(value);
|
|
1193
|
-
this.$state.set(
|
|
1194
|
-
...this.$state.get(
|
|
1202
|
+
this.$state.set("WHERE", [
|
|
1203
|
+
...this.$state.get("WHERE"),
|
|
1195
1204
|
[
|
|
1196
|
-
this.$state.get(
|
|
1197
|
-
`${this.$constants(
|
|
1205
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1206
|
+
`${this.$constants("BINARY")}`,
|
|
1198
1207
|
`${this.bindColumn(column)}`,
|
|
1199
1208
|
`${operator}`,
|
|
1200
|
-
`${this._checkValueHasRaw(this.$utils.escape(value))}
|
|
1201
|
-
].join(
|
|
1209
|
+
`${this._checkValueHasRaw(this.$utils.escape(value))}`,
|
|
1210
|
+
].join(" "),
|
|
1202
1211
|
]);
|
|
1203
1212
|
return this;
|
|
1204
1213
|
}
|
|
@@ -1231,15 +1240,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1231
1240
|
[value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
|
|
1232
1241
|
value = this.$utils.escape(value);
|
|
1233
1242
|
value = this.$utils.covertBooleanToNumber(value);
|
|
1234
|
-
this.$state.set(
|
|
1235
|
-
...this.$state.get(
|
|
1243
|
+
this.$state.set("WHERE", [
|
|
1244
|
+
...this.$state.get("WHERE"),
|
|
1236
1245
|
[
|
|
1237
|
-
this.$state.get(
|
|
1238
|
-
`${this.$constants(
|
|
1246
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1247
|
+
`${this.$constants("BINARY")}`,
|
|
1239
1248
|
`${this.bindColumn(column)}`,
|
|
1240
1249
|
`${operator}`,
|
|
1241
|
-
`${this._checkValueHasRaw(this.$utils.escape(value))}
|
|
1242
|
-
].join(
|
|
1250
|
+
`${this._checkValueHasRaw(this.$utils.escape(value))}`,
|
|
1251
|
+
].join(" "),
|
|
1243
1252
|
]);
|
|
1244
1253
|
return this;
|
|
1245
1254
|
}
|
|
@@ -1252,22 +1261,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1252
1261
|
*/
|
|
1253
1262
|
whereQuery(callback) {
|
|
1254
1263
|
var _a;
|
|
1255
|
-
const db = new DB_1.DB((_a = this.$state.get(
|
|
1264
|
+
const db = new DB_1.DB((_a = this.$state.get("TABLE_NAME")) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ""));
|
|
1256
1265
|
const repository = callback(db);
|
|
1257
1266
|
if (repository instanceof Promise)
|
|
1258
1267
|
throw new Error('"whereQuery" is not supported a Promise');
|
|
1259
1268
|
if (!(repository instanceof DB_1.DB))
|
|
1260
1269
|
throw new Error(`Unknown callback query: '${repository}'`);
|
|
1261
|
-
const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get(
|
|
1270
|
+
const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get("WHERE")) || [];
|
|
1262
1271
|
if (!where.length)
|
|
1263
1272
|
return this;
|
|
1264
|
-
const query = where.join(
|
|
1265
|
-
this.$state.set(
|
|
1266
|
-
...this.$state.get(
|
|
1273
|
+
const query = where.join(" ");
|
|
1274
|
+
this.$state.set("WHERE", [
|
|
1275
|
+
...this.$state.get("WHERE"),
|
|
1267
1276
|
[
|
|
1268
|
-
this.$state.get(
|
|
1269
|
-
`(${query})
|
|
1270
|
-
].join(
|
|
1277
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1278
|
+
`(${query})`,
|
|
1279
|
+
].join(" "),
|
|
1271
1280
|
]);
|
|
1272
1281
|
return this;
|
|
1273
1282
|
}
|
|
@@ -1290,22 +1299,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1290
1299
|
*/
|
|
1291
1300
|
orWhereQuery(callback) {
|
|
1292
1301
|
var _a;
|
|
1293
|
-
const db = new DB_1.DB((_a = this.$state.get(
|
|
1302
|
+
const db = new DB_1.DB((_a = this.$state.get("TABLE_NAME")) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ""));
|
|
1294
1303
|
const repository = callback(db);
|
|
1295
1304
|
if (repository instanceof Promise)
|
|
1296
1305
|
throw new Error('"whereQuery" is not supported a Promise');
|
|
1297
1306
|
if (!(repository instanceof DB_1.DB))
|
|
1298
1307
|
throw new Error(`Unknown callback query: '[${repository}]'`);
|
|
1299
|
-
const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get(
|
|
1308
|
+
const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get("WHERE")) || [];
|
|
1300
1309
|
if (!where.length)
|
|
1301
1310
|
return this;
|
|
1302
|
-
const query = where.join(
|
|
1303
|
-
this.$state.set(
|
|
1304
|
-
...this.$state.get(
|
|
1311
|
+
const query = where.join(" ");
|
|
1312
|
+
this.$state.set("WHERE", [
|
|
1313
|
+
...this.$state.get("WHERE"),
|
|
1305
1314
|
[
|
|
1306
|
-
this.$state.get(
|
|
1307
|
-
`(${query})
|
|
1308
|
-
].join(
|
|
1315
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1316
|
+
`(${query})`,
|
|
1317
|
+
].join(" "),
|
|
1309
1318
|
]);
|
|
1310
1319
|
return this;
|
|
1311
1320
|
}
|
|
@@ -1388,22 +1397,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1388
1397
|
throw new Error(`can't find then condition`);
|
|
1389
1398
|
query = [
|
|
1390
1399
|
...query,
|
|
1391
|
-
`${this.$constants(
|
|
1400
|
+
`${this.$constants("WHEN")} ${c.when} ${this.$constants("THEN")} ${c.then}`,
|
|
1392
1401
|
];
|
|
1393
1402
|
}
|
|
1394
|
-
this.$state.set(
|
|
1395
|
-
...this.$state.get(
|
|
1403
|
+
this.$state.set("WHERE", [
|
|
1404
|
+
...this.$state.get("WHERE"),
|
|
1396
1405
|
[
|
|
1397
1406
|
[
|
|
1398
|
-
this.$state.get(
|
|
1399
|
-
|
|
1400
|
-
this.$constants(
|
|
1401
|
-
query.join(
|
|
1402
|
-
elseCase == null ?
|
|
1403
|
-
this.$constants(
|
|
1404
|
-
|
|
1405
|
-
].join(
|
|
1406
|
-
].join(
|
|
1407
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
1408
|
+
"(",
|
|
1409
|
+
this.$constants("CASE"),
|
|
1410
|
+
query.join(" "),
|
|
1411
|
+
elseCase == null ? "" : `ELSE ${elseCase}`,
|
|
1412
|
+
this.$constants("END"),
|
|
1413
|
+
")",
|
|
1414
|
+
].join(" "),
|
|
1415
|
+
].join(" "),
|
|
1407
1416
|
]);
|
|
1408
1417
|
return this;
|
|
1409
1418
|
}
|
|
@@ -1427,22 +1436,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1427
1436
|
throw new Error(`can't find then condition`);
|
|
1428
1437
|
query = [
|
|
1429
1438
|
...query,
|
|
1430
|
-
`${this.$constants(
|
|
1439
|
+
`${this.$constants("WHEN")} ${c.when} ${this.$constants("THEN")} ${c.then}`,
|
|
1431
1440
|
];
|
|
1432
1441
|
}
|
|
1433
|
-
this.$state.set(
|
|
1434
|
-
...this.$state.get(
|
|
1442
|
+
this.$state.set("WHERE", [
|
|
1443
|
+
...this.$state.get("WHERE"),
|
|
1435
1444
|
[
|
|
1436
1445
|
[
|
|
1437
|
-
this.$state.get(
|
|
1438
|
-
|
|
1439
|
-
this.$constants(
|
|
1440
|
-
query.join(
|
|
1441
|
-
elseCase == null ?
|
|
1442
|
-
this.$constants(
|
|
1443
|
-
|
|
1444
|
-
].join(
|
|
1445
|
-
].join(
|
|
1446
|
+
this.$state.get("WHERE").length ? `${this.$constants("OR")}` : "",
|
|
1447
|
+
"(",
|
|
1448
|
+
this.$constants("CASE"),
|
|
1449
|
+
query.join(" "),
|
|
1450
|
+
elseCase == null ? "" : `ELSE ${elseCase}`,
|
|
1451
|
+
this.$constants("END"),
|
|
1452
|
+
")",
|
|
1453
|
+
].join(" "),
|
|
1454
|
+
].join(" "),
|
|
1446
1455
|
]);
|
|
1447
1456
|
return this;
|
|
1448
1457
|
}
|
|
@@ -1453,7 +1462,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1453
1462
|
* @returns {this}
|
|
1454
1463
|
*/
|
|
1455
1464
|
case(cases, as) {
|
|
1456
|
-
let query = [this.$constants(
|
|
1465
|
+
let query = [this.$constants("CASE")];
|
|
1457
1466
|
for (let i = 0; i < cases.length; i++) {
|
|
1458
1467
|
const c = cases[i];
|
|
1459
1468
|
if (cases.length - 1 === i) {
|
|
@@ -1461,8 +1470,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1461
1470
|
throw new Error(`can't find then condition`);
|
|
1462
1471
|
query = [
|
|
1463
1472
|
...query,
|
|
1464
|
-
`${this.$constants(
|
|
1465
|
-
`${this.$constants(
|
|
1473
|
+
`${this.$constants("ELSE")} '${c.then}'`,
|
|
1474
|
+
`${this.$constants("END")}`,
|
|
1466
1475
|
];
|
|
1467
1476
|
continue;
|
|
1468
1477
|
}
|
|
@@ -1472,12 +1481,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1472
1481
|
throw new Error(`can't find then condition`);
|
|
1473
1482
|
query = [
|
|
1474
1483
|
...query,
|
|
1475
|
-
`${this.$constants(
|
|
1484
|
+
`${this.$constants("WHEN")} ${c.when} ${this.$constants("THEN")} '${c.then}'`,
|
|
1476
1485
|
];
|
|
1477
1486
|
}
|
|
1478
1487
|
if (query.length <= 1)
|
|
1479
1488
|
return this;
|
|
1480
|
-
this.$state.set(
|
|
1489
|
+
this.$state.set("SELECT", [
|
|
1490
|
+
...this.$state.get("SELECT"),
|
|
1491
|
+
`${query.join(" ")} ${this.$constants("AS")} ${as}`,
|
|
1492
|
+
]);
|
|
1481
1493
|
return this;
|
|
1482
1494
|
}
|
|
1483
1495
|
/**
|
|
@@ -1497,14 +1509,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1497
1509
|
* @returns {this}
|
|
1498
1510
|
*/
|
|
1499
1511
|
join(localKey, referenceKey) {
|
|
1500
|
-
this._handleJoin(
|
|
1512
|
+
this._handleJoin("INNER_JOIN", localKey, referenceKey);
|
|
1501
1513
|
return this;
|
|
1502
1514
|
// const callback = localKey(new Join(this,'INNER_JOIN'))
|
|
1503
1515
|
// this.$state.set('JOIN', [
|
|
1504
|
-
// ...this.$state.get('JOIN'),
|
|
1516
|
+
// ...this.$state.get('JOIN'),
|
|
1505
1517
|
// callback['toString']()
|
|
1506
1518
|
// ]
|
|
1507
|
-
// )
|
|
1519
|
+
// )
|
|
1508
1520
|
// return this
|
|
1509
1521
|
// }
|
|
1510
1522
|
// let table = referenceKey?.split('.')?.shift()
|
|
@@ -1514,11 +1526,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1514
1526
|
// referenceKey = String(referenceKey?.split('|')?.pop() ?? referenceKey) as `${string}.${string}`
|
|
1515
1527
|
// }
|
|
1516
1528
|
// this.$state.set('JOIN', [
|
|
1517
|
-
// ...this.$state.get('JOIN'),
|
|
1529
|
+
// ...this.$state.get('JOIN'),
|
|
1518
1530
|
// [
|
|
1519
1531
|
// `${this.$constants('INNER_JOIN')}`,
|
|
1520
|
-
// alias == null
|
|
1521
|
-
// ? `\`${table}\``
|
|
1532
|
+
// alias == null
|
|
1533
|
+
// ? `\`${table}\``
|
|
1522
1534
|
// : `\`${table}\` ${this.$constants('AS')} \`${alias}\``
|
|
1523
1535
|
// ,
|
|
1524
1536
|
// `${this.$constants('ON')}`,
|
|
@@ -1539,7 +1551,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1539
1551
|
* @returns {this}
|
|
1540
1552
|
*/
|
|
1541
1553
|
rightJoin(localKey, referenceKey) {
|
|
1542
|
-
this._handleJoin(
|
|
1554
|
+
this._handleJoin("RIGHT_JOIN", localKey, referenceKey);
|
|
1543
1555
|
return this;
|
|
1544
1556
|
}
|
|
1545
1557
|
/**
|
|
@@ -1553,7 +1565,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1553
1565
|
* @returns {this}
|
|
1554
1566
|
*/
|
|
1555
1567
|
leftJoin(localKey, referenceKey) {
|
|
1556
|
-
this._handleJoin(
|
|
1568
|
+
this._handleJoin("LEFT_JOIN", localKey, referenceKey);
|
|
1557
1569
|
return this;
|
|
1558
1570
|
}
|
|
1559
1571
|
/**
|
|
@@ -1565,7 +1577,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1565
1577
|
* @returns {this}
|
|
1566
1578
|
*/
|
|
1567
1579
|
crossJoin(localKey, referenceKey) {
|
|
1568
|
-
this._handleJoin(
|
|
1580
|
+
this._handleJoin("CROSS_JOIN", localKey, referenceKey);
|
|
1569
1581
|
return this;
|
|
1570
1582
|
}
|
|
1571
1583
|
/**
|
|
@@ -1582,15 +1594,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1582
1594
|
* .get()
|
|
1583
1595
|
* @returns {this}
|
|
1584
1596
|
*/
|
|
1585
|
-
joinSubQuery({ localKey, foreignKey, sql }) {
|
|
1586
|
-
this.$state.set(
|
|
1587
|
-
...this.$state.get(
|
|
1597
|
+
joinSubQuery({ localKey, foreignKey, sql, }) {
|
|
1598
|
+
this.$state.set("JOIN", [
|
|
1599
|
+
...this.$state.get("JOIN"),
|
|
1588
1600
|
[
|
|
1589
|
-
`${this.$constants(
|
|
1601
|
+
`${this.$constants("INNER_JOIN")}`,
|
|
1590
1602
|
`(${sql}) as subquery`,
|
|
1591
|
-
`${this.$constants(
|
|
1592
|
-
`${this.bindColumn(localKey)} = subquery.\`${foreignKey}
|
|
1593
|
-
].join(
|
|
1603
|
+
`${this.$constants("ON")}`,
|
|
1604
|
+
`${this.bindColumn(localKey)} = subquery.\`${foreignKey}\``,
|
|
1605
|
+
].join(" "),
|
|
1594
1606
|
]);
|
|
1595
1607
|
return this;
|
|
1596
1608
|
}
|
|
@@ -1602,21 +1614,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1602
1614
|
* @param {string?} order by default order = 'asc' but you can used 'asc' or 'desc'
|
|
1603
1615
|
* @returns {this}
|
|
1604
1616
|
*/
|
|
1605
|
-
orderBy(column, order =
|
|
1606
|
-
const orderBy = [column]
|
|
1617
|
+
orderBy(column, order = "ASC") {
|
|
1618
|
+
const orderBy = [column]
|
|
1619
|
+
.map((c) => {
|
|
1607
1620
|
if (/\./.test(c))
|
|
1608
|
-
return this.bindColumn(c.replace(/'/g,
|
|
1609
|
-
if (c.includes(this.$constants(
|
|
1610
|
-
return c === null || c === void 0 ? void 0 : c.replace(this.$constants(
|
|
1621
|
+
return this.bindColumn(c.replace(/'/g, ""));
|
|
1622
|
+
if (c.includes(this.$constants("RAW")))
|
|
1623
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1611
1624
|
return this.bindColumn(c);
|
|
1612
|
-
})
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1625
|
+
})
|
|
1626
|
+
.join(", ");
|
|
1627
|
+
this.$state.set("ORDER_BY", [
|
|
1628
|
+
...this.$state.get("ORDER_BY"),
|
|
1629
|
+
`${orderBy} ${order.toUpperCase()}`,
|
|
1616
1630
|
]);
|
|
1617
|
-
this.$state.set(
|
|
1618
|
-
...this.$state.get(
|
|
1619
|
-
`\`${column}\` ${order.toUpperCase()}
|
|
1631
|
+
this.$state.set("ORDER_BY", [
|
|
1632
|
+
...this.$state.get("ORDER_BY"),
|
|
1633
|
+
`\`${column}\` ${order.toUpperCase()}`,
|
|
1620
1634
|
]);
|
|
1621
1635
|
return this;
|
|
1622
1636
|
}
|
|
@@ -1630,13 +1644,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1630
1644
|
* @param {string?} order [order=asc] asc, desc
|
|
1631
1645
|
* @returns {this}
|
|
1632
1646
|
*/
|
|
1633
|
-
orderByRaw(column, order =
|
|
1634
|
-
if (column.includes(this.$constants(
|
|
1635
|
-
column = column === null || column === void 0 ? void 0 : column.replace(this.$constants(
|
|
1647
|
+
orderByRaw(column, order = "ASC") {
|
|
1648
|
+
if (column.includes(this.$constants("RAW"))) {
|
|
1649
|
+
column = column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1636
1650
|
}
|
|
1637
|
-
this.$state.set(
|
|
1638
|
-
...this.$state.get(
|
|
1639
|
-
`${column} ${order.toUpperCase()}
|
|
1651
|
+
this.$state.set("ORDER_BY", [
|
|
1652
|
+
...this.$state.get("ORDER_BY"),
|
|
1653
|
+
`${column} ${order.toUpperCase()}`,
|
|
1640
1654
|
]);
|
|
1641
1655
|
return this;
|
|
1642
1656
|
}
|
|
@@ -1646,9 +1660,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1646
1660
|
* @returns {this}
|
|
1647
1661
|
*/
|
|
1648
1662
|
random() {
|
|
1649
|
-
this.$state.set(
|
|
1650
|
-
...this.$state.get(
|
|
1651
|
-
`${this.$constants(
|
|
1663
|
+
this.$state.set("ORDER_BY", [
|
|
1664
|
+
...this.$state.get("ORDER_BY"),
|
|
1665
|
+
`${this.$constants("RAND")}`,
|
|
1652
1666
|
]);
|
|
1653
1667
|
return this;
|
|
1654
1668
|
}
|
|
@@ -1668,19 +1682,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1668
1682
|
* @returns {this}
|
|
1669
1683
|
*/
|
|
1670
1684
|
latest(...columns) {
|
|
1671
|
-
let orderBy =
|
|
1685
|
+
let orderBy = "`id`";
|
|
1672
1686
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1673
|
-
orderBy = columns
|
|
1687
|
+
orderBy = columns
|
|
1688
|
+
.map((c) => {
|
|
1674
1689
|
if (/\./.test(c))
|
|
1675
1690
|
return this.bindColumn(c);
|
|
1676
|
-
if (c.includes(this.$constants(
|
|
1677
|
-
return c === null || c === void 0 ? void 0 : c.replace(this.$constants(
|
|
1691
|
+
if (c.includes(this.$constants("RAW")))
|
|
1692
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1678
1693
|
return `\`${c}\``;
|
|
1679
|
-
})
|
|
1694
|
+
})
|
|
1695
|
+
.join(", ");
|
|
1680
1696
|
}
|
|
1681
|
-
this.$state.set(
|
|
1682
|
-
...this.$state.get(
|
|
1683
|
-
`${orderBy} ${this.$constants(
|
|
1697
|
+
this.$state.set("ORDER_BY", [
|
|
1698
|
+
...this.$state.get("ORDER_BY"),
|
|
1699
|
+
`${orderBy} ${this.$constants("DESC")}`,
|
|
1684
1700
|
]);
|
|
1685
1701
|
return this;
|
|
1686
1702
|
}
|
|
@@ -1694,17 +1710,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1694
1710
|
* @returns {this}
|
|
1695
1711
|
*/
|
|
1696
1712
|
latestRaw(...columns) {
|
|
1697
|
-
let orderBy =
|
|
1713
|
+
let orderBy = "`id`";
|
|
1698
1714
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1699
|
-
orderBy = columns
|
|
1700
|
-
|
|
1701
|
-
|
|
1715
|
+
orderBy = columns
|
|
1716
|
+
.map((column) => {
|
|
1717
|
+
if (column.includes(this.$constants("RAW")))
|
|
1718
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1702
1719
|
return column;
|
|
1703
|
-
})
|
|
1720
|
+
})
|
|
1721
|
+
.join(", ");
|
|
1704
1722
|
}
|
|
1705
|
-
this.$state.set(
|
|
1706
|
-
...this.$state.get(
|
|
1707
|
-
`${orderBy} ${this.$constants(
|
|
1723
|
+
this.$state.set("ORDER_BY", [
|
|
1724
|
+
...this.$state.get("ORDER_BY"),
|
|
1725
|
+
`${orderBy} ${this.$constants("DESC")}`,
|
|
1708
1726
|
]);
|
|
1709
1727
|
return this;
|
|
1710
1728
|
}
|
|
@@ -1716,19 +1734,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1716
1734
|
* @returns {this}
|
|
1717
1735
|
*/
|
|
1718
1736
|
oldest(...columns) {
|
|
1719
|
-
let orderBy =
|
|
1737
|
+
let orderBy = "`id`";
|
|
1720
1738
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1721
|
-
orderBy = columns
|
|
1739
|
+
orderBy = columns
|
|
1740
|
+
.map((c) => {
|
|
1722
1741
|
if (/\./.test(c))
|
|
1723
1742
|
return this.bindColumn(c);
|
|
1724
|
-
if (c.includes(this.$constants(
|
|
1725
|
-
return c === null || c === void 0 ? void 0 : c.replace(this.$constants(
|
|
1743
|
+
if (c.includes(this.$constants("RAW")))
|
|
1744
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1726
1745
|
return `\`${c}\``;
|
|
1727
|
-
})
|
|
1746
|
+
})
|
|
1747
|
+
.join(", ");
|
|
1728
1748
|
}
|
|
1729
|
-
this.$state.set(
|
|
1730
|
-
...this.$state.get(
|
|
1731
|
-
`${orderBy} ${this.$constants(
|
|
1749
|
+
this.$state.set("ORDER_BY", [
|
|
1750
|
+
...this.$state.get("ORDER_BY"),
|
|
1751
|
+
`${orderBy} ${this.$constants("ASC")}`,
|
|
1732
1752
|
]);
|
|
1733
1753
|
return this;
|
|
1734
1754
|
}
|
|
@@ -1742,17 +1762,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1742
1762
|
* @returns {this}
|
|
1743
1763
|
*/
|
|
1744
1764
|
oldestRaw(...columns) {
|
|
1745
|
-
let orderBy =
|
|
1765
|
+
let orderBy = "`id`";
|
|
1746
1766
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1747
|
-
orderBy = columns
|
|
1748
|
-
|
|
1749
|
-
|
|
1767
|
+
orderBy = columns
|
|
1768
|
+
.map((column) => {
|
|
1769
|
+
if (column.includes(this.$constants("RAW")))
|
|
1770
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1750
1771
|
return column;
|
|
1751
|
-
})
|
|
1772
|
+
})
|
|
1773
|
+
.join(", ");
|
|
1752
1774
|
}
|
|
1753
|
-
this.$state.set(
|
|
1754
|
-
...this.$state.get(
|
|
1755
|
-
`${orderBy} ${this.$constants(
|
|
1775
|
+
this.$state.set("ORDER_BY", [
|
|
1776
|
+
...this.$state.get("ORDER_BY"),
|
|
1777
|
+
`${orderBy} ${this.$constants("ASC")}`,
|
|
1756
1778
|
]);
|
|
1757
1779
|
return this;
|
|
1758
1780
|
}
|
|
@@ -1766,20 +1788,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1766
1788
|
* @returns {this}
|
|
1767
1789
|
*/
|
|
1768
1790
|
groupBy(...columns) {
|
|
1769
|
-
let groupBy =
|
|
1791
|
+
let groupBy = "id";
|
|
1770
1792
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1771
|
-
groupBy = columns
|
|
1793
|
+
groupBy = columns
|
|
1794
|
+
.map((c) => {
|
|
1772
1795
|
if (/\./.test(c))
|
|
1773
|
-
return this.bindColumn(c.replace(/'/g,
|
|
1774
|
-
if (c.includes(this.$constants(
|
|
1775
|
-
return c === null || c === void 0 ? void 0 : c.replace(this.$constants(
|
|
1796
|
+
return this.bindColumn(c.replace(/'/g, ""));
|
|
1797
|
+
if (c.includes(this.$constants("RAW")))
|
|
1798
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1776
1799
|
return this.bindColumn(c);
|
|
1777
|
-
})
|
|
1800
|
+
})
|
|
1801
|
+
.join(", ");
|
|
1778
1802
|
}
|
|
1779
|
-
this.$state.set(
|
|
1780
|
-
...this.$state.get('GROUP_BY'),
|
|
1781
|
-
`${groupBy}`
|
|
1782
|
-
]);
|
|
1803
|
+
this.$state.set("GROUP_BY", [...this.$state.get("GROUP_BY"), `${groupBy}`]);
|
|
1783
1804
|
return this;
|
|
1784
1805
|
}
|
|
1785
1806
|
/**
|
|
@@ -1794,18 +1815,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1794
1815
|
* @returns {this}
|
|
1795
1816
|
*/
|
|
1796
1817
|
groupByRaw(...columns) {
|
|
1797
|
-
let groupBy =
|
|
1818
|
+
let groupBy = "id";
|
|
1798
1819
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1799
|
-
groupBy = columns
|
|
1800
|
-
|
|
1801
|
-
|
|
1820
|
+
groupBy = columns
|
|
1821
|
+
.map((column) => {
|
|
1822
|
+
if (column.includes(this.$constants("RAW")))
|
|
1823
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1802
1824
|
return column;
|
|
1803
|
-
})
|
|
1825
|
+
})
|
|
1826
|
+
.join(", ");
|
|
1804
1827
|
}
|
|
1805
|
-
this.$state.set(
|
|
1806
|
-
...this.$state.get('GROUP_BY'),
|
|
1807
|
-
`${groupBy}`
|
|
1808
|
-
]);
|
|
1828
|
+
this.$state.set("GROUP_BY", [...this.$state.get("GROUP_BY"), `${groupBy}`]);
|
|
1809
1829
|
return this;
|
|
1810
1830
|
}
|
|
1811
1831
|
/**
|
|
@@ -1818,10 +1838,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1818
1838
|
* @returns {this}
|
|
1819
1839
|
*/
|
|
1820
1840
|
having(condition) {
|
|
1821
|
-
if (condition.includes(this.$constants(
|
|
1822
|
-
condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants(
|
|
1841
|
+
if (condition.includes(this.$constants("RAW"))) {
|
|
1842
|
+
condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants("RAW"), "");
|
|
1823
1843
|
}
|
|
1824
|
-
this.$state.set(
|
|
1844
|
+
this.$state.set("HAVING", `${this.$constants("HAVING")} ${condition}`);
|
|
1825
1845
|
return this;
|
|
1826
1846
|
}
|
|
1827
1847
|
/**
|
|
@@ -1851,7 +1871,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1851
1871
|
number = Math.pow(2, 31) - 1; // int 32 bit
|
|
1852
1872
|
if (number < 0 || number === -0)
|
|
1853
1873
|
number = 0;
|
|
1854
|
-
this.$state.set(
|
|
1874
|
+
this.$state.set("LIMIT", number);
|
|
1855
1875
|
return this;
|
|
1856
1876
|
}
|
|
1857
1877
|
/**
|
|
@@ -1875,9 +1895,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1875
1895
|
number = this.$utils.softNumber(number);
|
|
1876
1896
|
if (number < 0 || number === -0)
|
|
1877
1897
|
number = 0;
|
|
1878
|
-
this.$state.set(
|
|
1879
|
-
if (!this.$state.get(
|
|
1880
|
-
this.$state.set(
|
|
1898
|
+
this.$state.set("OFFSET", `${this.$constants("OFFSET")} ${number}`);
|
|
1899
|
+
if (!this.$state.get("LIMIT"))
|
|
1900
|
+
this.$state.set("LIMIT", number);
|
|
1881
1901
|
return this;
|
|
1882
1902
|
}
|
|
1883
1903
|
/**
|
|
@@ -1897,7 +1917,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1897
1917
|
* @returns {this} this
|
|
1898
1918
|
*/
|
|
1899
1919
|
hidden(...columns) {
|
|
1900
|
-
this.$state.set(
|
|
1920
|
+
this.$state.set("HIDDEN", columns);
|
|
1901
1921
|
return this;
|
|
1902
1922
|
}
|
|
1903
1923
|
/**
|
|
@@ -1913,7 +1933,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1913
1933
|
update(data, updateNotExists = []) {
|
|
1914
1934
|
this.limit(1);
|
|
1915
1935
|
if (!Object.keys(data).length)
|
|
1916
|
-
throw new Error(
|
|
1936
|
+
throw new Error("This method must be required");
|
|
1917
1937
|
if (updateNotExists.length) {
|
|
1918
1938
|
for (const c of updateNotExists) {
|
|
1919
1939
|
for (const column in data) {
|
|
@@ -1926,12 +1946,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1926
1946
|
}
|
|
1927
1947
|
}
|
|
1928
1948
|
const query = this._queryUpdate(data);
|
|
1929
|
-
this.$state.set(
|
|
1930
|
-
`${this.$constants(
|
|
1931
|
-
`${this.$state.get(
|
|
1932
|
-
`${query}
|
|
1933
|
-
].join(
|
|
1934
|
-
this.$state.set(
|
|
1949
|
+
this.$state.set("UPDATE", [
|
|
1950
|
+
`${this.$constants("UPDATE")}`,
|
|
1951
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
1952
|
+
`${query}`,
|
|
1953
|
+
].join(" "));
|
|
1954
|
+
this.$state.set("SAVE", "UPDATE");
|
|
1935
1955
|
return this;
|
|
1936
1956
|
}
|
|
1937
1957
|
/**
|
|
@@ -1946,7 +1966,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1946
1966
|
*/
|
|
1947
1967
|
updateMany(data, updateNotExists = []) {
|
|
1948
1968
|
if (!Object.keys(data).length)
|
|
1949
|
-
throw new Error(
|
|
1969
|
+
throw new Error("This method must be required");
|
|
1950
1970
|
if (updateNotExists.length) {
|
|
1951
1971
|
for (const c of updateNotExists) {
|
|
1952
1972
|
for (const column in data) {
|
|
@@ -1959,12 +1979,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1959
1979
|
}
|
|
1960
1980
|
}
|
|
1961
1981
|
const query = this._queryUpdate(data);
|
|
1962
|
-
this.$state.set(
|
|
1963
|
-
`${this.$constants(
|
|
1964
|
-
`${this.$state.get(
|
|
1965
|
-
`${query}
|
|
1966
|
-
].join(
|
|
1967
|
-
this.$state.set(
|
|
1982
|
+
this.$state.set("UPDATE", [
|
|
1983
|
+
`${this.$constants("UPDATE")}`,
|
|
1984
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
1985
|
+
`${query}`,
|
|
1986
|
+
].join(" "));
|
|
1987
|
+
this.$state.set("SAVE", "UPDATE");
|
|
1968
1988
|
return this;
|
|
1969
1989
|
}
|
|
1970
1990
|
/**
|
|
@@ -1984,15 +2004,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1984
2004
|
throw new Error(`The method 'updateMultiple' must not be empty.`);
|
|
1985
2005
|
this.limit(cases.length);
|
|
1986
2006
|
const updateColumns = cases.reduce((columns, item) => {
|
|
1987
|
-
return (item.columns &&
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
2007
|
+
return (item.columns &&
|
|
2008
|
+
Object.keys(item.columns).forEach((key) => (columns[key] = [
|
|
2009
|
+
this.$constants("RAW"),
|
|
2010
|
+
this.$constants("CASE"),
|
|
2011
|
+
`${this.$constants("ELSE")} ${this.bindColumn(key)}`,
|
|
2012
|
+
this.$constants("END"),
|
|
2013
|
+
])),
|
|
2014
|
+
columns);
|
|
1993
2015
|
}, {});
|
|
1994
2016
|
const columns = cases.reduce((columns, item) => {
|
|
1995
|
-
return (item.columns &&
|
|
2017
|
+
return (item.columns &&
|
|
2018
|
+
Object.keys(item.columns).forEach((key) => (columns[key] = "")),
|
|
2019
|
+
columns);
|
|
1996
2020
|
}, {});
|
|
1997
2021
|
for (let i = cases.length - 1; i >= 0; i--) {
|
|
1998
2022
|
const c = cases[i];
|
|
@@ -2008,33 +2032,34 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2008
2032
|
for (const [key, value] of Object.entries(c.columns)) {
|
|
2009
2033
|
if (updateColumns[key] == null)
|
|
2010
2034
|
continue;
|
|
2011
|
-
const startIndex = updateColumns[key].indexOf(this.$constants(
|
|
2012
|
-
const str = `${this.$constants(
|
|
2035
|
+
const startIndex = updateColumns[key].indexOf(this.$constants("CASE"));
|
|
2036
|
+
const str = `${this.$constants("WHEN")} ${when.join(` ${this.$constants("AND")} `)} ${this.$constants("THEN")} '${value}'`;
|
|
2013
2037
|
updateColumns[key].splice(startIndex + 1, 0, str);
|
|
2014
2038
|
}
|
|
2015
2039
|
}
|
|
2016
2040
|
for (const key in columns) {
|
|
2017
2041
|
if (updateColumns[key] == null)
|
|
2018
2042
|
continue;
|
|
2019
|
-
columns[key] = `( ${updateColumns[key].join(
|
|
2043
|
+
columns[key] = `( ${updateColumns[key].join(" ")} )`;
|
|
2020
2044
|
}
|
|
2021
2045
|
const keyValue = Object.entries(columns).map(([column, value]) => {
|
|
2022
|
-
if (typeof value ===
|
|
2046
|
+
if (typeof value === "string" &&
|
|
2047
|
+
!value.includes(this.$constants("RAW"))) {
|
|
2023
2048
|
value = this.$utils.escapeActions(value);
|
|
2024
2049
|
}
|
|
2025
|
-
return `${this.bindColumn(column)} = ${value == null || value === this.$constants(
|
|
2026
|
-
? this.$constants(
|
|
2027
|
-
: typeof value ===
|
|
2028
|
-
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants(
|
|
2050
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants("NULL")
|
|
2051
|
+
? this.$constants("NULL")
|
|
2052
|
+
: typeof value === "string" && value.includes(this.$constants("RAW"))
|
|
2053
|
+
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants("RAW"), "")
|
|
2029
2054
|
: `'${this.$utils.covertBooleanToNumber(value)}'`}`;
|
|
2030
2055
|
});
|
|
2031
|
-
const query = `${this.$constants(
|
|
2032
|
-
this.$state.set(
|
|
2033
|
-
`${this.$constants(
|
|
2034
|
-
`${this.$state.get(
|
|
2035
|
-
`${query}
|
|
2036
|
-
].join(
|
|
2037
|
-
this.$state.set(
|
|
2056
|
+
const query = `${this.$constants("SET")} ${keyValue.join(", ")}`;
|
|
2057
|
+
this.$state.set("UPDATE", [
|
|
2058
|
+
`${this.$constants("UPDATE")}`,
|
|
2059
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2060
|
+
`${query}`,
|
|
2061
|
+
].join(" "));
|
|
2062
|
+
this.$state.set("SAVE", "UPDATE");
|
|
2038
2063
|
return this;
|
|
2039
2064
|
}
|
|
2040
2065
|
/**
|
|
@@ -2049,18 +2074,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2049
2074
|
updateNotExists(data) {
|
|
2050
2075
|
this.limit(1);
|
|
2051
2076
|
if (!Object.keys(data).length)
|
|
2052
|
-
throw new Error(
|
|
2077
|
+
throw new Error("This method must be required");
|
|
2053
2078
|
for (const column in data) {
|
|
2054
2079
|
const value = data[column];
|
|
2055
2080
|
data[column] = this._updateHandler(column, value);
|
|
2056
2081
|
}
|
|
2057
2082
|
const query = this._queryUpdate(data);
|
|
2058
|
-
this.$state.set(
|
|
2059
|
-
`${this.$constants(
|
|
2060
|
-
`${this.$state.get(
|
|
2061
|
-
`${query}
|
|
2062
|
-
].join(
|
|
2063
|
-
this.$state.set(
|
|
2083
|
+
this.$state.set("UPDATE", [
|
|
2084
|
+
`${this.$constants("UPDATE")}`,
|
|
2085
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2086
|
+
`${query}`,
|
|
2087
|
+
].join(" "));
|
|
2088
|
+
this.$state.set("SAVE", "UPDATE");
|
|
2064
2089
|
return this;
|
|
2065
2090
|
}
|
|
2066
2091
|
/**
|
|
@@ -2072,14 +2097,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2072
2097
|
*/
|
|
2073
2098
|
insert(data) {
|
|
2074
2099
|
if (!Object.keys(data).length)
|
|
2075
|
-
throw new Error(
|
|
2100
|
+
throw new Error("This method must be required");
|
|
2076
2101
|
const query = this._queryInsert(data);
|
|
2077
|
-
this.$state.set(
|
|
2078
|
-
`${this.$constants(
|
|
2079
|
-
`${this.$state.get(
|
|
2080
|
-
`${query}
|
|
2081
|
-
].join(
|
|
2082
|
-
this.$state.set(
|
|
2102
|
+
this.$state.set("INSERT", [
|
|
2103
|
+
`${this.$constants("INSERT")}`,
|
|
2104
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2105
|
+
`${query}`,
|
|
2106
|
+
].join(" "));
|
|
2107
|
+
this.$state.set("SAVE", "INSERT");
|
|
2083
2108
|
return this;
|
|
2084
2109
|
}
|
|
2085
2110
|
/**
|
|
@@ -2091,14 +2116,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2091
2116
|
*/
|
|
2092
2117
|
create(data) {
|
|
2093
2118
|
if (!Object.keys(data).length)
|
|
2094
|
-
throw new Error(
|
|
2119
|
+
throw new Error("This method must be required");
|
|
2095
2120
|
const query = this._queryInsert(data);
|
|
2096
|
-
this.$state.set(
|
|
2097
|
-
`${this.$constants(
|
|
2098
|
-
`${this.$state.get(
|
|
2099
|
-
`${query}
|
|
2100
|
-
].join(
|
|
2101
|
-
this.$state.set(
|
|
2121
|
+
this.$state.set("INSERT", [
|
|
2122
|
+
`${this.$constants("INSERT")}`,
|
|
2123
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2124
|
+
`${query}`,
|
|
2125
|
+
].join(" "));
|
|
2126
|
+
this.$state.set("SAVE", "INSERT");
|
|
2102
2127
|
return this;
|
|
2103
2128
|
}
|
|
2104
2129
|
/**
|
|
@@ -2110,14 +2135,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2110
2135
|
*/
|
|
2111
2136
|
createMultiple(data) {
|
|
2112
2137
|
if (!Object.keys(data).length)
|
|
2113
|
-
throw new Error(
|
|
2138
|
+
throw new Error("This method must be required");
|
|
2114
2139
|
const query = this._queryInsertMultiple(data);
|
|
2115
|
-
this.$state.set(
|
|
2116
|
-
`${this.$constants(
|
|
2117
|
-
`${this.$state.get(
|
|
2118
|
-
`${query}
|
|
2119
|
-
].join(
|
|
2120
|
-
this.$state.set(
|
|
2140
|
+
this.$state.set("INSERT", [
|
|
2141
|
+
`${this.$constants("INSERT")}`,
|
|
2142
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2143
|
+
`${query}`,
|
|
2144
|
+
].join(" "));
|
|
2145
|
+
this.$state.set("SAVE", "INSERT_MULTIPLE");
|
|
2121
2146
|
return this;
|
|
2122
2147
|
}
|
|
2123
2148
|
/**
|
|
@@ -2140,12 +2165,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2140
2165
|
*/
|
|
2141
2166
|
createNotExists(data) {
|
|
2142
2167
|
const query = this._queryInsert(data);
|
|
2143
|
-
this.$state.set(
|
|
2144
|
-
`${this.$constants(
|
|
2145
|
-
`${this.$state.get(
|
|
2146
|
-
`${query}
|
|
2147
|
-
].join(
|
|
2148
|
-
this.$state.set(
|
|
2168
|
+
this.$state.set("INSERT", [
|
|
2169
|
+
`${this.$constants("INSERT")}`,
|
|
2170
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2171
|
+
`${query}`,
|
|
2172
|
+
].join(" "));
|
|
2173
|
+
this.$state.set("SAVE", "INSERT_NOT_EXISTS");
|
|
2149
2174
|
return this;
|
|
2150
2175
|
}
|
|
2151
2176
|
/**
|
|
@@ -2170,12 +2195,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2170
2195
|
*/
|
|
2171
2196
|
createOrSelect(data) {
|
|
2172
2197
|
const queryInsert = this._queryInsert(data);
|
|
2173
|
-
this.$state.set(
|
|
2174
|
-
`${this.$constants(
|
|
2175
|
-
`${this.$state.get(
|
|
2176
|
-
`${queryInsert}
|
|
2177
|
-
].join(
|
|
2178
|
-
this.$state.set(
|
|
2198
|
+
this.$state.set("INSERT", [
|
|
2199
|
+
`${this.$constants("INSERT")}`,
|
|
2200
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2201
|
+
`${queryInsert}`,
|
|
2202
|
+
].join(" "));
|
|
2203
|
+
this.$state.set("SAVE", "INSERT_OR_SELECT");
|
|
2179
2204
|
return this;
|
|
2180
2205
|
}
|
|
2181
2206
|
/**
|
|
@@ -2202,17 +2227,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2202
2227
|
this.limit(1);
|
|
2203
2228
|
const queryUpdate = this._queryUpdate(data);
|
|
2204
2229
|
const queryInsert = this._queryInsert(data);
|
|
2205
|
-
this.$state.set(
|
|
2206
|
-
`${this.$constants(
|
|
2207
|
-
`${this.$state.get(
|
|
2208
|
-
`${queryInsert}
|
|
2209
|
-
].join(
|
|
2210
|
-
this.$state.set(
|
|
2211
|
-
`${this.$constants(
|
|
2212
|
-
`${this.$state.get(
|
|
2213
|
-
`${queryUpdate}
|
|
2214
|
-
].join(
|
|
2215
|
-
this.$state.set(
|
|
2230
|
+
this.$state.set("INSERT", [
|
|
2231
|
+
`${this.$constants("INSERT")}`,
|
|
2232
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2233
|
+
`${queryInsert}`,
|
|
2234
|
+
].join(" "));
|
|
2235
|
+
this.$state.set("UPDATE", [
|
|
2236
|
+
`${this.$constants("UPDATE")}`,
|
|
2237
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2238
|
+
`${queryUpdate}`,
|
|
2239
|
+
].join(" "));
|
|
2240
|
+
this.$state.set("SAVE", "UPDATE_OR_INSERT");
|
|
2216
2241
|
return this;
|
|
2217
2242
|
}
|
|
2218
2243
|
/**
|
|
@@ -2276,7 +2301,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2276
2301
|
*
|
|
2277
2302
|
* This method is particularly useful for debugging and understanding the SQL queries generated by your application.
|
|
2278
2303
|
* @returns {string}
|
|
2279
|
-
|
|
2304
|
+
*/
|
|
2280
2305
|
toRawSQL() {
|
|
2281
2306
|
return this.toString();
|
|
2282
2307
|
}
|
|
@@ -2285,7 +2310,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2285
2310
|
* @returns {string} return table name
|
|
2286
2311
|
*/
|
|
2287
2312
|
getTableName() {
|
|
2288
|
-
return this.$state.get(
|
|
2313
|
+
return this.$state.get("TABLE_NAME").replace(/\`/g, "");
|
|
2289
2314
|
}
|
|
2290
2315
|
/**
|
|
2291
2316
|
* The 'getColumns' method is used to get columns
|
|
@@ -2294,11 +2319,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2294
2319
|
getColumns() {
|
|
2295
2320
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2296
2321
|
const sql = [
|
|
2297
|
-
`${this.$constants(
|
|
2298
|
-
`${this.$constants(
|
|
2299
|
-
`${this.$constants(
|
|
2300
|
-
`\`${this.$state.get(
|
|
2301
|
-
].join(
|
|
2322
|
+
`${this.$constants("SHOW")}`,
|
|
2323
|
+
`${this.$constants("COLUMNS")}`,
|
|
2324
|
+
`${this.$constants("FROM")}`,
|
|
2325
|
+
`\`${this.$state.get("TABLE_NAME").replace(/\`/g, "")}\``,
|
|
2326
|
+
].join(" ");
|
|
2302
2327
|
const rawColumns = yield this._queryStatement(sql);
|
|
2303
2328
|
const columns = rawColumns.map((column) => column.Field);
|
|
2304
2329
|
return columns;
|
|
@@ -2311,11 +2336,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2311
2336
|
getSchema() {
|
|
2312
2337
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2313
2338
|
const sql = [
|
|
2314
|
-
`${this.$constants(
|
|
2315
|
-
`${this.$constants(
|
|
2316
|
-
`${this.$constants(
|
|
2317
|
-
`\`${this.$state.get(
|
|
2318
|
-
].join(
|
|
2339
|
+
`${this.$constants("SHOW")}`,
|
|
2340
|
+
`${this.$constants("COLUMNS")}`,
|
|
2341
|
+
`${this.$constants("FROM")}`,
|
|
2342
|
+
`\`${this.$state.get("TABLE_NAME").replace(/\`/g, "")}\``,
|
|
2343
|
+
].join(" ");
|
|
2319
2344
|
return yield this._queryStatement(sql);
|
|
2320
2345
|
});
|
|
2321
2346
|
}
|
|
@@ -2326,25 +2351,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2326
2351
|
*/
|
|
2327
2352
|
bindColumn(column) {
|
|
2328
2353
|
if (!/\./.test(column)) {
|
|
2329
|
-
if (column ===
|
|
2330
|
-
return
|
|
2331
|
-
const alias = this.$state.get(
|
|
2332
|
-
if (this.getTableName() ===
|
|
2333
|
-
return `\`${column.replace(/`/g,
|
|
2354
|
+
if (column === "*")
|
|
2355
|
+
return "*";
|
|
2356
|
+
const alias = this.$state.get("ALIAS");
|
|
2357
|
+
if (this.getTableName() === "") {
|
|
2358
|
+
return `\`${column.replace(/`/g, "")}\``;
|
|
2334
2359
|
}
|
|
2335
2360
|
return [
|
|
2336
|
-
alias == null || alias ===
|
|
2337
|
-
? `\`${this.getTableName().replace(/`/g,
|
|
2338
|
-
: `\`${alias.replace(/`/g,
|
|
2339
|
-
|
|
2340
|
-
`\`${column.replace(/`/g,
|
|
2341
|
-
].join(
|
|
2361
|
+
alias == null || alias === ""
|
|
2362
|
+
? `\`${this.getTableName().replace(/`/g, "")}\``
|
|
2363
|
+
: `\`${alias.replace(/`/g, "")}\``,
|
|
2364
|
+
".",
|
|
2365
|
+
`\`${column.replace(/`/g, "")}\``,
|
|
2366
|
+
].join("");
|
|
2342
2367
|
}
|
|
2343
|
-
const [table, c] = column.split(
|
|
2344
|
-
if (c ===
|
|
2345
|
-
return `\`${table.replace(/`/g,
|
|
2368
|
+
const [table, c] = column.split(".");
|
|
2369
|
+
if (c === "*") {
|
|
2370
|
+
return `\`${table.replace(/`/g, "")}\`.*`;
|
|
2346
2371
|
}
|
|
2347
|
-
return `\`${table.replace(/`/g,
|
|
2372
|
+
return `\`${table.replace(/`/g, "")}\`.\`${c.replace(/`/g, "")}\``;
|
|
2348
2373
|
}
|
|
2349
2374
|
/**
|
|
2350
2375
|
* The 'debug' method is used to console.log raw SQL query that would be executed by a query builder
|
|
@@ -2352,7 +2377,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2352
2377
|
* @returns {this} this this
|
|
2353
2378
|
*/
|
|
2354
2379
|
debug(debug = true) {
|
|
2355
|
-
this.$state.set(
|
|
2380
|
+
this.$state.set("DEBUG", debug);
|
|
2356
2381
|
return this;
|
|
2357
2382
|
}
|
|
2358
2383
|
/**
|
|
@@ -2361,29 +2386,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2361
2386
|
* @returns {this} this this
|
|
2362
2387
|
*/
|
|
2363
2388
|
dd(debug = true) {
|
|
2364
|
-
this.$state.set(
|
|
2389
|
+
this.$state.set("DEBUG", debug);
|
|
2365
2390
|
return this;
|
|
2366
2391
|
}
|
|
2367
2392
|
/**
|
|
2368
2393
|
* The 'hook' method is used function when execute returns a result to callback function
|
|
2369
2394
|
* @param {Function} func function for callback result
|
|
2370
2395
|
* @returns {this}
|
|
2371
|
-
|
|
2396
|
+
*/
|
|
2372
2397
|
hook(func) {
|
|
2373
2398
|
if (typeof func !== "function")
|
|
2374
2399
|
throw new Error(`this '${func}' is not a function`);
|
|
2375
|
-
this.$state.set(
|
|
2400
|
+
this.$state.set("HOOKS", [...this.$state.get("HOOKS"), func]);
|
|
2376
2401
|
return this;
|
|
2377
2402
|
}
|
|
2378
2403
|
/**
|
|
2379
2404
|
* The 'before' method is used function when execute returns a result to callback function
|
|
2380
2405
|
* @param {Function} func function for callback result
|
|
2381
2406
|
* @returns {this}
|
|
2382
|
-
|
|
2407
|
+
*/
|
|
2383
2408
|
before(func) {
|
|
2384
2409
|
if (typeof func !== "function")
|
|
2385
2410
|
throw new Error(`this '${func}' is not a function`);
|
|
2386
|
-
this.$state.set(
|
|
2411
|
+
this.$state.set("HOOKS", [...this.$state.get("HOOKS"), func]);
|
|
2387
2412
|
return this;
|
|
2388
2413
|
}
|
|
2389
2414
|
/**
|
|
@@ -2398,7 +2423,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2398
2423
|
*/
|
|
2399
2424
|
connection(options) {
|
|
2400
2425
|
const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
|
|
2401
|
-
const pool = new
|
|
2426
|
+
const pool = new Pool_1.PoolConnection(Object.assign({ host,
|
|
2402
2427
|
port,
|
|
2403
2428
|
database,
|
|
2404
2429
|
user,
|
|
@@ -2414,13 +2439,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2414
2439
|
loadEnv(env) {
|
|
2415
2440
|
if (env === null)
|
|
2416
2441
|
return this;
|
|
2417
|
-
const options = (0,
|
|
2418
|
-
const pool = new
|
|
2442
|
+
const options = (0, Pool_1.loadOptionsEnvironment)();
|
|
2443
|
+
const pool = new Pool_1.PoolConnection({
|
|
2419
2444
|
host: String(options.host),
|
|
2420
2445
|
port: Number(options.port),
|
|
2421
2446
|
database: String(options.database),
|
|
2422
2447
|
user: String(options.username),
|
|
2423
|
-
password: String(options.password)
|
|
2448
|
+
password: String(options.password),
|
|
2424
2449
|
});
|
|
2425
2450
|
this.$pool.set(pool.connected());
|
|
2426
2451
|
return this;
|
|
@@ -2431,8 +2456,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2431
2456
|
* @returns {this} this
|
|
2432
2457
|
*/
|
|
2433
2458
|
pool(pool) {
|
|
2434
|
-
if (!(pool === null || pool === void 0 ? void 0 : pool.hasOwnProperty(
|
|
2435
|
-
throw new Error(
|
|
2459
|
+
if (!(pool === null || pool === void 0 ? void 0 : pool.hasOwnProperty("query"))) {
|
|
2460
|
+
throw new Error("pool must have a query property");
|
|
2436
2461
|
}
|
|
2437
2462
|
this.$pool.set(pool);
|
|
2438
2463
|
return this;
|
|
@@ -2443,11 +2468,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2443
2468
|
* @returns {this} this
|
|
2444
2469
|
*/
|
|
2445
2470
|
bind(connection) {
|
|
2446
|
-
if (!(connection === null || connection === void 0 ? void 0 : connection.hasOwnProperty(
|
|
2447
|
-
throw new Error(
|
|
2471
|
+
if (!(connection === null || connection === void 0 ? void 0 : connection.hasOwnProperty("query"))) {
|
|
2472
|
+
throw new Error("connection must have a query property");
|
|
2448
2473
|
}
|
|
2449
|
-
if (typeof connection.query !==
|
|
2450
|
-
throw new Error(
|
|
2474
|
+
if (typeof connection.query !== "function") {
|
|
2475
|
+
throw new Error("connection must have a query function");
|
|
2451
2476
|
}
|
|
2452
2477
|
this.$pool.set(connection);
|
|
2453
2478
|
return this;
|
|
@@ -2480,13 +2505,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2480
2505
|
* @returns {promise<any>}
|
|
2481
2506
|
*/
|
|
2482
2507
|
increment() {
|
|
2483
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2484
|
-
const query = `${this.$constants(
|
|
2485
|
-
this.$state.set(
|
|
2486
|
-
`${this.$constants(
|
|
2487
|
-
`${this.$state.get(
|
|
2488
|
-
`${query}
|
|
2489
|
-
].join(
|
|
2508
|
+
return __awaiter(this, arguments, void 0, function* (column = "id", value = 1) {
|
|
2509
|
+
const query = `${this.$constants("SET")} ${column} = ${column} + ${value}`;
|
|
2510
|
+
this.$state.set("UPDATE", [
|
|
2511
|
+
`${this.$constants("UPDATE")}`,
|
|
2512
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2513
|
+
`${query}`,
|
|
2514
|
+
].join(" "));
|
|
2490
2515
|
return yield this._update(true);
|
|
2491
2516
|
});
|
|
2492
2517
|
}
|
|
@@ -2498,19 +2523,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2498
2523
|
* @returns {promise<any>}
|
|
2499
2524
|
*/
|
|
2500
2525
|
decrement() {
|
|
2501
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2502
|
-
const query = `${this.$constants(
|
|
2503
|
-
this.$state.set(
|
|
2504
|
-
`${this.$constants(
|
|
2505
|
-
`${this.$state.get(
|
|
2506
|
-
`${query}
|
|
2507
|
-
].join(
|
|
2526
|
+
return __awaiter(this, arguments, void 0, function* (column = "id", value = 1) {
|
|
2527
|
+
const query = `${this.$constants("SET")} ${column} = ${column} - ${value}`;
|
|
2528
|
+
this.$state.set("UPDATE", [
|
|
2529
|
+
`${this.$constants("UPDATE")}`,
|
|
2530
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2531
|
+
`${query}`,
|
|
2532
|
+
].join(" "));
|
|
2508
2533
|
return yield this._update(true);
|
|
2509
2534
|
});
|
|
2510
2535
|
}
|
|
2511
2536
|
version() {
|
|
2512
2537
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2513
|
-
const result = yield this._queryStatement(`${this.$constants(
|
|
2538
|
+
const result = yield this._queryStatement(`${this.$constants("SELECT")} VERSION() as version`);
|
|
2514
2539
|
return result[0].version;
|
|
2515
2540
|
});
|
|
2516
2541
|
}
|
|
@@ -2523,11 +2548,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2523
2548
|
all() {
|
|
2524
2549
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2525
2550
|
return yield this._queryStatement([
|
|
2526
|
-
`${this.$constants(
|
|
2551
|
+
`${this.$constants("SELECT")}`,
|
|
2527
2552
|
`*`,
|
|
2528
|
-
`${this.$constants(
|
|
2529
|
-
`${this.$state.get(
|
|
2530
|
-
].join(
|
|
2553
|
+
`${this.$constants("FROM")}`,
|
|
2554
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2555
|
+
].join(" "));
|
|
2531
2556
|
});
|
|
2532
2557
|
}
|
|
2533
2558
|
/**
|
|
@@ -2539,7 +2564,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2539
2564
|
*/
|
|
2540
2565
|
find(id) {
|
|
2541
2566
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2542
|
-
this.where(
|
|
2567
|
+
this.where("id", id);
|
|
2543
2568
|
return yield this.first();
|
|
2544
2569
|
});
|
|
2545
2570
|
}
|
|
@@ -2570,7 +2595,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2570
2595
|
this.offset(offset);
|
|
2571
2596
|
let sql = this._queryBuilder().select();
|
|
2572
2597
|
const result = yield this._queryStatement(sql);
|
|
2573
|
-
if ((_a = this.$state.get(
|
|
2598
|
+
if ((_a = this.$state.get("HIDDEN")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2574
2599
|
this._hiddenColumn(result);
|
|
2575
2600
|
if (!result.length)
|
|
2576
2601
|
return {
|
|
@@ -2582,14 +2607,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2582
2607
|
current_page: currentPage,
|
|
2583
2608
|
last_page: 0,
|
|
2584
2609
|
next_page: 0,
|
|
2585
|
-
prev_page: 0
|
|
2610
|
+
prev_page: 0,
|
|
2586
2611
|
},
|
|
2587
|
-
data: []
|
|
2612
|
+
data: [],
|
|
2588
2613
|
};
|
|
2589
2614
|
const total = yield new DB_1.DB()
|
|
2590
2615
|
.copyBuilder(this, { where: true, join: true })
|
|
2591
2616
|
.bind(this.$pool.get())
|
|
2592
|
-
.debug(this.$state.get(
|
|
2617
|
+
.debug(this.$state.get("DEBUG"))
|
|
2593
2618
|
.unset({ alias: true })
|
|
2594
2619
|
.count();
|
|
2595
2620
|
let lastPage = Math.ceil(total / limit) || 0;
|
|
@@ -2604,9 +2629,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2604
2629
|
current_page: currentPage,
|
|
2605
2630
|
last_page: lastPage,
|
|
2606
2631
|
next_page: nextPage,
|
|
2607
|
-
prev_page: prevPage
|
|
2632
|
+
prev_page: prevPage,
|
|
2608
2633
|
},
|
|
2609
|
-
data: result !== null && result !== void 0 ? result : []
|
|
2634
|
+
data: result !== null && result !== void 0 ? result : [],
|
|
2610
2635
|
};
|
|
2611
2636
|
});
|
|
2612
2637
|
}
|
|
@@ -2642,39 +2667,41 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2642
2667
|
first(cb) {
|
|
2643
2668
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2644
2669
|
var _a, _b;
|
|
2645
|
-
if ((_a = this.$state.get(
|
|
2646
|
-
this.select(...yield this.exceptColumns());
|
|
2670
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2671
|
+
this.select(...(yield this.exceptColumns()));
|
|
2647
2672
|
this.limit(1);
|
|
2648
2673
|
let sql = this._queryBuilder().select();
|
|
2649
2674
|
if (cb) {
|
|
2650
2675
|
const callbackSql = cb(sql);
|
|
2651
|
-
if (callbackSql == null || callbackSql ===
|
|
2652
|
-
throw new Error(
|
|
2676
|
+
if (callbackSql == null || callbackSql === "")
|
|
2677
|
+
throw new Error("Please provide a callback for execution");
|
|
2653
2678
|
sql = callbackSql;
|
|
2654
2679
|
}
|
|
2655
2680
|
const result = yield this._queryStatement(sql);
|
|
2656
|
-
if (this.$state.get(
|
|
2681
|
+
if (this.$state.get("VOID"))
|
|
2657
2682
|
return null;
|
|
2658
|
-
if ((_b = this.$state.get(
|
|
2683
|
+
if ((_b = this.$state.get("HIDDEN")) === null || _b === void 0 ? void 0 : _b.length)
|
|
2659
2684
|
this._hiddenColumn(result);
|
|
2660
|
-
if (this.$state.get(
|
|
2661
|
-
const pluck = this.$state.get(
|
|
2685
|
+
if (this.$state.get("PLUCK")) {
|
|
2686
|
+
const pluck = this.$state.get("PLUCK");
|
|
2662
2687
|
const newData = result === null || result === void 0 ? void 0 : result.shift();
|
|
2663
2688
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
2664
2689
|
if (!checkProperty)
|
|
2665
2690
|
throw new Error(`can't find property '${pluck}' of result`);
|
|
2666
2691
|
const r = newData[pluck] || null;
|
|
2667
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2692
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), r);
|
|
2668
2693
|
return r;
|
|
2669
2694
|
}
|
|
2670
|
-
if (this.$state.get(
|
|
2671
|
-
const returnType = this.$state.get(
|
|
2672
|
-
return this._resultHandler(returnType ===
|
|
2695
|
+
if (this.$state.get("RETURN_TYPE") != null) {
|
|
2696
|
+
const returnType = this.$state.get("RETURN_TYPE");
|
|
2697
|
+
return this._resultHandler(returnType === "object"
|
|
2673
2698
|
? result[0]
|
|
2674
|
-
: returnType ===
|
|
2699
|
+
: returnType === "array"
|
|
2700
|
+
? result
|
|
2701
|
+
: [result]);
|
|
2675
2702
|
}
|
|
2676
2703
|
const r = result[0] || null;
|
|
2677
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2704
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), r);
|
|
2678
2705
|
return this._resultHandler(r);
|
|
2679
2706
|
});
|
|
2680
2707
|
}
|
|
@@ -2702,15 +2729,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2702
2729
|
firstOrError(message, options) {
|
|
2703
2730
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2704
2731
|
var _a, _b;
|
|
2705
|
-
if ((_a = this.$state.get(
|
|
2706
|
-
this.select(...yield this.exceptColumns());
|
|
2732
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2733
|
+
this.select(...(yield this.exceptColumns()));
|
|
2707
2734
|
this.limit(1);
|
|
2708
2735
|
let sql = this._queryBuilder().select();
|
|
2709
2736
|
const result = yield this._queryStatement(sql);
|
|
2710
|
-
if ((_b = this.$state.get(
|
|
2737
|
+
if ((_b = this.$state.get("HIDDEN")) === null || _b === void 0 ? void 0 : _b.length)
|
|
2711
2738
|
this._hiddenColumn(result);
|
|
2712
|
-
if (this.$state.get(
|
|
2713
|
-
const pluck = this.$state.get(
|
|
2739
|
+
if (this.$state.get("PLUCK")) {
|
|
2740
|
+
const pluck = this.$state.get("PLUCK");
|
|
2714
2741
|
const newData = result === null || result === void 0 ? void 0 : result.shift();
|
|
2715
2742
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
2716
2743
|
if (!checkProperty)
|
|
@@ -2721,7 +2748,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2721
2748
|
throw { message, code: 400 };
|
|
2722
2749
|
throw Object.assign({ message }, options);
|
|
2723
2750
|
}
|
|
2724
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2751
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), data);
|
|
2725
2752
|
return this._resultHandler(data);
|
|
2726
2753
|
}
|
|
2727
2754
|
const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
@@ -2731,7 +2758,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2731
2758
|
}
|
|
2732
2759
|
throw Object.assign({ message }, options);
|
|
2733
2760
|
}
|
|
2734
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2761
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), data);
|
|
2735
2762
|
return this._resultHandler(data);
|
|
2736
2763
|
});
|
|
2737
2764
|
}
|
|
@@ -2759,46 +2786,48 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2759
2786
|
get(cb) {
|
|
2760
2787
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2761
2788
|
var _a, _b;
|
|
2762
|
-
if ((_a = this.$state.get(
|
|
2763
|
-
this.select(...yield this.exceptColumns());
|
|
2789
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2790
|
+
this.select(...(yield this.exceptColumns()));
|
|
2764
2791
|
let sql = this._queryBuilder().select();
|
|
2765
2792
|
if (cb) {
|
|
2766
2793
|
const callbackSql = cb(sql);
|
|
2767
|
-
if (callbackSql == null || callbackSql ===
|
|
2768
|
-
throw new Error(
|
|
2794
|
+
if (callbackSql == null || callbackSql === "")
|
|
2795
|
+
throw new Error("Please provide a callback for execution");
|
|
2769
2796
|
sql = callbackSql;
|
|
2770
2797
|
}
|
|
2771
2798
|
const result = yield this._queryStatement(sql);
|
|
2772
|
-
if (this.$state.get(
|
|
2799
|
+
if (this.$state.get("VOID"))
|
|
2773
2800
|
return [];
|
|
2774
|
-
if ((_b = this.$state.get(
|
|
2801
|
+
if ((_b = this.$state.get("HIDDEN")) === null || _b === void 0 ? void 0 : _b.length)
|
|
2775
2802
|
this._hiddenColumn(result);
|
|
2776
|
-
if (this.$state.get(
|
|
2803
|
+
if (this.$state.get("CHUNK")) {
|
|
2777
2804
|
const data = result.reduce((resultArray, item, index) => {
|
|
2778
|
-
const chunkIndex = Math.floor(index / this.$state.get(
|
|
2805
|
+
const chunkIndex = Math.floor(index / this.$state.get("CHUNK"));
|
|
2779
2806
|
if (!resultArray[chunkIndex])
|
|
2780
2807
|
resultArray[chunkIndex] = [];
|
|
2781
2808
|
resultArray[chunkIndex].push(item);
|
|
2782
2809
|
return resultArray;
|
|
2783
2810
|
}, []);
|
|
2784
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2811
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), data || []);
|
|
2785
2812
|
return this._resultHandler(data || []);
|
|
2786
2813
|
}
|
|
2787
|
-
if (this.$state.get(
|
|
2788
|
-
const pluck = this.$state.get(
|
|
2814
|
+
if (this.$state.get("PLUCK")) {
|
|
2815
|
+
const pluck = this.$state.get("PLUCK");
|
|
2789
2816
|
const newData = result.map((d) => d[pluck]);
|
|
2790
2817
|
if (newData.every((d) => d == null)) {
|
|
2791
2818
|
throw new Error(`can't find property '${pluck}' of result`);
|
|
2792
2819
|
}
|
|
2793
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2820
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), newData || []);
|
|
2794
2821
|
return this._resultHandler(newData || []);
|
|
2795
2822
|
}
|
|
2796
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2797
|
-
if (this.$state.get(
|
|
2798
|
-
const returnType = this.$state.get(
|
|
2799
|
-
return this._resultHandler(returnType ===
|
|
2823
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), result || []);
|
|
2824
|
+
if (this.$state.get("RETURN_TYPE") != null) {
|
|
2825
|
+
const returnType = this.$state.get("RETURN_TYPE");
|
|
2826
|
+
return this._resultHandler(returnType === "object"
|
|
2800
2827
|
? result[0]
|
|
2801
|
-
: returnType ===
|
|
2828
|
+
: returnType === "array"
|
|
2829
|
+
? result
|
|
2830
|
+
: [result]);
|
|
2802
2831
|
}
|
|
2803
2832
|
return this._resultHandler(result || []);
|
|
2804
2833
|
});
|
|
@@ -2828,7 +2857,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2828
2857
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2829
2858
|
const sql = this._queryBuilder().select();
|
|
2830
2859
|
const result = yield this._queryStatement(sql);
|
|
2831
|
-
if (this.$state.get(
|
|
2860
|
+
if (this.$state.get("HIDDEN").length)
|
|
2832
2861
|
this._hiddenColumn(result);
|
|
2833
2862
|
return this._resultHandler(JSON.stringify(result));
|
|
2834
2863
|
});
|
|
@@ -2843,7 +2872,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2843
2872
|
* @returns {promise<Array>}
|
|
2844
2873
|
*/
|
|
2845
2874
|
toArray() {
|
|
2846
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2875
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2847
2876
|
this.selectRaw(`${this.bindColumn(column)}`);
|
|
2848
2877
|
const sql = this._queryBuilder().select();
|
|
2849
2878
|
const result = yield this._queryStatement(sql);
|
|
@@ -2862,15 +2891,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2862
2891
|
var _a;
|
|
2863
2892
|
const sql = new Builder()
|
|
2864
2893
|
.copyBuilder(this, { where: true, limit: true, join: true })
|
|
2865
|
-
.selectRaw(
|
|
2894
|
+
.selectRaw("1")
|
|
2866
2895
|
.limit(1)
|
|
2867
2896
|
.toString();
|
|
2868
2897
|
const result = yield this._queryStatement([
|
|
2869
|
-
`${this.$constants(
|
|
2870
|
-
`${this.$constants(
|
|
2898
|
+
`${this.$constants("SELECT")}`,
|
|
2899
|
+
`${this.$constants("EXISTS")}`,
|
|
2871
2900
|
`(${sql})`,
|
|
2872
|
-
`${this.$constants(
|
|
2873
|
-
].join(
|
|
2901
|
+
`${this.$constants("AS")} \`aggregate\``,
|
|
2902
|
+
].join(" "));
|
|
2874
2903
|
return Boolean(this._resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || false));
|
|
2875
2904
|
});
|
|
2876
2905
|
}
|
|
@@ -2882,14 +2911,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2882
2911
|
* @returns {promise<number>}
|
|
2883
2912
|
*/
|
|
2884
2913
|
count() {
|
|
2885
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2886
|
-
const distinct = this.$state.get(
|
|
2887
|
-
column =
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2914
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2915
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2916
|
+
column =
|
|
2917
|
+
column === "*"
|
|
2918
|
+
? "*"
|
|
2919
|
+
: distinct
|
|
2920
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2921
|
+
: `${this.bindColumn(column)}`;
|
|
2922
|
+
this.selectRaw(`${this.$constants("COUNT")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2893
2923
|
const sql = this._queryBuilder().select();
|
|
2894
2924
|
const result = yield this._queryStatement(sql);
|
|
2895
2925
|
return Number(this._resultHandler(result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) || 0));
|
|
@@ -2903,13 +2933,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2903
2933
|
* @returns {promise<number>}
|
|
2904
2934
|
*/
|
|
2905
2935
|
avg() {
|
|
2906
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2907
|
-
const distinct = this.$state.get(
|
|
2908
|
-
column = distinct
|
|
2909
|
-
|
|
2936
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2937
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2938
|
+
column = distinct
|
|
2939
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2940
|
+
: `${this.bindColumn(column)}`;
|
|
2941
|
+
this.selectRaw(`${this.$constants("AVG")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2910
2942
|
const sql = this._queryBuilder().select();
|
|
2911
2943
|
const result = yield this._queryStatement(sql);
|
|
2912
|
-
return Number(this._resultHandler((result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) ||
|
|
2944
|
+
return Number(this._resultHandler((result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) ||
|
|
2945
|
+
0) / result.length));
|
|
2913
2946
|
});
|
|
2914
2947
|
}
|
|
2915
2948
|
/**
|
|
@@ -2920,10 +2953,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2920
2953
|
* @returns {promise<number>}
|
|
2921
2954
|
*/
|
|
2922
2955
|
sum() {
|
|
2923
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2924
|
-
const distinct = this.$state.get(
|
|
2925
|
-
column = distinct
|
|
2926
|
-
|
|
2956
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2957
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2958
|
+
column = distinct
|
|
2959
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2960
|
+
: `${this.bindColumn(column)}`;
|
|
2961
|
+
this.selectRaw(`${this.$constants("SUM")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2927
2962
|
const sql = this._queryBuilder().select();
|
|
2928
2963
|
const result = yield this._queryStatement(sql);
|
|
2929
2964
|
return Number(this._resultHandler(result.reduce((prev, cur) => { var _a; return prev + Number((_a = cur === null || cur === void 0 ? void 0 : cur.aggregate) !== null && _a !== void 0 ? _a : 0); }, 0) || 0));
|
|
@@ -2937,11 +2972,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2937
2972
|
* @returns {promise<number>}
|
|
2938
2973
|
*/
|
|
2939
2974
|
max() {
|
|
2940
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2975
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2941
2976
|
var _a;
|
|
2942
|
-
const distinct = this.$state.get(
|
|
2943
|
-
column = distinct
|
|
2944
|
-
|
|
2977
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2978
|
+
column = distinct
|
|
2979
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2980
|
+
: `${this.bindColumn(column)}`;
|
|
2981
|
+
this.selectRaw(`${this.$constants("MAX")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2945
2982
|
const sql = this._queryBuilder().select();
|
|
2946
2983
|
const result = yield this._queryStatement(sql);
|
|
2947
2984
|
return Number(this._resultHandler(((_a = result.sort((a, b) => (b === null || b === void 0 ? void 0 : b.aggregate) - (a === null || a === void 0 ? void 0 : a.aggregate))[0]) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
|
|
@@ -2955,11 +2992,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2955
2992
|
* @returns {promise<number>}
|
|
2956
2993
|
*/
|
|
2957
2994
|
min() {
|
|
2958
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2995
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2959
2996
|
var _a;
|
|
2960
|
-
const distinct = this.$state.get(
|
|
2961
|
-
column = distinct
|
|
2962
|
-
|
|
2997
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2998
|
+
column = distinct
|
|
2999
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
3000
|
+
: `${this.bindColumn(column)}`;
|
|
3001
|
+
this.selectRaw(`${this.$constants("MIN")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2963
3002
|
const sql = this._queryBuilder().select();
|
|
2964
3003
|
const result = yield this._queryStatement(sql);
|
|
2965
3004
|
return Number(this._resultHandler(((_a = result.sort((a, b) => (a === null || a === void 0 ? void 0 : a.aggregate) - (b === null || b === void 0 ? void 0 : b.aggregate))[0]) === null || _a === void 0 ? void 0 : _a.aggregate) || 0));
|
|
@@ -2973,16 +3012,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2973
3012
|
*/
|
|
2974
3013
|
delete() {
|
|
2975
3014
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2976
|
-
if (!this.$state.get(
|
|
3015
|
+
if (!this.$state.get("WHERE").length) {
|
|
2977
3016
|
throw new Error("can't delete without where condition");
|
|
2978
3017
|
}
|
|
2979
3018
|
this.limit(1);
|
|
2980
|
-
this.$state.set(
|
|
2981
|
-
`${this.$constants(
|
|
2982
|
-
`${this.$constants(
|
|
2983
|
-
`${this.$state.get(
|
|
2984
|
-
].join(
|
|
2985
|
-
const result = yield this._actionStatement({
|
|
3019
|
+
this.$state.set("DELETE", [
|
|
3020
|
+
`${this.$constants("DELETE")}`,
|
|
3021
|
+
`${this.$constants("FROM")}`,
|
|
3022
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3023
|
+
].join(" "));
|
|
3024
|
+
const result = yield this._actionStatement({
|
|
3025
|
+
sql: this._queryBuilder().delete(),
|
|
3026
|
+
});
|
|
2986
3027
|
if (result)
|
|
2987
3028
|
return Boolean(this._resultHandler(!!result || false));
|
|
2988
3029
|
return Boolean(this._resultHandler(false));
|
|
@@ -2996,15 +3037,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2996
3037
|
*/
|
|
2997
3038
|
deleteMany() {
|
|
2998
3039
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2999
|
-
if (!this.$state.get(
|
|
3040
|
+
if (!this.$state.get("WHERE").length) {
|
|
3000
3041
|
throw new Error("can't delete without where condition");
|
|
3001
3042
|
}
|
|
3002
|
-
this.$state.set(
|
|
3003
|
-
`${this.$constants(
|
|
3004
|
-
`${this.$constants(
|
|
3005
|
-
`${this.$state.get(
|
|
3006
|
-
].join(
|
|
3007
|
-
const result = yield this._actionStatement({
|
|
3043
|
+
this.$state.set("DELETE", [
|
|
3044
|
+
`${this.$constants("DELETE")}`,
|
|
3045
|
+
`${this.$constants("FROM")}`,
|
|
3046
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3047
|
+
].join(" "));
|
|
3048
|
+
const result = yield this._actionStatement({
|
|
3049
|
+
sql: this._queryBuilder().delete(),
|
|
3050
|
+
});
|
|
3008
3051
|
if (result)
|
|
3009
3052
|
return Boolean(this._resultHandler(!!result || false));
|
|
3010
3053
|
return Boolean(this._resultHandler(false));
|
|
@@ -3021,12 +3064,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3021
3064
|
*/
|
|
3022
3065
|
forceDelete() {
|
|
3023
3066
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3024
|
-
this.$state.set(
|
|
3025
|
-
`${this.$constants(
|
|
3026
|
-
`${this.$constants(
|
|
3027
|
-
`${this.$state.get(
|
|
3028
|
-
].join(
|
|
3029
|
-
const result = yield this._actionStatement({
|
|
3067
|
+
this.$state.set("DELETE", [
|
|
3068
|
+
`${this.$constants("DELETE")}`,
|
|
3069
|
+
`${this.$constants("FROM")}`,
|
|
3070
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3071
|
+
].join(" "));
|
|
3072
|
+
const result = yield this._actionStatement({
|
|
3073
|
+
sql: this._queryBuilder().delete(),
|
|
3074
|
+
});
|
|
3030
3075
|
if (result)
|
|
3031
3076
|
return Boolean(this._resultHandler(!!result || false));
|
|
3032
3077
|
return Boolean(this._resultHandler(!!result || false));
|
|
@@ -3050,34 +3095,34 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3050
3095
|
getGroupBy(column) {
|
|
3051
3096
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3052
3097
|
var _a, _b, _c;
|
|
3053
|
-
if ((_a = this.$state.get(
|
|
3054
|
-
this.select(...yield this.exceptColumns());
|
|
3098
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
3099
|
+
this.select(...(yield this.exceptColumns()));
|
|
3055
3100
|
const results = yield new Builder()
|
|
3056
3101
|
.copyBuilder(this, {
|
|
3057
3102
|
where: true,
|
|
3058
3103
|
limit: true,
|
|
3059
3104
|
join: true,
|
|
3060
|
-
orderBy: true
|
|
3105
|
+
orderBy: true,
|
|
3061
3106
|
})
|
|
3062
3107
|
.select(column)
|
|
3063
3108
|
.selectRaw([
|
|
3064
|
-
`${this.$constants(
|
|
3065
|
-
`${this.$constants(
|
|
3066
|
-
].join(
|
|
3109
|
+
`${this.$constants("GROUP_CONCAT")}(${this.bindColumn("id")})`,
|
|
3110
|
+
`${this.$constants("AS")} \`aggregate\``,
|
|
3111
|
+
].join(" "))
|
|
3067
3112
|
.groupBy(column)
|
|
3068
3113
|
.oldest()
|
|
3069
3114
|
.bind(this.$pool.get())
|
|
3070
|
-
.debug(this.$state.get(
|
|
3115
|
+
.debug(this.$state.get("DEBUG"))
|
|
3071
3116
|
.get();
|
|
3072
3117
|
const ids = [];
|
|
3073
3118
|
for (const r of results) {
|
|
3074
|
-
const splits = ((_c = (_b = r === null || r === void 0 ? void 0 : r.aggregate) === null || _b === void 0 ? void 0 : _b.split(
|
|
3119
|
+
const splits = ((_c = (_b = r === null || r === void 0 ? void 0 : r.aggregate) === null || _b === void 0 ? void 0 : _b.split(",")) !== null && _c !== void 0 ? _c : []).map((v) => Number(v));
|
|
3075
3120
|
ids.push(...splits);
|
|
3076
3121
|
}
|
|
3077
3122
|
const grouping = yield new Builder()
|
|
3078
|
-
.whereIn(
|
|
3123
|
+
.whereIn("id", ids)
|
|
3079
3124
|
.bind(this.$pool.get())
|
|
3080
|
-
.debug(this.$state.get(
|
|
3125
|
+
.debug(this.$state.get("DEBUG"))
|
|
3081
3126
|
.get();
|
|
3082
3127
|
const result = grouping.reduce((map, data) => {
|
|
3083
3128
|
const id = data[column];
|
|
@@ -3091,20 +3136,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3091
3136
|
});
|
|
3092
3137
|
}
|
|
3093
3138
|
/**
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3139
|
+
* The 'findGroupBy' method is used to execute a database query and retrieve the result set that matches the query conditions.
|
|
3140
|
+
*
|
|
3141
|
+
* It retrieves multiple records from a database table based on the criteria specified in the query.
|
|
3142
|
+
*
|
|
3143
|
+
* It returns record to new Map
|
|
3144
|
+
* @param {string} column
|
|
3145
|
+
* @example
|
|
3146
|
+
* const results = await new DB('posts')
|
|
3147
|
+
* .findGroupBy('user_id')
|
|
3148
|
+
*
|
|
3149
|
+
* // you can find with user id in the results
|
|
3150
|
+
* const postsByUserId1 = results.get(1)
|
|
3151
|
+
* @returns {promise<Array>}
|
|
3152
|
+
*/
|
|
3108
3153
|
findGroupBy(column) {
|
|
3109
3154
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3110
3155
|
return yield this.getGroupBy(column);
|
|
@@ -3118,15 +3163,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3118
3163
|
*/
|
|
3119
3164
|
save() {
|
|
3120
3165
|
return __awaiter(this, arguments, void 0, function* ({ waitMs = 0 } = {}) {
|
|
3121
|
-
this.$state.set(
|
|
3122
|
-
switch (this.$state.get(
|
|
3123
|
-
case
|
|
3124
|
-
|
|
3125
|
-
case
|
|
3126
|
-
|
|
3127
|
-
case
|
|
3128
|
-
|
|
3129
|
-
|
|
3166
|
+
this.$state.set("AFTER_SAVE", waitMs);
|
|
3167
|
+
switch (this.$state.get("SAVE")) {
|
|
3168
|
+
case "INSERT":
|
|
3169
|
+
return yield this._insert();
|
|
3170
|
+
case "UPDATE":
|
|
3171
|
+
return yield this._update();
|
|
3172
|
+
case "INSERT_MULTIPLE":
|
|
3173
|
+
return yield this._insertMultiple();
|
|
3174
|
+
case "INSERT_NOT_EXISTS":
|
|
3175
|
+
return yield this._insertNotExists();
|
|
3176
|
+
case "UPDATE_OR_INSERT":
|
|
3177
|
+
return yield this._updateOrInsert();
|
|
3178
|
+
case "INSERT_OR_SELECT":
|
|
3179
|
+
return yield this._insertOrSelect();
|
|
3180
|
+
default:
|
|
3181
|
+
throw new Error(`unknown this [${this.$state.get("SAVE")}]`);
|
|
3130
3182
|
}
|
|
3131
3183
|
});
|
|
3132
3184
|
}
|
|
@@ -3139,14 +3191,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3139
3191
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3140
3192
|
const makeStatement = (columns) => {
|
|
3141
3193
|
return [
|
|
3142
|
-
`${this.$constants(
|
|
3143
|
-
`${columns.join(
|
|
3144
|
-
`${this.$constants(
|
|
3194
|
+
`${this.$constants("SELECT")}`,
|
|
3195
|
+
`${columns.join(", ")}`,
|
|
3196
|
+
`${this.$constants("FROM")}`,
|
|
3145
3197
|
`\`${this.getTableName()}\``,
|
|
3146
|
-
].join(
|
|
3198
|
+
].join(" ");
|
|
3147
3199
|
};
|
|
3148
3200
|
const schemaTable = yield this.getSchema();
|
|
3149
|
-
const columns = schemaTable.map(column => this.bindColumn(column.Field));
|
|
3201
|
+
const columns = schemaTable.map((column) => this.bindColumn(column.Field));
|
|
3150
3202
|
return makeStatement(columns);
|
|
3151
3203
|
});
|
|
3152
3204
|
}
|
|
@@ -3159,15 +3211,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3159
3211
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3160
3212
|
const makeStatement = (columns) => {
|
|
3161
3213
|
return [
|
|
3162
|
-
`${this.$constants(
|
|
3214
|
+
`${this.$constants("INSERT")}`,
|
|
3163
3215
|
`\`${this.getTableName()}\``,
|
|
3164
|
-
`(${columns.join(
|
|
3165
|
-
`${this.$constants(
|
|
3166
|
-
`(${Array(columns.length).fill(
|
|
3167
|
-
].join(
|
|
3216
|
+
`(${columns.join(", ")})`,
|
|
3217
|
+
`${this.$constants("VALUES")}`,
|
|
3218
|
+
`(${Array(columns.length).fill("`?`").join(" , ")})`,
|
|
3219
|
+
].join(" ");
|
|
3168
3220
|
};
|
|
3169
3221
|
const schemaTable = yield this.getSchema();
|
|
3170
|
-
const columns = schemaTable.map(column => `\`${column.Field}\``);
|
|
3222
|
+
const columns = schemaTable.map((column) => `\`${column.Field}\``);
|
|
3171
3223
|
return makeStatement(columns);
|
|
3172
3224
|
});
|
|
3173
3225
|
}
|
|
@@ -3180,16 +3232,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3180
3232
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3181
3233
|
const makeStatement = (columns) => {
|
|
3182
3234
|
return [
|
|
3183
|
-
`${this.$constants(
|
|
3235
|
+
`${this.$constants("UPDATE")}`,
|
|
3184
3236
|
`\`${this.getTableName()}\``,
|
|
3185
|
-
`${this.$constants(
|
|
3186
|
-
`(${columns.join(
|
|
3187
|
-
`${this.$constants(
|
|
3188
|
-
`${this.bindColumn(
|
|
3189
|
-
].join(
|
|
3237
|
+
`${this.$constants("SET")}`,
|
|
3238
|
+
`(${columns.join(", ")})`,
|
|
3239
|
+
`${this.$constants("WHERE")}`,
|
|
3240
|
+
`${this.bindColumn("id")} = '?'`,
|
|
3241
|
+
].join(" ");
|
|
3190
3242
|
};
|
|
3191
3243
|
const schemaTable = yield this.getSchema();
|
|
3192
|
-
const columns = schemaTable.map(column => `${this.bindColumn(column.Field)} = '?'`);
|
|
3244
|
+
const columns = schemaTable.map((column) => `${this.bindColumn(column.Field)} = '?'`);
|
|
3193
3245
|
return makeStatement(columns);
|
|
3194
3246
|
});
|
|
3195
3247
|
}
|
|
@@ -3202,12 +3254,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3202
3254
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3203
3255
|
const makeStatement = () => {
|
|
3204
3256
|
return [
|
|
3205
|
-
`${this.$constants(
|
|
3206
|
-
`${this.$constants(
|
|
3257
|
+
`${this.$constants("DELETE")}`,
|
|
3258
|
+
`${this.$constants("FROM")}`,
|
|
3207
3259
|
`\`${this.getTableName()}\``,
|
|
3208
|
-
`${this.$constants(
|
|
3209
|
-
`${this.bindColumn(
|
|
3210
|
-
].join(
|
|
3260
|
+
`${this.$constants("WHERE")}`,
|
|
3261
|
+
`${this.bindColumn("id")} = \`?\``,
|
|
3262
|
+
].join(" ");
|
|
3211
3263
|
};
|
|
3212
3264
|
return makeStatement();
|
|
3213
3265
|
});
|
|
@@ -3221,13 +3273,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3221
3273
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3222
3274
|
const makeStatement = (columns) => {
|
|
3223
3275
|
return [
|
|
3224
|
-
`${this.$constants(
|
|
3276
|
+
`${this.$constants("CREATE_TABLE_NOT_EXISTS")}`,
|
|
3225
3277
|
`\`${this.getTableName()}\``,
|
|
3226
3278
|
`(`,
|
|
3227
|
-
`\n${columns === null || columns === void 0 ? void 0 : columns.join(
|
|
3279
|
+
`\n${columns === null || columns === void 0 ? void 0 : columns.join(",\n")}`,
|
|
3228
3280
|
`\n)`,
|
|
3229
|
-
`${this.$constants(
|
|
3230
|
-
].join(
|
|
3281
|
+
`${this.$constants("ENGINE")}`,
|
|
3282
|
+
].join(" ");
|
|
3231
3283
|
};
|
|
3232
3284
|
const columns = yield this.showSchema();
|
|
3233
3285
|
return makeStatement(columns);
|
|
@@ -3241,13 +3293,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3241
3293
|
showTables() {
|
|
3242
3294
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3243
3295
|
const sql = [
|
|
3244
|
-
`${this.$constants(
|
|
3245
|
-
`${this.$constants(
|
|
3246
|
-
].join(
|
|
3296
|
+
`${this.$constants("SHOW")}`,
|
|
3297
|
+
`${this.$constants("TABLES")}`,
|
|
3298
|
+
].join(" ");
|
|
3247
3299
|
const results = yield this._queryStatement(sql);
|
|
3248
3300
|
return results
|
|
3249
|
-
.map(table => String(Object.values(table)[0]))
|
|
3250
|
-
.filter(d => d != null || d !==
|
|
3301
|
+
.map((table) => String(Object.values(table)[0]))
|
|
3302
|
+
.filter((d) => d != null || d !== "");
|
|
3251
3303
|
});
|
|
3252
3304
|
}
|
|
3253
3305
|
/**
|
|
@@ -3258,13 +3310,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3258
3310
|
* @returns {Promise<Array>}
|
|
3259
3311
|
*/
|
|
3260
3312
|
showColumns() {
|
|
3261
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3313
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3262
3314
|
const sql = [
|
|
3263
|
-
`${this.$constants(
|
|
3264
|
-
`${this.$constants(
|
|
3265
|
-
`${this.$constants(
|
|
3266
|
-
`\`${table.replace(/\`/g,
|
|
3267
|
-
].join(
|
|
3315
|
+
`${this.$constants("SHOW")}`,
|
|
3316
|
+
`${this.$constants("COLUMNS")}`,
|
|
3317
|
+
`${this.$constants("FROM")}`,
|
|
3318
|
+
`\`${table.replace(/\`/g, "")}\``,
|
|
3319
|
+
].join(" ");
|
|
3268
3320
|
const rawColumns = yield this._queryStatement(sql);
|
|
3269
3321
|
const columns = rawColumns.map((column) => column.Field);
|
|
3270
3322
|
return columns;
|
|
@@ -3277,28 +3329,28 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3277
3329
|
* @returns {Promise<Array>}
|
|
3278
3330
|
*/
|
|
3279
3331
|
showSchema() {
|
|
3280
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3332
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3281
3333
|
const sql = [
|
|
3282
|
-
`${this.$constants(
|
|
3283
|
-
`${this.$constants(
|
|
3284
|
-
`${this.$constants(
|
|
3285
|
-
`\`${table.replace(/\`/g,
|
|
3286
|
-
].join(
|
|
3334
|
+
`${this.$constants("SHOW")}`,
|
|
3335
|
+
`${this.$constants("COLUMNS")}`,
|
|
3336
|
+
`${this.$constants("FROM")}`,
|
|
3337
|
+
`\`${table.replace(/\`/g, "")}\``,
|
|
3338
|
+
].join(" ");
|
|
3287
3339
|
const raws = yield this._queryStatement(sql);
|
|
3288
3340
|
return raws.map((r) => {
|
|
3289
3341
|
const schema = [];
|
|
3290
3342
|
schema.push(`\`${r.Field}\``);
|
|
3291
3343
|
schema.push(`${r.Type}`);
|
|
3292
|
-
if (r.Null ===
|
|
3344
|
+
if (r.Null === "YES") {
|
|
3293
3345
|
schema.push(`NULL`);
|
|
3294
3346
|
}
|
|
3295
|
-
if (r.Null ===
|
|
3347
|
+
if (r.Null === "NO") {
|
|
3296
3348
|
schema.push(`NOT NULL`);
|
|
3297
3349
|
}
|
|
3298
|
-
if (r.Key ===
|
|
3350
|
+
if (r.Key === "PRI") {
|
|
3299
3351
|
schema.push(`PRIMARY KEY`);
|
|
3300
3352
|
}
|
|
3301
|
-
if (r.Key ===
|
|
3353
|
+
if (r.Key === "UNI") {
|
|
3302
3354
|
schema.push(`UNIQUE`);
|
|
3303
3355
|
}
|
|
3304
3356
|
if (r.Default) {
|
|
@@ -3307,7 +3359,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3307
3359
|
if (r.Extra) {
|
|
3308
3360
|
schema.push(`${r.Extra.toUpperCase()}`);
|
|
3309
3361
|
}
|
|
3310
|
-
return schema.join(
|
|
3362
|
+
return schema.join(" ");
|
|
3311
3363
|
});
|
|
3312
3364
|
});
|
|
3313
3365
|
}
|
|
@@ -3318,7 +3370,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3318
3370
|
* @returns {Promise<Array>}
|
|
3319
3371
|
*/
|
|
3320
3372
|
showSchemas() {
|
|
3321
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3373
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3322
3374
|
return this.showSchema(table);
|
|
3323
3375
|
});
|
|
3324
3376
|
}
|
|
@@ -3330,22 +3382,26 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3330
3382
|
* @returns {Promise<Array>}
|
|
3331
3383
|
*/
|
|
3332
3384
|
showValues() {
|
|
3333
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3385
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3334
3386
|
const sql = [
|
|
3335
|
-
`${this.$constants(
|
|
3336
|
-
|
|
3337
|
-
`${this.$constants(
|
|
3338
|
-
`\`${table.replace(/\`/g,
|
|
3339
|
-
].join(
|
|
3387
|
+
`${this.$constants("SELECT")}`,
|
|
3388
|
+
"*",
|
|
3389
|
+
`${this.$constants("FROM")}`,
|
|
3390
|
+
`\`${table.replace(/\`/g, "")}\``,
|
|
3391
|
+
].join(" ");
|
|
3340
3392
|
const raw = yield this._queryStatement(sql);
|
|
3341
3393
|
const values = raw.map((value) => {
|
|
3342
|
-
return `(${Object.values(value)
|
|
3343
|
-
|
|
3394
|
+
return `(${Object.values(value)
|
|
3395
|
+
.map((v) => {
|
|
3396
|
+
if (this.$utils.typeOf(v) === "date")
|
|
3344
3397
|
return `'${this.$utils.timestamp(v)}'`;
|
|
3345
|
-
if (this.$utils.typeOf(v) ===
|
|
3398
|
+
if (this.$utils.typeOf(v) === "object" &&
|
|
3399
|
+
v != null &&
|
|
3400
|
+
!Array.isArray(v))
|
|
3346
3401
|
return `'${JSON.stringify(v)}'`;
|
|
3347
|
-
return v == null ? this.$constants(
|
|
3348
|
-
})
|
|
3402
|
+
return v == null ? this.$constants("NULL") : `'${v}'`;
|
|
3403
|
+
})
|
|
3404
|
+
.join(", ")})`;
|
|
3349
3405
|
});
|
|
3350
3406
|
return values;
|
|
3351
3407
|
});
|
|
@@ -3360,26 +3416,27 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3360
3416
|
*/
|
|
3361
3417
|
faker(rows, cb) {
|
|
3362
3418
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3363
|
-
if (this.$state.get(
|
|
3419
|
+
if (this.$state.get("TABLE_NAME") === "" ||
|
|
3420
|
+
this.$state.get("TABLE_NAME") == null) {
|
|
3364
3421
|
throw new Error("Unknow this table name");
|
|
3365
3422
|
}
|
|
3366
3423
|
const sql = [
|
|
3367
|
-
`${this.$constants(
|
|
3368
|
-
`${this.$constants(
|
|
3369
|
-
`${this.$constants(
|
|
3370
|
-
`${this.$state.get(
|
|
3371
|
-
].join(
|
|
3424
|
+
`${this.$constants("SHOW")}`,
|
|
3425
|
+
`${this.$constants("FIELDS")}`,
|
|
3426
|
+
`${this.$constants("FROM")}`,
|
|
3427
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3428
|
+
].join(" ");
|
|
3372
3429
|
const fields = yield this._queryStatement(sql);
|
|
3373
3430
|
const fakers = [];
|
|
3374
|
-
const uuid =
|
|
3375
|
-
const passed = (field) => [
|
|
3431
|
+
const uuid = "uuid";
|
|
3432
|
+
const passed = (field) => ["id", "_id"].some((p) => field === p);
|
|
3376
3433
|
for (let row = 0; row < rows; row++) {
|
|
3377
3434
|
let columnAndValue = {};
|
|
3378
3435
|
for (const { Field: field, Type: type } of fields) {
|
|
3379
3436
|
if (passed(field))
|
|
3380
3437
|
continue;
|
|
3381
3438
|
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: field === uuid
|
|
3382
|
-
? this.$utils.faker(
|
|
3439
|
+
? this.$utils.faker("uuid")
|
|
3383
3440
|
: this.$utils.faker(type) });
|
|
3384
3441
|
}
|
|
3385
3442
|
if (cb) {
|
|
@@ -3394,8 +3451,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3394
3451
|
for (const data of chunked) {
|
|
3395
3452
|
promises.push(() => {
|
|
3396
3453
|
return new DB_1.DB(table)
|
|
3397
|
-
.debug(this.$state.get(
|
|
3398
|
-
.createMultiple([...data])
|
|
3454
|
+
.debug(this.$state.get("DEBUG"))
|
|
3455
|
+
.createMultiple([...data])
|
|
3456
|
+
.void()
|
|
3399
3457
|
.save();
|
|
3400
3458
|
});
|
|
3401
3459
|
}
|
|
@@ -3411,17 +3469,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3411
3469
|
* @returns {promise<boolean>}
|
|
3412
3470
|
*/
|
|
3413
3471
|
truncate() {
|
|
3414
|
-
return __awaiter(this, arguments, void 0, function* ({ force = false } = {}) {
|
|
3415
|
-
if (this.$state.get(
|
|
3472
|
+
return __awaiter(this, arguments, void 0, function* ({ force = false, } = {}) {
|
|
3473
|
+
if (this.$state.get("TABLE_NAME") == null ||
|
|
3474
|
+
this.$state.get("TABLE_NAME") === "") {
|
|
3416
3475
|
console.log(`Please set the your table name`);
|
|
3417
3476
|
return false;
|
|
3418
3477
|
}
|
|
3419
3478
|
const sql = [
|
|
3420
|
-
`${this.$constants(
|
|
3421
|
-
`${this.$state.get(
|
|
3422
|
-
].join(
|
|
3479
|
+
`${this.$constants("TRUNCATE_TABLE")}`,
|
|
3480
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3481
|
+
].join(" ");
|
|
3423
3482
|
if (!force) {
|
|
3424
|
-
console.log(`Truncating will delete all data from the table '${this.$state.get(
|
|
3483
|
+
console.log(`Truncating will delete all data from the table '${this.$state.get("TABLE_NAME")}'. Are you sure you want to proceed?. Please confirm if you want to force the operation.`);
|
|
3425
3484
|
return false;
|
|
3426
3485
|
}
|
|
3427
3486
|
yield this._queryStatement(sql);
|
|
@@ -3438,16 +3497,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3438
3497
|
*/
|
|
3439
3498
|
drop() {
|
|
3440
3499
|
return __awaiter(this, arguments, void 0, function* ({ force = false } = {}) {
|
|
3441
|
-
if (this.$state.get(
|
|
3500
|
+
if (this.$state.get("TABLE_NAME") == null ||
|
|
3501
|
+
this.$state.get("TABLE_NAME") === "") {
|
|
3442
3502
|
console.log(`Please set the your table name`);
|
|
3443
3503
|
return false;
|
|
3444
3504
|
}
|
|
3445
3505
|
const sql = [
|
|
3446
|
-
`${this.$constants(
|
|
3447
|
-
`${this.$state.get(
|
|
3448
|
-
].join(
|
|
3506
|
+
`${this.$constants("DROP_TABLE")}`,
|
|
3507
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3508
|
+
].join(" ");
|
|
3449
3509
|
if (!force) {
|
|
3450
|
-
console.log(`Droping will drop the table '${this.$state.get(
|
|
3510
|
+
console.log(`Droping will drop the table '${this.$state.get("TABLE_NAME")}'. Are you sure you want to proceed?. Please confirm if you want to force the operation.`);
|
|
3451
3511
|
return false;
|
|
3452
3512
|
}
|
|
3453
3513
|
yield this._queryStatement(sql);
|
|
@@ -3456,171 +3516,186 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3456
3516
|
}
|
|
3457
3517
|
exceptColumns() {
|
|
3458
3518
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3459
|
-
const excepts = this.$state.get(
|
|
3519
|
+
const excepts = this.$state.get("EXCEPTS");
|
|
3460
3520
|
const hasDot = excepts.some((except) => /\./.test(except));
|
|
3461
|
-
const names = excepts
|
|
3521
|
+
const names = excepts
|
|
3522
|
+
.map((except) => {
|
|
3462
3523
|
if (/\./.test(except))
|
|
3463
|
-
return except.split(
|
|
3524
|
+
return except.split(".")[0];
|
|
3464
3525
|
return null;
|
|
3465
|
-
})
|
|
3466
|
-
|
|
3526
|
+
})
|
|
3527
|
+
.filter((d) => d != null);
|
|
3528
|
+
const tableNames = names.length
|
|
3529
|
+
? [...new Set(names)]
|
|
3530
|
+
: [this.$state.get("TABLE_NAME")];
|
|
3467
3531
|
const removeExcepts = [];
|
|
3468
3532
|
for (const tableName of tableNames) {
|
|
3469
3533
|
const sql = [
|
|
3470
|
-
`${this.$constants(
|
|
3471
|
-
`${this.$constants(
|
|
3472
|
-
`${this.$constants(
|
|
3473
|
-
`${tableName}
|
|
3474
|
-
].join(
|
|
3534
|
+
`${this.$constants("SHOW")}`,
|
|
3535
|
+
`${this.$constants("COLUMNS")}`,
|
|
3536
|
+
`${this.$constants("FROM")}`,
|
|
3537
|
+
`${tableName}`,
|
|
3538
|
+
].join(" ");
|
|
3475
3539
|
const rawColumns = yield this._queryStatement(sql);
|
|
3476
3540
|
const columns = rawColumns.map((column) => column.Field);
|
|
3477
3541
|
const removeExcept = columns.filter((column) => {
|
|
3478
3542
|
return excepts.every((except) => {
|
|
3479
3543
|
if (/\./.test(except)) {
|
|
3480
|
-
const [table, _] = except.split(
|
|
3544
|
+
const [table, _] = except.split(".");
|
|
3481
3545
|
return except !== `${table}.${column}`;
|
|
3482
3546
|
}
|
|
3483
3547
|
return except !== column;
|
|
3484
3548
|
});
|
|
3485
3549
|
});
|
|
3486
|
-
removeExcepts.push(hasDot ? removeExcept.map(r => `${tableName}.${r}`) : removeExcept);
|
|
3550
|
+
removeExcepts.push(hasDot ? removeExcept.map((r) => `${tableName}.${r}`) : removeExcept);
|
|
3487
3551
|
}
|
|
3488
3552
|
return removeExcepts.flat();
|
|
3489
3553
|
});
|
|
3490
3554
|
}
|
|
3491
3555
|
_updateHandler(column, value) {
|
|
3492
3556
|
return DB_1.DB.raw([
|
|
3493
|
-
this.$constants(
|
|
3494
|
-
this.$constants(
|
|
3495
|
-
`(\`${column}\` = "" ${this.$constants(
|
|
3496
|
-
this.$constants(
|
|
3497
|
-
`"${value !== null && value !== void 0 ? value : ""}" ${this.$constants(
|
|
3498
|
-
this.$constants(
|
|
3499
|
-
].join(
|
|
3557
|
+
this.$constants("CASE"),
|
|
3558
|
+
this.$constants("WHEN"),
|
|
3559
|
+
`(\`${column}\` = "" ${this.$constants("OR")} \`${column}\` ${this.$constants("IS_NULL")})`,
|
|
3560
|
+
this.$constants("THEN"),
|
|
3561
|
+
`"${value !== null && value !== void 0 ? value : ""}" ${this.$constants("ELSE")} \`${column}\``,
|
|
3562
|
+
this.$constants("END"),
|
|
3563
|
+
].join(" "));
|
|
3500
3564
|
}
|
|
3501
3565
|
copyBuilder(instance, options) {
|
|
3502
3566
|
if (!(instance instanceof Builder))
|
|
3503
|
-
throw new Error(
|
|
3567
|
+
throw new Error("Value is not a instanceof Builder");
|
|
3504
3568
|
const copy = Object.fromEntries(instance.$state.get());
|
|
3505
3569
|
const newInstance = new Builder();
|
|
3506
3570
|
newInstance.$state.clone(copy);
|
|
3507
|
-
newInstance.$state.set(
|
|
3508
|
-
newInstance.$state.set(
|
|
3571
|
+
newInstance.$state.set("SAVE", "");
|
|
3572
|
+
newInstance.$state.set("DEBUG", false);
|
|
3509
3573
|
if ((options === null || options === void 0 ? void 0 : options.insert) == null || !options.insert)
|
|
3510
|
-
newInstance.$state.set(
|
|
3574
|
+
newInstance.$state.set("INSERT", "");
|
|
3511
3575
|
if ((options === null || options === void 0 ? void 0 : options.update) == null || !options.update)
|
|
3512
|
-
newInstance.$state.set(
|
|
3576
|
+
newInstance.$state.set("UPDATE", "");
|
|
3513
3577
|
if ((options === null || options === void 0 ? void 0 : options.delete) == null || !options.delete)
|
|
3514
|
-
newInstance.$state.set(
|
|
3578
|
+
newInstance.$state.set("DELETE", "");
|
|
3515
3579
|
if ((options === null || options === void 0 ? void 0 : options.where) == null || !options.where)
|
|
3516
|
-
newInstance.$state.set(
|
|
3580
|
+
newInstance.$state.set("WHERE", []);
|
|
3517
3581
|
if ((options === null || options === void 0 ? void 0 : options.limit) == null || !options.limit)
|
|
3518
|
-
newInstance.$state.set(
|
|
3582
|
+
newInstance.$state.set("LIMIT", "");
|
|
3519
3583
|
if ((options === null || options === void 0 ? void 0 : options.offset) == null || !options.offset)
|
|
3520
|
-
newInstance.$state.set(
|
|
3584
|
+
newInstance.$state.set("OFFSET", "");
|
|
3521
3585
|
if ((options === null || options === void 0 ? void 0 : options.groupBy) == null || !options.groupBy)
|
|
3522
|
-
newInstance.$state.set(
|
|
3586
|
+
newInstance.$state.set("GROUP_BY", "");
|
|
3523
3587
|
if ((options === null || options === void 0 ? void 0 : options.orderBy) == null || !options.orderBy)
|
|
3524
|
-
newInstance.$state.set(
|
|
3588
|
+
newInstance.$state.set("ORDER_BY", []);
|
|
3525
3589
|
if ((options === null || options === void 0 ? void 0 : options.select) == null || !options.select)
|
|
3526
|
-
newInstance.$state.set(
|
|
3590
|
+
newInstance.$state.set("SELECT", []);
|
|
3527
3591
|
if ((options === null || options === void 0 ? void 0 : options.join) == null || !options.join)
|
|
3528
|
-
newInstance.$state.set(
|
|
3592
|
+
newInstance.$state.set("JOIN", []);
|
|
3529
3593
|
if ((options === null || options === void 0 ? void 0 : options.having) == null || !options.having)
|
|
3530
|
-
newInstance.$state.set(
|
|
3594
|
+
newInstance.$state.set("HAVING", "");
|
|
3531
3595
|
return newInstance;
|
|
3532
3596
|
}
|
|
3533
3597
|
_queryBuilder() {
|
|
3534
3598
|
return this._buildQueryStatement();
|
|
3535
3599
|
}
|
|
3536
3600
|
_buildQueryStatement() {
|
|
3537
|
-
const buildSQL = (sql) => sql
|
|
3601
|
+
const buildSQL = (sql) => sql
|
|
3602
|
+
.filter((s) => s !== "" || s == null)
|
|
3603
|
+
.join(" ")
|
|
3604
|
+
.replace(/\s+/g, " ");
|
|
3538
3605
|
const bindJoin = (values) => {
|
|
3539
3606
|
if (!Array.isArray(values) || !values.length)
|
|
3540
3607
|
return null;
|
|
3541
|
-
return values.join(
|
|
3608
|
+
return values.join(" ");
|
|
3542
3609
|
};
|
|
3543
3610
|
const bindWhere = (values) => {
|
|
3544
3611
|
if (!Array.isArray(values) || !values.length)
|
|
3545
3612
|
return null;
|
|
3546
|
-
return `${this.$constants(
|
|
3613
|
+
return `${this.$constants("WHERE")} ${values
|
|
3614
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
3615
|
+
.join(" ")}`;
|
|
3547
3616
|
};
|
|
3548
3617
|
const bindOrderBy = (values) => {
|
|
3549
3618
|
if (!Array.isArray(values) || !values.length)
|
|
3550
3619
|
return null;
|
|
3551
|
-
return `${this.$constants(
|
|
3620
|
+
return `${this.$constants("ORDER_BY")} ${values
|
|
3621
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
3622
|
+
.join(", ")}`;
|
|
3552
3623
|
};
|
|
3553
3624
|
const bindGroupBy = (values) => {
|
|
3554
3625
|
if (!Array.isArray(values) || !values.length)
|
|
3555
3626
|
return null;
|
|
3556
|
-
return `${this.$constants(
|
|
3627
|
+
return `${this.$constants("GROUP_BY")} ${values
|
|
3628
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
3629
|
+
.join(", ")}`;
|
|
3557
3630
|
};
|
|
3558
3631
|
const bindSelect = (values) => {
|
|
3559
3632
|
if (!values.length) {
|
|
3560
|
-
if (!this.$state.get(
|
|
3561
|
-
return `${this.$constants(
|
|
3562
|
-
return `${this.$constants(
|
|
3633
|
+
if (!this.$state.get("DISTINCT"))
|
|
3634
|
+
return `${this.$constants("SELECT")} *`;
|
|
3635
|
+
return `${this.$constants("SELECT")} ${this.$constants("DISTINCT")} *`;
|
|
3563
3636
|
}
|
|
3564
|
-
const findIndex = values.indexOf(
|
|
3637
|
+
const findIndex = values.indexOf("*");
|
|
3565
3638
|
if (findIndex > -1) {
|
|
3566
3639
|
const removed = values.splice(findIndex, 1);
|
|
3567
3640
|
values.unshift(removed[0]);
|
|
3568
3641
|
}
|
|
3569
|
-
return `${this.$constants(
|
|
3642
|
+
return `${this.$constants("SELECT")} ${values.join(", ")}`;
|
|
3570
3643
|
};
|
|
3571
|
-
const bindFrom = ({ from, table, alias, rawAlias }) => {
|
|
3572
|
-
if (alias != null && alias !==
|
|
3573
|
-
if (rawAlias != null && rawAlias !==
|
|
3574
|
-
const raw = String(rawAlias)
|
|
3575
|
-
|
|
3576
|
-
|
|
3644
|
+
const bindFrom = ({ from, table, alias, rawAlias, }) => {
|
|
3645
|
+
if (alias != null && alias !== "") {
|
|
3646
|
+
if (rawAlias != null && rawAlias !== "") {
|
|
3647
|
+
const raw = String(rawAlias)
|
|
3648
|
+
.replace(/^\(\s*|\s*\)$/g, "")
|
|
3649
|
+
.trim();
|
|
3650
|
+
const normalizedRawAlias = raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
3651
|
+
return `${from} (${normalizedRawAlias}) ${this.$constants("AS")} \`${alias}\``;
|
|
3577
3652
|
}
|
|
3578
|
-
return `${from} ${table} ${this.$constants(
|
|
3653
|
+
return `${from} ${table} ${this.$constants("AS")} \`${alias}\``;
|
|
3579
3654
|
}
|
|
3580
3655
|
return `${from} ${table}`;
|
|
3581
3656
|
};
|
|
3582
3657
|
const bindLimit = (limit) => {
|
|
3583
|
-
if (limit ===
|
|
3584
|
-
return
|
|
3585
|
-
return `${this.$constants(
|
|
3658
|
+
if (limit === "" || limit == null)
|
|
3659
|
+
return "";
|
|
3660
|
+
return `${this.$constants("LIMIT")} ${limit}`;
|
|
3586
3661
|
};
|
|
3587
3662
|
const select = () => {
|
|
3588
3663
|
const sql = buildSQL([
|
|
3589
|
-
bindSelect(this.$state.get(
|
|
3664
|
+
bindSelect(this.$state.get("SELECT")),
|
|
3590
3665
|
bindFrom({
|
|
3591
|
-
from: this.$constants(
|
|
3592
|
-
table: this.$state.get(
|
|
3593
|
-
alias: this.$state.get(
|
|
3594
|
-
rawAlias: this.$state.get(
|
|
3666
|
+
from: this.$constants("FROM"),
|
|
3667
|
+
table: this.$state.get("TABLE_NAME"),
|
|
3668
|
+
alias: this.$state.get("ALIAS"),
|
|
3669
|
+
rawAlias: this.$state.get("RAW_ALIAS"),
|
|
3595
3670
|
}),
|
|
3596
|
-
bindJoin(this.$state.get(
|
|
3597
|
-
bindWhere(this.$state.get(
|
|
3598
|
-
bindGroupBy(this.$state.get(
|
|
3599
|
-
this.$state.get(
|
|
3600
|
-
bindOrderBy(this.$state.get(
|
|
3601
|
-
bindLimit(this.$state.get(
|
|
3602
|
-
this.$state.get(
|
|
3671
|
+
bindJoin(this.$state.get("JOIN")),
|
|
3672
|
+
bindWhere(this.$state.get("WHERE")),
|
|
3673
|
+
bindGroupBy(this.$state.get("GROUP_BY")),
|
|
3674
|
+
this.$state.get("HAVING"),
|
|
3675
|
+
bindOrderBy(this.$state.get("ORDER_BY")),
|
|
3676
|
+
bindLimit(this.$state.get("LIMIT")),
|
|
3677
|
+
this.$state.get("OFFSET"),
|
|
3603
3678
|
]).trimEnd();
|
|
3604
|
-
if (this.$state.get(
|
|
3605
|
-
return `WITH ${this.$state.get(
|
|
3679
|
+
if (this.$state.get("CTE").length) {
|
|
3680
|
+
return `WITH ${this.$state.get("CTE")} ${sql}`;
|
|
3606
3681
|
}
|
|
3607
3682
|
return sql;
|
|
3608
3683
|
};
|
|
3609
|
-
const insert = () => buildSQL([this.$state.get(
|
|
3684
|
+
const insert = () => buildSQL([this.$state.get("INSERT")]);
|
|
3610
3685
|
const update = () => {
|
|
3611
3686
|
return buildSQL([
|
|
3612
|
-
this.$state.get(
|
|
3613
|
-
bindWhere(this.$state.get(
|
|
3614
|
-
bindOrderBy(this.$state.get(
|
|
3615
|
-
bindLimit(this.$state.get(
|
|
3687
|
+
this.$state.get("UPDATE"),
|
|
3688
|
+
bindWhere(this.$state.get("WHERE")),
|
|
3689
|
+
bindOrderBy(this.$state.get("ORDER_BY")),
|
|
3690
|
+
bindLimit(this.$state.get("LIMIT")),
|
|
3616
3691
|
]);
|
|
3617
3692
|
};
|
|
3618
3693
|
const remove = () => {
|
|
3619
3694
|
return buildSQL([
|
|
3620
|
-
this.$state.get(
|
|
3621
|
-
bindWhere(this.$state.get(
|
|
3622
|
-
bindOrderBy(this.$state.get(
|
|
3623
|
-
bindLimit(this.$state.get(
|
|
3695
|
+
this.$state.get("DELETE"),
|
|
3696
|
+
bindWhere(this.$state.get("WHERE")),
|
|
3697
|
+
bindOrderBy(this.$state.get("ORDER_BY")),
|
|
3698
|
+
bindLimit(this.$state.get("LIMIT")),
|
|
3624
3699
|
]);
|
|
3625
3700
|
};
|
|
3626
3701
|
return {
|
|
@@ -3628,55 +3703,55 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3628
3703
|
insert,
|
|
3629
3704
|
update,
|
|
3630
3705
|
delete: remove,
|
|
3631
|
-
where: () => bindWhere(this.$state.get(
|
|
3706
|
+
where: () => bindWhere(this.$state.get("WHERE")),
|
|
3632
3707
|
any: () => {
|
|
3633
|
-
if (this.$state.get(
|
|
3708
|
+
if (this.$state.get("INSERT"))
|
|
3634
3709
|
return insert();
|
|
3635
|
-
if (this.$state.get(
|
|
3710
|
+
if (this.$state.get("UPDATE"))
|
|
3636
3711
|
return update();
|
|
3637
|
-
if (this.$state.get(
|
|
3712
|
+
if (this.$state.get("DELETE"))
|
|
3638
3713
|
return remove();
|
|
3639
3714
|
return select();
|
|
3640
|
-
}
|
|
3715
|
+
},
|
|
3641
3716
|
};
|
|
3642
3717
|
}
|
|
3643
3718
|
_resultHandler(data) {
|
|
3644
|
-
if (!this.$state.get(
|
|
3645
|
-
this.$state.set(
|
|
3719
|
+
if (!this.$state.get("VOID")) {
|
|
3720
|
+
this.$state.set("RESULT", data);
|
|
3646
3721
|
}
|
|
3647
3722
|
this.$state.reset();
|
|
3648
3723
|
this.$logger.reset();
|
|
3649
3724
|
return data;
|
|
3650
3725
|
}
|
|
3651
3726
|
_resultHandlerExists(data) {
|
|
3652
|
-
if (!this.$state.get(
|
|
3653
|
-
this.$state.set(
|
|
3727
|
+
if (!this.$state.get("VOID")) {
|
|
3728
|
+
this.$state.set("RESULT", data);
|
|
3654
3729
|
}
|
|
3655
3730
|
this.$state.reset();
|
|
3656
3731
|
this.$logger.reset();
|
|
3657
3732
|
return data;
|
|
3658
3733
|
}
|
|
3659
3734
|
whereReference(tableAndLocalKey, tableAndForeignKey) {
|
|
3660
|
-
this.$state.set(
|
|
3661
|
-
...this.$state.get(
|
|
3735
|
+
this.$state.set("WHERE", [
|
|
3736
|
+
...this.$state.get("WHERE"),
|
|
3662
3737
|
[
|
|
3663
|
-
this.$state.get(
|
|
3664
|
-
`${tableAndLocalKey} = ${tableAndForeignKey}
|
|
3665
|
-
].join(
|
|
3738
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
3739
|
+
`${tableAndLocalKey} = ${tableAndForeignKey}`,
|
|
3740
|
+
].join(" "),
|
|
3666
3741
|
]);
|
|
3667
3742
|
return this;
|
|
3668
3743
|
}
|
|
3669
3744
|
_queryStatement(sql) {
|
|
3670
3745
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3671
|
-
if (this.$state.get(
|
|
3746
|
+
if (this.$state.get("DEBUG"))
|
|
3672
3747
|
this.$utils.consoleDebug(sql);
|
|
3673
3748
|
const result = yield this.$pool.query(sql);
|
|
3674
3749
|
return result;
|
|
3675
3750
|
});
|
|
3676
3751
|
}
|
|
3677
3752
|
_actionStatement(_a) {
|
|
3678
|
-
return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false }) {
|
|
3679
|
-
if (this.$state.get(
|
|
3753
|
+
return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false, }) {
|
|
3754
|
+
if (this.$state.get("DEBUG"))
|
|
3680
3755
|
this.$utils.consoleDebug(sql);
|
|
3681
3756
|
if (returnId) {
|
|
3682
3757
|
const result = yield this.$pool.query(sql);
|
|
@@ -3688,111 +3763,113 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3688
3763
|
}
|
|
3689
3764
|
_insertNotExists() {
|
|
3690
3765
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3691
|
-
if (!this.$state.get(
|
|
3766
|
+
if (!this.$state.get("WHERE").length)
|
|
3692
3767
|
throw new Error("Can't insert not exists without where condition");
|
|
3693
3768
|
let sql = [
|
|
3694
|
-
`${this.$constants(
|
|
3695
|
-
`${this.$constants(
|
|
3769
|
+
`${this.$constants("SELECT")}`,
|
|
3770
|
+
`${this.$constants("EXISTS")}(${this.$constants("SELECT")}`,
|
|
3696
3771
|
`*`,
|
|
3697
|
-
`${this.$constants(
|
|
3698
|
-
`${this.$state.get(
|
|
3772
|
+
`${this.$constants("FROM")}`,
|
|
3773
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3699
3774
|
`${this._queryBuilder().where()}`,
|
|
3700
|
-
`${this.$constants(
|
|
3701
|
-
`${this.$constants(
|
|
3702
|
-
].join(
|
|
3775
|
+
`${this.$constants("LIMIT")} 1)`,
|
|
3776
|
+
`${this.$constants("AS")} 'exists'`,
|
|
3777
|
+
].join(" ");
|
|
3703
3778
|
const [{ exists: result }] = yield this._queryStatement(sql);
|
|
3704
3779
|
const check = !!Number.parseInt(result);
|
|
3705
3780
|
switch (check) {
|
|
3706
3781
|
case false: {
|
|
3707
3782
|
const [result, id] = yield this._actionStatement({
|
|
3708
|
-
sql: this.$state.get(
|
|
3709
|
-
returnId: true
|
|
3783
|
+
sql: this.$state.get("INSERT"),
|
|
3784
|
+
returnId: true,
|
|
3710
3785
|
});
|
|
3711
|
-
if (this.$state.get(
|
|
3786
|
+
if (this.$state.get("VOID") || !result)
|
|
3712
3787
|
return this._resultHandler(undefined);
|
|
3713
|
-
yield this.$utils.wait(this.$state.get(
|
|
3788
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3714
3789
|
const data = yield new Builder()
|
|
3715
3790
|
.copyBuilder(this, { select: true })
|
|
3716
|
-
.where(
|
|
3791
|
+
.where("id", id)
|
|
3717
3792
|
.first();
|
|
3718
3793
|
return this._resultHandler(data);
|
|
3719
3794
|
}
|
|
3720
|
-
default:
|
|
3795
|
+
default:
|
|
3796
|
+
return this._resultHandler(null);
|
|
3721
3797
|
}
|
|
3722
3798
|
});
|
|
3723
3799
|
}
|
|
3724
3800
|
_insert() {
|
|
3725
3801
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3726
3802
|
const [result, id] = yield this._actionStatement({
|
|
3727
|
-
sql: this.$state.get(
|
|
3728
|
-
returnId: true
|
|
3803
|
+
sql: this.$state.get("INSERT"),
|
|
3804
|
+
returnId: true,
|
|
3729
3805
|
});
|
|
3730
|
-
if (this.$state.get(
|
|
3806
|
+
if (this.$state.get("VOID") || !result)
|
|
3731
3807
|
return this._resultHandler(undefined);
|
|
3732
|
-
yield this.$utils.wait(this.$state.get(
|
|
3808
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3733
3809
|
const results = yield new Builder()
|
|
3734
3810
|
.copyBuilder(this, { select: true })
|
|
3735
|
-
.where(
|
|
3811
|
+
.where("id", id)
|
|
3736
3812
|
.first();
|
|
3737
3813
|
return this._resultHandler(results);
|
|
3738
3814
|
});
|
|
3739
3815
|
}
|
|
3740
3816
|
_checkValueHasRaw(value) {
|
|
3741
|
-
const detectedValue = typeof value ===
|
|
3742
|
-
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants(
|
|
3817
|
+
const detectedValue = typeof value === "string" && value.includes(this.$constants("RAW"))
|
|
3818
|
+
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants("RAW"), "")
|
|
3743
3819
|
: `'${this.$utils.covertBooleanToNumber(value)}'`;
|
|
3744
3820
|
return detectedValue;
|
|
3745
3821
|
}
|
|
3746
3822
|
_checkValueHasOp(str) {
|
|
3747
3823
|
var _a;
|
|
3748
|
-
if (typeof str !==
|
|
3824
|
+
if (typeof str !== "string")
|
|
3749
3825
|
str = String(str);
|
|
3750
|
-
if (!str.includes(this.$constants(
|
|
3826
|
+
if (!str.includes(this.$constants("OP")) ||
|
|
3827
|
+
!str.includes(this.$constants("VALUE"))) {
|
|
3751
3828
|
return null;
|
|
3752
3829
|
}
|
|
3753
|
-
const opRegex = new RegExp(`\\${this.$constants(
|
|
3754
|
-
const valueRegex = new RegExp(`\\${this.$constants(
|
|
3830
|
+
const opRegex = new RegExp(`\\${this.$constants("OP")}\\(([^)]+)\\)`);
|
|
3831
|
+
const valueRegex = new RegExp(`\\${this.$constants("VALUE")}\\(([^)]+)\\)`);
|
|
3755
3832
|
const opMatch = str.match(opRegex);
|
|
3756
3833
|
const valueMatch = str.match(valueRegex);
|
|
3757
|
-
const op = opMatch ? opMatch[1] :
|
|
3758
|
-
const value = valueMatch ? valueMatch[1] :
|
|
3834
|
+
const op = opMatch ? opMatch[1] : "";
|
|
3835
|
+
const value = valueMatch ? valueMatch[1] : "";
|
|
3759
3836
|
return {
|
|
3760
|
-
op: op.replace(this.$constants(
|
|
3761
|
-
value: (_a = value === null || value === void 0 ? void 0 : value.replace(this.$constants(
|
|
3837
|
+
op: op.replace(this.$constants("OP"), ""),
|
|
3838
|
+
value: (_a = value === null || value === void 0 ? void 0 : value.replace(this.$constants("VALUE"), "")) !== null && _a !== void 0 ? _a : "",
|
|
3762
3839
|
};
|
|
3763
3840
|
}
|
|
3764
3841
|
_insertMultiple() {
|
|
3765
3842
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3766
3843
|
const [result, id] = yield this._actionStatement({
|
|
3767
3844
|
sql: this._queryBuilder().insert(),
|
|
3768
|
-
returnId: true
|
|
3845
|
+
returnId: true,
|
|
3769
3846
|
});
|
|
3770
|
-
if (this.$state.get(
|
|
3847
|
+
if (this.$state.get("VOID") || !result)
|
|
3771
3848
|
return this._resultHandler(undefined);
|
|
3772
3849
|
const arrayId = [...Array(result)].map((_, i) => i + id);
|
|
3773
|
-
yield this.$utils.wait(this.$state.get(
|
|
3850
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3774
3851
|
const resultData = yield new Builder()
|
|
3775
3852
|
.copyBuilder(this, { select: true, limit: true })
|
|
3776
|
-
.whereIn(
|
|
3853
|
+
.whereIn("id", arrayId)
|
|
3777
3854
|
.get();
|
|
3778
3855
|
return this._resultHandler(resultData);
|
|
3779
3856
|
});
|
|
3780
3857
|
}
|
|
3781
3858
|
_insertOrSelect() {
|
|
3782
3859
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3783
|
-
if (!this.$state.get(
|
|
3860
|
+
if (!this.$state.get("WHERE").length) {
|
|
3784
3861
|
throw new Error("Can't create or select without where condition");
|
|
3785
3862
|
}
|
|
3786
3863
|
let sql = [
|
|
3787
|
-
`${this.$constants(
|
|
3788
|
-
`${this.$constants(
|
|
3864
|
+
`${this.$constants("SELECT")}`,
|
|
3865
|
+
`${this.$constants("EXISTS")}(${this.$constants("SELECT")}`,
|
|
3789
3866
|
`1`,
|
|
3790
|
-
`${this.$constants(
|
|
3791
|
-
`${this.$state.get(
|
|
3867
|
+
`${this.$constants("FROM")}`,
|
|
3868
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3792
3869
|
`${this._queryBuilder().where()}`,
|
|
3793
|
-
`${this.$constants(
|
|
3794
|
-
`${this.$constants(
|
|
3795
|
-
].join(
|
|
3870
|
+
`${this.$constants("LIMIT")} 1)`,
|
|
3871
|
+
`${this.$constants("AS")} 'exists'`,
|
|
3872
|
+
].join(" ");
|
|
3796
3873
|
let check = false;
|
|
3797
3874
|
const [{ exists: result }] = yield this._queryStatement(sql);
|
|
3798
3875
|
check = !!parseInt(result);
|
|
@@ -3800,16 +3877,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3800
3877
|
case false: {
|
|
3801
3878
|
const [result, id] = yield this._actionStatement({
|
|
3802
3879
|
sql: this._queryBuilder().insert(),
|
|
3803
|
-
returnId: true
|
|
3880
|
+
returnId: true,
|
|
3804
3881
|
});
|
|
3805
|
-
if (this.$state.get(
|
|
3882
|
+
if (this.$state.get("VOID") || !result)
|
|
3806
3883
|
return this._resultHandler(undefined);
|
|
3807
|
-
yield this.$utils.wait(this.$state.get(
|
|
3884
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3808
3885
|
const data = yield new Builder()
|
|
3809
3886
|
.copyBuilder(this, { select: true })
|
|
3810
|
-
.where(
|
|
3887
|
+
.where("id", id)
|
|
3811
3888
|
.first();
|
|
3812
|
-
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action:
|
|
3889
|
+
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action: "insert" });
|
|
3813
3890
|
return this._resultHandler(resultData);
|
|
3814
3891
|
}
|
|
3815
3892
|
case true: {
|
|
@@ -3819,11 +3896,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3819
3896
|
const data = yield this._queryStatement(sql);
|
|
3820
3897
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
3821
3898
|
for (const val of data) {
|
|
3822
|
-
val.$action =
|
|
3899
|
+
val.$action = "select";
|
|
3823
3900
|
}
|
|
3824
3901
|
return this._resultHandler(data || []);
|
|
3825
3902
|
}
|
|
3826
|
-
const resultData = Object.assign(Object.assign({}, data[0]), { $action:
|
|
3903
|
+
const resultData = Object.assign(Object.assign({}, data[0]), { $action: "select" });
|
|
3827
3904
|
return this._resultHandler(resultData);
|
|
3828
3905
|
}
|
|
3829
3906
|
default: {
|
|
@@ -3834,19 +3911,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3834
3911
|
}
|
|
3835
3912
|
_updateOrInsert() {
|
|
3836
3913
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3837
|
-
if (!this.$state.get(
|
|
3914
|
+
if (!this.$state.get("WHERE").length) {
|
|
3838
3915
|
throw new Error("Can't update or insert without where condition");
|
|
3839
3916
|
}
|
|
3840
3917
|
let sql = [
|
|
3841
|
-
`${this.$constants(
|
|
3842
|
-
`${this.$constants(
|
|
3918
|
+
`${this.$constants("SELECT")}`,
|
|
3919
|
+
`${this.$constants("EXISTS")}(${this.$constants("SELECT")}`,
|
|
3843
3920
|
`1`,
|
|
3844
|
-
`${this.$constants(
|
|
3845
|
-
`${this.$state.get(
|
|
3921
|
+
`${this.$constants("FROM")}`,
|
|
3922
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3846
3923
|
`${this._queryBuilder().where()}`,
|
|
3847
|
-
`${this.$constants(
|
|
3848
|
-
`${this.$constants(
|
|
3849
|
-
].join(
|
|
3924
|
+
`${this.$constants("LIMIT")} 1)`,
|
|
3925
|
+
`${this.$constants("AS")} 'exists'`,
|
|
3926
|
+
].join(" ");
|
|
3850
3927
|
let check = false;
|
|
3851
3928
|
const [{ exists: result }] = yield this._queryStatement(sql);
|
|
3852
3929
|
check = !!parseInt(result);
|
|
@@ -3854,33 +3931,35 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3854
3931
|
case false: {
|
|
3855
3932
|
const [result, id] = yield this._actionStatement({
|
|
3856
3933
|
sql: this._queryBuilder().insert(),
|
|
3857
|
-
returnId: true
|
|
3934
|
+
returnId: true,
|
|
3858
3935
|
});
|
|
3859
|
-
if (this.$state.get(
|
|
3936
|
+
if (this.$state.get("VOID") || !result)
|
|
3860
3937
|
return this._resultHandler(undefined);
|
|
3861
|
-
yield this.$utils.wait(this.$state.get(
|
|
3938
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3862
3939
|
const data = yield new Builder()
|
|
3863
3940
|
.copyBuilder(this, { select: true })
|
|
3864
|
-
.where(
|
|
3941
|
+
.where("id", id)
|
|
3865
3942
|
.first();
|
|
3866
|
-
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action:
|
|
3943
|
+
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action: "insert" });
|
|
3867
3944
|
return this._resultHandler(resultData);
|
|
3868
3945
|
}
|
|
3869
3946
|
case true: {
|
|
3870
3947
|
const result = yield this._actionStatement({
|
|
3871
|
-
sql: this._queryBuilder().update()
|
|
3948
|
+
sql: this._queryBuilder().update(),
|
|
3872
3949
|
});
|
|
3873
|
-
if (this.$state.get(
|
|
3950
|
+
if (this.$state.get("VOID") || !result)
|
|
3874
3951
|
return this._resultHandler(null);
|
|
3875
|
-
yield this.$utils.wait(this.$state.get(
|
|
3876
|
-
const data = yield this._queryStatement(new Builder()
|
|
3952
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3953
|
+
const data = yield this._queryStatement(new Builder()
|
|
3954
|
+
.copyBuilder(this, { select: true, where: true })
|
|
3955
|
+
.toString());
|
|
3877
3956
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
3878
3957
|
for (const val of data) {
|
|
3879
|
-
val.$action =
|
|
3958
|
+
val.$action = "update";
|
|
3880
3959
|
}
|
|
3881
3960
|
return this._resultHandler(data || []);
|
|
3882
3961
|
}
|
|
3883
|
-
const resultData = Object.assign(Object.assign({}, data[0]), { $action:
|
|
3962
|
+
const resultData = Object.assign(Object.assign({}, data[0]), { $action: "update" });
|
|
3884
3963
|
return this._resultHandler(resultData);
|
|
3885
3964
|
}
|
|
3886
3965
|
default: {
|
|
@@ -3891,14 +3970,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3891
3970
|
}
|
|
3892
3971
|
_update() {
|
|
3893
3972
|
return __awaiter(this, arguments, void 0, function* (ignoreWhere = false) {
|
|
3894
|
-
if (!this.$state.get(
|
|
3973
|
+
if (!this.$state.get("WHERE").length && !ignoreWhere)
|
|
3895
3974
|
throw new Error("can't update without where condition");
|
|
3896
3975
|
const result = yield this._actionStatement({
|
|
3897
|
-
sql: this._queryBuilder().update()
|
|
3976
|
+
sql: this._queryBuilder().update(),
|
|
3898
3977
|
});
|
|
3899
|
-
if (this.$state.get(
|
|
3978
|
+
if (this.$state.get("VOID") || !result)
|
|
3900
3979
|
return this._resultHandler(undefined);
|
|
3901
|
-
yield this.$utils.wait(this.$state.get(
|
|
3980
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3902
3981
|
const sql = this._queryBuilder().select();
|
|
3903
3982
|
const data = yield this._queryStatement(sql);
|
|
3904
3983
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1)
|
|
@@ -3909,7 +3988,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3909
3988
|
}
|
|
3910
3989
|
_hiddenColumn(data) {
|
|
3911
3990
|
var _a;
|
|
3912
|
-
const hidden = this.$state.get(
|
|
3991
|
+
const hidden = this.$state.get("HIDDEN");
|
|
3913
3992
|
if ((_a = Object.keys(data)) === null || _a === void 0 ? void 0 : _a.length) {
|
|
3914
3993
|
hidden.forEach((column) => {
|
|
3915
3994
|
data.forEach((objColumn) => {
|
|
@@ -3922,31 +4001,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3922
4001
|
_queryUpdate(data) {
|
|
3923
4002
|
this.$utils.covertDateToDateString(data);
|
|
3924
4003
|
const values = Object.entries(data).map(([column, value]) => {
|
|
3925
|
-
if (typeof value ===
|
|
4004
|
+
if (typeof value === "string" &&
|
|
4005
|
+
!value.includes(this.$constants("RAW"))) {
|
|
3926
4006
|
value = this.$utils.escapeActions(value);
|
|
3927
4007
|
}
|
|
3928
|
-
return `${this.bindColumn(column)} = ${value == null || value === this.$constants(
|
|
3929
|
-
? this.$constants(
|
|
4008
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants("NULL")
|
|
4009
|
+
? this.$constants("NULL")
|
|
3930
4010
|
: this._checkValueHasRaw(value)}`;
|
|
3931
4011
|
});
|
|
3932
|
-
return `${this.$constants(
|
|
4012
|
+
return `${this.$constants("SET")} ${values}`;
|
|
3933
4013
|
}
|
|
3934
4014
|
_queryInsert(data) {
|
|
3935
4015
|
data = this.$utils.covertDateToDateString(data);
|
|
3936
4016
|
const columns = Object.keys(data).map((column) => this.bindColumn(column));
|
|
3937
4017
|
const values = Object.values(data).map((value) => {
|
|
3938
|
-
if (typeof value ===
|
|
4018
|
+
if (typeof value === "string" &&
|
|
4019
|
+
!value.includes(this.$constants("RAW"))) {
|
|
3939
4020
|
value = this.$utils.escapeActions(value);
|
|
3940
4021
|
}
|
|
3941
|
-
return `${value == null || value === this.$constants(
|
|
3942
|
-
? this.$constants(
|
|
4022
|
+
return `${value == null || value === this.$constants("NULL")
|
|
4023
|
+
? this.$constants("NULL")
|
|
3943
4024
|
: this._checkValueHasRaw(value)}`;
|
|
3944
4025
|
});
|
|
3945
|
-
return [
|
|
3946
|
-
`(${columns})`,
|
|
3947
|
-
`${this.$constants('VALUES')}`,
|
|
3948
|
-
`(${values})`
|
|
3949
|
-
].join(' ');
|
|
4026
|
+
return [`(${columns})`, `${this.$constants("VALUES")}`, `(${values})`].join(" ");
|
|
3950
4027
|
}
|
|
3951
4028
|
_queryInsertMultiple(data) {
|
|
3952
4029
|
var _a;
|
|
@@ -3954,79 +4031,80 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3954
4031
|
for (let objects of data) {
|
|
3955
4032
|
this.$utils.covertDateToDateString(objects);
|
|
3956
4033
|
const vals = Object.values(objects).map((value) => {
|
|
3957
|
-
if (typeof value ===
|
|
4034
|
+
if (typeof value === "string" &&
|
|
4035
|
+
!value.includes(this.$constants("RAW"))) {
|
|
3958
4036
|
value = this.$utils.escapeActions(value);
|
|
3959
4037
|
}
|
|
3960
|
-
return `${value == null || value === this.$constants(
|
|
3961
|
-
? this.$constants(
|
|
4038
|
+
return `${value == null || value === this.$constants("NULL")
|
|
4039
|
+
? this.$constants("NULL")
|
|
3962
4040
|
: this._checkValueHasRaw(value)}`;
|
|
3963
4041
|
});
|
|
3964
|
-
values.push(`(${vals.join(
|
|
4042
|
+
values.push(`(${vals.join(",")})`);
|
|
3965
4043
|
}
|
|
3966
4044
|
const columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => this.bindColumn(column));
|
|
3967
4045
|
return [
|
|
3968
4046
|
`(${columns})`,
|
|
3969
|
-
`${this.$constants(
|
|
3970
|
-
`${values.join(
|
|
3971
|
-
].join(
|
|
4047
|
+
`${this.$constants("VALUES")}`,
|
|
4048
|
+
`${values.join(",")}`,
|
|
4049
|
+
].join(" ");
|
|
3972
4050
|
}
|
|
3973
4051
|
_valueAndOperator(value, operator, useDefault = false) {
|
|
3974
4052
|
if (useDefault)
|
|
3975
|
-
return [operator,
|
|
4053
|
+
return [operator, "="];
|
|
3976
4054
|
if (operator == null) {
|
|
3977
|
-
return [[],
|
|
4055
|
+
return [[], "="];
|
|
3978
4056
|
}
|
|
3979
|
-
if (operator.toUpperCase() === this.$constants(
|
|
4057
|
+
if (operator.toUpperCase() === this.$constants("LIKE")) {
|
|
3980
4058
|
operator = operator.toUpperCase();
|
|
3981
4059
|
}
|
|
3982
4060
|
return [value, operator];
|
|
3983
4061
|
}
|
|
3984
|
-
_handleJoin(type =
|
|
4062
|
+
_handleJoin(type = "INNER_JOIN", localKey, referenceKey) {
|
|
3985
4063
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
3986
|
-
if (typeof localKey ===
|
|
4064
|
+
if (typeof localKey === "function") {
|
|
3987
4065
|
const callback = localKey(new Join_1.Join(this, type));
|
|
3988
|
-
this.$state.set(
|
|
3989
|
-
...this.$state.get(
|
|
3990
|
-
callback[
|
|
4066
|
+
this.$state.set("JOIN", [
|
|
4067
|
+
...this.$state.get("JOIN"),
|
|
4068
|
+
callback["toString"](),
|
|
3991
4069
|
]);
|
|
3992
4070
|
return this;
|
|
3993
4071
|
}
|
|
3994
|
-
let table = (_a = referenceKey === null || referenceKey === void 0 ? void 0 : referenceKey.split(
|
|
4072
|
+
let table = (_a = referenceKey === null || referenceKey === void 0 ? void 0 : referenceKey.split(".")) === null || _a === void 0 ? void 0 : _a.shift();
|
|
3995
4073
|
const aliasRef = /\|/.test(String(table));
|
|
3996
4074
|
if (aliasRef) {
|
|
3997
|
-
const tableRef = (_b = table === null || table === void 0 ? void 0 : table.split(
|
|
3998
|
-
table = `\`${tableRef}\` ${this.$constants(
|
|
3999
|
-
referenceKey = String((_e = (_d = referenceKey === null || referenceKey === void 0 ? void 0 : referenceKey.split(
|
|
4075
|
+
const tableRef = (_b = table === null || table === void 0 ? void 0 : table.split("|")) === null || _b === void 0 ? void 0 : _b.shift();
|
|
4076
|
+
table = `\`${tableRef}\` ${this.$constants("AS")} \`${(_c = table === null || table === void 0 ? void 0 : table.split("|")) === null || _c === void 0 ? void 0 : _c.pop()}\``;
|
|
4077
|
+
referenceKey = String((_e = (_d = referenceKey === null || referenceKey === void 0 ? void 0 : referenceKey.split("|")) === null || _d === void 0 ? void 0 : _d.pop()) !== null && _e !== void 0 ? _e : referenceKey);
|
|
4000
4078
|
}
|
|
4001
4079
|
const alias = /\|/.test(String(localKey));
|
|
4002
4080
|
if (alias) {
|
|
4003
|
-
localKey = String((_g = (_f = localKey === null || localKey === void 0 ? void 0 : localKey.split(
|
|
4081
|
+
localKey = String((_g = (_f = localKey === null || localKey === void 0 ? void 0 : localKey.split("|")) === null || _f === void 0 ? void 0 : _f.pop()) !== null && _g !== void 0 ? _g : localKey);
|
|
4004
4082
|
}
|
|
4005
|
-
this.$state.set(
|
|
4006
|
-
...this.$state.get(
|
|
4083
|
+
this.$state.set("JOIN", [
|
|
4084
|
+
...this.$state.get("JOIN"),
|
|
4007
4085
|
[
|
|
4008
4086
|
`${this.$constants(type)}`,
|
|
4009
4087
|
aliasRef ? `${table}` : `\`${table}\``,
|
|
4010
|
-
`${this.$constants(
|
|
4011
|
-
`${this.bindColumn(localKey)} = ${this.bindColumn(String(referenceKey))}
|
|
4012
|
-
].join(
|
|
4088
|
+
`${this.$constants("ON")}`,
|
|
4089
|
+
`${this.bindColumn(localKey)} = ${this.bindColumn(String(referenceKey))}`,
|
|
4090
|
+
].join(" "),
|
|
4013
4091
|
]);
|
|
4014
4092
|
return this;
|
|
4015
4093
|
}
|
|
4016
4094
|
_initialConnection() {
|
|
4017
4095
|
this.$utils = utils_1.utils;
|
|
4018
4096
|
this.$pool = (() => {
|
|
4019
|
-
let pool =
|
|
4097
|
+
let pool = Pool_1.Pool;
|
|
4020
4098
|
return {
|
|
4021
4099
|
query: (sql) => __awaiter(this, void 0, void 0, function* () { return yield pool.query(sql); }),
|
|
4022
4100
|
get: () => pool,
|
|
4023
4101
|
set: (newConnection) => {
|
|
4024
4102
|
pool = newConnection;
|
|
4025
4103
|
return;
|
|
4026
|
-
}
|
|
4104
|
+
},
|
|
4027
4105
|
};
|
|
4028
4106
|
})();
|
|
4029
|
-
this.$state = new State_1.StateHandler(
|
|
4107
|
+
this.$state = new State_1.StateHandler("default");
|
|
4030
4108
|
this.$logger = (() => {
|
|
4031
4109
|
let logger = [];
|
|
4032
4110
|
return {
|
|
@@ -4039,7 +4117,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
4039
4117
|
logger = [];
|
|
4040
4118
|
return;
|
|
4041
4119
|
},
|
|
4042
|
-
check: (data) => logger.indexOf(data) != -1
|
|
4120
|
+
check: (data) => logger.indexOf(data) != -1,
|
|
4043
4121
|
};
|
|
4044
4122
|
})();
|
|
4045
4123
|
this.$constants = (name) => {
|