Git Product home page Git Product logo

sqlparse's Introduction

sqlparse - LALR grammar based SQL parser

build Hex.pm Coveralls github GitHub release GitHub Release Date GitHub 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,[{':',{'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}]}}

Access the parse tree of the example code:

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,<<>>}}]

Access the token list of the example code:

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}]

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 = 'test' 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

sqlparse's People

Contributors

walter-weinmann avatar c-bik avatar stoch avatar k2informatics avatar acautin avatar shamis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.