Skip to main content

Insert

Struct Insert 

Source
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,

Source

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"));
Source

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);
Source

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());
Source

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"));
Source

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"));
Source

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);
Source

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();

Trait Implementations§

Source§

impl<M> Clone for Insert<M>

Source§

fn clone(&self) -> Insert<M>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M> Debug for Insert<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<M> From<Insert<M>> for Statement<M>

Source§

fn from(value: Insert<M>) -> Statement<M>

Converts to this type from the input type.
Source§

impl<T> From<Insert<T>> for Expr<T>

Source§

fn from(value: Insert<T>) -> Expr<T>

Converts to this type from the input type.
Source§

impl<T> From<Insert<T>> for Expr<Option<T>>

Source§

fn from(value: Insert<T>) -> Expr<Option<T>>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<M> Freeze for Insert<M>
where PhantomData<M>: Freeze,

§

impl<M> RefUnwindSafe for Insert<M>

§

impl<M> Send for Insert<M>
where PhantomData<M>: Send,

§

impl<M> Sync for Insert<M>
where PhantomData<M>: Sync,

§

impl<M> Unpin for Insert<M>
where PhantomData<M>: Unpin,

§

impl<M> UnsafeUnpin for Insert<M>

§

impl<M> UnwindSafe for Insert<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more