Skip to main content

InsertQuery

Struct InsertQuery 

Source
pub struct InsertQuery {
    pub hints: Hints,
    pub modifiers: Modifiers,
    pub table: TableRef,
    pub values: Values,
    pub set: Set,
    pub row_alias: RowAlias,
    pub duplicate_key_update: Set,
}
Expand description

A MySQL INSERT.

From https://dev.mysql.com/doc/refman/8.4/en/insert.html, with the three row sources folded together:

INSERT [hint_comment] [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    { {VALUES | VALUE} (value_list) [, (value_list)] ...
    | SET assignment_list
    | [(col_name [, col_name] ...)] { SELECT ... | TABLE tbl | VALUES row_list } }
    [AS row_alias[(col_alias [, col_alias] ...)]]
    [ON DUPLICATE KEY UPDATE assignment_list]

INTO is written unconditionally even though the grammar makes it optional: the shorter form saves four characters and costs a reader a double-take.

§Three row sources, one field each

VALUES, a query, and SET are alternatives. Values holds the first two; set is the third, and it wins when both are present, because a half-and-half rendering is not a statement. With none of them, VALUES () is written — MySQL’s spelling of “every column takes its default”, which PostgreSQL spells DEFAULT VALUES.

Choosing SET also drops the insert column list, because the SET production does not have one: INSERT INTO t (a) SET b = 1 is a syntax error, not a statement with a redundant clause. PARTITION is in both productions and stays.

§No WITH

MySQL permits a CTE only immediately before the SELECT of an INSERT … SELECT, never in front of the INSERT (15.2.20). So there is no with field and no insert::with mod; put the WITH on the sub-query given to insert::query.

Fields§

§hints: Hints

/*+ … */.

§modifiers: Modifiers

LOW_PRIORITY / HIGH_PRIORITY / DELAYED, and IGNORE.

§table: TableRef

The target: table, partitions, and the insert column list.

§values: Values

The rows, or the query to insert the results of.

§set: Set

SET a = 1, b = 2, the assignment-list row source.

§row_alias: RowAlias

AS row_alias [(cols)].

§duplicate_key_update: Set

ON DUPLICATE KEY UPDATE ….

Implementations§

Source§

impl InsertQuery

Source

pub fn new() -> InsertQuery

An INSERT with nothing set yet.

Source

pub fn apply(&mut self, mods: impl Mod<InsertQuery>)

Apply more mods to an existing query.

Trait Implementations§

Source§

impl Clone for InsertQuery

Source§

fn clone(&self) -> InsertQuery

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 Debug for InsertQuery

Source§

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

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

impl Default for InsertQuery

Source§

fn default() -> InsertQuery

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

impl Expression for InsertQuery

Source§

fn write_sql(&self, w: &mut SqlWriter<'_>)

Append this fragment to w. Read more
Source§

impl HasDuplicateKeyUpdate for InsertQuery

Source§

fn duplicate_key_update_mut(&mut self) -> &mut Set

The ON DUPLICATE KEY UPDATE assignments.
Source§

impl HasHints for InsertQuery

Source§

fn hints_mut(&mut self) -> &mut Hints

The hints to add to.
Source§

impl HasModifiers for InsertQuery

Source§

fn modifiers_mut(&mut self) -> &mut Modifiers

The modifiers to add to.
Source§

impl HasRowAlias for InsertQuery

Source§

fn row_alias_mut(&mut self) -> &mut RowAlias

The row alias to set.
Source§

impl HasSet for InsertQuery

The INSERT … SET row source, not the ON DUPLICATE KEY UPDATE list — see HasDuplicateKeyUpdate.

Source§

fn set_mut(&mut self) -> &mut Set

The assignment list to modify.
Source§

impl HasTableRef for InsertQuery

Source§

fn table_ref_mut(&mut self) -> &mut TableRef

The table reference to modify.
Source§

impl HasValues for InsertQuery

Source§

fn values_mut(&mut self) -> &mut Values

The row source to modify.
Source§

impl IntoExpr for InsertQuery

Source§

fn into_expr(self) -> Expr

Perform the conversion.
Source§

impl IntoExprList for InsertQuery

Source§

fn into_expr_list(self) -> Vec<Expr>

Perform the conversion.
Source§

impl Query for InsertQuery

Source§

fn query_type(&self) -> QueryType

Which statement this renders.
Source§

fn dialect(&self) -> &dyn Dialect

The dialect this query renders itself in. Read more
Source§

fn build(&self) -> Result<(String, Vec<Value>), Error>

Render to SQL and arguments, numbering placeholders from 1. Read more
Source§

fn build_from(&self, start: usize) -> Result<(String, Vec<Value>), Error>

build with a different first placeholder position, for splicing into a statement that already has arguments — bob’s BuildN.
Source§

impl<H, L, M> QueryExtensions<H, L, M> for InsertQuery

Source§

fn hooks(&self) -> &[Hook]

Hooks to run before this query executes.
Source§

fn loaders(&self) -> &[Loader]

Loaders to run after this query executes, for eagerly loaded relations.
Source§

fn mapper_mods(&self) -> &[MapperMod]

Adjustments to the row mapper, for relations loaded in the same query.

Auto Trait Implementations§

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.