Skip to main content

Module insert

Module insert 

Source
Expand description

Mods for sqlite::insert.

use keelson_sqlite as sqlite;
use keelson_sqlite::{arg, insert};

let q = sqlite::insert((
    insert::into("users").columns(["id", "name"]),
    insert::values((arg(1i32), arg("ada"))),
    insert::on_conflict("id").do_update(insert::set_excluded(["name"])),
    insert::returning("*"),
));

§The mods that are not for the INSERT itself

set, set_col, set_excluded and where_ apply to a ConflictClause, not to an InsertQuery — an INSERT has no SET and no WHERE. They are here because this is where they are used: inside on_conflict(..).do_update(..). An InsertQuery does not implement the traits they need, so misplacing one is a compile error rather than a surprise.

The two WHEREs of an upsert are easy to conflate and behave nothing alike: on_conflict(..).where_(..) is the index predicate, matched against a partial unique index’s own definition, while where_ inside do_update filters which conflicting rows are updated.

§Several upserts

on_conflict appends. SQLite 3.35 and later try each ON CONFLICT clause in turn, and only the last may omit its conflict target — so on_conflict("id").do_update(..) followed by on_conflict(()).do_nothing() is a statement, and the reverse order is not.

Re-exports§

pub use crate::shared::into_table as into;
pub use crate::shared::on_conflict;
pub use crate::shared::or_abort;
pub use crate::shared::or_fail;
pub use crate::shared::or_ignore;
pub use crate::shared::or_replace;
pub use crate::shared::or_rollback;
pub use crate::shared::recursive;
pub use crate::shared::returning;
pub use crate::shared::rows;
pub use crate::shared::set;
pub use crate::shared::set_col;
pub use crate::shared::set_excluded;
pub use crate::shared::values;
pub use crate::shared::values_from_query as query;
pub use crate::shared::where_;
pub use crate::shared::with;