Insert

Struct Insert 

Source
pub struct Insert<T> { /* private fields */ }
Expand description

An INSERT statement for adding rows to a table.

See the SQLite docs for details.

#[derive(Clone, PartialEq, Eq, Hash, Debug, Table, Param, ResultRecord)]
struct Food {
    #[nanosql(unique)]
    name: String,
    sugar: u16,
    energy: u32,
}

let inserted_foods = [
    Food { name: "beef".into(), sugar: 0, energy: 250 },
    Food { name: "fish".into(), sugar: 0, energy: 200 },
    Food { name: "chocolate".into(), sugar: 61, energy: 545 },
];
let mut conn = Connection::connect_in_memory()?;
conn.create_table::<Food>()?;

let returned_foods = conn.insert_or_ignore_batch(inserted_foods.clone())?;
assert!(returned_foods.iter().flatten().eq(&inserted_foods));

// convenience query for retrieving the properties of the "fish" record
define_query! {
    GetFish<'lt>: () => Single<Food> {
        r#"
        SELECT name AS name, sugar AS sugar, energy AS energy
        FROM food
        WHERE name = 'fish'
        "#
    }
}
// Try to insert a duplicate using various conflict resolution algorithms,
// applicable when PRIMARY KEY or UNIQUE constraints are violated.

// The default is ABORT: duplicates cause an error, the DB is not modified.
let result = conn.compile_invoke(
    Insert::<Food>::new(),
    Food { name: "fish".into(), sugar: 13, energy: 37 },
);
assert!(matches!(
    result,
    Err(Error::Sqlite(SqliteError::SqliteFailure(
        FfiError {
            code: ErrorCode::ConstraintViolation,
            ..
        },
        _
    )))
));
assert_eq!(
    conn.compile_invoke(GetFish, ())?,
    Single(Food { name: "fish".into(), sugar: 0, energy: 200 }),
);

// IGNORE means no error, but the database is still not modified.
let entity = conn.compile_invoke(
    Insert::<Food>::or_ignore(),
    Food { name: "fish".into(), sugar: 14, energy: 38 },
)?;
assert_eq!(entity, None);
assert_eq!(
    conn.compile_invoke(GetFish, ())?,
    Single(Food { name: "fish".into(), sugar: 0, energy: 200 }),
);

// REPLACE means update the other fields of the conflicting row.
let entity = conn.compile_invoke(
    Insert::<Food>::or_replace(),
    Food { name: "fish".into(), sugar: 15, energy: 39 },
)?;
assert_eq!(
    entity,
    Some(Food { name: "fish".into(), sugar: 15, energy: 39 }),
);
assert_eq!(
    conn.compile_invoke(GetFish, ())?,
    Single(Food { name: "fish".into(), sugar: 15, energy: 39 }),
);

// INSERT OR IGNORE also works via a convenience extension method on `Connection`
let entity = conn.insert_or_ignore_one(Food {
    name: "fish".into(),
    sugar: 16,
    energy: 49,
})?;
assert_eq!(entity, None);
assert_eq!(
    conn.compile_invoke(GetFish, ())?,
    Single(Food { name: "fish".into(), sugar: 15, energy: 39 }),
);

let entity = conn.insert_or_ignore_one(Food {
    name: "plum".into(),
    sugar: 43,
    energy: 129,
})?;
assert_eq!(
    entity,
    Some(Food { name: "plum".into(), sugar: 43, energy: 129 })
);

// replacing also works using a convenience method on connections
let replaced = conn.insert_or_replace_one(Food {
    name: "fish".into(),
    sugar: 17,
    energy: 64,
})?;
assert_eq!(
    replaced,
    Food { name: "fish".into(), sugar: 17, energy: 64 },
);
assert_eq!(
    conn.compile_invoke(GetFish, ())?,
    Single(replaced)
);

Implementations§

Source§

impl<T> Insert<T>

Source

pub const fn new() -> Self

Creates an INSERT statement with the default conflict resolution behavior, which is ABORT.

Source

pub const fn with_behavior(behavior: ConflictResolution) -> Self

Creates an INSERT statement with the specified conflict resolution behavior.

Source

pub const fn or_ignore() -> Self

Creates an INSERT OR IGNORE statement.

Source

pub const fn or_replace() -> Self

Creates an INSERT OR REPLACE statement.

Trait Implementations§

Source§

impl<T> Clone for Insert<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Table> Debug for Insert<T>

Source§

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

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

impl<T> Default for Insert<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Query for Insert<T>
where T: Table + ResultRecord,

Source§

type Output = Option<T>

The optional is Some(_) if the row was inserted or updated, and None if it was ignored.

Source§

fn format_sql(&self, formatter: &mut Formatter<'_>) -> Result

TODO(H2CO3): respect optional/defaulted columns

Source§

type Input<'p> = <T as Table>::InsertInput<'p>

The parameter type of the query. This must be either of the following: Read more
Source§

fn display_sql(&self) -> SqlDisplay<&Self>

Returns a formatter object that displays the SQL text for this query.
Source§

impl<T> Copy for Insert<T>

Auto Trait Implementations§

§

impl<T> Freeze for Insert<T>

§

impl<T> RefUnwindSafe for Insert<T>

§

impl<T> Send for Insert<T>

§

impl<T> Sync for Insert<T>

§

impl<T> Unpin for Insert<T>

§

impl<T> UnwindSafe for Insert<T>

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, 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.