sea_query::query

Struct OnConflict

source
pub struct OnConflict { /* private fields */ }

Implementations§

source§

impl OnConflict

source

pub fn new() -> Self

Create a ON CONFLICT expression without target column, a special method designed for MySQL

source

pub fn column<C>(column: C) -> Self
where C: IntoIden,

Set ON CONFLICT target column

source

pub fn columns<I, C>(columns: I) -> Self
where C: IntoIden, I: IntoIterator<Item = C>,

Set ON CONFLICT target columns

source

pub fn expr<T>(&mut self, expr: T) -> &mut Self
where T: Into<SimpleExpr>,

Set ON CONFLICT target expression

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic(["abcd".into(), 3.1415.into()])
    .on_conflict(
        OnConflict::new()
            .expr(Expr::col(Glyph::Id))
            .update_column(Glyph::Aspect)
            .value(Glyph::Image, Expr::val(1).add(2))
            .to_owned(),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    [
        r#"INSERT INTO `glyph` (`aspect`, `image`)"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON DUPLICATE KEY UPDATE `aspect` = VALUES(`aspect`), `image` = 1 + 2"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = 1 + 2"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = 1 + 2"#,
    ]
    .join(" ")
);
source

pub fn exprs<I, T>(&mut self, exprs: I) -> &mut Self
where T: Into<SimpleExpr>, I: IntoIterator<Item = T>,

Set multiple target expressions for ON CONFLICT. See OnConflict::expr

source

pub fn do_nothing(&mut self) -> &mut Self

Set ON CONFLICT do nothing.

Please use Self::do_nothing_on() and provide primary keys if you are using MySQL.

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic(["abcd".into(), 3.1415.into()])
    .on_conflict(
        OnConflict::columns([Glyph::Id, Glyph::Aspect])
            .do_nothing()
            .to_owned(),
    )
    .to_owned();

// Sadly this is not valid today.
assert_eq!(
    query.to_string(MysqlQueryBuilder),
    [
        r#"INSERT INTO `glyph` (`aspect`, `image`)"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON DUPLICATE KEY IGNORE"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
    ]
    .join(" ")
);
source

pub fn do_nothing_on<C, I>(&mut self, pk_cols: I) -> &mut Self
where C: IntoIden, I: IntoIterator<Item = C>,

Set ON CONFLICT do nothing, but with MySQL specific polyfill.

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic(["abcd".into(), 3.1415.into()])
    .on_conflict(
        OnConflict::columns([Glyph::Id, Glyph::Aspect])
            .do_nothing_on([Glyph::Id])
            .to_owned(),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    [
        r#"INSERT INTO `glyph` (`aspect`, `image`)"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON DUPLICATE KEY UPDATE `id` = `id`"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
    ]
    .join(" ")
);
source

pub fn update_column<C>(&mut self, column: C) -> &mut Self
where C: IntoIden,

Set ON CONFLICT update column

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic([
        "abcd".into(),
        3.1415.into(),
    ])
    .on_conflict(
        OnConflict::columns([Glyph::Id, Glyph::Aspect])
            .update_column(Glyph::Aspect)
            .value(Glyph::Image, Expr::val(1).add(2))
            .to_owned()
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    [
        r#"INSERT INTO `glyph` (`aspect`, `image`)"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON DUPLICATE KEY UPDATE `aspect` = VALUES(`aspect`), `image` = 1 + 2"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id", "aspect") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = 1 + 2"#,
    ]
    .join(" ")
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    [
        r#"INSERT INTO "glyph" ("aspect", "image")"#,
        r#"VALUES ('abcd', 3.1415)"#,
        r#"ON CONFLICT ("id", "aspect") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = 1 + 2"#,
    ]
    .join(" ")
);
source

pub fn update_columns<C, I>(&mut self, columns: I) -> &mut Self
where C: IntoIden, I: IntoIterator<Item = C>,

Set ON CONFLICT update columns

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic([
        2.into(),
        3.into(),
    ])
    .on_conflict(
        OnConflict::column(Glyph::Id)
            .update_columns([Glyph::Aspect, Glyph::Image])
            .to_owned(),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `aspect` = VALUES(`aspect`), `image` = VALUES(`image`)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = "excluded"."image""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = "excluded"."image""#
);
source

pub fn values<C, I>(&mut self, values: I) -> &mut Self
where C: IntoIden, I: IntoIterator<Item = (C, SimpleExpr)>,

Set ON CONFLICT update exprs

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic([
        2.into(),
        3.into(),
    ])
    .on_conflict(
        OnConflict::column(Glyph::Id)
            .value(Glyph::Image, Expr::val(1).add(2))
            .to_owned()
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `image` = 1 + 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2"#
);
source

pub fn value<C, T>(&mut self, col: C, value: T) -> &mut Self
where C: IntoIden, T: Into<SimpleExpr>,

Set ON CONFLICT update value

source

pub fn target_and_where(&mut self, other: SimpleExpr) -> &mut Self

Set target WHERE

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic([
        2.into(),
        3.into(),
    ])
    .on_conflict(
        OnConflict::column(Glyph::Id)
            .value(Glyph::Image, Expr::val(1).add(2))
            .target_and_where(Expr::col((Glyph::Table, Glyph::Aspect)).is_null())
            .to_owned()
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `image` = 1 + 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") WHERE "glyph"."aspect" IS NULL DO UPDATE SET "image" = 1 + 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") WHERE "glyph"."aspect" IS NULL DO UPDATE SET "image" = 1 + 2"#
);
source

pub fn target_and_where_option( &mut self, other: Option<SimpleExpr>, ) -> &mut Self

Set target WHERE

source

pub fn target_cond_where<C>(&mut self, condition: C) -> &mut Self
where C: IntoCondition,

Set target WHERE

source

pub fn action_and_where(&mut self, other: SimpleExpr) -> &mut Self

Set action WHERE

§Examples
use sea_query::{tests_cfg::*, *};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Aspect, Glyph::Image])
    .values_panic([
        2.into(),
        3.into(),
    ])
    .on_conflict(
        OnConflict::column(Glyph::Id)
            .value(Glyph::Image, Expr::val(1).add(2))
            .action_and_where(Expr::col((Glyph::Table, Glyph::Aspect)).is_null())
            .to_owned()
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `image` = 1 + 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2 WHERE "glyph"."aspect" IS NULL"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2 WHERE "glyph"."aspect" IS NULL"#
);
source

pub fn action_and_where_option( &mut self, other: Option<SimpleExpr>, ) -> &mut Self

Set action WHERE

source

pub fn action_cond_where<C>(&mut self, condition: C) -> &mut Self
where C: IntoCondition,

Set action WHERE

Trait Implementations§

source§

impl Clone for OnConflict

source§

fn clone(&self) -> OnConflict

Returns a copy 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 Debug for OnConflict

source§

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

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

impl Default for OnConflict

source§

fn default() -> OnConflict

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

impl PartialEq for OnConflict

source§

fn eq(&self, other: &OnConflict) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for OnConflict

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.