-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement where clauses on upsert for pg #2479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4070575
implement first version of filter target with FilterDsl. Eventually a…
ce7478b
implement where clause with dedicated DecoratableTarget trait
190759d
Fix exports in lib.rs
8a2f76f
Fix failing test again
ropottnik af3fedb
remove unnecessary generic from DecoratableConflictTarget
ropottnik 2014b0b
cleanup export and remove obsolete comment
ropottnik c6d79f9
fix trait bound for backend
ropottnik d9f9fc6
Simplify DecoratableConflictTarget further
ropottnik 5fc9a1d
comment out failing test
ropottnik 9844cb0
add test for repeated target filtering
ropottnik 186123a
apply requested changes on #2479
ropottnik 2f29c77
fix formatting
ropottnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| target | ||
| Cargo.lock | ||
| !diesel_cli/Cargo.lock | ||
| .env | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
diesel/src/query_builder/upsert/on_conflict_target_decorations.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use crate::backend::{Backend, SupportsOnConflictClause, SupportsOnConflictTargetDecorations}; | ||
| use crate::expression::Expression; | ||
| use crate::query_builder::upsert::on_conflict_target::{ConflictTarget, NoConflictTarget}; | ||
| use crate::query_builder::where_clause::{NoWhereClause, WhereAnd, WhereClause}; | ||
| use crate::query_builder::{AstPass, QueryFragment, QueryResult}; | ||
| use crate::sql_types::BoolOrNullableBool; | ||
|
|
||
| pub trait UndecoratedConflictTarget {} | ||
|
|
||
| impl UndecoratedConflictTarget for NoConflictTarget {} | ||
| impl<T> UndecoratedConflictTarget for ConflictTarget<T> {} | ||
|
|
||
| /// Interface to add information to conflict targets. | ||
| /// Designed to be open for further additions to conflict targets like constraints | ||
| pub trait DecoratableTarget<P> { | ||
| /// Output type of filter_target operation | ||
| type FilterOutput; | ||
| /// equivalent to filter of FilterDsl but aimed at conflict targets | ||
| fn filter_target(self, predicate: P) -> Self::FilterOutput; | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct DecoratedConflictTarget<T, U> { | ||
| target: T, | ||
| where_clause: U, | ||
| } | ||
|
|
||
| impl<T, P> DecoratableTarget<P> for T | ||
| where | ||
| P: Expression, | ||
| P::SqlType: BoolOrNullableBool, | ||
| T: UndecoratedConflictTarget, | ||
| { | ||
| type FilterOutput = DecoratedConflictTarget<T, WhereClause<P>>; | ||
ropottnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fn filter_target(self, predicate: P) -> Self::FilterOutput { | ||
| DecoratedConflictTarget { | ||
| target: self, | ||
| where_clause: NoWhereClause.and(predicate), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T, U, P> DecoratableTarget<P> for DecoratedConflictTarget<T, U> | ||
| where | ||
| P: Expression, | ||
| P::SqlType: BoolOrNullableBool, | ||
| U: WhereAnd<P>, | ||
| { | ||
| type FilterOutput = DecoratedConflictTarget<T, <U as WhereAnd<P>>::Output>; | ||
ropottnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fn filter_target(self, predicate: P) -> Self::FilterOutput { | ||
| DecoratedConflictTarget { | ||
| target: self.target, | ||
| where_clause: self.where_clause.and(predicate), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<DB, T, U> QueryFragment<DB> for DecoratedConflictTarget<T, U> | ||
| where | ||
| T: QueryFragment<DB>, | ||
| U: QueryFragment<DB>, | ||
| DB: Backend + SupportsOnConflictClause + SupportsOnConflictTargetDecorations, | ||
| { | ||
| fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> { | ||
| self.target.walk_ast(out.reborrow())?; | ||
| self.where_clause.walk_ast(out.reborrow())?; | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason why this needs to be a separate trait and we cannot reuse
FilterDslhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @weiznich!
My initial thought also was to use
FilterDsl. However, I created a new trait for 2 reasonsWHEREclause on aSELECTorUPDATEstatement which filters the statement as a whole. In contrast theWHEREclause added to a conflict target specifies the on conflict policy. These statements are usually chained together so to me it made sense to indicate that theWHEREclause groups with the conflict target.DecoratableTargetsuch that it easily allows for further specification of the on conflict policy such as adding constraint names as indicated in the PostgreSQL docs on insert.does that make sense to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds reasonable 👍