sqlparse is a production-ready SQL parser written in pure Erlang. sqlparse is aligned to the Oracle SQL language and enriched with imem and JSONPath specific features.
SELECT column_a|:f()|, column_b
FROM table_a
WHERE column_b = 'test'
ORDER BY 2 DESC,
1;
1> {ok, {ParseTree, Tokens}} = sqlparse:parsetree_with_tokens("SELECT column_a|:f()|, column_b FROM table_a WHERE column_b = 'test' ORDER BY 2 DESC, 1;").
{ok,{[{{select,[{fields,[{':',{'fun',<<"f">>,[]},
<<"column_a">>},
<<"column_b">>]},
{from,[<<"table_a">>]},
{where,{'=',<<"column_b">>,<<"'test'">>}},
{'hierarchical query',{}},
{'group by',[]},
{having,{}},
{'order by',[{<<"2">>,<<"desc">>},{<<"1">>,<<>>}]}]},
{extra,<<>>}}],
[{'SELECT',1},
{'NAME',8,"column_a"},
{'JSON',1,":f()"},
{',',1},
{'NAME',8,"column_b"},
{'FROM',1},
{'NAME',7,"table_a"},
{'WHERE',1},
{'NAME',8,"column_b"},
{'=',1},
{'STRING',1,"'test'"},
{'ORDER',1},
{'BY',1},
{'INTNUM',1,"2"},
{'DESC',1},
{',',1},
{'INTNUM',1,"1"},
{';',1}]}}
2> ParseTree.
[{{select,[{fields,[{':',{'fun',<<"f">>,[]},<<"column_a">>},
<<"column_b">>]},
{from,[<<"table_a">>]},
{where,{'=',<<"column_b">>,<<"'test'">>}},
{'hierarchical query',{}},
{'group by',[]},
{having,{}},
{'order by',[{<<"2">>,<<"desc">>},{<<"1">>,<<>>}]}]},
{extra,<<>>}}]
3> Tokens.
[{'SELECT',1},
{'NAME',8,"column_a"},
{'JSON',1,":f()"},
{',',1},
{'NAME',8,"column_b"},
{'FROM',1},
{'NAME',7,"table_a"},
{'WHERE',1},
{'NAME',8,"column_b"},
{'=',1},
{'STRING',1,"'test'"},
{'ORDER',1},
{'BY',1},
{'INTNUM',1,"2"},
{'DESC',1},
{',',1},
{'INTNUM',1,"1"},
{';',1}]
4> sqlparse_fold:top_down(sqlparse_format_flat, ParseTree, []).
<<"select column_a|:f()|, column_b from table_a where column_b = 'test' order by 2 desc, 1">>
The documentation for sqlparse is available here: Wiki.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request