tspace-mysql 1.4.7 → 1.4.9

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 (39) hide show
  1. package/README.md +128 -49
  2. package/dist/lib/{tspace/Interface.d.ts → Interface.d.ts} +18 -2
  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 +3 -3
  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 +22 -2
  9. package/dist/lib/tspace/Blueprint.d.ts +19 -5
  10. package/dist/lib/tspace/Blueprint.js +41 -19
  11. package/dist/lib/tspace/Builder.d.ts +26 -11
  12. package/dist/lib/tspace/Builder.js +378 -330
  13. package/dist/lib/tspace/DB.d.ts +42 -2
  14. package/dist/lib/tspace/DB.js +61 -5
  15. package/dist/lib/tspace/Decorator.d.ts +20 -0
  16. package/dist/lib/tspace/Decorator.js +166 -0
  17. package/dist/lib/tspace/{ProxyHandler.js → Handlers/Proxy.js} +1 -1
  18. package/dist/lib/tspace/{RelationHandler.d.ts → Handlers/Relation.d.ts} +4 -4
  19. package/dist/lib/tspace/{RelationHandler.js → Handlers/Relation.js} +103 -28
  20. package/dist/lib/tspace/{StateHandler.js → Handlers/State.js} +3 -2
  21. package/dist/lib/tspace/Model.d.ts +275 -11
  22. package/dist/lib/tspace/Model.js +989 -99
  23. package/dist/lib/tspace/Schema.js +5 -8
  24. package/dist/lib/tspace/index.d.ts +4 -0
  25. package/dist/lib/tspace/index.js +20 -2
  26. package/dist/lib/utils/index.d.ts +1 -0
  27. package/dist/lib/utils/index.js +9 -0
  28. package/dist/tests/01-Pool.test.d.ts +1 -0
  29. package/dist/tests/01-Pool.test.js +45 -0
  30. package/dist/tests/02-DB.test.d.ts +1 -0
  31. package/dist/tests/02-DB.test.js +109 -0
  32. package/dist/tests/03-Model.test.d.ts +1 -0
  33. package/dist/tests/03-Model.test.js +73 -0
  34. package/package.json +14 -3
  35. /package/dist/lib/{tspace/Interface.js → Interface.js} +0 -0
  36. /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractDB.js +0 -0
  37. /package/dist/lib/tspace/{Abstract → Abstracts}/AbstractModel.js +0 -0
  38. /package/dist/lib/tspace/{ProxyHandler.d.ts → Handlers/Proxy.d.ts} +0 -0
  39. /package/dist/lib/tspace/{StateHandler.d.ts → Handlers/State.d.ts} +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();
@@ -60,11 +60,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
60
60
  let select = columns.map((column) => {
61
61
  if (column === '*' || (column.includes('*') && /\./.test(column)))
62
62
  return column;
63
- if (/\./.test(column))
64
- return this.bindColumn(column);
65
63
  if (column.includes(this.$constants('RAW')))
66
64
  return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
67
- return `\`${column}\``;
65
+ return this.bindColumn(column);
68
66
  });
69
67
  select = [...this._getState('SELECT'), ...select];
70
68
  if (this._getState('DISTINCT') && select.length) {
@@ -215,13 +213,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
215
213
  value = this.$utils.escape(value);
216
214
  value = this._valueTrueFalse(value);
217
215
  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(' '));
216
+ ...this._getState('WHERE'),
217
+ [
218
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
219
+ `${this.bindColumn(String(column))}`,
220
+ `${operator}`,
221
+ `${this._checkValueHasRaw(value)}`
222
+ ].join(' ')
223
+ ]);
225
224
  return this;
226
225
  }
227
226
  /**
@@ -240,13 +239,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
240
239
  value = this.$utils.escape(value);
241
240
  value = this._valueTrueFalse(value);
242
241
  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(' '));
242
+ ...this._getState('WHERE'),
243
+ [
244
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
245
+ `${this.bindColumn(String(column))}`,
246
+ `${operator}`,
247
+ `${this._checkValueHasRaw(value)}`
248
+ ].join(' ')
249
+ ]);
250
250
  return this;
251
251
  }
252
252
  /**
@@ -259,11 +259,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
259
259
  */
260
260
  whereRaw(sql) {
261
261
  this._setState('WHERE', [
262
- this._queryWhereIsExists()
263
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
264
- : `${this.$constants('WHERE')}`,
265
- `${sql}`,
266
- ].join(' '));
262
+ ...this._getState('WHERE'),
263
+ [
264
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
265
+ `${sql}`
266
+ ].join(' ')
267
+ ]);
267
268
  return this;
268
269
  }
269
270
  /**
@@ -276,11 +277,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
276
277
  */
277
278
  orWhereRaw(sql) {
278
279
  this._setState('WHERE', [
279
- this._queryWhereIsExists()
280
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
281
- : `${this.$constants('WHERE')}`,
282
- `${sql}`,
283
- ].join(' '));
280
+ ...this._getState('WHERE'),
281
+ [
282
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
283
+ `${sql}`
284
+ ].join(' ')
285
+ ]);
284
286
  return this;
285
287
  }
286
288
  /**
@@ -297,13 +299,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
297
299
  const operator = '=';
298
300
  const value = this.$utils.escape(columns[column]);
299
301
  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(' '));
302
+ ...this._getState('WHERE'),
303
+ [
304
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
305
+ `${this.bindColumn(String(column))}`,
306
+ `${operator}`,
307
+ `${this._checkValueHasRaw(value)}`
308
+ ].join(' ')
309
+ ]);
307
310
  }
308
311
  return this;
309
312
  }
@@ -322,13 +325,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
322
325
  value = this.$utils.escape(value);
323
326
  value = this._valueTrueFalse(value);
324
327
  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(' '));
328
+ ...this._getState('WHERE'),
329
+ [
330
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
331
+ `${this.bindColumn(column)}->>'$.${key}'`,
332
+ `${operator == null ? "=" : operator.toLocaleUpperCase()}`,
333
+ `${this._checkValueHasRaw(value)}`
334
+ ].join(' ')
335
+ ]);
332
336
  return this;
333
337
  }
334
338
  /**
@@ -343,17 +347,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
343
347
  * @return {this}
344
348
  */
345
349
  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;
350
+ return this.whereJSON(column, { key, value, operator });
357
351
  }
358
352
  /**
359
353
  *
@@ -365,12 +359,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
365
359
  */
366
360
  whereExists(sql) {
367
361
  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(' '));
362
+ ...this._getState('WHERE'),
363
+ [
364
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
365
+ `${this.$constants('EXISTS')}`,
366
+ `(${sql})`
367
+ ].join(' ')
368
+ ]);
374
369
  return this;
375
370
  }
376
371
  /**
@@ -380,11 +375,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
380
375
  */
381
376
  whereId(id, column = 'id') {
382
377
  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(' '));
378
+ ...this._getState('WHERE'),
379
+ [
380
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
381
+ `${this.bindColumn(column)} = ${this.$utils.escape(id)}`,
382
+ ].join(' ')
383
+ ]);
388
384
  return this;
389
385
  }
390
386
  /**
@@ -395,11 +391,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
395
391
  whereEmail(email) {
396
392
  const column = 'email';
397
393
  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(' '));
394
+ ...this._getState('WHERE'),
395
+ [
396
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
397
+ `${this.bindColumn(column)} = ${this.$utils.escape(email)}`,
398
+ ].join(' ')
399
+ ]);
403
400
  return this;
404
401
  }
405
402
  /**
@@ -410,11 +407,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
410
407
  */
411
408
  whereUser(userId, column = 'user_id') {
412
409
  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(' '));
410
+ ...this._getState('WHERE'),
411
+ [
412
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
413
+ `${this.bindColumn(column)} = ${this.$utils.escape(userId)}`,
414
+ ].join(' ')
415
+ ]);
418
416
  return this;
419
417
  }
420
418
  /**
@@ -432,13 +430,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
432
430
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
433
431
  : this.$constants('NULL');
434
432
  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(' '));
433
+ ...this._getState('WHERE'),
434
+ [
435
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
436
+ `${this.bindColumn(column)}`,
437
+ `${this.$constants('IN')}`,
438
+ `(${values})`
439
+ ].join(' ')
440
+ ]);
442
441
  return this;
443
442
  }
444
443
  /**
@@ -456,13 +455,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
456
455
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
457
456
  : this.$constants('NULL');
458
457
  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(' '));
458
+ ...this._getState('WHERE'),
459
+ [
460
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
461
+ `${this.bindColumn(column)}`,
462
+ `${this.$constants('IN')}`,
463
+ `(${values})`
464
+ ].join(' ')
465
+ ]);
466
466
  return this;
467
467
  }
468
468
  /**
@@ -480,13 +480,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
480
480
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
481
481
  : this.$constants('NULL');
482
482
  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(' '));
483
+ ...this._getState('WHERE'),
484
+ [
485
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
486
+ `${this.bindColumn(column)}`,
487
+ `${this.$constants('NOT_IN')}`,
488
+ `(${values})`
489
+ ].join(' ')
490
+ ]);
490
491
  return this;
491
492
  }
492
493
  /**
@@ -504,13 +505,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
504
505
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
505
506
  : this.$constants('NULL');
506
507
  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(' '));
508
+ ...this._getState('WHERE'),
509
+ [
510
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
511
+ `${this.bindColumn(column)}`,
512
+ `${this.$constants('NOT_IN')}`,
513
+ `(${values})`
514
+ ].join(' ')
515
+ ]);
514
516
  return this;
515
517
  }
516
518
  /**
@@ -527,13 +529,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
527
529
  if (!this.$utils.isSubQuery(subQuery))
528
530
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
529
531
  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(' '));
532
+ ...this._getState('WHERE'),
533
+ [
534
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
535
+ `${this.bindColumn(column)}`,
536
+ `${this.$constants('IN')}`,
537
+ `(${subQuery})`
538
+ ].join(' ')
539
+ ]);
537
540
  return this;
538
541
  }
539
542
  /**
@@ -550,13 +553,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
550
553
  if (!this.$utils.isSubQuery(subQuery))
551
554
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
552
555
  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(' '));
556
+ ...this._getState('WHERE'),
557
+ [
558
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
559
+ `${this.bindColumn(column)}`,
560
+ `${this.$constants('NOT_IN')}`,
561
+ `(${subQuery})`
562
+ ].join(' ')
563
+ ]);
560
564
  return this;
561
565
  }
562
566
  /**
@@ -573,13 +577,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
573
577
  if (!this.$utils.isSubQuery(subQuery))
574
578
  throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
575
579
  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(' '));
580
+ ...this._getState('WHERE'),
581
+ [
582
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
583
+ `${this.bindColumn(column)}`,
584
+ `${this.$constants('IN')}`,
585
+ `(${subQuery})`
586
+ ].join(' ')
587
+ ]);
583
588
  return this;
584
589
  }
585
590
  /**
@@ -596,13 +601,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
596
601
  if (!this.$utils.isSubQuery(subQuery))
597
602
  throw new Error(`This "${subQuery}" is invalid sub query (Sub query Operand should contain 1 column(s) not select * )`);
598
603
  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(' '));
604
+ ...this._getState('WHERE'),
605
+ [
606
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
607
+ `${this.bindColumn(column)}`,
608
+ `${this.$constants('NOT_IN')}`,
609
+ `(${subQuery})`
610
+ ].join(' ')
611
+ ]);
606
612
  return this;
607
613
  }
608
614
  /**
@@ -618,22 +624,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
618
624
  throw new Error("Value is't array");
619
625
  if (!array.length) {
620
626
  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(' '));
627
+ ...this._getState('WHERE'),
628
+ [
629
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
630
+ `${this.bindColumn(column)}`,
631
+ `${this.$constants('BETWEEN')}`,
632
+ `${this.$constants('NULL')}`,
633
+ `${this.$constants('AND')}`,
634
+ `${this.$constants('NULL')}`
635
+ ].join(' ')
636
+ ]);
627
637
  return this;
628
638
  }
629
639
  const [value1, value2] = array;
630
640
  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(' '));
641
+ ...this._getState('WHERE'),
642
+ [
643
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
644
+ `${this.bindColumn(column)}`,
645
+ `${this.$constants('BETWEEN')}`,
646
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
647
+ `${this.$constants('AND')}`,
648
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
649
+ ].join(' ')
650
+ ]);
637
651
  return this;
638
652
  }
639
653
  /**
@@ -649,22 +663,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
649
663
  throw new Error("Value is't array");
650
664
  if (!array.length) {
651
665
  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(' '));
666
+ ...this._getState('WHERE'),
667
+ [
668
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
669
+ `${this.bindColumn(column)}`,
670
+ `${this.$constants('BETWEEN')}`,
671
+ `${this.$constants('NULL')}`,
672
+ `${this.$constants('AND')}`,
673
+ `${this.$constants('NULL')}`
674
+ ].join(' ')
675
+ ]);
658
676
  return this;
659
677
  }
660
678
  const [value1, value2] = array;
661
679
  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(' '));
680
+ ...this._getState('WHERE'),
681
+ [
682
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
683
+ `${this.bindColumn(column)}`,
684
+ `${this.$constants('BETWEEN')}`,
685
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
686
+ `${this.$constants('AND')}`,
687
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
688
+ ].join(' ')
689
+ ]);
668
690
  return this;
669
691
  }
670
692
  /**
@@ -680,22 +702,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
680
702
  throw new Error("Value is't array");
681
703
  if (!array.length) {
682
704
  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(' '));
705
+ ...this._getState('WHERE'),
706
+ [
707
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
708
+ `${this.bindColumn(column)}`,
709
+ `${this.$constants('NOT_BETWEEN')}`,
710
+ `${this.$constants('NULL')}`,
711
+ `${this.$constants('AND')}`,
712
+ `${this.$constants('NULL')}`
713
+ ].join(' ')
714
+ ]);
689
715
  return this;
690
716
  }
691
717
  const [value1, value2] = array;
692
718
  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(' '));
719
+ ...this._getState('WHERE'),
720
+ [
721
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
722
+ `${this.bindColumn(column)}`,
723
+ `${this.$constants('NOT_BETWEEN')}`,
724
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
725
+ `${this.$constants('AND')}`,
726
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
727
+ ].join(' ')
728
+ ]);
699
729
  return this;
700
730
  }
701
731
  /**
@@ -711,22 +741,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
711
741
  throw new Error("Value is't array");
712
742
  if (!array.length) {
713
743
  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(' '));
744
+ ...this._getState('WHERE'),
745
+ [
746
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
747
+ `${this.bindColumn(column)}`,
748
+ `${this.$constants('NOT_BETWEEN')}`,
749
+ `${this.$constants('NULL')}`,
750
+ `${this.$constants('AND')}`,
751
+ `${this.$constants('NULL')}`
752
+ ].join(' ')
753
+ ]);
720
754
  return this;
721
755
  }
722
756
  const [value1, value2] = array;
723
757
  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(' '));
758
+ ...this._getState('WHERE'),
759
+ [
760
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
761
+ `${this.bindColumn(column)}`,
762
+ `${this.$constants('NOT_BETWEEN')}`,
763
+ `${this._checkValueHasRaw(this.$utils.escape(value1))}`,
764
+ `${this.$constants('AND')}`,
765
+ `${this._checkValueHasRaw(this.$utils.escape(value2))}`
766
+ ].join(' ')
767
+ ]);
730
768
  return this;
731
769
  }
732
770
  /**
@@ -738,12 +776,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
738
776
  */
739
777
  whereNull(column) {
740
778
  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(' '));
779
+ ...this._getState('WHERE'),
780
+ [
781
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
782
+ `${this.bindColumn(column)}`,
783
+ `${this.$constants('IS_NULL')}`
784
+ ].join(' ')
785
+ ]);
747
786
  return this;
748
787
  }
749
788
  /**
@@ -755,12 +794,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
755
794
  */
756
795
  orWhereNull(column) {
757
796
  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(' '));
797
+ ...this._getState('WHERE'),
798
+ [
799
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
800
+ `${this.bindColumn(column)}`,
801
+ `${this.$constants('IS_NULL')}`
802
+ ].join(' ')
803
+ ]);
764
804
  return this;
765
805
  }
766
806
  /**
@@ -772,12 +812,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
772
812
  */
773
813
  whereNotNull(column) {
774
814
  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(' '));
815
+ ...this._getState('WHERE'),
816
+ [
817
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
818
+ `${this.bindColumn(column)}`,
819
+ `${this.$constants('IS_NOT_NULL')}`
820
+ ].join(' ')
821
+ ]);
781
822
  return this;
782
823
  }
783
824
  /**
@@ -789,12 +830,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
789
830
  */
790
831
  orWhereNotNull(column) {
791
832
  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(' '));
833
+ ...this._getState('WHERE'),
834
+ [
835
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
836
+ `${this.bindColumn(column)}`,
837
+ `${this.$constants('IS_NOT_NULL')}`
838
+ ].join(' ')
839
+ ]);
798
840
  return this;
799
841
  }
800
842
  /**
@@ -813,13 +855,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
813
855
  value = this.$utils.escape(value);
814
856
  value = this._valueTrueFalse(value);
815
857
  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(' '));
858
+ ...this._getState('WHERE'),
859
+ [
860
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
861
+ `${this.$constants('BINARY')}`,
862
+ `${this.bindColumn(column)}`,
863
+ `${operator}`,
864
+ `${this._checkValueHasRaw(this.$utils.escape(value))}`
865
+ ].join(' ')
866
+ ]);
823
867
  return this;
824
868
  }
825
869
  /**
@@ -852,13 +896,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
852
896
  value = this.$utils.escape(value);
853
897
  value = this._valueTrueFalse(value);
854
898
  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(' '));
899
+ ...this._getState('WHERE'),
900
+ [
901
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
902
+ `${this.$constants('BINARY')}`,
903
+ `${this.bindColumn(column)}`,
904
+ `${operator}`,
905
+ `${this._checkValueHasRaw(this.$utils.escape(value))}`
906
+ ].join(' ')
907
+ ]);
862
908
  return this;
863
909
  }
864
910
  /**
@@ -875,17 +921,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
875
921
  if (repository instanceof Promise)
876
922
  throw new Error('"whereQuery" is not supported a Promise');
877
923
  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 === '')
924
+ throw new Error(`Unknown callback query: '${repository}'`);
925
+ const where = (repository === null || repository === void 0 ? void 0 : repository._getState('WHERE')) || [];
926
+ if (!where.length)
881
927
  return this;
882
- const query = where.replace('WHERE', '');
928
+ const query = where.join(' ');
883
929
  this._setState('WHERE', [
884
- this._queryWhereIsExists()
885
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
886
- : `${this.$constants('WHERE')}`,
887
- `(${query})`
888
- ].join(' '));
930
+ ...this._getState('WHERE'),
931
+ [
932
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
933
+ `(${query})`
934
+ ].join(' ')
935
+ ]);
889
936
  return this;
890
937
  }
891
938
  /**
@@ -913,18 +960,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
913
960
  throw new Error('"whereQuery" is not supported a Promise');
914
961
  if (!(repository instanceof DB_1.DB))
915
962
  throw new Error(`Unknown callback query: '[${repository}]'`);
916
- const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || '';
917
- if (where === '')
963
+ const where = (repository === null || repository === void 0 ? void 0 : repository._getState('WHERE')) || [];
964
+ if (!where.length)
918
965
  return this;
919
- const query = where.replace('WHERE', '');
966
+ const query = where.join(' ');
920
967
  this._setState('WHERE', [
921
- this._queryWhereIsExists()
922
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
923
- : `${this.$constants('WHERE')}`,
924
- `(${query})`
925
- ].join(' '));
968
+ ...this._getState('WHERE'),
969
+ [
970
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
971
+ `(${query})`
972
+ ].join(' ')
973
+ ]);
926
974
  return this;
927
975
  }
976
+ /**
977
+ * The 'orWhereGroup' method is used to add conditions to a database query to create a grouped condition.
978
+ *
979
+ * It allows you to specify conditions that records in the database must meet in order to be included in the result set.
980
+ * @param {function} callback callback query
981
+ * @return {this}
982
+ */
983
+ orWhereGroup(callback) {
984
+ return this.orWhereQuery(callback);
985
+ }
928
986
  /**
929
987
  * The 'whereCases' method is used to add conditions with cases to a database query.
930
988
  *
@@ -949,18 +1007,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
949
1007
  ];
950
1008
  }
951
1009
  this._setState('WHERE', [
952
- this._queryWhereIsExists()
953
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
954
- : `${this.$constants('WHERE')}`,
1010
+ ...this._getState('WHERE'),
955
1011
  [
956
- '(',
957
- this.$constants('CASE'),
958
- query.join(' '),
959
- elseCase == null ? '' : `ELSE ${elseCase}`,
960
- this.$constants('END'),
961
- ')'
1012
+ [
1013
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
1014
+ '(',
1015
+ this.$constants('CASE'),
1016
+ query.join(' '),
1017
+ elseCase == null ? '' : `ELSE ${elseCase}`,
1018
+ this.$constants('END'),
1019
+ ')'
1020
+ ].join(' ')
962
1021
  ].join(' ')
963
- ].join(' '));
1022
+ ]);
964
1023
  return this;
965
1024
  }
966
1025
  /**
@@ -987,18 +1046,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
987
1046
  ];
988
1047
  }
989
1048
  this._setState('WHERE', [
990
- this._queryWhereIsExists()
991
- ? `${this._getState('WHERE')} ${this.$constants('OR')}`
992
- : `${this.$constants('WHERE')}`,
1049
+ ...this._getState('WHERE'),
993
1050
  [
994
- '(',
995
- this.$constants('CASE'),
996
- query.join(' '),
997
- elseCase == null ? '' : `ELSE ${elseCase}`,
998
- this.$constants('END'),
999
- ')'
1051
+ [
1052
+ this._getState('WHERE').length ? `${this.$constants('OR')}` : '',
1053
+ '(',
1054
+ this.$constants('CASE'),
1055
+ query.join(' '),
1056
+ elseCase == null ? '' : `ELSE ${elseCase}`,
1057
+ this.$constants('END'),
1058
+ ')'
1059
+ ].join(' ')
1000
1060
  ].join(' ')
1001
- ].join(' '));
1061
+ ]);
1002
1062
  return this;
1003
1063
  }
1004
1064
  /**
@@ -1060,8 +1120,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1060
1120
  `${this.$constants('INNER_JOIN')}`,
1061
1121
  `\`${table}\` ${this.$constants('ON')}`,
1062
1122
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1063
- ]
1064
- .join(' ')
1123
+ ].join(' ')
1065
1124
  ]);
1066
1125
  return this;
1067
1126
  }
@@ -1084,8 +1143,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1084
1143
  `${this.$constants('RIGHT_JOIN')}`,
1085
1144
  `\`${table}\` ${this.$constants('ON')}`,
1086
1145
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1087
- ]
1088
- .join(' ')
1146
+ ].join(' ')
1089
1147
  ]);
1090
1148
  return this;
1091
1149
  }
@@ -1108,8 +1166,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1108
1166
  `${this.$constants('LEFT_JOIN')}`,
1109
1167
  `\`${table}\` ${this.$constants('ON')}`,
1110
1168
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1111
- ]
1112
- .join(' ')
1169
+ ].join(' ')
1113
1170
  ]);
1114
1171
  return this;
1115
1172
  }
@@ -1130,8 +1187,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1130
1187
  `${this.$constants('CROSS_JOIN')}`,
1131
1188
  `\`${table}\` ${this.$constants('ON')}`,
1132
1189
  `${this.bindColumn(localKey)} = ${this.bindColumn(referenceKey)}`
1133
- ]
1134
- .join(' ')
1190
+ ].join(' ')
1135
1191
  ]);
1136
1192
  return this;
1137
1193
  }
@@ -1762,21 +1818,37 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1762
1818
  return this.toString();
1763
1819
  }
1764
1820
  /**
1765
- *
1821
+ * The 'getTableName' method is used to get table name
1766
1822
  * @return {string} return table name
1767
1823
  */
1768
1824
  getTableName() {
1769
1825
  return this._getState('TABLE_NAME').replace(/\`/g, '');
1770
1826
  }
1771
1827
  /**
1772
- *
1828
+ * The 'getSchema' method is used to get schema information
1829
+ * @return {this} this this
1830
+ */
1831
+ getSchema() {
1832
+ return __awaiter(this, void 0, void 0, function* () {
1833
+ const sql = [
1834
+ `${this.$constants('SHOW')}`,
1835
+ `${this.$constants('COLUMNS')}`,
1836
+ `${this.$constants('FROM')}`,
1837
+ `\`${this._getState('TABLE_NAME').replace(/\`/g, '')}\``
1838
+ ].join(' ');
1839
+ return yield this._queryStatement(sql);
1840
+ });
1841
+ }
1842
+ /**
1843
+ * The 'bindColumn' method is used to concat table and column -> `users`.`id`
1844
+ * @param {string} column
1773
1845
  * @return {string} return table.column
1774
1846
  */
1775
1847
  bindColumn(column) {
1776
1848
  if (!/\./.test(column))
1777
- return `${this._getState('TABLE_NAME')}.\`${column}\``;
1849
+ return `\`${this.getTableName().replace(/`/g, '')}\`.\`${column.replace(/`/g, '')}\``;
1778
1850
  const [table, c] = column.split('.');
1779
- return `\`${table}\`.\`${c}\``;
1851
+ return `\`${table.replace(/`/g, '')}\`.\`${c.replace(/`/g, '')}\``;
1780
1852
  }
1781
1853
  /**
1782
1854
  * The 'debug' method is used to console.log raw SQL query that would be executed by a query builder
@@ -2043,7 +2115,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2043
2115
  sqlCount,
2044
2116
  this._getState('FROM'),
2045
2117
  this._getState('TABLE_NAME'),
2046
- this._getState('WHERE'),
2118
+ this._queryBuilder().where()
2047
2119
  ].join(' ');
2048
2120
  const count = yield this._queryStatement(sqlTotal);
2049
2121
  const total = ((_b = count === null || count === void 0 ? void 0 : count.shift()) === null || _b === void 0 ? void 0 : _b.total) || 0;
@@ -2096,7 +2168,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2096
2168
  var _a, _b;
2097
2169
  return __awaiter(this, void 0, void 0, function* () {
2098
2170
  if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2099
- this.select(...yield this._exceptColumns());
2171
+ this.select(...yield this.exceptColumns());
2100
2172
  this.limit(1);
2101
2173
  const sql = this._queryBuilder().select();
2102
2174
  const result = yield this._queryStatement(sql);
@@ -2141,7 +2213,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2141
2213
  var _a, _b;
2142
2214
  return __awaiter(this, void 0, void 0, function* () {
2143
2215
  if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2144
- this.select(...yield this._exceptColumns());
2216
+ this.select(...yield this.exceptColumns());
2145
2217
  let sql = this._queryBuilder().select();
2146
2218
  if (!sql.includes(this.$constants('LIMIT')))
2147
2219
  sql = `${sql} ${this.$constants('LIMIT')} 1`;
@@ -2200,7 +2272,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2200
2272
  var _a, _b;
2201
2273
  return __awaiter(this, void 0, void 0, function* () {
2202
2274
  if ((_a = this._getState('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
2203
- this.select(...yield this._exceptColumns());
2275
+ this.select(...yield this.exceptColumns());
2204
2276
  const sql = this._queryBuilder().select();
2205
2277
  const result = yield this._queryStatement(sql);
2206
2278
  if ((_b = this._getState('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
@@ -2394,7 +2466,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2394
2466
  delete() {
2395
2467
  var _a;
2396
2468
  return __awaiter(this, void 0, void 0, function* () {
2397
- if (!this._getState('WHERE')) {
2469
+ if (!this._getState('where').length) {
2398
2470
  throw new Error("can't delete without where condition");
2399
2471
  }
2400
2472
  this.limit(1);
@@ -2418,7 +2490,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2418
2490
  deleteMany() {
2419
2491
  var _a;
2420
2492
  return __awaiter(this, void 0, void 0, function* () {
2421
- if (!this._getState('WHERE')) {
2493
+ if (!this._getState('where').length) {
2422
2494
  throw new Error("can't delete without where condition");
2423
2495
  }
2424
2496
  this._setState('DELETE', [
@@ -2448,7 +2520,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2448
2520
  `${this.$constants('DELETE')}`,
2449
2521
  `${this._getState('FROM')}`,
2450
2522
  `${this._getState('TABLE_NAME')}`,
2451
- `${this._getState('WHERE')}`
2523
+ `${this._queryBuilder().where()}`
2452
2524
  ].join(' '));
2453
2525
  const result = yield this._actionStatement({ sql: this._queryBuilder().delete() });
2454
2526
  if (result)
@@ -2525,29 +2597,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2525
2597
  */
2526
2598
  save() {
2527
2599
  return __awaiter(this, void 0, void 0, function* () {
2528
- const attributes = this.$attributes;
2529
- if (attributes != null) {
2530
- while (true) {
2531
- if (this._getState('WHERE')) {
2532
- const query = this._queryUpdate(attributes);
2533
- this._setState('UPDATE', [
2534
- `${this.$constants('UPDATE')}`,
2535
- `${this._getState('TABLE_NAME')}`,
2536
- `${query}`
2537
- ].join(' '));
2538
- this._setState('SAVE', 'UPDATE');
2539
- break;
2540
- }
2541
- const query = this._queryInsert(attributes);
2542
- this._setState('INSERT', [
2543
- `${this.$constants('INSERT')}`,
2544
- `${this._getState('TABLE_NAME')}`,
2545
- `${query}`
2546
- ].join(' '));
2547
- this._setState('SAVE', 'INSERT');
2548
- break;
2549
- }
2550
- }
2551
2600
  switch (this._getState('SAVE')) {
2552
2601
  case 'INSERT_MULTIPLE': return yield this._insertMultiple();
2553
2602
  case 'INSERT': return yield this._insert();
@@ -2914,10 +2963,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2914
2963
  * @param {number} rows number of rows
2915
2964
  * @return {promise<any>}
2916
2965
  */
2917
- faker(rows = 1) {
2966
+ faker(rows, cb) {
2918
2967
  return __awaiter(this, void 0, void 0, function* () {
2919
2968
  let data = [];
2920
- this.void();
2921
2969
  const sql = [
2922
2970
  `${this.$constants('SHOW')}`,
2923
2971
  `${this.$constants('FIELDS')}`,
@@ -2938,16 +2986,13 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2938
2986
  continue;
2939
2987
  columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
2940
2988
  }
2989
+ if (cb) {
2990
+ data = [...data, cb(columnAndValue, row)];
2991
+ continue;
2992
+ }
2941
2993
  data = [...data, columnAndValue];
2942
2994
  }
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();
2995
+ return yield this.createMultiple(data).save();
2951
2996
  });
2952
2997
  }
2953
2998
  /**
@@ -2980,7 +3025,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2980
3025
  return true;
2981
3026
  });
2982
3027
  }
2983
- _exceptColumns() {
3028
+ exceptColumns() {
2984
3029
  return __awaiter(this, void 0, void 0, function* () {
2985
3030
  const excepts = this._getState('EXCEPTS');
2986
3031
  const hasDot = excepts.some((except) => /\./.test(except));
@@ -3009,7 +3054,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3009
3054
  return except !== column;
3010
3055
  });
3011
3056
  });
3012
- removeExcepts.push(hasDot ? removeExcept.map(r => `\`${tableName}\`.${r}`) : removeExcept);
3057
+ removeExcepts.push(hasDot ? removeExcept.map(r => `${tableName}.${r}`) : removeExcept);
3013
3058
  }
3014
3059
  return removeExcepts.flat();
3015
3060
  });
@@ -3055,7 +3100,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3055
3100
  return this._buildQueryStatement();
3056
3101
  }
3057
3102
  _buildQueryStatement() {
3058
- const buildSQL = (sql) => sql.filter(s => s !== '' || s == null).join(' ');
3103
+ const buildSQL = (sql) => sql.filter(s => s !== '' || s == null).join(' ').replace(/\s+/g, ' ');
3059
3104
  const bindSelect = (values) => {
3060
3105
  if (!values.length) {
3061
3106
  if (!this._getState('DISTINCT'))
@@ -3071,15 +3116,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3071
3116
  };
3072
3117
  const bindJoin = (values) => {
3073
3118
  if (!values.length)
3074
- return '';
3119
+ return null;
3075
3120
  return values.join(' ');
3076
3121
  };
3122
+ const bindWhere = (values) => {
3123
+ if (!values.length)
3124
+ return null;
3125
+ return `${this.$constants('WHERE')} ${values.map(v => v.replace(/^\s/, '').replace(/\s+/g, ' ')).join(' ')}`;
3126
+ };
3077
3127
  const select = () => buildSQL([
3078
3128
  bindSelect(this.$state.get('SELECT')),
3079
3129
  this.$state.get('FROM'),
3080
3130
  this.$state.get('TABLE_NAME'),
3081
3131
  bindJoin(this.$state.get('JOIN')),
3082
- this.$state.get('WHERE'),
3132
+ bindWhere(this.$state.get('WHERE')),
3083
3133
  this.$state.get('GROUP_BY'),
3084
3134
  this.$state.get('HAVING'),
3085
3135
  this.$state.get('ORDER_BY'),
@@ -3087,13 +3137,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3087
3137
  this.$state.get('OFFSET')
3088
3138
  ]);
3089
3139
  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')]);
3140
+ const update = () => buildSQL([this.$state.get('UPDATE'), bindWhere(this.$state.get('WHERE')), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3141
+ const remove = () => buildSQL([this.$state.get('DELETE'), bindWhere(this.$state.get('WHERE')), this.$state.get('ORDER_BY'), this.$state.get('LIMIT')]);
3092
3142
  return {
3093
3143
  select,
3094
3144
  insert,
3095
3145
  update,
3096
3146
  delete: remove,
3147
+ where: () => bindWhere(this.$state.get('WHERE')),
3097
3148
  any: () => {
3098
3149
  if (this.$state.get('INSERT'))
3099
3150
  return insert();
@@ -3119,11 +3170,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3119
3170
  }
3120
3171
  whereReference(tableAndLocalKey, tableAndForeignKey) {
3121
3172
  this._setState('WHERE', [
3122
- this._queryWhereIsExists()
3123
- ? `${this._getState('WHERE')} ${this.$constants('AND')}`
3124
- : `${this.$constants('WHERE')}`,
3125
- `${tableAndLocalKey} = ${tableAndForeignKey}`
3126
- ].join(' '));
3173
+ ...this._getState('WHERE'),
3174
+ [
3175
+ this._getState('WHERE').length ? `${this.$constants('AND')}` : '',
3176
+ `${tableAndLocalKey} = ${tableAndForeignKey}`
3177
+ ].join(' ')
3178
+ ]);
3127
3179
  return this;
3128
3180
  }
3129
3181
  _queryStatement(sql) {
@@ -3146,13 +3198,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3146
3198
  return result;
3147
3199
  });
3148
3200
  }
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
3201
  _insertNotExists() {
3154
3202
  return __awaiter(this, void 0, void 0, function* () {
3155
- if (!this._getState('WHERE'))
3203
+ if (!this._getState('where').length)
3156
3204
  throw new Error("Can't insert not exists without where condition");
3157
3205
  let sql = [
3158
3206
  `${this.$constants('SELECT')}`,
@@ -3160,7 +3208,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3160
3208
  `*`,
3161
3209
  `${this._getState('FROM')}`,
3162
3210
  `${this._getState('TABLE_NAME')}`,
3163
- `${this._getState('WHERE')}`,
3211
+ `${this._queryBuilder().where()}`,
3164
3212
  `${this.$constants('LIMIT')} 1)`,
3165
3213
  `${this.$constants('AS')} 'exists'`
3166
3214
  ].join(' ');
@@ -3228,7 +3276,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3228
3276
  }
3229
3277
  _insertOrSelect() {
3230
3278
  return __awaiter(this, void 0, void 0, function* () {
3231
- if (!this._getState('WHERE')) {
3279
+ if (!this._getState('where').length) {
3232
3280
  throw new Error("Can't create or select without where condition");
3233
3281
  }
3234
3282
  let sql = [
@@ -3237,7 +3285,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3237
3285
  `*`,
3238
3286
  `${this._getState('FROM')}`,
3239
3287
  `${this._getState('TABLE_NAME')}`,
3240
- `${this._getState('WHERE')}`,
3288
+ `${this._queryBuilder().where()}`,
3241
3289
  `${this.$constants('LIMIT')} 1)`,
3242
3290
  `${this.$constants('AS')} 'exists'`
3243
3291
  ].join(' ');
@@ -3282,7 +3330,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3282
3330
  }
3283
3331
  _updateOrInsert() {
3284
3332
  return __awaiter(this, void 0, void 0, function* () {
3285
- if (!this._getState('WHERE')) {
3333
+ if (!this._getState('where').length) {
3286
3334
  throw new Error("Can't update or insert without where condition");
3287
3335
  }
3288
3336
  let sql = [
@@ -3291,7 +3339,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3291
3339
  `*`,
3292
3340
  `${this._getState('FROM')}`,
3293
3341
  `${this._getState('TABLE_NAME')}`,
3294
- `${this._getState('WHERE')}`,
3342
+ `${this._queryBuilder().where()}`,
3295
3343
  `${this.$constants('LIMIT')} 1)`,
3296
3344
  `${this.$constants('AS')} 'exists'`
3297
3345
  ].join(' ');
@@ -3338,7 +3386,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3338
3386
  }
3339
3387
  _update(ignoreWhere = false) {
3340
3388
  return __awaiter(this, void 0, void 0, function* () {
3341
- if (!this._getState('WHERE') && !ignoreWhere)
3389
+ if (!this._getState('where').length && !ignoreWhere)
3342
3390
  throw new Error("can't update without where condition");
3343
3391
  const result = yield this._actionStatement({
3344
3392
  sql: this._queryBuilder().update()
@@ -3471,7 +3519,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3471
3519
  throw new Error(`Not found constants : ${name}`);
3472
3520
  return constants_1.CONSTANTS[name.toUpperCase()];
3473
3521
  };
3474
- this.$state = new StateHandler_1.StateHandler(this.$constants('DEFAULT'));
3522
+ this.$state = new State_1.StateHandler(this.$constants('DEFAULT'));
3475
3523
  }
3476
3524
  }
3477
3525
  exports.Builder = Builder;