Skip to content

Commit 6bedb7b

Browse files
Dynamic limit and offset
1 parent 5c715e6 commit 6bedb7b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
packages: .
1+
packages: ., ../opaleye/*.cabal
22
constraints: ansi-wl-pprint < 1.0.0
33
allow-newer: base16:base, base16:deepseq, base16:text

src/Rel8/Query/Limit.hs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
1+
{-# language LambdaCase #-}
2+
13
module Rel8.Query.Limit
24
( limit
35
, offset
46
)
57
where
68

79
-- base
10+
import Data.Int (Int64)
811
import Prelude
912

1013
-- opaleye
1114
import qualified Opaleye
15+
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
1216

1317
-- rel8
18+
import Rel8.Expr (Expr)
19+
import Rel8.Expr.Opaleye (toColumn, toPrimExpr)
1420
import Rel8.Query ( Query )
1521
import Rel8.Query.Opaleye ( mapOpaleye )
1622

1723

1824
-- | @limit n@ select at most @n@ rows from a query. @limit n@ is equivalent
1925
-- to the SQL @LIMIT n@.
20-
limit :: Word -> Query a -> Query a
21-
limit = mapOpaleye . Opaleye.limit . fromIntegral
26+
limit :: Expr Int64 -> Query a -> Query a
27+
limit = mapOpaleye . Opaleye.limitField . toColumn . stripCast . toPrimExpr
2228

2329

2430
-- | @offset n@ drops the first @n@ rows from a query. @offset n@ is equivalent
2531
-- to the SQL @OFFSET n@.
26-
offset :: Word -> Query a -> Query a
27-
offset = mapOpaleye . Opaleye.offset . fromIntegral
32+
offset :: Expr Int64 -> Query a -> Query a
33+
offset = mapOpaleye . Opaleye.offsetField . toColumn . stripCast . toPrimExpr
34+
35+
36+
stripCast :: Opaleye.PrimExpr -> Opaleye.PrimExpr
37+
stripCast = \case
38+
Opaleye.CastExpr "\"int8\"" a -> a
39+
a -> a

0 commit comments

Comments
 (0)