sqlparse - LALR grammar based SQL parser

buildHex.pmCoveralls githubGitHub releaseGitHub Release DateGitHub commits since latest release

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.

1. Usage

Example code:

SELECT column_a|:f()|, column_b 
  FROM table_a
 WHERE column_b = 'test'
 ORDER BY 2 DESC,
          1;

Parsing the example code:

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,[{&#39;:&#39;,{&#39;fun&#39;,<<"f">>,[]},
                              <<"column_a">>},
                         <<"column_b">>]},
                {from,[<<"table_a">>]},
                {where,{&#39;=&#39;,<<"column_b">>,<<"&#39;test&#39;">>}},
                {&#39;hierarchical query&#39;,{}},
                {&#39;group by&#39;,[]},
                {having,{}},
                {&#39;order by&#39;,[{<<"2">>,<<"desc">>},{<<"1">>,<<>>}]}]},
       {extra,<<>>}}],
     [{&#39;SELECT&#39;,1},
      {&#39;NAME&#39;,8,"column_a"},
      {&#39;JSON&#39;,1,":f()"},
      {&#39;,&#39;,1},
      {&#39;NAME&#39;,8,"column_b"},
      {&#39;FROM&#39;,1},
      {&#39;NAME&#39;,7,"table_a"},
      {&#39;WHERE&#39;,1},
      {&#39;NAME&#39;,8,"column_b"},
      {&#39;=&#39;,1},
      {&#39;STRING&#39;,1,"&#39;test&#39;"},
      {&#39;ORDER&#39;,1},
      {&#39;BY&#39;,1},
      {&#39;INTNUM&#39;,1,"2"},
      {&#39;DESC&#39;,1},
      {&#39;,&#39;,1},
      {&#39;INTNUM&#39;,1,"1"},
      {&#39;;&#39;,1}]}}

Access the parse tree of the example code:

2> ParseTree.
[{{select,[{fields,[{&#39;:&#39;,{&#39;fun&#39;,<<"f">>,[]},<<"column_a">>},
                    <<"column_b">>]},
           {from,[<<"table_a">>]},
           {where,{&#39;=&#39;,<<"column_b">>,<<"&#39;test&#39;">>}},
           {&#39;hierarchical query&#39;,{}},
           {&#39;group by&#39;,[]},
           {having,{}},
           {&#39;order by&#39;,[{<<"2">>,<<"desc">>},{<<"1">>,<<>>}]}]},
  {extra,<<>>}}]

Access the token list of the example code:

3> Tokens.
[{&#39;SELECT&#39;,1},
 {&#39;NAME&#39;,8,"column_a"},
 {&#39;JSON&#39;,1,":f()"},
 {&#39;,&#39;,1},
 {&#39;NAME&#39;,8,"column_b"},
 {&#39;FROM&#39;,1},
 {&#39;NAME&#39;,7,"table_a"},
 {&#39;WHERE&#39;,1},
 {&#39;NAME&#39;,8,"column_b"},
 {&#39;=&#39;,1},
 {&#39;STRING&#39;,1,"&#39;test&#39;"},
 {&#39;ORDER&#39;,1},
 {&#39;BY&#39;,1},
 {&#39;INTNUM&#39;,1,"2"},
 {&#39;DESC&#39;,1},
 {&#39;,&#39;,1},
 {&#39;INTNUM&#39;,1,"1"},
 {&#39;;&#39;,1}]

Compile the code from a parse tree:

4> sqlparse_fold:top_down(sqlparse_format_flat, ParseTree, []).
<<"select column_a|:f()|, column_b from table_a where column_b = &#39;test&#39; order by 2 desc, 1">>

2. Documentation

The documentation for sqlparse is available here: Wiki.

3. Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request