CommonParser v0.2.0 CommonParser.SimpleSql View Source

SQL parser

a sql: select * from account_state where balance > 100

tag := ascii_string([?a..?z], min: 1)

tag_list := tag | tag , tag

selector := * | tag_list

condition := expr

sql := select selector from tag where condition

Link to this section Summary

Functions

parse a full sql. Note that where condition is not parsed. You can use CommonParser.Expr.parse/2 to validate the where condition

parse from statement. For testing purpose. Please use parse/2 instead

parse select statement. For testing purpose. Please use parse/2 instead

Link to this section Functions

Link to this function

parse(binary, opts \\ []) View Source
parse(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: pos_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

parse a full sql. Note that where condition is not parsed. You can use CommonParser.Expr.parse/2 to validate the where condition.

iex> CommonParser.SimpleSql.parse("select from abc where c < 10") {:ok, [select: [""], from: [:abc], where: ["c < 10"]], "", %{}, {1, 0}, 30}

iex(7)> CommonParser.SimpleSql.parse(~S(select balance, nonce, num_txs from abc where c < 10 and b not in ["c", "d"])) {:ok, [select: ["balance", "nonce", "num_txs"], from: [:abc], where: [~S(c < 10 and b not in ["c", "d"])]], "", %{}, {1, 0}, 76}

Link to this function

parse_from(binary, opts \\ []) View Source
parse_from(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: pos_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

parse from statement. For testing purpose. Please use parse/2 instead.

iex> CommonParser.SimpleSql.parse_from("from abc") {:ok, [from: [:abc]], "", %{}, {1, 0}, 8}

iex(4)> CommonParser.SimpleSql.parse_from("from abc, def") {:ok, [from: [:abc]], ", def", %{}, {1, 0}, 8}

Link to this function

parse_select(binary, opts \\ []) View Source
parse_select(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: pos_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

parse select statement. For testing purpose. Please use parse/2 instead.

iex> CommonParser.SimpleSql.parse_select("select ") {:ok, [select: [""]], "", %{}, {1, 0}, 8}

iex> CommonParser.SimpleSql.parse_select("select a, b, c, a") {:ok, [select: ["a", "b", "c"]], "", %{}, {1, 0}, 17}