cake/insert
A DSL to build INSERT queries.
Types
pub type InsertColumns =
@internal InsertColumns
pub type InsertConflictStrategy(a) =
@internal InsertConflictStrategy(a)
pub type InsertIntoTable =
@internal InsertIntoTable
pub type InsertSource(a) =
@internal InsertSource(a)
pub type InsertValue =
@internal InsertValue
pub type WriteQuery(a) =
@internal WriteQuery(a)
Values
pub fn bool(value value: Bool) -> InsertValue
Create an InsertValue from a column String and a Bool value.
pub fn columns(
insert insert: Insert(a),
columns columns: List(String),
) -> Insert(a)
Specify the columns to insert into.
NOTICE: You have to specify the columns and ensure their names are
correct, as well as their count which must be equal to the count of
InsertRows the encoder function returns or is given as source
values.
pub fn comment(
insert insert: Insert(a),
comment comment: String,
) -> Insert(a)
Specify a comment for the INSERT query.
pub fn date(date value: calendar.Date) -> InsertValue
Create an InsertValue from a calendar.Date.
pub fn epilog(
insert insert: Insert(a),
epilog epilog: String,
) -> Insert(a)
Specify an epilog for the INSERT query.
pub fn float(value value: Float) -> InsertValue
Create an InsertValue from a column String and a Float value.
pub fn fragment(value value: fragment.Fragment) -> InsertValue
Create an InsertValue from a Fragment.
Example
import cake/fragment as f
import cake/insert as i
i.fragment(f.prepared("$::uuid", [f.string("0000000000-0000-4000-a000-a00000000000")]))
pub fn from_records(
table_name table_name: String,
columns columns: List(String),
records records: List(a),
encoder encoder: fn(a) -> InsertRow,
) -> Insert(a)
Create an INSERT query from a list of gleam records.
The encoder function is used to convert each record into an InsertRow.
pub fn from_values(
table_name table_name: String,
columns columns: List(String),
values values: List(InsertRow),
) -> Insert(a)
Create an INSERT query from a list of InsertRows.
pub fn get_columns(insert insert: Insert(a)) -> InsertColumns
Get the columns to insert into from an Insert query.
pub fn get_modifier(insert insert: Insert(a)) -> String
Get the modifier from an Insert query.
pub fn get_on_conflict(
insert insert: Insert(a),
) -> InsertConflictStrategy(a)
Get the conflict strategy from an Insert query.
pub fn get_source(insert insert: Insert(a)) -> InsertSource(a)
Get the source from an Insert query which is either a list of records,
accompanied by an encoder function or a list of InsertRows.
pub fn get_table(insert insert: Insert(a)) -> InsertIntoTable
Get the table name to insert into from an Insert query.
pub fn int(value value: Int) -> InsertValue
Create an InsertValue from a column String and an Int value.
pub fn modifier(
insert insert: Insert(a),
modifier modifier: String,
) -> Insert(a)
Specify a modifier for the INSERT query.
pub fn no_comment(insert insert: Insert(a)) -> Insert(a)
Specify that no comment should be added to the INSERT query.
pub fn no_epilog(insert insert: Insert(a)) -> Insert(a)
Specify that no epilog should be added to the INSERT query.
pub fn no_modifier(insert insert: Insert(a)) -> Insert(a)
Specify that no modifier should be used for the given INSERT query.
pub fn no_returning(insert insert: Insert(a)) -> Insert(a)
Specify that no columns should be returned after the INSERT query.
pub fn on_columns_conflict_ignore(
insert insert: Insert(a),
columns columns: List(String),
where where: Where,
) -> Insert(a)
This specifies that specific conflicts do not result in an error but instead are just ignored and not inserted.
Conflict Target: Columns
pub fn on_columns_conflict_update(
insert insert: Insert(a),
columns columns: List(String),
where where: Where,
update update: Update(a),
) -> Insert(a)
Inserts or updates on conflict, also called ´UPSERT´.
This function generates PostgreSQL/SQLite-specific upsert queries using
ON CONFLICT ... DO UPDATE syntax.
For MySQL/MariaDB, use on_duplicate_key_update() instead.
Conflict Target: Columns
Important
- Use
excluded.columnin UPDATE expressions to reference insert values - Explicitly specify which columns to check for conflicts
- Supports optional WHERE clause for conditional updates
Database Support
- 🐘PostgreSQL: ✅ Fully supported
- 🪶SQLite: ✅ Fully supported
- 🦭MariaDB: ❌ Not supported - use
on_duplicate_key_update() - 🐬MySQL: ❌ Not supported - use
on_duplicate_key_update()
pub fn on_conflict_error(insert insert: Insert(a)) -> Insert(a)
This specifies that any conflicts result in the query to fail
This is the default behaviour.
pub fn on_constraint_conflict_ignore(
insert insert: Insert(a),
constraint constraint: String,
where where: Where,
) -> Insert(a)
This specifies that specific conflicts do not result in an error but instead are just ignored and not inserted.
Conflict Target: Constraint
pub fn on_constraint_conflict_update(
insert insert: Insert(a),
constraint constraint: String,
where where: Where,
update update: Update(a),
) -> Insert(a)
Inserts or updates on conflict, also called ´UPSERT´.
This function generates PostgreSQL-specific upsert queries using
ON CONFLICT ON CONSTRAINT ... DO UPDATE syntax.
For MySQL/MariaDB, use on_duplicate_key_update() instead.
For SQLite, use on_columns_conflict_update() instead.
Conflict Target: Named Constraint
Database Support
- 🐘PostgreSQL: ✅ Fully supported
- 🪶SQLite: ❌ Not supported - use
on_columns_conflict_update() - 🦭MariaDB: ❌ Not supported - use
on_duplicate_key_update() - 🐬MySQL: ❌ Not supported - use
on_duplicate_key_update()
pub fn on_duplicate_key_update(
insert insert: Insert(a),
update update: Update(a),
) -> Insert(a)
MySQL/MariaDB upsert using ON DUPLICATE KEY UPDATE syntax.
This function generates MySQL/MariaDB-specific upsert queries. Only use this when targeting MySQL or MariaDB databases.
For PostgreSQL/SQLite, use on_columns_conflict_update() or
on_constraint_conflict_update() instead.
Important
- Use
VALUES(column)(notexcluded.column) in your UPDATE expressions - Updates occur on the first matched unique/primary key constraint
- No explicit conflict target - relies on existing indexes
Example
import cake/insert as i
import cake/update as u
[[i.string("user1"), i.int(100)] |> i.row]
|> i.from_values(table_name: "scores", columns: ["username", "score"])
|> i.on_duplicate_key_update(
update: u.new()
|> u.set("score" |> u.set_expression("VALUES(score) + scores.score")),
)
Generates:
INSERT INTO scores (username, score) VALUES (?, ?)
ON DUPLICATE KEY UPDATE score = VALUES(score) + scores.score
Database Support
- 🦭MariaDB: ✅ Supported
- 🐬MySQL: ✅ Supported
- 🐘PostgreSQL: ❌ Not supported - use
on_columns_conflict_update() - 🪶SQLite: ❌ Not supported - use
on_columns_conflict_update()
pub fn returning(
insert insert: Insert(a),
returning returning: List(String),
) -> Insert(a)
Specify the columns to return after the INSERT query.
pub fn row(values values: List(InsertValue)) -> InsertRow
Create an InsertRow from a list of InsertValues.
pub fn source_records(
insert insert: Insert(a),
source records: List(a),
encoder encoder: fn(a) -> InsertRow,
) -> Insert(a)
Specify the source records to insert.
pub fn source_values(
insert insert: Insert(a),
source rows: List(InsertRow),
) -> Insert(a)
Specify the source values to insert.
pub fn string(value value: String) -> InsertValue
Create an InsertValue from a column String and a String value.
pub fn table(
insert insert: Insert(a),
table_name table_name: String,
) -> Insert(a)
Specify the table to insert into.
pub fn to_query(insert insert: Insert(a)) -> WriteQuery(a)
Creates a WriteQuery from an Insert query.