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