|
| 1 | +use crate::backend::{Backend, SupportsOnConflictClause, SupportsOnConflictTargetDecorations}; |
| 2 | +use crate::expression::Expression; |
| 3 | +use crate::query_builder::upsert::on_conflict_target::{ConflictTarget, NoConflictTarget}; |
| 4 | +use crate::query_builder::where_clause::{NoWhereClause, WhereAnd, WhereClause}; |
| 5 | +use crate::query_builder::{AstPass, QueryFragment, QueryResult}; |
| 6 | +use crate::sql_types::BoolOrNullableBool; |
| 7 | + |
| 8 | +pub trait UndecoratedConflictTarget {} |
| 9 | + |
| 10 | +impl UndecoratedConflictTarget for NoConflictTarget {} |
| 11 | +impl<T> UndecoratedConflictTarget for ConflictTarget<T> {} |
| 12 | + |
| 13 | +/// Interface to add information to conflict targets. |
| 14 | +/// Designed to be open for further additions to conflict targets like constraints |
| 15 | +pub trait DecoratableTarget<P> { |
| 16 | + /// Output type of filter_target operation |
| 17 | + type FilterOutput; |
| 18 | + /// equivalent to filter of FilterDsl but aimed at conflict targets |
| 19 | + fn filter_target(self, predicate: P) -> Self::FilterOutput; |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug)] |
| 23 | +pub struct DecoratedConflictTarget<T, U> { |
| 24 | + target: T, |
| 25 | + where_clause: U, |
| 26 | +} |
| 27 | + |
| 28 | +impl<T, P> DecoratableTarget<P> for T |
| 29 | +where |
| 30 | + P: Expression, |
| 31 | + P::SqlType: BoolOrNullableBool, |
| 32 | + T: UndecoratedConflictTarget, |
| 33 | +{ |
| 34 | + type FilterOutput = DecoratedConflictTarget<T, WhereClause<P>>; |
| 35 | + |
| 36 | + fn filter_target(self, predicate: P) -> Self::FilterOutput { |
| 37 | + DecoratedConflictTarget { |
| 38 | + target: self, |
| 39 | + where_clause: NoWhereClause.and(predicate), |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<T, U, P> DecoratableTarget<P> for DecoratedConflictTarget<T, U> |
| 45 | +where |
| 46 | + P: Expression, |
| 47 | + P::SqlType: BoolOrNullableBool, |
| 48 | + U: WhereAnd<P>, |
| 49 | +{ |
| 50 | + type FilterOutput = DecoratedConflictTarget<T, <U as WhereAnd<P>>::Output>; |
| 51 | + |
| 52 | + fn filter_target(self, predicate: P) -> Self::FilterOutput { |
| 53 | + DecoratedConflictTarget { |
| 54 | + target: self.target, |
| 55 | + where_clause: self.where_clause.and(predicate), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<DB, T, U> QueryFragment<DB> for DecoratedConflictTarget<T, U> |
| 61 | +where |
| 62 | + T: QueryFragment<DB>, |
| 63 | + U: QueryFragment<DB>, |
| 64 | + DB: Backend + SupportsOnConflictClause + SupportsOnConflictTargetDecorations, |
| 65 | +{ |
| 66 | + fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> { |
| 67 | + self.target.walk_ast(out.reborrow())?; |
| 68 | + self.where_clause.walk_ast(out.reborrow())?; |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | +} |
0 commit comments