Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ console.log("DevExpress Filter:", JSON.stringify(devexpressFilter, null, 2));
const devexpressFilter = convertAstToDevextreme(ast, sampleState, false); // Disables short-circuiting
```

### **DevExtreme with React Example**

To see an example of how to use `sqlparser-devexpress` with DevExtreme DataGrid in React, check out the live example on CodeSandbox:

[DevExtreme DataGrid with SQL Filter Example](https://codesandbox.io/p/sandbox/with-data-grid-with-sql-as-filter-string-my64v2)

In this example, SQL `WHERE` clauses are parsed into an Abstract Syntax Tree (AST) and then transformed into a DevExpress-compatible filter format that is used directly in the DataGrid component for filtering data.

## Roadmap

- Support for additional SQL operators and functions.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sqlparser-devexpress",
"version": "2.3.4",
"version": "2.3.6",
"main": "src/index.js",
"type": "module",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/@types/default.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ParsedResult {

export interface ConvertToDevExpressFormatParams {
ast: any; // Define a more specific type if possible
resultObject: StateDataObject;
resultObject?: StateDataObject | null;
enableShortCircuit?: boolean;
}

Expand All @@ -30,6 +30,6 @@ export function convertSQLToAst(

export function convertAstToDevextreme(
ast: any, // Define a more specific type if possible
state: StateDataObject,
state?: StateDataObject | null,
enableShortCircuit?: boolean,
): any;
8 changes: 7 additions & 1 deletion src/core/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ function DevExpressConverter() {

const left = ast.left !== undefined ? processAstNode(ast.left) : convertValue(ast.field);
const right = ast.right !== undefined ? processAstNode(ast.right) : convertValue(ast.value);
const operatorToken = ast.operator.toLowerCase();
let operatorToken = ast.operator.toLowerCase();

if(operatorToken === "like") {
operatorToken = "contains";
}else if (operatorToken === "not like") {
operatorToken = "notcontains";
}

let comparison = [left, operatorToken, right];

Expand Down
9 changes: 9 additions & 0 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ export function parse(input, variables = []) {
if (currentToken.type === "function") {
const functionNode = parseFunction();

if(fieldType === "identifier" && functionNode.type === "function") {
return {
type: "comparison",
field,
operator,
value: functionNode
}
}

// Wrap the function inside a comparison if it's directly after an operator
const leftComparison = {
type: "comparison",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function convertSQLToAst(filterString, enableConsoleLogs = false) {
return parsedResult;
}

export function convertAstToDevextreme(ast, state, enableShortCircuit = true) {
export function convertAstToDevextreme(ast, state = null, enableShortCircuit = true) {
return convertToDevExpressFormat({ ast, resultObject: state, enableShortCircuit })
}

Expand Down
8 changes: 8 additions & 0 deletions tests/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ describe("Parser SQL to dx Filter Builder", () => {
]

]
},
{
input: "CompanyName like '{LeadDocument.CompanyID}' AND BranchName not like '{LeadDocument.BranchID}'",
expected: [
["CompanyName", "contains", "7"],
"and",
["BranchName", "notcontains", "42"]
]
}
];

Expand Down