tspace-mysql 1.4.7 → 1.4.8

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 (31) hide show
  1. package/README.md +24 -7
  2. package/dist/lib/{tspace/Interface.d.ts → Interface.d.ts} +2 -1
  3. package/dist/lib/connection/index.d.ts +1 -1
  4. package/dist/lib/constants/index.js +3 -3
  5. package/dist/lib/tspace/{Abstract → Abstracts}/AbstractBuilder.d.ts +2 -2
  6. package/dist/lib/tspace/{Abstract → Abstracts}/AbstractBuilder.js +2 -2
  7. package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.d.ts +8 -2
  8. package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.d.ts +4 -2
  9. package/dist/lib/tspace/Blueprint.d.ts +11 -1
  10. package/dist/lib/tspace/Blueprint.js +11 -1
  11. package/dist/lib/tspace/Builder.d.ts +22 -7
  12. package/dist/lib/tspace/Builder.js +384 -310
  13. package/dist/lib/tspace/DB.d.ts +42 -2
  14. package/dist/lib/tspace/DB.js +61 -5
  15. package/dist/lib/tspace/{ProxyHandler.js → Handlers/Proxy.js} +1 -1
  16. package/dist/lib/tspace/{RelationHandler.d.ts → Handlers/Relation.d.ts} +4 -4
  17. package/dist/lib/tspace/{RelationHandler.js → Handlers/Relation.js} +95 -19
  18. package/dist/lib/tspace/Model.d.ts +71 -10
  19. package/dist/lib/tspace/Model.js +228 -64
  20. package/dist/lib/tspace/Schema.js +4 -5
  21. package/dist/lib/tspace/index.d.ts +2 -0
  22. package/dist/lib/tspace/index.js +3 -1
  23. package/dist/lib/utils/index.d.ts +1 -0
  24. package/dist/lib/utils/index.js +8 -0
  25. package/package.json +1 -1
  26. /package/dist/lib/{tspace/Interface.js → Interface.js} +0 -0
  27. /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.js +0 -0
  28. /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.js +0 -0
  29. /package/dist/lib/tspace/{ProxyHandler.d.ts → Handlers/Proxy.d.ts} +0 -0
  30. /package/dist/lib/tspace/{StateHandler.d.ts → Handlers/State.d.ts} +0 -0
  31. /package/dist/lib/tspace/{StateHandler.js → Handlers/State.js} +0 -0
@@ -26,12 +26,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Builder = void 0;
27
27
  const fs_1 = __importDefault(require("fs"));
28
28
  const sql_formatter_1 = require("sql-formatter");
29
- const AbstractBuilder_1 = require("./Abstract/AbstractBuilder");
29
+ const AbstractBuilder_1 = require("./Abstracts/AbstractBuilder");
30
30
  const utils_1 = require("../utils");
31
31
  const constants_1 = require("../constants");
32
32
  const DB_1 = require("./DB");
33
33
  const connection_1 = require("../connection");
34
- const StateHandler_1 = require("./StateHandler");
34
+ const State_1 = require("./Handlers/State");
35
35
  class Builder extends AbstractBuilder_1.AbstractBuilder {
36
36
  constructor() {
37
37
  super();
@@ -215,13 +215,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
215
215
  value = this.$utils.escape(value);
216
216
  value = this._valueTrueFalse(value);
217
217
  this._setState('WHERE', [
218
- this._queryWhereIsExists()
219
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
220
- : `${this.$constants('WHERE')}`,
221
- `${this.bindColumn(column)}`,
222
- `${operator}`,
223
- `${this._checkValueHasRaw(value)}`
224
- ].join(' '));
218
+ ...this._getState('WHERE'),
219
+ [
220
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
221
+ `${this.bindColumn(String(column))}`,
222
+ `${operator}`,
223
+ `${this._checkValueHasRaw(value)}`
224
+ ].join(' ')
225
+ ]);
225
226
  return this;
226
227
  }
227
228
  /**
@@ -240,13 +241,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
240
241
  value = this.$utils.escape(value);
241
242
  value = this._valueTrueFalse(value);
242
243
  this._setState('WHERE', [
243
- this._queryWhereIsExists()
244
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
245
- : `${this.$constants('WHERE')}`,
246
- `${this.bindColumn(column)}`,
247
- `${operator}`,
248
- `${this._checkValueHasRaw(value)}`
249
- ].join(' '));
244
+ ...this._getState('WHERE'),
245
+ [
246
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
247
+ `${this.bindColumn(String(column))}`,
248
+ `${operator}`,
249
+ `${this._checkValueHasRaw(value)}`
250
+ ].join(' ')
251
+ ]);
250
252
  return this;
251
253
  }
252
254
  /**
@@ -259,11 +261,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
259
261
  */
260
262
  whereRaw(sql) {
261
263
  this._setState('WHERE', [
262
- this._queryWhereIsExists()
263
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
264
- : `${this.$constants('WHERE')}`,
265
- `${sql}`,
266
- ].join(' '));
264
+ ...this._getState('WHERE'),
265
+ [
266
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
267
+ `${sql}`
268
+ ].join(' ')
269
+ ]);
267
270
  return this;
268
271
  }
269
272
  /**
@@ -276,11 +279,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
276
279
  */
277
280
  orWhereRaw(sql) {
278
281
  this._setState('WHERE', [
279
- this._queryWhereIsExists()
280
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
281
- : `${this.$constants('WHERE')}`,
282
- `${sql}`,
283
- ].join(' '));
282
+ ...this._getState('WHERE'),
283
+ [
284
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
285
+ `${sql}`
286
+ ].join(' ')
287
+ ]);
284
288
  return this;
285
289
  }
286
290
  /**
@@ -297,13 +301,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
297
301
  const operator = '=';
298
302
  const value = this.$utils.escape(columns[column]);
299
303
  this._setState('WHERE', [
300
- this._queryWhereIsExists()
301
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
302
- : `${this.$constants('WHERE')}`,
303
- `${this.bindColumn(column)}`,
304
- `${operator}`,
305
- `${this._checkValueHasRaw(value)}`
306
- ].join(' '));
304
+ ...this._getState('WHERE'),
305
+ [
306
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
307
+ `${this.bindColumn(String(column))}`,
308
+ `${operator}`,
309
+ `${this._checkValueHasRaw(value)}`
310
+ ].join(' ')
311
+ ]);
307
312
  }
308
313
  return this;
309
314
  }
@@ -322,13 +327,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
322
327
  value = this.$utils.escape(value);
323
328
  value = this._valueTrueFalse(value);
324
329
  this._setState('WHERE', [
325
- this._queryWhereIsExists()
326
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
327
- : `${this.$constants('WHERE')}`,
328
- `${this.bindColumn(column)}->>'$.${key}'`,
329
- `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
330
- `${this._checkValueHasRaw(value)}`
331
- ].join(' '));
330
+ ...this._getState('WHERE'),
331
+ [
332
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
333
+ `${this.bindColumn(column)}->>'$.${key}'`,
334
+ `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
335
+ `${this._checkValueHasRaw(value)}`
336
+ ].join(' ')
337
+ ]);
332
338
  return this;
333
339
  }
334
340
  /**
@@ -343,17 +349,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
343
349
  * @return {this}
344
350
  */
345
351
  whereJson(column, { key, value, operator }) {
346
- value = this.$utils.escape(value);
347
- value = this._valueTrueFalse(value);
348
- this._setState('WHERE', [
349
- this._queryWhereIsExists()
350
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
351
- : `${this.$constants('WHERE')}`,
352
- `${this.bindColumn(column)}->>'$.${key}'`,
353
- `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
354
- `${this._checkValueHasRaw(value)}`
355
- ].join(' '));
356
- return this;
352
+ return this.whereJSON(column, { key, value, operator });
357
353
  }
358
354
  /**
359
355
  *
@@ -365,12 +361,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
365
361
  */
366
362
  whereExists(sql) {
367
363
  this._setState('WHERE', [
368
- this._queryWhereIsExists()
369
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
370
- : `${this.$constants('WHERE')}`,
371
- `${this.$constants('EXISTS')}`,
372
- `(${sql})`
373
- ].join(' '));
364
+ ...this._getState('WHERE'),
365
+ [
366
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
367
+ `${this.$constants('EXISTS')}`,
368
+ `(${sql})`
369
+ ].join(' ')
370
+ ]);
374
371
  return this;
375
372
  }
376
373
  /**
@@ -380,11 +377,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
380
377
  */
381
378
  whereId(id, column = 'id') {
382
379
  this._setState('WHERE', [
383
- this._queryWhereIsExists()
384
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
385
- : `${this.$constants('WHERE')}`,
386
- `${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
387
- ].join(' '));
380
+ ...this._getState('WHERE'),
381
+ [
382
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
383
+ `${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
384
+ ].join(' ')
385
+ ]);
388
386
  return this;
389
387
  }
390
388
  /**
@@ -395,11 +393,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
395
393
  whereEmail(email) {
396
394
  const column = 'email';
397
395
  this._setState('WHERE', [
398
- this._queryWhereIsExists()
399
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
400
- : `${this.$constants('WHERE')}`,
401
- `${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
402
- ].join(' '));
396
+ ...this._getState('WHERE'),
397
+ [
398
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
399
+ `${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
400
+ ].join(' ')
401
+ ]);
403
402
  return this;
404
403
  }
405
404
  /**
@@ -410,11 +409,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
410
409
  */
411
410
  whereUser(userId, column = 'user_id') {
412
411
  this._setState('WHERE', [
413
- this._queryWhereIsExists()
414
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
415
- : `${this.$constants('WHERE')}`,
416
- `${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
417
- ].join(' '));
412
+ ...this._getState('WHERE'),
413
+ [
414
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
415
+ `${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
416
+ ].join(' ')
417
+ ]);
418
418
  return this;
419
419
  }
420
420
  /**
@@ -432,13 +432,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
432
432
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
433
433
  : this.$constants('NULL');
434
434
  this._setState('WHERE', [
435
- this._queryWhereIsExists()
436
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
437
- : `${this.$constants('WHERE')}`,
438
- `${this.bindColumn(column)}`,
439
- `${this.$constants('IN')}`,
440
- `(${values})`
441
- ].join(' '));
435
+ ...this._getState('WHERE'),
436
+ [
437
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
438
+ `${this.bindColumn(column)}`,
439
+ `${this.$constants('IN')}`,
440
+ `(${values})`
441
+ ].join(' ')
442
+ ]);
442
443
  return this;
443
444
  }
444
445
  /**
@@ -456,13 +457,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
456
457
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
457
458
  : this.$constants('NULL');
458
459
  this._setState('WHERE', [
459
- this._queryWhereIsExists()
460
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
461
- : `${this.$constants('WHERE')}`,
462
- `${this.bindColumn(column)}`,
463
- `${this.$constants('IN')}`,
464
- `(${values})`
465
- ].join(' '));
460
+ ...this._getState('WHERE'),
461
+ [
462
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
463
+ `${this.bindColumn(column)}`,
464
+ `${this.$constants('IN')}`,
465
+ `(${values})`
466
+ ].join(' ')
467
+ ]);
466
468
  return this;
467
469
  }
468
470
  /**
@@ -480,13 +482,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
480
482
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
481
483
  : this.$constants('NULL');
482
484
  this._setState('WHERE', [
483
- this._queryWhereIsExists()
484
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
485
- : `${this.$constants('WHERE')}`,
486
- `${this.bindColumn(column)}`,
487
- `${this.$constants('NOT_IN')}`,
488
- `(${values})`
489
- ].join(' '));
485
+ ...this._getState('WHERE'),
486
+ [
487
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
488
+ `${this.bindColumn(column)}`,
489
+ `${this.$constants('NOT_IN')}`,
490
+ `(${values})`
491
+ ].join(' ')
492
+ ]);
490
493
  return this;
491
494
  }
492
495
  /**
@@ -504,13 +507,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
504
507
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
505
508
  : this.$constants('NULL');
506
509
  this._setState('WHERE', [
507
- this._queryWhereIsExists()
508
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
509
- : `${this.$constants('WHERE')}`,
510
- `${this.bindColumn(column)}`,
511
- `${this.$constants('NOT_IN')}`,
512
- `(${values})`
513
- ].join(' '));
510
+ ...this._getState('WHERE'),
511
+ [
512
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
513
+ `${this.bindColumn(column)}`,
514
+ `${this.$constants('NOT_IN')}`,
515
+ `(${values})`
516
+ ].join(' ')
517
+ ]);
514
518
  return this;
515
519
  }
516
520
  /**
@@ -527,13 +531,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
527
531
  if (!this.$utils.isSubQuery(subQuery))
528
532
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
529
533
  this._setState('WHERE', [
530
- this._queryWhereIsExists()
531
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
532
- : `${this.$constants('WHERE')}`,
533
- `${this.bindColumn(column)}`,
534
- `${this.$constants('IN')}`,
535
- `(${subQuery})`
536
- ].join(' '));
534
+ ...this._getState('WHERE'),
535
+ [
536
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
537
+ `${this.bindColumn(column)}`,
538
+ `${this.$constants('IN')}`,
539
+ `(${subQuery})`
540
+ ].join(' ')
541
+ ]);
537
542
  return this;
538
543
  }
539
544
  /**
@@ -550,13 +555,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
550
555
  if (!this.$utils.isSubQuery(subQuery))
551
556
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
552
557
  this._setState('WHERE', [
553
- this._queryWhereIsExists()
554
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
555
- : `${this.$constants('WHERE')}`,
556
- `${this.bindColumn(column)}`,
557
- `${this.$constants('NOT_IN')}`,
558
- `(${subQuery})`
559
- ].join(' '));
558
+ ...this._getState('WHERE'),
559
+ [
560
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
561
+ `${this.bindColumn(column)}`,
562
+ `${this.$constants('NOT_IN')}`,
563
+ `(${subQuery})`
564
+ ].join(' ')
565
+ ]);
560
566
  return this;
561
567
  }
562
568
  /**
@@ -573,13 +579,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
573
579
  if (!this.$utils.isSubQuery(subQuery))
574
580
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
575
581
  this._setState('WHERE', [
576
- this._queryWhereIsExists()
577
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
578
- : `${this.$constants('WHERE')}`,
579
- `${this.bindColumn(column)}`,
580
- `${this.$constants('IN')}`,
581
- `(${subQuery})`
582
- ].join(' '));
582
+ ...this._getState('WHERE'),
583
+ [
584
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
585
+ `${this.bindColumn(column)}`,
586
+ `${this.$constants('IN')}`,
587
+ `(${subQuery})`
588
+ ].join(' ')
589
+ ]);
583
590
  return this;
584
591
  }
585
592
  /**
@@ -596,13 +603,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
596
603
  if (!this.$utils.isSubQuery(subQuery))
597
604
  throw new Error(`This "${subQuery}" is invalid sub query (Sub query Operand should contain 1 column(s) not select * )`);
598
605
  this._setState('WHERE', [
599
- this._queryWhereIsExists()
600
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
601
- : `${this.$constants('WHERE')}`,
602
- `${this.bindColumn(column)}`,
603
- `${this.$constants('NOT_IN')}`,
604
- `(${subQuery})`
605
- ].join(' '));
606
+ ...this._getState('WHERE'),
607
+ [
608
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
609
+ `${this.bindColumn(column)}`,
610
+ `${this.$constants('NOT_IN')}`,
611
+ `(${subQuery})`
612
+ ].join(' ')
613
+ ]);
606
614
  return this;
607
615
  }
608
616
  /**
@@ -618,22 +626,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
618
626
  throw new Error("Value is't array");
619
627
  if (!array.length) {
620
628
  this._setState('WHERE', [
621
- this._queryWhereIsExists()
622
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
623
- : `${this.$constants('WHERE')}`,
624
- `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
625
- `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
626
- ].join(' '));
629
+ ...this._getState('WHERE'),
630
+ [
631
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
632
+ `${this.bindColumn(column)}`,
633
+ `${this.$constants('BETWEEN')}`,
634
+ `${this.$constants('NULL')}`,
635
+ `${this.$constants('AND')}`,
636
+ `${this.$constants('NULL')}`
637
+ ].join(' ')
638
+ ]);
627
639
  return this;
628
640
  }
629
641
  const [value1, value2] = array;
630
642
  this._setState('WHERE', [
631
- this._queryWhereIsExists()
632
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
633
- : `${this.$constants('WHERE')}`,
634
- `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
635
- `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
636
- ].join(' '));
643
+ ...this._getState('WHERE'),
644
+ [
645
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
646
+ `${this.bindColumn(column)}`,
647
+ `${this.$constants('BETWEEN')}`,
648
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
649
+ `${this.$constants('AND')}`,
650
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
651
+ ].join(' ')
652
+ ]);
637
653
  return this;
638
654
  }
639
655
  /**
@@ -649,22 +665,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
649
665
  throw new Error("Value is't array");
650
666
  if (!array.length) {
651
667
  this._setState('WHERE', [
652
- this._queryWhereIsExists()
653
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
654
- : `${this.$constants('WHERE')}`,
655
- `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
656
- `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
657
- ].join(' '));
668
+ ...this._getState('WHERE'),
669
+ [
670
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
671
+ `${this.bindColumn(column)}`,
672
+ `${this.$constants('BETWEEN')}`,
673
+ `${this.$constants('NULL')}`,
674
+ `${this.$constants('AND')}`,
675
+ `${this.$constants('NULL')}`
676
+ ].join(' ')
677
+ ]);
658
678
  return this;
659
679
  }
660
680
  const [value1, value2] = array;
661
681
  this._setState('WHERE', [
662
- this._queryWhereIsExists()
663
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
664
- : `${this.$constants('WHERE')}`,
665
- `${this.bindColumn(column)} ${this.$constants('BETWEEN')}`,
666
- `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
667
- ].join(' '));
682
+ ...this._getState('WHERE'),
683
+ [
684
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
685
+ `${this.bindColumn(column)}`,
686
+ `${this.$constants('BETWEEN')}`,
687
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
688
+ `${this.$constants('AND')}`,
689
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
690
+ ].join(' ')
691
+ ]);
668
692
  return this;
669
693
  }
670
694
  /**
@@ -680,22 +704,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
680
704
  throw new Error("Value is't array");
681
705
  if (!array.length) {
682
706
  this._setState('WHERE', [
683
- this._queryWhereIsExists()
684
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
685
- : `${this.$constants('WHERE')}`,
686
- `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
687
- `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
688
- ].join(' '));
707
+ ...this._getState('WHERE'),
708
+ [
709
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
710
+ `${this.bindColumn(column)}`,
711
+ `${this.$constants('NOT_BETWEEN')}`,
712
+ `${this.$constants('NULL')}`,
713
+ `${this.$constants('AND')}`,
714
+ `${this.$constants('NULL')}`
715
+ ].join(' ')
716
+ ]);
689
717
  return this;
690
718
  }
691
719
  const [value1, value2] = array;
692
720
  this._setState('WHERE', [
693
- this._queryWhereIsExists()
694
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
695
- : `${this.$constants('WHERE')}`,
696
- `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
697
- `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
698
- ].join(' '));
721
+ ...this._getState('WHERE'),
722
+ [
723
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
724
+ `${this.bindColumn(column)}`,
725
+ `${this.$constants('NOT_BETWEEN')}`,
726
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
727
+ `${this.$constants('AND')}`,
728
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
729
+ ].join(' ')
730
+ ]);
699
731
  return this;
700
732
  }
701
733
  /**
@@ -711,22 +743,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
711
743
  throw new Error("Value is't array");
712
744
  if (!array.length) {
713
745
  this._setState('WHERE', [
714
- this._queryWhereIsExists()
715
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
716
- : `${this.$constants('WHERE')}`,
717
- `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
718
- `${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
719
- ].join(' '));
746
+ ...this._getState('WHERE'),
747
+ [
748
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
749
+ `${this.bindColumn(column)}`,
750
+ `${this.$constants('NOT_BETWEEN')}`,
751
+ `${this.$constants('NULL')}`,
752
+ `${this.$constants('AND')}`,
753
+ `${this.$constants('NULL')}`
754
+ ].join(' ')
755
+ ]);
720
756
  return this;
721
757
  }
722
758
  const [value1, value2] = array;
723
759
  this._setState('WHERE', [
724
- this._queryWhereIsExists()
725
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
726
- : `${this.$constants('WHERE')}`,
727
- `${this.bindColumn(column)} ${this.$constants('NOT_BETWEEN')}`,
728
- `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
729
- ].join(' '));
760
+ ...this._getState('WHERE'),
761
+ [
762
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
763
+ `${this.bindColumn(column)}`,
764
+ `${this.$constants('NOT_BETWEEN')}`,
765
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
766
+ `${this.$constants('AND')}`,
767
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
768
+ ].join(' ')
769
+ ]);
730
770
  return this;
731
771
  }
732
772
  /**
@@ -738,12 +778,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
738
778
  */
739
779
  whereNull(column) {
740
780
  this._setState('WHERE', [
741
- this._queryWhereIsExists()
742
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
743
- : `${this.$constants('WHERE')}`,
744
- `${this.bindColumn(column)}`,
745
- `${this.$constants('IS_NULL')}`
746
- ].join(' '));
781
+ ...this._getState('WHERE'),
782
+ [
783
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
784
+ `${this.bindColumn(column)}`,
785
+ `${this.$constants('IS_NULL')}`
786
+ ].join(' ')
787
+ ]);
747
788
  return this;
748
789
  }
749
790
  /**
@@ -755,12 +796,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
755
796
  */
756
797
  orWhereNull(column) {
757
798
  this._setState('WHERE', [
758
- this._queryWhereIsExists()
759
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
760
- : `${this.$constants('WHERE')}`,
761
- `${this.bindColumn(column)}`,
762
- `${this.$constants('IS_NULL')}`
763
- ].join(' '));
799
+ ...this._getState('WHERE'),
800
+ [
801
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
802
+ `${this.bindColumn(column)}`,
803
+ `${this.$constants('IS_NULL')}`
804
+ ].join(' ')
805
+ ]);
764
806
  return this;
765
807
  }
766
808
  /**
@@ -772,12 +814,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
772
814
  */
773
815
  whereNotNull(column) {
774
816
  this._setState('WHERE', [
775
- this._queryWhereIsExists()
776
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
777
- : `${this.$constants('WHERE')}`,
778
- `${this.bindColumn(column)}`,
779
- `${this.$constants('IS_NOT_NULL')}`
780
- ].join(' '));
817
+ ...this._getState('WHERE'),
818
+ [
819
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
820
+ `${this.bindColumn(column)}`,
821
+ `${this.$constants('IS_NOT_NULL')}`
822
+ ].join(' ')
823
+ ]);
781
824
  return this;
782
825
  }
783
826
  /**
@@ -789,12 +832,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
789
832
  */
790
833
  orWhereNotNull(column) {
791
834
  this._setState('WHERE', [
792
- this._queryWhereIsExists()
793
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
794
- : `${this.$constants('WHERE')}`,
795
- `${this.bindColumn(column)}`,
796
- `${this.$constants('IS_NOT_NULL')}`
797
- ].join(' '));
835
+ ...this._getState('WHERE'),
836
+ [
837
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
838
+ `${this.bindColumn(column)}`,
839
+ `${this.$constants('IS_NOT_NULL')}`
840
+ ].join(' ')
841
+ ]);
798
842
  return this;
799
843
  }
800
844
  /**
@@ -813,13 +857,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
813
857
  value = this.$utils.escape(value);
814
858
  value = this._valueTrueFalse(value);
815
859
  this._setState('WHERE', [
816
- this._queryWhereIsExists()
817
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
818
- : `${this.$constants('WHERE')}`,
819
- `${this.$constants('BINARY')} ${this.bindColumn(column)}`,
820
- `${operator}`,
821
- `${this._checkValueHasRaw(this.$utils.escape(value))}`
822
- ].join(' '));
860
+ ...this._getState('WHERE'),
861
+ [
862
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
863
+ `${this.$constants('BINARY')}`,
864
+ `${this.bindColumn(column)}`,
865
+ `${operator}`,
866
+ `${this._checkValueHasRaw(this.$utils.escape(value))}`
867
+ ].join(' ')
868
+ ]);
823
869
  return this;
824
870
  }
825
871
  /**
@@ -852,13 +898,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
852
898
  value = this.$utils.escape(value);
853
899
  value = this._valueTrueFalse(value);
854
900
  this._setState('WHERE', [
855
- this._queryWhereIsExists()
856
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
857
- : `${this.$constants('WHERE')}`,
858
- `${this.$constants('BINARY')} ${this.bindColumn(column)}`,
859
- `${operator}`,
860
- `${this._checkValueHasRaw(this.$utils.escape(value))}`
861
- ].join(' '));
901
+ ...this._getState('WHERE'),
902
+ [
903
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
904
+ `${this.$constants('BINARY')}`,
905
+ `${this.bindColumn(column)}`,
906
+ `${operator}`,
907
+ `${this._checkValueHasRaw(this.$utils.escape(value))}`
908
+ ].join(' ')
909
+ ]);
862
910
  return this;
863
911
  }
864
912
  /**
@@ -875,17 +923,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
875
923
  if (repository instanceof Promise)
876
924
  throw new Error('"whereQuery" is not supported a Promise');
877
925
  if (!(repository instanceof DB_1.DB))
878
- throw new Error(`Unknown callback query: '[${repository}]'`);
879
- const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || '';
880
- if (where === '')
926
+ throw new Error(`Unknown callback query: '${repository}'`);
927
+ const where = (repository === null || repository === void 0 ? void 0 : repository._getState('WHERE')) || [];
928
+ if (!where.length)
881
929
  return this;
882
- const query = where.replace('WHERE', '');
930
+ const query = where.join(' ');
883
931
  this._setState('WHERE', [
884
- this._queryWhereIsExists()
885
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
886
- : `${this.$constants('WHERE')}`,
887
- `(${query})`
888
- ].join(' '));
932
+ ...this._getState('WHERE'),
933
+ [
934
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
935
+ `(${query})`
936
+ ].join(' ')
937
+ ]);
889
938
  return this;
890
939
  }
891
940
  /**
@@ -913,18 +962,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
913
962
  throw new Error('"whereQuery" is not supported a Promise');
914
963
  if (!(repository instanceof DB_1.DB))
915
964
  throw new Error(`Unknown callback query: '[${repository}]'`);
916
- const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || '';
917
- if (where === '')
965
+ const where = (repository === null || repository === void 0 ? void 0 : repository._getState('WHERE')) || [];
966
+ if (!where.length)
918
967
  return this;
919
- const query = where.replace('WHERE', '');
968
+ const query = where.join(' ');
920
969
  this._setState('WHERE', [
921
- this._queryWhereIsExists()
922
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
923
- : `${this.$constants('WHERE')}`,
924
- `(${query})`
925
- ].join(' '));
970
+ ...this._getState('WHERE'),
971
+ [
972
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
973
+ `(${query})`
974
+ ].join(' ')
975
+ ]);
926
976
  return this;
927
977
  }
978
+ /**
979
+ * The 'orWhereGroup' method is used to add conditions to a database query to create a grouped condition.
980
+ *
981
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
982
+ * @param {function} callback callback query
983
+ * @return {this}
984
+ */
985
+ orWhereGroup(callback) {
986
+ return this.orWhereQuery(callback);
987
+ }
928
988
  /**
929
989
  * The 'whereCases' method is used to add conditions with cases to a database query.
930
990
  *
@@ -949,18 +1009,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
949
1009
  ];
950
1010
  }
951
1011
  this._setState('WHERE', [
952
- this._queryWhereIsExists()
953
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
954
- : `${this.$constants('WHERE')}`,
1012
+ ...this._getState('WHERE'),
955
1013
  [
956
- '(',
957
- this.$constants('CASE'),
958
- query.join(' '),
959
- elseCase == null ? '' : `ELSE ${elseCase}`,
960
- this.$constants('END'),
961
- ')'
1014
+ [
1015
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
1016
+ '(',
1017
+ this.$constants('CASE'),
1018
+ query.join(' '),
1019
+ elseCase == null ? '' : `ELSE ${elseCase}`,
1020
+ this.$constants('END'),
1021
+ ')'
1022
+ ].join(' ')
962
1023
  ].join(' ')
963
- ].join(' '));
1024
+ ]);
964
1025
  return this;
965
1026
  }
966
1027
  /**
@@ -987,18 +1048,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
987
1048
  ];
988
1049
  }
989
1050
  this._setState('WHERE', [
990
- this._queryWhereIsExists()
991
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
992
- : `${this.$constants('WHERE')}`,
1051
+ ...this._getState('WHERE'),
993
1052
  [
994
- '(',
995
- this.$constants('CASE'),
996
- query.join(' '),
997
- elseCase == null ? '' : `ELSE ${elseCase}`,
998
- this.$constants('END'),
999
- ')'
1053
+ [
1054
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
1055
+ '(',
1056
+ this.$constants('CASE'),
1057
+ query.join(' '),
1058
+ elseCase == null ? '' : `ELSE ${elseCase}`,
1059
+ this.$constants('END'),
1060
+ ')'
1061
+ ].join(' ')
1000
1062
  ].join(' ')
1001
- ].join(' '));
1063
+ ]);
1002
1064
  return this;
1003
1065
  }
1004
1066
  /**
@@ -1060,8 +1122,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1060
1122
  `${this.$constants('INNER_JOIN')}`,
1061
1123
  `\`${table}\` ${this.$constants('ON')}`,
1062
1124
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1063
- ]
1064
- .join(' ')
1125
+ ].join(' ')
1065
1126
  ]);
1066
1127
  return this;
1067
1128
  }
@@ -1084,8 +1145,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1084
1145
  `${this.$constants('RIGHT_JOIN')}`,
1085
1146
  `\`${table}\` ${this.$constants('ON')}`,
1086
1147
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1087
- ]
1088
- .join(' ')
1148
+ ].join(' ')
1089
1149
  ]);
1090
1150
  return this;
1091
1151
  }
@@ -1108,8 +1168,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1108
1168
  `${this.$constants('LEFT_JOIN')}`,
1109
1169
  `\`${table}\` ${this.$constants('ON')}`,
1110
1170
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1111
- ]
1112
- .join(' ')
1171
+ ].join(' ')
1113
1172
  ]);
1114
1173
  return this;
1115
1174
  }
@@ -1130,8 +1189,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1130
1189
  `${this.$constants('CROSS_JOIN')}`,
1131
1190
  `\`${table}\` ${this.$constants('ON')}`,
1132
1191
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1133
- ]
1134
- .join(' ')
1192
+ ].join(' ')
1135
1193
  ]);
1136
1194
  return this;
1137
1195
  }
@@ -1762,14 +1820,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1762
1820
  return this.toString();
1763
1821
  }
1764
1822
  /**
1765
- *
1823
+ * The 'getTableName' method is used to get table name
1766
1824
  * @return {string} return table name
1767
1825
  */
1768
1826
  getTableName() {
1769
1827
  return this._getState('TABLE_NAME').replace(/\`/g, '');
1770
1828
  }
1771
1829
  /**
1772
- *
1830
+ * The 'getSchema' method is used to get schema information
1831
+ * @return {this} this this
1832
+ */
1833
+ getSchema() {
1834
+ return __awaiter(this, void 0, void 0, function* () {
1835
+ const sql = [
1836
+ `${this.$constants('SHOW')}`,
1837
+ `${this.$constants('COLUMNS')}`,
1838
+ `${this.$constants('FROM')}`,
1839
+ `\`${this._getState('TABLE_NAME').replace(/\`/g, '')}\``
1840
+ ].join(' ');
1841
+ return yield this._queryStatement(sql);
1842
+ });
1843
+ }
1844
+ /**
1845
+ * The 'bindColumn' method is used to concat table and column -> `users`.`id`
1846
+ * @param {string} column
1773
1847
  * @return {string} return table.column
1774
1848
  */
1775
1849
  bindColumn(column) {
@@ -2043,7 +2117,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2043
2117
  sqlCount,
2044
2118
  this._getState('FROM'),
2045
2119
  this._getState('TABLE_NAME'),
2046
- this._getState('WHERE'),
2120
+ this._queryBuilder().where()
2047
2121
  ].join(' ');
2048
2122
  const count = yield this._queryStatement(sqlTotal);
2049
2123
  const total = ((_b = count === null || count === void 0 ? void 0 : count.shift()) === null || _b === void 0 ? void 0 : _b.total) || 0;
@@ -2096,7 +2170,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2096
2170
  var _a, _b;
2097
2171
  return __awaiter(this, void 0, void 0, function* () {
2098
2172
  if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2099
- this.select(...yield this._exceptColumns());
2173
+ this.select(...yield this.exceptColumns());
2100
2174
  this.limit(1);
2101
2175
  const sql = this._queryBuilder().select();
2102
2176
  const result = yield this._queryStatement(sql);
@@ -2141,7 +2215,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2141
2215
  var _a, _b;
2142
2216
  return __awaiter(this, void 0, void 0, function* () {
2143
2217
  if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2144
- this.select(...yield this._exceptColumns());
2218
+ this.select(...yield this.exceptColumns());
2145
2219
  let sql = this._queryBuilder().select();
2146
2220
  if (!sql.includes(this.$constants('LIMIT')))
2147
2221
  sql = `${sql} ${this.$constants('LIMIT')} 1`;
@@ -2200,7 +2274,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2200
2274
  var _a, _b;
2201
2275
  return __awaiter(this, void 0, void 0, function* () {
2202
2276
  if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2203
- this.select(...yield this._exceptColumns());
2277
+ this.select(...yield this.exceptColumns());
2204
2278
  const sql = this._queryBuilder().select();
2205
2279
  const result = yield this._queryStatement(sql);
2206
2280
  if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
@@ -2394,7 +2468,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2394
2468
  delete() {
2395
2469
  var _a;
2396
2470
  return __awaiter(this, void 0, void 0, function* () {
2397
- if (!this._getState('WHERE')) {
2471
+ if (!this._getState('where').length) {
2398
2472
  throw new Error("can't delete without where condition");
2399
2473
  }
2400
2474
  this.limit(1);
@@ -2418,7 +2492,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2418
2492
  deleteMany() {
2419
2493
  var _a;
2420
2494
  return __awaiter(this, void 0, void 0, function* () {
2421
- if (!this._getState('WHERE')) {
2495
+ if (!this._getState('where').length) {
2422
2496
  throw new Error("can't delete without where condition");
2423
2497
  }
2424
2498
  this._setState('DELETE', [
@@ -2448,7 +2522,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2448
2522
  `${this.$constants('DELETE')}`,
2449
2523
  `${this._getState('FROM')}`,
2450
2524
  `${this._getState('TABLE_NAME')}`,
2451
- `${this._getState('WHERE')}`
2525
+ `${this._queryBuilder().where()}`
2452
2526
  ].join(' '));
2453
2527
  const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2454
2528
  if (result)
@@ -2528,23 +2602,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2528
2602
  const attributes = this.$attributes;
2529
2603
  if (attributes != null) {
2530
2604
  while (true) {
2531
- if (this._getState('WHERE')) {
2532
- const query = this._queryUpdate(attributes);
2533
- this._setState('UPDATE', [
2534
- `${this.$constants('UPDATE')}`,
2605
+ if (!this._getState('where').length) {
2606
+ const query = this._queryInsert(attributes);
2607
+ this._setState('INSERT', [
2608
+ `${this.$constants('INSERT')}`,
2535
2609
  `${this._getState('TABLE_NAME')}`,
2536
2610
  `${query}`
2537
2611
  ].join(' '));
2538
- this._setState('SAVE', 'UPDATE');
2612
+ this._setState('SAVE', 'INSERT');
2539
2613
  break;
2540
2614
  }
2541
- const query = this._queryInsert(attributes);
2542
- this._setState('INSERT', [
2543
- `${this.$constants('INSERT')}`,
2615
+ const query = this._queryUpdate(attributes);
2616
+ this._setState('UPDATE', [
2617
+ `${this.$constants('UPDATE')}`,
2544
2618
  `${this._getState('TABLE_NAME')}`,
2545
2619
  `${query}`
2546
2620
  ].join(' '));
2547
- this._setState('SAVE', 'INSERT');
2621
+ this._setState('SAVE', 'UPDATE');
2548
2622
  break;
2549
2623
  }
2550
2624
  }
@@ -2917,7 +2991,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2917
2991
  faker(rows = 1) {
2918
2992
  return __awaiter(this, void 0, void 0, function* () {
2919
2993
  let data = [];
2920
- this.void();
2921
2994
  const sql = [
2922
2995
  `${this.$constants('SHOW')}`,
2923
2996
  `${this.$constants('FIELDS')}`,
@@ -2940,14 +3013,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2940
3013
  }
2941
3014
  data = [...data, columnAndValue];
2942
3015
  }
2943
- const query = this._queryInsertMultiple(data);
2944
- this._setState('INSERT', [
2945
- `${this.$constants('INSERT')}`,
2946
- `${this._getState('TABLE_NAME')}`,
2947
- `${query}`
2948
- ].join(' '));
2949
- this._setState('SAVE', 'INSERT_MULTIPLE');
2950
- return this.save();
3016
+ return yield this.createMultiple(data).save();
2951
3017
  });
2952
3018
  }
2953
3019
  /**
@@ -2980,7 +3046,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2980
3046
  return true;
2981
3047
  });
2982
3048
  }
2983
- _exceptColumns() {
3049
+ exceptColumns() {
2984
3050
  return __awaiter(this, void 0, void 0, function* () {
2985
3051
  const excepts = this._getState('EXCEPTS');
2986
3052
  const hasDot = excepts.some((except) => /\./.test(except));
@@ -3009,7 +3075,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3009
3075
  return except !== column;
3010
3076
  });
3011
3077
  });
3012
- removeExcepts.push(hasDot ? removeExcept.map(r => `\`${tableName}\`.${r}`) : removeExcept);
3078
+ removeExcepts.push(hasDot ? removeExcept.map(r => `${tableName}.${r}`) : removeExcept);
3013
3079
  }
3014
3080
  return removeExcepts.flat();
3015
3081
  });
@@ -3055,7 +3121,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3055
3121
  return this._buildQueryStatement();
3056
3122
  }
3057
3123
  _buildQueryStatement() {
3058
- const buildSQL = (sql) => sql.filter(s => s !== '' || s == null).join(' ');
3124
+ const buildSQL = (sql) => sql.filter(s => s !== '' || s == null).join(' ').replace(/\s+/g, ' ');
3059
3125
  const bindSelect = (values) => {
3060
3126
  if (!values.length) {
3061
3127
  if (!this._getState('DISTINCT'))
@@ -3071,15 +3137,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3071
3137
  };
3072
3138
  const bindJoin = (values) => {
3073
3139
  if (!values.length)
3074
- return '';
3140
+ return null;
3075
3141
  return values.join(' ');
3076
3142
  };
3143
+ const bindWhere = (values) => {
3144
+ if (!values.length)
3145
+ return null;
3146
+ return `${this.$constants('WHERE')} ${values.map(v => v.replace(/^\s/, '').replace(/\s+/g, ' ')).join(' ')}`;
3147
+ };
3077
3148
  const select = () => buildSQL([
3078
3149
  bindSelect(this.$state.get('SELECT')),
3079
3150
  this.$state.get('FROM'),
3080
3151
  this.$state.get('TABLE_NAME'),
3081
3152
  bindJoin(this.$state.get('JOIN')),
3082
- this.$state.get('WHERE'),
3153
+ bindWhere(this.$state.get('WHERE')),
3083
3154
  this.$state.get('GROUP_BY'),
3084
3155
  this.$state.get('HAVING'),
3085
3156
  this.$state.get('ORDER_BY'),
@@ -3087,13 +3158,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3087
3158
  this.$state.get('OFFSET')
3088
3159
  ]);
3089
3160
  const insert = () => buildSQL([this.$state.get('INSERT')]);
3090
- const update = () => buildSQL([this.$state.get('UPDATE'), this.$state.get('WHERE'), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3091
- const remove = () => buildSQL([this.$state.get('DELETE'), this.$state.get('WHERE'), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3161
+ const update = () => buildSQL([this.$state.get('UPDATE'), bindWhere(this.$state.get('WHERE')), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3162
+ const remove = () => buildSQL([this.$state.get('DELETE'), bindWhere(this.$state.get('WHERE')), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3092
3163
  return {
3093
3164
  select,
3094
3165
  insert,
3095
3166
  update,
3096
3167
  delete: remove,
3168
+ where: () => bindWhere(this.$state.get('WHERE')),
3097
3169
  any: () => {
3098
3170
  if (this.$state.get('INSERT'))
3099
3171
  return insert();
@@ -3119,11 +3191,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3119
3191
  }
3120
3192
  whereReference(tableAndLocalKey, tableAndForeignKey) {
3121
3193
  this._setState('WHERE', [
3122
- this._queryWhereIsExists()
3123
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
3124
- : `${this.$constants('WHERE')}`,
3125
- `${tableAndLocalKey} = ${tableAndForeignKey}`
3126
- ].join(' '));
3194
+ ...this._getState('WHERE'),
3195
+ [
3196
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
3197
+ `${tableAndLocalKey} = ${tableAndForeignKey}`
3198
+ ].join(' ')
3199
+ ]);
3127
3200
  return this;
3128
3201
  }
3129
3202
  _queryStatement(sql) {
@@ -3146,13 +3219,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3146
3219
  return result;
3147
3220
  });
3148
3221
  }
3149
- _queryWhereIsExists() {
3150
- var _a;
3151
- return ((_a = String(this._getState('WHERE'))) === null || _a === void 0 ? void 0 : _a.includes(this.$constants('WHERE'))) || false;
3152
- }
3153
3222
  _insertNotExists() {
3154
3223
  return __awaiter(this, void 0, void 0, function* () {
3155
- if (!this._getState('WHERE'))
3224
+ if (!this._getState('where').length)
3156
3225
  throw new Error("Can't insert not exists without where condition");
3157
3226
  let sql = [
3158
3227
  `${this.$constants('SELECT')}`,
@@ -3160,7 +3229,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3160
3229
  `*`,
3161
3230
  `${this._getState('FROM')}`,
3162
3231
  `${this._getState('TABLE_NAME')}`,
3163
- `${this._getState('WHERE')}`,
3232
+ `${this._queryBuilder().where()}`,
3164
3233
  `${this.$constants('LIMIT')} 1)`,
3165
3234
  `${this.$constants('AS')} 'exists'`
3166
3235
  ].join(' ');
@@ -3214,8 +3283,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3214
3283
  sql: this._queryBuilder().insert(),
3215
3284
  returnId: true
3216
3285
  });
3286
+ console.log({
3287
+ result,
3288
+ id
3289
+ });
3217
3290
  if (this._getState('VOID') || !result)
3218
3291
  return this._resultHandler(undefined);
3292
+ console.log('hi');
3219
3293
  const arrayId = [...Array(result)].map((_, i) => i + id);
3220
3294
  const sql = new Builder()
3221
3295
  .copyBuilder(this, { select: true })
@@ -3228,7 +3302,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3228
3302
  }
3229
3303
  _insertOrSelect() {
3230
3304
  return __awaiter(this, void 0, void 0, function* () {
3231
- if (!this._getState('WHERE')) {
3305
+ if (!this._getState('where').length) {
3232
3306
  throw new Error("Can't create or select without where condition");
3233
3307
  }
3234
3308
  let sql = [
@@ -3237,7 +3311,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3237
3311
  `*`,
3238
3312
  `${this._getState('FROM')}`,
3239
3313
  `${this._getState('TABLE_NAME')}`,
3240
- `${this._getState('WHERE')}`,
3314
+ `${this._queryBuilder().where()}`,
3241
3315
  `${this.$constants('LIMIT')} 1)`,
3242
3316
  `${this.$constants('AS')} 'exists'`
3243
3317
  ].join(' ');
@@ -3282,7 +3356,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3282
3356
  }
3283
3357
  _updateOrInsert() {
3284
3358
  return __awaiter(this, void 0, void 0, function* () {
3285
- if (!this._getState('WHERE')) {
3359
+ if (!this._getState('where').length) {
3286
3360
  throw new Error("Can't update or insert without where condition");
3287
3361
  }
3288
3362
  let sql = [
@@ -3291,7 +3365,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3291
3365
  `*`,
3292
3366
  `${this._getState('FROM')}`,
3293
3367
  `${this._getState('TABLE_NAME')}`,
3294
- `${this._getState('WHERE')}`,
3368
+ `${this._queryBuilder().where()}`,
3295
3369
  `${this.$constants('LIMIT')} 1)`,
3296
3370
  `${this.$constants('AS')} 'exists'`
3297
3371
  ].join(' ');
@@ -3338,7 +3412,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3338
3412
  }
3339
3413
  _update(ignoreWhere = false) {
3340
3414
  return __awaiter(this, void 0, void 0, function* () {
3341
- if (!this._getState('WHERE') && !ignoreWhere)
3415
+ if (!this._getState('where').length && !ignoreWhere)
3342
3416
  throw new Error("can't update without where condition");
3343
3417
  const result = yield this._actionStatement({
3344
3418
  sql: this._queryBuilder().update()
@@ -3471,7 +3545,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3471
3545
  throw new Error(`Not found constants : ${name}`);
3472
3546
  return constants_1.CONSTANTS[name.toUpperCase()];
3473
3547
  };
3474
- this.$state = new StateHandler_1.StateHandler(this.$constants('DEFAULT'));
3548
+ this.$state = new State_1.StateHandler(this.$constants('DEFAULT'));
3475
3549
  }
3476
3550
  }
3477
3551
  exports.Builder = Builder;