{-# OPTIONS_HADDOCK not-home #-}

{-# LANGUAGE FlexibleContexts #-}

module Opaleye.Internal.RunQueryExternal
                 (module Opaleye.Internal.RunQueryExternal,
                         -- * Datatypes
                         IRQ.Cursor,
                         IRQ.FromFields,
                         IRQ.FromField,
                         -- * Creating new 'FromField's
                         IRQ.fieldQueryRunnerColumn,
                         IRQ.fieldParserQueryRunnerColumn) where

import           Control.Applicative (pure, (<$>))
import qualified Database.PostgreSQL.Simple as PGS
import qualified Database.PostgreSQL.Simple.Cursor  as PGSC

import qualified Opaleye.Select as S
import           Opaleye.Internal.RunQuery (prepareQuery)
import qualified Opaleye.Internal.RunQuery as IRQ

import qualified Data.Profunctor.Product.Default as D

-- * Running 'S.Select's

runQuery :: D.Default IRQ.FromFields fields haskells
         => PGS.Connection
         -> S.Select fields
         -> IO [haskells]
runQuery :: forall fields haskells.
Default FromFields fields haskells =>
Connection -> Select fields -> IO [haskells]
runQuery = forall fields haskells.
FromFields fields haskells
-> Connection -> Select fields -> IO [haskells]
runQueryExplicit forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def

runQueryFold
  :: D.Default IRQ.FromFields fields haskells
  => PGS.Connection
  -> S.Select fields
  -> b
  -> (b -> haskells -> IO b)
  -> IO b
runQueryFold :: forall fields haskells b.
Default FromFields fields haskells =>
Connection -> Select fields -> b -> (b -> haskells -> IO b) -> IO b
runQueryFold = forall fields haskells b.
FromFields fields haskells
-> Connection
-> Select fields
-> b
-> (b -> haskells -> IO b)
-> IO b
runQueryFoldExplicit forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def

-- * Explicit versions

runQueryExplicit :: IRQ.FromFields fields haskells
                 -> PGS.Connection
                 -> S.Select fields
                 -> IO [haskells]
runQueryExplicit :: forall fields haskells.
FromFields fields haskells
-> Connection -> Select fields -> IO [haskells]
runQueryExplicit FromFields fields haskells
qr Connection
conn Select fields
q = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (m :: * -> *) a. Monad m => a -> m a
return []) (forall r. RowParser r -> Connection -> Query -> IO [r]
PGS.queryWith_ RowParser haskells
parser Connection
conn) Maybe Query
sql
  where (Maybe Query
sql, RowParser haskells
parser) = forall fields haskells.
FromFields fields haskells
-> Select fields -> (Maybe Query, RowParser haskells)
IRQ.prepareQuery FromFields fields haskells
qr Select fields
q

runQueryFoldExplicit
  :: IRQ.FromFields fields haskells
  -> PGS.Connection
  -> S.Select fields
  -> b
  -> (b -> haskells -> IO b)
  -> IO b
runQueryFoldExplicit :: forall fields haskells b.
FromFields fields haskells
-> Connection
-> Select fields
-> b
-> (b -> haskells -> IO b)
-> IO b
runQueryFoldExplicit FromFields fields haskells
qr Connection
conn Select fields
q b
z b -> haskells -> IO b
f = case Maybe Query
sql of
  Maybe Query
Nothing   -> forall (m :: * -> *) a. Monad m => a -> m a
return b
z
  Just Query
sql' -> forall r a.
RowParser r -> Connection -> Query -> a -> (a -> r -> IO a) -> IO a
PGS.foldWith_ RowParser haskells
parser Connection
conn Query
sql' b
z b -> haskells -> IO b
f
  where (Maybe Query
sql, RowParser haskells
parser) = forall fields haskells.
FromFields fields haskells
-> Select fields -> (Maybe Query, RowParser haskells)
prepareQuery FromFields fields haskells
qr Select fields
q

-- * Cursor interface

declareCursor
    :: D.Default IRQ.FromFields fields haskells
    => PGS.Connection
    -> S.Select fields
    -> IO (IRQ.Cursor haskells)
declareCursor :: forall fields haskells.
Default FromFields fields haskells =>
Connection -> Select fields -> IO (Cursor haskells)
declareCursor = forall fields haskells.
FromFields fields haskells
-> Connection -> Select fields -> IO (Cursor haskells)
declareCursorExplicit forall (p :: * -> * -> *) a b. Default p a b => p a b
D.def

declareCursorExplicit
    :: IRQ.FromFields fields haskells
    -> PGS.Connection
    -> S.Select fields
    -> IO (IRQ.Cursor haskells)
declareCursorExplicit :: forall fields haskells.
FromFields fields haskells
-> Connection -> Select fields -> IO (Cursor haskells)
declareCursorExplicit FromFields fields haskells
qr Connection
conn Select fields
q =
    case Maybe Query
mbQuery of
      Maybe Query
Nothing    -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall haskells. Cursor haskells
IRQ.EmptyCursor
      Just Query
query -> forall haskells. RowParser haskells -> Cursor -> Cursor haskells
IRQ.Cursor RowParser haskells
rowParser forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Connection -> Query -> IO Cursor
PGSC.declareCursor Connection
conn Query
query
  where
    (Maybe Query
mbQuery, RowParser haskells
rowParser) = forall fields haskells.
FromFields fields haskells
-> Select fields -> (Maybe Query, RowParser haskells)
prepareQuery FromFields fields haskells
qr Select fields
q

closeCursor :: IRQ.Cursor fields -> IO ()
closeCursor :: forall fields. Cursor fields -> IO ()
closeCursor Cursor fields
IRQ.EmptyCursor       = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
closeCursor (IRQ.Cursor RowParser fields
_ Cursor
cursor) = Cursor -> IO ()
PGSC.closeCursor Cursor
cursor

foldForward
    :: IRQ.Cursor haskells
    -> Int
    -> (a -> haskells -> IO a)
    -> a
    -> IO (Either a a)
foldForward :: forall haskells a.
Cursor haskells
-> Int -> (a -> haskells -> IO a) -> a -> IO (Either a a)
foldForward Cursor haskells
IRQ.EmptyCursor              Int
_chunkSize a -> haskells -> IO a
_f a
z = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left a
z
foldForward (IRQ.Cursor RowParser haskells
rowParser Cursor
cursor) Int
chunkSize  a -> haskells -> IO a
f a
z =
    forall r a.
Cursor
-> RowParser r -> Int -> (a -> r -> IO a) -> a -> IO (Either a a)
PGSC.foldForwardWithParser Cursor
cursor RowParser haskells
rowParser Int
chunkSize a -> haskells -> IO a
f a
z