tspace-mysql 1.5.0 → 1.5.2

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.
Files changed (53) hide show
  1. package/README.md +1856 -701
  2. package/build/cli/dump/db.js +3 -2
  3. package/build/cli/generate/make.js +17 -15
  4. package/build/cli/generate/modelDecorator.d.ts +1 -1
  5. package/build/cli/generate/modelDecorator.js +2 -3
  6. package/build/cli/index.js +45 -27
  7. package/build/cli/migrate/make.d.ts +1 -1
  8. package/build/cli/migrate/make.js +2 -2
  9. package/build/cli/migrations/make-db.d.ts +4 -0
  10. package/build/cli/migrations/make-db.js +58 -0
  11. package/build/cli/migrations/make.d.ts +2 -0
  12. package/build/cli/migrations/make.js +201 -0
  13. package/build/lib/Interface.d.ts +24 -5
  14. package/build/lib/connection/index.d.ts +5 -1
  15. package/build/lib/connection/index.js +65 -8
  16. package/build/lib/connection/options.js +2 -2
  17. package/build/lib/constants/index.js +13 -3
  18. package/build/lib/{tspace → core}/Abstracts/AbstractBuilder.d.ts +1 -1
  19. package/build/lib/{tspace → core}/Abstracts/AbstractModel.d.ts +15 -13
  20. package/build/lib/{tspace → core}/Blueprint.d.ts +14 -3
  21. package/build/lib/{tspace → core}/Blueprint.js +30 -4
  22. package/build/lib/{tspace → core}/Builder.d.ts +140 -44
  23. package/build/lib/{tspace → core}/Builder.js +772 -459
  24. package/build/lib/{tspace → core}/DB.d.ts +20 -4
  25. package/build/lib/{tspace → core}/DB.js +73 -39
  26. package/build/lib/{tspace → core}/Decorator.js +2 -2
  27. package/build/lib/{tspace → core/Handlers}/Logger.d.ts +3 -3
  28. package/build/lib/{tspace → core/Handlers}/Logger.js +4 -4
  29. package/build/lib/{tspace → core}/Handlers/Proxy.js +2 -2
  30. package/build/lib/{tspace → core}/Handlers/Relation.d.ts +2 -4
  31. package/build/lib/{tspace → core}/Handlers/Relation.js +69 -48
  32. package/build/lib/{tspace → core}/Handlers/State.d.ts +1 -1
  33. package/build/lib/{tspace → core}/Handlers/State.js +3 -3
  34. package/build/lib/{tspace → core}/Model.d.ts +358 -133
  35. package/build/lib/{tspace → core}/Model.js +1316 -558
  36. package/build/lib/core/Schema.d.ts +137 -0
  37. package/build/lib/{tspace → core}/Schema.js +147 -52
  38. package/build/lib/core/Type.d.ts +6 -0
  39. package/build/lib/core/Type.js +2 -0
  40. package/build/lib/{tspace → core}/index.d.ts +2 -1
  41. package/build/lib/{tspace → core}/index.js +3 -2
  42. package/build/lib/index.d.ts +2 -2
  43. package/build/lib/index.js +2 -2
  44. package/build/lib/utils/index.d.ts +1 -0
  45. package/build/lib/utils/index.js +17 -7
  46. package/package.json +6 -4
  47. package/build/lib/tspace/Schema.d.ts +0 -70
  48. /package/build/lib/{tspace → core}/Abstracts/AbstractBuilder.js +0 -0
  49. /package/build/lib/{tspace → core}/Abstracts/AbstractDB.d.ts +0 -0
  50. /package/build/lib/{tspace → core}/Abstracts/AbstractDB.js +0 -0
  51. /package/build/lib/{tspace → core}/Abstracts/AbstractModel.js +0 -0
  52. /package/build/lib/{tspace → core}/Decorator.d.ts +0 -0
  53. /package/build/lib/{tspace → core}/Handlers/Proxy.d.ts +0 -0
@@ -39,7 +39,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
39
39
  * @return {this} this
40
40
  */
41
41
  distinct() {
42
- this._setState('DISTINCT', true);
42
+ this.$state.set('DISTINCT', true);
43
43
  return this;
44
44
  }
45
45
  /**
@@ -50,8 +50,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
50
50
  * @return {this} this
51
51
  */
52
52
  select(...columns) {
53
- if (!columns.length)
53
+ if (!columns.length) {
54
+ this.$state.set('SELECT', ['*']);
54
55
  return this;
56
+ }
55
57
  let select = columns.map((column) => {
56
58
  if (column === '*' || (column.includes('*') && /\./.test(column)))
57
59
  return column;
@@ -59,13 +61,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
59
61
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
60
62
  return this.bindColumn(column);
61
63
  });
62
- select = [...this._getState('SELECT'), ...select];
63
- if (this._getState('DISTINCT') && select.length) {
64
+ select = [...this.$state.get('SELECT'), ...select];
65
+ if (this.$state.get('DISTINCT') && select.length) {
64
66
  select[0] = String(select[0]).includes(this.$constants('DISTINCT'))
65
67
  ? select[0]
66
68
  : `${this.$constants('DISTINCT')} ${select[0]}`;
67
69
  }
68
- this._setState('SELECT', select);
70
+ this.$state.set('SELECT', select);
69
71
  return this;
70
72
  }
71
73
  /**
@@ -89,11 +91,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
89
91
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
90
92
  return column;
91
93
  });
92
- if (this._getState('DISTINCT') && select.length) {
93
- this._setState('SELECT', select);
94
+ if (this.$state.get('DISTINCT') && select.length) {
95
+ this.$state.set('SELECT', select);
94
96
  return this;
95
97
  }
96
- this._setState('SELECT', [...this._getState('SELECT'), ...select]);
98
+ this.$state.set('SELECT', [...this.$state.get('SELECT'), ...select]);
97
99
  return this;
98
100
  }
99
101
  /**
@@ -117,7 +119,36 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
117
119
  maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
118
120
  }
119
121
  const json = `${this.$constants('JSON_OBJECT')}(${maping.join(' , ')}) ${this.$constants('AS')} \`${alias}\``;
120
- this._setState('SELECT', [...this._getState('SELECT'), json]);
122
+ this.$state.set('SELECT', [...this.$state.get('SELECT'), json]);
123
+ return this;
124
+ }
125
+ /**
126
+ * The 'sleep' method is used to delay the query.
127
+ *
128
+ * @param {number} second - The number of seconds to sleep
129
+ * @return {this} this
130
+ */
131
+ sleep(second) {
132
+ const sql = `SELECT SLEEP(${second}) as delay`;
133
+ this.$state.set('JOIN', [
134
+ ...this.$state.get('JOIN'),
135
+ [
136
+ `${this.$constants('INNER_JOIN')}`,
137
+ `(${sql}) ${this.$constants('AS')} temp`,
138
+ `${this.$constants('ON')}`,
139
+ `1=1`
140
+ ].join(' ')
141
+ ]);
142
+ return this;
143
+ }
144
+ /**
145
+ * The 'returnType' method is used covert the results to type 'object' or 'array'.
146
+ *
147
+ * @param {string} type - The types 'object' | 'array'
148
+ * @return {this} this
149
+ */
150
+ returnType(type) {
151
+ this.$state.set('RETURN_TYPE', type);
121
152
  return this;
122
153
  }
123
154
  /**
@@ -129,7 +160,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
129
160
  * @return {this}
130
161
  */
131
162
  pluck(column) {
132
- this._setState('PLUCK', column);
163
+ this.$state.set('PLUCK', column);
133
164
  return this;
134
165
  }
135
166
  /**
@@ -140,7 +171,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
140
171
  * @return {this} this
141
172
  */
142
173
  except(...columns) {
143
- this._setState('EXCEPTS', columns.length ? columns : []);
174
+ if (!columns.length)
175
+ return this;
176
+ const exceptColumns = this.$state.get('EXCEPTS');
177
+ this.$state.set('EXCEPTS', [
178
+ ...columns,
179
+ ...exceptColumns
180
+ ]);
181
+ return this;
182
+ }
183
+ /**
184
+ * The 'exceptTimestamp' method is used to timestamp columns (created_at , updated_at) you don't want to retrieve from a database table.
185
+ *
186
+ * @return {this} this
187
+ */
188
+ exceptTimestamp() {
189
+ this.$state.set('EXCEPTS', ['created_at', 'updated_at']);
144
190
  return this;
145
191
  }
146
192
  /**
@@ -149,7 +195,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
149
195
  * @return {this} this
150
196
  */
151
197
  void() {
152
- this._setState('VOID', true);
198
+ this.$state.set('VOID', true);
153
199
  return this;
154
200
  }
155
201
  /**
@@ -160,7 +206,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
160
206
  * @return {this} this
161
207
  */
162
208
  only(...columns) {
163
- this._setState('ONLY', columns);
209
+ this.$state.set('ONLY', columns);
164
210
  return this;
165
211
  }
166
212
  /**
@@ -173,7 +219,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
173
219
  * @return {this} this
174
220
  */
175
221
  chunk(chunk) {
176
- this._setState('CHUNK', chunk);
222
+ this.$state.set('CHUNK', chunk);
177
223
  return this;
178
224
  }
179
225
  /**
@@ -207,10 +253,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
207
253
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
208
254
  value = this.$utils.escape(value);
209
255
  value = this._valueTrueFalse(value);
210
- this._setState('WHERE', [
211
- ...this._getState('WHERE'),
256
+ this.$state.set('WHERE', [
257
+ ...this.$state.get('WHERE'),
212
258
  [
213
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
259
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
214
260
  `${this.bindColumn(String(column))}`,
215
261
  `${operator}`,
216
262
  `${this._checkValueHasRaw(value)}`
@@ -233,10 +279,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
233
279
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
234
280
  value = this.$utils.escape(value);
235
281
  value = this._valueTrueFalse(value);
236
- this._setState('WHERE', [
237
- ...this._getState('WHERE'),
282
+ this.$state.set('WHERE', [
283
+ ...this.$state.get('WHERE'),
238
284
  [
239
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
285
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
240
286
  `${this.bindColumn(String(column))}`,
241
287
  `${operator}`,
242
288
  `${this._checkValueHasRaw(value)}`
@@ -253,10 +299,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
253
299
  * @return {this} this
254
300
  */
255
301
  whereRaw(sql) {
256
- this._setState('WHERE', [
257
- ...this._getState('WHERE'),
302
+ this.$state.set('WHERE', [
303
+ ...this.$state.get('WHERE'),
258
304
  [
259
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
305
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
260
306
  `${sql}`
261
307
  ].join(' ')
262
308
  ]);
@@ -271,10 +317,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
271
317
  * @return {this} this
272
318
  */
273
319
  orWhereRaw(sql) {
274
- this._setState('WHERE', [
275
- ...this._getState('WHERE'),
320
+ this.$state.set('WHERE', [
321
+ ...this.$state.get('WHERE'),
276
322
  [
277
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
323
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
278
324
  `${sql}`
279
325
  ].join(' ')
280
326
  ]);
@@ -293,10 +339,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
293
339
  for (const column in columns) {
294
340
  const operator = '=';
295
341
  const value = this.$utils.escape(columns[column]);
296
- this._setState('WHERE', [
297
- ...this._getState('WHERE'),
342
+ this.$state.set('WHERE', [
343
+ ...this.$state.get('WHERE'),
298
344
  [
299
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
345
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
300
346
  `${this.bindColumn(String(column))}`,
301
347
  `${operator}`,
302
348
  `${this._checkValueHasRaw(value)}`
@@ -319,10 +365,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
319
365
  whereJSON(column, { key, value, operator }) {
320
366
  value = this.$utils.escape(value);
321
367
  value = this._valueTrueFalse(value);
322
- this._setState('WHERE', [
323
- ...this._getState('WHERE'),
368
+ this.$state.set('WHERE', [
369
+ ...this.$state.get('WHERE'),
324
370
  [
325
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
371
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
326
372
  `${this.bindColumn(column)}->>'$.${key}'`,
327
373
  `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
328
374
  `${this._checkValueHasRaw(value)}`
@@ -353,26 +399,45 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
353
399
  * @return {this}
354
400
  */
355
401
  whereExists(sql) {
356
- this._setState('WHERE', [
357
- ...this._getState('WHERE'),
402
+ this.$state.set('WHERE', [
403
+ ...this.$state.get('WHERE'),
358
404
  [
359
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
405
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
360
406
  `${this.$constants('EXISTS')}`,
361
407
  `(${sql})`
362
408
  ].join(' ')
363
409
  ]);
364
410
  return this;
365
411
  }
412
+ /**
413
+ *
414
+ * The 'whereExists' method is used to add a conditional clause to a database query that checks for the existence of related records in a subquery or another table.
415
+ *
416
+ * It allows you to filter records based on whether a specified condition is true for related records.
417
+ * @param {string} sql
418
+ * @return {this}
419
+ */
420
+ whereNotExists(sql) {
421
+ this.$state.set('WHERE', [
422
+ ...this.$state.get('WHERE'),
423
+ [
424
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
425
+ `${this.$constants('NOT')} ${this.$constants('EXISTS')}`,
426
+ `(${sql})`
427
+ ].join(' ')
428
+ ]);
429
+ return this;
430
+ }
366
431
  /**
367
432
  *
368
433
  * @param {number} id
369
434
  * @return {this} this
370
435
  */
371
436
  whereId(id, column = 'id') {
372
- this._setState('WHERE', [
373
- ...this._getState('WHERE'),
437
+ this.$state.set('WHERE', [
438
+ ...this.$state.get('WHERE'),
374
439
  [
375
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
440
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
376
441
  `${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
377
442
  ].join(' ')
378
443
  ]);
@@ -385,10 +450,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
385
450
  */
386
451
  whereEmail(email) {
387
452
  const column = 'email';
388
- this._setState('WHERE', [
389
- ...this._getState('WHERE'),
453
+ this.$state.set('WHERE', [
454
+ ...this.$state.get('WHERE'),
390
455
  [
391
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
456
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
392
457
  `${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
393
458
  ].join(' ')
394
459
  ]);
@@ -401,10 +466,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
401
466
  * @return {this}
402
467
  */
403
468
  whereUser(userId, column = 'user_id') {
404
- this._setState('WHERE', [
405
- ...this._getState('WHERE'),
469
+ this.$state.set('WHERE', [
470
+ ...this.$state.get('WHERE'),
406
471
  [
407
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
472
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
408
473
  `${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
409
474
  ].join(' ')
410
475
  ]);
@@ -424,10 +489,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
424
489
  const values = array.length
425
490
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
426
491
  : this.$constants('NULL');
427
- this._setState('WHERE', [
428
- ...this._getState('WHERE'),
492
+ this.$state.set('WHERE', [
493
+ ...this.$state.get('WHERE'),
429
494
  [
430
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
495
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
431
496
  `${this.bindColumn(column)}`,
432
497
  `${this.$constants('IN')}`,
433
498
  `(${values})`
@@ -449,10 +514,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
449
514
  const values = array.length
450
515
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
451
516
  : this.$constants('NULL');
452
- this._setState('WHERE', [
453
- ...this._getState('WHERE'),
517
+ this.$state.set('WHERE', [
518
+ ...this.$state.get('WHERE'),
454
519
  [
455
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
520
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
456
521
  `${this.bindColumn(column)}`,
457
522
  `${this.$constants('IN')}`,
458
523
  `(${values})`
@@ -474,10 +539,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
474
539
  if (!array.length)
475
540
  return this;
476
541
  const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
477
- this._setState('WHERE', [
478
- ...this._getState('WHERE'),
542
+ this.$state.set('WHERE', [
543
+ ...this.$state.get('WHERE'),
479
544
  [
480
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
545
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
481
546
  `${this.bindColumn(column)}`,
482
547
  `${this.$constants('NOT_IN')}`,
483
548
  `(${values})`
@@ -499,10 +564,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
499
564
  if (!array.length)
500
565
  return this;
501
566
  const values = `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`;
502
- this._setState('WHERE', [
503
- ...this._getState('WHERE'),
567
+ this.$state.set('WHERE', [
568
+ ...this.$state.get('WHERE'),
504
569
  [
505
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
570
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
506
571
  `${this.bindColumn(column)}`,
507
572
  `${this.$constants('NOT_IN')}`,
508
573
  `(${values})`
@@ -523,10 +588,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
523
588
  whereSubQuery(column, subQuery) {
524
589
  if (!this.$utils.isSubQuery(subQuery))
525
590
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
526
- this._setState('WHERE', [
527
- ...this._getState('WHERE'),
591
+ this.$state.set('WHERE', [
592
+ ...this.$state.get('WHERE'),
528
593
  [
529
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
594
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
530
595
  `${this.bindColumn(column)}`,
531
596
  `${this.$constants('IN')}`,
532
597
  `(${subQuery})`
@@ -547,10 +612,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
547
612
  whereNotSubQuery(column, subQuery) {
548
613
  if (!this.$utils.isSubQuery(subQuery))
549
614
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
550
- this._setState('WHERE', [
551
- ...this._getState('WHERE'),
615
+ this.$state.set('WHERE', [
616
+ ...this.$state.get('WHERE'),
552
617
  [
553
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
618
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
554
619
  `${this.bindColumn(column)}`,
555
620
  `${this.$constants('NOT_IN')}`,
556
621
  `(${subQuery})`
@@ -571,10 +636,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
571
636
  orWhereSubQuery(column, subQuery) {
572
637
  if (!this.$utils.isSubQuery(subQuery))
573
638
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
574
- this._setState('WHERE', [
575
- ...this._getState('WHERE'),
639
+ this.$state.set('WHERE', [
640
+ ...this.$state.get('WHERE'),
576
641
  [
577
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
642
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
578
643
  `${this.bindColumn(column)}`,
579
644
  `${this.$constants('IN')}`,
580
645
  `(${subQuery})`
@@ -595,10 +660,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
595
660
  orWhereNotSubQuery(column, subQuery) {
596
661
  if (!this.$utils.isSubQuery(subQuery))
597
662
  throw new Error(`This "${subQuery}" is invalid sub query (Sub query Operand should contain 1 column(s) not select * )`);
598
- this._setState('WHERE', [
599
- ...this._getState('WHERE'),
663
+ this.$state.set('WHERE', [
664
+ ...this.$state.get('WHERE'),
600
665
  [
601
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
666
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
602
667
  `${this.bindColumn(column)}`,
603
668
  `${this.$constants('NOT_IN')}`,
604
669
  `(${subQuery})`
@@ -618,10 +683,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
618
683
  if (!Array.isArray(array))
619
684
  throw new Error("Value is't array");
620
685
  if (!array.length) {
621
- this._setState('WHERE', [
622
- ...this._getState('WHERE'),
686
+ this.$state.set('WHERE', [
687
+ ...this.$state.get('WHERE'),
623
688
  [
624
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
689
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
625
690
  `${this.bindColumn(column)}`,
626
691
  `${this.$constants('BETWEEN')}`,
627
692
  `${this.$constants('NULL')}`,
@@ -632,10 +697,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
632
697
  return this;
633
698
  }
634
699
  const [value1, value2] = array;
635
- this._setState('WHERE', [
636
- ...this._getState('WHERE'),
700
+ this.$state.set('WHERE', [
701
+ ...this.$state.get('WHERE'),
637
702
  [
638
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
703
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
639
704
  `${this.bindColumn(column)}`,
640
705
  `${this.$constants('BETWEEN')}`,
641
706
  `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
@@ -657,10 +722,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
657
722
  if (!Array.isArray(array))
658
723
  throw new Error("Value is't array");
659
724
  if (!array.length) {
660
- this._setState('WHERE', [
661
- ...this._getState('WHERE'),
725
+ this.$state.set('WHERE', [
726
+ ...this.$state.get('WHERE'),
662
727
  [
663
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
728
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
664
729
  `${this.bindColumn(column)}`,
665
730
  `${this.$constants('BETWEEN')}`,
666
731
  `${this.$constants('NULL')}`,
@@ -671,10 +736,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
671
736
  return this;
672
737
  }
673
738
  const [value1, value2] = array;
674
- this._setState('WHERE', [
675
- ...this._getState('WHERE'),
739
+ this.$state.set('WHERE', [
740
+ ...this.$state.get('WHERE'),
676
741
  [
677
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
742
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
678
743
  `${this.bindColumn(column)}`,
679
744
  `${this.$constants('BETWEEN')}`,
680
745
  `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
@@ -696,10 +761,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
696
761
  if (!Array.isArray(array))
697
762
  throw new Error("Value is't array");
698
763
  if (!array.length) {
699
- this._setState('WHERE', [
700
- ...this._getState('WHERE'),
764
+ this.$state.set('WHERE', [
765
+ ...this.$state.get('WHERE'),
701
766
  [
702
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
767
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
703
768
  `${this.bindColumn(column)}`,
704
769
  `${this.$constants('NOT_BETWEEN')}`,
705
770
  `${this.$constants('NULL')}`,
@@ -710,10 +775,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
710
775
  return this;
711
776
  }
712
777
  const [value1, value2] = array;
713
- this._setState('WHERE', [
714
- ...this._getState('WHERE'),
778
+ this.$state.set('WHERE', [
779
+ ...this.$state.get('WHERE'),
715
780
  [
716
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
781
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
717
782
  `${this.bindColumn(column)}`,
718
783
  `${this.$constants('NOT_BETWEEN')}`,
719
784
  `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
@@ -735,10 +800,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
735
800
  if (!Array.isArray(array))
736
801
  throw new Error("Value is't array");
737
802
  if (!array.length) {
738
- this._setState('WHERE', [
739
- ...this._getState('WHERE'),
803
+ this.$state.set('WHERE', [
804
+ ...this.$state.get('WHERE'),
740
805
  [
741
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
806
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
742
807
  `${this.bindColumn(column)}`,
743
808
  `${this.$constants('NOT_BETWEEN')}`,
744
809
  `${this.$constants('NULL')}`,
@@ -749,10 +814,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
749
814
  return this;
750
815
  }
751
816
  const [value1, value2] = array;
752
- this._setState('WHERE', [
753
- ...this._getState('WHERE'),
817
+ this.$state.set('WHERE', [
818
+ ...this.$state.get('WHERE'),
754
819
  [
755
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
820
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
756
821
  `${this.bindColumn(column)}`,
757
822
  `${this.$constants('NOT_BETWEEN')}`,
758
823
  `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
@@ -770,10 +835,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
770
835
  * @return {this}
771
836
  */
772
837
  whereNull(column) {
773
- this._setState('WHERE', [
774
- ...this._getState('WHERE'),
838
+ this.$state.set('WHERE', [
839
+ ...this.$state.get('WHERE'),
775
840
  [
776
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
841
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
777
842
  `${this.bindColumn(column)}`,
778
843
  `${this.$constants('IS_NULL')}`
779
844
  ].join(' ')
@@ -788,10 +853,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
788
853
  * @return {this}
789
854
  */
790
855
  orWhereNull(column) {
791
- this._setState('WHERE', [
792
- ...this._getState('WHERE'),
856
+ this.$state.set('WHERE', [
857
+ ...this.$state.get('WHERE'),
793
858
  [
794
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
859
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
795
860
  `${this.bindColumn(column)}`,
796
861
  `${this.$constants('IS_NULL')}`
797
862
  ].join(' ')
@@ -806,10 +871,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
806
871
  * @return {this}
807
872
  */
808
873
  whereNotNull(column) {
809
- this._setState('WHERE', [
810
- ...this._getState('WHERE'),
874
+ this.$state.set('WHERE', [
875
+ ...this.$state.get('WHERE'),
811
876
  [
812
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
877
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
813
878
  `${this.bindColumn(column)}`,
814
879
  `${this.$constants('IS_NOT_NULL')}`
815
880
  ].join(' ')
@@ -824,10 +889,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
824
889
  * @return {this}
825
890
  */
826
891
  orWhereNotNull(column) {
827
- this._setState('WHERE', [
828
- ...this._getState('WHERE'),
892
+ this.$state.set('WHERE', [
893
+ ...this.$state.get('WHERE'),
829
894
  [
830
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
895
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
831
896
  `${this.bindColumn(column)}`,
832
897
  `${this.$constants('IS_NOT_NULL')}`
833
898
  ].join(' ')
@@ -849,10 +914,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
849
914
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
850
915
  value = this.$utils.escape(value);
851
916
  value = this._valueTrueFalse(value);
852
- this._setState('WHERE', [
853
- ...this._getState('WHERE'),
917
+ this.$state.set('WHERE', [
918
+ ...this.$state.get('WHERE'),
854
919
  [
855
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
920
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
856
921
  `${this.$constants('BINARY')}`,
857
922
  `${this.bindColumn(column)}`,
858
923
  `${operator}`,
@@ -890,10 +955,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
890
955
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
891
956
  value = this.$utils.escape(value);
892
957
  value = this._valueTrueFalse(value);
893
- this._setState('WHERE', [
894
- ...this._getState('WHERE'),
958
+ this.$state.set('WHERE', [
959
+ ...this.$state.get('WHERE'),
895
960
  [
896
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
961
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
897
962
  `${this.$constants('BINARY')}`,
898
963
  `${this.bindColumn(column)}`,
899
964
  `${operator}`,
@@ -911,20 +976,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
911
976
  */
912
977
  whereQuery(callback) {
913
978
  var _a;
914
- const db = new DB_1.DB((_a = this._getState('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
979
+ const db = new DB_1.DB((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
915
980
  const repository = callback(db);
916
981
  if (repository instanceof Promise)
917
982
  throw new Error('"whereQuery" is not supported a Promise');
918
983
  if (!(repository instanceof DB_1.DB))
919
984
  throw new Error(`Unknown callback query: '${repository}'`);
920
- const where = (repository === null || repository === void 0 ? void 0 : repository._getState('WHERE')) || [];
985
+ const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || [];
921
986
  if (!where.length)
922
987
  return this;
923
988
  const query = where.join(' ');
924
- this._setState('WHERE', [
925
- ...this._getState('WHERE'),
989
+ this.$state.set('WHERE', [
990
+ ...this.$state.get('WHERE'),
926
991
  [
927
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
992
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
928
993
  `(${query})`
929
994
  ].join(' ')
930
995
  ]);
@@ -949,20 +1014,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
949
1014
  */
950
1015
  orWhereQuery(callback) {
951
1016
  var _a;
952
- const db = new DB_1.DB((_a = this._getState('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
1017
+ const db = new DB_1.DB((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
953
1018
  const repository = callback(db);
954
1019
  if (repository instanceof Promise)
955
1020
  throw new Error('"whereQuery" is not supported a Promise');
956
1021
  if (!(repository instanceof DB_1.DB))
957
1022
  throw new Error(`Unknown callback query: '[${repository}]'`);
958
- const where = (repository === null || repository === void 0 ? void 0 : repository._getState('WHERE')) || [];
1023
+ const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || [];
959
1024
  if (!where.length)
960
1025
  return this;
961
1026
  const query = where.join(' ');
962
- this._setState('WHERE', [
963
- ...this._getState('WHERE'),
1027
+ this.$state.set('WHERE', [
1028
+ ...this.$state.get('WHERE'),
964
1029
  [
965
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
1030
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
966
1031
  `(${query})`
967
1032
  ].join(' ')
968
1033
  ]);
@@ -978,6 +1043,55 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
978
1043
  orWhereGroup(callback) {
979
1044
  return this.orWhereQuery(callback);
980
1045
  }
1046
+ /**
1047
+ * The 'whereAny' method is used to add conditions to a database query,
1048
+ * where either the original condition or the new condition must be true.
1049
+ *
1050
+ * If has only 2 arguments default operator '='
1051
+ * @param {string[]} columns
1052
+ * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
1053
+ * @param {any?} value
1054
+ * @return {this}
1055
+ */
1056
+ whereAny(columns, operator, value) {
1057
+ [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
1058
+ value = this.$utils.escape(value);
1059
+ value = this._valueTrueFalse(value);
1060
+ this.whereQuery((query) => {
1061
+ for (const index in columns) {
1062
+ const column = columns[index];
1063
+ if (+index === 0) {
1064
+ query.where(column, operator, value);
1065
+ continue;
1066
+ }
1067
+ query.orWhere(column, operator, value);
1068
+ }
1069
+ return query;
1070
+ });
1071
+ return this;
1072
+ }
1073
+ /**
1074
+ * The 'whereAll' method is used to clause to a database query.
1075
+ *
1076
+ * This method allows you to specify conditions that the retrieved records must meet.
1077
+ *
1078
+ * If has only 2 arguments default operator '='
1079
+ * @param {string[]} columns
1080
+ * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
1081
+ * @param {any?} value
1082
+ * @return {this}
1083
+ */
1084
+ whereAll(columns, operator, value) {
1085
+ [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
1086
+ value = this.$utils.escape(value);
1087
+ value = this._valueTrueFalse(value);
1088
+ this.whereQuery((query) => {
1089
+ for (const column of columns)
1090
+ query.where(column, operator, value);
1091
+ return query;
1092
+ });
1093
+ return this;
1094
+ }
981
1095
  /**
982
1096
  * The 'whereCases' method is used to add conditions with cases to a database query.
983
1097
  *
@@ -1001,11 +1115,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1001
1115
  `${this.$constants('WHEN')} ${c.when} ${this.$constants('THEN')} ${c.then}`
1002
1116
  ];
1003
1117
  }
1004
- this._setState('WHERE', [
1005
- ...this._getState('WHERE'),
1118
+ this.$state.set('WHERE', [
1119
+ ...this.$state.get('WHERE'),
1006
1120
  [
1007
1121
  [
1008
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
1122
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
1009
1123
  '(',
1010
1124
  this.$constants('CASE'),
1011
1125
  query.join(' '),
@@ -1040,11 +1154,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1040
1154
  `${this.$constants('WHEN')} ${c.when} ${this.$constants('THEN')} ${c.then}`
1041
1155
  ];
1042
1156
  }
1043
- this._setState('WHERE', [
1044
- ...this._getState('WHERE'),
1157
+ this.$state.set('WHERE', [
1158
+ ...this.$state.get('WHERE'),
1045
1159
  [
1046
1160
  [
1047
- this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
1161
+ this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
1048
1162
  '(',
1049
1163
  this.$constants('CASE'),
1050
1164
  query.join(' '),
@@ -1087,7 +1201,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1087
1201
  }
1088
1202
  if (query.length <= 1)
1089
1203
  return this;
1090
- this._setState('SELECT', [...this._getState('SELECT'), `${query.join(' ')} ${this.$constants('AS')} ${as}`]);
1204
+ this.$state.set('SELECT', [...this.$state.get('SELECT'), `${query.join(' ')} ${this.$constants('AS')} ${as}`]);
1091
1205
  return this;
1092
1206
  }
1093
1207
  /**
@@ -1109,8 +1223,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1109
1223
  join(localKey, referenceKey) {
1110
1224
  var _a;
1111
1225
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
1112
- this._setState('JOIN', [
1113
- ...this._getState('JOIN'),
1226
+ this.$state.set('JOIN', [
1227
+ ...this.$state.get('JOIN'),
1114
1228
  [
1115
1229
  `${this.$constants('INNER_JOIN')}`,
1116
1230
  `\`${table}\` ${this.$constants('ON')}`,
@@ -1119,6 +1233,32 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1119
1233
  ]);
1120
1234
  return this;
1121
1235
  }
1236
+ /**
1237
+ * The 'join' method is used to perform various types of SQL joins between two or more database tables.
1238
+ *
1239
+ * Joins are used to combine data from different tables based on a specified condition, allowing you to retrieve data from related tables in a single query.
1240
+ * @param {object} property object { localKey , foreignKey , sqlr }
1241
+ * @property {string} property.localKey local key in current table
1242
+ * @property {string} property.foreignKey reference key in next table
1243
+ * @property {string} property.sql sql string
1244
+ * @example
1245
+ * await new DB('users')
1246
+ * .joinSubQuery({ localKey : 'id' , foreignKey : 'userId' , sql : '....sql'})
1247
+ * .get()
1248
+ * @return {this}
1249
+ */
1250
+ joinSubQuery({ localKey, foreignKey, sql }) {
1251
+ this.$state.set('JOIN', [
1252
+ ...this.$state.get('JOIN'),
1253
+ [
1254
+ `${this.$constants('INNER_JOIN')}`,
1255
+ `(${sql}) as subquery`,
1256
+ `${this.$constants('ON')}`,
1257
+ `${this.bindColumn(localKey)} = subquery.\`${foreignKey}\``
1258
+ ].join(' ')
1259
+ ]);
1260
+ return this;
1261
+ }
1122
1262
  /**
1123
1263
  * The 'rightJoin' method is used to perform a right join operation between two database tables.
1124
1264
  *
@@ -1132,8 +1272,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1132
1272
  rightJoin(localKey, referenceKey) {
1133
1273
  var _a;
1134
1274
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
1135
- this._setState('JOIN', [
1136
- ...this._getState('JOIN'),
1275
+ this.$state.set('JOIN', [
1276
+ ...this.$state.get('JOIN'),
1137
1277
  [
1138
1278
  `${this.$constants('RIGHT_JOIN')}`,
1139
1279
  `\`${table}\` ${this.$constants('ON')}`,
@@ -1155,8 +1295,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1155
1295
  leftJoin(localKey, referenceKey) {
1156
1296
  var _a;
1157
1297
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
1158
- this._setState('JOIN', [
1159
- ...this._getState('JOIN'),
1298
+ this.$state.set('JOIN', [
1299
+ ...this.$state.get('JOIN'),
1160
1300
  [
1161
1301
  `${this.$constants('LEFT_JOIN')}`,
1162
1302
  `\`${table}\` ${this.$constants('ON')}`,
@@ -1176,8 +1316,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1176
1316
  crossJoin(localKey, referenceKey) {
1177
1317
  var _a;
1178
1318
  const table = (_a = referenceKey.split('.')) === null || _a === void 0 ? void 0 : _a.shift();
1179
- this._setState('JOIN', [
1180
- ...this._getState('JOIN'),
1319
+ this.$state.set('JOIN', [
1320
+ ...this.$state.get('JOIN'),
1181
1321
  [
1182
1322
  `${this.$constants('CROSS_JOIN')}`,
1183
1323
  `\`${table}\` ${this.$constants('ON')}`,
@@ -1194,7 +1334,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1194
1334
  * @param {string?} order by default order = 'asc' but you can used 'asc' or 'desc'
1195
1335
  * @return {this}
1196
1336
  */
1197
- orderBy(column, order = this.$constants('ASC')) {
1337
+ orderBy(column, order = 'ASC') {
1198
1338
  if (typeof column !== 'string')
1199
1339
  return this;
1200
1340
  if (column.includes(this.$constants('RAW')) || /\./.test(column)) {
@@ -1202,8 +1342,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1202
1342
  if (/\./.test(column))
1203
1343
  column = this.bindColumn(column);
1204
1344
  }
1205
- this._setState('ORDER_BY', [
1206
- ...this._getState('ORDER_BY'),
1345
+ this.$state.set('ORDER_BY', [
1346
+ ...this.$state.get('ORDER_BY'),
1207
1347
  `\`${column}\` ${order.toUpperCase()}`
1208
1348
  ]);
1209
1349
  return this;
@@ -1222,12 +1362,32 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1222
1362
  if (column.includes(this.$constants('RAW'))) {
1223
1363
  column = column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1224
1364
  }
1225
- this._setState('ORDER_BY', [
1226
- ...this._getState('ORDER_BY'),
1365
+ this.$state.set('ORDER_BY', [
1366
+ ...this.$state.get('ORDER_BY'),
1227
1367
  `${column} ${order.toUpperCase()}`
1228
1368
  ]);
1229
1369
  return this;
1230
1370
  }
1371
+ /**
1372
+ * The 'random' method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
1373
+ *
1374
+ * @return {this}
1375
+ */
1376
+ random() {
1377
+ this.$state.set('ORDER_BY', [
1378
+ ...this.$state.get('ORDER_BY'),
1379
+ `${this.$constants('RAND')}`
1380
+ ]);
1381
+ return this;
1382
+ }
1383
+ /**
1384
+ * The 'inRandom' method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
1385
+ *
1386
+ * @return {this}
1387
+ */
1388
+ inRandom() {
1389
+ return this.random();
1390
+ }
1231
1391
  /**
1232
1392
  * The 'latest' method is used to specify the order in which the results of a database query should be sorted.
1233
1393
  *
@@ -1236,19 +1396,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1236
1396
  * @return {this}
1237
1397
  */
1238
1398
  latest(...columns) {
1239
- let column = 'id';
1399
+ let orderBy = '`id`';
1240
1400
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
1241
- column = columns.map(column => {
1242
- if (/\./.test(column))
1243
- return this.bindColumn(column);
1244
- if (column.includes(this.$constants('RAW')))
1245
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1246
- return column;
1401
+ orderBy = columns.map(c => {
1402
+ if (/\./.test(c))
1403
+ return this.bindColumn(c);
1404
+ if (c.includes(this.$constants('RAW')))
1405
+ return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
1406
+ return `\`${c}\``;
1247
1407
  }).join(', ');
1248
1408
  }
1249
- this._setState('ORDER_BY', [
1250
- ...this._getState('ORDER_BY'),
1251
- `\`${column}\` ${this.$constants('DESC')}`
1409
+ this.$state.set('ORDER_BY', [
1410
+ ...this.$state.get('ORDER_BY'),
1411
+ `${orderBy} ${this.$constants('DESC')}`
1252
1412
  ]);
1253
1413
  return this;
1254
1414
  }
@@ -1262,17 +1422,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1262
1422
  * @return {this}
1263
1423
  */
1264
1424
  latestRaw(...columns) {
1265
- let column = 'id';
1425
+ let orderBy = '`id`';
1266
1426
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
1267
- column = columns.map(column => {
1427
+ orderBy = columns.map(column => {
1268
1428
  if (column.includes(this.$constants('RAW')))
1269
1429
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1270
1430
  return column;
1271
1431
  }).join(', ');
1272
1432
  }
1273
- this._setState('ORDER_BY', [
1274
- ...this._getState('ORDER_BY'),
1275
- `${column} ${this.$constants('DESC')}`
1433
+ this.$state.set('ORDER_BY', [
1434
+ ...this.$state.get('ORDER_BY'),
1435
+ `${orderBy} ${this.$constants('DESC')}`
1276
1436
  ]);
1277
1437
  return this;
1278
1438
  }
@@ -1284,19 +1444,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1284
1444
  * @return {this}
1285
1445
  */
1286
1446
  oldest(...columns) {
1287
- let column = 'id';
1447
+ let orderBy = '`id`';
1288
1448
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
1289
- column = columns.map(column => {
1290
- if (/\./.test(column))
1291
- return this.bindColumn(column);
1292
- if (column.includes(this.$constants('RAW')))
1293
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1294
- return column;
1449
+ orderBy = columns.map(c => {
1450
+ if (/\./.test(c))
1451
+ return this.bindColumn(c);
1452
+ if (c.includes(this.$constants('RAW')))
1453
+ return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
1454
+ return `\`${c}\``;
1295
1455
  }).join(', ');
1296
1456
  }
1297
- this._setState('ORDER_BY', [
1298
- ...this._getState('ORDER_BY'),
1299
- `\`${column}\` ${this.$constants('ASC')}`
1457
+ this.$state.set('ORDER_BY', [
1458
+ ...this.$state.get('ORDER_BY'),
1459
+ `${orderBy} ${this.$constants('ASC')}`
1300
1460
  ]);
1301
1461
  return this;
1302
1462
  }
@@ -1310,17 +1470,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1310
1470
  * @return {this}
1311
1471
  */
1312
1472
  oldestRaw(...columns) {
1313
- let column = 'id';
1473
+ let orderBy = '`id`';
1314
1474
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
1315
- column = columns.map(column => {
1475
+ orderBy = columns.map(column => {
1316
1476
  if (column.includes(this.$constants('RAW')))
1317
1477
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1318
1478
  return column;
1319
1479
  }).join(', ');
1320
1480
  }
1321
- this._setState('ORDER_BY', [
1322
- ...this._getState('ORDER_BY'),
1323
- `${column} ${this.$constants('ASC')}`
1481
+ this.$state.set('ORDER_BY', [
1482
+ ...this.$state.get('ORDER_BY'),
1483
+ `${orderBy} ${this.$constants('ASC')}`
1324
1484
  ]);
1325
1485
  return this;
1326
1486
  }
@@ -1342,7 +1502,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1342
1502
  return `\`${column}\``;
1343
1503
  }).join(', ');
1344
1504
  }
1345
- this._setState('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1505
+ this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1346
1506
  return this;
1347
1507
  }
1348
1508
  /**
@@ -1365,7 +1525,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1365
1525
  return column;
1366
1526
  }).join(', ');
1367
1527
  }
1368
- this._setState('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1528
+ this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1369
1529
  return this;
1370
1530
  }
1371
1531
  /**
@@ -1380,10 +1540,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1380
1540
  having(condition) {
1381
1541
  if (condition.includes(this.$constants('RAW'))) {
1382
1542
  condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants('RAW'), '');
1383
- this._setState('HAVING', `${this.$constants('HAVING')} ${condition}`);
1384
- return this;
1385
1543
  }
1386
- this._setState('HAVING', `${this.$constants('HAVING')} \`${condition}\``);
1544
+ this.$state.set('HAVING', `${this.$constants('HAVING')} ${condition}`);
1387
1545
  return this;
1388
1546
  }
1389
1547
  /**
@@ -1398,30 +1556,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1398
1556
  * @return {this}
1399
1557
  */
1400
1558
  havingRaw(condition) {
1401
- if (condition.includes(this.$constants('RAW'))) {
1402
- condition = condition === null || condition === void 0 ? void 0 : condition.replace(this.$constants('RAW'), '');
1403
- }
1404
- this._setState('HAVING', `${this.$constants('HAVING')} ${condition}`);
1405
- return this;
1406
- }
1407
- /**
1408
- * The 'random' method is used to add a random ordering to a database query.
1409
- *
1410
- * This method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
1411
- * @return {this}
1412
- */
1413
- random() {
1414
- this._setState('ORDER_BY', `${this.$constants('ORDER_BY')} ${this.$constants('RAND')}`);
1415
- return this;
1416
- }
1417
- /**
1418
- * The 'inRandom' method is used to add a random ordering to a database query.
1419
- *
1420
- * This method is used to retrieve random records from a database table or to randomize the order in which records are returned in the query result set.
1421
- * @return {this}
1422
- */
1423
- inRandom() {
1424
- return this.random();
1559
+ return this.having(condition);
1425
1560
  }
1426
1561
  /**
1427
1562
  * The 'limit' method is used to limit the number of records returned by a database query.
@@ -1431,7 +1566,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1431
1566
  * @return {this}
1432
1567
  */
1433
1568
  limit(number = 1) {
1434
- this._setState('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1569
+ this.$state.set('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1435
1570
  return this;
1436
1571
  }
1437
1572
  /**
@@ -1452,9 +1587,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1452
1587
  * @return {this}
1453
1588
  */
1454
1589
  offset(number = 1) {
1455
- this._setState('OFFSET', `${this.$constants('OFFSET')} ${number}`);
1456
- if (!this._getState('LIMIT'))
1457
- this._setState('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1590
+ this.$state.set('OFFSET', `${this.$constants('OFFSET')} ${number}`);
1591
+ if (!this.$state.get('LIMIT'))
1592
+ this.$state.set('LIMIT', `${this.$constants('LIMIT')} ${number}`);
1458
1593
  return this;
1459
1594
  }
1460
1595
  /**
@@ -1474,7 +1609,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1474
1609
  * @return {this} this
1475
1610
  */
1476
1611
  hidden(...columns) {
1477
- this._setState('HIDDEN', columns);
1612
+ this.$state.set('HIDDEN', columns);
1478
1613
  return this;
1479
1614
  }
1480
1615
  /**
@@ -1503,12 +1638,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1503
1638
  }
1504
1639
  }
1505
1640
  const query = this._queryUpdate(data);
1506
- this._setState('UPDATE', [
1641
+ this.$state.set('UPDATE', [
1507
1642
  `${this.$constants('UPDATE')}`,
1508
- `${this._getState('TABLE_NAME')}`,
1643
+ `${this.$state.get('TABLE_NAME')}`,
1509
1644
  `${query}`
1510
1645
  ].join(' '));
1511
- this._setState('SAVE', 'UPDATE');
1646
+ this.$state.set('SAVE', 'UPDATE');
1512
1647
  return this;
1513
1648
  }
1514
1649
  /**
@@ -1536,12 +1671,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1536
1671
  }
1537
1672
  }
1538
1673
  const query = this._queryUpdate(data);
1539
- this._setState('UPDATE', [
1674
+ this.$state.set('UPDATE', [
1540
1675
  `${this.$constants('UPDATE')}`,
1541
- `${this._getState('TABLE_NAME')}`,
1676
+ `${this.$state.get('TABLE_NAME')}`,
1542
1677
  `${query}`
1543
1678
  ].join(' '));
1544
- this._setState('SAVE', 'UPDATE');
1679
+ this.$state.set('SAVE', 'UPDATE');
1545
1680
  return this;
1546
1681
  }
1547
1682
  /**
@@ -1562,12 +1697,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1562
1697
  data[column] = this._updateHandler(column, value);
1563
1698
  }
1564
1699
  const query = this._queryUpdate(data);
1565
- this._setState('UPDATE', [
1700
+ this.$state.set('UPDATE', [
1566
1701
  `${this.$constants('UPDATE')}`,
1567
- `${this._getState('TABLE_NAME')}`,
1702
+ `${this.$state.get('TABLE_NAME')}`,
1568
1703
  `${query}`
1569
1704
  ].join(' '));
1570
- this._setState('SAVE', 'UPDATE');
1705
+ this.$state.set('SAVE', 'UPDATE');
1571
1706
  return this;
1572
1707
  }
1573
1708
  /**
@@ -1581,12 +1716,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1581
1716
  if (!Object.keys(data).length)
1582
1717
  throw new Error('This method must be required');
1583
1718
  const query = this._queryInsert(data);
1584
- this._setState('INSERT', [
1719
+ this.$state.set('INSERT', [
1585
1720
  `${this.$constants('INSERT')}`,
1586
- `${this._getState('TABLE_NAME')}`,
1721
+ `${this.$state.get('TABLE_NAME')}`,
1587
1722
  `${query}`
1588
1723
  ].join(' '));
1589
- this._setState('SAVE', 'INSERT');
1724
+ this.$state.set('SAVE', 'INSERT');
1590
1725
  return this;
1591
1726
  }
1592
1727
  /**
@@ -1600,12 +1735,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1600
1735
  if (!Object.keys(data).length)
1601
1736
  throw new Error('This method must be required');
1602
1737
  const query = this._queryInsert(data);
1603
- this._setState('INSERT', [
1738
+ this.$state.set('INSERT', [
1604
1739
  `${this.$constants('INSERT')}`,
1605
- `${this._getState('TABLE_NAME')}`,
1740
+ `${this.$state.get('TABLE_NAME')}`,
1606
1741
  `${query}`
1607
1742
  ].join(' '));
1608
- this._setState('SAVE', 'INSERT');
1743
+ this.$state.set('SAVE', 'INSERT');
1609
1744
  return this;
1610
1745
  }
1611
1746
  /**
@@ -1619,24 +1754,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1619
1754
  if (!Object.keys(data).length)
1620
1755
  throw new Error('This method must be required');
1621
1756
  const query = this._queryInsertMultiple(data);
1622
- this._setState('INSERT', [
1757
+ this.$state.set('INSERT', [
1623
1758
  `${this.$constants('INSERT')}`,
1624
- `${this._getState('TABLE_NAME')}`,
1759
+ `${this.$state.get('TABLE_NAME')}`,
1625
1760
  `${query}`
1626
1761
  ].join(' '));
1627
- this._setState('SAVE', 'INSERT_MULTIPLE');
1762
+ this.$state.set('SAVE', 'INSERT_MULTIPLE');
1628
1763
  return this;
1629
1764
  }
1630
- /**
1631
- * The 'createMany' method is used to insert a new records into a database table associated.
1632
- *
1633
- * It simplifies the process of creating and inserting records with an array.
1634
- * @param {array} data create multiple data
1635
- * @return {this} this this
1636
- */
1637
- createMany(data) {
1638
- return this.createMultiple(data);
1639
- }
1640
1765
  /**
1641
1766
  * The 'insertMultiple' method is used to insert a new records into a database table associated.
1642
1767
  *
@@ -1647,16 +1772,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1647
1772
  insertMultiple(data) {
1648
1773
  return this.createMultiple(data);
1649
1774
  }
1650
- /**
1651
- * The 'insertMany' method is used to insert a new records into a database table associated.
1652
- *
1653
- * It simplifies the process of creating and inserting records with an array.
1654
- * @param {array} data create multiple data
1655
- * @return {this} this this
1656
- */
1657
- insertMany(data) {
1658
- return this.createMultiple(data);
1659
- }
1660
1775
  /**
1661
1776
  * The 'createNotExists' method to insert data into a database table while ignoring any duplicate key constraint violations.
1662
1777
  *
@@ -1667,12 +1782,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1667
1782
  */
1668
1783
  createNotExists(data) {
1669
1784
  const query = this._queryInsert(data);
1670
- this._setState('INSERT', [
1785
+ this.$state.set('INSERT', [
1671
1786
  `${this.$constants('INSERT')}`,
1672
- `${this._getState('TABLE_NAME')}`,
1787
+ `${this.$state.get('TABLE_NAME')}`,
1673
1788
  `${query}`
1674
1789
  ].join(' '));
1675
- this._setState('SAVE', 'INSERT_NOT_EXISTS');
1790
+ this.$state.set('SAVE', 'INSERT_NOT_EXISTS');
1676
1791
  return this;
1677
1792
  }
1678
1793
  /**
@@ -1697,12 +1812,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1697
1812
  */
1698
1813
  createOrSelect(data) {
1699
1814
  const queryInsert = this._queryInsert(data);
1700
- this._setState('INSERT', [
1815
+ this.$state.set('INSERT', [
1701
1816
  `${this.$constants('INSERT')}`,
1702
- `${this._getState('TABLE_NAME')}`,
1817
+ `${this.$state.get('TABLE_NAME')}`,
1703
1818
  `${queryInsert}`
1704
1819
  ].join(' '));
1705
- this._setState('SAVE', 'INSERT_OR_SELECT');
1820
+ this.$state.set('SAVE', 'INSERT_OR_SELECT');
1706
1821
  return this;
1707
1822
  }
1708
1823
  /**
@@ -1729,17 +1844,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1729
1844
  this.limit(1);
1730
1845
  const queryUpdate = this._queryUpdate(data);
1731
1846
  const queryInsert = this._queryInsert(data);
1732
- this._setState('INSERT', [
1847
+ this.$state.set('INSERT', [
1733
1848
  `${this.$constants('INSERT')}`,
1734
- `${this._getState('TABLE_NAME')}`,
1849
+ `${this.$state.get('TABLE_NAME')}`,
1735
1850
  `${queryInsert}`
1736
1851
  ].join(' '));
1737
- this._setState('UPDATE', [
1852
+ this.$state.set('UPDATE', [
1738
1853
  `${this.$constants('UPDATE')}`,
1739
- `${this._getState('TABLE_NAME')}`,
1854
+ `${this.$state.get('TABLE_NAME')}`,
1740
1855
  `${queryUpdate}`
1741
1856
  ].join(' '));
1742
- this._setState('SAVE', 'UPDATE_OR_INSERT');
1857
+ this.$state.set('SAVE', 'UPDATE_OR_INSERT');
1743
1858
  return this;
1744
1859
  }
1745
1860
  /**
@@ -1779,6 +1894,71 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1779
1894
  this.updateOrCreate(data);
1780
1895
  return this;
1781
1896
  }
1897
+ /**
1898
+ *
1899
+ * @param {{when : Object , columns : Object}[]} cases update multiple data specific columns by cases update
1900
+ * @property {Record<string,string | number | boolean | null | undefined>} cases.when
1901
+ * @property {Record<string,string | number | boolean | null | undefined>} cases.columns
1902
+ * @return {this} this
1903
+ */
1904
+ updateMultiple(cases) {
1905
+ if (!cases.length)
1906
+ throw new Error(`The method 'updateMultiple' must not be empty.`);
1907
+ this.limit(cases.length);
1908
+ const updateColumns = cases.reduce((columns, item) => {
1909
+ return (item.columns && Object.keys(item.columns).forEach(key => columns[key] = [
1910
+ this.$constants('RAW'),
1911
+ this.$constants('CASE'),
1912
+ `${this.$constants('ELSE')} ${this.bindColumn(key)}`,
1913
+ this.$constants('END')
1914
+ ]), columns);
1915
+ }, {});
1916
+ const columns = cases.reduce((columns, item) => {
1917
+ return (item.columns && Object.keys(item.columns).forEach(key => columns[key] = ''), columns);
1918
+ }, {});
1919
+ for (let i = cases.length - 1; i >= 0; i--) {
1920
+ const c = cases[i];
1921
+ if (c.when == null || !Object.keys(c.when).length)
1922
+ throw new Error(`This 'when' property is missing some properties`);
1923
+ if (c.columns == null || !Object.keys(c.columns).length)
1924
+ throw new Error(`This 'columns' property is missing some properties`);
1925
+ const when = Object.entries(c.when).map(([key, value]) => {
1926
+ value = this.$utils.escape(value);
1927
+ value = this._valueTrueFalse(value);
1928
+ return `${this.bindColumn(key)} = '${value}'`;
1929
+ });
1930
+ for (const [key, value] of Object.entries(c.columns)) {
1931
+ if (updateColumns[key] == null)
1932
+ continue;
1933
+ const startIndex = updateColumns[key].indexOf(this.$constants('CASE'));
1934
+ const str = `${this.$constants('WHEN')} ${when.join(` ${this.$constants('AND')} `)} ${this.$constants('THEN')} '${value}'`;
1935
+ updateColumns[key].splice(startIndex + 1, 0, str);
1936
+ }
1937
+ }
1938
+ for (const key in columns) {
1939
+ if (updateColumns[key] == null)
1940
+ continue;
1941
+ columns[key] = `( ${updateColumns[key].join(' ')} )`;
1942
+ }
1943
+ const keyValue = Object.entries(columns).map(([column, value]) => {
1944
+ if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
1945
+ value = this.$utils.escapeActions(value);
1946
+ }
1947
+ return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
1948
+ ? 'NULL'
1949
+ : typeof value === 'string' && value.includes(this.$constants('RAW'))
1950
+ ? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
1951
+ : `'${this.$utils.covertBooleanToNumber(value)}'`}`;
1952
+ });
1953
+ const query = `${this.$constants('SET')} ${keyValue.join(', ')}`;
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');
1960
+ return this;
1961
+ }
1782
1962
  /**
1783
1963
  * The 'toString' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
1784
1964
  *
@@ -1812,7 +1992,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1812
1992
  * @return {string} return table name
1813
1993
  */
1814
1994
  getTableName() {
1815
- return this._getState('TABLE_NAME').replace(/\`/g, '');
1995
+ return this.$state.get('TABLE_NAME').replace(/\`/g, '');
1816
1996
  }
1817
1997
  /**
1818
1998
  * The 'getSchema' method is used to get schema information
@@ -1824,7 +2004,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1824
2004
  `${this.$constants('SHOW')}`,
1825
2005
  `${this.$constants('COLUMNS')}`,
1826
2006
  `${this.$constants('FROM')}`,
1827
- `\`${this._getState('TABLE_NAME').replace(/\`/g, '')}\``
2007
+ `\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
1828
2008
  ].join(' ');
1829
2009
  return yield this._queryStatement(sql);
1830
2010
  });
@@ -1835,8 +2015,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1835
2015
  * @return {string} return table.column
1836
2016
  */
1837
2017
  bindColumn(column) {
1838
- if (!/\./.test(column))
2018
+ if (!/\./.test(column)) {
1839
2019
  return `\`${this.getTableName().replace(/`/g, '')}\`.\`${column.replace(/`/g, '')}\``;
2020
+ }
1840
2021
  const [table, c] = column.split('.');
1841
2022
  return `\`${table.replace(/`/g, '')}\`.\`${c.replace(/`/g, '')}\``;
1842
2023
  }
@@ -1846,7 +2027,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1846
2027
  * @return {this} this this
1847
2028
  */
1848
2029
  debug(debug = true) {
1849
- this._setState('DEBUG', debug);
2030
+ this.$state.set('DEBUG', debug);
1850
2031
  return this;
1851
2032
  }
1852
2033
  /**
@@ -1855,7 +2036,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1855
2036
  * @return {this} this this
1856
2037
  */
1857
2038
  dd(debug = true) {
1858
- this._setState('DEBUG', debug);
2039
+ this.$state.set('DEBUG', debug);
1859
2040
  return this;
1860
2041
  }
1861
2042
  /**
@@ -1866,7 +2047,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1866
2047
  hook(func) {
1867
2048
  if (typeof func !== "function")
1868
2049
  throw new Error(`this '${func}' is not a function`);
1869
- this._setState('HOOKS', [...this._getState('HOOKS'), func]);
2050
+ this.$state.set('HOOKS', [...this.$state.get('HOOKS'), func]);
1870
2051
  return this;
1871
2052
  }
1872
2053
  /**
@@ -1877,7 +2058,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1877
2058
  before(func) {
1878
2059
  if (typeof func !== "function")
1879
2060
  throw new Error(`this '${func}' is not a function`);
1880
- this._setState('HOOKS', [...this._getState('HOOKS'), func]);
2061
+ this.$state.set('HOOKS', [...this.$state.get('HOOKS'), func]);
1881
2062
  return this;
1882
2063
  }
1883
2064
  /**
@@ -1975,12 +2156,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1975
2156
  * @param {number} value
1976
2157
  * @return {promise<any>}
1977
2158
  */
1978
- increment(column = 'id', value = 1) {
1979
- return __awaiter(this, void 0, void 0, function* () {
2159
+ increment() {
2160
+ return __awaiter(this, arguments, void 0, function* (column = 'id', value = 1) {
1980
2161
  const query = `${this.$constants('SET')} ${column} = ${column} + ${value}`;
1981
- this._setState('UPDATE', [
2162
+ this.$state.set('UPDATE', [
1982
2163
  `${this.$constants('UPDATE')}`,
1983
- `${this._getState('TABLE_NAME')}`,
2164
+ `${this.$state.get('TABLE_NAME')}`,
1984
2165
  `${query}`
1985
2166
  ].join(' '));
1986
2167
  return yield this._update(true);
@@ -1993,12 +2174,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1993
2174
  * @param {number} value
1994
2175
  * @return {promise<any>}
1995
2176
  */
1996
- decrement(column = 'id', value = 1) {
1997
- return __awaiter(this, void 0, void 0, function* () {
2177
+ decrement() {
2178
+ return __awaiter(this, arguments, void 0, function* (column = 'id', value = 1) {
1998
2179
  const query = `${this.$constants('SET')} ${column} = ${column} - ${value}`;
1999
- this._setState('UPDATE', [
2180
+ this.$state.set('UPDATE', [
2000
2181
  `${this.$constants('UPDATE')}`,
2001
- `${this._getState('TABLE_NAME')}`,
2182
+ `${this.$state.get('TABLE_NAME')}`,
2002
2183
  `${query}`
2003
2184
  ].join(' '));
2004
2185
  return yield this._update(true);
@@ -2022,7 +2203,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2022
2203
  `${this.$constants('SELECT')}`,
2023
2204
  `*`,
2024
2205
  `${this.$constants('FROM')}`,
2025
- `${this._getState('TABLE_NAME')}`
2206
+ `${this.$state.get('TABLE_NAME')}`
2026
2207
  ].join(' '));
2027
2208
  });
2028
2209
  }
@@ -2039,7 +2220,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2039
2220
  `${this.$constants('SELECT')}`,
2040
2221
  `*`,
2041
2222
  `${this.$constants('FROM')}`,
2042
- `${this._getState('TABLE_NAME')}`,
2223
+ `${this.$state.get('TABLE_NAME')}`,
2043
2224
  `${this.$constants('WHERE')} id = ${id}`
2044
2225
  ].join(' '));
2045
2226
  return (result === null || result === void 0 ? void 0 : result.shift()) || null;
@@ -2056,8 +2237,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2056
2237
  * @return {promise<Pagination>}
2057
2238
  */
2058
2239
  pagination(paginationOptions) {
2059
- var _a, _b, _c;
2060
2240
  return __awaiter(this, void 0, void 0, function* () {
2241
+ var _a, _b, _c;
2061
2242
  let limit = 15;
2062
2243
  let page = 1;
2063
2244
  if (paginationOptions != null) {
@@ -2068,20 +2249,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2068
2249
  const nextPage = currentPage + 1;
2069
2250
  const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
2070
2251
  const offset = (page - 1) * limit;
2252
+ this.limit(limit);
2253
+ this.offset(offset);
2071
2254
  let sql = this._queryBuilder().select();
2072
- if (!sql.includes(this.$constants('LIMIT'))) {
2073
- sql = [
2074
- `${sql}`,
2075
- `${this.$constants('LIMIT')}`,
2076
- `${limit}`,
2077
- `${this.$constants('OFFSET')} ${offset}`
2078
- ].join(' ');
2079
- }
2080
- else {
2081
- sql = sql.replace(this._getState('LIMIT'), `${this.$constants('LIMIT')} ${limit} ${this.$constants('OFFSET')} ${offset}`);
2082
- }
2083
2255
  const result = yield this._queryStatement(sql);
2084
- if ((_a = this._getState('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
2256
+ if ((_a = this.$state.get('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
2085
2257
  this._hiddenColumn(result);
2086
2258
  if (!result.length)
2087
2259
  return {
@@ -2103,8 +2275,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2103
2275
  ].join(' ');
2104
2276
  const sqlTotal = [
2105
2277
  sqlCount,
2106
- this._getState('FROM'),
2107
- this._getState('TABLE_NAME'),
2278
+ this.$state.get('FROM'),
2279
+ this.$state.get('TABLE_NAME'),
2108
2280
  this._queryBuilder().where()
2109
2281
  ].join(' ');
2110
2282
  const count = yield this._queryStatement(sqlTotal);
@@ -2152,30 +2324,45 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2152
2324
  * The 'first' method is used to retrieve the first record that matches the query conditions.
2153
2325
  *
2154
2326
  * It allows you to retrieve a single record from a database table that meets the specified criteria.
2327
+ * @param {Function?} cb callback function return query sql
2155
2328
  * @return {promise<object | null>}
2156
2329
  */
2157
- first() {
2158
- var _a, _b;
2330
+ first(cb) {
2159
2331
  return __awaiter(this, void 0, void 0, function* () {
2160
- if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2332
+ var _a, _b;
2333
+ if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2161
2334
  this.select(...yield this.exceptColumns());
2162
2335
  this.limit(1);
2163
- const sql = this._queryBuilder().select();
2336
+ let sql = this._queryBuilder().select();
2337
+ if (cb) {
2338
+ const callbackSql = cb(sql);
2339
+ if (callbackSql == null || callbackSql === '')
2340
+ throw new Error('Please provide a callback for execution');
2341
+ sql = callbackSql;
2342
+ }
2164
2343
  const result = yield this._queryStatement(sql);
2165
- if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2344
+ if (this.$state.get('VOID'))
2345
+ return null;
2346
+ if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2166
2347
  this._hiddenColumn(result);
2167
- if (this._getState('PLUCK')) {
2168
- const pluck = this._getState('PLUCK');
2348
+ if (this.$state.get('PLUCK')) {
2349
+ const pluck = this.$state.get('PLUCK');
2169
2350
  const newData = result === null || result === void 0 ? void 0 : result.shift();
2170
2351
  const checkProperty = newData.hasOwnProperty(pluck);
2171
2352
  if (!checkProperty)
2172
2353
  throw new Error(`can't find property '${pluck}' of result`);
2173
2354
  const r = newData[pluck] || null;
2174
- yield this.$utils.hookHandle(this._getState('HOOKS'), r);
2355
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), r);
2175
2356
  return r;
2176
2357
  }
2177
- const r = (result === null || result === void 0 ? void 0 : result.shift()) || null;
2178
- yield this.$utils.hookHandle(this._getState('HOOKS'), r);
2358
+ if (this.$state.get('RETURN_TYPE') != null) {
2359
+ const returnType = this.$state.get('RETURN_TYPE');
2360
+ return this._resultHandler(returnType === 'object'
2361
+ ? result[0]
2362
+ : returnType === 'array' ? result : [result]);
2363
+ }
2364
+ const r = result[0] || null;
2365
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), r);
2179
2366
  return this._resultHandler(r);
2180
2367
  });
2181
2368
  }
@@ -2184,11 +2371,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2184
2371
  * The 'findOne' method is used to retrieve the first record that matches the query conditions.
2185
2372
  *
2186
2373
  * It allows you to retrieve a single record from a database table that meets the specified criteria.
2374
+ * @param {Function?} cb callback function return query sql
2187
2375
  * @return {promise<object | null>}
2188
2376
  */
2189
- findOne() {
2377
+ findOne(cb) {
2190
2378
  return __awaiter(this, void 0, void 0, function* () {
2191
- return yield this.first();
2379
+ return yield this.first(cb);
2192
2380
  });
2193
2381
  }
2194
2382
  /**
@@ -2200,20 +2388,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2200
2388
  * @return {promise<object | Error>}
2201
2389
  */
2202
2390
  firstOrError(message, options) {
2203
- var _a, _b;
2204
2391
  return __awaiter(this, void 0, void 0, function* () {
2205
- if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2392
+ var _a, _b;
2393
+ if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2206
2394
  this.select(...yield this.exceptColumns());
2207
2395
  let sql = this._queryBuilder().select();
2208
2396
  if (!sql.includes(this.$constants('LIMIT')))
2209
2397
  sql = `${sql} ${this.$constants('LIMIT')} 1`;
2210
2398
  else
2211
- sql = sql.replace(this._getState('LIMIT'), `${this.$constants('LIMIT')} 1`);
2399
+ sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
2212
2400
  const result = yield this._queryStatement(sql);
2213
- if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2401
+ if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2214
2402
  this._hiddenColumn(result);
2215
- if (this._getState('PLUCK')) {
2216
- const pluck = this._getState('PLUCK');
2403
+ if (this.$state.get('PLUCK')) {
2404
+ const pluck = this.$state.get('PLUCK');
2217
2405
  const newData = result === null || result === void 0 ? void 0 : result.shift();
2218
2406
  const checkProperty = newData.hasOwnProperty(pluck);
2219
2407
  if (!checkProperty)
@@ -2224,7 +2412,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2224
2412
  throw { message, code: 400 };
2225
2413
  throw Object.assign({ message }, options);
2226
2414
  }
2227
- yield this.$utils.hookHandle(this._getState('HOOKS'), data);
2415
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), data);
2228
2416
  return this._resultHandler(data);
2229
2417
  }
2230
2418
  const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
@@ -2234,7 +2422,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2234
2422
  }
2235
2423
  throw Object.assign({ message }, options);
2236
2424
  }
2237
- yield this.$utils.hookHandle(this._getState('HOOKS'), data);
2425
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), data);
2238
2426
  return this._resultHandler(data);
2239
2427
  });
2240
2428
  }
@@ -2256,38 +2444,53 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2256
2444
  * The 'get' method is used to execute a database query and retrieve the result set that matches the query conditions.
2257
2445
  *
2258
2446
  * It retrieves multiple records from a database table based on the criteria specified in the query.
2447
+ * @param {Function?} cb callback function return query sql
2259
2448
  * @return {promise<any[]>}
2260
2449
  */
2261
- get() {
2262
- var _a, _b;
2450
+ get(cb) {
2263
2451
  return __awaiter(this, void 0, void 0, function* () {
2264
- if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2452
+ var _a, _b;
2453
+ if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2265
2454
  this.select(...yield this.exceptColumns());
2266
- const sql = this._queryBuilder().select();
2455
+ let sql = this._queryBuilder().select();
2456
+ if (cb) {
2457
+ const callbackSql = cb(sql);
2458
+ if (callbackSql == null || callbackSql === '')
2459
+ throw new Error('Please provide a callback for execution');
2460
+ sql = callbackSql;
2461
+ }
2267
2462
  const result = yield this._queryStatement(sql);
2268
- if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2463
+ if (this.$state.get('VOID'))
2464
+ return [];
2465
+ if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
2269
2466
  this._hiddenColumn(result);
2270
- if (this._getState('CHUNK')) {
2467
+ if (this.$state.get('CHUNK')) {
2271
2468
  const data = result.reduce((resultArray, item, index) => {
2272
- const chunkIndex = Math.floor(index / this._getState('CHUNK'));
2469
+ const chunkIndex = Math.floor(index / this.$state.get('CHUNK'));
2273
2470
  if (!resultArray[chunkIndex])
2274
2471
  resultArray[chunkIndex] = [];
2275
2472
  resultArray[chunkIndex].push(item);
2276
2473
  return resultArray;
2277
2474
  }, []);
2278
- yield this.$utils.hookHandle(this._getState('HOOKS'), data || []);
2475
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), data || []);
2279
2476
  return this._resultHandler(data || []);
2280
2477
  }
2281
- if (this._getState('PLUCK')) {
2282
- const pluck = this._getState('PLUCK');
2478
+ if (this.$state.get('PLUCK')) {
2479
+ const pluck = this.$state.get('PLUCK');
2283
2480
  const newData = result.map((d) => d[pluck]);
2284
2481
  if (newData.every((d) => d == null)) {
2285
2482
  throw new Error(`can't find property '${pluck}' of result`);
2286
2483
  }
2287
- yield this.$utils.hookHandle(this._getState('HOOKS'), newData || []);
2484
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), newData || []);
2288
2485
  return this._resultHandler(newData || []);
2289
2486
  }
2290
- yield this.$utils.hookHandle(this._getState('HOOKS'), result || []);
2487
+ yield this.$utils.hookHandle(this.$state.get('HOOKS'), result || []);
2488
+ if (this.$state.get('RETURN_TYPE') != null) {
2489
+ const returnType = this.$state.get('RETURN_TYPE');
2490
+ return this._resultHandler(returnType === 'object'
2491
+ ? result[0]
2492
+ : returnType === 'array' ? result : [result]);
2493
+ }
2291
2494
  return this._resultHandler(result || []);
2292
2495
  });
2293
2496
  }
@@ -2295,11 +2498,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2295
2498
  * The 'findMany' method is used to execute a database query and retrieve the result set that matches the query conditions.
2296
2499
  *
2297
2500
  * It retrieves multiple records from a database table based on the criteria specified in the query.
2501
+ * @param {Function?} cb callback function return query sql
2298
2502
  * @return {promise<any[]>}
2299
2503
  */
2300
- findMany() {
2504
+ findMany(cb) {
2301
2505
  return __awaiter(this, void 0, void 0, function* () {
2302
- return yield this.get();
2506
+ return yield this.get(cb);
2303
2507
  });
2304
2508
  }
2305
2509
  /**
@@ -2315,7 +2519,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2315
2519
  return __awaiter(this, void 0, void 0, function* () {
2316
2520
  const sql = this._queryBuilder().select();
2317
2521
  const result = yield this._queryStatement(sql);
2318
- if (this._getState('HIDDEN').length)
2522
+ if (this.$state.get('HIDDEN').length)
2319
2523
  this._hiddenColumn(result);
2320
2524
  return this._resultHandler(JSON.stringify(result));
2321
2525
  });
@@ -2329,9 +2533,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2329
2533
  * @param {string=} column [column=id]
2330
2534
  * @return {promise<Array>}
2331
2535
  */
2332
- toArray(column = 'id') {
2333
- return __awaiter(this, void 0, void 0, function* () {
2334
- this.selectRaw(column);
2536
+ toArray() {
2537
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2538
+ this.selectRaw(`${this.bindColumn(column)}`);
2335
2539
  const sql = this._queryBuilder().select();
2336
2540
  const result = yield this._queryStatement(sql);
2337
2541
  const toArray = result.map((data) => data[column]);
@@ -2345,8 +2549,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2345
2549
  * @return {promise<boolean>}
2346
2550
  */
2347
2551
  exists() {
2348
- var _a;
2349
2552
  return __awaiter(this, void 0, void 0, function* () {
2553
+ var _a;
2350
2554
  this.limit(1);
2351
2555
  this.selectRaw('1');
2352
2556
  const sql = this._queryBuilder().select();
@@ -2366,12 +2570,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2366
2570
  * @param {string=} column [column=id]
2367
2571
  * @return {promise<number>}
2368
2572
  */
2369
- count(column = 'id') {
2370
- return __awaiter(this, void 0, void 0, function* () {
2371
- const distinct = this._getState('DISTINCT');
2573
+ count() {
2574
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2575
+ const distinct = this.$state.get('DISTINCT');
2372
2576
  column = distinct
2373
- ? `${this.$constants('DISTINCT')} \`${column}\``
2374
- : `\`${column}\``;
2577
+ ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}`
2578
+ : `${this.bindColumn(column)}`;
2375
2579
  this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2376
2580
  const sql = this._queryBuilder().select();
2377
2581
  const result = yield this._queryStatement(sql);
@@ -2385,10 +2589,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2385
2589
  * @param {string=} column [column=id]
2386
2590
  * @return {promise<number>}
2387
2591
  */
2388
- avg(column = 'id') {
2389
- return __awaiter(this, void 0, void 0, function* () {
2390
- const distinct = this._getState('DISTINCT');
2391
- column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2592
+ avg() {
2593
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2594
+ const distinct = this.$state.get('DISTINCT');
2595
+ column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2392
2596
  this.selectRaw(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2393
2597
  const sql = this._queryBuilder().select();
2394
2598
  const result = yield this._queryStatement(sql);
@@ -2402,10 +2606,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2402
2606
  * @param {string=} column [column=id]
2403
2607
  * @return {promise<number>}
2404
2608
  */
2405
- sum(column = 'id') {
2406
- return __awaiter(this, void 0, void 0, function* () {
2407
- const distinct = this._getState('DISTINCT');
2408
- column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2609
+ sum() {
2610
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2611
+ const distinct = this.$state.get('DISTINCT');
2612
+ column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2409
2613
  this.selectRaw(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2410
2614
  const sql = this._queryBuilder().select();
2411
2615
  const result = yield this._queryStatement(sql);
@@ -2419,11 +2623,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2419
2623
  * @param {string=} column [column=id]
2420
2624
  * @return {promise<number>}
2421
2625
  */
2422
- max(column = 'id') {
2423
- var _a;
2424
- return __awaiter(this, void 0, void 0, function* () {
2425
- const distinct = this._getState('DISTINCT');
2426
- column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2626
+ max() {
2627
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2628
+ var _a;
2629
+ const distinct = this.$state.get('DISTINCT');
2630
+ column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2427
2631
  this.selectRaw(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2428
2632
  const sql = this._queryBuilder().select();
2429
2633
  const result = yield this._queryStatement(sql);
@@ -2437,11 +2641,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2437
2641
  * @param {string=} column [column=id]
2438
2642
  * @return {promise<number>}
2439
2643
  */
2440
- min(column = 'id') {
2441
- var _a;
2442
- return __awaiter(this, void 0, void 0, function* () {
2443
- const distinct = this._getState('DISTINCT');
2444
- column = distinct ? `${this.$constants('DISTINCT')} \`${column}\`` : `\`${column}\``;
2644
+ min() {
2645
+ return __awaiter(this, arguments, void 0, function* (column = 'id') {
2646
+ var _a;
2647
+ const distinct = this.$state.get('DISTINCT');
2648
+ column = distinct ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}` : `${this.bindColumn(column)}`;
2445
2649
  this.selectRaw(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2446
2650
  const sql = this._queryBuilder().select();
2447
2651
  const result = yield this._queryStatement(sql);
@@ -2455,16 +2659,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2455
2659
  * @return {promise<boolean>}
2456
2660
  */
2457
2661
  delete() {
2458
- var _a;
2459
2662
  return __awaiter(this, void 0, void 0, function* () {
2460
- if (!this._getState('where').length) {
2663
+ var _a;
2664
+ if (!this.$state.get('where').length) {
2461
2665
  throw new Error("can't delete without where condition");
2462
2666
  }
2463
2667
  this.limit(1);
2464
- this._setState('DELETE', [
2668
+ this.$state.set('DELETE', [
2465
2669
  `${this.$constants('DELETE')}`,
2466
- `${this._getState('FROM')}`,
2467
- `${this._getState('TABLE_NAME')}`,
2670
+ `${this.$state.get('FROM')}`,
2671
+ `${this.$state.get('TABLE_NAME')}`,
2468
2672
  ].join(' '));
2469
2673
  const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2470
2674
  if (result)
@@ -2479,15 +2683,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2479
2683
  * @return {promise<boolean>}
2480
2684
  */
2481
2685
  deleteMany() {
2482
- var _a;
2483
2686
  return __awaiter(this, void 0, void 0, function* () {
2484
- if (!this._getState('where').length) {
2687
+ var _a;
2688
+ if (!this.$state.get('where').length) {
2485
2689
  throw new Error("can't delete without where condition");
2486
2690
  }
2487
- this._setState('DELETE', [
2691
+ this.$state.set('DELETE', [
2488
2692
  `${this.$constants('DELETE')}`,
2489
- `${this._getState('FROM')}`,
2490
- `${this._getState('TABLE_NAME')}`,
2693
+ `${this.$state.get('FROM')}`,
2694
+ `${this.$state.get('TABLE_NAME')}`,
2491
2695
  ].join(' '));
2492
2696
  const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2493
2697
  if (result)
@@ -2505,13 +2709,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2505
2709
  * @return {promise<boolean>}
2506
2710
  */
2507
2711
  forceDelete() {
2508
- var _a, _b;
2509
2712
  return __awaiter(this, void 0, void 0, function* () {
2510
- this._setState('DELETE', [
2713
+ var _a, _b;
2714
+ this.$state.set('DELETE', [
2511
2715
  `${this.$constants('DELETE')}`,
2512
- `${this._getState('FROM')}`,
2513
- `${this._getState('TABLE_NAME')}`,
2514
- `${this._queryBuilder().where()}`
2716
+ `${this.$state.get('FROM')}`,
2717
+ `${this.$state.get('TABLE_NAME')}`
2515
2718
  ].join(' '));
2516
2719
  const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2517
2720
  if (result)
@@ -2548,7 +2751,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2548
2751
  `${this.$constants('SELECT')}`,
2549
2752
  `*`,
2550
2753
  `${this.$constants('FROM')}`,
2551
- `${this._getState('TABLE_NAME')}`,
2754
+ `${this.$state.get('TABLE_NAME')}`,
2552
2755
  `${this.$constants('WHERE')} id`,
2553
2756
  `${this.$constants('IN')}`,
2554
2757
  `(${data.map((a) => `\'${a}\'`).join(',') || ['0']})`
@@ -2588,22 +2791,125 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2588
2791
  */
2589
2792
  save() {
2590
2793
  return __awaiter(this, void 0, void 0, function* () {
2591
- switch (this._getState('SAVE')) {
2794
+ switch (this.$state.get('SAVE')) {
2592
2795
  case 'INSERT_MULTIPLE': return yield this._insertMultiple();
2593
2796
  case 'INSERT': return yield this._insert();
2594
2797
  case 'UPDATE': return yield this._update();
2595
2798
  case 'INSERT_NOT_EXISTS': return yield this._insertNotExists();
2596
2799
  case 'UPDATE_OR_INSERT': return yield this._updateOrInsert();
2597
2800
  case 'INSERT_OR_SELECT': return yield this._insertOrSelect();
2598
- default: throw new Error(`unknown this [${this._getState('SAVE')}]`);
2801
+ default: throw new Error(`unknown this [${this.$state.get('SAVE')}]`);
2599
2802
  }
2600
2803
  });
2601
2804
  }
2602
2805
  /**
2603
- * The 'showTables' method is used to show schema table.
2604
- *
2605
- * @return {Promise<Array>}
2606
- */
2806
+ *
2807
+ * The 'makeSelectStatement' method is used to make select statement.
2808
+ * @return {Promise<string>} string
2809
+ */
2810
+ makeSelectStatement() {
2811
+ return __awaiter(this, void 0, void 0, function* () {
2812
+ const makeStatement = (columns) => {
2813
+ return [
2814
+ `${this.$constants('SELECT')}`,
2815
+ `${columns.join(', ')}`,
2816
+ `${this.$constants('FROM')}`,
2817
+ `\`${this.getTableName()}\``,
2818
+ ].join(' ');
2819
+ };
2820
+ const schemaTable = yield this.getSchema();
2821
+ const columns = schemaTable.map(column => this.bindColumn(column.Field));
2822
+ return makeStatement(columns);
2823
+ });
2824
+ }
2825
+ /**
2826
+ *
2827
+ * The 'makeInsertStatement' method is used to make insert table statement.
2828
+ * @return {Promise<string>} string
2829
+ */
2830
+ makeInsertStatement() {
2831
+ return __awaiter(this, void 0, void 0, function* () {
2832
+ const makeStatement = (columns) => {
2833
+ return [
2834
+ `${this.$constants('INSERT')}`,
2835
+ `\`${this.getTableName()}\``,
2836
+ `(${columns.join(', ')})`,
2837
+ `${this.$constants('VALUES')}`,
2838
+ `(${Array(columns.length).fill('`?`').join(' , ')})`
2839
+ ].join(' ');
2840
+ };
2841
+ const schemaTable = yield this.getSchema();
2842
+ const columns = schemaTable.map(column => `\`${column.Field}\``);
2843
+ return makeStatement(columns);
2844
+ });
2845
+ }
2846
+ /**
2847
+ *
2848
+ * The 'makeUpdateStatement' method is used to make update table statement.
2849
+ * @return {Promise<string>} string
2850
+ */
2851
+ makeUpdateStatement() {
2852
+ return __awaiter(this, void 0, void 0, function* () {
2853
+ const makeStatement = (columns) => {
2854
+ return [
2855
+ `${this.$constants('UPDATE')}`,
2856
+ `\`${this.getTableName()}\``,
2857
+ `${this.$constants('SET')}`,
2858
+ `(${columns.join(', ')})`,
2859
+ `${this.$constants('WHERE')}`,
2860
+ `${this.bindColumn('id')} = '?'`
2861
+ ].join(' ');
2862
+ };
2863
+ const schemaTable = yield this.getSchema();
2864
+ const columns = schemaTable.map(column => `${this.bindColumn(column.Field)} = '?'`);
2865
+ return makeStatement(columns);
2866
+ });
2867
+ }
2868
+ /**
2869
+ *
2870
+ * The 'makeDeleteStatement' method is used to make delete statement.
2871
+ * @return {Promise<string>} string
2872
+ */
2873
+ makeDeleteStatement() {
2874
+ return __awaiter(this, void 0, void 0, function* () {
2875
+ const makeStatement = () => {
2876
+ return [
2877
+ `${this.$constants('DELETE')}`,
2878
+ `${this.$constants('FROM')}`,
2879
+ `\`${this.getTableName()}\``,
2880
+ `${this.$constants('WHERE')}`,
2881
+ `${this.bindColumn('id')} = \`?\``
2882
+ ].join(' ');
2883
+ };
2884
+ return makeStatement();
2885
+ });
2886
+ }
2887
+ /**
2888
+ *
2889
+ * The 'makeCreateTableStatement' method is used to make create table statement.
2890
+ * @return {Promise<string>} string
2891
+ */
2892
+ makeCreateTableStatement() {
2893
+ return __awaiter(this, void 0, void 0, function* () {
2894
+ const makeStatement = (columns) => {
2895
+ return [
2896
+ `${this.$constants('CREATE_TABLE_NOT_EXISTS')}`,
2897
+ `\`${this.getTableName()}\``,
2898
+ `(`,
2899
+ `\n${columns === null || columns === void 0 ? void 0 : columns.join(',\n')}`,
2900
+ `\n)`,
2901
+ `${this.$constants('ENGINE')}`
2902
+ ].join(' ');
2903
+ };
2904
+ const columns = yield this.showSchema();
2905
+ return makeStatement(columns);
2906
+ });
2907
+ }
2908
+ /**
2909
+ * The 'showTables' method is used to show schema table.
2910
+ *
2911
+ * @return {Promise<Array>}
2912
+ */
2607
2913
  showTables() {
2608
2914
  return __awaiter(this, void 0, void 0, function* () {
2609
2915
  const sql = [
@@ -2623,8 +2929,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2623
2929
  * @param {string=} table table name
2624
2930
  * @return {Promise<Array>}
2625
2931
  */
2626
- showColumns(table = this._getState('TABLE_NAME')) {
2627
- return __awaiter(this, void 0, void 0, function* () {
2932
+ showColumns() {
2933
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2628
2934
  const sql = [
2629
2935
  `${this.$constants('SHOW')}`,
2630
2936
  `${this.$constants('COLUMNS')}`,
@@ -2642,8 +2948,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2642
2948
  * @param {string=} table [table= current table name]
2643
2949
  * @return {Promise<Array>}
2644
2950
  */
2645
- showSchema(table = this._getState('TABLE_NAME')) {
2646
- return __awaiter(this, void 0, void 0, function* () {
2951
+ showSchema() {
2952
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2647
2953
  const sql = [
2648
2954
  `${this.$constants('SHOW')}`,
2649
2955
  `${this.$constants('COLUMNS')}`,
@@ -2683,8 +2989,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2683
2989
  * @param {string=} table [table= current table name]
2684
2990
  * @return {Promise<Array>}
2685
2991
  */
2686
- showSchemas(table = this._getState('TABLE_NAME')) {
2687
- return __awaiter(this, void 0, void 0, function* () {
2992
+ showSchemas() {
2993
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2688
2994
  return this.showSchema(table);
2689
2995
  });
2690
2996
  }
@@ -2695,8 +3001,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2695
3001
  * @param {string=} table table name
2696
3002
  * @return {Promise<Array>}
2697
3003
  */
2698
- showValues(table = this._getState('TABLE_NAME')) {
2699
- return __awaiter(this, void 0, void 0, function* () {
3004
+ showValues() {
3005
+ return __awaiter(this, arguments, void 0, function* (table = this.$state.get('TABLE_NAME')) {
2700
3006
  const sql = [
2701
3007
  `${this.$constants('SELECT')}`,
2702
3008
  '*',
@@ -2729,11 +3035,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2729
3035
  `${this.$constants('SHOW')}`,
2730
3036
  `${this.$constants('FIELDS')}`,
2731
3037
  `${this.$constants('FROM')}`,
2732
- `${this._getState('TABLE_NAME')}`
3038
+ `${this.$state.get('TABLE_NAME')}`
2733
3039
  ].join(' ');
2734
3040
  const fields = yield this._queryStatement(sql);
2735
3041
  for (let row = 0; row < rows; row++) {
2736
- if (this._getState('TABLE_NAME') === '' || this._getState('TABLE_NAME') == null) {
3042
+ if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
2737
3043
  throw new Error("Unknow this table name");
2738
3044
  }
2739
3045
  let columnAndValue = {};
@@ -2763,7 +3069,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2763
3069
  return __awaiter(this, void 0, void 0, function* () {
2764
3070
  const sql = [
2765
3071
  `${this.$constants('TRUNCATE_TABLE')}`,
2766
- `${this._getState('TABLE_NAME')}`
3072
+ `${this.$state.get('TABLE_NAME')}`
2767
3073
  ].join(' ');
2768
3074
  yield this._queryStatement(sql);
2769
3075
  return true;
@@ -2778,7 +3084,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2778
3084
  return __awaiter(this, void 0, void 0, function* () {
2779
3085
  const sql = [
2780
3086
  `${this.$constants('DROP_TABLE')}`,
2781
- `${this._getState('TABLE_NAME')}`
3087
+ `${this.$state.get('TABLE_NAME')}`
2782
3088
  ].join(' ');
2783
3089
  yield this._queryStatement(sql);
2784
3090
  return true;
@@ -2786,14 +3092,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2786
3092
  }
2787
3093
  exceptColumns() {
2788
3094
  return __awaiter(this, void 0, void 0, function* () {
2789
- const excepts = this._getState('EXCEPTS');
3095
+ const excepts = this.$state.get('EXCEPTS');
2790
3096
  const hasDot = excepts.some((except) => /\./.test(except));
2791
3097
  const names = excepts.map((except) => {
2792
3098
  if (/\./.test(except))
2793
3099
  return except.split('.')[0];
2794
3100
  return null;
2795
3101
  }).filter((d) => d != null);
2796
- const tableNames = names.length ? [...new Set(names)] : [this._getState('TABLE_NAME')];
3102
+ const tableNames = names.length ? [...new Set(names)] : [this.$state.get('TABLE_NAME')];
2797
3103
  const removeExcepts = [];
2798
3104
  for (const tableName of tableNames) {
2799
3105
  const sql = [
@@ -2862,7 +3168,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2862
3168
  const buildSQL = (sql) => sql.filter(s => s !== '' || s == null).join(' ').replace(/\s+/g, ' ');
2863
3169
  const bindSelect = (values) => {
2864
3170
  if (!values.length) {
2865
- if (!this._getState('DISTINCT'))
3171
+ if (!this.$state.get('DISTINCT'))
2866
3172
  return `${this.$constants('SELECT')} *`;
2867
3173
  return `${this.$constants('SELECT')} ${this.$constants('DISTINCT')} *`;
2868
3174
  }
@@ -2874,35 +3180,51 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2874
3180
  return `${this.$constants('SELECT')} ${values.join(', ')}`;
2875
3181
  };
2876
3182
  const bindJoin = (values) => {
2877
- if (!values.length)
3183
+ if (!Array.isArray(values) || !values.length)
2878
3184
  return null;
2879
3185
  return values.join(' ');
2880
3186
  };
2881
3187
  const bindWhere = (values) => {
2882
- if (!values.length)
3188
+ if (!Array.isArray(values) || !values.length)
2883
3189
  return null;
2884
3190
  return `${this.$constants('WHERE')} ${values.map(v => v.replace(/^\s/, '').replace(/\s+/g, ' ')).join(' ')}`;
2885
3191
  };
2886
3192
  const bindOrderBy = (values) => {
2887
- if (!values.length)
3193
+ if (!Array.isArray(values) || !values.length)
2888
3194
  return null;
2889
3195
  return `${this.$constants('ORDER_BY')} ${values.map(v => v.replace(/^\s/, '').replace(/\s+/g, ' ')).join(', ')}`;
2890
3196
  };
2891
- const select = () => buildSQL([
2892
- bindSelect(this.$state.get('SELECT')),
2893
- this.$state.get('FROM'),
2894
- this.$state.get('TABLE_NAME'),
2895
- bindJoin(this.$state.get('JOIN')),
2896
- bindWhere(this.$state.get('WHERE')),
2897
- this.$state.get('GROUP_BY'),
2898
- this.$state.get('HAVING'),
2899
- bindOrderBy(this.$state.get('ORDER_BY')),
2900
- this.$state.get('LIMIT'),
2901
- this.$state.get('OFFSET')
2902
- ]);
3197
+ const select = () => {
3198
+ return buildSQL([
3199
+ bindSelect(this.$state.get('SELECT')),
3200
+ this.$state.get('FROM'),
3201
+ this.$state.get('TABLE_NAME'),
3202
+ bindJoin(this.$state.get('JOIN')),
3203
+ bindWhere(this.$state.get('WHERE')),
3204
+ this.$state.get('GROUP_BY'),
3205
+ this.$state.get('HAVING'),
3206
+ bindOrderBy(this.$state.get('ORDER_BY')),
3207
+ this.$state.get('LIMIT'),
3208
+ this.$state.get('OFFSET')
3209
+ ]);
3210
+ };
2903
3211
  const insert = () => buildSQL([this.$state.get('INSERT')]);
2904
- const update = () => buildSQL([this.$state.get('UPDATE'), bindWhere(this.$state.get('WHERE')), bindOrderBy(this.$state.get('ORDER_BY')), this.$state.get('LIMIT')]);
2905
- const remove = () => buildSQL([this.$state.get('DELETE'), bindWhere(this.$state.get('WHERE')), bindOrderBy(this.$state.get('ORDER_BY')), this.$state.get('LIMIT')]);
3212
+ const update = () => {
3213
+ return buildSQL([
3214
+ this.$state.get('UPDATE'),
3215
+ bindWhere(this.$state.get('WHERE')),
3216
+ bindOrderBy(this.$state.get('ORDER_BY')),
3217
+ this.$state.get('LIMIT')
3218
+ ]);
3219
+ };
3220
+ const remove = () => {
3221
+ return buildSQL([
3222
+ this.$state.get('DELETE'),
3223
+ bindWhere(this.$state.get('WHERE')),
3224
+ bindOrderBy(this.$state.get('ORDER_BY')),
3225
+ this.$state.get('LIMIT')
3226
+ ]);
3227
+ };
2906
3228
  return {
2907
3229
  select,
2908
3230
  insert,
@@ -2920,23 +3242,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2920
3242
  }
2921
3243
  };
2922
3244
  }
2923
- _getState(key) {
2924
- return this.$state.get(key.toLocaleUpperCase());
2925
- }
2926
- _setState(key, value) {
2927
- return this.$state.set(key, value);
2928
- }
2929
3245
  _resultHandler(data) {
2930
- this._setState('RESULT', data);
2931
- this.$state.resetState();
3246
+ this.$state.set('RESULT', data);
3247
+ this.$state.reset();
2932
3248
  this.$logger.reset();
2933
3249
  return data;
2934
3250
  }
2935
3251
  whereReference(tableAndLocalKey, tableAndForeignKey) {
2936
- this._setState('WHERE', [
2937
- ...this._getState('WHERE'),
3252
+ this.$state.set('WHERE', [
3253
+ ...this.$state.get('WHERE'),
2938
3254
  [
2939
- this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
3255
+ this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
2940
3256
  `${tableAndLocalKey} = ${tableAndForeignKey}`
2941
3257
  ].join(' ')
2942
3258
  ]);
@@ -2944,15 +3260,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2944
3260
  }
2945
3261
  _queryStatement(sql) {
2946
3262
  return __awaiter(this, void 0, void 0, function* () {
2947
- if (this._getState('DEBUG'))
3263
+ if (this.$state.get('DEBUG'))
2948
3264
  this.$utils.consoleDebug(sql);
2949
3265
  const result = yield this.$pool.query(sql);
2950
3266
  return result;
2951
3267
  });
2952
3268
  }
2953
- _actionStatement({ sql, returnId = false }) {
2954
- return __awaiter(this, void 0, void 0, function* () {
2955
- if (this._getState('DEBUG'))
3269
+ _actionStatement(_a) {
3270
+ return __awaiter(this, arguments, void 0, function* ({ sql, returnId = false }) {
3271
+ if (this.$state.get('DEBUG'))
2956
3272
  this.$utils.consoleDebug(sql);
2957
3273
  if (returnId) {
2958
3274
  const result = yield this.$pool.query(sql);
@@ -2964,14 +3280,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2964
3280
  }
2965
3281
  _insertNotExists() {
2966
3282
  return __awaiter(this, void 0, void 0, function* () {
2967
- if (!this._getState('where').length)
3283
+ if (!this.$state.get('where').length)
2968
3284
  throw new Error("Can't insert not exists without where condition");
2969
3285
  let sql = [
2970
3286
  `${this.$constants('SELECT')}`,
2971
3287
  `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
2972
3288
  `*`,
2973
- `${this._getState('FROM')}`,
2974
- `${this._getState('TABLE_NAME')}`,
3289
+ `${this.$state.get('FROM')}`,
3290
+ `${this.$state.get('TABLE_NAME')}`,
2975
3291
  `${this._queryBuilder().where()}`,
2976
3292
  `${this.$constants('LIMIT')} 1)`,
2977
3293
  `${this.$constants('AS')} 'exists'`
@@ -2981,10 +3297,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2981
3297
  switch (check) {
2982
3298
  case false: {
2983
3299
  const [result, id] = yield this._actionStatement({
2984
- sql: this._getState('INSERT'),
3300
+ sql: this.$state.get('INSERT'),
2985
3301
  returnId: true
2986
3302
  });
2987
- if (this._getState('VOID') || !result)
3303
+ if (this.$state.get('VOID') || !result)
2988
3304
  return this._resultHandler(undefined);
2989
3305
  const sql = new Builder()
2990
3306
  .copyBuilder(this, { select: true })
@@ -3000,10 +3316,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3000
3316
  _insert() {
3001
3317
  return __awaiter(this, void 0, void 0, function* () {
3002
3318
  const [result, id] = yield this._actionStatement({
3003
- sql: this._getState('INSERT'),
3319
+ sql: this.$state.get('INSERT'),
3004
3320
  returnId: true
3005
3321
  });
3006
- if (this._getState('VOID') || !result)
3322
+ if (this.$state.get('VOID') || !result)
3007
3323
  return this._resultHandler(undefined);
3008
3324
  const sql = new Builder()
3009
3325
  .copyBuilder(this, { select: true })
@@ -3011,14 +3327,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3011
3327
  .toString();
3012
3328
  const data = yield this._queryStatement(sql);
3013
3329
  const resultData = (data === null || data === void 0 ? void 0 : data.shift()) || null;
3014
- this._setState('RESULT', resultData);
3330
+ this.$state.set('RESULT', resultData);
3015
3331
  return this._resultHandler(resultData);
3016
3332
  });
3017
3333
  }
3018
3334
  _checkValueHasRaw(value) {
3019
3335
  return typeof value === 'string' && value.startsWith(this.$constants('RAW'))
3020
- ? value.replace(`${this.$constants('RAW')} `, '').replace(this.$constants('RAW'), '')
3021
- : `'${value}'`;
3336
+ ? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
3337
+ : `'${this.$utils.covertBooleanToNumber(value)}'`;
3022
3338
  }
3023
3339
  _insertMultiple() {
3024
3340
  return __awaiter(this, void 0, void 0, function* () {
@@ -3026,7 +3342,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3026
3342
  sql: this._queryBuilder().insert(),
3027
3343
  returnId: true
3028
3344
  });
3029
- if (this._getState('VOID') || !result)
3345
+ if (this.$state.get('VOID') || !result)
3030
3346
  return this._resultHandler(undefined);
3031
3347
  const arrayId = [...Array(result)].map((_, i) => i + id);
3032
3348
  const sql = new Builder()
@@ -3040,15 +3356,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3040
3356
  }
3041
3357
  _insertOrSelect() {
3042
3358
  return __awaiter(this, void 0, void 0, function* () {
3043
- if (!this._getState('where').length) {
3359
+ if (!this.$state.get('where').length) {
3044
3360
  throw new Error("Can't create or select without where condition");
3045
3361
  }
3046
3362
  let sql = [
3047
3363
  `${this.$constants('SELECT')}`,
3048
3364
  `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
3049
3365
  `1`,
3050
- `${this._getState('FROM')}`,
3051
- `${this._getState('TABLE_NAME')}`,
3366
+ `${this.$state.get('FROM')}`,
3367
+ `${this.$state.get('TABLE_NAME')}`,
3052
3368
  `${this._queryBuilder().where()}`,
3053
3369
  `${this.$constants('LIMIT')} 1)`,
3054
3370
  `${this.$constants('AS')} 'exists'`
@@ -3062,7 +3378,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3062
3378
  sql: this._queryBuilder().insert(),
3063
3379
  returnId: true
3064
3380
  });
3065
- if (this._getState('VOID') || !result)
3381
+ if (this.$state.get('VOID') || !result)
3066
3382
  return this._resultHandler(undefined);
3067
3383
  const sql = new Builder()
3068
3384
  .copyBuilder(this, { select: true })
@@ -3094,15 +3410,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3094
3410
  }
3095
3411
  _updateOrInsert() {
3096
3412
  return __awaiter(this, void 0, void 0, function* () {
3097
- if (!this._getState('where').length) {
3413
+ if (!this.$state.get('where').length) {
3098
3414
  throw new Error("Can't update or insert without where condition");
3099
3415
  }
3100
3416
  let sql = [
3101
3417
  `${this.$constants('SELECT')}`,
3102
3418
  `${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
3103
3419
  `1`,
3104
- `${this._getState('FROM')}`,
3105
- `${this._getState('TABLE_NAME')}`,
3420
+ `${this.$state.get('FROM')}`,
3421
+ `${this.$state.get('TABLE_NAME')}`,
3106
3422
  `${this._queryBuilder().where()}`,
3107
3423
  `${this.$constants('LIMIT')} 1)`,
3108
3424
  `${this.$constants('AS')} 'exists'`
@@ -3116,7 +3432,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3116
3432
  sql: this._queryBuilder().insert(),
3117
3433
  returnId: true
3118
3434
  });
3119
- if (this._getState('VOID') || !result)
3435
+ if (this.$state.get('VOID') || !result)
3120
3436
  return this._resultHandler(undefined);
3121
3437
  const sql = new Builder()
3122
3438
  .copyBuilder(this, { select: true })
@@ -3130,7 +3446,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3130
3446
  const result = yield this._actionStatement({
3131
3447
  sql: this._queryBuilder().update()
3132
3448
  });
3133
- if (this._getState('VOID') || !result)
3449
+ if (this.$state.get('VOID') || !result)
3134
3450
  return this._resultHandler(null);
3135
3451
  const data = yield this._queryStatement(new Builder().copyBuilder(this, { select: true, where: true }).toString());
3136
3452
  if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
@@ -3148,14 +3464,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3148
3464
  }
3149
3465
  });
3150
3466
  }
3151
- _update(ignoreWhere = false) {
3152
- return __awaiter(this, void 0, void 0, function* () {
3153
- if (!this._getState('where').length && !ignoreWhere)
3467
+ _update() {
3468
+ return __awaiter(this, arguments, void 0, function* (ignoreWhere = false) {
3469
+ if (!this.$state.get('where').length && !ignoreWhere)
3154
3470
  throw new Error("can't update without where condition");
3155
3471
  const result = yield this._actionStatement({
3156
3472
  sql: this._queryBuilder().update()
3157
3473
  });
3158
- if (this._getState('VOID') || !result)
3474
+ if (this.$state.get('VOID') || !result)
3159
3475
  return this._resultHandler(undefined);
3160
3476
  const sql = this._queryBuilder().select();
3161
3477
  const data = yield this._queryStatement(sql);
@@ -3167,7 +3483,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3167
3483
  }
3168
3484
  _hiddenColumn(data) {
3169
3485
  var _a;
3170
- const hidden = this._getState('HIDDEN');
3486
+ const hidden = this.$state.get('HIDDEN');
3171
3487
  if ((_a = Object.keys(data)) === null || _a === void 0 ? void 0 : _a.length) {
3172
3488
  hidden.forEach((column) => {
3173
3489
  data.forEach((objColumn) => {
@@ -3180,27 +3496,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3180
3496
  _queryUpdate(data) {
3181
3497
  this.$utils.covertDataToDateIfDate(data);
3182
3498
  const values = Object.entries(data).map(([column, value]) => {
3183
- if (typeof value === 'string' && !(value.includes(this.$constants('RAW'))))
3184
- value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
3185
- return `\`${column}\` = ${value == null || value === 'NULL'
3499
+ if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
3500
+ value = this.$utils.escapeActions(value);
3501
+ }
3502
+ return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
3186
3503
  ? 'NULL'
3187
- : typeof value === 'string' && value.includes(this.$constants('RAW'))
3188
- ? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
3189
- : `'${this.$utils.covertBooleanToNumber(value)}'`}`;
3504
+ : this._checkValueHasRaw(value)}`;
3190
3505
  });
3191
3506
  return `${this.$constants('SET')} ${values}`;
3192
3507
  }
3193
3508
  _queryInsert(data) {
3194
3509
  this.$utils.covertDataToDateIfDate(data);
3195
- const columns = Object.keys(data).map((column) => `\`${column}\``);
3510
+ const columns = Object.keys(data).map((column) => this.bindColumn(column));
3196
3511
  const values = Object.values(data).map((value) => {
3197
- if (typeof value === 'string' && !(value.includes(this.$constants('RAW'))))
3198
- value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
3512
+ if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
3513
+ value = this.$utils.escapeActions(value);
3514
+ }
3199
3515
  return `${value == null || value === 'NULL'
3200
3516
  ? 'NULL'
3201
- : typeof value === 'string' && value.includes(this.$constants('RAW'))
3202
- ? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
3203
- : `'${this.$utils.covertBooleanToNumber(value)}'`}`;
3517
+ : this._checkValueHasRaw(value)}`;
3204
3518
  });
3205
3519
  return [
3206
3520
  `(${columns})`,
@@ -3214,17 +3528,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3214
3528
  for (let objects of data) {
3215
3529
  this.$utils.covertDataToDateIfDate(data);
3216
3530
  const vals = Object.values(objects).map((value) => {
3217
- if (typeof value === 'string')
3218
- value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
3531
+ if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
3532
+ value = this.$utils.escapeActions(value);
3533
+ }
3219
3534
  return `${value == null || value === 'NULL'
3220
3535
  ? 'NULL'
3221
- : typeof value === 'string' && value.includes(this.$constants('RAW'))
3222
- ? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
3223
- : `'${this.$utils.covertBooleanToNumber(value)}'`}`;
3536
+ : this._checkValueHasRaw(value)}`;
3224
3537
  });
3225
3538
  values.push(`(${vals.join(',')})`);
3226
3539
  }
3227
- const columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => `\`${column}\``);
3540
+ const columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => this.bindColumn(column));
3228
3541
  return [
3229
3542
  `(${columns})`,
3230
3543
  `${this.$constants('VALUES')}`,