support on conflict in insert query

This commit is contained in:
Liber Wang 2022-06-10 21:18:53 +08:00
parent 1ea8b457bf
commit 41ce3e7817

View File

@ -3,7 +3,7 @@ use crate::{
PrimaryKeyTrait, QueryTrait, PrimaryKeyTrait, QueryTrait,
}; };
use core::marker::PhantomData; use core::marker::PhantomData;
use sea_query::{Alias, Expr, InsertStatement, ValueTuple}; use sea_query::{Alias, Expr, InsertStatement, OnConflict, ValueTuple};
/// Performs INSERT operations on a ActiveModel /// Performs INSERT operations on a ActiveModel
#[derive(Debug)] #[derive(Debug)]
@ -160,6 +160,54 @@ where
} }
self self
} }
/// On conflict
///
/// on conflict do nothing
/// ```
/// use sea_orm::{entity::*, query::*, sea_query::OnConflict, tests_cfg::cake, DbBackend};
///
/// let orange = cake::ActiveModel {
/// id: ActiveValue::set(2),
/// name: ActiveValue::set("Orange".to_owned()),
/// };
/// assert_eq!(
/// cake::Entity::insert(orange)
/// .on_conflict(
/// OnConflict::column(cake::Column::Name)
/// .do_nothing()
/// .to_owned()
/// )
/// .build(DbBackend::Postgres)
/// .to_string(),
/// r#"INSERT INTO "cake" ("id", "name") VALUES (2, 'Orange') ON CONFLICT ("name") DO NOTHING"#,
/// );
/// ```
///
/// on conflict do update
/// ```
/// use sea_orm::{entity::*, query::*, sea_query::OnConflict, tests_cfg::cake, DbBackend};
///
/// let orange = cake::ActiveModel {
/// id: ActiveValue::set(2),
/// name: ActiveValue::set("Orange".to_owned()),
/// };
/// assert_eq!(
/// cake::Entity::insert(orange)
/// .on_conflict(
/// OnConflict::column(cake::Column::Name)
/// .update_column(cake::Column::Name)
/// .to_owned()
/// )
/// .build(DbBackend::Postgres)
/// .to_string(),
/// r#"INSERT INTO "cake" ("id", "name") VALUES (2, 'Orange') ON CONFLICT ("name") DO UPDATE SET "name" = "excluded"."name""#,
/// );
/// ```
pub fn on_conflict(mut self, on_conflict: OnConflict) -> Self {
self.query.on_conflict(on_conflict);
self
}
} }
impl<A> QueryTrait for Insert<A> impl<A> QueryTrait for Insert<A>
@ -183,8 +231,10 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use sea_query::OnConflict;
use crate::tests_cfg::cake; use crate::tests_cfg::cake;
use crate::{ActiveValue, DbBackend, Insert, QueryTrait}; use crate::{ActiveValue, DbBackend, EntityTrait, Insert, QueryTrait};
#[test] #[test]
fn insert_1() { fn insert_1() {
@ -267,4 +317,44 @@ mod tests {
r#"INSERT INTO "cake" ("id", "name") VALUES (NULL, 'Apple'), (2, 'Orange')"#, r#"INSERT INTO "cake" ("id", "name") VALUES (NULL, 'Apple'), (2, 'Orange')"#,
); );
} }
#[test]
fn insert_6() {
let orange = cake::ActiveModel {
id: ActiveValue::set(2),
name: ActiveValue::set("Orange".to_owned()),
};
assert_eq!(
cake::Entity::insert(orange)
.on_conflict(
OnConflict::column(cake::Column::Name)
.do_nothing()
.to_owned()
)
.build(DbBackend::Postgres)
.to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (2, 'Orange') ON CONFLICT ("name") DO NOTHING"#,
);
}
#[test]
fn insert_7() {
let orange = cake::ActiveModel {
id: ActiveValue::set(2),
name: ActiveValue::set("Orange".to_owned()),
};
assert_eq!(
cake::Entity::insert(orange)
.on_conflict(
OnConflict::column(cake::Column::Name)
.update_column(cake::Column::Name)
.to_owned()
)
.build(DbBackend::Postgres)
.to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (2, 'Orange') ON CONFLICT ("name") DO UPDATE SET "name" = "excluded"."name""#,
);
}
} }