cquill/adapter

Types

The core adapter interface.

This uses a record of functions pattern rather than behaviours because:

  1. Gleam doesn’t have Elixir-style behaviours
  2. Records are first-class and composable
  3. Easier to test with mock adapters
  4. Type-safe at compile time

Type parameters:

  • conn: The connection/pool type specific to this adapter
  • row: The raw row type returned by the adapter (usually List(Dynamic))
pub opaque type Adapter(conn, row)

Declares what optional features an adapter supports. This allows compile-time checking of feature availability.

pub type AdapterCapabilities {
  AdapterCapabilities(
    transactions: Bool,
    returning: Bool,
    batch_insert: Bool,
    upsert: Bool,
    max_params: option.Option(Int),
    json_operations: Bool,
    array_types: Bool,
  )
}

Constructors

  • AdapterCapabilities(
      transactions: Bool,
      returning: Bool,
      batch_insert: Bool,
      upsert: Bool,
      max_params: option.Option(Int),
      json_operations: Bool,
      array_types: Bool,
    )

    Arguments

    transactions

    Does the adapter support multi-statement transactions?

    returning

    Does the adapter support RETURNING clauses (get inserted ID)?

    batch_insert

    Does the adapter support batch insert operations?

    upsert

    Does the adapter support upsert (INSERT ON CONFLICT)?

    max_params

    Maximum number of parameters in a single query (None = unlimited)

    json_operations

    Does the adapter support JSON operations?

    array_types

    Does the adapter support array types?

Represents a compiled query ready for adapter execution. The full Query AST is defined in cquill/query, but adapters receive this simplified representation.

pub type CompiledQuery {
  CompiledQuery(
    sql: String,
    params: List(QueryParam),
    expected_columns: Int,
  )
}

Constructors

  • CompiledQuery(
      sql: String,
      params: List(QueryParam),
      expected_columns: Int,
    )

    Arguments

    sql

    The SQL or query string to execute

    params

    Bound parameters in order

    expected_columns

    Expected number of columns in result

Query parameters with type information for proper encoding

pub type QueryParam {
  ParamInt(Int)
  ParamFloat(Float)
  ParamString(String)
  ParamBool(Bool)
  ParamNull
  ParamBytes(BitArray)
  ParamCustom(type_name: String, value: String)
}

Constructors

  • ParamInt(Int)
  • ParamFloat(Float)
  • ParamString(String)
  • ParamBool(Bool)
  • ParamNull
  • ParamBytes(BitArray)
  • ParamCustom(type_name: String, value: String)

    For adapter-specific parameter types

pub type TransactionError(e) =
  error.TransactionError(e)

Values

pub fn capabilities(
  adapter: Adapter(conn, row),
) -> AdapterCapabilities

Get the adapter’s declared capabilities

pub fn default_capabilities() -> AdapterCapabilities

Default capabilities - minimal feature set all adapters support

pub fn format_error(err: error.AdapterError) -> String
pub fn from_mysql_error(
  code: Int,
  message: String,
) -> error.AdapterError
pub fn from_postgres_error(
  code: String,
  message: String,
  detail: String,
) -> error.AdapterError
pub fn from_sqlite_error(
  code: Int,
  message: String,
) -> error.AdapterError
pub fn insert_returning(
  adapter: Adapter(conn, row),
  connection: conn,
  compiled: CompiledQuery,
) -> Result(option.Option(row), error.AdapterError)

Execute an INSERT with RETURNING (returns NotSupported if unavailable)

pub fn is_connection_error(err: error.AdapterError) -> Bool
pub fn is_constraint_violation(err: error.AdapterError) -> Bool
pub fn is_foreign_key_violation(err: error.AdapterError) -> Bool
pub fn is_not_found(err: error.AdapterError) -> Bool
pub fn is_query_error(err: error.AdapterError) -> Bool
pub fn is_recoverable(err: error.AdapterError) -> Bool
pub fn is_unique_violation(err: error.AdapterError) -> Bool
pub fn mutate(
  adapter: Adapter(conn, row),
  connection: conn,
  compiled: CompiledQuery,
) -> Result(Int, error.AdapterError)

Execute a mutation and return affected row count

pub fn mutation_query(
  sql: String,
  params: List(QueryParam),
) -> CompiledQuery

Create a mutation query (INSERT/UPDATE/DELETE)

pub fn name(adapter: Adapter(conn, row)) -> String

Get the adapter’s name

pub fn new(
  name name: String,
  capabilities capabilities: AdapterCapabilities,
  execute_query execute_query: fn(conn, CompiledQuery) -> Result(
    List(row),
    error.AdapterError,
  ),
  execute_mutation execute_mutation: fn(conn, CompiledQuery) -> Result(
    Int,
    error.AdapterError,
  ),
  execute_returning execute_returning: fn(conn, CompiledQuery) -> Result(
    option.Option(row),
    error.AdapterError,
  ),
  begin_transaction begin_transaction: fn(conn) -> Result(
    conn,
    error.AdapterError,
  ),
  commit_transaction commit_transaction: fn(conn) -> Result(
    Nil,
    error.AdapterError,
  ),
  rollback_transaction rollback_transaction: fn(conn) -> Result(
    Nil,
    error.AdapterError,
  ),
) -> Adapter(conn, row)

Create a new adapter with the given operations.

pub fn param_bool(value: Bool) -> QueryParam

Create a boolean parameter

pub fn param_bytes(value: BitArray) -> QueryParam

Create a bytes parameter

pub fn param_custom(
  type_name: String,
  value: String,
) -> QueryParam

Create a custom type parameter

pub fn param_float(value: Float) -> QueryParam

Create a float parameter

pub fn param_int(value: Int) -> QueryParam

Create an integer parameter

pub fn param_null() -> QueryParam

Create a null parameter

pub fn param_string(value: String) -> QueryParam

Create a string parameter

pub fn query(
  adapter: Adapter(conn, row),
  connection: conn,
  compiled: CompiledQuery,
) -> Result(List(row), error.AdapterError)

Execute a query and return all rows

pub fn query_one(
  adapter: Adapter(conn, row),
  connection: conn,
  compiled: CompiledQuery,
) -> Result(row, error.AdapterError)

Execute a query and return exactly one row, or error

pub fn query_optional(
  adapter: Adapter(conn, row),
  connection: conn,
  compiled: CompiledQuery,
) -> Result(option.Option(row), error.AdapterError)

Execute a query and return an optional row (None if not found)

pub fn query_with_columns(
  sql: String,
  params: List(QueryParam),
  expected_columns: Int,
) -> CompiledQuery

Create a query with expected column count

pub fn select_query(
  sql: String,
  params: List(QueryParam),
) -> CompiledQuery

Create a simple SELECT query

pub fn sql_capabilities() -> AdapterCapabilities

Full SQL database capabilities (Postgres, MySQL, etc.)

pub fn supports_arrays(adapter: Adapter(conn, row)) -> Bool

Check if adapter supports array types

pub fn supports_batch_insert(adapter: Adapter(conn, row)) -> Bool

Check if adapter supports batch inserts

pub fn supports_json(adapter: Adapter(conn, row)) -> Bool

Check if adapter supports JSON operations

pub fn supports_returning(adapter: Adapter(conn, row)) -> Bool

Check if adapter supports RETURNING clause

pub fn supports_transactions(adapter: Adapter(conn, row)) -> Bool

Check if adapter supports transactions

pub fn supports_upsert(adapter: Adapter(conn, row)) -> Bool

Check if adapter supports upsert operations

pub fn transaction(
  adapter: Adapter(conn, row),
  connection: conn,
  operation: fn(conn) -> Result(a, e),
) -> Result(a, error.TransactionError(e))

Execute a function within a transaction. Returns UserError if the user’s operation function fails. Returns AdapterTransactionError if the adapter/database fails during the transaction.

pub fn transaction_with_adapter_errors(
  adapter: Adapter(conn, row),
  connection: conn,
  operation: fn(conn) -> Result(a, error.AdapterError),
) -> Result(a, error.TransactionError(Nil))

Execute a function within a transaction with adapter error handling. This version allows the operation to return AdapterError, which is wrapped in AdapterTransactionError on failure instead of UserError.

Search Document