tspace-mysql 1.8.2 → 1.8.4
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 +443 -68
- package/build/cli/generate/model.js +3 -3
- package/build/cli/generate/model.js.map +1 -1
- package/build/lib/constants/index.d.ts +7 -1
- package/build/lib/constants/index.js +7 -1
- package/build/lib/constants/index.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractDB.d.ts +2 -2
- package/build/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractView.d.ts +10 -0
- package/build/lib/core/Abstracts/AbstractView.js +9 -0
- package/build/lib/core/Abstracts/AbstractView.js.map +1 -0
- package/build/lib/core/Blueprint.d.ts +24 -4
- package/build/lib/core/Blueprint.js +78 -87
- package/build/lib/core/Blueprint.js.map +1 -1
- package/build/lib/core/Builder.d.ts +39 -30
- package/build/lib/core/Builder.js +1094 -1044
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/Cache/DBCache.js +15 -16
- package/build/lib/core/Cache/DBCache.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/Decorator.d.ts +1 -1
- package/build/lib/core/Decorator.js.map +1 -1
- package/build/lib/core/Handlers/Relation.d.ts +2 -1
- package/build/lib/core/Handlers/Relation.js +60 -15
- package/build/lib/core/Handlers/Relation.js.map +1 -1
- package/build/lib/core/Handlers/State.d.ts +1 -0
- package/build/lib/core/Handlers/State.js +1 -1
- package/build/lib/core/Handlers/State.js.map +1 -1
- package/build/lib/core/JoinModel.d.ts +1 -1
- package/build/lib/core/JoinModel.js.map +1 -1
- package/build/lib/core/Meta.d.ts +55 -0
- package/build/lib/core/Meta.js +175 -0
- package/build/lib/core/Meta.js.map +1 -0
- package/build/lib/core/Model.d.ts +158 -94
- package/build/lib/core/Model.js +1586 -1203
- 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/Schema.d.ts +2 -2
- package/build/lib/core/Schema.js +113 -78
- package/build/lib/core/Schema.js.map +1 -1
- package/build/lib/core/SqlLike.js.map +1 -1
- package/build/lib/core/StoredProcedure.d.ts +120 -0
- package/build/lib/core/StoredProcedure.js +161 -0
- package/build/lib/core/StoredProcedure.js.map +1 -0
- package/build/lib/core/UtilityTypes.d.ts +25 -44
- package/build/lib/core/View.d.ts +96 -0
- package/build/lib/core/View.js +141 -0
- package/build/lib/core/View.js.map +1 -0
- package/build/lib/core/index.d.ts +17 -12
- package/build/lib/core/index.js +9 -4
- package/build/lib/core/index.js.map +1 -1
- package/build/lib/types/index.d.ts +23 -3
- package/build/lib/utils/index.js +5 -2
- package/build/lib/utils/index.js.map +1 -1
- package/build/tests/01-Pool.test.js +0 -10
- package/build/tests/01-Pool.test.js.map +1 -1
- package/build/tests/04-Model-pattern.test.js +1 -1
- package/build/tests/04-Model-pattern.test.js.map +1 -1
- package/build/tests/05-View.test.d.ts +1 -0
- package/build/tests/05-View.test.js +83 -0
- package/build/tests/05-View.test.js.map +1 -0
- package/build/tests/06-Meta.test.d.ts +1 -0
- package/build/tests/06-Meta.test.js +103 -0
- package/build/tests/06-Meta.test.js.map +1 -0
- package/build/tests/07-Virtual-column.test.d.ts +1 -0
- package/build/tests/07-Virtual-column.test.js +160 -0
- package/build/tests/07-Virtual-column.test.js.map +1 -0
- package/build/tests/schema-spec.js +25 -25
- package/build/tests/schema-spec.js.map +1 -1
- package/package.json +1 -1
- package/build/lib/connection/index.js.map +0 -1
|
@@ -23,11 +23,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
23
23
|
exports.Builder = void 0;
|
|
24
24
|
const AbstractBuilder_1 = require("./Abstracts/AbstractBuilder");
|
|
25
25
|
const utils_1 = require("../utils");
|
|
26
|
-
const constants_1 = require("../constants");
|
|
27
26
|
const DB_1 = require("./DB");
|
|
28
27
|
const State_1 = require("./Handlers/State");
|
|
29
28
|
const Join_1 = require("./Join");
|
|
30
|
-
const
|
|
29
|
+
const constants_1 = require("../constants");
|
|
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.replace(/`/g, '')}\``);
|
|
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.replace(/`/g, '')}\``);
|
|
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
|
}
|
|
@@ -869,15 +878,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
869
878
|
* @param {string} subQuery
|
|
870
879
|
* @returns {this}
|
|
871
880
|
*/
|
|
872
|
-
whereSubQuery(column, subQuery) {
|
|
873
|
-
this.$state.set(
|
|
874
|
-
...this.$state.get(
|
|
881
|
+
whereSubQuery(column, subQuery, options = { operator: constants_1.CONSTANTS['IN'] }) {
|
|
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
|
-
|
|
879
|
-
`(${subQuery})
|
|
880
|
-
].join(
|
|
887
|
+
options.operator,
|
|
888
|
+
`(${subQuery})`,
|
|
889
|
+
].join(" "),
|
|
881
890
|
]);
|
|
882
891
|
return this;
|
|
883
892
|
}
|
|
@@ -891,15 +900,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
891
900
|
* @param {string} subQuery
|
|
892
901
|
* @returns {this}
|
|
893
902
|
*/
|
|
894
|
-
whereNotSubQuery(column, subQuery) {
|
|
895
|
-
this.$state.set(
|
|
896
|
-
...this.$state.get(
|
|
903
|
+
whereNotSubQuery(column, subQuery, options = { operator: constants_1.CONSTANTS['NOT_IN'] }) {
|
|
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
|
-
|
|
901
|
-
`(${subQuery})
|
|
902
|
-
].join(
|
|
909
|
+
options.operator, ,
|
|
910
|
+
`(${subQuery})`,
|
|
911
|
+
].join(" "),
|
|
903
912
|
]);
|
|
904
913
|
return this;
|
|
905
914
|
}
|
|
@@ -913,15 +922,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
913
922
|
* @param {string} subQuery
|
|
914
923
|
* @returns {this}
|
|
915
924
|
*/
|
|
916
|
-
orWhereSubQuery(column, subQuery) {
|
|
917
|
-
this.$state.set(
|
|
918
|
-
...this.$state.get(
|
|
925
|
+
orWhereSubQuery(column, subQuery, options = { operator: constants_1.CONSTANTS['IN'] }) {
|
|
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
|
-
|
|
923
|
-
`(${subQuery})
|
|
924
|
-
].join(
|
|
931
|
+
options.operator,
|
|
932
|
+
`(${subQuery})`,
|
|
933
|
+
].join(" "),
|
|
925
934
|
]);
|
|
926
935
|
return this;
|
|
927
936
|
}
|
|
@@ -935,15 +944,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
935
944
|
* @param {string} subQuery
|
|
936
945
|
* @returns {this}
|
|
937
946
|
*/
|
|
938
|
-
orWhereNotSubQuery(column, subQuery) {
|
|
939
|
-
this.$state.set(
|
|
940
|
-
...this.$state.get(
|
|
947
|
+
orWhereNotSubQuery(column, subQuery, options = { operator: constants_1.CONSTANTS['NOT_IN'] }) {
|
|
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
|
-
|
|
945
|
-
`(${subQuery})
|
|
946
|
-
].join(
|
|
953
|
+
options.operator,
|
|
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,36 +1509,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1497
1509
|
* @returns {this}
|
|
1498
1510
|
*/
|
|
1499
1511
|
join(localKey, referenceKey) {
|
|
1500
|
-
this._handleJoin(
|
|
1501
|
-
return this;
|
|
1502
|
-
// const callback = localKey(new Join(this,'INNER_JOIN'))
|
|
1503
|
-
// this.$state.set('JOIN', [
|
|
1504
|
-
// ...this.$state.get('JOIN'),
|
|
1505
|
-
// callback['toString']()
|
|
1506
|
-
// ]
|
|
1507
|
-
// )
|
|
1508
|
-
// return this
|
|
1509
|
-
// }
|
|
1510
|
-
// let table = referenceKey?.split('.')?.shift()
|
|
1511
|
-
// const alias = table?.split('|')?.pop()
|
|
1512
|
-
// if(alias != null) {
|
|
1513
|
-
// table = table?.split('|')?.shift()
|
|
1514
|
-
// referenceKey = String(referenceKey?.split('|')?.pop() ?? referenceKey) as `${string}.${string}`
|
|
1515
|
-
// }
|
|
1516
|
-
// this.$state.set('JOIN', [
|
|
1517
|
-
// ...this.$state.get('JOIN'),
|
|
1518
|
-
// [
|
|
1519
|
-
// `${this.$constants('INNER_JOIN')}`,
|
|
1520
|
-
// alias == null
|
|
1521
|
-
// ? `\`${table}\``
|
|
1522
|
-
// : `\`${table}\` ${this.$constants('AS')} \`${alias}\``
|
|
1523
|
-
// ,
|
|
1524
|
-
// `${this.$constants('ON')}`,
|
|
1525
|
-
// `${this.bindColumn(localKey)} = ${this.bindColumn(String(referenceKey))}`
|
|
1526
|
-
// ].join(' ')
|
|
1527
|
-
// ]
|
|
1528
|
-
// )
|
|
1529
|
-
// return this
|
|
1512
|
+
this._handleJoin("INNER_JOIN", localKey, referenceKey);
|
|
1513
|
+
return this;
|
|
1530
1514
|
}
|
|
1531
1515
|
/**
|
|
1532
1516
|
* The 'rightJoin' method is used to perform a right join operation between two database tables.
|
|
@@ -1539,7 +1523,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1539
1523
|
* @returns {this}
|
|
1540
1524
|
*/
|
|
1541
1525
|
rightJoin(localKey, referenceKey) {
|
|
1542
|
-
this._handleJoin(
|
|
1526
|
+
this._handleJoin("RIGHT_JOIN", localKey, referenceKey);
|
|
1543
1527
|
return this;
|
|
1544
1528
|
}
|
|
1545
1529
|
/**
|
|
@@ -1553,7 +1537,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1553
1537
|
* @returns {this}
|
|
1554
1538
|
*/
|
|
1555
1539
|
leftJoin(localKey, referenceKey) {
|
|
1556
|
-
this._handleJoin(
|
|
1540
|
+
this._handleJoin("LEFT_JOIN", localKey, referenceKey);
|
|
1557
1541
|
return this;
|
|
1558
1542
|
}
|
|
1559
1543
|
/**
|
|
@@ -1565,7 +1549,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1565
1549
|
* @returns {this}
|
|
1566
1550
|
*/
|
|
1567
1551
|
crossJoin(localKey, referenceKey) {
|
|
1568
|
-
this._handleJoin(
|
|
1552
|
+
this._handleJoin("CROSS_JOIN", localKey, referenceKey);
|
|
1569
1553
|
return this;
|
|
1570
1554
|
}
|
|
1571
1555
|
/**
|
|
@@ -1582,15 +1566,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1582
1566
|
* .get()
|
|
1583
1567
|
* @returns {this}
|
|
1584
1568
|
*/
|
|
1585
|
-
joinSubQuery({ localKey, foreignKey, sql }) {
|
|
1586
|
-
this.$state.set(
|
|
1587
|
-
...this.$state.get(
|
|
1569
|
+
joinSubQuery({ localKey, foreignKey, sql, }) {
|
|
1570
|
+
this.$state.set("JOIN", [
|
|
1571
|
+
...this.$state.get("JOIN"),
|
|
1588
1572
|
[
|
|
1589
|
-
`${this.$constants(
|
|
1573
|
+
`${this.$constants("INNER_JOIN")}`,
|
|
1590
1574
|
`(${sql}) as subquery`,
|
|
1591
|
-
`${this.$constants(
|
|
1592
|
-
`${this.bindColumn(localKey)} = subquery.\`${foreignKey}
|
|
1593
|
-
].join(
|
|
1575
|
+
`${this.$constants("ON")}`,
|
|
1576
|
+
`${this.bindColumn(localKey)} = subquery.\`${foreignKey}\``,
|
|
1577
|
+
].join(" "),
|
|
1594
1578
|
]);
|
|
1595
1579
|
return this;
|
|
1596
1580
|
}
|
|
@@ -1602,21 +1586,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1602
1586
|
* @param {string?} order by default order = 'asc' but you can used 'asc' or 'desc'
|
|
1603
1587
|
* @returns {this}
|
|
1604
1588
|
*/
|
|
1605
|
-
orderBy(column, order =
|
|
1606
|
-
const orderBy = [column]
|
|
1589
|
+
orderBy(column, order = "ASC") {
|
|
1590
|
+
const orderBy = [column]
|
|
1591
|
+
.map((c) => {
|
|
1607
1592
|
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(
|
|
1593
|
+
return this.bindColumn(c.replace(/'/g, ""));
|
|
1594
|
+
if (c.includes(this.$constants("RAW")))
|
|
1595
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1611
1596
|
return this.bindColumn(c);
|
|
1612
|
-
})
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1597
|
+
})
|
|
1598
|
+
.join(", ");
|
|
1599
|
+
this.$state.set("ORDER_BY", [
|
|
1600
|
+
...this.$state.get("ORDER_BY"),
|
|
1601
|
+
`${orderBy} ${order.toUpperCase()}`,
|
|
1616
1602
|
]);
|
|
1617
|
-
this.$state.set(
|
|
1618
|
-
...this.$state.get(
|
|
1619
|
-
`\`${column}\` ${order.toUpperCase()}
|
|
1603
|
+
this.$state.set("ORDER_BY", [
|
|
1604
|
+
...this.$state.get("ORDER_BY"),
|
|
1605
|
+
`\`${column}\` ${order.toUpperCase()}`,
|
|
1620
1606
|
]);
|
|
1621
1607
|
return this;
|
|
1622
1608
|
}
|
|
@@ -1630,13 +1616,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1630
1616
|
* @param {string?} order [order=asc] asc, desc
|
|
1631
1617
|
* @returns {this}
|
|
1632
1618
|
*/
|
|
1633
|
-
orderByRaw(column, order =
|
|
1634
|
-
if (column.includes(this.$constants(
|
|
1635
|
-
column = column === null || column === void 0 ? void 0 : column.replace(this.$constants(
|
|
1619
|
+
orderByRaw(column, order = "ASC") {
|
|
1620
|
+
if (column.includes(this.$constants("RAW"))) {
|
|
1621
|
+
column = column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1636
1622
|
}
|
|
1637
|
-
this.$state.set(
|
|
1638
|
-
...this.$state.get(
|
|
1639
|
-
`${column} ${order.toUpperCase()}
|
|
1623
|
+
this.$state.set("ORDER_BY", [
|
|
1624
|
+
...this.$state.get("ORDER_BY"),
|
|
1625
|
+
`${column} ${order.toUpperCase()}`,
|
|
1640
1626
|
]);
|
|
1641
1627
|
return this;
|
|
1642
1628
|
}
|
|
@@ -1646,9 +1632,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1646
1632
|
* @returns {this}
|
|
1647
1633
|
*/
|
|
1648
1634
|
random() {
|
|
1649
|
-
this.$state.set(
|
|
1650
|
-
...this.$state.get(
|
|
1651
|
-
`${this.$constants(
|
|
1635
|
+
this.$state.set("ORDER_BY", [
|
|
1636
|
+
...this.$state.get("ORDER_BY"),
|
|
1637
|
+
`${this.$constants("RAND")}`,
|
|
1652
1638
|
]);
|
|
1653
1639
|
return this;
|
|
1654
1640
|
}
|
|
@@ -1668,19 +1654,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1668
1654
|
* @returns {this}
|
|
1669
1655
|
*/
|
|
1670
1656
|
latest(...columns) {
|
|
1671
|
-
let orderBy =
|
|
1657
|
+
let orderBy = "`id`";
|
|
1672
1658
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1673
|
-
orderBy = columns
|
|
1659
|
+
orderBy = columns
|
|
1660
|
+
.map((c) => {
|
|
1674
1661
|
if (/\./.test(c))
|
|
1675
1662
|
return this.bindColumn(c);
|
|
1676
|
-
if (c.includes(this.$constants(
|
|
1677
|
-
return c === null || c === void 0 ? void 0 : c.replace(this.$constants(
|
|
1663
|
+
if (c.includes(this.$constants("RAW")))
|
|
1664
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1678
1665
|
return `\`${c}\``;
|
|
1679
|
-
})
|
|
1666
|
+
})
|
|
1667
|
+
.join(", ");
|
|
1680
1668
|
}
|
|
1681
|
-
this.$state.set(
|
|
1682
|
-
...this.$state.get(
|
|
1683
|
-
`${orderBy} ${this.$constants(
|
|
1669
|
+
this.$state.set("ORDER_BY", [
|
|
1670
|
+
...this.$state.get("ORDER_BY"),
|
|
1671
|
+
`${orderBy} ${this.$constants("DESC")}`,
|
|
1684
1672
|
]);
|
|
1685
1673
|
return this;
|
|
1686
1674
|
}
|
|
@@ -1694,17 +1682,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1694
1682
|
* @returns {this}
|
|
1695
1683
|
*/
|
|
1696
1684
|
latestRaw(...columns) {
|
|
1697
|
-
let orderBy =
|
|
1685
|
+
let orderBy = "`id`";
|
|
1698
1686
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1699
|
-
orderBy = columns
|
|
1700
|
-
|
|
1701
|
-
|
|
1687
|
+
orderBy = columns
|
|
1688
|
+
.map((column) => {
|
|
1689
|
+
if (column.includes(this.$constants("RAW")))
|
|
1690
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1702
1691
|
return column;
|
|
1703
|
-
})
|
|
1692
|
+
})
|
|
1693
|
+
.join(", ");
|
|
1704
1694
|
}
|
|
1705
|
-
this.$state.set(
|
|
1706
|
-
...this.$state.get(
|
|
1707
|
-
`${orderBy} ${this.$constants(
|
|
1695
|
+
this.$state.set("ORDER_BY", [
|
|
1696
|
+
...this.$state.get("ORDER_BY"),
|
|
1697
|
+
`${orderBy} ${this.$constants("DESC")}`,
|
|
1708
1698
|
]);
|
|
1709
1699
|
return this;
|
|
1710
1700
|
}
|
|
@@ -1716,19 +1706,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1716
1706
|
* @returns {this}
|
|
1717
1707
|
*/
|
|
1718
1708
|
oldest(...columns) {
|
|
1719
|
-
let orderBy =
|
|
1709
|
+
let orderBy = "`id`";
|
|
1720
1710
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1721
|
-
orderBy = columns
|
|
1711
|
+
orderBy = columns
|
|
1712
|
+
.map((c) => {
|
|
1722
1713
|
if (/\./.test(c))
|
|
1723
1714
|
return this.bindColumn(c);
|
|
1724
|
-
if (c.includes(this.$constants(
|
|
1725
|
-
return c === null || c === void 0 ? void 0 : c.replace(this.$constants(
|
|
1715
|
+
if (c.includes(this.$constants("RAW")))
|
|
1716
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1726
1717
|
return `\`${c}\``;
|
|
1727
|
-
})
|
|
1718
|
+
})
|
|
1719
|
+
.join(", ");
|
|
1728
1720
|
}
|
|
1729
|
-
this.$state.set(
|
|
1730
|
-
...this.$state.get(
|
|
1731
|
-
`${orderBy} ${this.$constants(
|
|
1721
|
+
this.$state.set("ORDER_BY", [
|
|
1722
|
+
...this.$state.get("ORDER_BY"),
|
|
1723
|
+
`${orderBy} ${this.$constants("ASC")}`,
|
|
1732
1724
|
]);
|
|
1733
1725
|
return this;
|
|
1734
1726
|
}
|
|
@@ -1742,17 +1734,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1742
1734
|
* @returns {this}
|
|
1743
1735
|
*/
|
|
1744
1736
|
oldestRaw(...columns) {
|
|
1745
|
-
let orderBy =
|
|
1737
|
+
let orderBy = "`id`";
|
|
1746
1738
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1747
|
-
orderBy = columns
|
|
1748
|
-
|
|
1749
|
-
|
|
1739
|
+
orderBy = columns
|
|
1740
|
+
.map((column) => {
|
|
1741
|
+
if (column.includes(this.$constants("RAW")))
|
|
1742
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1750
1743
|
return column;
|
|
1751
|
-
})
|
|
1744
|
+
})
|
|
1745
|
+
.join(", ");
|
|
1752
1746
|
}
|
|
1753
|
-
this.$state.set(
|
|
1754
|
-
...this.$state.get(
|
|
1755
|
-
`${orderBy} ${this.$constants(
|
|
1747
|
+
this.$state.set("ORDER_BY", [
|
|
1748
|
+
...this.$state.get("ORDER_BY"),
|
|
1749
|
+
`${orderBy} ${this.$constants("ASC")}`,
|
|
1756
1750
|
]);
|
|
1757
1751
|
return this;
|
|
1758
1752
|
}
|
|
@@ -1766,20 +1760,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1766
1760
|
* @returns {this}
|
|
1767
1761
|
*/
|
|
1768
1762
|
groupBy(...columns) {
|
|
1769
|
-
let groupBy =
|
|
1763
|
+
let groupBy = "id";
|
|
1770
1764
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1771
|
-
groupBy = columns
|
|
1765
|
+
groupBy = columns
|
|
1766
|
+
.map((c) => {
|
|
1772
1767
|
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(
|
|
1768
|
+
return this.bindColumn(c.replace(/'/g, ""));
|
|
1769
|
+
if (c.includes(this.$constants("RAW")))
|
|
1770
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants("RAW"), "");
|
|
1776
1771
|
return this.bindColumn(c);
|
|
1777
|
-
})
|
|
1772
|
+
})
|
|
1773
|
+
.join(", ");
|
|
1778
1774
|
}
|
|
1779
|
-
this.$state.set(
|
|
1780
|
-
...this.$state.get('GROUP_BY'),
|
|
1781
|
-
`${groupBy}`
|
|
1782
|
-
]);
|
|
1775
|
+
this.$state.set("GROUP_BY", [...this.$state.get("GROUP_BY"), `${groupBy}`]);
|
|
1783
1776
|
return this;
|
|
1784
1777
|
}
|
|
1785
1778
|
/**
|
|
@@ -1794,18 +1787,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1794
1787
|
* @returns {this}
|
|
1795
1788
|
*/
|
|
1796
1789
|
groupByRaw(...columns) {
|
|
1797
|
-
let groupBy =
|
|
1790
|
+
let groupBy = "id";
|
|
1798
1791
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1799
|
-
groupBy = columns
|
|
1800
|
-
|
|
1801
|
-
|
|
1792
|
+
groupBy = columns
|
|
1793
|
+
.map((column) => {
|
|
1794
|
+
if (column.includes(this.$constants("RAW")))
|
|
1795
|
+
return column === null || column === void 0 ? void 0 : column.replace(this.$constants("RAW"), "");
|
|
1802
1796
|
return column;
|
|
1803
|
-
})
|
|
1797
|
+
})
|
|
1798
|
+
.join(", ");
|
|
1804
1799
|
}
|
|
1805
|
-
this.$state.set(
|
|
1806
|
-
...this.$state.get('GROUP_BY'),
|
|
1807
|
-
`${groupBy}`
|
|
1808
|
-
]);
|
|
1800
|
+
this.$state.set("GROUP_BY", [...this.$state.get("GROUP_BY"), `${groupBy}`]);
|
|
1809
1801
|
return this;
|
|
1810
1802
|
}
|
|
1811
1803
|
/**
|
|
@@ -1818,10 +1810,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1818
1810
|
* @returns {this}
|
|
1819
1811
|
*/
|
|
1820
1812
|
having(condition) {
|
|
1821
|
-
if (condition.includes(this.$constants(
|
|
1822
|
-
condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants(
|
|
1813
|
+
if (condition.includes(this.$constants("RAW"))) {
|
|
1814
|
+
condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants("RAW"), "");
|
|
1823
1815
|
}
|
|
1824
|
-
this.$state.set(
|
|
1816
|
+
this.$state.set("HAVING", `${this.$constants("HAVING")} ${condition}`);
|
|
1825
1817
|
return this;
|
|
1826
1818
|
}
|
|
1827
1819
|
/**
|
|
@@ -1851,7 +1843,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1851
1843
|
number = Math.pow(2, 31) - 1; // int 32 bit
|
|
1852
1844
|
if (number < 0 || number === -0)
|
|
1853
1845
|
number = 0;
|
|
1854
|
-
this.$state.set(
|
|
1846
|
+
this.$state.set("LIMIT", number);
|
|
1855
1847
|
return this;
|
|
1856
1848
|
}
|
|
1857
1849
|
/**
|
|
@@ -1875,9 +1867,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1875
1867
|
number = this.$utils.softNumber(number);
|
|
1876
1868
|
if (number < 0 || number === -0)
|
|
1877
1869
|
number = 0;
|
|
1878
|
-
this.$state.set(
|
|
1879
|
-
if (!this.$state.get(
|
|
1880
|
-
this.$state.set(
|
|
1870
|
+
this.$state.set("OFFSET", `${this.$constants("OFFSET")} ${number}`);
|
|
1871
|
+
if (!this.$state.get("LIMIT"))
|
|
1872
|
+
this.$state.set("LIMIT", number);
|
|
1881
1873
|
return this;
|
|
1882
1874
|
}
|
|
1883
1875
|
/**
|
|
@@ -1897,7 +1889,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1897
1889
|
* @returns {this} this
|
|
1898
1890
|
*/
|
|
1899
1891
|
hidden(...columns) {
|
|
1900
|
-
this.$state.set(
|
|
1892
|
+
this.$state.set("HIDDEN", columns);
|
|
1901
1893
|
return this;
|
|
1902
1894
|
}
|
|
1903
1895
|
/**
|
|
@@ -1913,7 +1905,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1913
1905
|
update(data, updateNotExists = []) {
|
|
1914
1906
|
this.limit(1);
|
|
1915
1907
|
if (!Object.keys(data).length)
|
|
1916
|
-
throw new Error(
|
|
1908
|
+
throw new Error("This method must be required");
|
|
1917
1909
|
if (updateNotExists.length) {
|
|
1918
1910
|
for (const c of updateNotExists) {
|
|
1919
1911
|
for (const column in data) {
|
|
@@ -1926,12 +1918,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1926
1918
|
}
|
|
1927
1919
|
}
|
|
1928
1920
|
const query = this._queryUpdate(data);
|
|
1929
|
-
this.$state.set(
|
|
1930
|
-
`${this.$constants(
|
|
1931
|
-
`${this.$state.get(
|
|
1932
|
-
`${query}
|
|
1933
|
-
].join(
|
|
1934
|
-
this.$state.set(
|
|
1921
|
+
this.$state.set("UPDATE", [
|
|
1922
|
+
`${this.$constants("UPDATE")}`,
|
|
1923
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
1924
|
+
`${query}`,
|
|
1925
|
+
].join(" "));
|
|
1926
|
+
this.$state.set("SAVE", "UPDATE");
|
|
1935
1927
|
return this;
|
|
1936
1928
|
}
|
|
1937
1929
|
/**
|
|
@@ -1946,7 +1938,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1946
1938
|
*/
|
|
1947
1939
|
updateMany(data, updateNotExists = []) {
|
|
1948
1940
|
if (!Object.keys(data).length)
|
|
1949
|
-
throw new Error(
|
|
1941
|
+
throw new Error("This method must be required");
|
|
1950
1942
|
if (updateNotExists.length) {
|
|
1951
1943
|
for (const c of updateNotExists) {
|
|
1952
1944
|
for (const column in data) {
|
|
@@ -1959,12 +1951,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1959
1951
|
}
|
|
1960
1952
|
}
|
|
1961
1953
|
const query = this._queryUpdate(data);
|
|
1962
|
-
this.$state.set(
|
|
1963
|
-
`${this.$constants(
|
|
1964
|
-
`${this.$state.get(
|
|
1965
|
-
`${query}
|
|
1966
|
-
].join(
|
|
1967
|
-
this.$state.set(
|
|
1954
|
+
this.$state.set("UPDATE", [
|
|
1955
|
+
`${this.$constants("UPDATE")}`,
|
|
1956
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
1957
|
+
`${query}`,
|
|
1958
|
+
].join(" "));
|
|
1959
|
+
this.$state.set("SAVE", "UPDATE");
|
|
1968
1960
|
return this;
|
|
1969
1961
|
}
|
|
1970
1962
|
/**
|
|
@@ -1984,15 +1976,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1984
1976
|
throw new Error(`The method 'updateMultiple' must not be empty.`);
|
|
1985
1977
|
this.limit(cases.length);
|
|
1986
1978
|
const updateColumns = cases.reduce((columns, item) => {
|
|
1987
|
-
return (item.columns &&
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1979
|
+
return (item.columns &&
|
|
1980
|
+
Object.keys(item.columns).forEach((key) => (columns[key] = [
|
|
1981
|
+
this.$constants("RAW"),
|
|
1982
|
+
this.$constants("CASE"),
|
|
1983
|
+
`${this.$constants("ELSE")} ${this.bindColumn(key)}`,
|
|
1984
|
+
this.$constants("END"),
|
|
1985
|
+
])),
|
|
1986
|
+
columns);
|
|
1993
1987
|
}, {});
|
|
1994
1988
|
const columns = cases.reduce((columns, item) => {
|
|
1995
|
-
return (item.columns &&
|
|
1989
|
+
return (item.columns &&
|
|
1990
|
+
Object.keys(item.columns).forEach((key) => (columns[key] = "")),
|
|
1991
|
+
columns);
|
|
1996
1992
|
}, {});
|
|
1997
1993
|
for (let i = cases.length - 1; i >= 0; i--) {
|
|
1998
1994
|
const c = cases[i];
|
|
@@ -2008,33 +2004,34 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2008
2004
|
for (const [key, value] of Object.entries(c.columns)) {
|
|
2009
2005
|
if (updateColumns[key] == null)
|
|
2010
2006
|
continue;
|
|
2011
|
-
const startIndex = updateColumns[key].indexOf(this.$constants(
|
|
2012
|
-
const str = `${this.$constants(
|
|
2007
|
+
const startIndex = updateColumns[key].indexOf(this.$constants("CASE"));
|
|
2008
|
+
const str = `${this.$constants("WHEN")} ${when.join(` ${this.$constants("AND")} `)} ${this.$constants("THEN")} '${value}'`;
|
|
2013
2009
|
updateColumns[key].splice(startIndex + 1, 0, str);
|
|
2014
2010
|
}
|
|
2015
2011
|
}
|
|
2016
2012
|
for (const key in columns) {
|
|
2017
2013
|
if (updateColumns[key] == null)
|
|
2018
2014
|
continue;
|
|
2019
|
-
columns[key] = `( ${updateColumns[key].join(
|
|
2015
|
+
columns[key] = `( ${updateColumns[key].join(" ")} )`;
|
|
2020
2016
|
}
|
|
2021
2017
|
const keyValue = Object.entries(columns).map(([column, value]) => {
|
|
2022
|
-
if (typeof value ===
|
|
2018
|
+
if (typeof value === "string" &&
|
|
2019
|
+
!value.includes(this.$constants("RAW"))) {
|
|
2023
2020
|
value = this.$utils.escapeActions(value);
|
|
2024
2021
|
}
|
|
2025
|
-
return `${this.bindColumn(column)} = ${value == null || value === this.$constants(
|
|
2026
|
-
? this.$constants(
|
|
2027
|
-
: typeof value ===
|
|
2028
|
-
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants(
|
|
2022
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants("NULL")
|
|
2023
|
+
? this.$constants("NULL")
|
|
2024
|
+
: typeof value === "string" && value.includes(this.$constants("RAW"))
|
|
2025
|
+
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants("RAW"), "")
|
|
2029
2026
|
: `'${this.$utils.covertBooleanToNumber(value)}'`}`;
|
|
2030
2027
|
});
|
|
2031
|
-
const query = `${this.$constants(
|
|
2032
|
-
this.$state.set(
|
|
2033
|
-
`${this.$constants(
|
|
2034
|
-
`${this.$state.get(
|
|
2035
|
-
`${query}
|
|
2036
|
-
].join(
|
|
2037
|
-
this.$state.set(
|
|
2028
|
+
const query = `${this.$constants("SET")} ${keyValue.join(", ")}`;
|
|
2029
|
+
this.$state.set("UPDATE", [
|
|
2030
|
+
`${this.$constants("UPDATE")}`,
|
|
2031
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2032
|
+
`${query}`,
|
|
2033
|
+
].join(" "));
|
|
2034
|
+
this.$state.set("SAVE", "UPDATE");
|
|
2038
2035
|
return this;
|
|
2039
2036
|
}
|
|
2040
2037
|
/**
|
|
@@ -2049,18 +2046,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2049
2046
|
updateNotExists(data) {
|
|
2050
2047
|
this.limit(1);
|
|
2051
2048
|
if (!Object.keys(data).length)
|
|
2052
|
-
throw new Error(
|
|
2049
|
+
throw new Error("This method must be required");
|
|
2053
2050
|
for (const column in data) {
|
|
2054
2051
|
const value = data[column];
|
|
2055
2052
|
data[column] = this._updateHandler(column, value);
|
|
2056
2053
|
}
|
|
2057
2054
|
const query = this._queryUpdate(data);
|
|
2058
|
-
this.$state.set(
|
|
2059
|
-
`${this.$constants(
|
|
2060
|
-
`${this.$state.get(
|
|
2061
|
-
`${query}
|
|
2062
|
-
].join(
|
|
2063
|
-
this.$state.set(
|
|
2055
|
+
this.$state.set("UPDATE", [
|
|
2056
|
+
`${this.$constants("UPDATE")}`,
|
|
2057
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2058
|
+
`${query}`,
|
|
2059
|
+
].join(" "));
|
|
2060
|
+
this.$state.set("SAVE", "UPDATE");
|
|
2064
2061
|
return this;
|
|
2065
2062
|
}
|
|
2066
2063
|
/**
|
|
@@ -2072,14 +2069,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2072
2069
|
*/
|
|
2073
2070
|
insert(data) {
|
|
2074
2071
|
if (!Object.keys(data).length)
|
|
2075
|
-
throw new Error(
|
|
2072
|
+
throw new Error("This method must be required");
|
|
2076
2073
|
const query = this._queryInsert(data);
|
|
2077
|
-
this.$state.set(
|
|
2078
|
-
`${this.$constants(
|
|
2079
|
-
`${this.$state.get(
|
|
2080
|
-
`${query}
|
|
2081
|
-
].join(
|
|
2082
|
-
this.$state.set(
|
|
2074
|
+
this.$state.set("INSERT", [
|
|
2075
|
+
`${this.$constants("INSERT")}`,
|
|
2076
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2077
|
+
`${query}`,
|
|
2078
|
+
].join(" "));
|
|
2079
|
+
this.$state.set("SAVE", "INSERT");
|
|
2083
2080
|
return this;
|
|
2084
2081
|
}
|
|
2085
2082
|
/**
|
|
@@ -2091,14 +2088,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2091
2088
|
*/
|
|
2092
2089
|
create(data) {
|
|
2093
2090
|
if (!Object.keys(data).length)
|
|
2094
|
-
throw new Error(
|
|
2091
|
+
throw new Error("This method must be required");
|
|
2095
2092
|
const query = this._queryInsert(data);
|
|
2096
|
-
this.$state.set(
|
|
2097
|
-
`${this.$constants(
|
|
2098
|
-
`${this.$state.get(
|
|
2099
|
-
`${query}
|
|
2100
|
-
].join(
|
|
2101
|
-
this.$state.set(
|
|
2093
|
+
this.$state.set("INSERT", [
|
|
2094
|
+
`${this.$constants("INSERT")}`,
|
|
2095
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2096
|
+
`${query}`,
|
|
2097
|
+
].join(" "));
|
|
2098
|
+
this.$state.set("SAVE", "INSERT");
|
|
2102
2099
|
return this;
|
|
2103
2100
|
}
|
|
2104
2101
|
/**
|
|
@@ -2110,14 +2107,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2110
2107
|
*/
|
|
2111
2108
|
createMultiple(data) {
|
|
2112
2109
|
if (!Object.keys(data).length)
|
|
2113
|
-
throw new Error(
|
|
2110
|
+
throw new Error("This method must be required");
|
|
2114
2111
|
const query = this._queryInsertMultiple(data);
|
|
2115
|
-
this.$state.set(
|
|
2116
|
-
`${this.$constants(
|
|
2117
|
-
`${this.$state.get(
|
|
2118
|
-
`${query}
|
|
2119
|
-
].join(
|
|
2120
|
-
this.$state.set(
|
|
2112
|
+
this.$state.set("INSERT", [
|
|
2113
|
+
`${this.$constants("INSERT")}`,
|
|
2114
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2115
|
+
`${query}`,
|
|
2116
|
+
].join(" "));
|
|
2117
|
+
this.$state.set("SAVE", "INSERT_MULTIPLE");
|
|
2121
2118
|
return this;
|
|
2122
2119
|
}
|
|
2123
2120
|
/**
|
|
@@ -2140,12 +2137,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2140
2137
|
*/
|
|
2141
2138
|
createNotExists(data) {
|
|
2142
2139
|
const query = this._queryInsert(data);
|
|
2143
|
-
this.$state.set(
|
|
2144
|
-
`${this.$constants(
|
|
2145
|
-
`${this.$state.get(
|
|
2146
|
-
`${query}
|
|
2147
|
-
].join(
|
|
2148
|
-
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_NOT_EXISTS");
|
|
2149
2146
|
return this;
|
|
2150
2147
|
}
|
|
2151
2148
|
/**
|
|
@@ -2170,12 +2167,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2170
2167
|
*/
|
|
2171
2168
|
createOrSelect(data) {
|
|
2172
2169
|
const queryInsert = this._queryInsert(data);
|
|
2173
|
-
this.$state.set(
|
|
2174
|
-
`${this.$constants(
|
|
2175
|
-
`${this.$state.get(
|
|
2176
|
-
`${queryInsert}
|
|
2177
|
-
].join(
|
|
2178
|
-
this.$state.set(
|
|
2170
|
+
this.$state.set("INSERT", [
|
|
2171
|
+
`${this.$constants("INSERT")}`,
|
|
2172
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2173
|
+
`${queryInsert}`,
|
|
2174
|
+
].join(" "));
|
|
2175
|
+
this.$state.set("SAVE", "INSERT_OR_SELECT");
|
|
2179
2176
|
return this;
|
|
2180
2177
|
}
|
|
2181
2178
|
/**
|
|
@@ -2202,17 +2199,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2202
2199
|
this.limit(1);
|
|
2203
2200
|
const queryUpdate = this._queryUpdate(data);
|
|
2204
2201
|
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(
|
|
2202
|
+
this.$state.set("INSERT", [
|
|
2203
|
+
`${this.$constants("INSERT")}`,
|
|
2204
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2205
|
+
`${queryInsert}`,
|
|
2206
|
+
].join(" "));
|
|
2207
|
+
this.$state.set("UPDATE", [
|
|
2208
|
+
`${this.$constants("UPDATE")}`,
|
|
2209
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2210
|
+
`${queryUpdate}`,
|
|
2211
|
+
].join(" "));
|
|
2212
|
+
this.$state.set("SAVE", "UPDATE_OR_INSERT");
|
|
2216
2213
|
return this;
|
|
2217
2214
|
}
|
|
2218
2215
|
/**
|
|
@@ -2276,7 +2273,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2276
2273
|
*
|
|
2277
2274
|
* This method is particularly useful for debugging and understanding the SQL queries generated by your application.
|
|
2278
2275
|
* @returns {string}
|
|
2279
|
-
|
|
2276
|
+
*/
|
|
2280
2277
|
toRawSQL() {
|
|
2281
2278
|
return this.toString();
|
|
2282
2279
|
}
|
|
@@ -2285,7 +2282,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2285
2282
|
* @returns {string} return table name
|
|
2286
2283
|
*/
|
|
2287
2284
|
getTableName() {
|
|
2288
|
-
return this.$state.get(
|
|
2285
|
+
return this.$state.get("TABLE_NAME").replace(/\`/g, "");
|
|
2289
2286
|
}
|
|
2290
2287
|
/**
|
|
2291
2288
|
* The 'getColumns' method is used to get columns
|
|
@@ -2294,11 +2291,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2294
2291
|
getColumns() {
|
|
2295
2292
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2296
2293
|
const sql = [
|
|
2297
|
-
`${this.$constants(
|
|
2298
|
-
`${this.$constants(
|
|
2299
|
-
`${this.$constants(
|
|
2300
|
-
`\`${this.$state.get(
|
|
2301
|
-
].join(
|
|
2294
|
+
`${this.$constants("SHOW")}`,
|
|
2295
|
+
`${this.$constants("COLUMNS")}`,
|
|
2296
|
+
`${this.$constants("FROM")}`,
|
|
2297
|
+
`\`${this.$state.get("TABLE_NAME").replace(/\`/g, "")}\``,
|
|
2298
|
+
].join(" ");
|
|
2302
2299
|
const rawColumns = yield this._queryStatement(sql);
|
|
2303
2300
|
const columns = rawColumns.map((column) => column.Field);
|
|
2304
2301
|
return columns;
|
|
@@ -2311,11 +2308,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2311
2308
|
getSchema() {
|
|
2312
2309
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2313
2310
|
const sql = [
|
|
2314
|
-
`${this.$constants(
|
|
2315
|
-
`${this.$constants(
|
|
2316
|
-
`${this.$constants(
|
|
2317
|
-
`\`${this.$state.get(
|
|
2318
|
-
].join(
|
|
2311
|
+
`${this.$constants("SHOW")}`,
|
|
2312
|
+
`${this.$constants("COLUMNS")}`,
|
|
2313
|
+
`${this.$constants("FROM")}`,
|
|
2314
|
+
`\`${this.$state.get("TABLE_NAME").replace(/\`/g, "")}\``,
|
|
2315
|
+
].join(" ");
|
|
2319
2316
|
return yield this._queryStatement(sql);
|
|
2320
2317
|
});
|
|
2321
2318
|
}
|
|
@@ -2326,25 +2323,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2326
2323
|
*/
|
|
2327
2324
|
bindColumn(column) {
|
|
2328
2325
|
if (!/\./.test(column)) {
|
|
2329
|
-
if (column ===
|
|
2330
|
-
return
|
|
2331
|
-
const alias = this.$state.get(
|
|
2332
|
-
if (this.getTableName() ===
|
|
2333
|
-
return `\`${column.replace(/`/g,
|
|
2326
|
+
if (column === "*")
|
|
2327
|
+
return "*";
|
|
2328
|
+
const alias = this.$state.get("ALIAS");
|
|
2329
|
+
if (this.getTableName() === "") {
|
|
2330
|
+
return `\`${column.replace(/`/g, "")}\``;
|
|
2334
2331
|
}
|
|
2335
2332
|
return [
|
|
2336
|
-
alias == null || alias ===
|
|
2337
|
-
? `\`${this.getTableName().replace(/`/g,
|
|
2338
|
-
: `\`${alias.replace(/`/g,
|
|
2339
|
-
|
|
2340
|
-
`\`${column.replace(/`/g,
|
|
2341
|
-
].join(
|
|
2333
|
+
alias == null || alias === ""
|
|
2334
|
+
? `\`${this.getTableName().replace(/`/g, "")}\``
|
|
2335
|
+
: `\`${alias.replace(/`/g, "")}\``,
|
|
2336
|
+
".",
|
|
2337
|
+
`\`${column.replace(/`/g, "")}\``,
|
|
2338
|
+
].join("");
|
|
2342
2339
|
}
|
|
2343
|
-
const [table, c] = column.split(
|
|
2344
|
-
if (c ===
|
|
2345
|
-
return `\`${table.replace(/`/g,
|
|
2340
|
+
const [table, c] = column.split(".");
|
|
2341
|
+
if (c === "*") {
|
|
2342
|
+
return `\`${table.replace(/`/g, "")}\`.*`;
|
|
2346
2343
|
}
|
|
2347
|
-
return `\`${table.replace(/`/g,
|
|
2344
|
+
return `\`${table.replace(/`/g, "")}\`.\`${c.replace(/`/g, "")}\``;
|
|
2348
2345
|
}
|
|
2349
2346
|
/**
|
|
2350
2347
|
* The 'debug' method is used to console.log raw SQL query that would be executed by a query builder
|
|
@@ -2352,7 +2349,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2352
2349
|
* @returns {this} this this
|
|
2353
2350
|
*/
|
|
2354
2351
|
debug(debug = true) {
|
|
2355
|
-
this.$state.set(
|
|
2352
|
+
this.$state.set("DEBUG", debug);
|
|
2356
2353
|
return this;
|
|
2357
2354
|
}
|
|
2358
2355
|
/**
|
|
@@ -2361,29 +2358,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2361
2358
|
* @returns {this} this this
|
|
2362
2359
|
*/
|
|
2363
2360
|
dd(debug = true) {
|
|
2364
|
-
this.$state.set(
|
|
2361
|
+
this.$state.set("DEBUG", debug);
|
|
2365
2362
|
return this;
|
|
2366
2363
|
}
|
|
2367
2364
|
/**
|
|
2368
2365
|
* The 'hook' method is used function when execute returns a result to callback function
|
|
2369
2366
|
* @param {Function} func function for callback result
|
|
2370
2367
|
* @returns {this}
|
|
2371
|
-
|
|
2368
|
+
*/
|
|
2372
2369
|
hook(func) {
|
|
2373
2370
|
if (typeof func !== "function")
|
|
2374
2371
|
throw new Error(`this '${func}' is not a function`);
|
|
2375
|
-
this.$state.set(
|
|
2372
|
+
this.$state.set("HOOKS", [...this.$state.get("HOOKS"), func]);
|
|
2376
2373
|
return this;
|
|
2377
2374
|
}
|
|
2378
2375
|
/**
|
|
2379
2376
|
* The 'before' method is used function when execute returns a result to callback function
|
|
2380
2377
|
* @param {Function} func function for callback result
|
|
2381
2378
|
* @returns {this}
|
|
2382
|
-
|
|
2379
|
+
*/
|
|
2383
2380
|
before(func) {
|
|
2384
2381
|
if (typeof func !== "function")
|
|
2385
2382
|
throw new Error(`this '${func}' is not a function`);
|
|
2386
|
-
this.$state.set(
|
|
2383
|
+
this.$state.set("HOOKS", [...this.$state.get("HOOKS"), func]);
|
|
2387
2384
|
return this;
|
|
2388
2385
|
}
|
|
2389
2386
|
/**
|
|
@@ -2398,7 +2395,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2398
2395
|
*/
|
|
2399
2396
|
connection(options) {
|
|
2400
2397
|
const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
|
|
2401
|
-
const pool = new
|
|
2398
|
+
const pool = new Pool_1.PoolConnection(Object.assign({ host,
|
|
2402
2399
|
port,
|
|
2403
2400
|
database,
|
|
2404
2401
|
user,
|
|
@@ -2414,13 +2411,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2414
2411
|
loadEnv(env) {
|
|
2415
2412
|
if (env === null)
|
|
2416
2413
|
return this;
|
|
2417
|
-
const options = (0,
|
|
2418
|
-
const pool = new
|
|
2414
|
+
const options = (0, Pool_1.loadOptionsEnvironment)();
|
|
2415
|
+
const pool = new Pool_1.PoolConnection({
|
|
2419
2416
|
host: String(options.host),
|
|
2420
2417
|
port: Number(options.port),
|
|
2421
2418
|
database: String(options.database),
|
|
2422
2419
|
user: String(options.username),
|
|
2423
|
-
password: String(options.password)
|
|
2420
|
+
password: String(options.password),
|
|
2424
2421
|
});
|
|
2425
2422
|
this.$pool.set(pool.connected());
|
|
2426
2423
|
return this;
|
|
@@ -2431,8 +2428,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2431
2428
|
* @returns {this} this
|
|
2432
2429
|
*/
|
|
2433
2430
|
pool(pool) {
|
|
2434
|
-
if (!(pool === null || pool === void 0 ? void 0 : pool.hasOwnProperty(
|
|
2435
|
-
throw new Error(
|
|
2431
|
+
if (!(pool === null || pool === void 0 ? void 0 : pool.hasOwnProperty("query"))) {
|
|
2432
|
+
throw new Error("pool must have a query property");
|
|
2436
2433
|
}
|
|
2437
2434
|
this.$pool.set(pool);
|
|
2438
2435
|
return this;
|
|
@@ -2443,11 +2440,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2443
2440
|
* @returns {this} this
|
|
2444
2441
|
*/
|
|
2445
2442
|
bind(connection) {
|
|
2446
|
-
if (!(connection === null || connection === void 0 ? void 0 : connection.hasOwnProperty(
|
|
2447
|
-
throw new Error(
|
|
2443
|
+
if (!(connection === null || connection === void 0 ? void 0 : connection.hasOwnProperty("query"))) {
|
|
2444
|
+
throw new Error("connection must have a query property");
|
|
2448
2445
|
}
|
|
2449
|
-
if (typeof connection.query !==
|
|
2450
|
-
throw new Error(
|
|
2446
|
+
if (typeof connection.query !== "function") {
|
|
2447
|
+
throw new Error("connection must have a query function");
|
|
2451
2448
|
}
|
|
2452
2449
|
this.$pool.set(connection);
|
|
2453
2450
|
return this;
|
|
@@ -2480,13 +2477,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2480
2477
|
* @returns {promise<any>}
|
|
2481
2478
|
*/
|
|
2482
2479
|
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(
|
|
2480
|
+
return __awaiter(this, arguments, void 0, function* (column = "id", value = 1) {
|
|
2481
|
+
const query = `${this.$constants("SET")} ${column} = ${column} + ${value}`;
|
|
2482
|
+
this.$state.set("UPDATE", [
|
|
2483
|
+
`${this.$constants("UPDATE")}`,
|
|
2484
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2485
|
+
`${query}`,
|
|
2486
|
+
].join(" "));
|
|
2490
2487
|
return yield this._update(true);
|
|
2491
2488
|
});
|
|
2492
2489
|
}
|
|
@@ -2498,19 +2495,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2498
2495
|
* @returns {promise<any>}
|
|
2499
2496
|
*/
|
|
2500
2497
|
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(
|
|
2498
|
+
return __awaiter(this, arguments, void 0, function* (column = "id", value = 1) {
|
|
2499
|
+
const query = `${this.$constants("SET")} ${column} = ${column} - ${value}`;
|
|
2500
|
+
this.$state.set("UPDATE", [
|
|
2501
|
+
`${this.$constants("UPDATE")}`,
|
|
2502
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2503
|
+
`${query}`,
|
|
2504
|
+
].join(" "));
|
|
2508
2505
|
return yield this._update(true);
|
|
2509
2506
|
});
|
|
2510
2507
|
}
|
|
2511
2508
|
version() {
|
|
2512
2509
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2513
|
-
const result = yield this._queryStatement(`${this.$constants(
|
|
2510
|
+
const result = yield this._queryStatement(`${this.$constants("SELECT")} VERSION() as version`);
|
|
2514
2511
|
return result[0].version;
|
|
2515
2512
|
});
|
|
2516
2513
|
}
|
|
@@ -2523,11 +2520,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2523
2520
|
all() {
|
|
2524
2521
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2525
2522
|
return yield this._queryStatement([
|
|
2526
|
-
`${this.$constants(
|
|
2523
|
+
`${this.$constants("SELECT")}`,
|
|
2527
2524
|
`*`,
|
|
2528
|
-
`${this.$constants(
|
|
2529
|
-
`${this.$state.get(
|
|
2530
|
-
].join(
|
|
2525
|
+
`${this.$constants("FROM")}`,
|
|
2526
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2527
|
+
].join(" "));
|
|
2531
2528
|
});
|
|
2532
2529
|
}
|
|
2533
2530
|
/**
|
|
@@ -2539,7 +2536,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2539
2536
|
*/
|
|
2540
2537
|
find(id) {
|
|
2541
2538
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2542
|
-
this.where(
|
|
2539
|
+
this.where("id", id);
|
|
2543
2540
|
return yield this.first();
|
|
2544
2541
|
});
|
|
2545
2542
|
}
|
|
@@ -2570,7 +2567,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2570
2567
|
this.offset(offset);
|
|
2571
2568
|
let sql = this._queryBuilder().select();
|
|
2572
2569
|
const result = yield this._queryStatement(sql);
|
|
2573
|
-
if ((_a = this.$state.get(
|
|
2570
|
+
if ((_a = this.$state.get("HIDDEN")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2574
2571
|
this._hiddenColumn(result);
|
|
2575
2572
|
if (!result.length)
|
|
2576
2573
|
return {
|
|
@@ -2582,14 +2579,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2582
2579
|
current_page: currentPage,
|
|
2583
2580
|
last_page: 0,
|
|
2584
2581
|
next_page: 0,
|
|
2585
|
-
prev_page: 0
|
|
2582
|
+
prev_page: 0,
|
|
2586
2583
|
},
|
|
2587
|
-
data: []
|
|
2584
|
+
data: [],
|
|
2588
2585
|
};
|
|
2589
2586
|
const total = yield new DB_1.DB()
|
|
2590
2587
|
.copyBuilder(this, { where: true, join: true })
|
|
2591
2588
|
.bind(this.$pool.get())
|
|
2592
|
-
.debug(this.$state.get(
|
|
2589
|
+
.debug(this.$state.get("DEBUG"))
|
|
2593
2590
|
.unset({ alias: true })
|
|
2594
2591
|
.count();
|
|
2595
2592
|
let lastPage = Math.ceil(total / limit) || 0;
|
|
@@ -2604,9 +2601,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2604
2601
|
current_page: currentPage,
|
|
2605
2602
|
last_page: lastPage,
|
|
2606
2603
|
next_page: nextPage,
|
|
2607
|
-
prev_page: prevPage
|
|
2604
|
+
prev_page: prevPage,
|
|
2608
2605
|
},
|
|
2609
|
-
data: result !== null && result !== void 0 ? result : []
|
|
2606
|
+
data: result !== null && result !== void 0 ? result : [],
|
|
2610
2607
|
};
|
|
2611
2608
|
});
|
|
2612
2609
|
}
|
|
@@ -2642,39 +2639,41 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2642
2639
|
first(cb) {
|
|
2643
2640
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2644
2641
|
var _a, _b;
|
|
2645
|
-
if ((_a = this.$state.get(
|
|
2646
|
-
this.select(...yield this.exceptColumns());
|
|
2642
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2643
|
+
this.select(...(yield this.exceptColumns()));
|
|
2647
2644
|
this.limit(1);
|
|
2648
2645
|
let sql = this._queryBuilder().select();
|
|
2649
2646
|
if (cb) {
|
|
2650
2647
|
const callbackSql = cb(sql);
|
|
2651
|
-
if (callbackSql == null || callbackSql ===
|
|
2652
|
-
throw new Error(
|
|
2648
|
+
if (callbackSql == null || callbackSql === "")
|
|
2649
|
+
throw new Error("Please provide a callback for execution");
|
|
2653
2650
|
sql = callbackSql;
|
|
2654
2651
|
}
|
|
2655
2652
|
const result = yield this._queryStatement(sql);
|
|
2656
|
-
if (this.$state.get(
|
|
2653
|
+
if (this.$state.get("VOID"))
|
|
2657
2654
|
return null;
|
|
2658
|
-
if ((_b = this.$state.get(
|
|
2655
|
+
if ((_b = this.$state.get("HIDDEN")) === null || _b === void 0 ? void 0 : _b.length)
|
|
2659
2656
|
this._hiddenColumn(result);
|
|
2660
|
-
if (this.$state.get(
|
|
2661
|
-
const pluck = this.$state.get(
|
|
2657
|
+
if (this.$state.get("PLUCK")) {
|
|
2658
|
+
const pluck = this.$state.get("PLUCK");
|
|
2662
2659
|
const newData = result === null || result === void 0 ? void 0 : result.shift();
|
|
2663
2660
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
2664
2661
|
if (!checkProperty)
|
|
2665
2662
|
throw new Error(`can't find property '${pluck}' of result`);
|
|
2666
2663
|
const r = newData[pluck] || null;
|
|
2667
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2664
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), r);
|
|
2668
2665
|
return r;
|
|
2669
2666
|
}
|
|
2670
|
-
if (this.$state.get(
|
|
2671
|
-
const returnType = this.$state.get(
|
|
2672
|
-
return this._resultHandler(returnType ===
|
|
2667
|
+
if (this.$state.get("RETURN_TYPE") != null) {
|
|
2668
|
+
const returnType = this.$state.get("RETURN_TYPE");
|
|
2669
|
+
return this._resultHandler(returnType === "object"
|
|
2673
2670
|
? result[0]
|
|
2674
|
-
: returnType ===
|
|
2671
|
+
: returnType === "array"
|
|
2672
|
+
? result
|
|
2673
|
+
: [result]);
|
|
2675
2674
|
}
|
|
2676
2675
|
const r = result[0] || null;
|
|
2677
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2676
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), r);
|
|
2678
2677
|
return this._resultHandler(r);
|
|
2679
2678
|
});
|
|
2680
2679
|
}
|
|
@@ -2702,15 +2701,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2702
2701
|
firstOrError(message, options) {
|
|
2703
2702
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2704
2703
|
var _a, _b;
|
|
2705
|
-
if ((_a = this.$state.get(
|
|
2706
|
-
this.select(...yield this.exceptColumns());
|
|
2704
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2705
|
+
this.select(...(yield this.exceptColumns()));
|
|
2707
2706
|
this.limit(1);
|
|
2708
2707
|
let sql = this._queryBuilder().select();
|
|
2709
2708
|
const result = yield this._queryStatement(sql);
|
|
2710
|
-
if ((_b = this.$state.get(
|
|
2709
|
+
if ((_b = this.$state.get("HIDDEN")) === null || _b === void 0 ? void 0 : _b.length)
|
|
2711
2710
|
this._hiddenColumn(result);
|
|
2712
|
-
if (this.$state.get(
|
|
2713
|
-
const pluck = this.$state.get(
|
|
2711
|
+
if (this.$state.get("PLUCK")) {
|
|
2712
|
+
const pluck = this.$state.get("PLUCK");
|
|
2714
2713
|
const newData = result === null || result === void 0 ? void 0 : result.shift();
|
|
2715
2714
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
2716
2715
|
if (!checkProperty)
|
|
@@ -2721,7 +2720,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2721
2720
|
throw { message, code: 400 };
|
|
2722
2721
|
throw Object.assign({ message }, options);
|
|
2723
2722
|
}
|
|
2724
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2723
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), data);
|
|
2725
2724
|
return this._resultHandler(data);
|
|
2726
2725
|
}
|
|
2727
2726
|
const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
@@ -2731,7 +2730,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2731
2730
|
}
|
|
2732
2731
|
throw Object.assign({ message }, options);
|
|
2733
2732
|
}
|
|
2734
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2733
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), data);
|
|
2735
2734
|
return this._resultHandler(data);
|
|
2736
2735
|
});
|
|
2737
2736
|
}
|
|
@@ -2759,46 +2758,48 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2759
2758
|
get(cb) {
|
|
2760
2759
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2761
2760
|
var _a, _b;
|
|
2762
|
-
if ((_a = this.$state.get(
|
|
2763
|
-
this.select(...yield this.exceptColumns());
|
|
2761
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
2762
|
+
this.select(...(yield this.exceptColumns()));
|
|
2764
2763
|
let sql = this._queryBuilder().select();
|
|
2765
2764
|
if (cb) {
|
|
2766
2765
|
const callbackSql = cb(sql);
|
|
2767
|
-
if (callbackSql == null || callbackSql ===
|
|
2768
|
-
throw new Error(
|
|
2766
|
+
if (callbackSql == null || callbackSql === "")
|
|
2767
|
+
throw new Error("Please provide a callback for execution");
|
|
2769
2768
|
sql = callbackSql;
|
|
2770
2769
|
}
|
|
2771
2770
|
const result = yield this._queryStatement(sql);
|
|
2772
|
-
if (this.$state.get(
|
|
2771
|
+
if (this.$state.get("VOID"))
|
|
2773
2772
|
return [];
|
|
2774
|
-
if ((_b = this.$state.get(
|
|
2773
|
+
if ((_b = this.$state.get("HIDDEN")) === null || _b === void 0 ? void 0 : _b.length)
|
|
2775
2774
|
this._hiddenColumn(result);
|
|
2776
|
-
if (this.$state.get(
|
|
2775
|
+
if (this.$state.get("CHUNK")) {
|
|
2777
2776
|
const data = result.reduce((resultArray, item, index) => {
|
|
2778
|
-
const chunkIndex = Math.floor(index / this.$state.get(
|
|
2777
|
+
const chunkIndex = Math.floor(index / this.$state.get("CHUNK"));
|
|
2779
2778
|
if (!resultArray[chunkIndex])
|
|
2780
2779
|
resultArray[chunkIndex] = [];
|
|
2781
2780
|
resultArray[chunkIndex].push(item);
|
|
2782
2781
|
return resultArray;
|
|
2783
2782
|
}, []);
|
|
2784
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2783
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), data || []);
|
|
2785
2784
|
return this._resultHandler(data || []);
|
|
2786
2785
|
}
|
|
2787
|
-
if (this.$state.get(
|
|
2788
|
-
const pluck = this.$state.get(
|
|
2786
|
+
if (this.$state.get("PLUCK")) {
|
|
2787
|
+
const pluck = this.$state.get("PLUCK");
|
|
2789
2788
|
const newData = result.map((d) => d[pluck]);
|
|
2790
2789
|
if (newData.every((d) => d == null)) {
|
|
2791
2790
|
throw new Error(`can't find property '${pluck}' of result`);
|
|
2792
2791
|
}
|
|
2793
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2792
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), newData || []);
|
|
2794
2793
|
return this._resultHandler(newData || []);
|
|
2795
2794
|
}
|
|
2796
|
-
yield this.$utils.hookHandle(this.$state.get(
|
|
2797
|
-
if (this.$state.get(
|
|
2798
|
-
const returnType = this.$state.get(
|
|
2799
|
-
return this._resultHandler(returnType ===
|
|
2795
|
+
yield this.$utils.hookHandle(this.$state.get("HOOKS"), result || []);
|
|
2796
|
+
if (this.$state.get("RETURN_TYPE") != null) {
|
|
2797
|
+
const returnType = this.$state.get("RETURN_TYPE");
|
|
2798
|
+
return this._resultHandler(returnType === "object"
|
|
2800
2799
|
? result[0]
|
|
2801
|
-
: returnType ===
|
|
2800
|
+
: returnType === "array"
|
|
2801
|
+
? result
|
|
2802
|
+
: [result]);
|
|
2802
2803
|
}
|
|
2803
2804
|
return this._resultHandler(result || []);
|
|
2804
2805
|
});
|
|
@@ -2828,7 +2829,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2828
2829
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2829
2830
|
const sql = this._queryBuilder().select();
|
|
2830
2831
|
const result = yield this._queryStatement(sql);
|
|
2831
|
-
if (this.$state.get(
|
|
2832
|
+
if (this.$state.get("HIDDEN").length)
|
|
2832
2833
|
this._hiddenColumn(result);
|
|
2833
2834
|
return this._resultHandler(JSON.stringify(result));
|
|
2834
2835
|
});
|
|
@@ -2843,7 +2844,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2843
2844
|
* @returns {promise<Array>}
|
|
2844
2845
|
*/
|
|
2845
2846
|
toArray() {
|
|
2846
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2847
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2847
2848
|
this.selectRaw(`${this.bindColumn(column)}`);
|
|
2848
2849
|
const sql = this._queryBuilder().select();
|
|
2849
2850
|
const result = yield this._queryStatement(sql);
|
|
@@ -2862,15 +2863,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2862
2863
|
var _a;
|
|
2863
2864
|
const sql = new Builder()
|
|
2864
2865
|
.copyBuilder(this, { where: true, limit: true, join: true })
|
|
2865
|
-
.selectRaw(
|
|
2866
|
+
.selectRaw("1")
|
|
2866
2867
|
.limit(1)
|
|
2867
2868
|
.toString();
|
|
2868
2869
|
const result = yield this._queryStatement([
|
|
2869
|
-
`${this.$constants(
|
|
2870
|
-
`${this.$constants(
|
|
2870
|
+
`${this.$constants("SELECT")}`,
|
|
2871
|
+
`${this.$constants("EXISTS")}`,
|
|
2871
2872
|
`(${sql})`,
|
|
2872
|
-
`${this.$constants(
|
|
2873
|
-
].join(
|
|
2873
|
+
`${this.$constants("AS")} \`aggregate\``,
|
|
2874
|
+
].join(" "));
|
|
2874
2875
|
return Boolean(this._resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.aggregate) || false));
|
|
2875
2876
|
});
|
|
2876
2877
|
}
|
|
@@ -2882,14 +2883,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2882
2883
|
* @returns {promise<number>}
|
|
2883
2884
|
*/
|
|
2884
2885
|
count() {
|
|
2885
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2886
|
-
const distinct = this.$state.get(
|
|
2887
|
-
column =
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2886
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2887
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2888
|
+
column =
|
|
2889
|
+
column === "*"
|
|
2890
|
+
? "*"
|
|
2891
|
+
: distinct
|
|
2892
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2893
|
+
: `${this.bindColumn(column)}`;
|
|
2894
|
+
this.selectRaw(`${this.$constants("COUNT")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2893
2895
|
const sql = this._queryBuilder().select();
|
|
2894
2896
|
const result = yield this._queryStatement(sql);
|
|
2895
2897
|
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 +2905,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2903
2905
|
* @returns {promise<number>}
|
|
2904
2906
|
*/
|
|
2905
2907
|
avg() {
|
|
2906
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2907
|
-
const distinct = this.$state.get(
|
|
2908
|
-
column = distinct
|
|
2909
|
-
|
|
2908
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2909
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2910
|
+
column = distinct
|
|
2911
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2912
|
+
: `${this.bindColumn(column)}`;
|
|
2913
|
+
this.selectRaw(`${this.$constants("AVG")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2910
2914
|
const sql = this._queryBuilder().select();
|
|
2911
2915
|
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) ||
|
|
2916
|
+
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) ||
|
|
2917
|
+
0) / result.length));
|
|
2913
2918
|
});
|
|
2914
2919
|
}
|
|
2915
2920
|
/**
|
|
@@ -2920,10 +2925,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2920
2925
|
* @returns {promise<number>}
|
|
2921
2926
|
*/
|
|
2922
2927
|
sum() {
|
|
2923
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2924
|
-
const distinct = this.$state.get(
|
|
2925
|
-
column = distinct
|
|
2926
|
-
|
|
2928
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2929
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2930
|
+
column = distinct
|
|
2931
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2932
|
+
: `${this.bindColumn(column)}`;
|
|
2933
|
+
this.selectRaw(`${this.$constants("SUM")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2927
2934
|
const sql = this._queryBuilder().select();
|
|
2928
2935
|
const result = yield this._queryStatement(sql);
|
|
2929
2936
|
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 +2944,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2937
2944
|
* @returns {promise<number>}
|
|
2938
2945
|
*/
|
|
2939
2946
|
max() {
|
|
2940
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2947
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2941
2948
|
var _a;
|
|
2942
|
-
const distinct = this.$state.get(
|
|
2943
|
-
column = distinct
|
|
2944
|
-
|
|
2949
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2950
|
+
column = distinct
|
|
2951
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2952
|
+
: `${this.bindColumn(column)}`;
|
|
2953
|
+
this.selectRaw(`${this.$constants("MAX")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2945
2954
|
const sql = this._queryBuilder().select();
|
|
2946
2955
|
const result = yield this._queryStatement(sql);
|
|
2947
2956
|
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 +2964,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2955
2964
|
* @returns {promise<number>}
|
|
2956
2965
|
*/
|
|
2957
2966
|
min() {
|
|
2958
|
-
return __awaiter(this, arguments, void 0, function* (column =
|
|
2967
|
+
return __awaiter(this, arguments, void 0, function* (column = "id") {
|
|
2959
2968
|
var _a;
|
|
2960
|
-
const distinct = this.$state.get(
|
|
2961
|
-
column = distinct
|
|
2962
|
-
|
|
2969
|
+
const distinct = this.$state.get("DISTINCT");
|
|
2970
|
+
column = distinct
|
|
2971
|
+
? `${this.$constants("DISTINCT")} ${this.bindColumn(column)}`
|
|
2972
|
+
: `${this.bindColumn(column)}`;
|
|
2973
|
+
this.selectRaw(`${this.$constants("MIN")}(${column}) ${this.$constants("AS")} \`aggregate\``);
|
|
2963
2974
|
const sql = this._queryBuilder().select();
|
|
2964
2975
|
const result = yield this._queryStatement(sql);
|
|
2965
2976
|
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 +2984,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2973
2984
|
*/
|
|
2974
2985
|
delete() {
|
|
2975
2986
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2976
|
-
if (!this.$state.get(
|
|
2987
|
+
if (!this.$state.get("WHERE").length) {
|
|
2977
2988
|
throw new Error("can't delete without where condition");
|
|
2978
2989
|
}
|
|
2979
2990
|
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({
|
|
2991
|
+
this.$state.set("DELETE", [
|
|
2992
|
+
`${this.$constants("DELETE")}`,
|
|
2993
|
+
`${this.$constants("FROM")}`,
|
|
2994
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
2995
|
+
].join(" "));
|
|
2996
|
+
const result = yield this._actionStatement({
|
|
2997
|
+
sql: this._queryBuilder().delete(),
|
|
2998
|
+
});
|
|
2986
2999
|
if (result)
|
|
2987
3000
|
return Boolean(this._resultHandler(!!result || false));
|
|
2988
3001
|
return Boolean(this._resultHandler(false));
|
|
@@ -2996,15 +3009,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2996
3009
|
*/
|
|
2997
3010
|
deleteMany() {
|
|
2998
3011
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2999
|
-
if (!this.$state.get(
|
|
3012
|
+
if (!this.$state.get("WHERE").length) {
|
|
3000
3013
|
throw new Error("can't delete without where condition");
|
|
3001
3014
|
}
|
|
3002
|
-
this.$state.set(
|
|
3003
|
-
`${this.$constants(
|
|
3004
|
-
`${this.$constants(
|
|
3005
|
-
`${this.$state.get(
|
|
3006
|
-
].join(
|
|
3007
|
-
const result = yield this._actionStatement({
|
|
3015
|
+
this.$state.set("DELETE", [
|
|
3016
|
+
`${this.$constants("DELETE")}`,
|
|
3017
|
+
`${this.$constants("FROM")}`,
|
|
3018
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3019
|
+
].join(" "));
|
|
3020
|
+
const result = yield this._actionStatement({
|
|
3021
|
+
sql: this._queryBuilder().delete(),
|
|
3022
|
+
});
|
|
3008
3023
|
if (result)
|
|
3009
3024
|
return Boolean(this._resultHandler(!!result || false));
|
|
3010
3025
|
return Boolean(this._resultHandler(false));
|
|
@@ -3021,12 +3036,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3021
3036
|
*/
|
|
3022
3037
|
forceDelete() {
|
|
3023
3038
|
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({
|
|
3039
|
+
this.$state.set("DELETE", [
|
|
3040
|
+
`${this.$constants("DELETE")}`,
|
|
3041
|
+
`${this.$constants("FROM")}`,
|
|
3042
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3043
|
+
].join(" "));
|
|
3044
|
+
const result = yield this._actionStatement({
|
|
3045
|
+
sql: this._queryBuilder().delete(),
|
|
3046
|
+
});
|
|
3030
3047
|
if (result)
|
|
3031
3048
|
return Boolean(this._resultHandler(!!result || false));
|
|
3032
3049
|
return Boolean(this._resultHandler(!!result || false));
|
|
@@ -3050,34 +3067,34 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3050
3067
|
getGroupBy(column) {
|
|
3051
3068
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3052
3069
|
var _a, _b, _c;
|
|
3053
|
-
if ((_a = this.$state.get(
|
|
3054
|
-
this.select(...yield this.exceptColumns());
|
|
3070
|
+
if ((_a = this.$state.get("EXCEPTS")) === null || _a === void 0 ? void 0 : _a.length)
|
|
3071
|
+
this.select(...(yield this.exceptColumns()));
|
|
3055
3072
|
const results = yield new Builder()
|
|
3056
3073
|
.copyBuilder(this, {
|
|
3057
3074
|
where: true,
|
|
3058
3075
|
limit: true,
|
|
3059
3076
|
join: true,
|
|
3060
|
-
orderBy: true
|
|
3077
|
+
orderBy: true,
|
|
3061
3078
|
})
|
|
3062
3079
|
.select(column)
|
|
3063
3080
|
.selectRaw([
|
|
3064
|
-
`${this.$constants(
|
|
3065
|
-
`${this.$constants(
|
|
3066
|
-
].join(
|
|
3081
|
+
`${this.$constants("GROUP_CONCAT")}(${this.bindColumn("id")})`,
|
|
3082
|
+
`${this.$constants("AS")} \`aggregate\``,
|
|
3083
|
+
].join(" "))
|
|
3067
3084
|
.groupBy(column)
|
|
3068
3085
|
.oldest()
|
|
3069
3086
|
.bind(this.$pool.get())
|
|
3070
|
-
.debug(this.$state.get(
|
|
3087
|
+
.debug(this.$state.get("DEBUG"))
|
|
3071
3088
|
.get();
|
|
3072
3089
|
const ids = [];
|
|
3073
3090
|
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(
|
|
3091
|
+
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
3092
|
ids.push(...splits);
|
|
3076
3093
|
}
|
|
3077
3094
|
const grouping = yield new Builder()
|
|
3078
|
-
.whereIn(
|
|
3095
|
+
.whereIn("id", ids)
|
|
3079
3096
|
.bind(this.$pool.get())
|
|
3080
|
-
.debug(this.$state.get(
|
|
3097
|
+
.debug(this.$state.get("DEBUG"))
|
|
3081
3098
|
.get();
|
|
3082
3099
|
const result = grouping.reduce((map, data) => {
|
|
3083
3100
|
const id = data[column];
|
|
@@ -3091,20 +3108,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3091
3108
|
});
|
|
3092
3109
|
}
|
|
3093
3110
|
/**
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3111
|
+
* The 'findGroupBy' method is used to execute a database query and retrieve the result set that matches the query conditions.
|
|
3112
|
+
*
|
|
3113
|
+
* It retrieves multiple records from a database table based on the criteria specified in the query.
|
|
3114
|
+
*
|
|
3115
|
+
* It returns record to new Map
|
|
3116
|
+
* @param {string} column
|
|
3117
|
+
* @example
|
|
3118
|
+
* const results = await new DB('posts')
|
|
3119
|
+
* .findGroupBy('user_id')
|
|
3120
|
+
*
|
|
3121
|
+
* // you can find with user id in the results
|
|
3122
|
+
* const postsByUserId1 = results.get(1)
|
|
3123
|
+
* @returns {promise<Array>}
|
|
3124
|
+
*/
|
|
3108
3125
|
findGroupBy(column) {
|
|
3109
3126
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3110
3127
|
return yield this.getGroupBy(column);
|
|
@@ -3118,15 +3135,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3118
3135
|
*/
|
|
3119
3136
|
save() {
|
|
3120
3137
|
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
|
-
|
|
3138
|
+
this.$state.set("AFTER_SAVE", waitMs);
|
|
3139
|
+
switch (this.$state.get("SAVE")) {
|
|
3140
|
+
case "INSERT":
|
|
3141
|
+
return yield this._insert();
|
|
3142
|
+
case "UPDATE":
|
|
3143
|
+
return yield this._update();
|
|
3144
|
+
case "INSERT_MULTIPLE":
|
|
3145
|
+
return yield this._insertMultiple();
|
|
3146
|
+
case "INSERT_NOT_EXISTS":
|
|
3147
|
+
return yield this._insertNotExists();
|
|
3148
|
+
case "UPDATE_OR_INSERT":
|
|
3149
|
+
return yield this._updateOrInsert();
|
|
3150
|
+
case "INSERT_OR_SELECT":
|
|
3151
|
+
return yield this._insertOrSelect();
|
|
3152
|
+
default:
|
|
3153
|
+
throw new Error(`unknown this [${this.$state.get("SAVE")}]`);
|
|
3130
3154
|
}
|
|
3131
3155
|
});
|
|
3132
3156
|
}
|
|
@@ -3139,14 +3163,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3139
3163
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3140
3164
|
const makeStatement = (columns) => {
|
|
3141
3165
|
return [
|
|
3142
|
-
`${this.$constants(
|
|
3143
|
-
`${columns.join(
|
|
3144
|
-
`${this.$constants(
|
|
3166
|
+
`${this.$constants("SELECT")}`,
|
|
3167
|
+
`${columns.join(", ")}`,
|
|
3168
|
+
`${this.$constants("FROM")}`,
|
|
3145
3169
|
`\`${this.getTableName()}\``,
|
|
3146
|
-
].join(
|
|
3170
|
+
].join(" ");
|
|
3147
3171
|
};
|
|
3148
3172
|
const schemaTable = yield this.getSchema();
|
|
3149
|
-
const columns = schemaTable.map(column => this.bindColumn(column.Field));
|
|
3173
|
+
const columns = schemaTable.map((column) => this.bindColumn(column.Field));
|
|
3150
3174
|
return makeStatement(columns);
|
|
3151
3175
|
});
|
|
3152
3176
|
}
|
|
@@ -3159,15 +3183,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3159
3183
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3160
3184
|
const makeStatement = (columns) => {
|
|
3161
3185
|
return [
|
|
3162
|
-
`${this.$constants(
|
|
3186
|
+
`${this.$constants("INSERT")}`,
|
|
3163
3187
|
`\`${this.getTableName()}\``,
|
|
3164
|
-
`(${columns.join(
|
|
3165
|
-
`${this.$constants(
|
|
3166
|
-
`(${Array(columns.length).fill(
|
|
3167
|
-
].join(
|
|
3188
|
+
`(${columns.join(", ")})`,
|
|
3189
|
+
`${this.$constants("VALUES")}`,
|
|
3190
|
+
`(${Array(columns.length).fill("`?`").join(" , ")})`,
|
|
3191
|
+
].join(" ");
|
|
3168
3192
|
};
|
|
3169
3193
|
const schemaTable = yield this.getSchema();
|
|
3170
|
-
const columns = schemaTable.map(column => `\`${column.Field}\``);
|
|
3194
|
+
const columns = schemaTable.map((column) => `\`${column.Field}\``);
|
|
3171
3195
|
return makeStatement(columns);
|
|
3172
3196
|
});
|
|
3173
3197
|
}
|
|
@@ -3180,16 +3204,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3180
3204
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3181
3205
|
const makeStatement = (columns) => {
|
|
3182
3206
|
return [
|
|
3183
|
-
`${this.$constants(
|
|
3207
|
+
`${this.$constants("UPDATE")}`,
|
|
3184
3208
|
`\`${this.getTableName()}\``,
|
|
3185
|
-
`${this.$constants(
|
|
3186
|
-
`(${columns.join(
|
|
3187
|
-
`${this.$constants(
|
|
3188
|
-
`${this.bindColumn(
|
|
3189
|
-
].join(
|
|
3209
|
+
`${this.$constants("SET")}`,
|
|
3210
|
+
`(${columns.join(", ")})`,
|
|
3211
|
+
`${this.$constants("WHERE")}`,
|
|
3212
|
+
`${this.bindColumn("id")} = '?'`,
|
|
3213
|
+
].join(" ");
|
|
3190
3214
|
};
|
|
3191
3215
|
const schemaTable = yield this.getSchema();
|
|
3192
|
-
const columns = schemaTable.map(column => `${this.bindColumn(column.Field)} = '?'`);
|
|
3216
|
+
const columns = schemaTable.map((column) => `${this.bindColumn(column.Field)} = '?'`);
|
|
3193
3217
|
return makeStatement(columns);
|
|
3194
3218
|
});
|
|
3195
3219
|
}
|
|
@@ -3202,12 +3226,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3202
3226
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3203
3227
|
const makeStatement = () => {
|
|
3204
3228
|
return [
|
|
3205
|
-
`${this.$constants(
|
|
3206
|
-
`${this.$constants(
|
|
3229
|
+
`${this.$constants("DELETE")}`,
|
|
3230
|
+
`${this.$constants("FROM")}`,
|
|
3207
3231
|
`\`${this.getTableName()}\``,
|
|
3208
|
-
`${this.$constants(
|
|
3209
|
-
`${this.bindColumn(
|
|
3210
|
-
].join(
|
|
3232
|
+
`${this.$constants("WHERE")}`,
|
|
3233
|
+
`${this.bindColumn("id")} = \`?\``,
|
|
3234
|
+
].join(" ");
|
|
3211
3235
|
};
|
|
3212
3236
|
return makeStatement();
|
|
3213
3237
|
});
|
|
@@ -3221,13 +3245,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3221
3245
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3222
3246
|
const makeStatement = (columns) => {
|
|
3223
3247
|
return [
|
|
3224
|
-
`${this.$constants(
|
|
3248
|
+
`${this.$constants("CREATE_TABLE_NOT_EXISTS")}`,
|
|
3225
3249
|
`\`${this.getTableName()}\``,
|
|
3226
3250
|
`(`,
|
|
3227
|
-
`\n${columns === null || columns === void 0 ? void 0 : columns.join(
|
|
3251
|
+
`\n${columns === null || columns === void 0 ? void 0 : columns.join(",\n")}`,
|
|
3228
3252
|
`\n)`,
|
|
3229
|
-
`${this.$constants(
|
|
3230
|
-
].join(
|
|
3253
|
+
`${this.$constants("ENGINE")}`,
|
|
3254
|
+
].join(" ");
|
|
3231
3255
|
};
|
|
3232
3256
|
const columns = yield this.showSchema();
|
|
3233
3257
|
return makeStatement(columns);
|
|
@@ -3241,13 +3265,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3241
3265
|
showTables() {
|
|
3242
3266
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3243
3267
|
const sql = [
|
|
3244
|
-
`${this.$constants(
|
|
3245
|
-
`${this.$constants(
|
|
3246
|
-
].join(
|
|
3268
|
+
`${this.$constants("SHOW")}`,
|
|
3269
|
+
`${this.$constants("TABLES")}`,
|
|
3270
|
+
].join(" ");
|
|
3247
3271
|
const results = yield this._queryStatement(sql);
|
|
3248
3272
|
return results
|
|
3249
|
-
.map(table => String(Object.values(table)[0]))
|
|
3250
|
-
.filter(d => d != null || d !==
|
|
3273
|
+
.map((table) => String(Object.values(table)[0]))
|
|
3274
|
+
.filter((d) => d != null || d !== "");
|
|
3251
3275
|
});
|
|
3252
3276
|
}
|
|
3253
3277
|
/**
|
|
@@ -3258,13 +3282,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3258
3282
|
* @returns {Promise<Array>}
|
|
3259
3283
|
*/
|
|
3260
3284
|
showColumns() {
|
|
3261
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3285
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3262
3286
|
const sql = [
|
|
3263
|
-
`${this.$constants(
|
|
3264
|
-
`${this.$constants(
|
|
3265
|
-
`${this.$constants(
|
|
3266
|
-
`\`${table.replace(/\`/g,
|
|
3267
|
-
].join(
|
|
3287
|
+
`${this.$constants("SHOW")}`,
|
|
3288
|
+
`${this.$constants("COLUMNS")}`,
|
|
3289
|
+
`${this.$constants("FROM")}`,
|
|
3290
|
+
`\`${table.replace(/\`/g, "")}\``,
|
|
3291
|
+
].join(" ");
|
|
3268
3292
|
const rawColumns = yield this._queryStatement(sql);
|
|
3269
3293
|
const columns = rawColumns.map((column) => column.Field);
|
|
3270
3294
|
return columns;
|
|
@@ -3277,28 +3301,28 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3277
3301
|
* @returns {Promise<Array>}
|
|
3278
3302
|
*/
|
|
3279
3303
|
showSchema() {
|
|
3280
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3304
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3281
3305
|
const sql = [
|
|
3282
|
-
`${this.$constants(
|
|
3283
|
-
`${this.$constants(
|
|
3284
|
-
`${this.$constants(
|
|
3285
|
-
`\`${table.replace(/\`/g,
|
|
3286
|
-
].join(
|
|
3306
|
+
`${this.$constants("SHOW")}`,
|
|
3307
|
+
`${this.$constants("COLUMNS")}`,
|
|
3308
|
+
`${this.$constants("FROM")}`,
|
|
3309
|
+
`\`${table.replace(/\`/g, "")}\``,
|
|
3310
|
+
].join(" ");
|
|
3287
3311
|
const raws = yield this._queryStatement(sql);
|
|
3288
3312
|
return raws.map((r) => {
|
|
3289
3313
|
const schema = [];
|
|
3290
3314
|
schema.push(`\`${r.Field}\``);
|
|
3291
3315
|
schema.push(`${r.Type}`);
|
|
3292
|
-
if (r.Null ===
|
|
3316
|
+
if (r.Null === "YES") {
|
|
3293
3317
|
schema.push(`NULL`);
|
|
3294
3318
|
}
|
|
3295
|
-
if (r.Null ===
|
|
3319
|
+
if (r.Null === "NO") {
|
|
3296
3320
|
schema.push(`NOT NULL`);
|
|
3297
3321
|
}
|
|
3298
|
-
if (r.Key ===
|
|
3322
|
+
if (r.Key === "PRI") {
|
|
3299
3323
|
schema.push(`PRIMARY KEY`);
|
|
3300
3324
|
}
|
|
3301
|
-
if (r.Key ===
|
|
3325
|
+
if (r.Key === "UNI") {
|
|
3302
3326
|
schema.push(`UNIQUE`);
|
|
3303
3327
|
}
|
|
3304
3328
|
if (r.Default) {
|
|
@@ -3307,7 +3331,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3307
3331
|
if (r.Extra) {
|
|
3308
3332
|
schema.push(`${r.Extra.toUpperCase()}`);
|
|
3309
3333
|
}
|
|
3310
|
-
return schema.join(
|
|
3334
|
+
return schema.join(" ");
|
|
3311
3335
|
});
|
|
3312
3336
|
});
|
|
3313
3337
|
}
|
|
@@ -3318,7 +3342,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3318
3342
|
* @returns {Promise<Array>}
|
|
3319
3343
|
*/
|
|
3320
3344
|
showSchemas() {
|
|
3321
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3345
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3322
3346
|
return this.showSchema(table);
|
|
3323
3347
|
});
|
|
3324
3348
|
}
|
|
@@ -3330,22 +3354,26 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3330
3354
|
* @returns {Promise<Array>}
|
|
3331
3355
|
*/
|
|
3332
3356
|
showValues() {
|
|
3333
|
-
return __awaiter(this, arguments, void 0, function* (table = this.$state.get(
|
|
3357
|
+
return __awaiter(this, arguments, void 0, function* (table = this.$state.get("TABLE_NAME")) {
|
|
3334
3358
|
const sql = [
|
|
3335
|
-
`${this.$constants(
|
|
3336
|
-
|
|
3337
|
-
`${this.$constants(
|
|
3338
|
-
`\`${table.replace(/\`/g,
|
|
3339
|
-
].join(
|
|
3359
|
+
`${this.$constants("SELECT")}`,
|
|
3360
|
+
"*",
|
|
3361
|
+
`${this.$constants("FROM")}`,
|
|
3362
|
+
`\`${table.replace(/\`/g, "")}\``,
|
|
3363
|
+
].join(" ");
|
|
3340
3364
|
const raw = yield this._queryStatement(sql);
|
|
3341
3365
|
const values = raw.map((value) => {
|
|
3342
|
-
return `(${Object.values(value)
|
|
3343
|
-
|
|
3366
|
+
return `(${Object.values(value)
|
|
3367
|
+
.map((v) => {
|
|
3368
|
+
if (this.$utils.typeOf(v) === "date")
|
|
3344
3369
|
return `'${this.$utils.timestamp(v)}'`;
|
|
3345
|
-
if (this.$utils.typeOf(v) ===
|
|
3370
|
+
if (this.$utils.typeOf(v) === "object" &&
|
|
3371
|
+
v != null &&
|
|
3372
|
+
!Array.isArray(v))
|
|
3346
3373
|
return `'${JSON.stringify(v)}'`;
|
|
3347
|
-
return v == null ? this.$constants(
|
|
3348
|
-
})
|
|
3374
|
+
return v == null ? this.$constants("NULL") : `'${v}'`;
|
|
3375
|
+
})
|
|
3376
|
+
.join(", ")})`;
|
|
3349
3377
|
});
|
|
3350
3378
|
return values;
|
|
3351
3379
|
});
|
|
@@ -3360,26 +3388,27 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3360
3388
|
*/
|
|
3361
3389
|
faker(rows, cb) {
|
|
3362
3390
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3363
|
-
if (this.$state.get(
|
|
3391
|
+
if (this.$state.get("TABLE_NAME") === "" ||
|
|
3392
|
+
this.$state.get("TABLE_NAME") == null) {
|
|
3364
3393
|
throw new Error("Unknow this table name");
|
|
3365
3394
|
}
|
|
3366
3395
|
const sql = [
|
|
3367
|
-
`${this.$constants(
|
|
3368
|
-
`${this.$constants(
|
|
3369
|
-
`${this.$constants(
|
|
3370
|
-
`${this.$state.get(
|
|
3371
|
-
].join(
|
|
3396
|
+
`${this.$constants("SHOW")}`,
|
|
3397
|
+
`${this.$constants("FIELDS")}`,
|
|
3398
|
+
`${this.$constants("FROM")}`,
|
|
3399
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3400
|
+
].join(" ");
|
|
3372
3401
|
const fields = yield this._queryStatement(sql);
|
|
3373
3402
|
const fakers = [];
|
|
3374
|
-
const uuid =
|
|
3375
|
-
const passed = (field) => [
|
|
3403
|
+
const uuid = "uuid";
|
|
3404
|
+
const passed = (field) => ["id", "_id"].some((p) => field === p);
|
|
3376
3405
|
for (let row = 0; row < rows; row++) {
|
|
3377
3406
|
let columnAndValue = {};
|
|
3378
3407
|
for (const { Field: field, Type: type } of fields) {
|
|
3379
3408
|
if (passed(field))
|
|
3380
3409
|
continue;
|
|
3381
3410
|
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: field === uuid
|
|
3382
|
-
? this.$utils.faker(
|
|
3411
|
+
? this.$utils.faker("uuid")
|
|
3383
3412
|
: this.$utils.faker(type) });
|
|
3384
3413
|
}
|
|
3385
3414
|
if (cb) {
|
|
@@ -3394,8 +3423,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3394
3423
|
for (const data of chunked) {
|
|
3395
3424
|
promises.push(() => {
|
|
3396
3425
|
return new DB_1.DB(table)
|
|
3397
|
-
.debug(this.$state.get(
|
|
3398
|
-
.createMultiple([...data])
|
|
3426
|
+
.debug(this.$state.get("DEBUG"))
|
|
3427
|
+
.createMultiple([...data])
|
|
3428
|
+
.void()
|
|
3399
3429
|
.save();
|
|
3400
3430
|
});
|
|
3401
3431
|
}
|
|
@@ -3411,17 +3441,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3411
3441
|
* @returns {promise<boolean>}
|
|
3412
3442
|
*/
|
|
3413
3443
|
truncate() {
|
|
3414
|
-
return __awaiter(this, arguments, void 0, function* ({ force = false } = {}) {
|
|
3415
|
-
if (this.$state.get(
|
|
3444
|
+
return __awaiter(this, arguments, void 0, function* ({ force = false, } = {}) {
|
|
3445
|
+
if (this.$state.get("TABLE_NAME") == null ||
|
|
3446
|
+
this.$state.get("TABLE_NAME") === "") {
|
|
3416
3447
|
console.log(`Please set the your table name`);
|
|
3417
3448
|
return false;
|
|
3418
3449
|
}
|
|
3419
3450
|
const sql = [
|
|
3420
|
-
`${this.$constants(
|
|
3421
|
-
`${this.$state.get(
|
|
3422
|
-
].join(
|
|
3451
|
+
`${this.$constants("TRUNCATE_TABLE")}`,
|
|
3452
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3453
|
+
].join(" ");
|
|
3423
3454
|
if (!force) {
|
|
3424
|
-
console.log(`Truncating will delete all data from the table '${this.$state.get(
|
|
3455
|
+
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
3456
|
return false;
|
|
3426
3457
|
}
|
|
3427
3458
|
yield this._queryStatement(sql);
|
|
@@ -3438,16 +3469,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3438
3469
|
*/
|
|
3439
3470
|
drop() {
|
|
3440
3471
|
return __awaiter(this, arguments, void 0, function* ({ force = false } = {}) {
|
|
3441
|
-
if (this.$state.get(
|
|
3472
|
+
if (this.$state.get("TABLE_NAME") == null ||
|
|
3473
|
+
this.$state.get("TABLE_NAME") === "") {
|
|
3442
3474
|
console.log(`Please set the your table name`);
|
|
3443
3475
|
return false;
|
|
3444
3476
|
}
|
|
3445
3477
|
const sql = [
|
|
3446
|
-
`${this.$constants(
|
|
3447
|
-
`${this.$state.get(
|
|
3448
|
-
].join(
|
|
3478
|
+
`${this.$constants("DROP_TABLE")}`,
|
|
3479
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3480
|
+
].join(" ");
|
|
3449
3481
|
if (!force) {
|
|
3450
|
-
console.log(`Droping will drop the table '${this.$state.get(
|
|
3482
|
+
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
3483
|
return false;
|
|
3452
3484
|
}
|
|
3453
3485
|
yield this._queryStatement(sql);
|
|
@@ -3456,171 +3488,186 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3456
3488
|
}
|
|
3457
3489
|
exceptColumns() {
|
|
3458
3490
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3459
|
-
const excepts = this.$state.get(
|
|
3491
|
+
const excepts = this.$state.get("EXCEPTS");
|
|
3460
3492
|
const hasDot = excepts.some((except) => /\./.test(except));
|
|
3461
|
-
const names = excepts
|
|
3493
|
+
const names = excepts
|
|
3494
|
+
.map((except) => {
|
|
3462
3495
|
if (/\./.test(except))
|
|
3463
|
-
return except.split(
|
|
3496
|
+
return except.split(".")[0];
|
|
3464
3497
|
return null;
|
|
3465
|
-
})
|
|
3466
|
-
|
|
3498
|
+
})
|
|
3499
|
+
.filter((d) => d != null);
|
|
3500
|
+
const tableNames = names.length
|
|
3501
|
+
? [...new Set(names)]
|
|
3502
|
+
: [this.$state.get("TABLE_NAME")];
|
|
3467
3503
|
const removeExcepts = [];
|
|
3468
3504
|
for (const tableName of tableNames) {
|
|
3469
3505
|
const sql = [
|
|
3470
|
-
`${this.$constants(
|
|
3471
|
-
`${this.$constants(
|
|
3472
|
-
`${this.$constants(
|
|
3473
|
-
`${tableName}
|
|
3474
|
-
].join(
|
|
3506
|
+
`${this.$constants("SHOW")}`,
|
|
3507
|
+
`${this.$constants("COLUMNS")}`,
|
|
3508
|
+
`${this.$constants("FROM")}`,
|
|
3509
|
+
`${tableName}`,
|
|
3510
|
+
].join(" ");
|
|
3475
3511
|
const rawColumns = yield this._queryStatement(sql);
|
|
3476
3512
|
const columns = rawColumns.map((column) => column.Field);
|
|
3477
3513
|
const removeExcept = columns.filter((column) => {
|
|
3478
3514
|
return excepts.every((except) => {
|
|
3479
3515
|
if (/\./.test(except)) {
|
|
3480
|
-
const [table, _] = except.split(
|
|
3516
|
+
const [table, _] = except.split(".");
|
|
3481
3517
|
return except !== `${table}.${column}`;
|
|
3482
3518
|
}
|
|
3483
3519
|
return except !== column;
|
|
3484
3520
|
});
|
|
3485
3521
|
});
|
|
3486
|
-
removeExcepts.push(hasDot ? removeExcept.map(r => `${tableName}.${r}`) : removeExcept);
|
|
3522
|
+
removeExcepts.push(hasDot ? removeExcept.map((r) => `${tableName}.${r}`) : removeExcept);
|
|
3487
3523
|
}
|
|
3488
3524
|
return removeExcepts.flat();
|
|
3489
3525
|
});
|
|
3490
3526
|
}
|
|
3491
3527
|
_updateHandler(column, value) {
|
|
3492
3528
|
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(
|
|
3529
|
+
this.$constants("CASE"),
|
|
3530
|
+
this.$constants("WHEN"),
|
|
3531
|
+
`(\`${column}\` = "" ${this.$constants("OR")} \`${column}\` ${this.$constants("IS_NULL")})`,
|
|
3532
|
+
this.$constants("THEN"),
|
|
3533
|
+
`"${value !== null && value !== void 0 ? value : ""}" ${this.$constants("ELSE")} \`${column}\``,
|
|
3534
|
+
this.$constants("END"),
|
|
3535
|
+
].join(" "));
|
|
3500
3536
|
}
|
|
3501
3537
|
copyBuilder(instance, options) {
|
|
3502
3538
|
if (!(instance instanceof Builder))
|
|
3503
|
-
throw new Error(
|
|
3539
|
+
throw new Error("Value is not a instanceof Builder");
|
|
3504
3540
|
const copy = Object.fromEntries(instance.$state.get());
|
|
3505
3541
|
const newInstance = new Builder();
|
|
3506
3542
|
newInstance.$state.clone(copy);
|
|
3507
|
-
newInstance.$state.set(
|
|
3508
|
-
newInstance.$state.set(
|
|
3543
|
+
newInstance.$state.set("SAVE", "");
|
|
3544
|
+
newInstance.$state.set("DEBUG", false);
|
|
3509
3545
|
if ((options === null || options === void 0 ? void 0 : options.insert) == null || !options.insert)
|
|
3510
|
-
newInstance.$state.set(
|
|
3546
|
+
newInstance.$state.set("INSERT", "");
|
|
3511
3547
|
if ((options === null || options === void 0 ? void 0 : options.update) == null || !options.update)
|
|
3512
|
-
newInstance.$state.set(
|
|
3548
|
+
newInstance.$state.set("UPDATE", "");
|
|
3513
3549
|
if ((options === null || options === void 0 ? void 0 : options.delete) == null || !options.delete)
|
|
3514
|
-
newInstance.$state.set(
|
|
3550
|
+
newInstance.$state.set("DELETE", "");
|
|
3515
3551
|
if ((options === null || options === void 0 ? void 0 : options.where) == null || !options.where)
|
|
3516
|
-
newInstance.$state.set(
|
|
3552
|
+
newInstance.$state.set("WHERE", []);
|
|
3517
3553
|
if ((options === null || options === void 0 ? void 0 : options.limit) == null || !options.limit)
|
|
3518
|
-
newInstance.$state.set(
|
|
3554
|
+
newInstance.$state.set("LIMIT", "");
|
|
3519
3555
|
if ((options === null || options === void 0 ? void 0 : options.offset) == null || !options.offset)
|
|
3520
|
-
newInstance.$state.set(
|
|
3556
|
+
newInstance.$state.set("OFFSET", "");
|
|
3521
3557
|
if ((options === null || options === void 0 ? void 0 : options.groupBy) == null || !options.groupBy)
|
|
3522
|
-
newInstance.$state.set(
|
|
3558
|
+
newInstance.$state.set("GROUP_BY", "");
|
|
3523
3559
|
if ((options === null || options === void 0 ? void 0 : options.orderBy) == null || !options.orderBy)
|
|
3524
|
-
newInstance.$state.set(
|
|
3560
|
+
newInstance.$state.set("ORDER_BY", []);
|
|
3525
3561
|
if ((options === null || options === void 0 ? void 0 : options.select) == null || !options.select)
|
|
3526
|
-
newInstance.$state.set(
|
|
3562
|
+
newInstance.$state.set("SELECT", []);
|
|
3527
3563
|
if ((options === null || options === void 0 ? void 0 : options.join) == null || !options.join)
|
|
3528
|
-
newInstance.$state.set(
|
|
3564
|
+
newInstance.$state.set("JOIN", []);
|
|
3529
3565
|
if ((options === null || options === void 0 ? void 0 : options.having) == null || !options.having)
|
|
3530
|
-
newInstance.$state.set(
|
|
3566
|
+
newInstance.$state.set("HAVING", "");
|
|
3531
3567
|
return newInstance;
|
|
3532
3568
|
}
|
|
3533
3569
|
_queryBuilder() {
|
|
3534
3570
|
return this._buildQueryStatement();
|
|
3535
3571
|
}
|
|
3536
3572
|
_buildQueryStatement() {
|
|
3537
|
-
const buildSQL = (sql) => sql
|
|
3573
|
+
const buildSQL = (sql) => sql
|
|
3574
|
+
.filter((s) => s !== "" || s == null)
|
|
3575
|
+
.join(" ")
|
|
3576
|
+
.replace(/\s+/g, " ");
|
|
3538
3577
|
const bindJoin = (values) => {
|
|
3539
3578
|
if (!Array.isArray(values) || !values.length)
|
|
3540
3579
|
return null;
|
|
3541
|
-
return values.join(
|
|
3580
|
+
return values.join(" ");
|
|
3542
3581
|
};
|
|
3543
3582
|
const bindWhere = (values) => {
|
|
3544
3583
|
if (!Array.isArray(values) || !values.length)
|
|
3545
3584
|
return null;
|
|
3546
|
-
return `${this.$constants(
|
|
3585
|
+
return `${this.$constants("WHERE")} ${values
|
|
3586
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
3587
|
+
.join(" ")}`;
|
|
3547
3588
|
};
|
|
3548
3589
|
const bindOrderBy = (values) => {
|
|
3549
3590
|
if (!Array.isArray(values) || !values.length)
|
|
3550
3591
|
return null;
|
|
3551
|
-
return `${this.$constants(
|
|
3592
|
+
return `${this.$constants("ORDER_BY")} ${values
|
|
3593
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
3594
|
+
.join(", ")}`;
|
|
3552
3595
|
};
|
|
3553
3596
|
const bindGroupBy = (values) => {
|
|
3554
3597
|
if (!Array.isArray(values) || !values.length)
|
|
3555
3598
|
return null;
|
|
3556
|
-
return `${this.$constants(
|
|
3599
|
+
return `${this.$constants("GROUP_BY")} ${values
|
|
3600
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
3601
|
+
.join(", ")}`;
|
|
3557
3602
|
};
|
|
3558
3603
|
const bindSelect = (values) => {
|
|
3559
3604
|
if (!values.length) {
|
|
3560
|
-
if (!this.$state.get(
|
|
3561
|
-
return `${this.$constants(
|
|
3562
|
-
return `${this.$constants(
|
|
3605
|
+
if (!this.$state.get("DISTINCT"))
|
|
3606
|
+
return `${this.$constants("SELECT")} *`;
|
|
3607
|
+
return `${this.$constants("SELECT")} ${this.$constants("DISTINCT")} *`;
|
|
3563
3608
|
}
|
|
3564
|
-
const findIndex = values.indexOf(
|
|
3609
|
+
const findIndex = values.indexOf("*");
|
|
3565
3610
|
if (findIndex > -1) {
|
|
3566
3611
|
const removed = values.splice(findIndex, 1);
|
|
3567
3612
|
values.unshift(removed[0]);
|
|
3568
3613
|
}
|
|
3569
|
-
return `${this.$constants(
|
|
3614
|
+
return `${this.$constants("SELECT")} ${values.join(", ")}`;
|
|
3570
3615
|
};
|
|
3571
|
-
const bindFrom = ({ from, table, alias, rawAlias }) => {
|
|
3572
|
-
if (alias != null && alias !==
|
|
3573
|
-
if (rawAlias != null && rawAlias !==
|
|
3574
|
-
const raw = String(rawAlias)
|
|
3575
|
-
|
|
3576
|
-
|
|
3616
|
+
const bindFrom = ({ from, table, alias, rawAlias, }) => {
|
|
3617
|
+
if (alias != null && alias !== "") {
|
|
3618
|
+
if (rawAlias != null && rawAlias !== "") {
|
|
3619
|
+
const raw = String(rawAlias)
|
|
3620
|
+
.replace(/^\(\s*|\s*\)$/g, "")
|
|
3621
|
+
.trim();
|
|
3622
|
+
const normalizedRawAlias = raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
3623
|
+
return `${from} (${normalizedRawAlias}) ${this.$constants("AS")} \`${alias}\``;
|
|
3577
3624
|
}
|
|
3578
|
-
return `${from} ${table} ${this.$constants(
|
|
3625
|
+
return `${from} ${table} ${this.$constants("AS")} \`${alias}\``;
|
|
3579
3626
|
}
|
|
3580
3627
|
return `${from} ${table}`;
|
|
3581
3628
|
};
|
|
3582
3629
|
const bindLimit = (limit) => {
|
|
3583
|
-
if (limit ===
|
|
3584
|
-
return
|
|
3585
|
-
return `${this.$constants(
|
|
3630
|
+
if (limit === "" || limit == null)
|
|
3631
|
+
return "";
|
|
3632
|
+
return `${this.$constants("LIMIT")} ${limit}`;
|
|
3586
3633
|
};
|
|
3587
3634
|
const select = () => {
|
|
3588
3635
|
const sql = buildSQL([
|
|
3589
|
-
bindSelect(this.$state.get(
|
|
3636
|
+
bindSelect(this.$state.get("SELECT")),
|
|
3590
3637
|
bindFrom({
|
|
3591
|
-
from: this.$constants(
|
|
3592
|
-
table: this.$state.get(
|
|
3593
|
-
alias: this.$state.get(
|
|
3594
|
-
rawAlias: this.$state.get(
|
|
3638
|
+
from: this.$constants("FROM"),
|
|
3639
|
+
table: this.$state.get("TABLE_NAME"),
|
|
3640
|
+
alias: this.$state.get("ALIAS"),
|
|
3641
|
+
rawAlias: this.$state.get("RAW_ALIAS"),
|
|
3595
3642
|
}),
|
|
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(
|
|
3643
|
+
bindJoin(this.$state.get("JOIN")),
|
|
3644
|
+
bindWhere(this.$state.get("WHERE")),
|
|
3645
|
+
bindGroupBy(this.$state.get("GROUP_BY")),
|
|
3646
|
+
this.$state.get("HAVING"),
|
|
3647
|
+
bindOrderBy(this.$state.get("ORDER_BY")),
|
|
3648
|
+
bindLimit(this.$state.get("LIMIT")),
|
|
3649
|
+
this.$state.get("OFFSET"),
|
|
3603
3650
|
]).trimEnd();
|
|
3604
|
-
if (this.$state.get(
|
|
3605
|
-
return `WITH ${this.$state.get(
|
|
3651
|
+
if (this.$state.get("CTE").length) {
|
|
3652
|
+
return `WITH ${this.$state.get("CTE")} ${sql}`;
|
|
3606
3653
|
}
|
|
3607
3654
|
return sql;
|
|
3608
3655
|
};
|
|
3609
|
-
const insert = () => buildSQL([this.$state.get(
|
|
3656
|
+
const insert = () => buildSQL([this.$state.get("INSERT")]);
|
|
3610
3657
|
const update = () => {
|
|
3611
3658
|
return buildSQL([
|
|
3612
|
-
this.$state.get(
|
|
3613
|
-
bindWhere(this.$state.get(
|
|
3614
|
-
bindOrderBy(this.$state.get(
|
|
3615
|
-
bindLimit(this.$state.get(
|
|
3659
|
+
this.$state.get("UPDATE"),
|
|
3660
|
+
bindWhere(this.$state.get("WHERE")),
|
|
3661
|
+
bindOrderBy(this.$state.get("ORDER_BY")),
|
|
3662
|
+
bindLimit(this.$state.get("LIMIT")),
|
|
3616
3663
|
]);
|
|
3617
3664
|
};
|
|
3618
3665
|
const remove = () => {
|
|
3619
3666
|
return buildSQL([
|
|
3620
|
-
this.$state.get(
|
|
3621
|
-
bindWhere(this.$state.get(
|
|
3622
|
-
bindOrderBy(this.$state.get(
|
|
3623
|
-
bindLimit(this.$state.get(
|
|
3667
|
+
this.$state.get("DELETE"),
|
|
3668
|
+
bindWhere(this.$state.get("WHERE")),
|
|
3669
|
+
bindOrderBy(this.$state.get("ORDER_BY")),
|
|
3670
|
+
bindLimit(this.$state.get("LIMIT")),
|
|
3624
3671
|
]);
|
|
3625
3672
|
};
|
|
3626
3673
|
return {
|
|
@@ -3628,55 +3675,55 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3628
3675
|
insert,
|
|
3629
3676
|
update,
|
|
3630
3677
|
delete: remove,
|
|
3631
|
-
where: () => bindWhere(this.$state.get(
|
|
3678
|
+
where: () => bindWhere(this.$state.get("WHERE")),
|
|
3632
3679
|
any: () => {
|
|
3633
|
-
if (this.$state.get(
|
|
3680
|
+
if (this.$state.get("INSERT"))
|
|
3634
3681
|
return insert();
|
|
3635
|
-
if (this.$state.get(
|
|
3682
|
+
if (this.$state.get("UPDATE"))
|
|
3636
3683
|
return update();
|
|
3637
|
-
if (this.$state.get(
|
|
3684
|
+
if (this.$state.get("DELETE"))
|
|
3638
3685
|
return remove();
|
|
3639
3686
|
return select();
|
|
3640
|
-
}
|
|
3687
|
+
},
|
|
3641
3688
|
};
|
|
3642
3689
|
}
|
|
3643
3690
|
_resultHandler(data) {
|
|
3644
|
-
if (!this.$state.get(
|
|
3645
|
-
this.$state.set(
|
|
3691
|
+
if (!this.$state.get("VOID")) {
|
|
3692
|
+
this.$state.set("RESULT", data);
|
|
3646
3693
|
}
|
|
3647
3694
|
this.$state.reset();
|
|
3648
3695
|
this.$logger.reset();
|
|
3649
3696
|
return data;
|
|
3650
3697
|
}
|
|
3651
3698
|
_resultHandlerExists(data) {
|
|
3652
|
-
if (!this.$state.get(
|
|
3653
|
-
this.$state.set(
|
|
3699
|
+
if (!this.$state.get("VOID")) {
|
|
3700
|
+
this.$state.set("RESULT", data);
|
|
3654
3701
|
}
|
|
3655
3702
|
this.$state.reset();
|
|
3656
3703
|
this.$logger.reset();
|
|
3657
3704
|
return data;
|
|
3658
3705
|
}
|
|
3659
3706
|
whereReference(tableAndLocalKey, tableAndForeignKey) {
|
|
3660
|
-
this.$state.set(
|
|
3661
|
-
...this.$state.get(
|
|
3707
|
+
this.$state.set("WHERE", [
|
|
3708
|
+
...this.$state.get("WHERE"),
|
|
3662
3709
|
[
|
|
3663
|
-
this.$state.get(
|
|
3664
|
-
`${tableAndLocalKey} = ${tableAndForeignKey}
|
|
3665
|
-
].join(
|
|
3710
|
+
this.$state.get("WHERE").length ? `${this.$constants("AND")}` : "",
|
|
3711
|
+
`${tableAndLocalKey} = ${tableAndForeignKey}`,
|
|
3712
|
+
].join(" "),
|
|
3666
3713
|
]);
|
|
3667
3714
|
return this;
|
|
3668
3715
|
}
|
|
3669
3716
|
_queryStatement(sql) {
|
|
3670
3717
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3671
|
-
if (this.$state.get(
|
|
3718
|
+
if (this.$state.get("DEBUG"))
|
|
3672
3719
|
this.$utils.consoleDebug(sql);
|
|
3673
3720
|
const result = yield this.$pool.query(sql);
|
|
3674
3721
|
return result;
|
|
3675
3722
|
});
|
|
3676
3723
|
}
|
|
3677
3724
|
_actionStatement(_a) {
|
|
3678
|
-
return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false }) {
|
|
3679
|
-
if (this.$state.get(
|
|
3725
|
+
return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false, }) {
|
|
3726
|
+
if (this.$state.get("DEBUG"))
|
|
3680
3727
|
this.$utils.consoleDebug(sql);
|
|
3681
3728
|
if (returnId) {
|
|
3682
3729
|
const result = yield this.$pool.query(sql);
|
|
@@ -3688,111 +3735,113 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3688
3735
|
}
|
|
3689
3736
|
_insertNotExists() {
|
|
3690
3737
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3691
|
-
if (!this.$state.get(
|
|
3738
|
+
if (!this.$state.get("WHERE").length)
|
|
3692
3739
|
throw new Error("Can't insert not exists without where condition");
|
|
3693
3740
|
let sql = [
|
|
3694
|
-
`${this.$constants(
|
|
3695
|
-
`${this.$constants(
|
|
3741
|
+
`${this.$constants("SELECT")}`,
|
|
3742
|
+
`${this.$constants("EXISTS")}(${this.$constants("SELECT")}`,
|
|
3696
3743
|
`*`,
|
|
3697
|
-
`${this.$constants(
|
|
3698
|
-
`${this.$state.get(
|
|
3744
|
+
`${this.$constants("FROM")}`,
|
|
3745
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3699
3746
|
`${this._queryBuilder().where()}`,
|
|
3700
|
-
`${this.$constants(
|
|
3701
|
-
`${this.$constants(
|
|
3702
|
-
].join(
|
|
3747
|
+
`${this.$constants("LIMIT")} 1)`,
|
|
3748
|
+
`${this.$constants("AS")} 'exists'`,
|
|
3749
|
+
].join(" ");
|
|
3703
3750
|
const [{ exists: result }] = yield this._queryStatement(sql);
|
|
3704
3751
|
const check = !!Number.parseInt(result);
|
|
3705
3752
|
switch (check) {
|
|
3706
3753
|
case false: {
|
|
3707
3754
|
const [result, id] = yield this._actionStatement({
|
|
3708
|
-
sql: this.$state.get(
|
|
3709
|
-
returnId: true
|
|
3755
|
+
sql: this.$state.get("INSERT"),
|
|
3756
|
+
returnId: true,
|
|
3710
3757
|
});
|
|
3711
|
-
if (this.$state.get(
|
|
3758
|
+
if (this.$state.get("VOID") || !result)
|
|
3712
3759
|
return this._resultHandler(undefined);
|
|
3713
|
-
yield this.$utils.wait(this.$state.get(
|
|
3760
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3714
3761
|
const data = yield new Builder()
|
|
3715
3762
|
.copyBuilder(this, { select: true })
|
|
3716
|
-
.where(
|
|
3763
|
+
.where("id", id)
|
|
3717
3764
|
.first();
|
|
3718
3765
|
return this._resultHandler(data);
|
|
3719
3766
|
}
|
|
3720
|
-
default:
|
|
3767
|
+
default:
|
|
3768
|
+
return this._resultHandler(null);
|
|
3721
3769
|
}
|
|
3722
3770
|
});
|
|
3723
3771
|
}
|
|
3724
3772
|
_insert() {
|
|
3725
3773
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3726
3774
|
const [result, id] = yield this._actionStatement({
|
|
3727
|
-
sql: this.$state.get(
|
|
3728
|
-
returnId: true
|
|
3775
|
+
sql: this.$state.get("INSERT"),
|
|
3776
|
+
returnId: true,
|
|
3729
3777
|
});
|
|
3730
|
-
if (this.$state.get(
|
|
3778
|
+
if (this.$state.get("VOID") || !result)
|
|
3731
3779
|
return this._resultHandler(undefined);
|
|
3732
|
-
yield this.$utils.wait(this.$state.get(
|
|
3780
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3733
3781
|
const results = yield new Builder()
|
|
3734
3782
|
.copyBuilder(this, { select: true })
|
|
3735
|
-
.where(
|
|
3783
|
+
.where("id", id)
|
|
3736
3784
|
.first();
|
|
3737
3785
|
return this._resultHandler(results);
|
|
3738
3786
|
});
|
|
3739
3787
|
}
|
|
3740
3788
|
_checkValueHasRaw(value) {
|
|
3741
|
-
const detectedValue = typeof value ===
|
|
3742
|
-
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants(
|
|
3789
|
+
const detectedValue = typeof value === "string" && value.includes(this.$constants("RAW"))
|
|
3790
|
+
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants("RAW"), "")
|
|
3743
3791
|
: `'${this.$utils.covertBooleanToNumber(value)}'`;
|
|
3744
3792
|
return detectedValue;
|
|
3745
3793
|
}
|
|
3746
3794
|
_checkValueHasOp(str) {
|
|
3747
3795
|
var _a;
|
|
3748
|
-
if (typeof str !==
|
|
3796
|
+
if (typeof str !== "string")
|
|
3749
3797
|
str = String(str);
|
|
3750
|
-
if (!str.includes(this.$constants(
|
|
3798
|
+
if (!str.includes(this.$constants("OP")) ||
|
|
3799
|
+
!str.includes(this.$constants("VALUE"))) {
|
|
3751
3800
|
return null;
|
|
3752
3801
|
}
|
|
3753
|
-
const opRegex = new RegExp(`\\${this.$constants(
|
|
3754
|
-
const valueRegex = new RegExp(`\\${this.$constants(
|
|
3802
|
+
const opRegex = new RegExp(`\\${this.$constants("OP")}\\(([^)]+)\\)`);
|
|
3803
|
+
const valueRegex = new RegExp(`\\${this.$constants("VALUE")}\\(([^)]+)\\)`);
|
|
3755
3804
|
const opMatch = str.match(opRegex);
|
|
3756
3805
|
const valueMatch = str.match(valueRegex);
|
|
3757
|
-
const op = opMatch ? opMatch[1] :
|
|
3758
|
-
const value = valueMatch ? valueMatch[1] :
|
|
3806
|
+
const op = opMatch ? opMatch[1] : "";
|
|
3807
|
+
const value = valueMatch ? valueMatch[1] : "";
|
|
3759
3808
|
return {
|
|
3760
|
-
op: op.replace(this.$constants(
|
|
3761
|
-
value: (_a = value === null || value === void 0 ? void 0 : value.replace(this.$constants(
|
|
3809
|
+
op: op.replace(this.$constants("OP"), ""),
|
|
3810
|
+
value: (_a = value === null || value === void 0 ? void 0 : value.replace(this.$constants("VALUE"), "")) !== null && _a !== void 0 ? _a : "",
|
|
3762
3811
|
};
|
|
3763
3812
|
}
|
|
3764
3813
|
_insertMultiple() {
|
|
3765
3814
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3766
3815
|
const [result, id] = yield this._actionStatement({
|
|
3767
3816
|
sql: this._queryBuilder().insert(),
|
|
3768
|
-
returnId: true
|
|
3817
|
+
returnId: true,
|
|
3769
3818
|
});
|
|
3770
|
-
if (this.$state.get(
|
|
3819
|
+
if (this.$state.get("VOID") || !result)
|
|
3771
3820
|
return this._resultHandler(undefined);
|
|
3772
3821
|
const arrayId = [...Array(result)].map((_, i) => i + id);
|
|
3773
|
-
yield this.$utils.wait(this.$state.get(
|
|
3822
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3774
3823
|
const resultData = yield new Builder()
|
|
3775
3824
|
.copyBuilder(this, { select: true, limit: true })
|
|
3776
|
-
.whereIn(
|
|
3825
|
+
.whereIn("id", arrayId)
|
|
3777
3826
|
.get();
|
|
3778
3827
|
return this._resultHandler(resultData);
|
|
3779
3828
|
});
|
|
3780
3829
|
}
|
|
3781
3830
|
_insertOrSelect() {
|
|
3782
3831
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3783
|
-
if (!this.$state.get(
|
|
3832
|
+
if (!this.$state.get("WHERE").length) {
|
|
3784
3833
|
throw new Error("Can't create or select without where condition");
|
|
3785
3834
|
}
|
|
3786
3835
|
let sql = [
|
|
3787
|
-
`${this.$constants(
|
|
3788
|
-
`${this.$constants(
|
|
3836
|
+
`${this.$constants("SELECT")}`,
|
|
3837
|
+
`${this.$constants("EXISTS")}(${this.$constants("SELECT")}`,
|
|
3789
3838
|
`1`,
|
|
3790
|
-
`${this.$constants(
|
|
3791
|
-
`${this.$state.get(
|
|
3839
|
+
`${this.$constants("FROM")}`,
|
|
3840
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3792
3841
|
`${this._queryBuilder().where()}`,
|
|
3793
|
-
`${this.$constants(
|
|
3794
|
-
`${this.$constants(
|
|
3795
|
-
].join(
|
|
3842
|
+
`${this.$constants("LIMIT")} 1)`,
|
|
3843
|
+
`${this.$constants("AS")} 'exists'`,
|
|
3844
|
+
].join(" ");
|
|
3796
3845
|
let check = false;
|
|
3797
3846
|
const [{ exists: result }] = yield this._queryStatement(sql);
|
|
3798
3847
|
check = !!parseInt(result);
|
|
@@ -3800,16 +3849,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3800
3849
|
case false: {
|
|
3801
3850
|
const [result, id] = yield this._actionStatement({
|
|
3802
3851
|
sql: this._queryBuilder().insert(),
|
|
3803
|
-
returnId: true
|
|
3852
|
+
returnId: true,
|
|
3804
3853
|
});
|
|
3805
|
-
if (this.$state.get(
|
|
3854
|
+
if (this.$state.get("VOID") || !result)
|
|
3806
3855
|
return this._resultHandler(undefined);
|
|
3807
|
-
yield this.$utils.wait(this.$state.get(
|
|
3856
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3808
3857
|
const data = yield new Builder()
|
|
3809
3858
|
.copyBuilder(this, { select: true })
|
|
3810
|
-
.where(
|
|
3859
|
+
.where("id", id)
|
|
3811
3860
|
.first();
|
|
3812
|
-
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action:
|
|
3861
|
+
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action: "insert" });
|
|
3813
3862
|
return this._resultHandler(resultData);
|
|
3814
3863
|
}
|
|
3815
3864
|
case true: {
|
|
@@ -3819,11 +3868,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3819
3868
|
const data = yield this._queryStatement(sql);
|
|
3820
3869
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
3821
3870
|
for (const val of data) {
|
|
3822
|
-
val.$action =
|
|
3871
|
+
val.$action = "select";
|
|
3823
3872
|
}
|
|
3824
3873
|
return this._resultHandler(data || []);
|
|
3825
3874
|
}
|
|
3826
|
-
const resultData = Object.assign(Object.assign({}, data[0]), { $action:
|
|
3875
|
+
const resultData = Object.assign(Object.assign({}, data[0]), { $action: "select" });
|
|
3827
3876
|
return this._resultHandler(resultData);
|
|
3828
3877
|
}
|
|
3829
3878
|
default: {
|
|
@@ -3834,19 +3883,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3834
3883
|
}
|
|
3835
3884
|
_updateOrInsert() {
|
|
3836
3885
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3837
|
-
if (!this.$state.get(
|
|
3886
|
+
if (!this.$state.get("WHERE").length) {
|
|
3838
3887
|
throw new Error("Can't update or insert without where condition");
|
|
3839
3888
|
}
|
|
3840
3889
|
let sql = [
|
|
3841
|
-
`${this.$constants(
|
|
3842
|
-
`${this.$constants(
|
|
3890
|
+
`${this.$constants("SELECT")}`,
|
|
3891
|
+
`${this.$constants("EXISTS")}(${this.$constants("SELECT")}`,
|
|
3843
3892
|
`1`,
|
|
3844
|
-
`${this.$constants(
|
|
3845
|
-
`${this.$state.get(
|
|
3893
|
+
`${this.$constants("FROM")}`,
|
|
3894
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
3846
3895
|
`${this._queryBuilder().where()}`,
|
|
3847
|
-
`${this.$constants(
|
|
3848
|
-
`${this.$constants(
|
|
3849
|
-
].join(
|
|
3896
|
+
`${this.$constants("LIMIT")} 1)`,
|
|
3897
|
+
`${this.$constants("AS")} 'exists'`,
|
|
3898
|
+
].join(" ");
|
|
3850
3899
|
let check = false;
|
|
3851
3900
|
const [{ exists: result }] = yield this._queryStatement(sql);
|
|
3852
3901
|
check = !!parseInt(result);
|
|
@@ -3854,33 +3903,35 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3854
3903
|
case false: {
|
|
3855
3904
|
const [result, id] = yield this._actionStatement({
|
|
3856
3905
|
sql: this._queryBuilder().insert(),
|
|
3857
|
-
returnId: true
|
|
3906
|
+
returnId: true,
|
|
3858
3907
|
});
|
|
3859
|
-
if (this.$state.get(
|
|
3908
|
+
if (this.$state.get("VOID") || !result)
|
|
3860
3909
|
return this._resultHandler(undefined);
|
|
3861
|
-
yield this.$utils.wait(this.$state.get(
|
|
3910
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3862
3911
|
const data = yield new Builder()
|
|
3863
3912
|
.copyBuilder(this, { select: true })
|
|
3864
|
-
.where(
|
|
3913
|
+
.where("id", id)
|
|
3865
3914
|
.first();
|
|
3866
|
-
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action:
|
|
3915
|
+
const resultData = data == null ? null : Object.assign(Object.assign({}, data), { $action: "insert" });
|
|
3867
3916
|
return this._resultHandler(resultData);
|
|
3868
3917
|
}
|
|
3869
3918
|
case true: {
|
|
3870
3919
|
const result = yield this._actionStatement({
|
|
3871
|
-
sql: this._queryBuilder().update()
|
|
3920
|
+
sql: this._queryBuilder().update(),
|
|
3872
3921
|
});
|
|
3873
|
-
if (this.$state.get(
|
|
3922
|
+
if (this.$state.get("VOID") || !result)
|
|
3874
3923
|
return this._resultHandler(null);
|
|
3875
|
-
yield this.$utils.wait(this.$state.get(
|
|
3876
|
-
const data = yield this._queryStatement(new Builder()
|
|
3924
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3925
|
+
const data = yield this._queryStatement(new Builder()
|
|
3926
|
+
.copyBuilder(this, { select: true, where: true })
|
|
3927
|
+
.toString());
|
|
3877
3928
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
3878
3929
|
for (const val of data) {
|
|
3879
|
-
val.$action =
|
|
3930
|
+
val.$action = "update";
|
|
3880
3931
|
}
|
|
3881
3932
|
return this._resultHandler(data || []);
|
|
3882
3933
|
}
|
|
3883
|
-
const resultData = Object.assign(Object.assign({}, data[0]), { $action:
|
|
3934
|
+
const resultData = Object.assign(Object.assign({}, data[0]), { $action: "update" });
|
|
3884
3935
|
return this._resultHandler(resultData);
|
|
3885
3936
|
}
|
|
3886
3937
|
default: {
|
|
@@ -3891,14 +3942,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3891
3942
|
}
|
|
3892
3943
|
_update() {
|
|
3893
3944
|
return __awaiter(this, arguments, void 0, function* (ignoreWhere = false) {
|
|
3894
|
-
if (!this.$state.get(
|
|
3945
|
+
if (!this.$state.get("WHERE").length && !ignoreWhere)
|
|
3895
3946
|
throw new Error("can't update without where condition");
|
|
3896
3947
|
const result = yield this._actionStatement({
|
|
3897
|
-
sql: this._queryBuilder().update()
|
|
3948
|
+
sql: this._queryBuilder().update(),
|
|
3898
3949
|
});
|
|
3899
|
-
if (this.$state.get(
|
|
3950
|
+
if (this.$state.get("VOID") || !result)
|
|
3900
3951
|
return this._resultHandler(undefined);
|
|
3901
|
-
yield this.$utils.wait(this.$state.get(
|
|
3952
|
+
yield this.$utils.wait(this.$state.get("AFTER_SAVE"));
|
|
3902
3953
|
const sql = this._queryBuilder().select();
|
|
3903
3954
|
const data = yield this._queryStatement(sql);
|
|
3904
3955
|
if ((data === null || data === void 0 ? void 0 : data.length) > 1)
|
|
@@ -3909,7 +3960,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3909
3960
|
}
|
|
3910
3961
|
_hiddenColumn(data) {
|
|
3911
3962
|
var _a;
|
|
3912
|
-
const hidden = this.$state.get(
|
|
3963
|
+
const hidden = this.$state.get("HIDDEN");
|
|
3913
3964
|
if ((_a = Object.keys(data)) === null || _a === void 0 ? void 0 : _a.length) {
|
|
3914
3965
|
hidden.forEach((column) => {
|
|
3915
3966
|
data.forEach((objColumn) => {
|
|
@@ -3922,31 +3973,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3922
3973
|
_queryUpdate(data) {
|
|
3923
3974
|
this.$utils.covertDateToDateString(data);
|
|
3924
3975
|
const values = Object.entries(data).map(([column, value]) => {
|
|
3925
|
-
if (typeof value ===
|
|
3976
|
+
if (typeof value === "string" &&
|
|
3977
|
+
!value.includes(this.$constants("RAW"))) {
|
|
3926
3978
|
value = this.$utils.escapeActions(value);
|
|
3927
3979
|
}
|
|
3928
|
-
return `${this.bindColumn(column)} = ${value == null || value === this.$constants(
|
|
3929
|
-
? this.$constants(
|
|
3980
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants("NULL")
|
|
3981
|
+
? this.$constants("NULL")
|
|
3930
3982
|
: this._checkValueHasRaw(value)}`;
|
|
3931
3983
|
});
|
|
3932
|
-
return `${this.$constants(
|
|
3984
|
+
return `${this.$constants("SET")} ${values}`;
|
|
3933
3985
|
}
|
|
3934
3986
|
_queryInsert(data) {
|
|
3935
3987
|
data = this.$utils.covertDateToDateString(data);
|
|
3936
3988
|
const columns = Object.keys(data).map((column) => this.bindColumn(column));
|
|
3937
3989
|
const values = Object.values(data).map((value) => {
|
|
3938
|
-
if (typeof value ===
|
|
3990
|
+
if (typeof value === "string" &&
|
|
3991
|
+
!value.includes(this.$constants("RAW"))) {
|
|
3939
3992
|
value = this.$utils.escapeActions(value);
|
|
3940
3993
|
}
|
|
3941
|
-
return `${value == null || value === this.$constants(
|
|
3942
|
-
? this.$constants(
|
|
3994
|
+
return `${value == null || value === this.$constants("NULL")
|
|
3995
|
+
? this.$constants("NULL")
|
|
3943
3996
|
: this._checkValueHasRaw(value)}`;
|
|
3944
3997
|
});
|
|
3945
|
-
return [
|
|
3946
|
-
`(${columns})`,
|
|
3947
|
-
`${this.$constants('VALUES')}`,
|
|
3948
|
-
`(${values})`
|
|
3949
|
-
].join(' ');
|
|
3998
|
+
return [`(${columns})`, `${this.$constants("VALUES")}`, `(${values})`].join(" ");
|
|
3950
3999
|
}
|
|
3951
4000
|
_queryInsertMultiple(data) {
|
|
3952
4001
|
var _a;
|
|
@@ -3954,79 +4003,80 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3954
4003
|
for (let objects of data) {
|
|
3955
4004
|
this.$utils.covertDateToDateString(objects);
|
|
3956
4005
|
const vals = Object.values(objects).map((value) => {
|
|
3957
|
-
if (typeof value ===
|
|
4006
|
+
if (typeof value === "string" &&
|
|
4007
|
+
!value.includes(this.$constants("RAW"))) {
|
|
3958
4008
|
value = this.$utils.escapeActions(value);
|
|
3959
4009
|
}
|
|
3960
|
-
return `${value == null || value === this.$constants(
|
|
3961
|
-
? this.$constants(
|
|
4010
|
+
return `${value == null || value === this.$constants("NULL")
|
|
4011
|
+
? this.$constants("NULL")
|
|
3962
4012
|
: this._checkValueHasRaw(value)}`;
|
|
3963
4013
|
});
|
|
3964
|
-
values.push(`(${vals.join(
|
|
4014
|
+
values.push(`(${vals.join(",")})`);
|
|
3965
4015
|
}
|
|
3966
4016
|
const columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => this.bindColumn(column));
|
|
3967
4017
|
return [
|
|
3968
4018
|
`(${columns})`,
|
|
3969
|
-
`${this.$constants(
|
|
3970
|
-
`${values.join(
|
|
3971
|
-
].join(
|
|
4019
|
+
`${this.$constants("VALUES")}`,
|
|
4020
|
+
`${values.join(",")}`,
|
|
4021
|
+
].join(" ");
|
|
3972
4022
|
}
|
|
3973
4023
|
_valueAndOperator(value, operator, useDefault = false) {
|
|
3974
4024
|
if (useDefault)
|
|
3975
|
-
return [operator,
|
|
4025
|
+
return [operator, "="];
|
|
3976
4026
|
if (operator == null) {
|
|
3977
|
-
return [[],
|
|
4027
|
+
return [[], "="];
|
|
3978
4028
|
}
|
|
3979
|
-
if (operator.toUpperCase() === this.$constants(
|
|
4029
|
+
if (operator.toUpperCase() === this.$constants("LIKE")) {
|
|
3980
4030
|
operator = operator.toUpperCase();
|
|
3981
4031
|
}
|
|
3982
4032
|
return [value, operator];
|
|
3983
4033
|
}
|
|
3984
|
-
_handleJoin(type =
|
|
4034
|
+
_handleJoin(type = "INNER_JOIN", localKey, referenceKey) {
|
|
3985
4035
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
3986
|
-
if (typeof localKey ===
|
|
4036
|
+
if (typeof localKey === "function") {
|
|
3987
4037
|
const callback = localKey(new Join_1.Join(this, type));
|
|
3988
|
-
this.$state.set(
|
|
3989
|
-
...this.$state.get(
|
|
3990
|
-
callback[
|
|
4038
|
+
this.$state.set("JOIN", [
|
|
4039
|
+
...this.$state.get("JOIN"),
|
|
4040
|
+
callback["toString"](),
|
|
3991
4041
|
]);
|
|
3992
4042
|
return this;
|
|
3993
4043
|
}
|
|
3994
|
-
let table = (_a = referenceKey === null || referenceKey === void 0 ? void 0 : referenceKey.split(
|
|
4044
|
+
let table = (_a = referenceKey === null || referenceKey === void 0 ? void 0 : referenceKey.split(".")) === null || _a === void 0 ? void 0 : _a.shift();
|
|
3995
4045
|
const aliasRef = /\|/.test(String(table));
|
|
3996
4046
|
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(
|
|
4047
|
+
const tableRef = (_b = table === null || table === void 0 ? void 0 : table.split("|")) === null || _b === void 0 ? void 0 : _b.shift();
|
|
4048
|
+
table = `\`${tableRef}\` ${this.$constants("AS")} \`${(_c = table === null || table === void 0 ? void 0 : table.split("|")) === null || _c === void 0 ? void 0 : _c.pop()}\``;
|
|
4049
|
+
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
4050
|
}
|
|
4001
4051
|
const alias = /\|/.test(String(localKey));
|
|
4002
4052
|
if (alias) {
|
|
4003
|
-
localKey = String((_g = (_f = localKey === null || localKey === void 0 ? void 0 : localKey.split(
|
|
4053
|
+
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
4054
|
}
|
|
4005
|
-
this.$state.set(
|
|
4006
|
-
...this.$state.get(
|
|
4055
|
+
this.$state.set("JOIN", [
|
|
4056
|
+
...this.$state.get("JOIN"),
|
|
4007
4057
|
[
|
|
4008
4058
|
`${this.$constants(type)}`,
|
|
4009
|
-
aliasRef ? `${table}` : `\`${table}\``,
|
|
4010
|
-
`${this.$constants(
|
|
4011
|
-
`${this.bindColumn(localKey)} = ${this.bindColumn(String(referenceKey))}
|
|
4012
|
-
].join(
|
|
4059
|
+
aliasRef ? `${table}` : (table === null || table === void 0 ? void 0 : table.includes('`')) ? `${table}` : `\`${table}\``,
|
|
4060
|
+
`${this.$constants("ON")}`,
|
|
4061
|
+
`${this.bindColumn(localKey)} = ${this.bindColumn(String(referenceKey))}`,
|
|
4062
|
+
].join(" "),
|
|
4013
4063
|
]);
|
|
4014
4064
|
return this;
|
|
4015
4065
|
}
|
|
4016
4066
|
_initialConnection() {
|
|
4017
4067
|
this.$utils = utils_1.utils;
|
|
4018
4068
|
this.$pool = (() => {
|
|
4019
|
-
let pool =
|
|
4069
|
+
let pool = Pool_1.Pool;
|
|
4020
4070
|
return {
|
|
4021
4071
|
query: (sql) => __awaiter(this, void 0, void 0, function* () { return yield pool.query(sql); }),
|
|
4022
4072
|
get: () => pool,
|
|
4023
4073
|
set: (newConnection) => {
|
|
4024
4074
|
pool = newConnection;
|
|
4025
4075
|
return;
|
|
4026
|
-
}
|
|
4076
|
+
},
|
|
4027
4077
|
};
|
|
4028
4078
|
})();
|
|
4029
|
-
this.$state = new State_1.StateHandler(
|
|
4079
|
+
this.$state = new State_1.StateHandler("default");
|
|
4030
4080
|
this.$logger = (() => {
|
|
4031
4081
|
let logger = [];
|
|
4032
4082
|
return {
|
|
@@ -4039,7 +4089,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
4039
4089
|
logger = [];
|
|
4040
4090
|
return;
|
|
4041
4091
|
},
|
|
4042
|
-
check: (data) => logger.indexOf(data) != -1
|
|
4092
|
+
check: (data) => logger.indexOf(data) != -1,
|
|
4043
4093
|
};
|
|
4044
4094
|
})();
|
|
4045
4095
|
this.$constants = (name) => {
|