use crate::backend::DieselReserveSpecialization; use crate::expression::grouped::Grouped; use crate::expression::operators::Eq; use crate::expression::AppearsOnTable; use crate::query_builder::*; use crate::query_source::{Column, QuerySource}; use crate::Table; /// Types which can be passed to /// [`update.set`](UpdateStatement::set()). /// /// This trait can be [derived](derive@AsChangeset) pub trait AsChangeset { /// The table which `Self::Changeset` will be updating type Target: QuerySource; /// The update statement this type represents type Changeset; /// Convert `self` into the actual update statement being executed // This method is part of our public API // we won't change it to just appease clippy #[allow(clippy::wrong_self_convention)] fn as_changeset(self) -> Self::Changeset; } // This is a false positive, we reexport it later #[allow(unreachable_pub)] #[doc(inline)] pub use diesel_derives::AsChangeset; impl AsChangeset for Option { type Target = T::Target; type Changeset = Option; fn as_changeset(self) -> Self::Changeset { self.map(AsChangeset::as_changeset) } } impl<'update, T> AsChangeset for &'update Option where &'update T: AsChangeset, { type Target = <&'update T as AsChangeset>::Target; type Changeset = Option<<&'update T as AsChangeset>::Changeset>; fn as_changeset(self) -> Self::Changeset { self.as_ref().map(AsChangeset::as_changeset) } } impl AsChangeset for Eq where Left: AssignmentTarget, Right: AppearsOnTable, { type Target = Left::Table; type Changeset = Assign<::QueryAstNode, Right>; fn as_changeset(self) -> Self::Changeset { Assign { target: self.left.into_target(), expr: self.right, } } } impl AsChangeset for Grouped> where Eq: AsChangeset, { type Target = as AsChangeset>::Target; type Changeset = as AsChangeset>::Changeset; fn as_changeset(self) -> Self::Changeset { self.0.as_changeset() } } #[derive(Debug, Clone, Copy, QueryId)] pub struct Assign { target: Target, expr: Expr, } impl QueryFragment for Assign where DB: Backend, T: QueryFragment, U: QueryFragment, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { QueryFragment::walk_ast(&self.target, out.reborrow())?; out.push_sql(" = "); QueryFragment::walk_ast(&self.expr, out.reborrow()) } } /// Represents the left hand side of an assignment expression for an /// assignment in [AsChangeset]. The vast majority of the time, this will /// be a [Column]. However, in certain database backends, it's possible to /// assign to an expression. For example, in Postgres, it's possible to /// "UPDATE TABLE SET array_column\[1\] = 'foo'". pub trait AssignmentTarget { /// Table the assignment is to type Table: Table; /// A wrapper around a type to assign to (this wrapper should implement /// [QueryFragment]). type QueryAstNode; /// Move this in to the AST node which should implement [QueryFragment]. fn into_target(self) -> Self::QueryAstNode; } /// Represents a `Column` as an `AssignmentTarget`. The vast majority of /// targets in an update statement will be `Column`s. #[derive(Debug, Clone, Copy)] pub struct ColumnWrapperForUpdate(pub C); impl QueryFragment for ColumnWrapperForUpdate where DB: Backend + DieselReserveSpecialization, C: Column, { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> { out.push_identifier(C::NAME) } } impl AssignmentTarget for C where C: Column, { type Table = C::Table; type QueryAstNode = ColumnWrapperForUpdate; fn into_target(self) -> Self::QueryAstNode { ColumnWrapperForUpdate(self) } }