# `SqlParserEx`
[🔗](https://github.com/ndrluis/sql-parser-ex/blob/v0.1.0/lib/sql_parser_ex.ex#L1)

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

# `dialect`

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

# `parse_error`

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

# `sql_opt`

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

# `sql_string`

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

# `dialects`

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

Returns all supported dialect atoms.

# `parse`

```elixir
@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`

```elixir
@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`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
