1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
use expression::predicates::Or; use query_builder::insert_statement::{IncompleteInsertStatement, Insert}; use super::nodes::Replace; /// Creates a SQLite `INSERT OR REPLACE` statement. If a constraint violation /// fails, SQLite will attempt to replace the offending row instead. /// /// # Example /// /// ```rust /// # #[macro_use] extern crate diesel; /// # #[macro_use] extern crate diesel_codegen; /// # include!("src/doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> VarChar, /// # } /// # } /// # /// # #[derive(Insertable)] /// # #[table_name="users"] /// # struct User<'a> { /// # id: i32, /// # name: &'a str, /// # } /// # /// # /// # fn main() { /// # use self::users::dsl::*; /// # use self::diesel::{insert, insert_or_replace}; /// # use self::diesel::sqlite::SqliteConnection; /// # /// # let conn = SqliteConnection::establish(":memory:").unwrap(); /// # conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR)").unwrap(); /// insert(&NewUser::new("Sean")).into(users).execute(&conn).unwrap(); /// insert(&NewUser::new("Tess")).into(users).execute(&conn).unwrap(); /// /// let new_user = User { id: 1, name: "Jim" }; /// insert_or_replace(&new_user).into(users).execute(&conn).unwrap(); /// /// let names = users.select(name).order(id).load::<String>(&conn); /// assert_eq!(Ok(vec!["Jim".into(), "Tess".into()]), names); /// # } /// ``` pub fn insert_or_replace<T: ?Sized>(records: &T) -> IncompleteInsertStatement<&T, Or<Insert, Replace>> { IncompleteInsertStatement::new(records, Or::new(Insert, Replace)) }