Skip to main content

InsertIfAbsent

Struct InsertIfAbsent 

Source
pub struct InsertIfAbsent<'a> {
    pub table: &'a str,
    pub columns: &'a [&'a str],
    pub values: &'a [String],
    pub key_columns: &'a [&'a str],
}
Expand description

One row, written once — the pieces of an INSERT that must do nothing when a row already carries the same key.

The key value is not a field of its own: it is whichever values entry sits at the same index as the key column in columns. Two fields that must agree are two fields free to disagree, and the one they would disagree about here is the one that decides whether a second write is a duplicate.

Fields§

§table: &'a str

The target table, already spelled the way the caller wants it written — schema-qualified or bare.

§columns: &'a [&'a str]

The columns being written, in the order values supplies them.

§values: &'a [String]

One value expression per column.

§key_columns: &'a [&'a str]

The columns whose equality means this row is already here.

Implementations§

Source§

impl InsertIfAbsent<'_>

Source

pub fn value_of(&self, column: &str) -> Option<&str>

The value expression written into column, if it is being written.

Source

pub fn key_predicate(&self, qualifier: &str) -> String

key = value AND … — the test for already here, in terms of the values this INSERT would have written.

qualifier prefixes the column side, which the guarded form needs so the probe’s own row is named unambiguously.

§Panics

When a key column is not among the columns being written. A key the INSERT does not supply cannot be tested for, and silently dropping it from the predicate would make the guard match everything.

Source

pub fn on_conflict_do_nothing(&self) -> String

ON CONFLICT (key) DO NOTHING — for the platforms that let the engine decide, which is every platform that has the clause.

The engine tests the key as it writes, so two sessions registering one file at the same moment settle on one row rather than on whichever of them looked first.

Source

pub fn on_duplicate_key_no_op(&self) -> String

ON DUPLICATE KEY UPDATE key = key — the MySQL family’s no-op.

INSERT IGNORE would be shorter and it is not the same request: it downgrades every error on the statement to a warning — a truncated value, a bad date — so a row that should have been refused arrives mangled and silent. Assigning the key to itself touches only the collision this is about, and leaves everything else to complain normally.

Source

pub fn guarded_over_one_row(&self) -> String

The guarded form over an explicit one-row source — the same INSERT … SELECT … WHERE NOT EXISTS the portable default writes, with a FROM clause under it.

GoogleSQL refuses a WHERE on a query that selects from nothing — “Query without FROM clause cannot have a WHERE clause”, measured on the live service on every generated register_file procedure — while PostgreSQL and its cousins accept the bare form. (SELECT 1) is a one-row table that changes no result: the guard still decides whether the row is written, and the values are still the caller’s.

It stays a separate form rather than becoming the default because the engines that need no FROM read better without one, and a clause every platform carries for one platform’s sake is how a rendering stops saying what it means.

Trait Implementations§

Source§

impl<'a> Clone for InsertIfAbsent<'a>

Source§

fn clone(&self) -> Self

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<'a> Copy for InsertIfAbsent<'a>

Source§

impl<'a> Debug for InsertIfAbsent<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for InsertIfAbsent<'a>

§

impl<'a> Freeze for InsertIfAbsent<'a>

§

impl<'a> Move for InsertIfAbsent<'a>

§

impl<'a> RefUnwindSafe for InsertIfAbsent<'a>

§

impl<'a> Send for InsertIfAbsent<'a>

§

impl<'a> Sync for InsertIfAbsent<'a>

§

impl<'a> Unpin for InsertIfAbsent<'a>

§

impl<'a> UnsafeUnpin for InsertIfAbsent<'a>

§

impl<'a> UnwindSafe for InsertIfAbsent<'a>

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 = !

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.