-
Notifications
You must be signed in to change notification settings - Fork 171
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
Add limitMaybe_, offsetMaybe_ + tests #633
Open
thomasjm
wants to merge
2
commits into
haskell-beam:master
Choose a base branch
from
thomasjm:limit-offset-maybe
base: master
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.
+57
−3
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 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 |
---|---|---|
|
@@ -41,7 +41,8 @@ module Database.Beam.Query.Combinators | |
, QIfCond, QIfElse | ||
, (<|>.) | ||
|
||
, limit_, offset_ | ||
, limit_, limitMaybe_ | ||
, offset_, offsetMaybe_ | ||
|
||
, as_ | ||
|
||
|
@@ -66,8 +67,8 @@ module Database.Beam.Query.Combinators | |
, orderBy_, asc_, desc_, nullsFirst_, nullsLast_ | ||
) where | ||
|
||
import Database.Beam.Backend.Types | ||
import Database.Beam.Backend.SQL | ||
import Database.Beam.Backend.Types | ||
|
||
import Database.Beam.Query.Internal | ||
import Database.Beam.Query.Ord | ||
|
@@ -83,6 +84,7 @@ import Control.Applicative | |
import Data.Maybe | ||
import Data.Proxy | ||
import Data.Time (LocalTime) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
import GHC.TypeLits (TypeError, ErrorMessage(Text)) | ||
|
||
|
@@ -335,6 +337,15 @@ limit_ :: forall s a be db | |
limit_ limit' (Q q) = | ||
Q (liftF (QLimit limit' q (rewriteThread (Proxy @s)))) | ||
|
||
-- | Conditionally limit the number of results returned by a query. | ||
limitMaybe_ :: forall s a be db | ||
. ( Projectible be a | ||
, ThreadRewritable (QNested s) a ) | ||
=> Maybe Integer -> Q be db (QNested s) a -> Q be db s (WithRewrittenThread (QNested s) s a) | ||
limitMaybe_ (Just limit') (Q q) = | ||
Q (liftF (QLimit limit' q (rewriteThread (Proxy @s)))) | ||
limitMaybe_ Nothing (Q q) = Q (unsafeCoerce q) | ||
|
||
-- | Drop the first `offset'` results. | ||
offset_ :: forall s a be db | ||
. ( Projectible be a | ||
|
@@ -343,6 +354,15 @@ offset_ :: forall s a be db | |
offset_ offset' (Q q) = | ||
Q (liftF (QOffset offset' q (rewriteThread (Proxy @s)))) | ||
|
||
-- | Conditionally drop the first `offset'` results. | ||
offsetMaybe_ :: forall s a be db | ||
. ( Projectible be a | ||
, ThreadRewritable (QNested s) a ) | ||
=> Maybe Integer -> Q be db (QNested s) a -> Q be db s (WithRewrittenThread (QNested s) s a) | ||
offsetMaybe_ (Just offset') (Q q) = | ||
Q (liftF (QOffset offset' q (rewriteThread (Proxy @s)))) | ||
offsetMaybe_ Nothing (Q q) = Q (unsafeCoerce q) | ||
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. Same here. |
||
|
||
-- | Use the SQL @EXISTS@ operator to determine if the given query returns any results | ||
exists_ :: ( BeamSqlBackend be, HasQBuilder be, Projectible be a) | ||
=> Q be db s a -> QExpr be s Bool | ||
|
This file contains 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.
I think you should be able to use
rewriteThread
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.
Sorry, could you give a hint on how that would look? I haven't grokked this stuff yet...
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.
@tathougies I'm going through old PRs in and was blocked on this question -- could you clarify?
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.
Yeah, so the type doesn't match I believe because
(QNested s)
doesnt unify with
WithRewrittenThread (QNested s) s. The latter unwraps one level of
QNested`, which is what the rewriteThread function is fro. I believe if you do.rewriteThread (Proxy @s) q
instead of theunsafeCoerce q
, that should work.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.
I tried some stuff like that and wasn't able to get it to work:
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.
@tathougies pinging again, would love to get this merged
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.
@tathougies another ping, please help! or maybe we could just merge as-is?
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.
@tathougies here is the ping for 2024 :)