Lotus.Storage.Query (Lotus v0.16.4)

Copy Markdown View Source

Represents a saved Lotus query.

Queries can be stored, updated, listed, and executed by the host app. Supports {{var}} placeholders in the SQL statement for value substitution only. Variables are bound at runtime using configured variables and are only safe for SQL values (WHERE clauses, ORDER BY values, etc.), never for identifiers like table names, column names, or schema names.

Summary

Functions

Returns a MapSet of variable names that appear inside [[...]] optional clause blocks in the given SQL statement.

Extracts unique variable names from an SQL statement in order of first occurrence.

Types

t()

@type t() :: %Lotus.Storage.Query{
  __meta__: term(),
  data_repo: String.t() | nil,
  description: String.t() | nil,
  id: term(),
  inserted_at: DateTime.t(),
  name: String.t(),
  search_path: String.t() | nil,
  statement: String.t(),
  updated_at: DateTime.t(),
  variables: [Lotus.Storage.QueryVariable.t()]
}

Functions

changeset(query, attrs)

extract_optional_variable_names(statement)

@spec extract_optional_variable_names(String.t()) :: MapSet.t()

Returns a MapSet of variable names that appear inside [[...]] optional clause blocks in the given SQL statement.

extract_variables_from_statement(statement)

@spec extract_variables_from_statement(String.t()) :: [String.t()]

Extracts unique variable names from an SQL statement in order of first occurrence.

Variables are identified by the {{variable_name}} syntax.

Examples

iex> Lotus.Storage.Query.extract_variables_from_statement("SELECT * FROM users WHERE id = {{user_id}} AND status = {{status}}")
["user_id", "status"]

iex> Lotus.Storage.Query.extract_variables_from_statement("SELECT * FROM users WHERE id = {{user_id}} OR id = {{user_id}}")
["user_id"]

iex> Lotus.Storage.Query.extract_variables_from_statement("SELECT * FROM users")
[]

new(attrs)

to_sql_params(q, supplied_vars \\ %{})

@spec to_sql_params(t(), map()) :: {String.t(), [term()]}

update(query, attrs)