AshClickhouse.Identifier (AshClickhouse v0.4.0)

Copy Markdown View Source

Helpers for safely quoting and sanitizing ClickHouse identifiers.

ClickHouse identifiers (table names, column names, database names) may be quoted with backticks. Unquoted identifiers are limited to a restricted set of characters. This module provides validation and quoting helpers so the data layer can build SQL safely without opening SQL-injection vectors.

Summary

Functions

Quotes an identifier with backticks, escaping any embedded backticks.

Sanitizes an identifier, raising ArgumentError if it is invalid.

Returns true if the given identifier is a valid unquoted ClickHouse identifier.

Validates a database name, raising ArgumentError if invalid.

Validates a table name, raising ArgumentError if invalid.

Functions

quote_name(name)

@spec quote_name(String.t() | atom()) :: String.t()

Quotes an identifier with backticks, escaping any embedded backticks.

Examples

iex> AshClickhouse.Identifier.quote_name("users")
"`users`"

iex> AshClickhouse.Identifier.quote_name("my`table")
"`my``table`"

Column identifiers go through quote_name/1 (backtick-escaping) rather than the stricter sanitize!/1 used for table/database names. This is intentional: Ash attribute names are derived from atoms and are already safe, and quote_name/1 additionally tolerates arbitrary (e.g. expression-derived) column labels without rejecting valid identifiers that merely fall outside the [a-zA-Z_][a-zA-Z0-9_]* pattern. Table and database names, by contrast, are developer-supplied strings validated up front by sanitize!/1 so a bad name fails loudly at DDL time rather than producing malformed SQL.

sanitize!(name)

@spec sanitize!(String.t() | atom()) :: String.t()

Sanitizes an identifier, raising ArgumentError if it is invalid.

A valid ClickHouse identifier consists of letters, digits, and underscores, and must not start with a digit.

valid_identifier?(name)

@spec valid_identifier?(String.t() | atom()) :: boolean()

Returns true if the given identifier is a valid unquoted ClickHouse identifier.

validate_database!(database)

@spec validate_database!(String.t() | nil) :: :ok

Validates a database name, raising ArgumentError if invalid.

validate_table!(table)

@spec validate_table!(String.t()) :: :ok

Validates a table name, raising ArgumentError if invalid.