sqlparser-devexpress 2.6.0 → 2.6.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqlparser-devexpress",
3
- "version": "2.6.0",
3
+ "version": "2.6.2",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -13,6 +13,7 @@ export interface ConvertOptions {
13
13
  enableShortCircuit?: boolean;
14
14
  isValueNullShortCircuit?: boolean;
15
15
  treatNumberAsNullableBit?: boolean;
16
+ treatNullAsFalse?: boolean;
16
17
  };
17
18
  }
18
19
 
@@ -42,5 +42,6 @@ export function convertAstToDevextreme(
42
42
  enableShortCircuit?: boolean;
43
43
  isValueNullShortCircuit?: boolean;
44
44
  treatNumberAsNullableBit?: boolean;
45
+ treatNullAsFalse?: boolean;
45
46
  }
46
47
  ): DevExpressFilter;
@@ -10,6 +10,7 @@ function DevExpressConverter() {
10
10
  let EnableShortCircuit = true;
11
11
  let IsValueNullShortCircuit = false; // Flag to enable/disable null short-circuiting
12
12
  let TreatNumberAsNullableBit = false; // Flag to enable/disable optional boolean with number conversion
13
+ let TreatNullAsFalse = false; // Flag to evaluate null as equal to 0 / false / ""
13
14
 
14
15
  /**
15
16
  * Main conversion function that sets up the global context
@@ -18,14 +19,16 @@ function DevExpressConverter() {
18
19
  * @param {boolean} enableShortCircuit - Optional enabling and disabling the shortcircuit ie evaluating value = value scenario
19
20
  * @param {boolean} isValueNullShortCircuit - Optional enabling and disabling the null shortcircuit ie evaluating value = null scenario
20
21
  * @param {boolean} treatNumberAsNullableBit - Optional enabling and disabling the optional boolean with number conversion
22
+ * @param {boolean} treatNullAsFalse - Optional evaluating null as equal to 0 / false / "" (e.g. {var} = 0 is true when var is null)
21
23
  * @returns {Array|null} DevExpress format filter
22
24
  */
23
- function convert(ast, ResultObject = null, enableShortCircuit = true, isValueNullShortCircuit = false, treatNumberAsNullableBit = false) {
25
+ function convert(ast, ResultObject = null, enableShortCircuit = true, isValueNullShortCircuit = false, treatNumberAsNullableBit = false, treatNullAsFalse = false) {
24
26
  // Set up global context
25
27
  resultObject = ResultObject;
26
28
  EnableShortCircuit = enableShortCircuit;
27
29
  IsValueNullShortCircuit = isValueNullShortCircuit;
28
30
  TreatNumberAsNullableBit = treatNumberAsNullableBit;
31
+ TreatNullAsFalse = treatNullAsFalse;
29
32
 
30
33
  // Process the AST
31
34
  let result = processAstNode(ast);
@@ -291,6 +294,27 @@ function DevExpressConverter() {
291
294
  return value === 0 || value === 1 ? Boolean(value) : value;
292
295
  }
293
296
 
297
+ /**
298
+ * Converts boolean and numeric strings to their literal values.
299
+ *
300
+ * "true"/"false" (case-insensitive) become booleans and numeric strings become numbers.
301
+ * Empty strings and any other value are returned unchanged.
302
+ *
303
+ * @param {*} value - The value to normalize.
304
+ * @returns {*} - The literal value, or the original value if it is not a boolean or numeric string.
305
+ */
306
+ function normalizeLiteralString(value) {
307
+ if (typeof value !== "string") return value;
308
+
309
+ const trimmed = value.trim();
310
+ const lower = trimmed.toLowerCase();
311
+ if (lower === "true") return true;
312
+ if (lower === "false") return false;
313
+ if (trimmed !== "" && !isNaN(trimmed)) return Number(trimmed);
314
+
315
+ return value;
316
+ }
317
+
294
318
 
295
319
  /**
296
320
  * Handles function calls, focusing on ISNULL.
@@ -553,8 +577,21 @@ function DevExpressConverter() {
553
577
  if (left == null && leftDefault != undefined) left = leftDefault;
554
578
  if (right == null && rightDefault != undefined) right = rightDefault;
555
579
 
580
+ // Placeholders may resolve to strings like "1", "0", "true" or "False"
581
+ left = normalizeLiteralString(left);
582
+ right = normalizeLiteralString(right);
583
+
556
584
  if ((left !== null && isNaN(left)) || (right !== null && isNaN(right))) return null;
557
585
 
586
+ // With TreatNullAsFalse, NULL behaves like 0 / false / "" (NULL = 0 is true)
587
+ if (TreatNullAsFalse && (left === null) !== (right === null)) {
588
+ const other = left === null ? right : left;
589
+ if (other === 0 || other === false || other === "") {
590
+ if (operator === '=' || operator === '==') return true;
591
+ if (operator === '<>' || operator === '!=') return false;
592
+ }
593
+ }
594
+
558
595
  // NULL only equals NULL (NULL = 0 is false); use ISNULL(x, 0) to treat NULL as 0
559
596
  if (left === null || right === null) {
560
597
  if (operator === '=' || operator === '==') return left === right;
@@ -592,9 +629,10 @@ const devExpressConverter = DevExpressConverter();
592
629
  * @param {boolean} options.enableShortCircuit - Enable or disable short-circuit evaluation
593
630
  * @param {boolean} options.isValueNullShortCircuit - Enable or disable null short-circuit evaluation
594
631
  * @param {boolean} options.treatNumberAsNullableBit - Enable or disable optional boolean with number conversion
632
+ * @param {boolean} options.treatNullAsFalse - Evaluate null as equal to 0 / false / "" during short-circuit evaluation
595
633
  * @returns {Array|null} DevExpress format filter
596
634
  */
597
635
  export function convertToDevExpressFormat({ ast, resultObject = null, options = {} }) {
598
- const { enableShortCircuit = true, isValueNullShortCircuit = false, treatNumberAsNullableBit = false } = options;
599
- return devExpressConverter.init(ast, resultObject, enableShortCircuit, isValueNullShortCircuit, treatNumberAsNullableBit);
636
+ const { enableShortCircuit = true, isValueNullShortCircuit = false, treatNumberAsNullableBit = false, treatNullAsFalse = false } = options;
637
+ return devExpressConverter.init(ast, resultObject, enableShortCircuit, isValueNullShortCircuit, treatNumberAsNullableBit, treatNullAsFalse);
600
638
  }
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "converterOptions": {
20
20
  "isValueNullShortCircuit": true,
21
- "treatNumberAsNullableBit": true
21
+ "treatNumberAsNullableBit": true,
22
+ "treatNullAsFalse": true
22
23
  },
23
24
  "variables": {
24
25
  "SingleStepTransferDocument.CompanyID": 2,
@@ -74,7 +75,12 @@
74
75
  "QuoteDocument.IsVehicle": true,
75
76
  "PurchaseOrderLine.ItemID": 42,
76
77
  "PurchaseOrderLine.VarMatrixID0": null,
77
- "PurchaseOrderLine.VarMatrixID1": 5
78
+ "PurchaseOrderLine.VarMatrixID1": 5,
79
+ "SaleOrderDocument.AllowedDocType": "10809,40927",
80
+ "SaleOrderDocument.CompanyID": 2,
81
+ "SaleOrderDocument.CompanyContact": 15,
82
+ "SaleOrderDocument.ParentCompanyID": 3,
83
+ "SaleOrderDocument.IsVehicle": "True"
78
84
  },
79
85
  "groups": [
80
86
  {
@@ -971,19 +977,50 @@
971
977
  ],
972
978
  "and",
973
979
  [
974
- "varID",
975
- "=",
976
- null,
977
- {
978
- "type": "ISNULL",
979
- "position": "value",
980
- "defaultValue": 0
981
- },
982
- null
980
+ [
981
+ "varID",
982
+ "=",
983
+ null,
984
+ {
985
+ "type": "ISNULL",
986
+ "position": "value",
987
+ "defaultValue": 0
988
+ },
989
+ null
990
+ ],
991
+ "or",
992
+ [
993
+ [
994
+ "varID",
995
+ "=",
996
+ 0
997
+ ],
998
+ "or",
999
+ [
1000
+ "varID",
1001
+ "=",
1002
+ null,
1003
+ {
1004
+ "type": "ISNULL",
1005
+ "position": "column",
1006
+ "defaultValue": 0
1007
+ },
1008
+ null
1009
+ ],
1010
+ "or",
1011
+ [
1012
+ "varID",
1013
+ "=",
1014
+ false
1015
+ ]
1016
+ ]
983
1017
  ]
984
1018
  ],
985
- "expectedSql": "ISNULL(\"ItemID\", 0) = 42 AND \"varID\" = ISNULL(NULL, 0)",
986
- "note": "VarMatrixID0 is null: varID = ISNULL({VarMatrixID0}, 0) keeps the null with its ISNULL default (same as case 42), and {VarMatrixID0} = 0 is false (null only equals null), so that AND group is dropped."
1019
+ "expectedSql": [
1020
+ "ISNULL(\"ItemID\", 0) = 42",
1021
+ "AND ( \"varID\" = ISNULL(NULL, 0) OR ( \"varID\" = 0 OR ISNULL(\"varID\", 0) = 0 OR \"varID\" = 0 ) )"
1022
+ ],
1023
+ "note": "VarMatrixID0 is null: varID = ISNULL({VarMatrixID0}, 0) keeps the null with its ISNULL default (same as case 42). With treatNullAsFalse, {VarMatrixID0} = 0 is true (null is treated as 0), so that AND group reduces to ISNULL(varID, 0) = 0."
987
1024
  },
988
1025
  {
989
1026
  "id": 56,
@@ -1020,6 +1057,110 @@
1020
1057
  ],
1021
1058
  "expectedSql": "\"ItemID\" = 42",
1022
1059
  "note": "A null variable compared with a value is evaluated: null = 5 is false, so the AND group is dropped from the OR."
1060
+ },
1061
+ {
1062
+ "id": 58,
1063
+ "input": "DocType IN ({SaleOrderDocument.AllowedDocType}) AND ((ISNULL(IsDealer,0)=0 AND CompanyID={SaleOrderDocument.CompanyID}) OR (ISNULL(IsDealer,0)=1 AND CompanyContact={SaleOrderDocument.CompanyContact} AND CompanyID={SaleOrderDocument.ParentCompanyID})) AND ({SaleOrderDocument.IsVehicle}=0 OR ({SaleOrderDocument.IsVehicle}=1 AND EntityType=1004))",
1064
+ "filter": [
1065
+ [
1066
+ [
1067
+ "DocType",
1068
+ "=",
1069
+ "10809"
1070
+ ],
1071
+ "or",
1072
+ [
1073
+ "DocType",
1074
+ "=",
1075
+ "40927"
1076
+ ]
1077
+ ],
1078
+ "and",
1079
+ [
1080
+ [
1081
+ [
1082
+ [
1083
+ "IsDealer",
1084
+ "=",
1085
+ 0
1086
+ ],
1087
+ "or",
1088
+ [
1089
+ "IsDealer",
1090
+ "=",
1091
+ null,
1092
+ {
1093
+ "type": "ISNULL",
1094
+ "position": "column",
1095
+ "defaultValue": 0
1096
+ },
1097
+ null
1098
+ ],
1099
+ "or",
1100
+ [
1101
+ "IsDealer",
1102
+ "=",
1103
+ false
1104
+ ]
1105
+ ],
1106
+ "and",
1107
+ [
1108
+ "CompanyID",
1109
+ "=",
1110
+ 2
1111
+ ]
1112
+ ],
1113
+ "or",
1114
+ [
1115
+ [
1116
+ [
1117
+ [
1118
+ "IsDealer",
1119
+ "=",
1120
+ 1,
1121
+ {
1122
+ "type": "ISNULL",
1123
+ "position": "column",
1124
+ "defaultValue": 0
1125
+ },
1126
+ 1
1127
+ ],
1128
+ "or",
1129
+ [
1130
+ "IsDealer",
1131
+ "=",
1132
+ true
1133
+ ]
1134
+ ],
1135
+ "and",
1136
+ [
1137
+ "CompanyContact",
1138
+ "=",
1139
+ 15
1140
+ ]
1141
+ ],
1142
+ "and",
1143
+ [
1144
+ "CompanyID",
1145
+ "=",
1146
+ 3
1147
+ ]
1148
+ ]
1149
+ ],
1150
+ "and",
1151
+ [
1152
+ "EntityType",
1153
+ "=",
1154
+ 1004
1155
+ ]
1156
+ ],
1157
+ "expectedSql": [
1158
+ "( \"DocType\" = N'10809' OR \"DocType\" = N'40927' )",
1159
+ "AND ( ( ( \"IsDealer\" = 0 OR ISNULL(\"IsDealer\", 0) = 0 OR \"IsDealer\" = 0 ) AND \"CompanyID\" = 2 )",
1160
+ "OR ( ( ( ISNULL(\"IsDealer\", 0) = 1 OR \"IsDealer\" = 1 ) AND \"CompanyContact\" = 15 ) AND \"CompanyID\" = 3 ) )",
1161
+ "AND \"EntityType\" = 1004"
1162
+ ],
1163
+ "note": "IsVehicle resolves to the string \"True\": it must be evaluated as a boolean, so {IsVehicle}=0 is false and {IsVehicle}=1 is true, leaving only EntityType = 1004 from the last group."
1023
1164
  }
1024
1165
  ]
1025
1166
  },