sqlparser-devexpress 2.3.3 → 2.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.
- package/README.md +54 -0
- package/package.json +1 -1
- package/src/@types/default.d.ts +1 -1
- package/src/core/converter.js +11 -5
- package/src/core/parser.js +9 -0
- package/src/debug.js +1 -1
- package/src/index.js +1 -1
- package/tests/parser.test.js +17 -7
package/README.md
CHANGED
|
@@ -126,6 +126,60 @@ console.log("DevExpress Filter:", JSON.stringify(devexpressFilter, null, 2));
|
|
|
126
126
|
const devexpressFilter = convertAstToDevextreme(ast, sampleState, false); // Disables short-circuiting
|
|
127
127
|
```
|
|
128
128
|
|
|
129
|
+
### **DevExtreme with React Example**
|
|
130
|
+
|
|
131
|
+
First, install the necessary DevExtreme packages:
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
npm install devextreme devextreme-react
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Now, create a simple React component that integrates DevExtreme with the `sqlparser-devexpress` library:
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
import React, { useState } from 'react';
|
|
141
|
+
import DataGrid, { Column, FilterRow, HeaderFilter } from 'devextreme-react/data-grid';
|
|
142
|
+
import 'devextreme/dist/css/dx.light.css';
|
|
143
|
+
import { convertSQLToAst, convertAstToDevextreme } from 'sqlparser-devexpress';
|
|
144
|
+
|
|
145
|
+
const App = () => {
|
|
146
|
+
const [data, setData] = useState([
|
|
147
|
+
{ OrderID: 76548, Status: 1 },
|
|
148
|
+
{ OrderID: 76549, Status: 3 },
|
|
149
|
+
{ OrderID: 76550, Status: 2 },
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
const sqlQuery = "OrderID = {CustomerOrders.OrderID} OR Status IN (1, 3)";
|
|
153
|
+
const { ast, variables } = convertSQLToAst(sqlQuery);
|
|
154
|
+
|
|
155
|
+
// Extracted state for dynamic variables (usually fetched from your app state)
|
|
156
|
+
const sampleState = { "CustomerOrders.OrderID": 76548 };
|
|
157
|
+
|
|
158
|
+
const devexpressFilter = convertAstToDevextreme(ast, sampleState);
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div>
|
|
162
|
+
<DataGrid dataSource={data} filterValue={devexpressFilter}>
|
|
163
|
+
<Column dataField="OrderID" />
|
|
164
|
+
<Column dataField="Status" />
|
|
165
|
+
<FilterRow visible={true} />
|
|
166
|
+
<HeaderFilter visible={true} />
|
|
167
|
+
</DataGrid>
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export default App;
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### **Explanation**:
|
|
176
|
+
1. **DataGrid**: Displays the data with filtering.
|
|
177
|
+
2. **convertSQLToAst**: Converts the SQL `WHERE` clause into an AST.
|
|
178
|
+
3. **convertAstToDevextreme**: Converts the AST to the DevExtreme filter format.
|
|
179
|
+
4. **filterValue**: Applies the filter to the `DataGrid`.
|
|
180
|
+
|
|
181
|
+
This is a simple integration where the filter from SQLParser is used to filter the grid data in a React app using DevExtreme's `DataGrid`.
|
|
182
|
+
|
|
129
183
|
## Roadmap
|
|
130
184
|
|
|
131
185
|
- Support for additional SQL operators and functions.
|
package/package.json
CHANGED
package/src/@types/default.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export interface ParsedResult {
|
|
|
12
12
|
|
|
13
13
|
export interface ConvertToDevExpressFormatParams {
|
|
14
14
|
ast: any; // Define a more specific type if possible
|
|
15
|
-
resultObject
|
|
15
|
+
resultObject?: StateDataObject | null;
|
|
16
16
|
enableShortCircuit?: boolean;
|
|
17
17
|
}
|
|
18
18
|
|
package/src/core/converter.js
CHANGED
|
@@ -136,14 +136,20 @@ function DevExpressConverter() {
|
|
|
136
136
|
|
|
137
137
|
const left = ast.left !== undefined ? processAstNode(ast.left) : convertValue(ast.field);
|
|
138
138
|
const right = ast.right !== undefined ? processAstNode(ast.right) : convertValue(ast.value);
|
|
139
|
-
|
|
139
|
+
let operatorToken = ast.operator.toLowerCase();
|
|
140
|
+
|
|
141
|
+
if(operatorToken === "like") {
|
|
142
|
+
operatorToken = "contains";
|
|
143
|
+
}else if (operatorToken === "not like") {
|
|
144
|
+
operatorToken = "notcontains";
|
|
145
|
+
}
|
|
140
146
|
|
|
141
147
|
let comparison = [left, operatorToken, right];
|
|
142
148
|
|
|
143
149
|
if ((ast.left && isFunctionNullCheck(ast.left, true)) || (ast.value && isFunctionNullCheck(ast.value, false))) {
|
|
144
|
-
comparison = [[left, operatorToken, right], 'or', [left, operatorToken, null]];
|
|
150
|
+
comparison = [[left, operatorToken, right], 'or', [left, operatorToken, null, {type: "ISNULL", defaultValue: (ast.left ?? ast.value).args[1]?.value}]];
|
|
145
151
|
} else if (ast.right && isFunctionNullCheck(ast.right, true)) {
|
|
146
|
-
comparison = [[left, operatorToken, right], 'or', [right, operatorToken, null]];
|
|
152
|
+
comparison = [[left, operatorToken, right], 'or', [right, operatorToken, null, {type: "ISNULL", defaultValue: ast.right.args[1]?.value}]];
|
|
147
153
|
}
|
|
148
154
|
|
|
149
155
|
// Apply short-circuit evaluation if enabled
|
|
@@ -322,7 +328,7 @@ function DevExpressConverter() {
|
|
|
322
328
|
* @returns {boolean} True if the condition is always true.
|
|
323
329
|
*/
|
|
324
330
|
function isAlwaysTrue(condition) {
|
|
325
|
-
return Array.isArray(condition) && condition.length
|
|
331
|
+
return Array.isArray(condition) && condition.length >= 3 && evaluateExpression(...condition) == true;
|
|
326
332
|
}
|
|
327
333
|
|
|
328
334
|
/**
|
|
@@ -331,7 +337,7 @@ function DevExpressConverter() {
|
|
|
331
337
|
* @returns {boolean} True if the condition is always false.
|
|
332
338
|
*/
|
|
333
339
|
function isAlwaysFalse(condition) {
|
|
334
|
-
return Array.isArray(condition) && condition.length
|
|
340
|
+
return Array.isArray(condition) && condition.length >= 3 && evaluateExpression(...condition) == false;
|
|
335
341
|
}
|
|
336
342
|
|
|
337
343
|
/**
|
package/src/core/parser.js
CHANGED
|
@@ -173,6 +173,15 @@ export function parse(input, variables = []) {
|
|
|
173
173
|
if (currentToken.type === "function") {
|
|
174
174
|
const functionNode = parseFunction();
|
|
175
175
|
|
|
176
|
+
if(fieldType === "identifier" && functionNode.type === "function") {
|
|
177
|
+
return {
|
|
178
|
+
type: "comparison",
|
|
179
|
+
field,
|
|
180
|
+
operator,
|
|
181
|
+
value: functionNode
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
176
185
|
// Wrap the function inside a comparison if it's directly after an operator
|
|
177
186
|
const leftComparison = {
|
|
178
187
|
type: "comparison",
|
package/src/debug.js
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
// return convertToDevExpressFormat({ ast: astTree, resultObject: sampleData });
|
|
29
29
|
// }
|
|
30
30
|
|
|
31
|
-
// const devexpress = parseFilterString("
|
|
31
|
+
// const devexpress = parseFilterString("CompanyID not like '{CustomerOrders.OrderID}'", sampleData);
|
|
32
32
|
// console.log("DevExpress Filter:", JSON.stringify(devexpress, null, 2));
|
|
33
33
|
// // const devexpress = parseFilterString("(RS2ID in ({LeadStatementGlobalRpt.StateID}) Or ({LeadStatementGlobalRpt.StateID} =0)) And (RS3ID in (0,{LeadStatementGlobalRpt.RegionID}) Or {LeadStatementGlobalRpt.RegionID} =0 )", sampleData);
|
|
34
34
|
|
package/src/index.js
CHANGED
|
@@ -16,7 +16,7 @@ export function convertSQLToAst(filterString, enableConsoleLogs = false) {
|
|
|
16
16
|
return parsedResult;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export function convertAstToDevextreme(ast, state, enableShortCircuit = true) {
|
|
19
|
+
export function convertAstToDevextreme(ast, state = null, enableShortCircuit = true) {
|
|
20
20
|
return convertToDevExpressFormat({ ast, resultObject: state, enableShortCircuit })
|
|
21
21
|
}
|
|
22
22
|
|
package/tests/parser.test.js
CHANGED
|
@@ -137,11 +137,11 @@ describe("Parser SQL to dx Filter Builder", () => {
|
|
|
137
137
|
expected: [
|
|
138
138
|
["SourceID", "=", 2],
|
|
139
139
|
"or",
|
|
140
|
-
["SourceID", "=", null],
|
|
140
|
+
["SourceID", "=", null,{ "defaultValue": 0, "type": "ISNULL"}],
|
|
141
141
|
"or",
|
|
142
142
|
["SourceID", "=", 0],
|
|
143
143
|
"or",
|
|
144
|
-
["SourceID", "=", null]
|
|
144
|
+
["SourceID", "=", null,{ "defaultValue": 0, "type": "ISNULL"}]
|
|
145
145
|
]
|
|
146
146
|
},
|
|
147
147
|
{
|
|
@@ -153,14 +153,14 @@ describe("Parser SQL to dx Filter Builder", () => {
|
|
|
153
153
|
[
|
|
154
154
|
["CompanyID", "=", 0],
|
|
155
155
|
"or",
|
|
156
|
-
["CompanyID", "=", null]
|
|
156
|
+
["CompanyID", "=", null,{ "defaultValue": 0, "type": "ISNULL"}]
|
|
157
157
|
]
|
|
158
158
|
],
|
|
159
159
|
"and",
|
|
160
160
|
[
|
|
161
161
|
["IsSubdealer", "=", true],
|
|
162
162
|
"or",
|
|
163
|
-
["IsSubdealer", "=", null]
|
|
163
|
+
["IsSubdealer", "=", null,{ "defaultValue": 0, "type": "ISNULL"}]
|
|
164
164
|
]
|
|
165
165
|
]
|
|
166
166
|
},
|
|
@@ -187,7 +187,7 @@ describe("Parser SQL to dx Filter Builder", () => {
|
|
|
187
187
|
expected: [
|
|
188
188
|
["TicketID", "=", 123],
|
|
189
189
|
"or",
|
|
190
|
-
["TicketID", "=", null]
|
|
190
|
+
["TicketID", "=", null,{ "defaultValue": 0, "type": "ISNULL"}]
|
|
191
191
|
]
|
|
192
192
|
},
|
|
193
193
|
{
|
|
@@ -195,13 +195,23 @@ describe("Parser SQL to dx Filter Builder", () => {
|
|
|
195
195
|
expected: [
|
|
196
196
|
["CompanyID", "=", 7],
|
|
197
197
|
"or",
|
|
198
|
-
["CompanyID", "=", null],
|
|
198
|
+
["CompanyID", "=", null,{ "defaultValue": 0, "type": "ISNULL"}],
|
|
199
199
|
"or",
|
|
200
200
|
["CompanyID", "=", 0],
|
|
201
201
|
"or",
|
|
202
|
-
["CompanyID", "=", null
|
|
202
|
+
["CompanyID", "=", null,{ "defaultValue": 0, "type": "ISNULL"},
|
|
203
|
+
|
|
204
|
+
]
|
|
203
205
|
|
|
204
206
|
]
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
input: "CompanyName like '{LeadDocument.CompanyID}' AND BranchName not like '{LeadDocument.BranchID}'",
|
|
210
|
+
expected: [
|
|
211
|
+
["CompanyName", "contains", "7"],
|
|
212
|
+
"and",
|
|
213
|
+
["BranchName", "notcontains", "42"]
|
|
214
|
+
]
|
|
205
215
|
}
|
|
206
216
|
];
|
|
207
217
|
|