Skip to content

Commit 37d26c5

Browse files
authored
Merge pull request #20 from RohitM-IN/development
Fixed when the field and value is swapped devextreme wont handle it properly
2 parents 5309215 + 671268c commit 37d26c5

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "sqlparser-devexpress",
3-
"version": "2.3.17",
3+
"version": "2.3.19",
44
"main": "src/index.js",
55
"type": "module",
66
"scripts": {
7-
"test": "vitest"
7+
"test": "vitest run"
88
},
99
"exports": {
1010
"import": "./src/index.js",

src/core/converter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ function DevExpressConverter() {
139139
}
140140

141141
const left = ast.left !== undefined ? processAstNode(ast.left) : convertValue(ast.field);
142-
const leftDefault = ast.left?.args[1]?.value;
142+
const leftDefault = ast.left?.args && ast.left?.args[1]?.value;
143143
const right = ast.right !== undefined ? processAstNode(ast.right) : convertValue(ast.value);
144-
const rightDefault = ast.right?.args[1]?.value;
144+
const rightDefault = ast.right?.args && ast.right?.args[1]?.value;
145145
let operatorToken = ast.operator.toLowerCase();
146146
let includeExtradata = false;
147147

src/core/parser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ export function parse(input, variables = []) {
140140
// Recursively parse the right-hand expression with adjusted precedence
141141
const right = parseExpression(OPERATOR_PRECEDENCE[operator]);
142142
left = { type: "logical", operator, left, right };
143+
} else if (currentToken?.type == "identifier") {
144+
const right = parseValue(operator);
145+
let newOperator = inverseOperator(operator);
146+
147+
left = {
148+
type: "comparison",
149+
right: left,
150+
operator: newOperator,
151+
left: { type: "field", value: right }
152+
};
143153
}
144154
}
145155

@@ -218,12 +228,28 @@ export function parse(input, variables = []) {
218228
throw new Error(`Invalid comparison: ${field} ${operator} ${value}`);
219229
}
220230

231+
// Swap the field and value if the field is a placeholder and the value is an identifier
232+
if (valueType == "identifier" && fieldType == "placeholder") {
233+
let newOperator = inverseOperator(operator);
234+
return { type: "comparison", value: field, operator: newOperator, field: value, originalOperator };
235+
}
236+
221237
return { type: "comparison", field, operator, value, originalOperator };
222238
}
223239

224240
return { type: "field", value: field };
225241
}
226242

243+
function inverseOperator(operator) {
244+
switch (operator.toUpperCase()) {
245+
case ">": return "<";
246+
case "<": return ">";
247+
case ">=": return "<=";
248+
case "<=": return ">=";
249+
default: return operator; // Return the operator as is if no inverse is defined
250+
}
251+
}
252+
227253
// Parses values including numbers, strings, placeholders, and IN lists
228254
function parseValue(operatorToken) {
229255
if (!currentToken) throw new Error("Unexpected end of input");

tests/parser.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ describe("Parser SQL to dx Filter Builder", () => {
268268
{
269269
input: "ID IN ({SaleOrderStatusStmtGlobalRpt.RegionID})",
270270
expected: []
271+
},
272+
{
273+
input: "10 < ID AND ApplicableUoms IN ({WorkOrderLine.ApplicableUoms})",
274+
expected: [
275+
["ID", ">", 10],
276+
"and",
277+
[
278+
["ApplicableUoms", "=", "UOM1"],
279+
"or",
280+
["ApplicableUoms", "=", "UOM2"],
281+
"or",
282+
["ApplicableUoms", "=", "UOM3"]
283+
]
284+
],
285+
},
286+
{
287+
input: "{ServiceOrderDocument.SourceID} = ID",
288+
expected: [
289+
"ID", "=", 2
290+
]
271291
}
272292
];
273293

0 commit comments

Comments
 (0)