@@ -41,7 +41,8 @@ module Database.Beam.Query.Combinators
41
41
, QIfCond , QIfElse
42
42
, (<|>.)
43
43
44
- , limit_ , offset_
44
+ , limit_ , limitMaybe_
45
+ , offset_ , offsetMaybe_
45
46
46
47
, as_
47
48
@@ -66,8 +67,8 @@ module Database.Beam.Query.Combinators
66
67
, orderBy_ , asc_ , desc_ , nullsFirst_ , nullsLast_
67
68
) where
68
69
69
- import Database.Beam.Backend.Types
70
70
import Database.Beam.Backend.SQL
71
+ import Database.Beam.Backend.Types
71
72
72
73
import Database.Beam.Query.Internal
73
74
import Database.Beam.Query.Ord
@@ -83,6 +84,7 @@ import Control.Applicative
83
84
import Data.Maybe
84
85
import Data.Proxy
85
86
import Data.Time (LocalTime )
87
+ import Unsafe.Coerce (unsafeCoerce )
86
88
87
89
import GHC.TypeLits (TypeError , ErrorMessage (Text ))
88
90
@@ -335,6 +337,17 @@ limit_ :: forall s a be db
335
337
limit_ limit' (Q q) =
336
338
Q (liftF (QLimit limit' q (rewriteThread (Proxy @ s ))))
337
339
340
+ -- | Conditionally limit the number of results returned by a query.
341
+ limitMaybe_ :: forall s a be db
342
+ . ( Projectible be a
343
+ , ThreadRewritable (QNested s ) a )
344
+ => Maybe Integer -> Q be db (QNested s ) a -> Q be db s (WithRewrittenThread (QNested s ) s a )
345
+ limitMaybe_ (Just limit') (Q q) =
346
+ Q (liftF (QLimit limit' q (rewriteThread (Proxy @ s ))))
347
+ -- This uses unsafeCoerce, but should be safe since this function is tested.
348
+ -- See discussion on https://github.com/haskell-beam/beam/pull/633.
349
+ limitMaybe_ Nothing (Q q) = Q (unsafeCoerce q)
350
+
338
351
-- | Drop the first `offset'` results.
339
352
offset_ :: forall s a be db
340
353
. ( Projectible be a
@@ -343,6 +356,17 @@ offset_ :: forall s a be db
343
356
offset_ offset' (Q q) =
344
357
Q (liftF (QOffset offset' q (rewriteThread (Proxy @ s ))))
345
358
359
+ -- | Conditionally drop the first `offset'` results.
360
+ offsetMaybe_ :: forall s a be db
361
+ . ( Projectible be a
362
+ , ThreadRewritable (QNested s ) a )
363
+ => Maybe Integer -> Q be db (QNested s ) a -> Q be db s (WithRewrittenThread (QNested s ) s a )
364
+ offsetMaybe_ (Just offset') (Q q) =
365
+ Q (liftF (QOffset offset' q (rewriteThread (Proxy @ s ))))
366
+ -- This uses unsafeCoerce, but should be safe since this function is tested.
367
+ -- See discussion on https://github.com/haskell-beam/beam/pull/633.
368
+ offsetMaybe_ Nothing (Q q) = Q (unsafeCoerce q)
369
+
346
370
-- | Use the SQL @EXISTS@ operator to determine if the given query returns any results
347
371
exists_ :: ( BeamSqlBackend be , HasQBuilder be , Projectible be a )
348
372
=> Q be db s a -> QExpr be s Bool
0 commit comments