Skip to content

Instantly share code, notes, and snippets.

@dsferruzza
Created March 13, 2014 14:13
Show Gist options
  • Save dsferruzza/9529264 to your computer and use it in GitHub Desktop.
Save dsferruzza/9529264 to your computer and use it in GitHub Desktop.
Draft of AST for simpleSqlParser
/*
SELECT t1.col1, t2.col2 AS c2, NOW() AS "current time"
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t1.id = t2.id_table1
WHERE t1.col1 > 0 AND t1.col1 < 50
ORDER BY t2.col2 ASC
LIMIT 10,5
*/
var ast = {
expression: "SELECT t1.col1, t2.col2 AS c2, NOW() AS \"current time\" FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.id = t2.id_table1 WHERE t1.col1 > 0 ORDER BY t2.col2 LIMIT 10,5",
type: "select",
select: [
{ expression: "t1.col1", table: "t1", column: "col1", alias: null },
{ expression: "t2.col2", table: "t2", column: "col2", alias: "c2" },
{ expression: "NOW()", table: null, column: null, alias: "current time" },
],
from: [
{ name: "table1", alias: "t1" },
],
join: [
{
type: "left",
table: "table2",
alias: "t2",
condition: {
expression: "t1.id = t2.id_table1",
tree: {
{ left: "t1.id", operator: "=", right: "t2.id_table1" },
},
},
},
],
where: {
expression: "t1.col1 > 0 AND t1.col1 < 50",
tree: {
{
login: "and",
terms: [
{ left: "t1.col1", operator: ">", right: "0" },
{ left: "t1.col1", operator: "<", right: "50" },
],
},
},
},
order: [
{ expression: "t2.col2 ASC", table: "t2", column: "col2", order: "ASC" },
],
limit: {
from: 10,
nb: 5,
},
};
/*
If ast.type == "select", the following fields must exist (but can be empty):
- expression (string)
- select (array)
- from (array)
- join (array)
- where (array)
- order (array)
- limit (object)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment