-
Notifications
You must be signed in to change notification settings - Fork 10
add support for multiple where conditions for update #88
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
Open
Cavdy
wants to merge
2
commits into
tchoutri:main
Choose a base branch
from
Cavdy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ module Database.PostgreSQL.Entity | |
| -- ** Update | ||
| , update | ||
| , updateFieldsBy | ||
| , updateFieldsWhere | ||
|
|
||
| -- ** Deletion | ||
| , delete | ||
|
|
@@ -73,6 +74,7 @@ module Database.PostgreSQL.Entity | |
| , _updateBy | ||
| , _updateFields | ||
| , _updateFieldsBy | ||
| , _updateFieldsWhere | ||
|
|
||
| -- ** Deletion | ||
| , _delete | ||
|
|
@@ -361,6 +363,28 @@ updateFieldsBy | |
| -> DBT m Int64 | ||
| updateFieldsBy fs (f, oldValue) newValue = execute (_updateFieldsBy @e fs f) (toRow newValue ++ toRow (Only oldValue)) | ||
|
|
||
| {-| Update rows of an entity matching the given values | ||
|
|
||
| == Example | ||
|
|
||
| > let newName = "Tiberus McElroy" :: Text | ||
| > let oldName = "Johnson McElroy" :: Text | ||
| > updateFieldsWhere @Author [[field| name |]] [[field| name |]] (newName,oldName) | ||
|
|
||
| @since TODO | ||
| -} | ||
| updateFieldsWhere | ||
| :: forall e values m | ||
| . (Entity e, MonadIO m, ToRow values) | ||
| => Vector Field | ||
| -- ^ Fields to change | ||
| -> Vector Field | ||
| -- ^ Field on which to match and its value | ||
| -> values | ||
| -- ^ Values of those fields | ||
| -> DBT m Int64 | ||
| updateFieldsWhere fs f values = execute (_updateFieldsWhere @e fs f) values | ||
|
|
||
| {-| Delete an entity according to its primary key. | ||
|
|
||
| @since 0.0.1.0 | ||
|
|
@@ -683,25 +707,25 @@ _updateBy f = _updateFieldsBy @e (fields @e) f | |
| _updateFields :: forall e. Entity e => Vector Field -> Query | ||
| _updateFields fs = _updateFieldsBy @e fs (primaryKey @e) | ||
|
|
||
| {-| Produce an UPDATE statement for the given entity and fields, by the specified field. | ||
| {-| Produce an UPDATE statement for the given entity and fields, by the specified fields. | ||
|
|
||
| >>> _updateFieldsBy @Author [ [field| name |] ] [field| name |] | ||
| "UPDATE \"authors\" SET (\"name\") = ROW(?) WHERE \"name\" = ?" | ||
| >>> _updateFieldsWhere @Author [[field| name |]] [[field| author_id |], [field| name |]] | ||
| "UPDATE \"authors\" SET (\"name\") = ROW(?) WHERE \"author_id\" = ? AND \"name\" = ?" | ||
|
|
||
| >>> _updateFieldsBy @BlogPost [[field| author_id |], [field| title |]] [field| title |] | ||
| "UPDATE \"blogposts\" SET (\"author_id\", \"title\") = ROW(?, ?) WHERE \"title\" = ?" | ||
| >>> _updateFieldsWhere @BlogPost [[field| author_id |], [field| title |]] [[field| blogpost_id |], [field| title |]] | ||
| "UPDATE \"blogposts\" SET (\"author_id\", \"title\") = ROW(?, ?) WHERE \"blogpost_id\" = ? AND \"title\" = ?" | ||
|
|
||
| @since 0.0.1.0 | ||
| @since TODO | ||
| -} | ||
| _updateFieldsBy | ||
| _updateFieldsWhere | ||
| :: forall e | ||
| . Entity e | ||
| => Vector Field | ||
| -- ^ Field names to update | ||
| -> Field | ||
| -> Vector Field | ||
| -- ^ Field on which to match | ||
| -> Query | ||
| _updateFieldsBy fs' f = | ||
| _updateFieldsWhere fs' f = | ||
| textToQuery | ||
| ( "UPDATE " | ||
| <> getTableName @e | ||
|
|
@@ -710,14 +734,34 @@ _updateFieldsBy fs' f = | |
| <> " = " | ||
| <> newValues | ||
| ) | ||
| <> _where [f] | ||
| <> _where f | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah but we have a problem here, if |
||
| where | ||
| fs = V.filter (/= (primaryKey @e)) fs' | ||
| newValues = "ROW" <> inParens (generatePlaceholders fs) | ||
| updatedFields = | ||
| inParens $ | ||
| V.foldl1' (\element acc -> element <> ", " <> acc) (quoteName . fieldName <$> fs) | ||
|
|
||
| {-| Produce an UPDATE statement for the given entity and fields, by the specified field. | ||
|
|
||
| >>> _updateFieldsBy @Author [ [field| name |] ] [field| name |] | ||
| "UPDATE \"authors\" SET (\"name\") = ROW(?) WHERE \"name\" = ?" | ||
|
|
||
| >>> _updateFieldsBy @BlogPost [[field| author_id |], [field| title |]] [field| title |] | ||
| "UPDATE \"blogposts\" SET (\"author_id\", \"title\") = ROW(?, ?) WHERE \"title\" = ?" | ||
|
|
||
| @since 0.0.1.0 | ||
| -} | ||
| _updateFieldsBy | ||
| :: forall e | ||
| . Entity e | ||
| => Vector Field | ||
| -- ^ Field names to update | ||
| -> Field | ||
| -- ^ Field on which to match | ||
| -> Query | ||
| _updateFieldsBy fs' = _updateFieldsWhere @e fs' . V.singleton | ||
|
|
||
| {-| Produce a DELETE statement for the given entity, with a match on the Primary Key | ||
|
|
||
| __Examples__ | ||
|
|
||
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
Oops, something went wrong.
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.