/sqlparser

SQL Parsers

Primary LanguageErlangGNU Lesser General Public License v2.1LGPL-2.1

SQL Parser

Build Status Codecov License: LGPL 2.1 Hex

SQL Parsers let you to parse SQL strings to generic SQL records.

Usage

If you want to use, only add this in rebar.config using rebar3:

{deps, [
    {sqlparser, "0.3.1"}
]}.

The way to use it in the code:

-include_lib("sqlparser/include/sqlparser.hrl").

parsing(SQL) ->
    mysql_parser:parse(SQL).

You can use two different parsers at this moment: mysql_parser and sql92_parser. The second one was included from sqlapi. Both are intended to do the same but mysql_parser is more complete and less prone to fail and sql92_parser is faster (a lot more):

1> CompareNew = fun(Q) -> timer:tc(fun() -> sql92_parser:parse(Q) end) end.
2> CompareLegacy = fun(Q) -> timer:tc(fun() -> mysql_parser:parse(Q) end) end.
3> CompareNew("SELECT name, surname, nickname FROM users WHERE country = 'Spain'").
{63,
 {select,[{key,<<"name">>,<<"name">>,undefined},
          {key,<<"surname">>,<<"surname">>,undefined},
          {key,<<"nickname">>,<<"nickname">>,undefined}],
         [{table,<<"users">>,<<"users">>}],
         {condition,eq,
                    {key,<<"country">>,<<"country">>,undefined},
                    {value,undefined,<<"Spain">>}},
         undefined,undefined,undefined,undefined}}
4> CompareLegacy("SELECT name, surname, nickname FROM users WHERE country = 'Spain'").
{1803,
 {select,[{key,<<"name">>,<<"name">>,undefined},
          {key,<<"surname">>,<<"surname">>,undefined},
          {key,<<"nickname">>,<<"nickname">>,undefined}],
         [{table,<<"users">>,<<"users">>}],
         {condition,eq,
                    {key,<<"country">>,<<"country">>,undefined},
                    {value,undefined,<<"Spain">>}},
         undefined,undefined,undefined,undefined}}

For the same query mysql_parser takes 1803 microseconds (or 1,8 ms) and sql92 takes 63 microseconds (or 0,06 ms).

Enjoy!