Filip Kis
Filip Kis

Reputation: 1660

AlaSQL: Getting where opperands

I have a specific request that I'm rather sure it's not trivial, but thought of asking just in case. If I have any query with WHERE condition:

SELECT * FROM table1 WHERE a = b AND a->fn(b->c) > 0

I would like to know all the operands that take part in WHERE, in this case ['a', 'b', 'a->fn(b->c)', 0].

The reason is that I would want to keep track of those values and if any changes then I would just re-evaluate the query.

Upvotes: 1

Views: 459

Answers (1)

agershun
agershun

Reputation: 4107

You can use alasql.parse() function to produce Abstract Syntax Tree from SQL statement. To print part of the tree from WHERE clause of SELECT statement please use:

var ast = alasql.parse('SELECT * FROM table1 WHERE a = b AND a->fn(b->c) > 0');
console.log(ast.statements[0].where);

Then you will see the tree structure.

{"expression":
   {"left":
       {"left": {"columnid":"a"},"op":"=", "right":{"columnid":"b"}},
     "op":"AND",
     "right":{"left":
                  {"left":{"columnid":"a"},
                   "op":"->",
                   "right":
                        {"funcid":"fn","args":[
                          {"left":{"columnid":"b"},
                           "op":"->",
                           "right":"c"}
                         ]}},
              "op":">",
              "right":{"value":0}}}}

You can walk along this tree to collect all arguments. Each node is an object of yy.xxx types, so you can test it:

if(node instanceof yy.Column) // then process as the column

Is this answer for your question?

Upvotes: 3

Related Questions