cquill/typed/mutation

Types

A type-safe DELETE query carrying its target table type as a phantom parameter.

pub opaque type TypedDeleteQuery(table_type)

A type-safe INSERT query carrying its target table type as a phantom parameter. The phantom type ensures only columns from the target table can be used.

pub opaque type TypedInsertQuery(table_type)

A type-safe UPDATE query carrying its target table type as a phantom parameter.

pub opaque type TypedUpdateQuery(table_type)

Values

pub fn delete_all(
  query: TypedDeleteQuery(t),
) -> TypedDeleteQuery(t)

Explicitly mark that you want to delete all rows. This is a safety feature requiring explicit intent for bulk deletes.

Example

delete_from(temp_data)
|> delete_all()  // Explicit bulk delete
pub fn delete_from(table: table.Table(t)) -> TypedDeleteQuery(t)

Start building a DELETE query for a table.

Example

delete_from(users)
|> delete_where(typed_eq(user_id, 42))
pub fn delete_has_where(query: TypedDeleteQuery(t)) -> Bool

Check if the DELETE query has a WHERE clause. Useful for safety checks before executing.

pub fn delete_returning_all(
  query: TypedDeleteQuery(t),
) -> TypedDeleteQuery(t)

Add RETURNING * to get all deleted columns back.

pub fn delete_returning_by_name(
  query: TypedDeleteQuery(t),
  column_names: List(String),
) -> TypedDeleteQuery(t)

Add columns to the RETURNING clause by name.

pub fn delete_returning_column(
  query: TypedDeleteQuery(t),
  column: table.Column(t, a),
) -> TypedDeleteQuery(t)

Add a single column to the RETURNING clause.

pub fn delete_returning_columns(
  query: TypedDeleteQuery(t),
) -> List(String)

Get the returning columns from a DELETE query.

pub fn delete_table_name(query: TypedDeleteQuery(t)) -> String

Get the table name from a DELETE query.

pub fn delete_to_ast(
  query: TypedDeleteQuery(t),
) -> ast.DeleteQuery(Nil)

Extract the underlying AST delete query for adapter use.

pub fn delete_where(
  query: TypedDeleteQuery(t),
  condition: ast.Condition,
) -> TypedDeleteQuery(t)

Add a WHERE condition to the DELETE. Uses raw AST condition for flexibility.

pub fn delete_where_raw(
  query: TypedDeleteQuery(t),
  sql: String,
  params: List(ast.Value),
) -> TypedDeleteQuery(t)

Add a raw WHERE condition (for advanced use cases).

pub fn insert_bool_value(
  query: TypedInsertQuery(t),
  column: table.Column(t, Bool),
  value: Bool,
) -> TypedInsertQuery(t)

Add a boolean value to the INSERT.

pub fn insert_column(
  query: TypedInsertQuery(t),
  column: table.Column(t, a),
) -> TypedInsertQuery(t)

Specify a single column for the INSERT statement. Call multiple times to add more columns.

Example

insert_into(users)
|> insert_column(user_email)
|> insert_column(user_name)
pub fn insert_column_names(
  query: TypedInsertQuery(t),
) -> List(String)

Get the column names from an INSERT query.

pub fn insert_columns_by_name(
  query: TypedInsertQuery(t),
  column_names: List(String),
) -> TypedInsertQuery(t)

Specify columns for the INSERT statement by name. Use this when you need to specify multiple columns at once.

Example

insert_into(users)
|> insert_columns_by_name(["email", "name"])
pub fn insert_float_value(
  query: TypedInsertQuery(t),
  column: table.Column(t, Float),
  value: Float,
) -> TypedInsertQuery(t)

Add a float value to the INSERT.

pub fn insert_has_on_conflict(query: TypedInsertQuery(t)) -> Bool

Check if the INSERT has an ON CONFLICT clause.

pub fn insert_int_value(
  query: TypedInsertQuery(t),
  column: table.Column(t, Int),
  value: Int,
) -> TypedInsertQuery(t)

Add an integer value to the INSERT.

pub fn insert_into(table: table.Table(t)) -> TypedInsertQuery(t)

Start building an INSERT query for a table.

Example

insert_into(users)
|> insert_columns([user_email, user_name])
pub fn insert_null(
  query: TypedInsertQuery(t),
  column: table.Column(t, option.Option(a)),
) -> TypedInsertQuery(t)

Add a NULL value to the INSERT for an optional column.

pub fn insert_returning_all(
  query: TypedInsertQuery(t),
) -> TypedInsertQuery(t)

Add RETURNING * to get all inserted columns back.

pub fn insert_returning_by_name(
  query: TypedInsertQuery(t),
  column_names: List(String),
) -> TypedInsertQuery(t)

Add columns to the RETURNING clause by name.

Example

insert_into(users)
|> ...
|> insert_returning_by_name(["id", "email"])
pub fn insert_returning_column(
  query: TypedInsertQuery(t),
  column: table.Column(t, a),
) -> TypedInsertQuery(t)

Add a single column to the RETURNING clause.

Example

insert_into(users)
|> ...
|> insert_returning_column(user_id)
|> insert_returning_column(user_email)
pub fn insert_returning_columns(
  query: TypedInsertQuery(t),
) -> List(String)

Get the returning columns from an INSERT query.

pub fn insert_row(
  query: TypedInsertQuery(t),
  values: List(ast.Value),
) -> TypedInsertQuery(t)

Add a raw list of values for a single row. Use this when you have pre-encoded values.

pub fn insert_row_count(query: TypedInsertQuery(t)) -> Int

Get the number of rows in an INSERT query.

pub fn insert_rows(
  query: TypedInsertQuery(t),
  rows: List(List(ast.Value)),
) -> TypedInsertQuery(t)

Add multiple rows of values.

pub fn insert_string_value(
  query: TypedInsertQuery(t),
  column: table.Column(t, String),
  value: String,
) -> TypedInsertQuery(t)

Add a single value to the INSERT. The value type must match the column’s value type.

Example

insert_into(users)
|> insert_columns([user_email])
|> insert_string_value(user_email, "test@example.com")
pub fn insert_table_name(query: TypedInsertQuery(t)) -> String

Get the table name from an INSERT query.

pub fn insert_to_ast(
  query: TypedInsertQuery(t),
) -> ast.InsertQuery(Nil)

Extract the underlying AST insert query for adapter use.

pub fn on_conflict_constraint_do_nothing(
  query: TypedInsertQuery(t),
  constraint_name: String,
) -> TypedInsertQuery(t)

Handle conflicts on a named constraint by doing nothing.

pub fn on_conflict_constraint_do_update_by_name(
  query: TypedInsertQuery(t),
  constraint_name: String,
  update_columns: List(String),
) -> TypedInsertQuery(t)

Handle conflicts on a named constraint by updating columns.

pub fn on_conflict_constraint_do_update_single(
  query: TypedInsertQuery(t),
  constraint_name: String,
  update_column: table.Column(t, a),
) -> TypedInsertQuery(t)

Handle conflicts on a named constraint by updating a single column.

pub fn on_conflict_do_nothing(
  query: TypedInsertQuery(t),
) -> TypedInsertQuery(t)

Handle conflicts by doing nothing (skip conflicting rows).

Example

insert_into(users)
|> ...
|> on_conflict_do_nothing()
pub fn on_conflict_do_update_by_name(
  query: TypedInsertQuery(t),
  conflict_columns conflict_names: List(String),
  update_columns update_names: List(String),
) -> TypedInsertQuery(t)

Handle conflicts by updating specified columns (upsert). Uses column names for flexibility with mixed-type columns.

Example

insert_into(users)
|> ...
|> on_conflict_do_update_by_name(
    conflict_columns: ["email"],
    update_columns: ["name", "updated_at"]
  )
pub fn on_conflict_do_update_single(
  query: TypedInsertQuery(t),
  conflict_column: table.Column(t, a),
  update_column: table.Column(t, b),
) -> TypedInsertQuery(t)

Handle conflicts on a single conflict column by updating a single column. Type-safe version for single column conflicts.

pub fn set_bool(
  query: TypedUpdateQuery(t),
  column: table.Column(t, Bool),
  value: Bool,
) -> TypedUpdateQuery(t)

Set a boolean column’s value in the UPDATE.

pub fn set_float(
  query: TypedUpdateQuery(t),
  column: table.Column(t, Float),
  value: Float,
) -> TypedUpdateQuery(t)

Set a float column’s value in the UPDATE.

pub fn set_int(
  query: TypedUpdateQuery(t),
  column: table.Column(t, Int),
  value: Int,
) -> TypedUpdateQuery(t)

Set an integer column’s value in the UPDATE.

pub fn set_null(
  query: TypedUpdateQuery(t),
  column: table.Column(t, option.Option(a)),
) -> TypedUpdateQuery(t)

Set an optional column to NULL.

pub fn set_optional_int(
  query: TypedUpdateQuery(t),
  column: table.Column(t, option.Option(Int)),
  value: option.Option(Int),
) -> TypedUpdateQuery(t)

Set an optional int column’s value (Some or None).

pub fn set_optional_string(
  query: TypedUpdateQuery(t),
  column: table.Column(t, option.Option(String)),
  value: option.Option(String),
) -> TypedUpdateQuery(t)

Set an optional string column’s value (Some or None).

pub fn set_string(
  query: TypedUpdateQuery(t),
  column: table.Column(t, String),
  value: String,
) -> TypedUpdateQuery(t)

Set a string column’s value in the UPDATE. The column must be a String column belonging to the table.

Example

update(users)
|> set_string(user_email, "new@example.com")
pub fn set_where(
  query: TypedUpdateQuery(t),
  condition: ast.Condition,
) -> TypedUpdateQuery(t)

Import and use a typed condition for the WHERE clause. This reuses the TypedCondition type from the query module.

pub fn set_where_raw(
  query: TypedUpdateQuery(t),
  sql: String,
  params: List(ast.Value),
) -> TypedUpdateQuery(t)

Add a raw WHERE condition (for advanced use cases).

pub fn update(table: table.Table(t)) -> TypedUpdateQuery(t)

Start building an UPDATE query for a table.

Example

update(users)
|> set_string(user_email, "new@example.com")
|> set_where(typed_eq(user_id, 42))
pub fn update_has_where(query: TypedUpdateQuery(t)) -> Bool

Check if the UPDATE query has a WHERE clause. Useful for safety checks before executing.

pub fn update_returning_all(
  query: TypedUpdateQuery(t),
) -> TypedUpdateQuery(t)

Add RETURNING * to get all updated columns back.

pub fn update_returning_by_name(
  query: TypedUpdateQuery(t),
  column_names: List(String),
) -> TypedUpdateQuery(t)

Add columns to the RETURNING clause by name.

pub fn update_returning_column(
  query: TypedUpdateQuery(t),
  column: table.Column(t, a),
) -> TypedUpdateQuery(t)

Add a single column to the RETURNING clause.

pub fn update_returning_columns(
  query: TypedUpdateQuery(t),
) -> List(String)

Get the returning columns from an UPDATE query.

pub fn update_set_count(query: TypedUpdateQuery(t)) -> Int

Get the number of SET clauses in the UPDATE.

pub fn update_table_name(query: TypedUpdateQuery(t)) -> String

Get the table name from an UPDATE query.

pub fn update_to_ast(
  query: TypedUpdateQuery(t),
) -> ast.UpdateQuery(Nil)

Extract the underlying AST update query for adapter use.

Search Document