{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UndecidableInstances #-}
module Database.Beam.Backend.SQL
  ( module Database.Beam.Backend.SQL.Row
  , module Database.Beam.Backend.SQL.SQL2003
  , module Database.Beam.Backend.SQL.Types

  , MonadBeam(..)

  , BeamSqlBackend
  , BeamSqlBackendSyntax
  , MockSqlBackend

  , BeamSqlBackendIsString

  , BeamSql99ExpressionBackend
  , BeamSql99AggregationBackend
  , BeamSql99ConcatExpressionBackend
  , BeamSql99CommonTableExpressionBackend
  , BeamSql99RecursiveCTEBackend
  , BeamSql2003ExpressionBackend

  , BeamSqlT021Backend
  , BeamSqlT071Backend
  , BeamSqlT611Backend
  , BeamSqlT612Backend
  , BeamSqlT614Backend
  , BeamSqlT615Backend
  , BeamSqlT616Backend
  , BeamSqlT618Backend
  , BeamSqlT621Backend
  , BeamSql99DataTypeBackend

  , BeamSqlBackendSupportsOuterJoin

  , BeamSqlBackendSelectSyntax
  , BeamSqlBackendInsertSyntax
  , BeamSqlBackendInsertValuesSyntax
  , BeamSqlBackendUpdateSyntax
  , BeamSqlBackendDeleteSyntax
  , BeamSqlBackendCastTargetSyntax
  , BeamSqlBackendSelectTableSyntax
  , BeamSqlBackendAggregationQuantifierSyntax
  , BeamSqlBackendSetQuantifierSyntax
  , BeamSqlBackendFromSyntax
  , BeamSqlBackendTableNameSyntax

  , BeamSqlBackendExpressionSyntax
  , BeamSqlBackendDataTypeSyntax
  , BeamSqlBackendFieldNameSyntax
  , BeamSqlBackendExpressionQuantifierSyntax
  , BeamSqlBackendValueSyntax
  , BeamSqlBackendOrderingSyntax
  , BeamSqlBackendGroupingSyntax

  , BeamSqlBackendWindowFrameSyntax
  , BeamSqlBackendWindowFrameBoundsSyntax
  , BeamSqlBackendWindowFrameBoundSyntax

  , BeamSql99BackendCTESyntax

  , BeamSqlBackendCanSerialize
  , BeamSqlBackendCanDeserialize
  , BeamSqlBackendSupportsDataType
  ) where

import           Database.Beam.Backend.SQL.SQL2003
import           Database.Beam.Backend.SQL.Row
import           Database.Beam.Backend.SQL.Types
import           Database.Beam.Backend.Types

import           Control.Monad.Cont
import           Control.Monad.Except
import qualified Control.Monad.RWS.Lazy as Lazy
import qualified Control.Monad.RWS.Strict as Strict
import           Control.Monad.Reader
import qualified Control.Monad.State.Lazy as Lazy
import qualified Control.Monad.Writer.Lazy as Lazy
import qualified Control.Monad.State.Strict as Strict
import qualified Control.Monad.Writer.Strict as Strict

import           Data.Kind (Type)
import           Data.Tagged (Tagged)
import           Data.Text (Text)

-- * MonadBeam class

-- | A class that ties together a monad with a particular backend
--
--   Provided here is a low-level interface for executing commands. The 'run*'
--   functions are wrapped by the appropriate functions in 'Database.Beam.Query'.
--
--   This interface is very high-level and isn't meant to expose the full power
--   of the underlying database. Namely, it only supports simple data retrieval
--   strategies. More complicated strategies (for example, Postgres's @COPY@)
--   are supported in individual backends. See the documentation of those
--   backends for more details.
class (BeamBackend be, Monad m) =>
  MonadBeam be m | m -> be where
  {-# MINIMAL runReturningMany #-}

  -- | Run a query determined by the given syntax, providing an action that will
  --   be called to consume the results from the database (if any). The action
  --   will get a reader action that can be used to fetch the next row. When
  --   this reader action returns 'Nothing', there are no rows left to consume.
  --   When the reader action returns, the database result is freed.
  runReturningMany :: FromBackendRow be x
                   => BeamSqlBackendSyntax be
                      -- ^ The query to run
                   -> (m (Maybe x) -> m a)
                       -- ^ Reader action that will be called with a function to
                       -- fetch the next row
                   -> m a

  -- | Run the given command and don't consume any results. Useful for DML
  --   statements like INSERT, UPDATE, and DELETE, or DDL statements.
  runNoReturn :: BeamSqlBackendSyntax be -> m ()
  runNoReturn BeamSqlBackendSyntax be
cmd =
      forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
cmd forall a b. (a -> b) -> a -> b
$ \(m (Maybe ())
_ :: m (Maybe ())) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

  -- | Run the given command and fetch the unique result. The result is
  --   'Nothing' if either no results are returned or more than one result is
  --   returned.
  runReturningOne :: FromBackendRow be x => BeamSqlBackendSyntax be -> m (Maybe x)
  runReturningOne BeamSqlBackendSyntax be
cmd =
      forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
cmd forall a b. (a -> b) -> a -> b
$ \m (Maybe x)
next ->
        do Maybe x
a <- m (Maybe x)
next
           case Maybe x
a of
             Maybe x
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
             Just x
x -> do
               Maybe x
a' <- m (Maybe x)
next
               case Maybe x
a' of
                 Maybe x
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Maybe a
Just x
x)
                 Just x
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing

  -- | Run the given command and fetch the first result. The result is
  --   'Nothing' if no results are returned.
  --   This is not guaranteed to automatically limit the query to one result.
  runReturningFirst :: FromBackendRow be x => BeamSqlBackendSyntax be -> m (Maybe x)
  runReturningFirst BeamSqlBackendSyntax be
cmd = forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
cmd forall a. a -> a
id

  -- | Run the given command, collect all the results, and return them as a
  --   list. May be more convenient than 'runReturningMany', but reads the entire
  --   result set into memory.
  runReturningList :: FromBackendRow be x => BeamSqlBackendSyntax be -> m [x]
  runReturningList BeamSqlBackendSyntax be
cmd =
      forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
cmd forall a b. (a -> b) -> a -> b
$ \m (Maybe x)
next ->
          let collectM :: ([x] -> [x]) -> m [x]
collectM [x] -> [x]
acc = do
                Maybe x
a <- m (Maybe x)
next
                case Maybe x
a of
                  Maybe x
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([x] -> [x]
acc [])
                  Just x
x -> ([x] -> [x]) -> m [x]
collectM ([x] -> [x]
acc forall b c a. (b -> c) -> (a -> b) -> a -> c
. (x
xforall a. a -> [a] -> [a]
:))
          in ([x] -> [x]) -> m [x]
collectM forall a. a -> a
id

instance MonadBeam be m => MonadBeam be (ExceptT e m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (ExceptT e m (Maybe x) -> ExceptT e m a) -> ExceptT e m a
runReturningMany BeamSqlBackendSyntax be
s ExceptT e m (Maybe x) -> ExceptT e m a
a = forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT forall a b. (a -> b) -> a -> b
$ forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT e m (Maybe x) -> ExceptT e m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)))
    runNoReturn :: BeamSqlBackendSyntax be -> ExceptT e m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> ExceptT e m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> ExceptT e m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance MonadBeam be m => MonadBeam be (ContT r m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (ContT r m (Maybe x) -> ContT r m a) -> ContT r m a
runReturningMany BeamSqlBackendSyntax be
s ContT r m (Maybe x) -> ContT r m a
a = forall {k} (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT forall a b. (a -> b) -> a -> b
$ \a -> m r
r ->
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall {k} (r :: k) (m :: k -> *) a.
ContT r m a -> (a -> m r) -> m r
runContT (ContT r m (Maybe x) -> ContT r m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)) a -> m r
r)
    runNoReturn :: BeamSqlBackendSyntax be -> ContT r m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> ContT r m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> ContT r m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance MonadBeam be m => MonadBeam be (ReaderT r m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (ReaderT r m (Maybe x) -> ReaderT r m a) -> ReaderT r m a
runReturningMany BeamSqlBackendSyntax be
s ReaderT r m (Maybe x) -> ReaderT r m a
a = forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \r
r ->
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (ReaderT r m (Maybe x) -> ReaderT r m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)) r
r)
    runNoReturn :: BeamSqlBackendSyntax be -> ReaderT r m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> ReaderT r m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> ReaderT r m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance MonadBeam be m => MonadBeam be (Lazy.StateT s m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (StateT s m (Maybe x) -> StateT s m a) -> StateT s m a
runReturningMany BeamSqlBackendSyntax be
s StateT s m (Maybe x) -> StateT s m a
a = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Lazy.StateT forall a b. (a -> b) -> a -> b
$ \s
st ->
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
Lazy.runStateT (StateT s m (Maybe x) -> StateT s m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)) s
st)
    runNoReturn :: BeamSqlBackendSyntax be -> StateT s m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> StateT s m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> StateT s m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance MonadBeam be m => MonadBeam be (Strict.StateT s m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (StateT s m (Maybe x) -> StateT s m a) -> StateT s m a
runReturningMany BeamSqlBackendSyntax be
s StateT s m (Maybe x) -> StateT s m a
a = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Strict.StateT forall a b. (a -> b) -> a -> b
$ \s
st ->
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
Strict.runStateT (StateT s m (Maybe x) -> StateT s m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)) s
st)
    runNoReturn :: BeamSqlBackendSyntax be -> StateT s m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> StateT s m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> StateT s m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance (MonadBeam be m, Monoid s) => MonadBeam be (Lazy.WriterT s m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (WriterT s m (Maybe x) -> WriterT s m a) -> WriterT s m a
runReturningMany BeamSqlBackendSyntax be
s WriterT s m (Maybe x) -> WriterT s m a
a = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Lazy.WriterT forall a b. (a -> b) -> a -> b
$
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Lazy.runWriterT (WriterT s m (Maybe x) -> WriterT s m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)))
    runNoReturn :: BeamSqlBackendSyntax be -> WriterT s m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> WriterT s m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> WriterT s m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance (MonadBeam be m, Monoid s) => MonadBeam be (Strict.WriterT s m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (WriterT s m (Maybe x) -> WriterT s m a) -> WriterT s m a
runReturningMany BeamSqlBackendSyntax be
s WriterT s m (Maybe x) -> WriterT s m a
a = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Strict.WriterT forall a b. (a -> b) -> a -> b
$
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Strict.runWriterT (WriterT s m (Maybe x) -> WriterT s m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)))
    runNoReturn :: BeamSqlBackendSyntax be -> WriterT s m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> WriterT s m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> WriterT s m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance (MonadBeam be m, Monoid w) => MonadBeam be (Lazy.RWST r w s m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (RWST r w s m (Maybe x) -> RWST r w s m a) -> RWST r w s m a
runReturningMany BeamSqlBackendSyntax be
s RWST r w s m (Maybe x) -> RWST r w s m a
a = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Lazy.RWST forall a b. (a -> b) -> a -> b
$ \r
r s
st ->
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
Lazy.runRWST (RWST r w s m (Maybe x) -> RWST r w s m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)) r
r s
st)
    runNoReturn :: BeamSqlBackendSyntax be -> RWST r w s m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> RWST r w s m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> RWST r w s m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

instance (MonadBeam be m, Monoid w) => MonadBeam be (Strict.RWST r w s m) where
    runReturningMany :: forall x a.
FromBackendRow be x =>
BeamSqlBackendSyntax be
-> (RWST r w s m (Maybe x) -> RWST r w s m a) -> RWST r w s m a
runReturningMany BeamSqlBackendSyntax be
s RWST r w s m (Maybe x) -> RWST r w s m a
a = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST forall a b. (a -> b) -> a -> b
$ \r
r s
st ->
                           forall be (m :: * -> *) x a.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> (m (Maybe x) -> m a) -> m a
runReturningMany BeamSqlBackendSyntax be
s (\m (Maybe x)
nextRow -> forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
Strict.runRWST (RWST r w s m (Maybe x) -> RWST r w s m a
a (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe x)
nextRow)) r
r s
st)
    runNoReturn :: BeamSqlBackendSyntax be -> RWST r w s m ()
runNoReturn = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *).
MonadBeam be m =>
BeamSqlBackendSyntax be -> m ()
runNoReturn
    runReturningOne :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> RWST r w s m (Maybe x)
runReturningOne = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m (Maybe x)
runReturningOne
    runReturningList :: forall x.
FromBackendRow be x =>
BeamSqlBackendSyntax be -> RWST r w s m [x]
runReturningList = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be (m :: * -> *) x.
(MonadBeam be m, FromBackendRow be x) =>
BeamSqlBackendSyntax be -> m [x]
runReturningList

-- * BeamSqlBackend

-- | Class for all Beam SQL backends
class ( -- Every SQL backend must be a beam backend
        BeamBackend be

        -- Every SQL backend must have a reasonable SQL92 semantics
      , IsSql92Syntax (BeamSqlBackendSyntax be)
      , Sql92SanityCheck (BeamSqlBackendSyntax be)

        -- Needed for several combinators
      , HasSqlValueSyntax (BeamSqlBackendValueSyntax be) Bool
      , HasSqlValueSyntax (BeamSqlBackendValueSyntax be) SqlNull

        -- Needed for the Eq instance on QGenExpr
      , Eq (BeamSqlBackendExpressionSyntax be)
      ) => BeamSqlBackend be

type family BeamSqlBackendSyntax be :: Type

-- | Fake backend that cannot deserialize anything, but is useful for testing
data MockSqlBackend syntax

class Trivial a
instance Trivial a

instance BeamBackend (MockSqlBackend syntax) where
  type BackendFromField (MockSqlBackend syntax) = Trivial

instance ( IsSql92Syntax syntax
         , Sql92SanityCheck syntax

           -- Needed for several combinators
         , HasSqlValueSyntax (Sql92ValueSyntax syntax) Bool
         , HasSqlValueSyntax (Sql92ValueSyntax syntax) SqlNull

           -- Needed for the Eq instance on QGenExpr
         , Eq (Sql92ExpressionSyntax syntax)
         ) => BeamSqlBackend (MockSqlBackend syntax)
type instance BeamSqlBackendSyntax (MockSqlBackend syntax) = syntax

-- | Type class for things which are text-like in this backend
class BeamSqlBackendIsString be text
instance BeamSqlBackendIsString be t => BeamSqlBackendIsString be (Tagged tag t)
instance BeamSqlBackendIsString (MockSqlBackend cmd) Text
instance BeamSqlBackendIsString (MockSqlBackend cmd) [Char]

type BeamSql99ExpressionBackend be = IsSql99ExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSql99ConcatExpressionBackend be = IsSql99ConcatExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSql99CommonTableExpressionBackend be =
    ( BeamSqlBackend be
    , IsSql99CommonTableExpressionSelectSyntax (BeamSqlBackendSelectSyntax be)
    , IsSql99CommonTableExpressionSyntax (BeamSql99BackendCTESyntax be)
    , Sql99CTESelectSyntax (BeamSql99BackendCTESyntax be) ~ BeamSqlBackendSelectSyntax be )
type BeamSql99RecursiveCTEBackend be=
    ( BeamSql99CommonTableExpressionBackend be
    , IsSql99RecursiveCommonTableExpressionSelectSyntax (BeamSqlBackendSelectSyntax be) )
type BeamSql99AggregationBackend be = IsSql99AggregationExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSql2003ExpressionBackend be = ( IsSql2003ExpressionSyntax (BeamSqlBackendExpressionSyntax be)
                                       , Sql2003SanityCheck (BeamSqlBackendSyntax be) )

type BeamSqlBackendSupportsOuterJoin be = IsSql92FromOuterJoinSyntax (BeamSqlBackendFromSyntax be)

type BeamSqlT021Backend be = IsSql2003BinaryAndVarBinaryDataTypeSyntax (BeamSqlBackendCastTargetSyntax be)
type BeamSqlT071Backend be = IsSql2008BigIntDataTypeSyntax (BeamSqlBackendCastTargetSyntax be)
type BeamSqlT611Backend be = IsSql2003ExpressionElementaryOLAPOperationsSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlT612Backend be = IsSql2003ExpressionAdvancedOLAPOperationsSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlT614Backend be = IsSql2003NtileExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlT615Backend be = IsSql2003LeadAndLagExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlT616Backend be = IsSql2003FirstValueAndLastValueExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlT618Backend be = IsSql2003NthValueExpressionSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlT621Backend be =
  ( IsSql2003EnhancedNumericFunctionsExpressionSyntax (BeamSqlBackendExpressionSyntax be)
  , IsSql2003EnhancedNumericFunctionsAggregationExpressionSyntax (BeamSqlBackendExpressionSyntax be) )

type BeamSql99DataTypeBackend be =
    ( BeamSqlBackend be
    , IsSql99DataTypeSyntax (BeamSqlBackendCastTargetSyntax be) )

type BeamSqlBackendSelectSyntax be = Sql92SelectSyntax (BeamSqlBackendSyntax be)
type BeamSqlBackendInsertSyntax be = Sql92InsertSyntax (BeamSqlBackendSyntax be)
type BeamSqlBackendInsertValuesSyntax be = Sql92InsertValuesSyntax (BeamSqlBackendInsertSyntax be)
type BeamSqlBackendExpressionSyntax be = Sql92ExpressionSyntax (BeamSqlBackendSyntax be)
type BeamSqlBackendDataTypeSyntax be = Sql92ExpressionCastTargetSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlBackendFieldNameSyntax be = Sql92ExpressionFieldNameSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlBackendUpdateSyntax be = Sql92UpdateSyntax (BeamSqlBackendSyntax be)
type BeamSqlBackendDeleteSyntax be = Sql92DeleteSyntax (BeamSqlBackendSyntax be)
type BeamSqlBackendCastTargetSyntax be
    = Sql92ExpressionCastTargetSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlBackendExpressionQuantifierSyntax be = Sql92ExpressionQuantifierSyntax (Sql92ExpressionSyntax (BeamSqlBackendSyntax be))
type BeamSqlBackendValueSyntax be = Sql92ValueSyntax (BeamSqlBackendSyntax be)
type BeamSqlBackendSetQuantifierSyntax be = Sql92SelectTableSetQuantifierSyntax (BeamSqlBackendSelectTableSyntax be)
type BeamSqlBackendAggregationQuantifierSyntax be = Sql92AggregationSetQuantifierSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlBackendSelectTableSyntax be = Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
type BeamSqlBackendFromSyntax be = Sql92SelectFromSyntax (BeamSqlBackendSelectSyntax be)
type BeamSqlBackendTableNameSyntax be =  Sql92TableSourceTableNameSyntax (Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be))
type BeamSqlBackendOrderingSyntax be = Sql92SelectOrderingSyntax (BeamSqlBackendSelectSyntax be)
type BeamSqlBackendGroupingSyntax be = Sql92SelectTableGroupingSyntax (BeamSqlBackendSelectTableSyntax be)

type BeamSqlBackendWindowFrameSyntax be = Sql2003ExpressionWindowFrameSyntax (BeamSqlBackendExpressionSyntax be)
type BeamSqlBackendWindowFrameBoundsSyntax be = Sql2003WindowFrameBoundsSyntax (BeamSqlBackendWindowFrameSyntax be)
type BeamSqlBackendWindowFrameBoundSyntax be = Sql2003WindowFrameBoundsBoundSyntax (BeamSqlBackendWindowFrameBoundsSyntax be)

type BeamSql99BackendCTESyntax be = Sql99SelectCTESyntax (BeamSqlBackendSelectSyntax be)

type BeamSqlBackendCanSerialize be = HasSqlValueSyntax (BeamSqlBackendValueSyntax be)
type BeamSqlBackendCanDeserialize be = FromBackendRow be
type BeamSqlBackendSupportsDataType be x =
  ( BeamSqlBackendCanDeserialize be x
  , BeamSqlBackendCanSerialize be x )