Created
November 19, 2018 20:38
-
-
Save zebzhao/c627845e71928e4ee45243ab1be3407b to your computer and use it in GitHub Desktop.
Example of converting Angular 2 query builder object to SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| </head> | |
| <body> | |
| <script> | |
| var data = { | |
| "condition": "and", | |
| "rules": [ | |
| { | |
| "field": "age", | |
| "operator": "<=", | |
| "entity": "physical" | |
| }, | |
| { | |
| "field": "birthday", | |
| "operator": "=", | |
| "value": "2018-11-19T19:58:06.108Z", | |
| "entity": "nonphysical" | |
| }, | |
| { | |
| "condition": "or", | |
| "rules": [ | |
| { | |
| "field": "gender", | |
| "operator": "=", | |
| "entity": "physical", | |
| "value": "f" | |
| }, | |
| { | |
| "field": "occupation", | |
| "operator": "in", | |
| "entity": "nonphysical", | |
| "value": [ | |
| "unemployed" | |
| ] | |
| }, | |
| { | |
| "field": "school", | |
| "operator": "is null", | |
| "entity": "nonphysical" | |
| }, | |
| { | |
| "field": "notes", | |
| "operator": "=", | |
| "entity": "nonphysical" | |
| } | |
| ] | |
| } | |
| ] | |
| }; | |
| function valueToSQL(value) { | |
| switch (typeof value) { | |
| case 'string': | |
| return "'" + value + "'"; | |
| case 'boolean': | |
| return value ? '1' : '0'; | |
| case 'number': | |
| if (isFinite(value)) return value; | |
| } | |
| } | |
| function isDefined(value) { | |
| return value !== undefined; | |
| } | |
| function basicRulesetToSQL(ruleset) { | |
| return ruleset.rules.map((rule) => { | |
| if (rule.rules) { | |
| return "(" + basicRulesetToSQL(rule) + ")"; | |
| } | |
| var column = rule.field, | |
| operator, value; | |
| switch (rule.operator) { | |
| case "is null": | |
| case "is not null": | |
| operator = rule.operator; | |
| value = ""; | |
| break; | |
| case "in": | |
| case "not in": | |
| operator = rule.operator; | |
| if (Array.isArray(rule.value) && rule.value.length) | |
| value = "(" + rule.value.map(valueToSQL).filter(isDefined).join(", ") + ")"; | |
| break; | |
| default: | |
| operator = rule.operator; | |
| value = valueToSQL(rule.value); | |
| break; | |
| } | |
| if (isDefined(column) && isDefined(value) && isDefined(operator)) { | |
| return "(" + (column + " " + operator + " " + value).trim() + ")"; | |
| } | |
| }).filter(isDefined).join(" " + ruleset.condition + " "); | |
| } | |
| console.log(basicRulesetToSQL(data)); | |
| </script> | |
| </body> | |
| </html> |
Hi,
I ported your code to PHP and would like to share it.
The file is available here.
Thank you very much.
I am using nodejs for backend. I think this code will work fine. If any updated code is there for nodejs let me know please.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example