SqlParserEx (sql_parser_ex v0.1.0)

Copy Markdown View Source

Elixir NIF wrapper for the sqlparser Rust crate.

Parses SQL into an AST (as a decoded map) and reconstructs SQL from an AST.

Dialects

Pass a dialect atom as the dialect: option. Defaults to :generic.

Limitations

to_sql/2 accepts a dialect: option for API consistency, but the underlying Rust serializer uses a dialect-agnostic Display implementation. The dialect argument is validated but does not affect the reconstructed SQL output.

Examples

iex> {:ok, ast} = SqlParserEx.parse("SELECT 1")
iex> is_map(ast) and map_size(ast) > 0
true

Summary

Functions

Returns all supported dialect atoms.

Parses a SQL string and returns the single statement as an AST map.

Parses a SQL string and returns all statements as a list of AST maps.

Reconstructs a SQL string from an AST map returned by parse/2 or parse_many/2.

Types

dialect()

@type dialect() ::
  :generic
  | :ansi
  | :postgres
  | :mysql
  | :sqlite
  | :mssql
  | :bigquery
  | :clickhouse
  | :duckdb
  | :databricks
  | :hive
  | :redshift
  | :snowflake

parse_error()

@type parse_error() ::
  String.t()
  | {:unknown_dialect, atom()}
  | {:encode_error, Jason.EncodeError.t() | String.t()}

sql_opt()

@type sql_opt() :: {:dialect, dialect()}

sql_string()

@type sql_string() :: String.t()

Functions

dialects()

@spec dialects() :: [dialect()]

Returns all supported dialect atoms.

parse(sql, opts \\ [])

@spec parse(sql_string(), [sql_opt()]) :: {:ok, map()} | {:error, parse_error()}

Parses a SQL string and returns the single statement as an AST map.

Returns {:error, reason} if zero or multiple statements are found. Use parse_many/2 for multi-statement SQL.

parse_many(sql, opts \\ [])

@spec parse_many(sql_string(), [sql_opt()]) ::
  {:ok, [map()]} | {:error, parse_error()}

Parses a SQL string and returns all statements as a list of AST maps.

to_sql(ast, opts \\ [])

@spec to_sql(map(), [sql_opt()]) :: {:ok, sql_string()} | {:error, parse_error()}

Reconstructs a SQL string from an AST map returned by parse/2 or parse_many/2.

Note: the dialect: option is validated but does not affect output. The underlying Rust serializer uses a dialect-agnostic Display implementation.