Lotus.Source.Adapters.Ecto.SQL.Validator (Lotus v1.0.0)

Copy Markdown View Source

Validates SQL syntax by preparing it against the database without executing.

Uses EXPLAIN to parse the query server-side. Before validation, {{var}} placeholders are replaced with NULL and [[...]] optional brackets are stripped so the raw template can be checked.

Summary

Functions

Validates SQL syntax against the given data source.

Functions

validate(sql, data_source)

@spec validate(
  String.t() | module() | Lotus.Source.Adapter.t(),
  String.t() | module() | nil
) ::
  :ok | {:error, String.t()}

Validates SQL syntax against the given data source.

Neutralizes Lotus template syntax ({{var}}NULL, [[...]] → inner content) and runs EXPLAIN to check whether the database can parse the statement.

Returns :ok if the SQL is valid, or {:error, reason} with the database error message if it is not.

Examples

iex> Lotus.Source.Adapters.Ecto.SQL.Validator.validate("SELECT 1", "postgres")
:ok

iex> Lotus.Source.Adapters.Ecto.SQL.Validator.validate("NOT VALID SQL", "postgres")
{:error, "SQL syntax error: ..."}