Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages: .
packages: ., ../opaleye/*.cabal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you meant to commit this.

constraints: ansi-wl-pprint < 1.0.0
allow-newer: base16:base, base16:deepseq, base16:text
2 changes: 1 addition & 1 deletion rel8.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ library
, hasql >= 1.6.1.2 && < 1.9
, network-ip ^>= 0.3
, iproute ^>= 1.7
, opaleye ^>= 0.10.2.1
, opaleye ^>= 0.10.5.0
, pretty
, profunctors
, product-profunctors
Expand Down
20 changes: 16 additions & 4 deletions src/Rel8/Query/Limit.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
{-# language LambdaCase #-}

module Rel8.Query.Limit
( limit
, offset
)
where

-- base
import Data.Int (Int64)
import Prelude

-- opaleye
import qualified Opaleye
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye

-- rel8
import Rel8.Expr (Expr)
import Rel8.Expr.Opaleye (toColumn, toPrimExpr)
import Rel8.Query ( Query )
import Rel8.Query.Opaleye ( mapOpaleye )


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


-- | @offset n@ drops the first @n@ rows from a query. @offset n@ is equivalent
-- to the SQL @OFFSET n@.
offset :: Word -> Query a -> Query a
offset = mapOpaleye . Opaleye.offset . fromIntegral
offset :: Expr Int64 -> Query a -> Query a
offset = mapOpaleye . Opaleye.offsetField . toColumn . stripCast . toPrimExpr


stripCast :: Opaleye.PrimExpr -> Opaleye.PrimExpr
stripCast = \case
Opaleye.CastExpr "\"int8\"" a -> a
a -> a
Loading