use crate::associations::HasTable; #[cfg(any(feature = "sqlite", feature = "mysql"))] use crate::associations::Identifiable; use crate::connection::Connection; #[cfg(any(feature = "sqlite", feature = "mysql"))] use crate::dsl::Find; #[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))] use crate::dsl::Update; #[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))] use crate::expression::{is_aggregate, MixedAggregates, ValidGrouping}; use crate::query_builder::{AsChangeset, IntoUpdateTarget}; #[cfg(any(feature = "sqlite", feature = "mysql"))] use crate::query_dsl::methods::{ExecuteDsl, FindDsl}; #[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))] use crate::query_dsl::{LoadQuery, RunQueryDsl}; use crate::result::QueryResult; #[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))] use crate::Table; /// A trait defining how to update a record and fetch the updated entry /// on a certain backend. /// /// The only case where it is required to work with this trait is while /// implementing a new connection type. /// Otherwise use [`SaveChangesDsl`] /// /// For implementing this trait for a custom backend: /// * The `Changes` generic parameter represents the changeset that should be stored /// * The `Output` generic parameter represents the type of the response. pub trait UpdateAndFetchResults: Connection { /// See the traits documentation. fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult; } #[cfg(feature = "postgres")] use crate::pg::PgConnection; #[cfg(feature = "postgres")] impl<'b, Changes, Output> UpdateAndFetchResults for PgConnection where Changes: Copy + AsChangeset::Table> + IntoUpdateTarget, Update: LoadQuery<'b, PgConnection, Output>, ::AllColumns: ValidGrouping<()>, <::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates, { fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult { crate::update(changeset).set(changeset).get_result(self) } } #[cfg(feature = "sqlite")] use crate::sqlite::SqliteConnection; #[cfg(feature = "sqlite")] impl<'b, Changes, Output> UpdateAndFetchResults for SqliteConnection where Changes: Copy + Identifiable, Changes: AsChangeset::Table> + IntoUpdateTarget, Changes::Table: FindDsl, Update: ExecuteDsl, Find: LoadQuery<'b, SqliteConnection, Output>, ::AllColumns: ValidGrouping<()>, <::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates, { fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult { crate::update(changeset).set(changeset).execute(self)?; Changes::table().find(changeset.id()).get_result(self) } } #[cfg(feature = "mysql")] use crate::mysql::MysqlConnection; #[cfg(feature = "mysql")] impl<'b, Changes, Output> UpdateAndFetchResults for MysqlConnection where Changes: Copy + Identifiable, Changes: AsChangeset::Table> + IntoUpdateTarget, Changes::Table: FindDsl, Update: ExecuteDsl, Find: LoadQuery<'b, MysqlConnection, Output>, ::AllColumns: ValidGrouping<()>, <::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates, { fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult { crate::update(changeset).set(changeset).execute(self)?; Changes::table().find(changeset.id()).get_result(self) } } /// Sugar for types which implement both `AsChangeset` and `Identifiable` /// /// On backends which support the `RETURNING` keyword, /// `foo.save_changes(&conn)` is equivalent to /// `update(&foo).set(&foo).get_result(&conn)`. /// On other backends, two queries will be executed. /// /// # Example /// /// ```rust /// # include!("../doctest_setup.rs"); /// # use schema::animals; /// # /// #[derive(Queryable, Debug, PartialEq)] /// struct Animal { /// id: i32, /// species: String, /// legs: i32, /// name: Option, /// } /// /// #[derive(AsChangeset, Identifiable)] /// #[diesel(table_name = animals)] /// struct AnimalForm<'a> { /// id: i32, /// name: &'a str, /// } /// /// # fn main() { /// # run_test(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::animals::dsl::*; /// # let connection = &mut establish_connection(); /// let form = AnimalForm { /// id: 2, /// name: "Super scary", /// }; /// let changed_animal = form.save_changes(connection)?; /// let expected_animal = Animal { /// id: 2, /// species: String::from("spider"), /// legs: 8, /// name: Some(String::from("Super scary")), /// }; /// assert_eq!(expected_animal, changed_animal); /// # Ok(()) /// # } /// ``` pub trait SaveChangesDsl { /// See the trait documentation. fn save_changes(self, connection: &mut Conn) -> QueryResult where Self: Sized, Conn: UpdateAndFetchResults, { connection.update_and_fetch(self) } } impl SaveChangesDsl for T where T: Copy + AsChangeset::Table> + IntoUpdateTarget { }