tspace-mysql 1.3.3 → 1.3.5

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.
@@ -31,6 +31,7 @@ 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
35
  class Builder extends AbstractBuilder_1.AbstractBuilder {
35
36
  constructor() {
36
37
  super();
@@ -130,8 +131,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
130
131
  * @return {this} this
131
132
  */
132
133
  when(condition, callback) {
133
- if (condition)
134
- callback(this);
134
+ if (!condition)
135
+ return this;
136
+ const cb = callback(this);
137
+ if (cb instanceof Promise)
138
+ throw new Error('"when" is not supported a Promise');
135
139
  return this;
136
140
  }
137
141
  /**
@@ -227,21 +231,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
227
231
  ].join(' '));
228
232
  return this;
229
233
  }
230
- /**
231
- *
232
- * @param {string} tableAndLocalKey
233
- * @param {string?} tableAndForeignKey
234
- * @return {this}
235
- */
236
- whereReference(tableAndLocalKey, tableAndForeignKey) {
237
- this.$state.set('WHERE', [
238
- this._queryWhereIsExists()
239
- ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
240
- : `${this.$constants('WHERE')}`,
241
- `${tableAndLocalKey} = ${tableAndForeignKey}`
242
- ].join(' '));
243
- return this;
244
- }
245
234
  /**
246
235
  *
247
236
  * where exists
@@ -480,6 +469,27 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
480
469
  ].join(' '));
481
470
  return this;
482
471
  }
472
+ /**
473
+ * where between using [value1, value2]
474
+ * @param {string} column
475
+ * @param {array} array
476
+ * @return {this}
477
+ */
478
+ orWhereBetween(column, array) {
479
+ if (!Array.isArray(array))
480
+ throw new Error("Value is't array");
481
+ if (!array.length)
482
+ return this;
483
+ const [value1, value2] = array;
484
+ this.$state.set('WHERE', [
485
+ this._queryWhereIsExists()
486
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
487
+ : `${this.$constants('WHERE')}`,
488
+ `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
489
+ `${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
490
+ ].join(' '));
491
+ return this;
492
+ }
483
493
  /**
484
494
  * where not between using [value1, value2]
485
495
  * @param {string} column
@@ -516,6 +526,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
516
526
  ].join(' '));
517
527
  return this;
518
528
  }
529
+ /**
530
+ * where null using NULL
531
+ * @param {string} column
532
+ * @return {this}
533
+ */
534
+ orWhereNull(column) {
535
+ this.$state.set('WHERE', [
536
+ this._queryWhereIsExists()
537
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
538
+ : `${this.$constants('WHERE')}`,
539
+ `${this._bindTableAndColumnInQueryWhere(column)}`,
540
+ `${this.$constants('IS_NULL')}`
541
+ ].join(' '));
542
+ return this;
543
+ }
519
544
  /**
520
545
  * where not null using NULL
521
546
  * @param {string} column
@@ -531,6 +556,21 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
531
556
  ].join(' '));
532
557
  return this;
533
558
  }
559
+ /**
560
+ * where not null using NULL
561
+ * @param {string} column
562
+ * @return {this}
563
+ */
564
+ orWhereNotNull(column) {
565
+ this.$state.set('WHERE', [
566
+ this._queryWhereIsExists()
567
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
568
+ : `${this.$constants('WHERE')}`,
569
+ `${this._bindTableAndColumnInQueryWhere(column)}`,
570
+ `${this.$constants('IS_NOT_NULL')}`
571
+ ].join(' '));
572
+ return this;
573
+ }
534
574
  /**
535
575
  * where sensitive (uppercase, lowercase)
536
576
  * @param {string} column
@@ -562,6 +602,27 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
562
602
  whereStrict(column, operator, value) {
563
603
  return this.whereSensitive(column, operator, value);
564
604
  }
605
+ /**
606
+ * or where sensitive (uppercase, lowercase)
607
+ * @param {string} column
608
+ * @param {string?} operator = < > != !< !>
609
+ * @param {any?} value
610
+ * @return {this}
611
+ */
612
+ orWhereSensitive(column, operator, value) {
613
+ [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
614
+ value = this.$utils.escape(value);
615
+ value = this._valueTrueFalse(value);
616
+ this.$state.set('WHERE', [
617
+ this._queryWhereIsExists()
618
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
619
+ : `${this.$constants('WHERE')}`,
620
+ `BINARY ${this._bindTableAndColumnInQueryWhere(column)}`,
621
+ `${operator}`,
622
+ `${this._checkValueHasRaw(this.$utils.escape(value))}`
623
+ ].join(' '));
624
+ return this;
625
+ }
565
626
  /**
566
627
  * where group query
567
628
  * @param {function} callback callback query
@@ -571,9 +632,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
571
632
  var _a;
572
633
  const db = new DB_1.DB((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
573
634
  const repository = callback(db);
574
- if (!(repository instanceof DB_1.DB)) {
575
- throw new Error(`unknown callback query: '[${repository}]'`);
576
- }
635
+ if (repository instanceof Promise)
636
+ throw new Error('"whereQuery" is not supported a Promise');
637
+ if (!(repository instanceof DB_1.DB))
638
+ throw new Error(`Unknown callback query: '[${repository}]'`);
577
639
  const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || '';
578
640
  if (where === '')
579
641
  return this;
@@ -586,6 +648,39 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
586
648
  ].join(' '));
587
649
  return this;
588
650
  }
651
+ /**
652
+ * where group query
653
+ * @param {function} callback callback query
654
+ * @return {this}
655
+ */
656
+ whereGroup(callback) {
657
+ return this.whereQuery(callback);
658
+ }
659
+ /**
660
+ * where group query
661
+ * @param {function} callback callback query
662
+ * @return {this}
663
+ */
664
+ orWhereQuery(callback) {
665
+ var _a;
666
+ const db = new DB_1.DB((_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, ''));
667
+ const repository = callback(db);
668
+ if (repository instanceof Promise)
669
+ throw new Error('"whereQuery" is not supported a Promise');
670
+ if (!(repository instanceof DB_1.DB))
671
+ throw new Error(`Unknown callback query: '[${repository}]'`);
672
+ const where = (repository === null || repository === void 0 ? void 0 : repository.$state.get('WHERE')) || '';
673
+ if (where === '')
674
+ return this;
675
+ const query = where.replace('WHERE', '');
676
+ this.$state.set('WHERE', [
677
+ this._queryWhereIsExists()
678
+ ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
679
+ : `${this.$constants('WHERE')}`,
680
+ `(${query})`
681
+ ].join(' '));
682
+ return this;
683
+ }
589
684
  /**
590
685
  * select by cases
591
686
  * @param {array} cases array object [{ when : 'id < 7' , then : 'id is than under 7'}]
@@ -1248,8 +1343,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1248
1343
  ].join(' ');
1249
1344
  const rawColumns = yield this.queryStatement(sql);
1250
1345
  const columns = rawColumns.map((column) => column.Field);
1251
- const removeExcept = columns.filter((column) => !this.$state.get('EXCEPT').includes(column));
1252
- return removeExcept.join(', ');
1346
+ const removeExcept = columns.filter((column) => !String(this.$state.get('EXCEPT')).includes(column));
1347
+ return removeExcept;
1253
1348
  });
1254
1349
  }
1255
1350
  /**
@@ -1339,7 +1434,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1339
1434
  * @return {promise<Pagination>}
1340
1435
  */
1341
1436
  pagination(paginationOptions) {
1342
- var _a, _b;
1437
+ var _a, _b, _c;
1343
1438
  return __awaiter(this, void 0, void 0, function* () {
1344
1439
  let limit = 15;
1345
1440
  let page = 1;
@@ -1352,7 +1447,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1352
1447
  const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
1353
1448
  const offset = (page - 1) * limit;
1354
1449
  let sql = this._buildQuery();
1355
- sql = sql.replace(this.$state.get('LIMIT'), `${limit} ${this.$constants('OFFSET')} ${offset}`);
1356
1450
  if (!sql.includes(this.$constants('LIMIT'))) {
1357
1451
  sql = [
1358
1452
  `${sql}`,
@@ -1361,6 +1455,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1361
1455
  `${this.$constants('OFFSET')} ${offset}`
1362
1456
  ].join(' ');
1363
1457
  }
1458
+ else {
1459
+ sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} ${limit} ${this.$constants('OFFSET')} ${offset}`);
1460
+ }
1364
1461
  const result = yield this.queryStatement(sql);
1365
1462
  if ((_a = this.$state.get('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
1366
1463
  this._hiddenColumn(result);
@@ -1382,17 +1479,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1382
1479
  `${this.$constants('COUNT')}(*)`,
1383
1480
  `${this.$constants('AS')} total`
1384
1481
  ].join(' '));
1385
- sql = this._buildQuery();
1386
- const count = yield this.queryStatement(sql);
1387
- const total = count.shift().total || 0;
1482
+ const sqlTotal = [
1483
+ this.$state.get('SELECT'),
1484
+ this.$state.get('FROM'),
1485
+ this.$state.get('TABLE_NAME'),
1486
+ this.$state.get('WHERE'),
1487
+ ].join(' ');
1488
+ const count = yield this.queryStatement(sqlTotal);
1489
+ const total = ((_b = count === null || count === void 0 ? void 0 : count.shift()) === null || _b === void 0 ? void 0 : _b.total) || 0;
1388
1490
  let lastPage = Math.ceil(total / limit) || 0;
1389
1491
  lastPage = lastPage > 1 ? lastPage : 1;
1390
- const totalPage = (_b = result === null || result === void 0 ? void 0 : result.length) !== null && _b !== void 0 ? _b : 0;
1492
+ const totalPage = (_c = result === null || result === void 0 ? void 0 : result.length) !== null && _c !== void 0 ? _c : 0;
1391
1493
  return {
1392
1494
  meta: {
1495
+ total: total,
1496
+ limit: limit,
1393
1497
  total_page: totalPage,
1394
- total,
1395
- limit,
1396
1498
  current_page: currentPage,
1397
1499
  last_page: lastPage,
1398
1500
  next_page: nextPage,
@@ -1430,7 +1532,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1430
1532
  var _a, _b;
1431
1533
  return __awaiter(this, void 0, void 0, function* () {
1432
1534
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1433
- this.select(yield this.exceptColumns());
1535
+ this.select(...yield this.exceptColumns());
1434
1536
  this.limit(1);
1435
1537
  let sql = this._buildQuery();
1436
1538
  const result = yield this.queryStatement(sql);
@@ -1452,7 +1554,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1452
1554
  const hook = this.$state.get('HOOK');
1453
1555
  for (let i in hook)
1454
1556
  yield hook[i](r);
1455
- return r;
1557
+ return this.resultHandler(r);
1456
1558
  });
1457
1559
  }
1458
1560
  /**
@@ -1474,7 +1576,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1474
1576
  var _a, _b;
1475
1577
  return __awaiter(this, void 0, void 0, function* () {
1476
1578
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1477
- this.select(yield this.exceptColumns());
1579
+ this.select(...yield this.exceptColumns());
1478
1580
  let sql = this._buildQuery();
1479
1581
  if (!sql.includes(this.$constants('LIMIT')))
1480
1582
  sql = `${sql} ${this.$constants('LIMIT')} 1`;
@@ -1499,7 +1601,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1499
1601
  const hook = this.$state.get('HOOK');
1500
1602
  for (let i in hook)
1501
1603
  yield hook[i](data);
1502
- return data;
1604
+ return this.resultHandler(data);
1503
1605
  }
1504
1606
  const data = (result === null || result === void 0 ? void 0 : result.shift()) || null;
1505
1607
  if (data == null) {
@@ -1511,7 +1613,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1511
1613
  const hook = this.$state.get('HOOK');
1512
1614
  for (let i in hook)
1513
1615
  yield hook[i](data);
1514
- return data;
1616
+ return this.resultHandler(data);
1515
1617
  });
1516
1618
  }
1517
1619
  /**
@@ -1533,7 +1635,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1533
1635
  var _a, _b;
1534
1636
  return __awaiter(this, void 0, void 0, function* () {
1535
1637
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1536
- this.select(yield this.exceptColumns());
1638
+ this.select(...yield this.exceptColumns());
1537
1639
  const sql = this._buildQuery();
1538
1640
  const result = yield this.queryStatement(sql);
1539
1641
  if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
@@ -1549,7 +1651,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1549
1651
  const hook = this.$state.get('HOOK');
1550
1652
  for (let i in hook)
1551
1653
  yield hook[i](data || []);
1552
- return data || [];
1654
+ return this.resultHandler(data || []);
1553
1655
  }
1554
1656
  if (this.$state.get('PLUCK')) {
1555
1657
  const pluck = this.$state.get('PLUCK');
@@ -1560,12 +1662,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1560
1662
  const hook = this.$state.get('HOOK');
1561
1663
  for (let i in hook)
1562
1664
  yield hook[i](newData || []);
1563
- return newData || [];
1665
+ return this.resultHandler(newData || []);
1564
1666
  }
1565
1667
  const hook = this.$state.get('HOOK');
1566
1668
  for (let i in hook)
1567
1669
  yield hook[i](result || []);
1568
- return result || [];
1670
+ return this.resultHandler(result || []);
1569
1671
  });
1570
1672
  }
1571
1673
  /**
@@ -1587,12 +1689,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1587
1689
  var _a, _b;
1588
1690
  return __awaiter(this, void 0, void 0, function* () {
1589
1691
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1590
- this.select(yield this.exceptColumns());
1692
+ this.select(...yield this.exceptColumns());
1591
1693
  const sql = this._buildQuery();
1592
1694
  const result = yield this.queryStatement(sql);
1593
1695
  if ((_b = this.$state.get('HIDDEN')) === null || _b === void 0 ? void 0 : _b.length)
1594
1696
  this._hiddenColumn(result);
1595
- return JSON.stringify(result);
1697
+ return this.resultHandler(JSON.stringify(result));
1596
1698
  });
1597
1699
  }
1598
1700
  /**
@@ -1607,7 +1709,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1607
1709
  const sql = this._buildQuery();
1608
1710
  const result = yield this.queryStatement(sql);
1609
1711
  const toArray = result.map((data) => data[column]);
1610
- return toArray;
1712
+ return this.resultHandler(toArray);
1611
1713
  });
1612
1714
  }
1613
1715
  /**
@@ -1626,7 +1728,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1626
1728
  ].join(' '));
1627
1729
  const sql = this._buildQuery();
1628
1730
  const result = yield this.queryStatement(sql);
1629
- return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0;
1731
+ this.$state.resetState();
1732
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0));
1630
1733
  });
1631
1734
  }
1632
1735
  /**
@@ -1646,7 +1749,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1646
1749
  `${this.$state.get('WHERE')}`,
1647
1750
  `${this.$constants('LIMIT')} 1) ${this.$constants('AS')} 'exists'`
1648
1751
  ].join(' '));
1649
- return !!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false;
1752
+ return Boolean(this.resultHandler(!!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false));
1650
1753
  });
1651
1754
  }
1652
1755
  /**
@@ -1665,7 +1768,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1665
1768
  ].join(' '));
1666
1769
  const sql = this._buildQuery();
1667
1770
  const result = yield this.queryStatement(sql);
1668
- return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0;
1771
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0));
1669
1772
  });
1670
1773
  }
1671
1774
  /**
@@ -1680,7 +1783,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1680
1783
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('SUM')}(${column}) ${this.$constants('AS')} sum`);
1681
1784
  const sql = this._buildQuery();
1682
1785
  const result = yield this.queryStatement(sql);
1683
- return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0;
1786
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0));
1684
1787
  });
1685
1788
  }
1686
1789
  /**
@@ -1695,7 +1798,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1695
1798
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('MAX')}(${column}) ${this.$constants('AS')} max`);
1696
1799
  const sql = this._buildQuery();
1697
1800
  const result = yield this.queryStatement(sql);
1698
- return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0;
1801
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0));
1699
1802
  });
1700
1803
  }
1701
1804
  /**
@@ -1710,7 +1813,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1710
1813
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${this.$constants('MIN')}(${column}) ${this.$constants('AS')} min`);
1711
1814
  const sql = this._buildQuery();
1712
1815
  const result = yield this.queryStatement(sql);
1713
- return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0;
1816
+ return Number(this.resultHandler(((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0));
1714
1817
  });
1715
1818
  }
1716
1819
  /**
@@ -1732,8 +1835,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1732
1835
  ].join(' '));
1733
1836
  const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
1734
1837
  if (result)
1735
- return (_a = !!result) !== null && _a !== void 0 ? _a : false;
1736
- return false;
1838
+ return Boolean(this.resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
1839
+ return Boolean(this.resultHandler(false));
1737
1840
  });
1738
1841
  }
1739
1842
  /**
@@ -1742,7 +1845,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1742
1845
  * @return {promise<boolean>}
1743
1846
  */
1744
1847
  forceDelete() {
1745
- var _a;
1848
+ var _a, _b;
1746
1849
  return __awaiter(this, void 0, void 0, function* () {
1747
1850
  this.$state.set('DELETE', [
1748
1851
  `${this.$constants('DELETE')}`,
@@ -1752,8 +1855,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1752
1855
  ].join(' '));
1753
1856
  const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
1754
1857
  if (result)
1755
- return (_a = !!result) !== null && _a !== void 0 ? _a : false;
1756
- return false;
1858
+ return Boolean(this.resultHandler((_a = !!result) !== null && _a !== void 0 ? _a : false));
1859
+ return Boolean(this.resultHandler((_b = !!result) !== null && _b !== void 0 ? _b : false));
1757
1860
  });
1758
1861
  }
1759
1862
  /**
@@ -1796,7 +1899,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1796
1899
  data: newData
1797
1900
  });
1798
1901
  });
1799
- return resultData;
1902
+ return this.resultHandler(!resultData);
1800
1903
  });
1801
1904
  }
1802
1905
  /**
@@ -2134,9 +2237,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2134
2237
  return true;
2135
2238
  });
2136
2239
  }
2240
+ resultHandler(data) {
2241
+ this.$state.set('RESULT', data);
2242
+ this.$state.resetState();
2243
+ this.$logger.reset();
2244
+ return data;
2245
+ }
2246
+ /**
2247
+ *
2248
+ * @param {string} tableAndLocalKey
2249
+ * @param {string?} tableAndForeignKey
2250
+ * @return {this}
2251
+ */
2252
+ whereReference(tableAndLocalKey, tableAndForeignKey) {
2253
+ this.$state.set('WHERE', [
2254
+ this._queryWhereIsExists()
2255
+ ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
2256
+ : `${this.$constants('WHERE')}`,
2257
+ `${tableAndLocalKey} = ${tableAndForeignKey}`
2258
+ ].join(' '));
2259
+ return this;
2260
+ }
2137
2261
  _queryWhereIsExists() {
2138
2262
  var _a;
2139
- return ((_a = this.$state.get('WHERE')) === null || _a === void 0 ? void 0 : _a.includes(this.$constants('WHERE'))) || false;
2263
+ return ((_a = String(this.$state.get('WHERE'))) === null || _a === void 0 ? void 0 : _a.includes(this.$constants('WHERE'))) || false;
2140
2264
  }
2141
2265
  _bindTableAndColumnInQueryWhere(column) {
2142
2266
  return `${this.$state.get('TABLE_NAME')}.\`${column}\``;
@@ -2163,21 +2287,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2163
2287
  sql: this.$state.get('INSERT'),
2164
2288
  returnId: true
2165
2289
  });
2166
- if (this.$state.get('VOID'))
2167
- return null;
2168
- if (result) {
2169
- const sql = [
2170
- `${this.$state.get('SELECT')}`,
2171
- `${this.$state.get('FROM')}`,
2172
- `${this.$state.get('TABLE_NAME')}`,
2173
- `${this.$constants('WHERE')} id = ${id}`
2174
- ].join(' ');
2175
- const data = yield this.queryStatement(sql);
2176
- return (data === null || data === void 0 ? void 0 : data.shift()) || null;
2177
- }
2178
- return null;
2290
+ if (this.$state.get('VOID') || !result)
2291
+ return this.resultHandler(null);
2292
+ const sql = [
2293
+ `${this.$state.get('SELECT')}`,
2294
+ `${this.$state.get('FROM')}`,
2295
+ `${this.$state.get('TABLE_NAME')}`,
2296
+ `${this.$constants('WHERE')} id = ${id}`
2297
+ ].join(' ');
2298
+ const data = yield this.queryStatement(sql);
2299
+ return this.resultHandler((data === null || data === void 0 ? void 0 : data.shift()) || null);
2179
2300
  }
2180
- default: return null;
2301
+ default: return this.resultHandler(null);
2181
2302
  }
2182
2303
  });
2183
2304
  }
@@ -2207,21 +2328,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2207
2328
  sql: this.$state.get('INSERT'),
2208
2329
  returnId: true
2209
2330
  });
2210
- if (this.$state.get('VOID'))
2211
- return null;
2212
- if (result) {
2213
- const sql = [
2214
- `${this.$state.get('SELECT')}`,
2215
- `${this.$state.get('FROM')}`,
2216
- `${this.$state.get('TABLE_NAME')}`,
2217
- `${this.$constants('WHERE')} ${this.$state.get('TABLE_NAME')}.\`id\` = ${id}`
2218
- ].join(' ');
2219
- const data = yield this.queryStatement(sql);
2220
- const result = (data === null || data === void 0 ? void 0 : data.shift()) || null;
2221
- this.$state.set('RESULT', result);
2222
- return result;
2223
- }
2224
- return null;
2331
+ if (this.$state.get('VOID') || !result)
2332
+ return this.resultHandler(null);
2333
+ const sql = [
2334
+ `${this.$state.get('SELECT')}`,
2335
+ `${this.$state.get('FROM')}`,
2336
+ `${this.$state.get('TABLE_NAME')}`,
2337
+ `${this.$constants('WHERE')} ${this.$state.get('TABLE_NAME')}.\`id\` = ${id}`
2338
+ ].join(' ');
2339
+ const data = yield this.queryStatement(sql);
2340
+ const resultData = (data === null || data === void 0 ? void 0 : data.shift()) || null;
2341
+ this.$state.set('RESULT', resultData);
2342
+ return this.resultHandler(resultData);
2225
2343
  });
2226
2344
  }
2227
2345
  _checkValueHasRaw(value) {
@@ -2235,23 +2353,19 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2235
2353
  sql: this.$state.get('INSERT'),
2236
2354
  returnId: true
2237
2355
  });
2238
- if (this.$state.get('VOID'))
2239
- return null;
2240
- if (result) {
2241
- const arrayId = [...Array(result)].map((_, i) => i + id);
2242
- const sql = [
2243
- `${this.$state.get('SELECT')}`,
2244
- `${this.$state.get('FROM')}`,
2245
- `${this.$state.get('TABLE_NAME')}`,
2246
- `${this.$constants('WHERE')} id`,
2247
- `${this.$constants('IN')} (${arrayId})`
2248
- ].join(' ');
2249
- const data = yield this.queryStatement(sql);
2250
- const resultData = data || null;
2251
- this.$state.set('RESULT', resultData);
2252
- return resultData;
2253
- }
2254
- return null;
2356
+ if (this.$state.get('VOID') || !result)
2357
+ return this.resultHandler(null);
2358
+ const arrayId = [...Array(result)].map((_, i) => i + id);
2359
+ const sql = [
2360
+ `${this.$state.get('SELECT')}`,
2361
+ `${this.$state.get('FROM')}`,
2362
+ `${this.$state.get('TABLE_NAME')}`,
2363
+ `${this.$constants('WHERE')} id`,
2364
+ `${this.$constants('IN')} (${arrayId})`
2365
+ ].join(' ');
2366
+ const data = yield this.queryStatement(sql);
2367
+ const resultData = data || null;
2368
+ return this.resultHandler(resultData);
2255
2369
  });
2256
2370
  }
2257
2371
  _insertOrSelect() {
@@ -2278,21 +2392,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2278
2392
  sql: this.$state.get('INSERT'),
2279
2393
  returnId: true
2280
2394
  });
2281
- if (this.$state.get('VOID'))
2282
- return null;
2283
- if (result) {
2284
- const sql = [
2285
- `${this.$state.get('SELECT')}`,
2286
- `${this.$state.get('FROM')}`,
2287
- `${this.$state.get('TABLE_NAME')}`,
2288
- `${this.$constants('WHERE')} id = ${id}`
2289
- ].join(' ');
2290
- const data = yield this.queryStatement(sql);
2291
- const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'insert' }) || null;
2292
- this.$state.set('RESULT', resultData);
2293
- return resultData;
2294
- }
2295
- return null;
2395
+ if (this.$state.get('VOID') || !result)
2396
+ return this.resultHandler(null);
2397
+ const sql = [
2398
+ `${this.$state.get('SELECT')}`,
2399
+ `${this.$state.get('FROM')}`,
2400
+ `${this.$state.get('TABLE_NAME')}`,
2401
+ `${this.$constants('WHERE')} id = ${id}`
2402
+ ].join(' ');
2403
+ const data = yield this.queryStatement(sql);
2404
+ const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'insert' }) || null;
2405
+ return this.resultHandler(resultData);
2296
2406
  }
2297
2407
  case true: {
2298
2408
  const data = yield this.queryStatement([
@@ -2303,14 +2413,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2303
2413
  ].join(' '));
2304
2414
  if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
2305
2415
  for (const val of data) {
2306
- val.action_status = 'select';
2416
+ val.$action = 'select';
2307
2417
  }
2308
- return data || [];
2418
+ return this.resultHandler(data || []);
2309
2419
  }
2310
- return Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'select' }) || null;
2420
+ const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'select' }) || null;
2421
+ return this.resultHandler(resultData);
2311
2422
  }
2312
2423
  default: {
2313
- return null;
2424
+ return this.resultHandler(null);
2314
2425
  }
2315
2426
  }
2316
2427
  });
@@ -2339,21 +2450,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2339
2450
  sql: this.$state.get('INSERT'),
2340
2451
  returnId: true
2341
2452
  });
2342
- if (this.$state.get('VOID'))
2343
- return null;
2344
- if (result) {
2345
- const sql = [
2346
- `${this.$state.get('SELECT')}`,
2347
- `${this.$state.get('FROM')}`,
2348
- `${this.$state.get('TABLE_NAME')}`,
2349
- `${this.$constants('WHERE')} id = ${id}`
2350
- ].join(' ');
2351
- const data = yield this.queryStatement(sql);
2352
- const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'insert' }) || null;
2353
- this.$state.set('RESULT', resultData);
2354
- return resultData;
2355
- }
2356
- return null;
2453
+ if (this.$state.get('VOID') || !result)
2454
+ return this.resultHandler(null);
2455
+ const sql = [
2456
+ `${this.$state.get('SELECT')}`,
2457
+ `${this.$state.get('FROM')}`,
2458
+ `${this.$state.get('TABLE_NAME')}`,
2459
+ `${this.$constants('WHERE')} id = ${id}`
2460
+ ].join(' ');
2461
+ const data = yield this.queryStatement(sql);
2462
+ const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'insert' }) || null;
2463
+ return this.resultHandler(resultData);
2357
2464
  }
2358
2465
  case true: {
2359
2466
  const result = yield this.actionStatement({
@@ -2362,27 +2469,25 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2362
2469
  `${this.$state.get('WHERE')}`
2363
2470
  ].join(' ')
2364
2471
  });
2365
- if (this.$state.get('VOID'))
2366
- return null;
2367
- if (result) {
2368
- const data = yield this.queryStatement([
2369
- `${this.$state.get('SELECT')}`,
2370
- `${this.$state.get('FROM')}`,
2371
- `${this.$state.get('TABLE_NAME')}`,
2372
- `${this.$state.get('WHERE')}`
2373
- ].join(' '));
2374
- if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
2375
- for (const val of data) {
2376
- val.action_status = 'update';
2377
- }
2378
- return data || [];
2472
+ if (this.$state.get('VOID') || !result)
2473
+ return this.resultHandler(null);
2474
+ const data = yield this.queryStatement([
2475
+ `${this.$state.get('SELECT')}`,
2476
+ `${this.$state.get('FROM')}`,
2477
+ `${this.$state.get('TABLE_NAME')}`,
2478
+ `${this.$state.get('WHERE')}`
2479
+ ].join(' '));
2480
+ if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
2481
+ for (const val of data) {
2482
+ val.$action = 'update';
2379
2483
  }
2380
- return Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'update' }) || null;
2484
+ return this.resultHandler(data || []);
2381
2485
  }
2382
- return null;
2486
+ const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { $action: 'update' }) || null;
2487
+ return this.resultHandler(resultData);
2383
2488
  }
2384
2489
  default: {
2385
- return null;
2490
+ return this.resultHandler(null);
2386
2491
  }
2387
2492
  }
2388
2493
  });
@@ -2396,10 +2501,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2396
2501
  `${this.$state.get('UPDATE')}`, `${this.$state.get('WHERE')}`
2397
2502
  ].join(' ')
2398
2503
  });
2399
- if (this.$state.get('VOID'))
2400
- return null;
2401
- if (!result)
2402
- return null;
2504
+ if (this.$state.get('VOID') || !result)
2505
+ return this.resultHandler(null);
2403
2506
  const sql = [
2404
2507
  `${this.$state.get('SELECT')}`,
2405
2508
  `${this.$state.get('FROM')}`,
@@ -2408,10 +2511,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2408
2511
  ].join(' ');
2409
2512
  const data = yield this.queryStatement(sql);
2410
2513
  if ((data === null || data === void 0 ? void 0 : data.length) > 1)
2411
- return data || [];
2514
+ return this.resultHandler(data || []);
2412
2515
  const res = (data === null || data === void 0 ? void 0 : data.shift()) || null;
2413
- this.$state.set('RESULT', res);
2414
- return res;
2516
+ return this.resultHandler(res);
2415
2517
  });
2416
2518
  }
2417
2519
  _hiddenColumn(data) {
@@ -2481,7 +2583,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2481
2583
  if (useDefault)
2482
2584
  return [operator, '='];
2483
2585
  if (operator == null)
2484
- throw new Error('bad arguments');
2586
+ throw new Error('Bad arguments');
2485
2587
  if (operator.toUpperCase() === this.$constants('LIKE')) {
2486
2588
  operator = operator.toUpperCase();
2487
2589
  }
@@ -2512,7 +2614,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2512
2614
  }
2513
2615
  if (this.$state.get('DELETE')) {
2514
2616
  sql = [
2515
- this.$state.get('DELETE')
2617
+ this.$state.get('DELETE'),
2618
+ this.$state.get('WHERE')
2516
2619
  ];
2517
2620
  break;
2518
2621
  }
@@ -2553,6 +2656,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2553
2656
  logger = [...logger, data];
2554
2657
  return;
2555
2658
  },
2659
+ reset: () => {
2660
+ logger = [];
2661
+ return;
2662
+ },
2556
2663
  check: (data) => logger.indexOf(data) != -1
2557
2664
  };
2558
2665
  })();
@@ -2560,9 +2667,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2560
2667
  if (name == null)
2561
2668
  return constants_1.CONSTANTS;
2562
2669
  if (!constants_1.CONSTANTS.hasOwnProperty(name.toUpperCase()))
2563
- throw new Error(`not found constants : ${name}`);
2670
+ throw new Error(`Not found constants : ${name}`);
2564
2671
  return constants_1.CONSTANTS[name.toUpperCase()];
2565
2672
  };
2673
+ this.$state = new StateHandler_1.StateHandler(this.$constants('DEFAULT'));
2566
2674
  }
2567
2675
  }
2568
2676
  exports.Builder = Builder;