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

-- base
import Prelude

-- opaleye
import qualified Opaleye

-- rel8
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 :: Word -> Query a -> Query a
limit = (Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye ((Select a -> Select a) -> Query a -> Query a)
-> (Word -> Select a -> Select a) -> Word -> Query a -> Query a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Select a -> Select a
forall a. Int -> Select a -> Select a
Opaleye.limit (Int -> Select a -> Select a)
-> (Word -> Int) -> Word -> Select a -> Select a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral


-- | @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 :: Word -> Query a -> Query a
offset = (Select a -> Select a) -> Query a -> Query a
forall a b. (Select a -> Select b) -> Query a -> Query b
mapOpaleye ((Select a -> Select a) -> Query a -> Query a)
-> (Word -> Select a -> Select a) -> Word -> Query a -> Query a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Select a -> Select a
forall a. Int -> Select a -> Select a
Opaleye.offset (Int -> Select a -> Select a)
-> (Word -> Int) -> Word -> Select a -> Select a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral