@carbonorm/carbonnode 3.9.5 → 3.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -11,6 +11,7 @@ var C6Constants = {
11
11
  ADDTIME: 'ADDTIME',
12
12
  AS: 'AS',
13
13
  ASC: 'ASC',
14
+ AND: 'AND',
14
15
  BETWEEN: 'BETWEEN',
15
16
  CONCAT: 'CONCAT',
16
17
  CONVERT_TZ: 'CONVERT_TZ',
@@ -84,6 +85,7 @@ var C6Constants = {
84
85
  NOW: 'NOW',
85
86
  NULL: 'NULL',
86
87
  ORDER: 'ORDER',
88
+ OR: 'OR',
87
89
  PAGE: 'PAGE',
88
90
  PAGINATION: 'PAGINATION',
89
91
  RIGHT_OUTER: 'RIGHT_OUTER',
@@ -1449,24 +1451,43 @@ var ConditionBuilder = /** @class */ (function (_super) {
1449
1451
  var _this = _super !== null && _super.apply(this, arguments) || this;
1450
1452
  _this.aliasMap = {};
1451
1453
  _this.derivedAliases = new Set();
1452
- _this.OPERATORS = new Set([
1453
- C6C.EQUAL, C6C.NOT_EQUAL, C6C.LESS_THAN, C6C.LESS_THAN_OR_EQUAL_TO,
1454
- C6C.GREATER_THAN, C6C.GREATER_THAN_OR_EQUAL_TO,
1455
- C6C.LIKE, C6C.NOT_LIKE,
1456
- C6C.IN, C6C.NOT_IN, 'NOT IN',
1457
- C6C.IS, C6C.IS_NOT,
1458
- C6C.BETWEEN, 'NOT BETWEEN',
1459
- C6C.MATCH_AGAINST,
1460
- C6C.ST_DISTANCE_SPHERE,
1461
- // spatial predicates
1462
- C6C.ST_CONTAINS,
1463
- C6C.ST_INTERSECTS,
1464
- C6C.ST_WITHIN,
1465
- C6C.ST_CROSSES,
1466
- C6C.ST_DISJOINT,
1467
- C6C.ST_EQUALS,
1468
- C6C.ST_OVERLAPS,
1469
- C6C.ST_TOUCHES
1454
+ _this.BOOLEAN_OPERATORS = new Map([
1455
+ [C6C.AND, 'AND'],
1456
+ ['AND', 'AND'],
1457
+ [C6C.OR, 'OR'],
1458
+ ['OR', 'OR'],
1459
+ ]);
1460
+ _this.OPERATOR_ALIASES = new Map([
1461
+ [C6C.EQUAL, C6C.EQUAL],
1462
+ ['=', C6C.EQUAL],
1463
+ [C6C.EQUAL_NULL_SAFE, C6C.EQUAL_NULL_SAFE],
1464
+ ['<=>', C6C.EQUAL_NULL_SAFE],
1465
+ [C6C.NOT_EQUAL, C6C.NOT_EQUAL],
1466
+ ['<>', C6C.NOT_EQUAL],
1467
+ [C6C.LESS_THAN, C6C.LESS_THAN],
1468
+ ['<', C6C.LESS_THAN],
1469
+ [C6C.LESS_THAN_OR_EQUAL_TO, C6C.LESS_THAN_OR_EQUAL_TO],
1470
+ ['<=', C6C.LESS_THAN_OR_EQUAL_TO],
1471
+ [C6C.GREATER_THAN, C6C.GREATER_THAN],
1472
+ ['>', C6C.GREATER_THAN],
1473
+ [C6C.GREATER_THAN_OR_EQUAL_TO, C6C.GREATER_THAN_OR_EQUAL_TO],
1474
+ ['>=', C6C.GREATER_THAN_OR_EQUAL_TO],
1475
+ [C6C.LIKE, C6C.LIKE],
1476
+ ['LIKE', C6C.LIKE],
1477
+ [C6C.NOT_LIKE, 'NOT LIKE'],
1478
+ ['NOT LIKE', 'NOT LIKE'],
1479
+ [C6C.IN, C6C.IN],
1480
+ ['IN', C6C.IN],
1481
+ [C6C.NOT_IN, 'NOT IN'],
1482
+ ['NOT IN', 'NOT IN'],
1483
+ [C6C.IS, C6C.IS],
1484
+ ['IS', C6C.IS],
1485
+ [C6C.IS_NOT, 'IS NOT'],
1486
+ ['IS NOT', 'IS NOT'],
1487
+ [C6C.BETWEEN, C6C.BETWEEN],
1488
+ ['BETWEEN', C6C.BETWEEN],
1489
+ ['NOT BETWEEN', 'NOT BETWEEN'],
1490
+ [C6C.MATCH_AGAINST, C6C.MATCH_AGAINST],
1470
1491
  ]);
1471
1492
  return _this;
1472
1493
  }
@@ -1548,11 +1569,6 @@ var ConditionBuilder = /** @class */ (function (_super) {
1548
1569
  return (fullKey in table.COLUMNS ||
1549
1570
  Object.values(table.COLUMNS).includes(column));
1550
1571
  };
1551
- ConditionBuilder.prototype.validateOperator = function (op) {
1552
- if (!this.OPERATORS.has(op)) {
1553
- throw new Error("Invalid or unsupported SQL operator detected: '".concat(op, "'"));
1554
- }
1555
- };
1556
1572
  ConditionBuilder.prototype.addParam = function (params, column, value) {
1557
1573
  var _a, _b, _c, _d, _e;
1558
1574
  // Determine column definition from C6.TABLES to support type-aware conversions (e.g., BINARY hex -> Buffer)
@@ -1574,250 +1590,444 @@ var ConditionBuilder = /** @class */ (function (_super) {
1574
1590
  return '?';
1575
1591
  }
1576
1592
  };
1577
- ConditionBuilder.prototype.buildBooleanJoinedConditions = function (set, andMode, params) {
1578
- var _this = this;
1579
- if (andMode === void 0) { andMode = true; }
1580
- if (params === void 0) { params = []; }
1581
- var booleanOperator = andMode ? 'AND' : 'OR';
1582
- var addCondition = function (column, op, value) {
1583
- // Normalize common variants
1584
- var valueNorm = (value === C6C.NULL) ? null : value;
1585
- var displayOp = typeof op === 'string' ? op.replace('_', ' ') : op;
1586
- var extractSubSelect = function (input) {
1587
- if (Array.isArray(input) && input.length >= 2 && input[0] === C6C.SUBSELECT) {
1588
- return input[1];
1589
- }
1590
- if (input && typeof input === 'object' && C6C.SUBSELECT in input) {
1591
- return input[C6C.SUBSELECT];
1593
+ ConditionBuilder.prototype.normalizeOperatorKey = function (op) {
1594
+ if (typeof op !== 'string')
1595
+ return undefined;
1596
+ return this.OPERATOR_ALIASES.get(op);
1597
+ };
1598
+ ConditionBuilder.prototype.formatOperator = function (op) {
1599
+ var normalized = this.normalizeOperatorKey(op);
1600
+ if (!normalized) {
1601
+ throw new Error("Invalid or unsupported SQL operator detected: '".concat(op, "'"));
1602
+ }
1603
+ switch (normalized) {
1604
+ case 'NOT LIKE':
1605
+ case 'NOT IN':
1606
+ case 'IS NOT':
1607
+ case 'NOT BETWEEN':
1608
+ return normalized;
1609
+ case C6C.MATCH_AGAINST:
1610
+ return C6C.MATCH_AGAINST;
1611
+ default:
1612
+ return normalized;
1613
+ }
1614
+ };
1615
+ ConditionBuilder.prototype.isOperator = function (op) {
1616
+ return !!this.normalizeOperatorKey(op);
1617
+ };
1618
+ ConditionBuilder.prototype.looksLikeSafeFunctionExpression = function (value) {
1619
+ if (typeof value !== 'string')
1620
+ return false;
1621
+ var trimmed = value.trim();
1622
+ if (trimmed.length === 0)
1623
+ return false;
1624
+ if (trimmed.includes(';') || trimmed.includes('--') || trimmed.includes('/*') || trimmed.includes('*/')) {
1625
+ return false;
1626
+ }
1627
+ if (!trimmed.includes('(') || !trimmed.endsWith(')')) {
1628
+ return false;
1629
+ }
1630
+ var functionMatch = trimmed.match(/^([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\s*\(/);
1631
+ if (!functionMatch) {
1632
+ return false;
1633
+ }
1634
+ var allowedCharacters = /^[A-Za-z0-9_().,'"\s-]+$/;
1635
+ if (!allowedCharacters.test(trimmed)) {
1636
+ return false;
1637
+ }
1638
+ var depth = 0;
1639
+ for (var _i = 0, trimmed_1 = trimmed; _i < trimmed_1.length; _i++) {
1640
+ var char = trimmed_1[_i];
1641
+ if (char === '(') {
1642
+ depth += 1;
1643
+ }
1644
+ else if (char === ')') {
1645
+ depth -= 1;
1646
+ if (depth < 0) {
1647
+ return false;
1592
1648
  }
1593
- return undefined;
1594
- };
1595
- var rightSubSelectPayload = extractSubSelect(valueNorm);
1596
- var buildSubSelect = function (payload) {
1597
- if (!payload)
1598
- return undefined;
1599
- var builder = _this.buildScalarSubSelect;
1649
+ }
1650
+ }
1651
+ return depth === 0;
1652
+ };
1653
+ ConditionBuilder.prototype.ensureWrapped = function (expression) {
1654
+ var trimmed = expression.trim();
1655
+ if (!trimmed)
1656
+ return trimmed;
1657
+ if (trimmed.startsWith('(') && trimmed.endsWith(')')) {
1658
+ return trimmed;
1659
+ }
1660
+ return "(".concat(trimmed, ")");
1661
+ };
1662
+ ConditionBuilder.prototype.joinBooleanParts = function (parts, operator) {
1663
+ if (parts.length === 0)
1664
+ return '';
1665
+ if (parts.length === 1) {
1666
+ return parts[0];
1667
+ }
1668
+ return parts
1669
+ .map(function (part) {
1670
+ var trimmed = part.trim();
1671
+ var upper = trimmed.toUpperCase();
1672
+ var containsAnd = upper.includes(' AND ');
1673
+ var containsOr = upper.includes(' OR ');
1674
+ var needsWrap = (operator === 'AND' && containsOr) ||
1675
+ (operator === 'OR' && containsAnd);
1676
+ return needsWrap ? "(".concat(trimmed, ")") : trimmed;
1677
+ })
1678
+ .join(" ".concat(operator, " "));
1679
+ };
1680
+ ConditionBuilder.prototype.normalizeFunctionField = function (field, params) {
1681
+ var _this = this;
1682
+ if (field instanceof Map) {
1683
+ field = Object.fromEntries(field);
1684
+ }
1685
+ if (Array.isArray(field)) {
1686
+ if (field.length === 0)
1687
+ return field;
1688
+ var fn = field[0], args = field.slice(1);
1689
+ var normalizedArgs = args.map(function (arg) { return _this.normalizeFunctionField(arg, params); });
1690
+ return __spreadArray([fn], normalizedArgs, true);
1691
+ }
1692
+ if (field && typeof field === 'object') {
1693
+ if (C6C.SUBSELECT in field) {
1694
+ var builder = this.buildScalarSubSelect;
1600
1695
  if (typeof builder !== 'function') {
1601
1696
  throw new Error('Scalar subselect handling requires JoinBuilder context.');
1602
1697
  }
1603
- return builder.call(_this, payload, params);
1604
- };
1605
- var rightSubSelectSql = buildSubSelect(rightSubSelectPayload);
1606
- // Support function-based expressions like [C6C.ST_DISTANCE_SPHERE, col1, col2]
1607
- if (typeof column === 'string' &&
1608
- _this.OPERATORS.has(column) &&
1609
- Array.isArray(op)) {
1610
- // Helper to serialize operand which may be a qualified identifier or a nested function array
1611
- var serializeOperand = function (arg) {
1612
- var identifierPathRegex = /^[A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*$/;
1613
- if (Array.isArray(arg)) {
1614
- // Delegate to aggregate builder to handle nested functions/params
1615
- // @ts-ignore - buildAggregateField is defined upstream in AggregateBuilder
1616
- return _this.buildAggregateField(arg, params);
1617
- }
1618
- if (typeof arg === 'string') {
1619
- if (identifierPathRegex.test(arg)) {
1620
- _this.assertValidIdentifier(arg, 'WHERE argument');
1621
- return arg;
1622
- }
1623
- return arg;
1624
- }
1625
- return String(arg);
1626
- };
1627
- if (column === C6C.ST_DISTANCE_SPHERE) {
1628
- var col1 = op[0], col2 = op[1];
1629
- var threshold = Array.isArray(value) ? value[0] : value;
1630
- var left = serializeOperand(col1);
1631
- var right = serializeOperand(col2);
1632
- return "ST_Distance_Sphere(".concat(left, ", ").concat(right, ") < ").concat(_this.addParam(params, '', threshold));
1633
- }
1634
- if ([
1635
- C6C.ST_CONTAINS,
1636
- C6C.ST_INTERSECTS,
1637
- C6C.ST_WITHIN,
1638
- C6C.ST_CROSSES,
1639
- C6C.ST_DISJOINT,
1640
- C6C.ST_EQUALS,
1641
- C6C.ST_OVERLAPS,
1642
- C6C.ST_TOUCHES
1643
- ].includes(column)) {
1644
- var geom1 = op[0], geom2 = op[1];
1645
- var left = serializeOperand(geom1);
1646
- var right = serializeOperand(geom2);
1647
- return "".concat(column, "(").concat(left, ", ").concat(right, ")");
1698
+ return builder.call(this, field[C6C.SUBSELECT], params);
1699
+ }
1700
+ var entries = Object.entries(field);
1701
+ if (entries.length === 1) {
1702
+ var _a = entries[0], key = _a[0], value = _a[1];
1703
+ if (this.isOperator(key)) {
1704
+ return this.buildOperatorExpression(key, value, params);
1648
1705
  }
1706
+ return this.buildFunctionCall(key, value, params);
1707
+ }
1708
+ }
1709
+ return field;
1710
+ };
1711
+ ConditionBuilder.prototype.buildFunctionCall = function (fn, value, params) {
1712
+ var args = Array.isArray(value) ? value : [value];
1713
+ var normalized = this.normalizeFunctionField(__spreadArray([fn], args, true), params);
1714
+ return this.buildAggregateField(normalized, params);
1715
+ };
1716
+ ConditionBuilder.prototype.serializeOperand = function (operand, params, contextColumn) {
1717
+ var _a;
1718
+ var _this = this;
1719
+ var asParam = function (val) { return _this.addParam(params, contextColumn !== null && contextColumn !== void 0 ? contextColumn : '', val); };
1720
+ if (operand === C6C.NULL) {
1721
+ operand = null;
1722
+ }
1723
+ if (operand === null || typeof operand === 'number' || typeof operand === 'boolean') {
1724
+ return { sql: asParam(operand), isReference: false, isExpression: false, isSubSelect: false };
1725
+ }
1726
+ if (typeof operand === 'string') {
1727
+ if (this.isTableReference(operand) || this.isColumnRef(operand)) {
1728
+ return { sql: operand, isReference: true, isExpression: false, isSubSelect: false };
1649
1729
  }
1650
- var leftIsCol = _this.isColumnRef(column);
1651
- var leftIsRef = _this.isTableReference(column);
1652
- var rightIsCol = typeof value === 'string' && _this.isColumnRef(value);
1653
- if (!leftIsCol && !leftIsRef && !rightIsCol && !rightSubSelectSql) {
1654
- throw new Error("Potential SQL injection detected: '".concat(column, " ").concat(op, " ").concat(value, "'"));
1730
+ if (this.looksLikeSafeFunctionExpression(operand)) {
1731
+ return { sql: operand.trim(), isReference: false, isExpression: true, isSubSelect: false };
1655
1732
  }
1656
- _this.validateOperator(op);
1657
- if (op === C6C.MATCH_AGAINST && Array.isArray(value)) {
1658
- var search = value[0], mode = value[1];
1659
- var paramName = _this.useNamedParams ? "param".concat(Object.keys(params).length) : null;
1660
- if (_this.useNamedParams) {
1661
- params[paramName] = search;
1662
- }
1663
- else {
1664
- params.push(search);
1665
- }
1666
- var againstClause = void 0;
1667
- switch ((mode || '').toUpperCase()) {
1668
- case 'BOOLEAN':
1669
- againstClause = _this.useNamedParams ? "AGAINST(:".concat(paramName, " IN BOOLEAN MODE)") : "AGAINST(? IN BOOLEAN MODE)";
1670
- break;
1671
- case 'WITH QUERY EXPANSION':
1672
- againstClause = _this.useNamedParams ? "AGAINST(:".concat(paramName, " WITH QUERY EXPANSION)") : "AGAINST(? WITH QUERY EXPANSION)";
1673
- break;
1674
- default: // NATURAL or undefined
1675
- againstClause = _this.useNamedParams ? "AGAINST(:".concat(paramName, ")") : "AGAINST(?)";
1676
- break;
1677
- }
1678
- if (!leftIsCol) {
1679
- throw new Error("MATCH_AGAINST requires a table reference as the left operand. Column '".concat(column, "' is not a valid table reference."));
1733
+ return { sql: asParam(operand), isReference: false, isExpression: false, isSubSelect: false };
1734
+ }
1735
+ if (Array.isArray(operand)) {
1736
+ var normalized = this.normalizeFunctionField(operand, params);
1737
+ var sql = this.buildAggregateField(normalized, params);
1738
+ return { sql: sql, isReference: false, isExpression: true, isSubSelect: false };
1739
+ }
1740
+ if (operand instanceof Map) {
1741
+ operand = Object.fromEntries(operand);
1742
+ }
1743
+ if (typeof operand === 'object' && operand !== null) {
1744
+ if (C6C.SUBSELECT in operand) {
1745
+ var builder = this.buildScalarSubSelect;
1746
+ if (typeof builder !== 'function') {
1747
+ throw new Error('Scalar subselect handling requires JoinBuilder context.');
1680
1748
  }
1681
- var matchClause = "(MATCH(".concat(column, ") ").concat(againstClause, ")");
1682
- _this.config.verbose && console.log("[MATCH_AGAINST] ".concat(matchClause));
1683
- return matchClause;
1749
+ var subSql = builder.call(this, operand[C6C.SUBSELECT], params);
1750
+ return { sql: subSql, isReference: false, isExpression: true, isSubSelect: true };
1684
1751
  }
1685
- if ((op === C6C.IN || op === C6C.NOT_IN) && Array.isArray(value)) {
1686
- if (rightSubSelectSql) {
1687
- if (!leftIsRef) {
1688
- throw new Error("IN operator requires a table reference as the left operand. Column '".concat(column, "' is not a valid table reference."));
1689
- }
1690
- var normalized_1 = op.replace('_', ' ');
1691
- return "( ".concat(column, " ").concat(normalized_1, " ").concat(rightSubSelectSql, " )");
1752
+ var entries = Object.entries(operand);
1753
+ if (entries.length === 1) {
1754
+ var _b = entries[0], key = _b[0], value = _b[1];
1755
+ if (this.isOperator(key)) {
1756
+ var sql_1 = this.buildOperatorExpression(key, value, params);
1757
+ return { sql: this.ensureWrapped(sql_1), isReference: false, isExpression: true, isSubSelect: false };
1692
1758
  }
1693
- var placeholders = value.map(function (v) {
1694
- return _this.isColumnRef(v) ? v : _this.addParam(params, column, v);
1695
- }).join(', ');
1696
- var normalized = op.replace('_', ' ');
1697
- if (!leftIsRef) {
1698
- throw new Error("IN operator requires a table reference as the left operand. Column '".concat(column, "' is not a valid table reference."));
1759
+ if (this.BOOLEAN_OPERATORS.has(key)) {
1760
+ var sql_2 = this.buildBooleanExpression((_a = {}, _a[key] = value, _a), params, 'AND');
1761
+ return { sql: this.ensureWrapped(sql_2), isReference: false, isExpression: true, isSubSelect: false };
1699
1762
  }
1700
- return "( ".concat(column, " ").concat(normalized, " (").concat(placeholders, ") )");
1763
+ var sql = this.buildFunctionCall(key, value, params);
1764
+ return { sql: sql, isReference: false, isExpression: true, isSubSelect: false };
1765
+ }
1766
+ }
1767
+ throw new Error('Unsupported operand type in SQL expression.');
1768
+ };
1769
+ ConditionBuilder.prototype.buildOperatorExpression = function (op, rawOperands, params, contextColumn) {
1770
+ var _a, _b;
1771
+ var _this = this;
1772
+ var operator = this.formatOperator(op);
1773
+ if (operator === C6C.MATCH_AGAINST) {
1774
+ if (!Array.isArray(rawOperands) || rawOperands.length !== 2) {
1775
+ throw new Error('MATCH_AGAINST requires an array of two operands.');
1701
1776
  }
1702
- if (op === C6C.BETWEEN || op === 'NOT BETWEEN') {
1703
- if (!Array.isArray(value) || value.length !== 2) {
1704
- throw new Error("BETWEEN operator requires an array of two values");
1777
+ var left = rawOperands[0], right = rawOperands[1];
1778
+ var leftInfo_1 = this.serializeOperand(left, params, contextColumn);
1779
+ if (!leftInfo_1.isReference) {
1780
+ throw new Error('MATCH_AGAINST requires the left operand to be a table reference.');
1781
+ }
1782
+ if (!Array.isArray(right) || right.length === 0) {
1783
+ throw new Error('MATCH_AGAINST expects an array [search, mode?].');
1784
+ }
1785
+ var search = right[0], mode = right[1];
1786
+ var placeholder = this.addParam(params, leftInfo_1.sql, search);
1787
+ var againstClause = void 0;
1788
+ switch (typeof mode === 'string' ? mode.toUpperCase() : '') {
1789
+ case 'BOOLEAN':
1790
+ againstClause = "AGAINST(".concat(placeholder, " IN BOOLEAN MODE)");
1791
+ break;
1792
+ case 'WITH QUERY EXPANSION':
1793
+ againstClause = "AGAINST(".concat(placeholder, " WITH QUERY EXPANSION)");
1794
+ break;
1795
+ case 'NATURAL LANGUAGE MODE':
1796
+ againstClause = "AGAINST(".concat(placeholder, " IN NATURAL LANGUAGE MODE)");
1797
+ break;
1798
+ default:
1799
+ againstClause = "AGAINST(".concat(placeholder, ")");
1800
+ break;
1801
+ }
1802
+ var clause = "(MATCH(".concat(leftInfo_1.sql, ") ").concat(againstClause, ")");
1803
+ this.config.verbose && console.log("[MATCH_AGAINST] ".concat(clause));
1804
+ return clause;
1805
+ }
1806
+ var operands = Array.isArray(rawOperands) ? rawOperands : [rawOperands];
1807
+ if (operator === C6C.IN || operator === 'NOT IN') {
1808
+ if (operands.length < 2) {
1809
+ throw new Error("".concat(operator, " requires two operands."));
1810
+ }
1811
+ var leftRaw = operands[0], rest = operands.slice(1);
1812
+ var left_1 = leftRaw;
1813
+ var right = rest.length <= 1 ? rest[0] : rest;
1814
+ var leftInfo_2 = this.serializeOperand(left_1, params, typeof left_1 === 'string' ? left_1 : contextColumn);
1815
+ if (!leftInfo_2.isReference) {
1816
+ throw new Error("".concat(operator, " requires the left operand to be a table reference."));
1817
+ }
1818
+ if (Array.isArray(right)) {
1819
+ if (right.length === 0) {
1820
+ throw new Error("".concat(operator, " requires at least one value."));
1705
1821
  }
1706
- var start = value[0], end = value[1];
1707
- if (!leftIsRef) {
1708
- throw new Error("BETWEEN operator requires a table reference as the left operand. Column '".concat(column, "' is not a valid table reference."));
1822
+ if (right.length === 2 && right[0] === C6C.SUBSELECT) {
1823
+ var sub = this.serializeOperand(right, params, typeof left_1 === 'string' ? left_1 : contextColumn);
1824
+ return "( ".concat(leftInfo_2.sql, " ").concat(operator, " ").concat(sub.sql, " )");
1709
1825
  }
1710
- return "(".concat(column, ") ").concat(op.replace('_', ' '), " ").concat(_this.addParam(params, column, start), " AND ").concat(_this.addParam(params, column, end));
1826
+ var placeholders = right.map(function (item) {
1827
+ if (typeof item === 'string' && _this.isTableReference(item)) {
1828
+ return item;
1829
+ }
1830
+ var sql = _this.serializeOperand(item, params, typeof left_1 === 'string' ? left_1 : contextColumn).sql;
1831
+ return sql;
1832
+ });
1833
+ return "( ".concat(leftInfo_2.sql, " ").concat(operator, " (").concat(placeholders.join(', '), ") )");
1711
1834
  }
1712
- var rightIsRef = rightSubSelectSql ? false : _this.isTableReference(value);
1713
- if (leftIsRef && rightIsRef) {
1714
- return "(".concat(column, ") ").concat(displayOp, " ").concat(value);
1835
+ var rightInfo_1 = this.serializeOperand(right, params, typeof left_1 === 'string' ? left_1 : contextColumn);
1836
+ if (!rightInfo_1.isSubSelect) {
1837
+ throw new Error("".concat(operator, " requires an array of values or a subselect."));
1715
1838
  }
1716
- if (leftIsRef && rightSubSelectSql) {
1717
- return "(".concat(column, ") ").concat(displayOp, " ").concat(rightSubSelectSql);
1839
+ return "( ".concat(leftInfo_2.sql, " ").concat(operator, " ").concat(rightInfo_1.sql, " )");
1840
+ }
1841
+ if (operator === C6C.BETWEEN || operator === 'NOT BETWEEN') {
1842
+ var left = void 0;
1843
+ var start = void 0;
1844
+ var end = void 0;
1845
+ if (operands.length === 3) {
1846
+ left = operands[0], start = operands[1], end = operands[2];
1718
1847
  }
1719
- if (leftIsRef && !rightIsRef) {
1720
- return "(".concat(column, ") ").concat(displayOp, " ").concat(_this.addParam(params, column, valueNorm));
1848
+ else if (operands.length === 2 && Array.isArray(operands[1]) && operands[1].length === 2) {
1849
+ _a = operands, left = _a[0], _b = _a[1], start = _b[0], end = _b[1];
1721
1850
  }
1722
- if (rightIsRef) {
1723
- return "(".concat(_this.addParam(params, column, column), ") ").concat(displayOp, " ").concat(value);
1851
+ else {
1852
+ throw new Error("".concat(operator, " requires three operands."));
1724
1853
  }
1725
- throw new Error("Neither operand appears to be a table reference (".concat(column, ") or (").concat(value, ")"));
1726
- };
1727
- var parts = [];
1728
- var buildFromObject = function (obj, mode) {
1729
- var subParts = [];
1730
- var entries = Object.entries(obj);
1731
- var nonNumeric = entries.filter(function (_a) {
1732
- var k = _a[0];
1733
- return isNaN(Number(k));
1734
- });
1735
- var numeric = entries.filter(function (_a) {
1736
- var k = _a[0];
1737
- return !isNaN(Number(k));
1738
- });
1739
- var processEntry = function (k, v) {
1740
- // Operator-as-key handling, e.g., { [C6C.ST_DISTANCE_SPHERE]: [arg1, arg2, threshold] }
1741
- if (typeof k === 'string' && _this.OPERATORS.has(k) && Array.isArray(v)) {
1742
- if (k === C6C.ST_DISTANCE_SPHERE) {
1743
- // Accept either [arg1, arg2, threshold] or [[arg1, arg2], threshold]
1744
- var args = void 0;
1745
- var threshold = void 0;
1746
- if (Array.isArray(v[0]) && v.length >= 2) {
1747
- args = v[0];
1748
- threshold = v[1];
1749
- }
1750
- else {
1751
- args = v.slice(0, 2);
1752
- threshold = v[2];
1753
- }
1754
- subParts.push(addCondition(k, args, threshold));
1755
- return;
1756
- }
1757
- }
1758
- if (typeof v === 'object' && v !== null && Object.keys(v).length === 1) {
1759
- var _a = Object.entries(v)[0], op = _a[0], val = _a[1];
1760
- subParts.push(addCondition(k, op, val));
1854
+ var leftInfo_3 = this.serializeOperand(left, params, typeof left === 'string' ? left : contextColumn);
1855
+ if (!leftInfo_3.isReference) {
1856
+ throw new Error("".concat(operator, " requires the left operand to be a table reference."));
1857
+ }
1858
+ var startInfo = this.serializeOperand(start, params, typeof left === 'string' ? left : contextColumn);
1859
+ var endInfo = this.serializeOperand(end, params, typeof left === 'string' ? left : contextColumn);
1860
+ var betweenOperator = operator === 'NOT BETWEEN' ? 'NOT BETWEEN' : 'BETWEEN';
1861
+ return "".concat(this.ensureWrapped(leftInfo_3.sql), " ").concat(betweenOperator, " ").concat(startInfo.sql, " AND ").concat(endInfo.sql);
1862
+ }
1863
+ if (operands.length !== 2) {
1864
+ throw new Error("".concat(operator, " requires two operands."));
1865
+ }
1866
+ var leftOperand = operands[0], rightOperand = operands[1];
1867
+ var leftInfo = this.serializeOperand(leftOperand, params, typeof leftOperand === 'string' ? leftOperand : contextColumn);
1868
+ var rightInfo = this.serializeOperand(rightOperand, params, typeof leftOperand === 'string' ? leftOperand : contextColumn);
1869
+ if (!leftInfo.isReference && !leftInfo.isExpression && !rightInfo.isReference && !rightInfo.isExpression) {
1870
+ throw new Error("Potential SQL injection detected: '".concat(operator, "' with non-reference operands."));
1871
+ }
1872
+ var leftSql = leftInfo.isExpression ? leftInfo.sql : this.ensureWrapped(leftInfo.sql);
1873
+ var rightSql = rightInfo.isExpression ? rightInfo.sql : rightInfo.sql;
1874
+ return "".concat(leftSql, " ").concat(operator, " ").concat(rightSql);
1875
+ };
1876
+ ConditionBuilder.prototype.buildLegacyColumnCondition = function (column, value, params) {
1877
+ var _a;
1878
+ var _this = this;
1879
+ if (value instanceof Map) {
1880
+ value = Object.fromEntries(value);
1881
+ }
1882
+ if (Array.isArray(value)) {
1883
+ if (value.length >= 2 && typeof value[0] === 'string') {
1884
+ var op = value[0], rest = value.slice(1);
1885
+ return this.buildOperatorExpression(op, __spreadArray([column], rest, true), params, column);
1886
+ }
1887
+ if (value.length === 3 && typeof value[0] === 'string' && typeof value[1] === 'string') {
1888
+ return this.buildOperatorExpression(value[1], [value[0], value[2]], params, value[0]);
1889
+ }
1890
+ }
1891
+ if (typeof value === 'object' && value !== null) {
1892
+ var entries = Object.entries(value);
1893
+ if (entries.length === 1) {
1894
+ var _b = entries[0], op = _b[0], operand = _b[1];
1895
+ if (this.isOperator(op)) {
1896
+ return this.buildOperatorExpression(op, [column, operand], params, column);
1761
1897
  }
1762
- else if (Array.isArray(v) && v.length >= 2 && typeof v[0] === 'string') {
1763
- var _b = v, op = _b[0], val = _b[1];
1764
- subParts.push(addCondition(k, op, val));
1898
+ if (this.BOOLEAN_OPERATORS.has(op)) {
1899
+ var expression = this.buildBooleanExpression((_a = {}, _a[op] = operand, _a), params, 'AND');
1900
+ return expression;
1765
1901
  }
1766
- else if (typeof v === 'object' && v !== null) {
1767
- var sub = _this.buildBooleanJoinedConditions(v, mode, params);
1768
- if (sub)
1769
- subParts.push(sub);
1902
+ }
1903
+ var subParts = entries.map(function (_a) {
1904
+ var _b;
1905
+ var op = _a[0], operand = _a[1];
1906
+ if (_this.isOperator(op)) {
1907
+ return _this.buildOperatorExpression(op, [column, operand], params, column);
1770
1908
  }
1771
- else {
1772
- subParts.push(addCondition(k, '=', v));
1909
+ return _this.buildBooleanExpression((_b = {}, _b[op] = operand, _b), params, 'AND');
1910
+ }).filter(Boolean);
1911
+ return this.joinBooleanParts(subParts, 'AND');
1912
+ }
1913
+ return this.buildOperatorExpression(C6C.EQUAL, [column, value], params, column);
1914
+ };
1915
+ ConditionBuilder.prototype.buildBooleanExpression = function (node, params, defaultOperator) {
1916
+ var _this = this;
1917
+ if (node === null || node === undefined) {
1918
+ return '';
1919
+ }
1920
+ if (Array.isArray(node)) {
1921
+ if (node.length === 0)
1922
+ return '';
1923
+ if (node.length === 3 && typeof node[0] === 'string' && typeof node[1] === 'string') {
1924
+ return this.buildOperatorExpression(node[1], [node[0], node[2]], params, node[0]);
1925
+ }
1926
+ var parts_1 = node
1927
+ .map(function (item) { return _this.buildBooleanExpression(item, params, 'OR'); })
1928
+ .filter(Boolean);
1929
+ return this.joinBooleanParts(parts_1, 'OR');
1930
+ }
1931
+ if (node instanceof Map) {
1932
+ node = Object.fromEntries(node);
1933
+ }
1934
+ if (typeof node !== 'object') {
1935
+ throw new Error('Invalid WHERE clause structure.');
1936
+ }
1937
+ var entries = Object.entries(node);
1938
+ if (entries.length === 0)
1939
+ return '';
1940
+ if (entries.length === 1) {
1941
+ var _a = entries[0], key = _a[0], value = _a[1];
1942
+ if (this.BOOLEAN_OPERATORS.has(key)) {
1943
+ if (!Array.isArray(value)) {
1944
+ throw new Error("".concat(key, " expects an array of expressions."));
1773
1945
  }
1774
- };
1775
- // Process non-numeric keys first to preserve intuitive insertion order for params
1776
- for (var _i = 0, nonNumeric_1 = nonNumeric; _i < nonNumeric_1.length; _i++) {
1777
- var _a = nonNumeric_1[_i], k = _a[0], v = _a[1];
1778
- processEntry(k, v);
1946
+ var op_1 = this.BOOLEAN_OPERATORS.get(key);
1947
+ var parts_2 = value
1948
+ .map(function (item) { return _this.buildBooleanExpression(item, params, op_1); })
1949
+ .filter(Boolean);
1950
+ return this.joinBooleanParts(parts_2, op_1);
1779
1951
  }
1780
- // Then process numeric keys (treated as grouped OR conditions)
1781
- for (var _b = 0, numeric_1 = numeric; _b < numeric_1.length; _b++) {
1782
- var _c = numeric_1[_b]; _c[0]; var v = _c[1];
1783
- var sub = _this.buildBooleanJoinedConditions(v, false, params);
1784
- if (sub)
1785
- subParts.push(sub);
1952
+ if (this.isOperator(key)) {
1953
+ return this.buildOperatorExpression(key, value, params);
1786
1954
  }
1787
- return subParts.join(" ".concat(mode ? 'AND' : 'OR', " "));
1788
- };
1789
- if (Array.isArray(set)) {
1790
- // Detect a single condition triple: [column, op, value]
1791
- if (set.length === 3 && typeof set[0] === 'string' && typeof set[1] === 'string') {
1792
- var _a = set, column = _a[0], rawOp = _a[1], rawVal = _a[2];
1793
- var op = rawOp;
1794
- var value = rawVal === C6C.NULL ? null : rawVal;
1795
- var sub = addCondition(column, op, value);
1796
- if (sub)
1797
- parts.push(sub);
1955
+ if (!isNaN(Number(key))) {
1956
+ return this.buildBooleanExpression(value, params, 'OR');
1798
1957
  }
1799
- else {
1800
- for (var _i = 0, set_1 = set; _i < set_1.length; _i++) {
1801
- var item = set_1[_i];
1802
- var sub = this.buildBooleanJoinedConditions(item, false, params);
1803
- if (sub)
1804
- parts.push(sub);
1958
+ }
1959
+ var parts = [];
1960
+ var nonNumeric = entries.filter(function (_a) {
1961
+ var k = _a[0];
1962
+ return isNaN(Number(k));
1963
+ });
1964
+ var numeric = entries.filter(function (_a) {
1965
+ var k = _a[0];
1966
+ return !isNaN(Number(k));
1967
+ });
1968
+ var _loop_1 = function (key, value) {
1969
+ if (this_1.BOOLEAN_OPERATORS.has(key)) {
1970
+ var op_2 = this_1.BOOLEAN_OPERATORS.get(key);
1971
+ if (!Array.isArray(value)) {
1972
+ throw new Error("".concat(key, " expects an array of expressions."));
1973
+ }
1974
+ var nested = value
1975
+ .map(function (item) { return _this.buildBooleanExpression(item, params, op_2); })
1976
+ .filter(Boolean);
1977
+ if (nested.length) {
1978
+ parts.push(this_1.joinBooleanParts(nested, op_2));
1805
1979
  }
1980
+ return "continue";
1981
+ }
1982
+ if (this_1.isOperator(key)) {
1983
+ parts.push(this_1.buildOperatorExpression(key, value, params));
1984
+ return "continue";
1806
1985
  }
1986
+ parts.push(this_1.buildLegacyColumnCondition(key, value, params));
1987
+ };
1988
+ var this_1 = this;
1989
+ for (var _i = 0, nonNumeric_1 = nonNumeric; _i < nonNumeric_1.length; _i++) {
1990
+ var _b = nonNumeric_1[_i], key = _b[0], value = _b[1];
1991
+ _loop_1(key, value);
1807
1992
  }
1808
- else if (typeof set === 'object' && set !== null) {
1809
- var sub = buildFromObject(set, andMode);
1810
- if (sub)
1811
- parts.push(sub);
1993
+ for (var _c = 0, numeric_1 = numeric; _c < numeric_1.length; _c++) {
1994
+ var _d = numeric_1[_c], value = _d[1];
1995
+ var nested = this.buildBooleanExpression(value, params, 'OR');
1996
+ if (nested) {
1997
+ parts.push(nested);
1998
+ }
1812
1999
  }
1813
- var clause = parts.join(" ".concat(booleanOperator, " "));
1814
- return clause ? "(".concat(clause, ")") : '';
2000
+ return this.joinBooleanParts(parts, defaultOperator);
2001
+ };
2002
+ ConditionBuilder.prototype.buildBooleanJoinedConditions = function (set, andMode, params) {
2003
+ if (andMode === void 0) { andMode = true; }
2004
+ if (params === void 0) { params = []; }
2005
+ var expression = this.buildBooleanExpression(set, params, andMode ? 'AND' : 'OR');
2006
+ if (!expression)
2007
+ return '';
2008
+ return this.ensureWrapped(expression);
1815
2009
  };
1816
2010
  ConditionBuilder.prototype.buildWhereClause = function (whereArg, params) {
1817
2011
  var clause = this.buildBooleanJoinedConditions(whereArg, true, params);
1818
2012
  if (!clause)
1819
2013
  return '';
1820
- var trimmed = clause.replace(/^\((.*)\)$/, '$1');
2014
+ var trimmed = clause.trim();
2015
+ var upper = trimmed.toUpperCase();
2016
+ if (!upper.includes(' AND ') && !upper.includes(' OR ')) {
2017
+ if (trimmed.startsWith('(') && trimmed.endsWith(')')) {
2018
+ var inner = trimmed.substring(1, trimmed.length - 1);
2019
+ var innerUpper = inner.toUpperCase();
2020
+ var requiresOuterWrap = innerUpper.includes(' IN ') ||
2021
+ innerUpper.includes(' BETWEEN ') ||
2022
+ innerUpper.includes(' SELECT ');
2023
+ if (requiresOuterWrap) {
2024
+ trimmed = "( ".concat(inner.trim(), " )");
2025
+ }
2026
+ else {
2027
+ trimmed = inner.trim();
2028
+ }
2029
+ }
2030
+ }
1821
2031
  this.config.verbose && console.log("[WHERE] ".concat(trimmed));
1822
2032
  return " WHERE ".concat(trimmed);
1823
2033
  };
@@ -1834,24 +2044,29 @@ var JoinBuilder = /** @class */ (function (_super) {
1834
2044
  };
1835
2045
  JoinBuilder.prototype.buildJoinClauses = function (joinArgs, params) {
1836
2046
  var sql = '';
1837
- var _loop_1 = function (joinType) {
1838
- var joinKind = joinType.replace('_', ' ').toUpperCase();
2047
+ var joinTypeEntries = joinArgs instanceof Map
2048
+ ? Array.from(joinArgs.entries()).map(function (_a) {
2049
+ var key = _a[0], value = _a[1];
2050
+ return [String(key), value];
2051
+ })
2052
+ : Object.keys(joinArgs).map(function (key) { return [key, joinArgs[key]]; });
2053
+ var _loop_1 = function (joinTypeRaw, joinSection) {
2054
+ var joinKind = joinTypeRaw.replace('_', ' ').toUpperCase();
1839
2055
  var entries = [];
1840
- var joinSection = joinArgs[joinType];
1841
2056
  if (joinSection instanceof Map) {
1842
2057
  joinSection.forEach(function (value, key) {
1843
2058
  entries.push([key, value]);
1844
2059
  });
1845
2060
  }
1846
2061
  else {
1847
- for (var raw in joinSection) {
2062
+ Object.keys(joinSection).forEach(function (raw) {
1848
2063
  entries.push([raw, joinSection[raw]]);
1849
- }
2064
+ });
1850
2065
  }
1851
- for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
1852
- var _a = entries_1[_i], rawKey = _a[0], conditions = _a[1];
2066
+ for (var _b = 0, entries_1 = entries; _b < entries_1.length; _b++) {
2067
+ var _c = entries_1[_b], rawKey = _c[0], conditions = _c[1];
1853
2068
  var raw = typeof rawKey === 'string' ? rawKey : String(rawKey);
1854
- var _b = raw.trim().split(/\s+/, 2), table = _b[0], aliasCandidate = _b[1];
2069
+ var _d = raw.trim().split(/\s+/, 2), table = _d[0], aliasCandidate = _d[1];
1855
2070
  if (!table)
1856
2071
  continue;
1857
2072
  if (isDerivedTableKey(table)) {
@@ -1875,7 +2090,7 @@ var JoinBuilder = /** @class */ (function (_super) {
1875
2090
  throw new Error('Derived table subselects require a base table defined with C6C.FROM.');
1876
2091
  }
1877
2092
  var subBuilder = this_1.createSelectBuilder(subRequest);
1878
- var _c = subBuilder.build(fromTable, true), subSql = _c.sql, subParams = _c.params;
2093
+ var _e = subBuilder.build(fromTable, true), subSql = _e.sql, subParams = _e.params;
1879
2094
  var normalizedSql = this_1.integrateSubSelectParams(subSql, subParams, params);
1880
2095
  var formatted = normalizedSql.trim().split('\n').map(function (line) { return " ".concat(line); }).join('\n');
1881
2096
  var joinSql = "(\n".concat(formatted, "\n) AS `").concat(alias, "`");
@@ -1900,8 +2115,9 @@ var JoinBuilder = /** @class */ (function (_super) {
1900
2115
  }
1901
2116
  };
1902
2117
  var this_1 = this;
1903
- for (var joinType in joinArgs) {
1904
- _loop_1(joinType);
2118
+ for (var _i = 0, joinTypeEntries_1 = joinTypeEntries; _i < joinTypeEntries_1.length; _i++) {
2119
+ var _a = joinTypeEntries_1[_i], joinTypeRaw = _a[0], joinSection = _a[1];
2120
+ _loop_1(joinTypeRaw, joinSection);
1905
2121
  }
1906
2122
  this.config.verbose && console.log("[JOIN] ".concat(sql.trim()));
1907
2123
  return sql;