Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward compatibility from sql #1200

Open
jowher opened this issue Feb 3, 2025 · 10 comments
Open

Backward compatibility from sql #1200

jowher opened this issue Feb 3, 2025 · 10 comments

Comments

@jowher
Copy link

jowher commented Feb 3, 2025

Is your feature request related to a problem? Please describe.
Can we populate the query builder from the sql (backward compatibility), If I provide the WHERE clause logic can we populate the query builder? That'd be a great feature to have.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@ukrbublik
Copy link
Owner

Import from SQL is supported with package @react-awesome-query-builder/sql
https://github.com/ukrbublik/react-awesome-query-builder/tree/master/packages/sql

@jowher
Copy link
Author

jowher commented Mar 11, 2025

@ukrbublik Thanks for this, I noticed one issue though, we have some custom operators like IN,
when I try to import a query with IN it is throwing error: ["Can't convert operator IN"]
Is there a workaround to handle this?

@ukrbublik
Copy link
Owner

What's your SQL that gives error?
In demo app you can import query like color IN ('green', 'yellow')

@jowher
Copy link
Author

jowher commented Mar 11, 2025

tried this prcdr_cd IN ('95911','95886','95816','95913','95912','95910','94690') AND adjudication_dt < '2025-03-11'"
and it is erroring, the date filter loads properly

@jowher
Copy link
Author

jowher commented Mar 11, 2025

@ukrbublik Could it be because we are overriding certain operators : in, like, not_like etc,

I noticed for the operators that is overridden, builder not loading properly from query

Image

@ukrbublik
Copy link
Owner

For import you need to override sqlImort like

sqlImport: function (sqlObj, _, sqlDialect) {

For export - sqlOp or sqlFormatOp:

sqlFormatOp: (field, op, values, valueSrc, valueType, opDef, operatorOptions, fieldDef) => {

@jowher
Copy link
Author

jowher commented Mar 12, 2025

@ukrbublik I added as you mentioned :

 select_any_in: {
    label: 'Any in',
    sqlFormatOp: (field, _, values, valueSrc) => {
      if (valueSrc === 'value') {
        const coalesceField = `COALESCE(${field}, '')`
        return `${coalesceField} IN (${formatInputQuoteSelect(
          values,
        )})`.toUpperCase()
      } else return undefined
    },
    sqlImport: function (sqlObj: SqlObject, _: any, sqlDialect: string) {
      if (sqlObj?.operator === "IN" || sqlObj?.operator === "NOT IN") {
        const not = sqlObj?.operator === "NOT IN";
        
        const [fieldObj, valuesObj] = sqlObj.children || [];
    
        console.log("Processing sqlObj:", sqlObj);
        console.log("Operator:", sqlObj.operator);
        
        if (valuesObj && valuesObj.children && Array.isArray(valuesObj.children)) {
          const values = valuesObj.children.map(child => child.value);
          console.log("Original values for IN clause:", values);
    
          values.forEach(value => {
            console.log("Processing value:", value);
          });
    
          sqlObj.operator = not ? "not_in" : "in";
          console.log("Updated sqlObj after processing:", sqlObj);
          return sqlObj;
        } else {
          console.warn("Expected an array for IN clause values, but got:", valuesObj);
        }
      }
    },
    valueTypes: ["multiselect"],
  },

It is printing the sqlObj properly like the following but not loading to the widget

 {
    "not": false,
    "children": [
        {
            "field": "application_cd"
        },
        {
            "children": [
                {
                    "valueType": "single_quote_string",
                    "value": "C25"
                },
                {
                    "valueType": "single_quote_string",
                    "value": "ACS"
                }
            ],
            "not": false,
            "_type": "expr_list",
            "oneValueType": "single_quote_string",
            "values": [
                "C25",
                "ACS"
            ]
        }
    ],
    "operator": "in"
}

@jowher
Copy link
Author

jowher commented Mar 12, 2025

Also we have a custom IN operator or text widget (where we can pass comma separated list in textfield which we will process and create IN operator logic splitting on commas), in that case with the same sqlImport logic it is rendering in the ui but showing an error

Image

@jowher
Copy link
Author

jowher commented Mar 13, 2025

@ukrbublik In the code you pointed, I don't see any override added for in operator

@jowher
Copy link
Author

jowher commented Mar 13, 2025

when I add to load this query application_cd IN ('ACS', 'C25') AND adjudication_dt < '2025-03-12', it is only loading the date to the builder but application_cd is not there in the tree created,
I see a warning

[
    {
        "path": [
            "b9989a8b-4567-489a-bcde-f1958d32eb97",
            "9aabbaba-4567-489a-bcde-f1958d34e101"
        ],
        "errors": [
            {
                "key": "INCOMPLETE_LHS",
                "args": {},
                "side": "lhs",
                "fixed": true,
                "str": "Incomplete LHS"
            }
        ],
        "itemStr": "?",
        "itemPosition": {
            "caseNo": null,
            "globalNoByType": 1,
            "indexPath": [
                1
            ],
            "globalLeafNo": 1,
            "index": 2,
            "type": "rule",
            "isDeleted": true
        },
        "itemPositionStr": "Deleted leaf #2 (index path: 2)"
    }
]

I override my 'select_any_in' operator as I provided in the previous comment and that is consoling the sqlObj as in the previous comment, but for some reason it is not setting properly to the tree with the above warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants