pub struct OnConflict { /* private fields */ }Implementations§
Source§impl OnConflict
impl OnConflict
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a ON CONFLICT expression without target column, a special method designed for MySQL
Sourcepub fn columns<I, C>(columns: I) -> Selfwhere
C: IntoIden,
I: IntoIterator<Item = C>,
pub fn columns<I, C>(columns: I) -> Selfwhere
C: IntoIden,
I: IntoIterator<Item = C>,
Set ON CONFLICT target columns
Sourcepub fn constraint(constraint: &str) -> Self
pub fn constraint(constraint: &str) -> Self
Set ON CONSTRAINT target constraint name
Sourcepub fn expr<T>(&mut self, expr: T) -> &mut Self
pub fn expr<T>(&mut self, expr: T) -> &mut Self
Set ON CONFLICT target expression
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic(["abcd".into(), 3.1415.into()])
.on_conflict(
OnConflict::new()
.expr(Expr::col("id"))
.update_column("aspect")
.value("image", Expr::val(1).add(2))
.to_owned(),
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
[
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).unwrap(),
[
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).unwrap(),
[
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(" ")
);Sourcepub fn exprs<I, T>(&mut self, exprs: I) -> &mut Self
pub fn exprs<I, T>(&mut self, exprs: I) -> &mut Self
Set multiple target expressions for ON CONFLICT. See OnConflict::expr
Sourcepub fn do_nothing(&mut self) -> &mut Self
pub fn do_nothing(&mut self) -> &mut Self
Set ON CONFLICT do nothing.
MySQL has no such clause and refuses to render it: use
Self::do_nothing_on() and name the primary key columns there.
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic(["abcd".into(), 3.1415.into()])
.on_conflict(
OnConflict::columns(["id", "aspect"])
.do_nothing()
.to_owned(),
)
.to_owned();
assert!(query.to_string(MysqlQueryBuilder).is_err());
assert_eq!(
query.to_string(PostgresQueryBuilder).unwrap(),
[
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).unwrap(),
[
r#"INSERT INTO "glyph" ("aspect", "image")"#,
r#"VALUES ('abcd', 3.1415)"#,
r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
]
.join(" ")
);Sourcepub fn do_nothing_on<C, I>(&mut self, pk_cols: I) -> &mut Selfwhere
C: IntoIden,
I: IntoIterator<Item = C>,
pub fn do_nothing_on<C, I>(&mut self, pk_cols: I) -> &mut Selfwhere
C: IntoIden,
I: IntoIterator<Item = C>,
Set ON CONFLICT do nothing, but with MySQL specific polyfill.
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic(["abcd".into(), 3.1415.into()])
.on_conflict(
OnConflict::columns(["id", "aspect"])
.do_nothing_on(["id"])
.to_owned(),
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
[
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).unwrap(),
[
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).unwrap(),
[
r#"INSERT INTO "glyph" ("aspect", "image")"#,
r#"VALUES ('abcd', 3.1415)"#,
r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
]
.join(" ")
);Sourcepub fn update_column<C>(&mut self, column: C) -> &mut Selfwhere
C: IntoIden,
pub fn update_column<C>(&mut self, column: C) -> &mut Selfwhere
C: IntoIden,
Set ON CONFLICT update column
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic([
"abcd".into(),
3.1415.into(),
])
.on_conflict(
OnConflict::columns(["id", "aspect"])
.update_column("aspect")
.value("image", Expr::val(1).add(2))
.to_owned()
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
[
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).unwrap(),
[
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).unwrap(),
[
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(" ")
);Sourcepub fn update_columns<C, I>(&mut self, columns: I) -> &mut Selfwhere
C: IntoIden,
I: IntoIterator<Item = C>,
pub fn update_columns<C, I>(&mut self, columns: I) -> &mut Selfwhere
C: IntoIden,
I: IntoIterator<Item = C>,
Set ON CONFLICT update columns
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic([
2.into(),
3.into(),
])
.on_conflict(
OnConflict::column("id")
.update_columns(["aspect", "image"])
.to_owned(),
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
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).unwrap(),
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).unwrap(),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "aspect" = "excluded"."aspect", "image" = "excluded"."image""#
);Sourcepub fn values<C, I>(&mut self, values: I) -> &mut Self
pub fn values<C, I>(&mut self, values: I) -> &mut Self
Set ON CONFLICT update exprs. Append to current list of expressions.
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic([
2.into(),
3.into(),
])
.on_conflict(
OnConflict::column("id")
.values([("image", Expr::val(1).add(2))])
.to_owned()
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `image` = 1 + 2"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder).unwrap(),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder).unwrap(),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2"#
);Sourcepub fn target_and_where(&mut self, other: Expr) -> &mut Self
pub fn target_and_where(&mut self, other: Expr) -> &mut Self
Set target WHERE
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic([
2.into(),
3.into(),
])
.on_conflict(
OnConflict::column("id")
.value("image", Expr::val(1).add(2))
.target_and_where(Expr::col(("glyph", "aspect")).is_null())
.to_owned()
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, 3) ON DUPLICATE KEY UPDATE `image` = 1 + 2"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder).unwrap(),
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).unwrap(),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") WHERE "glyph"."aspect" IS NULL DO UPDATE SET "image" = 1 + 2"#
);Sourcepub fn target_and_where_option(&mut self, other: Option<Expr>) -> &mut Self
pub fn target_and_where_option(&mut self, other: Option<Expr>) -> &mut Self
Set target WHERE
Sourcepub fn target_cond_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
pub fn target_cond_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
Set target WHERE
Sourcepub fn action_and_where(&mut self, other: Expr) -> &mut Self
pub fn action_and_where(&mut self, other: Expr) -> &mut Self
Set action WHERE
MySQL’s ON DUPLICATE KEY UPDATE takes no condition, so it refuses to
render one.
§Examples
use sql_builder::*;
let query = Query::insert()
.into_table("glyph")
.columns(["aspect", "image"])
.values_panic([
2.into(),
3.into(),
])
.on_conflict(
OnConflict::column("id")
.value("image", Expr::val(1).add(2))
.action_and_where(Expr::col(("glyph", "aspect")).is_null())
.to_owned()
)
.to_owned();
assert!(query.to_string(MysqlQueryBuilder).is_err());
assert_eq!(
query.to_string(PostgresQueryBuilder).unwrap(),
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).unwrap(),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, 3) ON CONFLICT ("id") DO UPDATE SET "image" = 1 + 2 WHERE "glyph"."aspect" IS NULL"#
);Sourcepub fn action_and_where_option(&mut self, other: Option<Expr>) -> &mut Self
pub fn action_and_where_option(&mut self, other: Option<Expr>) -> &mut Self
Set action WHERE
Sourcepub fn action_cond_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
pub fn action_cond_where<C>(&mut self, condition: C) -> &mut Selfwhere
C: IntoCondition,
Set action WHERE