pub struct Insert<M> { /* private fields */ }Expand description
A typed insert statement for model M.
Insert represents one or more records to be created. Generated
create-builders (e.g., User::create()) produce Insert values under the
hood.
The type parameter M is the returning type — exec() produces the
newly created M instance(s). For inserts, the returning type always
matches the model being inserted.
Field values are set with set. Collection fields can be
extended with insert and insert_all.
Implementations§
Source§impl<M> Insert<M>where
M: Model,
impl<M> Insert<M>where
M: Model,
Sourcepub fn blank_single() -> Insert<M>
pub fn blank_single() -> Insert<M>
Create an insert statement with a single blank record.
Fields marked #[auto] are set to Expr::Default
(the database generates the value). All other fields are initialized to
NULL. The caller must fill in required fields with set
before executing; the blank record is not guaranteed to be valid on its
own.
§Examples
use toasty::stmt::Insert;
let mut insert = Insert::<User>::blank_single();
// Fill in the required fields
insert.set(0, toasty_core::stmt::Value::from(1_i64));
insert.set(1, toasty_core::stmt::Value::from("Alice"));Sourcepub const fn from_untyped(untyped: Insert) -> Insert<M>
pub const fn from_untyped(untyped: Insert) -> Insert<M>
Wrap a raw untyped stmt::Insert.
§Examples
use toasty::stmt::Insert;
use toasty::schema::Model;
// Construct from a raw untyped insert
let raw = toasty_core::stmt::Insert {
target: toasty_core::stmt::InsertTarget::Model(
<User as Model>::id(),
),
source: toasty_core::stmt::Query::unit(),
returning: None,
};
let _typed = Insert::<User>::from_untyped(raw);Sourcepub fn set_scope<S>(&mut self, scope: S)where
S: IntoScope<M>,
pub fn set_scope<S>(&mut self, scope: S)where
S: IntoScope<M>,
Set the scope of the insert.
§Examples
use toasty::stmt::Insert;
let mut insert = Insert::<User>::blank_single();
// Scope the insert to all users (used by association inserts)
insert.set_scope(User::all());Sourcepub fn set(&mut self, field: usize, expr: impl Into<Expr>)
pub fn set(&mut self, field: usize, expr: impl Into<Expr>)
Set the value of the field at field index in the current record.
§Examples
use toasty::stmt::Insert;
let mut insert = Insert::<User>::blank_single();
insert.set(0, toasty_core::stmt::Value::from(1_i64));
insert.set(1, toasty_core::stmt::Value::from("Alice"));Sourcepub fn insert(&mut self, field: usize, expr: impl Into<Expr>)
pub fn insert(&mut self, field: usize, expr: impl Into<Expr>)
Append a single value to the list field at field index.
If the field is currently NULL, it is replaced with a new single-element
list. If it is already a list, expr is appended.
§Examples
use toasty::stmt::Insert;
let mut insert = Insert::<User>::blank_single();
// Append a value to a list field (field index 1 for illustration)
insert.insert(1, toasty_core::stmt::Value::from("tag1"));
insert.insert(1, toasty_core::stmt::Value::from("tag2"));Sourcepub fn insert_all(&mut self, field: usize, expr: impl Into<Expr>)
pub fn insert_all(&mut self, field: usize, expr: impl Into<Expr>)
Merge a list expression into the list field at field index,
extending any existing list.
If the field is currently NULL, it is replaced with expr. If both
are lists, the items from expr are appended to the existing list.
§Examples
use toasty::stmt::Insert;
let mut insert = Insert::<User>::blank_single();
let list = toasty_core::stmt::Expr::list([
toasty_core::stmt::Expr::Value(toasty_core::stmt::Value::from("a")),
toasty_core::stmt::Expr::Value(toasty_core::stmt::Value::from("b")),
]);
insert.insert_all(1, list);Sourcepub fn into_list_expr(self) -> Expr<List<M>>
pub fn into_list_expr(self) -> Expr<List<M>>
Convert this insert into a list expression.
The resulting Expr<List<M>> wraps the insert as a sub-statement,
which can be used as the right-hand side of an association
insert call or embedded in other expressions.
§Examples
use toasty::stmt::{Insert, Expr, List};
let insert = Insert::<User>::blank_single();
let _expr: Expr<List<User>> = insert.into_list_expr();