Last active
January 22, 2016 23:31
-
-
Save kevinswiber/6cc6945d19111faea817 to your computer and use it in GitHub Desktop.
Ensure a filter exists in a CaQL query.
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
| var caql = require('caql'); | |
| var CaqlDecompiler = require('caql-decompiler'); | |
| function checkExpression(expr) { | |
| if (expr.type === 'Conjunction' && !expr.isNegated) { | |
| return checkExpression(expr.left) || checkExpression(expr.right); | |
| } | |
| return expr.type === 'ComparisonPredicate' | |
| && expr.field === 'type' | |
| && expr.operator === 'eq' | |
| && JSON.parse(expr.value) === 'photocell' | |
| && !expr.isNegated; | |
| } | |
| var query = 'where intensity >= 3.5'; | |
| var ast; | |
| try { | |
| ast = caql.parse(query); | |
| } catch(e) { | |
| console.error(e); | |
| process.exit(); | |
| } | |
| var hasFilterExpression = checkExpression(ast.filterNode.expression); | |
| var ql = query; | |
| if (!hasFilterExpression) { | |
| var expr = ast.filterNode.expression; | |
| var filter = new caql.Ast.ComparisonPredicateNode('type', 'eq', JSON.stringify('photocell')); | |
| ast.filterNode.expression = new caql.Ast.ConjunctionNode(filter, expr); | |
| var decompiler = new CaqlDecompiler(); | |
| ql = decompiler.decompile(ast); | |
| } | |
| console.log(ql); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment