{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE CPP #-}

module Database.Beam.Query.Combinators
    ( -- * Various SQL functions and constructs
      coalesce_, fromMaybe_, position_
    , charLength_, octetLength_, bitLength_
    , currentTimestamp_
    , lower_, upper_
    , trim_

    -- ** @IF-THEN-ELSE@ support
    , if_, then_, else_
    , then_'
    , ifThenElse_, bool_

    -- * SQL @UPDATE@ assignments
    , (<-.), current_

    -- * Project Haskell values to 'QGenExpr's
    , HaskellLiteralForQExpr
    , SqlValable(..), SqlValableTable
    , default_

    -- * General query combinators

    , all_, values_
    , allFromView_, join_, join_'
    , guard_, guard_', filter_, filter_'
    , related_, relatedBy_, relatedBy_'
    , leftJoin_, leftJoin_'
    , perhaps_, outerJoin_, outerJoin_'
    , subselect_, references_, references_'

    , nub_

    , SqlJustable(..)
    , SqlDeconstructMaybe(..)
    , SqlOrderable
    , QIfCond, QIfElse
    , (<|>.)

    , limit_, offset_

    , as_

    -- ** Subqueries
    , exists_, unique_, distinct_, subquery_

    -- ** Set operations
    -- |  'Q' values can be combined using a variety of set operations. See the
    --    <https://haskell-beam.github.io/beam/user-guide/queries/combining-queries manual section>.
    , union_, unionAll_
    , intersect_, intersectAll_
    , except_, exceptAll_

    -- * Window functions
    -- | See the corresponding
    --   <https://haskell-beam.github.io/beam/user-guide/queries/window-functions manual section> for more.
    , over_, frame_, bounds_, unbounded_, nrows_, fromBound_
    , noBounds_, noOrder_, noPartition_
    , partitionBy_, orderPartitionBy_, withWindow_

    -- * Ordering primitives
    , orderBy_, asc_, desc_, nullsFirst_, nullsLast_
    ) where

import Database.Beam.Backend.Types
import Database.Beam.Backend.SQL

import Database.Beam.Query.Internal
import Database.Beam.Query.Ord
import Database.Beam.Query.Operator
import Database.Beam.Query.Types

import Database.Beam.Schema.Tables

import Control.Monad.Identity
import Control.Monad.Free
import Control.Applicative

import Data.Maybe
import Data.Proxy
import Data.Time (LocalTime)

import GHC.TypeLits (TypeError, ErrorMessage(Text))

-- | Introduce all entries of a table into the 'Q' monad
all_ :: ( Database be db, BeamSqlBackend be )
       => DatabaseEntity be db (TableEntity table)
       -> Q be db s (table (QExpr be s))
all_ :: forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> Q be db s (table (QExpr be s))
all_ (DatabaseEntity dt :: DatabaseEntityDescriptor be (TableEntity table)
dt@(DatabaseTable {})) =
    forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r next (db :: (* -> *) -> *) s.
Projectible be r =>
(TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((TablePrefix, r) -> next)
-> QF be db s next
QAll (\TablePrefix
_ -> forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceTableNameSyntax tblSource -> tblSource
tableNamed (forall tblName.
IsSql92TableNameSyntax tblName =>
Maybe TablePrefix -> TablePrefix -> tblName
tableName (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (TableEntity tbl) -> Maybe TablePrefix
dbTableSchema DatabaseEntityDescriptor be (TableEntity table)
dt) (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (TableEntity tbl) -> TablePrefix
dbTableCurrentName DatabaseEntityDescriptor be (TableEntity table)
dt))) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,forall a. Maybe a
Nothing))
                    (forall be (table :: (* -> *) -> *) ctxt s.
(BeamSqlBackend be, Beamable table) =>
TableSettings table -> TablePrefix -> table (QGenExpr ctxt be s)
tableFieldsToExpressions (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (TableEntity tbl) -> TableSettings tbl
dbTableSettings DatabaseEntityDescriptor be (TableEntity table)
dt))
                    (\table (QGenExpr QValueContext be s)
_ -> forall a. Maybe a
Nothing) forall a b. (a, b) -> b
snd)

-- | Introduce all entries of a view into the 'Q' monad
allFromView_ :: ( Database be db, Beamable table
                , BeamSqlBackend be )
               => DatabaseEntity be db (ViewEntity table)
               -> Q be db s (table (QExpr be s))
allFromView_ :: forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Beamable table, BeamSqlBackend be) =>
DatabaseEntity be db (ViewEntity table)
-> Q be db s (table (QExpr be s))
allFromView_ (DatabaseEntity DatabaseEntityDescriptor be (ViewEntity table)
vw) =
    forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r next (db :: (* -> *) -> *) s.
Projectible be r =>
(TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((TablePrefix, r) -> next)
-> QF be db s next
QAll (\TablePrefix
_ -> forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceTableNameSyntax tblSource -> tblSource
tableNamed (forall tblName.
IsSql92TableNameSyntax tblName =>
Maybe TablePrefix -> TablePrefix -> tblName
tableName (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (ViewEntity tbl) -> Maybe TablePrefix
dbViewSchema DatabaseEntityDescriptor be (ViewEntity table)
vw) (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (ViewEntity tbl) -> TablePrefix
dbViewCurrentName DatabaseEntityDescriptor be (ViewEntity table)
vw))) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,forall a. Maybe a
Nothing))
                    (forall be (table :: (* -> *) -> *) ctxt s.
(BeamSqlBackend be, Beamable table) =>
TableSettings table -> TablePrefix -> table (QGenExpr ctxt be s)
tableFieldsToExpressions (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (ViewEntity tbl) -> TableSettings tbl
dbViewSettings DatabaseEntityDescriptor be (ViewEntity table)
vw))
                    (\table (QGenExpr QValueContext be s)
_ -> forall a. Maybe a
Nothing) forall a b. (a, b) -> b
snd)

-- | SQL @VALUES@ clause. Introduce the elements of the given list as
-- rows in a joined table.
values_ :: forall be db s a
         . ( Projectible be a
           , BeamSqlBackend be )
        => [ a ] -> Q be db s a
values_ :: forall be (db :: (* -> *) -> *) s a.
(Projectible be a, BeamSqlBackend be) =>
[a] -> Q be db s a
values_ [a]
rows =
    forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r next (db :: (* -> *) -> *) s.
Projectible be r =>
(TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((TablePrefix, r) -> next)
-> QF be db s next
QAll (\TablePrefix
tblPfx -> forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
[[Sql92TableSourceExpressionSyntax tblSource]] -> tblSource
tableFromValues (forall a b. (a -> b) -> [a] -> [b]
map (\a
row -> forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
project (forall {k} (t :: k). Proxy t
Proxy @be) a
row TablePrefix
tblPfx) [a]
rows)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,forall a. a -> Maybe a
Just [TablePrefix]
fieldNames))
                    (\TablePrefix
tblNm' -> forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall be res.
(BeamSqlBackend be, Projectible be res) =>
(TablePrefix -> BeamSqlBackendFieldNameSyntax be)
-> (res, [TablePrefix])
mkFieldNames (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
tblNm'))
                    (\a
_ -> forall a. Maybe a
Nothing) forall a b. (a, b) -> b
snd)
    where
      fieldNames :: [TablePrefix]
fieldNames = forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall be res.
(BeamSqlBackend be, Projectible be res) =>
(TablePrefix -> BeamSqlBackendFieldNameSyntax be)
-> (res, [TablePrefix])
mkFieldNames @be @a forall fn. IsSql92FieldNameSyntax fn => TablePrefix -> fn
unqualifiedField

-- | Introduce all entries of a table into the 'Q' monad based on the
--   given QExpr. The join condition is expected to return a
--   'Bool'. For a version that takes 'SqlBool' (a possibly @UNKNOWN@
--   boolean, that maps more closely to the SQL standard), see
--   'join_''.
join_ :: ( Database be db, Table table, BeamSqlBackend be )
      => DatabaseEntity be db (TableEntity table)
      -> (table (QExpr be s) -> QExpr be s Bool)
      -> Q be db s (table (QExpr be s))
join_ :: forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s Bool)
-> Q be db s (table (QExpr be s))
join_ DatabaseEntity be db (TableEntity table)
tbl table (QExpr be s) -> QExpr be s Bool
mkOn = forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s SqlBool)
-> Q be db s (table (QExpr be s))
join_' DatabaseEntity be db (TableEntity table)
tbl (forall context syntax s.
QGenExpr context syntax s Bool -> QGenExpr context syntax s SqlBool
sqlBool_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. table (QExpr be s) -> QExpr be s Bool
mkOn)

-- | Like 'join_', but accepting an @ON@ condition that returns
-- 'SqlBool'
join_' :: ( Database be db, Table table, BeamSqlBackend be )
       => DatabaseEntity be db (TableEntity table)
       -> (table (QExpr be s) -> QExpr be s SqlBool)
       -> Q be db s (table (QExpr be s))
join_' :: forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s SqlBool)
-> Q be db s (table (QExpr be s))
join_' (DatabaseEntity tbl :: DatabaseEntityDescriptor be (TableEntity table)
tbl@(DatabaseTable {})) table (QGenExpr QValueContext be s) -> QExpr be s SqlBool
mkOn =
    forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r next (db :: (* -> *) -> *) s.
Projectible be r =>
(TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((TablePrefix, r) -> next)
-> QF be db s next
QAll (\TablePrefix
_ -> forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceTableNameSyntax tblSource -> tblSource
tableNamed (forall tblName.
IsSql92TableNameSyntax tblName =>
Maybe TablePrefix -> TablePrefix -> tblName
tableName (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (TableEntity tbl) -> Maybe TablePrefix
dbTableSchema DatabaseEntityDescriptor be (TableEntity table)
tbl) (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (TableEntity tbl) -> TablePrefix
dbTableCurrentName DatabaseEntityDescriptor be (TableEntity table)
tbl))) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (, forall a. Maybe a
Nothing))
                    (forall be (table :: (* -> *) -> *) ctxt s.
(BeamSqlBackend be, Beamable table) =>
TableSettings table -> TablePrefix -> table (QGenExpr ctxt be s)
tableFieldsToExpressions (forall (tbl :: (* -> *) -> *) be.
DatabaseEntityDescriptor be (TableEntity tbl) -> TableSettings tbl
dbTableSettings DatabaseEntityDescriptor be (TableEntity table)
tbl))
                    (\table (QGenExpr QValueContext be s)
tbl' -> let QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
on = table (QGenExpr QValueContext be s) -> QExpr be s SqlBool
mkOn table (QGenExpr QValueContext be s)
tbl' in forall a. a -> Maybe a
Just WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
on) forall a b. (a, b) -> b
snd)

-- | Introduce a table using a left join with no ON clause. Because this is not
--   an inner join, the resulting table is made nullable. This means that each
--   field that would normally have type 'QExpr x' will now have type 'QExpr
--   (Maybe x)'.
perhaps_ :: forall s r be db.
          ( Projectible be r, BeamSqlBackend be
          , ThreadRewritable (QNested s) r
          , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r) )
         => Q be db (QNested s) r
         -> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
perhaps_ :: forall s r be (db :: (* -> *) -> *).
(Projectible be r, BeamSqlBackend be,
 ThreadRewritable (QNested s) r,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r)) =>
Q be db (QNested s) r
-> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
perhaps_ (Q QM be db (QNested s) r
sub) =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
QM be db (QNested s) r
-> TablePrefix
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> (r -> next)
-> QF be db s next
QArbitraryJoin
              QM be db (QNested s) r
sub TablePrefix
"" forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
leftJoin
              (\r
_ -> forall a. Maybe a
Nothing)
              (\r
r -> forall (f :: * -> *) x (tag :: (* -> *) -> * -> *).
Retaggable f x =>
(forall a. Columnar' f a -> Columnar' (tag f) a)
-> x -> Retag tag x
retag (\(Columnar' (QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e) :: Columnar' (QExpr be s) a) ->
                                            forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e) :: Columnar' (Nullable (QExpr be s)) a) forall a b. (a -> b) -> a -> b
$
                                  forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) r
r))

-- | Outer join. every row of each table, returning @NULL@ for any row
-- of either table for which the join condition finds no related rows.
--
-- This expects a join expression returning 'Bool', for a version that
-- accepts a 'SqlBool' (a possibly @UNKNOWN@ boolean, that maps more
-- closely to the SQL standard), see 'outerJoin_''
outerJoin_ :: forall s a b be db.
              ( BeamSqlBackend be, BeamSqlBackendSupportsOuterJoin be
              , Projectible be a, Projectible be b
              , ThreadRewritable (QNested s) a, ThreadRewritable (QNested s) b
              , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s a)
              , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s b)
              )
           => Q be db (QNested s) a
           -> Q be db (QNested s) b
           -> ( (WithRewrittenThread (QNested s) s a, WithRewrittenThread (QNested s) s b) -> QExpr be s Bool )
           -> Q be db s ( Retag Nullable (WithRewrittenThread (QNested s) s a)
                        , Retag Nullable (WithRewrittenThread (QNested s) s b) )
outerJoin_ :: forall s a b be (db :: (* -> *) -> *).
(BeamSqlBackend be, BeamSqlBackendSupportsOuterJoin be,
 Projectible be a, Projectible be b, ThreadRewritable (QNested s) a,
 ThreadRewritable (QNested s) b,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s a),
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s b)) =>
Q be db (QNested s) a
-> Q be db (QNested s) b
-> ((WithRewrittenThread (QNested s) s a,
     WithRewrittenThread (QNested s) s b)
    -> QExpr be s Bool)
-> Q be
     db
     s
     (Retag Nullable (WithRewrittenThread (QNested s) s a),
      Retag Nullable (WithRewrittenThread (QNested s) s b))
outerJoin_ Q be db (QNested s) a
a Q be db (QNested s) b
b (WithRewrittenThread (QNested s) s a,
 WithRewrittenThread (QNested s) s b)
-> QExpr be s Bool
on_ = forall s a b be (db :: (* -> *) -> *).
(BeamSqlBackend be, BeamSqlBackendSupportsOuterJoin be,
 Projectible be a, Projectible be b, ThreadRewritable (QNested s) a,
 ThreadRewritable (QNested s) b,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s a),
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s b)) =>
Q be db (QNested s) a
-> Q be db (QNested s) b
-> ((WithRewrittenThread (QNested s) s a,
     WithRewrittenThread (QNested s) s b)
    -> QExpr be s SqlBool)
-> Q be
     db
     s
     (Retag Nullable (WithRewrittenThread (QNested s) s a),
      Retag Nullable (WithRewrittenThread (QNested s) s b))
outerJoin_' Q be db (QNested s) a
a Q be db (QNested s) b
b (forall context syntax s.
QGenExpr context syntax s Bool -> QGenExpr context syntax s SqlBool
sqlBool_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WithRewrittenThread (QNested s) s a,
 WithRewrittenThread (QNested s) s b)
-> QExpr be s Bool
on_)

-- | Like 'outerJoin_', but accepting 'SqlBool'. Pairs of rows for
-- which the join condition is unknown are considered to be unrelated,
-- by SQL compliant databases at least.
outerJoin_' :: forall s a b be db.
               ( BeamSqlBackend be, BeamSqlBackendSupportsOuterJoin be
               , Projectible be a, Projectible be b
               , ThreadRewritable (QNested s) a, ThreadRewritable (QNested s) b
               , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s a)
               , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s b)
               )
            => Q be db (QNested s) a
            -> Q be db (QNested s) b
            -> ( (WithRewrittenThread (QNested s) s a, WithRewrittenThread (QNested s) s b) -> QExpr be s SqlBool )
            -> Q be db s ( Retag Nullable (WithRewrittenThread (QNested s) s a)
                         , Retag Nullable (WithRewrittenThread (QNested s) s b) )
outerJoin_' :: forall s a b be (db :: (* -> *) -> *).
(BeamSqlBackend be, BeamSqlBackendSupportsOuterJoin be,
 Projectible be a, Projectible be b, ThreadRewritable (QNested s) a,
 ThreadRewritable (QNested s) b,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s a),
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s b)) =>
Q be db (QNested s) a
-> Q be db (QNested s) b
-> ((WithRewrittenThread (QNested s) s a,
     WithRewrittenThread (QNested s) s b)
    -> QExpr be s SqlBool)
-> Q be
     db
     s
     (Retag Nullable (WithRewrittenThread (QNested s) s a),
      Retag Nullable (WithRewrittenThread (QNested s) s b))
outerJoin_' (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) b
b) (WithRewrittenThread (QNested s) s a,
 WithRewrittenThread (QNested s) s b)
-> QExpr be s SqlBool
on_ =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r r (db :: (* -> *) -> *) s next.
(Projectible be r, Projectible be r) =>
QM be db (QNested s) r
-> QM be db (QNested s) r
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> ((r, r)
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((r, r) -> next)
-> QF be db s next
QTwoWayJoin QM be db (QNested s) a
a QM be db (QNested s) b
b forall from.
IsSql92FromOuterJoinSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
outerJoin
              (\(a
a', b
b') ->
                 let QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e = (WithRewrittenThread (QNested s) s a,
 WithRewrittenThread (QNested s) s b)
-> QExpr be s SqlBool
on_ (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) a
a', forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) b
b')
                 in forall a. a -> Maybe a
Just WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e)
              (\(a
a', b
b') ->
                 let retag' :: (ThreadRewritable (QNested s) x, Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s x))
                            => x -> Retag Nullable (WithRewrittenThread (QNested s) s x)
                     retag' :: forall x.
(ThreadRewritable (QNested s) x,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s x)) =>
x -> Retag Nullable (WithRewrittenThread (QNested s) s x)
retag' = forall (f :: * -> *) x (tag :: (* -> *) -> * -> *).
Retaggable f x =>
(forall a. Columnar' f a -> Columnar' (tag f) a)
-> x -> Retag tag x
retag (\(Columnar' (QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e) :: Columnar' (QExpr be s) x) ->
                                        forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e) :: Columnar' (Nullable (QExpr be s)) x) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                              forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s)
                 in ( forall x.
(ThreadRewritable (QNested s) x,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s x)) =>
x -> Retag Nullable (WithRewrittenThread (QNested s) s x)
retag' a
a', forall x.
(ThreadRewritable (QNested s) x,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s x)) =>
x -> Retag Nullable (WithRewrittenThread (QNested s) s x)
retag' b
b' )))

-- | Introduce a table using a left join. The ON clause is required here.Because
--   this is not an inner join, the resulting table is made nullable. This means
--   that each field that would normally have type 'QExpr x' will now have type
--   'QExpr (Maybe x)'.
--
--   The @ON@ condition given must return 'Bool'. For a version that
--   accepts an @ON@ condition returning 'SqlBool', see 'leftJoin_''.
leftJoin_ :: forall s r be db.
           ( BeamSqlBackend be, Projectible be r
           , ThreadRewritable (QNested s) r
           , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r) )
          => Q be db (QNested s) r
          -> (WithRewrittenThread (QNested s) s r -> QExpr be s Bool)
          -> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
leftJoin_ :: forall s r be (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be r,
 ThreadRewritable (QNested s) r,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r)) =>
Q be db (QNested s) r
-> (WithRewrittenThread (QNested s) s r -> QExpr be s Bool)
-> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
leftJoin_ Q be db (QNested s) r
sub WithRewrittenThread (QNested s) s r -> QExpr be s Bool
on_ = forall s r be (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be r,
 ThreadRewritable (QNested s) r,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r)) =>
Q be db (QNested s) r
-> (WithRewrittenThread (QNested s) s r -> QExpr be s SqlBool)
-> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
leftJoin_' Q be db (QNested s) r
sub (forall context syntax s.
QGenExpr context syntax s Bool -> QGenExpr context syntax s SqlBool
sqlBool_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithRewrittenThread (QNested s) s r -> QExpr be s Bool
on_)

-- | Like 'leftJoin_', but accepts an @ON@ clause returning 'SqlBool'.
leftJoin_' :: forall s r be db.
            ( BeamSqlBackend be, Projectible be r
            , ThreadRewritable (QNested s) r
            , Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r) )
           => Q be db (QNested s) r
           -> (WithRewrittenThread (QNested s) s r -> QExpr be s SqlBool)
           -> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
leftJoin_' :: forall s r be (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be r,
 ThreadRewritable (QNested s) r,
 Retaggable (QExpr be s) (WithRewrittenThread (QNested s) s r)) =>
Q be db (QNested s) r
-> (WithRewrittenThread (QNested s) s r -> QExpr be s SqlBool)
-> Q be db s (Retag Nullable (WithRewrittenThread (QNested s) s r))
leftJoin_' (Q QM be db (QNested s) r
sub) WithRewrittenThread (QNested s) s r -> QExpr be s SqlBool
on_ =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
QM be db (QNested s) r
-> TablePrefix
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> (r -> next)
-> QF be db s next
QArbitraryJoin
               QM be db (QNested s) r
sub TablePrefix
"" forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
leftJoin
               (\r
r -> let QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e = WithRewrittenThread (QNested s) s r -> QExpr be s SqlBool
on_ (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) r
r) in forall a. a -> Maybe a
Just WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e)
               (\r
r -> forall (f :: * -> *) x (tag :: (* -> *) -> * -> *).
Retaggable f x =>
(forall a. Columnar' f a -> Columnar' (tag f) a)
-> x -> Retag tag x
retag (\(Columnar' (QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e) :: Columnar' (QExpr be s) a) ->
                                forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
e) :: Columnar' (Nullable (QExpr be s)) a) forall a b. (a -> b) -> a -> b
$
                      forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) r
r))

subselect_ :: forall s r be db.
            ( ThreadRewritable (QNested s) r
            , Projectible be r )
           => Q be db (QNested s) r
           -> Q be db s (WithRewrittenThread (QNested s) s r)
subselect_ :: forall s r be (db :: (* -> *) -> *).
(ThreadRewritable (QNested s) r, Projectible be r) =>
Q be db (QNested s) r
-> Q be db s (WithRewrittenThread (QNested s) s r)
subselect_ (Q QM be db (QNested s) r
q') =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
QM be db (QNested s) r -> (r -> next) -> QF be db s next
QSubSelect QM be db (QNested s) r
q' (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | Only allow results for which the 'QExpr' yields 'True'. For a
-- version that operates over possibly @NULL@ 'SqlBool's, see
-- 'guard_''.
guard_ :: forall be db s
        . BeamSqlBackend be
       => QExpr be s Bool -> Q be db s ()
guard_ :: forall be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
QExpr be s Bool -> Q be db s ()
guard_ = forall be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
QExpr be s SqlBool -> Q be db s ()
guard_' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall context syntax s.
QGenExpr context syntax s Bool -> QGenExpr context syntax s SqlBool
sqlBool_

-- | Only allow results for which the 'QExpr' yields @TRUE@.
--
-- This function operates over 'SqlBool', which are like haskell
-- 'Bool's, except for the special @UNKNOWN@ value that occurs when
-- comparisons include @NULL@. For a version that operates over known
-- non-@NULL@ booleans, see 'guard_'.
guard_' :: forall be db s
         . BeamSqlBackend be
        => QExpr be s SqlBool -> Q be db s ()
guard_' :: forall be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
QExpr be s SqlBool -> Q be db s ()
guard_' (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
guardE') = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be next (db :: (* -> *) -> *) s.
WithExprContext (BeamSqlBackendExpressionSyntax be)
-> next -> QF be db s next
QGuard TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
guardE' ()))

-- | Synonym for @clause >>= \\x -> guard_ (mkExpr x)>> pure x@. Use 'filter_'' for comparisons with 'SqlBool'
filter_ :: forall r be db s
         . BeamSqlBackend be
        => (r -> QExpr be s Bool)
        -> Q be db s r -> Q be db s r
filter_ :: forall r be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
(r -> QExpr be s Bool) -> Q be db s r -> Q be db s r
filter_ r -> QExpr be s Bool
mkExpr Q be db s r
clause = Q be db s r
clause forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \r
x -> forall be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
QExpr be s Bool -> Q be db s ()
guard_ (r -> QExpr be s Bool
mkExpr r
x) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure r
x

-- | Synonym for @clause >>= \\x -> guard_' (mkExpr x)>> pure x@. Use 'filter_' for comparisons with 'Bool'
filter_' :: forall r be db s
          . BeamSqlBackend be
        => (r -> QExpr be s SqlBool)
        -> Q be db s r -> Q be db s r
filter_' :: forall r be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
(r -> QExpr be s SqlBool) -> Q be db s r -> Q be db s r
filter_' r -> QExpr be s SqlBool
mkExpr Q be db s r
clause = Q be db s r
clause forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \r
x -> forall be (db :: (* -> *) -> *) s.
BeamSqlBackend be =>
QExpr be s SqlBool -> Q be db s ()
guard_' (r -> QExpr be s SqlBool
mkExpr r
x) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure r
x

-- | Introduce all entries of the given table which are referenced by the given 'PrimaryKey'
related_ :: forall be db rel s
          . ( Database be db, Table rel, BeamSqlBackend be
            , HasTableEquality be (PrimaryKey rel)
            )
         => DatabaseEntity be db (TableEntity rel)
         -> PrimaryKey rel (QExpr be s)
         -> Q be db s (rel (QExpr be s))
related_ :: forall be (db :: (* -> *) -> *) (rel :: (* -> *) -> *) s.
(Database be db, Table rel, BeamSqlBackend be,
 HasTableEquality be (PrimaryKey rel)) =>
DatabaseEntity be db (TableEntity rel)
-> PrimaryKey rel (QExpr be s) -> Q be db s (rel (QExpr be s))
related_ DatabaseEntity be db (TableEntity rel)
relTbl PrimaryKey rel (QGenExpr QValueContext be s)
relKey =
  forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s Bool)
-> Q be db s (table (QExpr be s))
join_ DatabaseEntity be db (TableEntity rel)
relTbl (\rel (QGenExpr QValueContext be s)
rel -> PrimaryKey rel (QGenExpr QValueContext be s)
relKey forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr Bool
==. forall (table :: (* -> *) -> *) (column :: * -> *).
Table table =>
table column -> PrimaryKey table column
primaryKey rel (QGenExpr QValueContext be s)
rel)

-- | Introduce all entries of the given table for which the expression (which can depend on the queried table returns true)
relatedBy_ :: forall be db rel s
            . ( Database be db, Table rel, BeamSqlBackend be )
           => DatabaseEntity be db (TableEntity rel)
           -> (rel (QExpr be s) -> QExpr be s Bool)
           -> Q be db s (rel (QExpr be s))
relatedBy_ :: forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s Bool)
-> Q be db s (table (QExpr be s))
relatedBy_ = forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s Bool)
-> Q be db s (table (QExpr be s))
join_

-- | Introduce all entries of the given table for which the expression (which can depend on the queried table returns true)
relatedBy_' :: forall be db rel s
             . ( Database be db, Table rel, BeamSqlBackend be )
            => DatabaseEntity be db (TableEntity rel)
            -> (rel (QExpr be s) -> QExpr be s SqlBool)
            -> Q be db s (rel (QExpr be s))
relatedBy_' :: forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s SqlBool)
-> Q be db s (table (QExpr be s))
relatedBy_' = forall be (db :: (* -> *) -> *) (table :: (* -> *) -> *) s.
(Database be db, Table table, BeamSqlBackend be) =>
DatabaseEntity be db (TableEntity table)
-> (table (QExpr be s) -> QExpr be s SqlBool)
-> Q be db s (table (QExpr be s))
join_'

-- | Generate an appropriate boolean 'QGenExpr' comparing the given foreign key
--   to the given table. Useful for creating join conditions.
--   Use 'references_'' for a 'SqlBool' comparison.
references_ :: ( Table t, BeamSqlBackend be
               , HasTableEquality be (PrimaryKey t) )
            => PrimaryKey t (QGenExpr ctxt be s) -> t (QGenExpr ctxt be s) -> QGenExpr ctxt be s Bool
references_ :: forall (t :: (* -> *) -> *) be ctxt s.
(Table t, BeamSqlBackend be, HasTableEquality be (PrimaryKey t)) =>
PrimaryKey t (QGenExpr ctxt be s)
-> t (QGenExpr ctxt be s) -> QGenExpr ctxt be s Bool
references_ PrimaryKey t (QGenExpr ctxt be s)
fk t (QGenExpr ctxt be s)
tbl = PrimaryKey t (QGenExpr ctxt be s)
fk forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr Bool
==. forall (t :: (* -> *) -> *) (f :: * -> *).
Table t =>
t f -> PrimaryKey t f
pk t (QGenExpr ctxt be s)
tbl

-- | Generate an appropriate boolean 'QGenExpr' comparing the given foreign key
--   to the given table. Useful for creating join conditions.
--   Use 'references_' for a 'Bool' comparison.
references_' :: ( Table t, BeamSqlBackend be
                , HasTableEquality be (PrimaryKey t) )
             => PrimaryKey t (QGenExpr ctxt be s) -> t (QGenExpr ctxt be s) -> QGenExpr ctxt be s SqlBool
references_' :: forall (t :: (* -> *) -> *) be ctxt s.
(Table t, BeamSqlBackend be, HasTableEquality be (PrimaryKey t)) =>
PrimaryKey t (QGenExpr ctxt be s)
-> t (QGenExpr ctxt be s) -> QGenExpr ctxt be s SqlBool
references_' PrimaryKey t (QGenExpr ctxt be s)
fk t (QGenExpr ctxt be s)
tbl = PrimaryKey t (QGenExpr ctxt be s)
fk forall (expr :: * -> *) a. SqlEq expr a => a -> a -> expr SqlBool
==?. forall (t :: (* -> *) -> *) (f :: * -> *).
Table t =>
t f -> PrimaryKey t f
pk t (QGenExpr ctxt be s)
tbl

-- | Only return distinct values from a query
nub_ :: ( BeamSqlBackend be, Projectible be r )
     => Q be db s r -> Q be db s r
nub_ :: forall be r (db :: (* -> *) -> *) s.
(BeamSqlBackend be, Projectible be r) =>
Q be db s r -> Q be db s r
nub_ (Q QM be db s r
sub) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be))
-> QM be db s r -> (r -> next) -> QF be db s next
QDistinct (\r
_ TablePrefix
_ -> forall q. IsSql92AggregationSetQuantifierSyntax q => q
setQuantifierDistinct) QM be db s r
sub forall a. a -> a
id)

-- | Limit the number of results returned by a query.
limit_ :: forall s a be db
        . ( Projectible be a
          , ThreadRewritable (QNested s) a )
        => Integer -> Q be db (QNested s) a -> Q be db s (WithRewrittenThread (QNested s) s a)
limit_ :: forall s a be (db :: (* -> *) -> *).
(Projectible be a, ThreadRewritable (QNested s) a) =>
Integer
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
limit_ Integer
limit' (Q QM be db (QNested s) a
q) =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
Integer -> QM be db (QNested s) r -> (r -> next) -> QF be db s next
QLimit Integer
limit' QM be db (QNested s) a
q (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | Drop the first `offset'` results.
offset_ :: forall s a be db
         . ( Projectible be a
           , ThreadRewritable (QNested s) a )
        => Integer -> Q be db (QNested s) a -> Q be db s (WithRewrittenThread (QNested s) s a)
offset_ :: forall s a be (db :: (* -> *) -> *).
(Projectible be a, ThreadRewritable (QNested s) a) =>
Integer
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
offset_ Integer
offset' (Q QM be db (QNested s) a
q) =
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
Integer -> QM be db (QNested s) r -> (r -> next) -> QF be db s next
QOffset Integer
offset' QM be db (QNested s) a
q (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | Use the SQL @EXISTS@ operator to determine if the given query returns any results
exists_ :: ( BeamSqlBackend be, HasQBuilder be, Projectible be a)
        => Q be db s a -> QExpr be s Bool
exists_ :: forall be a (db :: (* -> *) -> *) s.
(BeamSqlBackend be, HasQBuilder be, Projectible be a) =>
Q be db s a -> QExpr be s Bool
exists_ Q be db s a
q = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionSelectSyntax expr -> expr
existsE (forall be a (db :: (* -> *) -> *) s.
(HasQBuilder be, Projectible be a) =>
TablePrefix -> Q be db s a -> BeamSqlBackendSelectSyntax be
buildSqlQuery TablePrefix
tbl Q be db s a
q))

-- | Use the SQL @UNIQUE@ operator to determine if the given query produces a unique result
unique_ :: ( BeamSqlBackend be, HasQBuilder be, Projectible be a)
        => Q be db s a -> QExpr be s Bool
unique_ :: forall be a (db :: (* -> *) -> *) s.
(BeamSqlBackend be, HasQBuilder be, Projectible be a) =>
Q be db s a -> QExpr be s Bool
unique_ Q be db s a
q = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionSelectSyntax expr -> expr
uniqueE (forall be a (db :: (* -> *) -> *) s.
(HasQBuilder be, Projectible be a) =>
TablePrefix -> Q be db s a -> BeamSqlBackendSelectSyntax be
buildSqlQuery TablePrefix
tbl Q be db s a
q))

-- | Use the SQL99 @DISTINCT@ operator to determine if the given query produces a distinct result
distinct_ :: ( BeamSqlBackend be, BeamSql99ExpressionBackend be, HasQBuilder be, Projectible be a)
          => Q be db s a -> QExpr be s Bool
distinct_ :: forall be a (db :: (* -> *) -> *) s.
(BeamSqlBackend be, BeamSql99ExpressionBackend be, HasQBuilder be,
 Projectible be a) =>
Q be db s a -> QExpr be s Bool
distinct_ Q be db s a
q = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> forall expr.
IsSql99ExpressionSyntax expr =>
Sql92ExpressionSelectSyntax expr -> expr
distinctE (forall be a (db :: (* -> *) -> *) s.
(HasQBuilder be, Projectible be a) =>
TablePrefix -> Q be db s a -> BeamSqlBackendSelectSyntax be
buildSqlQuery TablePrefix
tbl Q be db s a
q))

-- | Project the (presumably) singular result of the given query as an expression
subquery_ :: ( BeamSqlBackend be, HasQBuilder be, Projectible be (QExpr be s a) )
          => Q be db s (QExpr be s a)
          -> QGenExpr ctxt be s a
subquery_ :: forall be s a (db :: (* -> *) -> *) ctxt.
(BeamSqlBackend be, HasQBuilder be,
 Projectible be (QExpr be s a)) =>
Q be db s (QExpr be s a) -> QGenExpr ctxt be s a
subquery_ Q be db s (QExpr be s a)
q =
  forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionSelectSyntax expr -> expr
subqueryE (forall be a (db :: (* -> *) -> *) s.
(HasQBuilder be, Projectible be a) =>
TablePrefix -> Q be db s a -> BeamSqlBackendSelectSyntax be
buildSqlQuery TablePrefix
tbl Q be db s (QExpr be s a)
q))

-- | SQL @CHAR_LENGTH@ function
charLength_ :: ( BeamSqlBackend be, BeamSqlBackendIsString be text, Integral a )
            => QGenExpr context be s text -> QGenExpr context be s a
charLength_ :: forall be text a context s.
(BeamSqlBackend be, BeamSqlBackendIsString be text, Integral a) =>
QGenExpr context be s text -> QGenExpr context be s a
charLength_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
charLengthE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s)

-- | SQL @OCTET_LENGTH@ function
octetLength_ :: ( BeamSqlBackend be, BeamSqlBackendIsString be text, Integral a )
             => QGenExpr context be s text -> QGenExpr context be s a
octetLength_ :: forall be text a context s.
(BeamSqlBackend be, BeamSqlBackendIsString be text, Integral a) =>
QGenExpr context be s text -> QGenExpr context be s a
octetLength_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
octetLengthE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s)

-- | SQL @BIT_LENGTH@ function
bitLength_ :: ( BeamSqlBackend be, Integral a )
           => QGenExpr context be s SqlBitString -> QGenExpr context be s a
bitLength_ :: forall be a context s.
(BeamSqlBackend be, Integral a) =>
QGenExpr context be s SqlBitString -> QGenExpr context be s a
bitLength_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
x) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
bitLengthE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
x)

-- | SQL @CURRENT_TIMESTAMP@ function
currentTimestamp_ :: BeamSqlBackend be => QGenExpr ctxt be s LocalTime
currentTimestamp_ :: forall be ctxt s. BeamSqlBackend be => QGenExpr ctxt be s LocalTime
currentTimestamp_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr. IsSql92ExpressionSyntax expr => expr
currentTimestampE)

-- | SQL @POSITION(.. IN ..)@ function
position_ :: ( BeamSqlBackendIsString be text
             , BeamSqlBackend be, Integral b )
          => QExpr be s text -> QExpr be s text -> QExpr be s b
position_ :: forall be text b s.
(BeamSqlBackendIsString be text, BeamSqlBackend be, Integral b) =>
QExpr be s text -> QExpr be s text -> QExpr be s b
position_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
needle) (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
haystack) =
  forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall expr. IsSql92ExpressionSyntax expr => expr -> expr -> expr
likeE TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
needle TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
haystack)

-- | SQL @LOWER@ function
lower_ ::  ( BeamSqlBackendIsString be text
           , BeamSqlBackend be )
       => QGenExpr context be s text -> QGenExpr context be s text
lower_ :: forall be text context s.
(BeamSqlBackendIsString be text, BeamSqlBackend be) =>
QGenExpr context be s text -> QGenExpr context be s text
lower_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
lowerE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s)

-- | SQL @UPPER@ function
upper_ :: ( BeamSqlBackendIsString be text
          , BeamSqlBackend be )
       => QGenExpr context be s text -> QGenExpr context be s text
upper_ :: forall be text context s.
(BeamSqlBackendIsString be text, BeamSqlBackend be) =>
QGenExpr context be s text -> QGenExpr context be s text
upper_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
upperE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s)

-- | SQL @TRIM@ function
trim_ :: ( BeamSqlBackendIsString be text
         , BeamSqlBackend be )
       => QGenExpr context be s text -> QGenExpr context be s text
trim_ :: forall be text context s.
(BeamSqlBackendIsString be text, BeamSqlBackend be) =>
QGenExpr context be s text -> QGenExpr context be s text
trim_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
trimE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
s)

-- | Combine all the given boolean value 'QGenExpr's with the '&&.' operator.
allE :: BeamSqlBackend be
     => [ QGenExpr context be s Bool ] -> QGenExpr context be s Bool
allE :: forall be context s.
BeamSqlBackend be =>
[QGenExpr context be s Bool] -> QGenExpr context be s Bool
allE [QGenExpr context be s Bool]
es = forall a. a -> Maybe a -> a
fromMaybe (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax Bool
True)))) forall a b. (a -> b) -> a -> b
$
          forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Maybe (QGenExpr context be s Bool)
expr QGenExpr context be s Bool
x ->
                   forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe QGenExpr context be s Bool
x (\QGenExpr context be s Bool
e -> QGenExpr context be s Bool
e forall be context s.
BeamSqlBackend be =>
QGenExpr context be s Bool
-> QGenExpr context be s Bool -> QGenExpr context be s Bool
&&. QGenExpr context be s Bool
x) Maybe (QGenExpr context be s Bool)
expr)
                forall a. Maybe a
Nothing [QGenExpr context be s Bool]
es

-- * UPDATE operators

-- | Extract an expression representing the current (non-UPDATEd) value of a 'QField'
current_ :: BeamSqlBackend be => QField s ty -> QExpr be s ty
current_ :: forall be s ty. BeamSqlBackend be => QField s ty -> QExpr be s ty
current_ (QField Bool
False TablePrefix
_ TablePrefix
nm) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionFieldNameSyntax expr -> expr
fieldE (forall fn. IsSql92FieldNameSyntax fn => TablePrefix -> fn
unqualifiedField TablePrefix
nm)))
current_ (QField Bool
True TablePrefix
tbl TablePrefix
nm) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionFieldNameSyntax expr -> expr
fieldE (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
tbl TablePrefix
nm)))

infix 4 <-.
class BeamSqlBackend be =>
  SqlUpdatable be s lhs rhs | rhs -> be, lhs -> s
                            , rhs -> s, lhs s be -> rhs
                            , rhs -> lhs where


  -- | Update a 'QField' or 'Beamable' type containing 'QField's with the given
  --   'QExpr' or 'Beamable' type containing 'QExpr'
  (<-.) :: lhs
        -> rhs
        -> QAssignment be s

instance BeamSqlBackend be => SqlUpdatable be s (QField s a) (QExpr be s a) where
  QField Bool
_ TablePrefix
_ TablePrefix
nm <-. :: QField s a -> QExpr be s a -> QAssignment be s
<-. QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
expr =
    forall be s.
[(BeamSqlBackendFieldNameSyntax be,
  BeamSqlBackendExpressionSyntax be)]
-> QAssignment be s
QAssignment [(forall fn. IsSql92FieldNameSyntax fn => TablePrefix -> fn
unqualifiedField TablePrefix
nm, TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
expr TablePrefix
"t")]

instance (BeamSqlBackend be, Beamable tbl) => SqlUpdatable be s (tbl (QField s)) (tbl (QExpr be s)) where
  tbl (QField s)
lhs <-. :: tbl (QField s) -> tbl (QExpr be s) -> QAssignment be s
<-. tbl (QExpr be s)
rhs =
    forall be s.
[(BeamSqlBackendFieldNameSyntax be,
  BeamSqlBackendExpressionSyntax be)]
-> QAssignment be s
QAssignment forall a b. (a -> b) -> a -> b
$
    forall (table :: (* -> *) -> *) (f :: * -> *) b.
Beamable table =>
(forall a. Columnar' f a -> b) -> table f -> [b]
allBeamValues (\(Columnar' (Const (Sql92ExpressionFieldNameSyntax
   (Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be))),
 Sql92UpdateExpressionSyntax
   (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
assignments)) -> (Sql92ExpressionFieldNameSyntax
   (Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be))),
 Sql92UpdateExpressionSyntax
   (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
assignments) forall a b. (a -> b) -> a -> b
$
    forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$
    forall (table :: (* -> *) -> *) (m :: * -> *) (f :: * -> *)
       (g :: * -> *) (h :: * -> *).
(Beamable table, Applicative m) =>
(forall a. Columnar' f a -> Columnar' g a -> m (Columnar' h a))
-> table f -> table g -> m (table h)
zipBeamFieldsM (\(Columnar' (QField Bool
_ TablePrefix
_ TablePrefix
f) :: Columnar' (QField s) t) (Columnar' (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e)) ->
                       forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall {k} a (b :: k). a -> Const a b
Const (forall fn. IsSql92FieldNameSyntax fn => TablePrefix -> fn
unqualifiedField TablePrefix
f, TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e TablePrefix
"t")) :: Columnar' (Const (BeamSqlBackendFieldNameSyntax be, BeamSqlBackendExpressionSyntax be)) t)) tbl (QField s)
lhs tbl (QExpr be s)
rhs

instance (BeamSqlBackend be, Beamable tbl) => SqlUpdatable be s (tbl (Nullable (QField s))) (tbl (Nullable (QExpr be s))) where
  tbl (Nullable (QField s))
lhs <-. :: tbl (Nullable (QField s))
-> tbl (Nullable (QExpr be s)) -> QAssignment be s
<-. tbl (Nullable (QExpr be s))
rhs =
    let lhs' :: tbl (QField s)
lhs' = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (QField Bool
q TablePrefix
tblName TablePrefix
fieldName') :: Columnar' (Nullable (QField s)) a) ->
                                forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall s ty. Bool -> TablePrefix -> TablePrefix -> QField s ty
QField Bool
q TablePrefix
tblName TablePrefix
fieldName') :: Columnar' (QField s)  a) tbl (Nullable (QField s))
lhs
        rhs' :: tbl (QExpr be s)
rhs' = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
e) :: Columnar' (Nullable (QExpr be s)) a) ->
                                forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix -> BeamSqlBackendExpressionSyntax be
e) :: Columnar' (QExpr be s) a) tbl (Nullable (QExpr be s))
rhs
    in tbl (QField s)
lhs' forall be s lhs rhs.
SqlUpdatable be s lhs rhs =>
lhs -> rhs -> QAssignment be s
<-. tbl (QExpr be s)
rhs'

-- | SQL @UNION@ operator
union_ :: forall be db s a
        . ( BeamSqlBackend be, Projectible be a
          , ThreadRewritable (QNested s) a )
       => Q be db (QNested s) a -> Q be db (QNested s) a
       -> Q be db s (WithRewrittenThread (QNested s) s a)
union_ :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a,
 ThreadRewritable (QNested s) a) =>
Q be db (QNested s) a
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
union_ (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) a
b) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be)
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> next)
-> QF be db s next
QSetOp (forall select.
IsSql92SelectTableSyntax select =>
Bool -> select -> select -> select
unionTables Bool
False) QM be db (QNested s) a
a QM be db (QNested s) a
b (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | SQL @UNION ALL@ operator
unionAll_ :: forall be db s a.
             ( BeamSqlBackend be, Projectible be a
             , ThreadRewritable (QNested s) a)
          => Q be db (QNested s) a -> Q be db (QNested s) a
          -> Q be db s (WithRewrittenThread (QNested s) s a)
unionAll_ :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a,
 ThreadRewritable (QNested s) a) =>
Q be db (QNested s) a
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
unionAll_ (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) a
b) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be)
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> next)
-> QF be db s next
QSetOp (forall select.
IsSql92SelectTableSyntax select =>
Bool -> select -> select -> select
unionTables Bool
True) QM be db (QNested s) a
a QM be db (QNested s) a
b (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | SQL @INTERSECT@ operator
intersect_ :: forall be db s a.
              ( BeamSqlBackend be, Projectible be a
              , ThreadRewritable (QNested s) a)
           => Q be db (QNested s) a -> Q be db (QNested s) a
           -> Q be db s (WithRewrittenThread (QNested s) s a)
intersect_ :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a,
 ThreadRewritable (QNested s) a) =>
Q be db (QNested s) a
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
intersect_ (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) a
b) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be)
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> next)
-> QF be db s next
QSetOp (forall select.
IsSql92SelectTableSyntax select =>
Bool -> select -> select -> select
intersectTables Bool
False) QM be db (QNested s) a
a QM be db (QNested s) a
b (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | SQL @INTERSECT ALL@ operator
intersectAll_ :: forall be db s a.
                 ( BeamSqlBackend be, Projectible be a
                 , ThreadRewritable (QNested s) a)
              => Q be db (QNested s) a -> Q be db (QNested s) a
              -> Q be db s (WithRewrittenThread (QNested s) s a)
intersectAll_ :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a,
 ThreadRewritable (QNested s) a) =>
Q be db (QNested s) a
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
intersectAll_ (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) a
b) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be)
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> next)
-> QF be db s next
QSetOp (forall select.
IsSql92SelectTableSyntax select =>
Bool -> select -> select -> select
intersectTables Bool
True) QM be db (QNested s) a
a QM be db (QNested s) a
b (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | SQL @EXCEPT@ operator
except_ :: forall be db s a.
           ( BeamSqlBackend be, Projectible be a
           , ThreadRewritable (QNested s) a)
        => Q be db (QNested s) a -> Q be db (QNested s) a
        -> Q be db s (WithRewrittenThread (QNested s) s a)
except_ :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a,
 ThreadRewritable (QNested s) a) =>
Q be db (QNested s) a
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
except_ (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) a
b) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be)
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> next)
-> QF be db s next
QSetOp (forall select.
IsSql92SelectTableSyntax select =>
Bool -> select -> select -> select
exceptTable Bool
False) QM be db (QNested s) a
a QM be db (QNested s) a
b (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | SQL @EXCEPT ALL@ operator
exceptAll_ :: forall be db s a.
              ( BeamSqlBackend be, Projectible be a
              , ThreadRewritable (QNested s) a)
           => Q be db (QNested s) a -> Q be db (QNested s) a
           -> Q be db s (WithRewrittenThread (QNested s) s a)
exceptAll_ :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a,
 ThreadRewritable (QNested s) a) =>
Q be db (QNested s) a
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
exceptAll_ (Q QM be db (QNested s) a
a) (Q QM be db (QNested s) a
b) = forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be
 -> BeamSqlBackendSelectTableSyntax be)
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> next)
-> QF be db s next
QSetOp (forall select.
IsSql92SelectTableSyntax select =>
Bool -> select -> select -> select
exceptTable Bool
True) QM be db (QNested s) a
a QM be db (QNested s) a
b (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

-- | Convenience function that allows you to use type applications to specify
--   the result of a 'QGenExpr'.
--
--   Useful to disambiguate the types of 'QGenExpr's without having to provide a
--   complete type signature. As an example, the 'countAll_' aggregate can
--   return a result of any 'Integral' type. Without further constraints, the
--   type is ambiguous. You can use 'as_' to disambiguate the return type.
--
--   For example, this is ambiguous
--
-- > aggregate_ (\_ -> countAll_) ..
--
--   But this is not
--
-- > aggregate_ (\_ -> as_ @Int32 countAll_) ..
--
as_ :: forall a ctxt be s. QGenExpr ctxt be s a -> QGenExpr ctxt be s a
as_ :: forall a ctxt be s. QGenExpr ctxt be s a -> QGenExpr ctxt be s a
as_ = forall a. a -> a
id

-- * Marshalling between Haskell literals and QExprs

type family HaskellLiteralForQExpr x = a
type instance HaskellLiteralForQExpr (QGenExpr context be s a) = a
type instance HaskellLiteralForQExpr (table (QGenExpr context be s)) = table Identity
type instance HaskellLiteralForQExpr (table (Nullable f)) = HaskellLiteralForQExpr_AddNullable (HaskellLiteralForQExpr (table f))

type family HaskellLiteralForQExpr_AddNullable x = a
type instance HaskellLiteralForQExpr_AddNullable (tbl f) = tbl (Nullable f)

type SqlValableTable be table =
   ( Beamable table
   , FieldsFulfillConstraint (HasSqlValueSyntax (BeamSqlBackendValueSyntax be)) table )

class SqlValable a where
    val_ :: HaskellLiteralForQExpr a -> a

instance ( BeamSqlBackendCanSerialize be a, BeamSqlBackend be ) =>
  SqlValable (QGenExpr ctxt be s a) where

  val_ :: HaskellLiteralForQExpr (QGenExpr ctxt be s a)
-> QGenExpr ctxt be s a
val_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax
instance ( Beamable table, BeamSqlBackend be
         , FieldsFulfillConstraint (BeamSqlBackendCanSerialize be) table ) =>
  SqlValable (table (QGenExpr ctxt be s)) where
  val_ :: HaskellLiteralForQExpr (table (QGenExpr ctxt be s))
-> table (QGenExpr ctxt be s)
val_ HaskellLiteralForQExpr (table (QGenExpr ctxt be s))
tbl =
    let fields :: table (WithConstraint (BeamSqlBackendCanSerialize be))
        fields :: table (WithConstraint (BeamSqlBackendCanSerialize be))
fields = forall (c :: * -> Constraint) (tbl :: (* -> *) -> *).
(FieldsFulfillConstraint c tbl, Beamable tbl) =>
tbl Identity -> tbl (WithConstraint c)
withConstrainedFields HaskellLiteralForQExpr (table (QGenExpr ctxt be s))
tbl
    in forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (WithConstraint a
x :: WithConstraint (BeamSqlBackendCanSerialize be) x)) ->
                         forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax a
x))))) table (WithConstraint (BeamSqlBackendCanSerialize be))
fields
instance ( Beamable table, BeamSqlBackend be
         , FieldsFulfillConstraintNullable (BeamSqlBackendCanSerialize be) table ) =>

         SqlValable (table (Nullable (QGenExpr ctxt be s))) where

  val_ :: HaskellLiteralForQExpr (table (Nullable (QGenExpr ctxt be s)))
-> table (Nullable (QGenExpr ctxt be s))
val_ HaskellLiteralForQExpr (table (Nullable (QGenExpr ctxt be s)))
tbl =
    let fields :: table (Nullable (WithConstraint (BeamSqlBackendCanSerialize be)))
        fields :: table (Nullable (WithConstraint (BeamSqlBackendCanSerialize be)))
fields = forall (c :: * -> Constraint) (tbl :: (* -> *) -> *).
(FieldsFulfillConstraintNullable c tbl, Beamable tbl) =>
tbl (Nullable Identity) -> tbl (Nullable (WithConstraint c))
withNullableConstrainedFields HaskellLiteralForQExpr (table (Nullable (QGenExpr ctxt be s)))
tbl
    in forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (WithConstraint Maybe a
x :: WithConstraint (BeamSqlBackendCanSerialize be) (Maybe x))) ->
                         forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax Maybe a
x))))) table (Nullable (WithConstraint (BeamSqlBackendCanSerialize be)))
fields

default_ :: BeamSqlBackend be => QGenExpr ctxt be s a
default_ :: forall be ctxt s a. BeamSqlBackend be => QGenExpr ctxt be s a
default_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall expr. IsSql92ExpressionSyntax expr => expr
defaultE)

-- * Window functions

noBounds_ :: QFrameBounds be
noBounds_ :: forall be. QFrameBounds be
noBounds_ = forall be.
Maybe (BeamSqlBackendWindowFrameBoundsSyntax be) -> QFrameBounds be
QFrameBounds forall a. Maybe a
Nothing

fromBound_ :: BeamSql2003ExpressionBackend be
           => QFrameBound be -> QFrameBounds be
fromBound_ :: forall be.
BeamSql2003ExpressionBackend be =>
QFrameBound be -> QFrameBounds be
fromBound_ QFrameBound be
start = forall be.
BeamSql2003ExpressionBackend be =>
QFrameBound be -> Maybe (QFrameBound be) -> QFrameBounds be
bounds_ QFrameBound be
start forall a. Maybe a
Nothing

bounds_ :: BeamSql2003ExpressionBackend be
        => QFrameBound be
        -> Maybe (QFrameBound be)
        -> QFrameBounds be
bounds_ :: forall be.
BeamSql2003ExpressionBackend be =>
QFrameBound be -> Maybe (QFrameBound be) -> QFrameBounds be
bounds_ (QFrameBound BeamSqlBackendWindowFrameBoundSyntax be
start) Maybe (QFrameBound be)
end =
    forall be.
Maybe (BeamSqlBackendWindowFrameBoundsSyntax be) -> QFrameBounds be
QFrameBounds forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
    forall bounds.
IsSql2003WindowFrameBoundsSyntax bounds =>
Sql2003WindowFrameBoundsBoundSyntax bounds
-> Maybe (Sql2003WindowFrameBoundsBoundSyntax bounds) -> bounds
fromToBoundSyntax BeamSqlBackendWindowFrameBoundSyntax be
start
      (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(QFrameBound BeamSqlBackendWindowFrameBoundSyntax be
end') -> BeamSqlBackendWindowFrameBoundSyntax be
end') Maybe (QFrameBound be)
end)

unbounded_ :: BeamSql2003ExpressionBackend be => QFrameBound be
unbounded_ :: forall be. BeamSql2003ExpressionBackend be => QFrameBound be
unbounded_ = forall be.
BeamSqlBackendWindowFrameBoundSyntax be -> QFrameBound be
QFrameBound forall bound. IsSql2003WindowFrameBoundSyntax bound => bound
unboundedSyntax

nrows_ :: BeamSql2003ExpressionBackend be
       => Int -> QFrameBound be
nrows_ :: forall be. BeamSql2003ExpressionBackend be => Int -> QFrameBound be
nrows_ Int
x = forall be.
BeamSqlBackendWindowFrameBoundSyntax be -> QFrameBound be
QFrameBound (forall bound. IsSql2003WindowFrameBoundSyntax bound => Int -> bound
nrowsBoundSyntax Int
x)

noPartition_ :: Integral a => Maybe (QExpr be s a)
noPartition_ :: forall a be s. Integral a => Maybe (QExpr be s a)
noPartition_ = forall a. Maybe a
Nothing

noOrder_ :: Integral a => Maybe (QOrd be s a)
noOrder_ :: forall a be s. Integral a => Maybe (QOrd be s a)
noOrder_ = forall a. Maybe a
Nothing

partitionBy_, orderPartitionBy_ :: partition -> Maybe partition
partitionBy_ :: forall a. a -> Maybe a
partitionBy_  = forall a. a -> Maybe a
Just
orderPartitionBy_ :: forall a. a -> Maybe a
orderPartitionBy_ = forall a. a -> Maybe a
Just

-- | Specify a window frame with all the options
frame_ :: forall be ordering partition s
        . ( BeamSql2003ExpressionBackend be
          , SqlOrderable be ordering
          , Projectible be partition )
       => Maybe partition {-^ PARTITION BY -}
       -> Maybe ordering  {-^ ORDER BY -}
       -> QFrameBounds be {-^ RANGE / ROWS -}
       -> QWindow be s
frame_ :: forall be ordering partition s.
(BeamSql2003ExpressionBackend be, SqlOrderable be ordering,
 Projectible be partition) =>
Maybe partition
-> Maybe ordering -> QFrameBounds be -> QWindow be s
frame_ Maybe partition
partition_ Maybe ordering
ordering_ (QFrameBounds Maybe (BeamSqlBackendWindowFrameBoundsSyntax be)
bounds) =
    forall be s.
WithExprContext (BeamSqlBackendWindowFrameSyntax be)
-> QWindow be s
QWindow forall a b. (a -> b) -> a -> b
$ \TablePrefix
tblPfx ->
    forall frame.
IsSql2003WindowFrameSyntax frame =>
Maybe [Sql2003WindowFrameExpressionSyntax frame]
-> Maybe [Sql2003WindowFrameOrderingSyntax frame]
-> Maybe (Sql2003WindowFrameBoundsSyntax frame)
-> frame
frameSyntax (case forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
project (forall {k} (t :: k). Proxy t
Proxy @be)) TablePrefix
tblPfx) Maybe partition
partition_ of
                   [] -> forall a. Maybe a
Nothing
                   [Sql92SelectTableExpressionSyntax
   (Sql92SelectSelectTableSyntax
      (Sql92SelectSyntax (BeamSqlBackendSyntax be)))]
xs -> forall a. a -> Maybe a
Just [Sql92SelectTableExpressionSyntax
   (Sql92SelectSelectTableSyntax
      (Sql92SelectSyntax (BeamSqlBackendSyntax be)))]
xs)
                (case forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering (forall {k} (t :: k). Proxy t
Proxy @be)) Maybe ordering
ordering_ of
                   Maybe
  [TablePrefix
   -> Sql2003WindowFrameOrderingSyntax
        (Sql2003ExpressionWindowFrameSyntax
           (Sql92SelectTableExpressionSyntax
              (Sql92SelectSelectTableSyntax
                 (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))]
Nothing -> forall a. Maybe a
Nothing
                   Just [] -> forall a. Maybe a
Nothing
                   Just [TablePrefix
 -> Sql2003WindowFrameOrderingSyntax
      (Sql2003ExpressionWindowFrameSyntax
         (Sql92SelectTableExpressionSyntax
            (Sql92SelectSelectTableSyntax
               (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))]
xs -> forall a. a -> Maybe a
Just (forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA [TablePrefix
 -> Sql2003WindowFrameOrderingSyntax
      (Sql2003ExpressionWindowFrameSyntax
         (Sql92SelectTableExpressionSyntax
            (Sql92SelectSelectTableSyntax
               (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))]
xs TablePrefix
tblPfx))
                Maybe (BeamSqlBackendWindowFrameBoundsSyntax be)
bounds

-- | Produce a window expression given an aggregate function and a window.
over_ :: BeamSql2003ExpressionBackend be
      => QAgg be s a -> QWindow be s -> QWindowExpr be s a
over_ :: forall be s a.
BeamSql2003ExpressionBackend be =>
QAgg be s a -> QWindow be s -> QWindowExpr be s a
over_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a) (QWindow WithExprContext (BeamSqlBackendWindowFrameSyntax be)
frame) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr.
IsSql2003ExpressionSyntax expr =>
expr -> Sql2003ExpressionWindowFrameSyntax expr -> expr
overE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> WithExprContext (BeamSqlBackendWindowFrameSyntax be)
frame)

-- | Compute a query over windows.
--
--   The first function builds window frames using the 'frame_', 'partitionBy_',
--   etc functions. The return type can be a single frame, tuples of frame, or
--   any arbitrarily nested tuple of the above. Instances up to 8-tuples are
--   provided.
--
--   The second function builds the resulting projection using the result of the
--   subquery as well as the window frames built in the first function. In this
--   function, window expressions can be included in the output using the
--   'over_' function.
--
withWindow_ :: forall window a s r be db
             . ( ProjectibleWithPredicate WindowFrameContext be (WithExprContext (BeamSqlBackendWindowFrameSyntax' be)) window
               , Projectible be r, Projectible be a
               , ContextRewritable a
               , ThreadRewritable (QNested s) (WithRewrittenContext a QValueContext) )
            => (r -> window)      -- ^ Window builder function
            -> (r -> window -> a) -- ^ Projection builder function. Has access to the windows generated above
            -> Q be db (QNested s) r -- ^ Query to window over
            -> Q be db s (WithRewrittenThread (QNested s) s (WithRewrittenContext a QValueContext))
withWindow_ :: forall window a s r be (db :: (* -> *) -> *).
(ProjectibleWithPredicate
   WindowFrameContext
   be
   (WithExprContext (BeamSqlBackendWindowFrameSyntax' be))
   window,
 Projectible be r, Projectible be a, ContextRewritable a,
 ThreadRewritable
   (QNested s) (WithRewrittenContext a QValueContext)) =>
(r -> window)
-> (r -> window -> a)
-> Q be db (QNested s) r
-> Q be
     db
     s
     (WithRewrittenThread
        (QNested s) s (WithRewrittenContext a QValueContext))
withWindow_ r -> window
mkWindow r -> window -> a
mkProjection (Q QM be db (QNested s) r
windowOver)=
  forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r r a (db :: (* -> *) -> *) s next.
(ProjectibleWithPredicate
   WindowFrameContext
   be
   (WithExprContext (BeamSqlBackendWindowFrameSyntax' be))
   r,
 Projectible be r, Projectible be a) =>
(r -> r)
-> (r -> r -> a)
-> QM be db (QNested s) r
-> (a -> next)
-> QF be db s next
QWindowOver r -> window
mkWindow r -> window -> a
mkProjection QM be db (QNested s) r
windowOver (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a ctxt.
ContextRewritable a =>
Proxy ctxt -> a -> WithRewrittenContext a ctxt
rewriteContext (forall {k} (t :: k). Proxy t
Proxy @QValueContext))))

-- * Order bys

class SqlOrderable be a | a -> be where
    makeSQLOrdering :: Proxy be -> a -> [ WithExprContext (BeamSqlBackendOrderingSyntax be) ]
instance SqlOrderable be (QOrd be s a) where
    makeSQLOrdering :: Proxy be
-> QOrd be s a
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
_ (QOrd WithExprContext (BeamSqlBackendOrderingSyntax be)
x) = [WithExprContext (BeamSqlBackendOrderingSyntax be)
x]
instance TypeError ('Text "Missing mandatory sorting order. Use either 'asc_' or 'desc_' to specify sorting order.") =>
    SqlOrderable be (QGenExpr ctx be s a) where
        makeSQLOrdering :: Proxy be
-> QGenExpr ctx be s a
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering = forall a. HasCallStack => [Char] -> a
error [Char]
"unreachable SqlOrderable QGenExpr instance"
instance SqlOrderable be a => SqlOrderable be [a] where
    makeSQLOrdering :: Proxy be
-> [a] -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be)
instance ( SqlOrderable be a, SqlOrderable be b ) => SqlOrderable be (a, b) where
    makeSQLOrdering :: Proxy be
-> (a, b) -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b
instance ( SqlOrderable be a, SqlOrderable be b
         , SqlOrderable be c ) => SqlOrderable be (a, b, c) where
    makeSQLOrdering :: Proxy be
-> (a, b, c) -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b, c
c) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be c
c
instance ( SqlOrderable be a, SqlOrderable be b
         , SqlOrderable be c, SqlOrderable be d ) => SqlOrderable be (a, b, c, d) where
    makeSQLOrdering :: Proxy be
-> (a, b, c, d)
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b, c
c, d
d) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be c
c forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be d
d
instance ( SqlOrderable be a, SqlOrderable be b
         , SqlOrderable be c, SqlOrderable be d
         , SqlOrderable be e ) => SqlOrderable be (a, b, c, d, e) where
    makeSQLOrdering :: Proxy be
-> (a, b, c, d, e)
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b, c
c, d
d, e
e) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be c
c forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be d
d forall a. Semigroup a => a -> a -> a
<>
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be e
e
instance ( SqlOrderable be a, SqlOrderable be b
         , SqlOrderable be c, SqlOrderable be d
         , SqlOrderable be e, SqlOrderable be f ) => SqlOrderable be (a, b, c, d, e, f) where
    makeSQLOrdering :: Proxy be
-> (a, b, c, d, e, f)
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b, c
c, d
d, e
e, f
f) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be c
c forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be d
d forall a. Semigroup a => a -> a -> a
<>
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be e
e forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be f
f
instance ( SqlOrderable be a, SqlOrderable be b
         , SqlOrderable be c, SqlOrderable be d
         , SqlOrderable be e, SqlOrderable be f
         , SqlOrderable be g ) => SqlOrderable be (a, b, c, d, e, f, g) where
    makeSQLOrdering :: Proxy be
-> (a, b, c, d, e, f, g)
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b, c
c, d
d, e
e, f
f, g
g) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be c
c forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be d
d forall a. Semigroup a => a -> a -> a
<>
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be e
e forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be f
f forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be g
g
instance ( SqlOrderable be a, SqlOrderable be b
         , SqlOrderable be c, SqlOrderable be d
         , SqlOrderable be e, SqlOrderable be f
         , SqlOrderable be g, SqlOrderable be h ) => SqlOrderable be (a, b, c, d, e, f, g, h) where
    makeSQLOrdering :: Proxy be
-> (a, b, c, d, e, f, g, h)
-> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be (a
a, b
b, c
c, d
d, e
e, f
f, g
g, h
h) =
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be a
a forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be b
b forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be c
c forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be d
d forall a. Semigroup a => a -> a -> a
<>
      forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be e
e forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be f
f forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be g
g forall a. Semigroup a => a -> a -> a
<> forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering Proxy be
be h
h

-- | Order by the given expressions. The return type of the ordering key should
--   either be the result of 'asc_' or 'desc_' (or another ordering 'QOrd'
--   generated by a backend-specific ordering) or an (possibly nested) tuple of
--   results of the former.
--
--   The <https://haskell-beam.github.io/beam/user-guide/queries/ordering manual section>
--   has more information.
orderBy_ :: forall s a ordering be db
          . ( Projectible be a, SqlOrderable be ordering
            , ThreadRewritable (QNested s) a )
         => (a -> ordering) -> Q be db (QNested s) a -> Q be db s (WithRewrittenThread (QNested s) s a)
orderBy_ :: forall s a ordering be (db :: (* -> *) -> *).
(Projectible be a, SqlOrderable be ordering,
 ThreadRewritable (QNested s) a) =>
(a -> ordering)
-> Q be db (QNested s) a
-> Q be db s (WithRewrittenThread (QNested s) s a)
orderBy_ a -> ordering
orderer (Q QM be db (QNested s) a
q) =
    forall be (db :: (* -> *) -> *) s a. QM be db s a -> Q be db s a
Q (forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
liftF (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(r -> WithExprContext [BeamSqlBackendOrderingSyntax be])
-> QM be db (QNested s) r -> (r -> next) -> QF be db s next
QOrderBy (forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall be a.
SqlOrderable be a =>
Proxy be
-> a -> [WithExprContext (BeamSqlBackendOrderingSyntax be)]
makeSQLOrdering (forall {k} (t :: k). Proxy t
Proxy @be) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ordering
orderer) QM be db (QNested s) a
q (forall s a s'.
ThreadRewritable s a =>
Proxy s' -> a -> WithRewrittenThread s s' a
rewriteThread (forall {k} (t :: k). Proxy t
Proxy @s))))

nullsFirst_ :: IsSql2003OrderingElementaryOLAPOperationsSyntax (BeamSqlBackendOrderingSyntax be)
            => QOrd be s a -> QOrd be s a
nullsFirst_ :: forall be s a.
IsSql2003OrderingElementaryOLAPOperationsSyntax
  (BeamSqlBackendOrderingSyntax be) =>
QOrd be s a -> QOrd be s a
nullsFirst_ (QOrd TablePrefix -> BeamSqlBackendOrderingSyntax be
e) = forall be s t.
(TablePrefix -> BeamSqlBackendOrderingSyntax be) -> QOrd be s t
QOrd (forall ord.
IsSql2003OrderingElementaryOLAPOperationsSyntax ord =>
ord -> ord
nullsFirstOrdering forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendOrderingSyntax be
e)

nullsLast_ :: IsSql2003OrderingElementaryOLAPOperationsSyntax (BeamSqlBackendOrderingSyntax be)
           => QOrd be s a -> QOrd be s a
nullsLast_ :: forall be s a.
IsSql2003OrderingElementaryOLAPOperationsSyntax
  (BeamSqlBackendOrderingSyntax be) =>
QOrd be s a -> QOrd be s a
nullsLast_ (QOrd TablePrefix -> BeamSqlBackendOrderingSyntax be
e) = forall be s t.
(TablePrefix -> BeamSqlBackendOrderingSyntax be) -> QOrd be s t
QOrd (forall ord.
IsSql2003OrderingElementaryOLAPOperationsSyntax ord =>
ord -> ord
nullsLastOrdering forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix -> BeamSqlBackendOrderingSyntax be
e)

-- | Produce a 'QOrd' corresponding to a SQL @ASC@ ordering
asc_ :: forall be s a
      . BeamSqlBackend be
     => QExpr be s a -> QOrd be s a
asc_ :: forall be s a. BeamSqlBackend be => QExpr be s a -> QOrd be s a
asc_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e) = forall be s t.
(TablePrefix -> BeamSqlBackendOrderingSyntax be) -> QOrd be s t
QOrd (forall ord.
IsSql92OrderingSyntax ord =>
Sql92OrderingExpressionSyntax ord -> ord
ascOrdering forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e)

-- | Produce a 'QOrd' corresponding to a SQL @DESC@ ordering
desc_ :: forall be s a
       . BeamSqlBackend be
      => QExpr be s a -> QOrd be s a
desc_ :: forall be s a. BeamSqlBackend be => QExpr be s a -> QOrd be s a
desc_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e) = forall be s t.
(TablePrefix -> BeamSqlBackendOrderingSyntax be) -> QOrd be s t
QOrd (forall ord.
IsSql92OrderingSyntax ord =>
Sql92OrderingExpressionSyntax ord -> ord
descOrdering forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e)

-- * Subqueries

-- * Nullable conversions

-- | Type class for things that can be nullable. This includes 'QExpr (Maybe a)', 'tbl (Nullable
-- QExpr)', and 'PrimaryKey tbl (Nullable QExpr)'
class SqlJustable a b | b -> a where

    -- | Given something of type 'QExpr a', 'tbl QExpr', or 'PrimaryKey tbl
    --   QExpr', turn it into a 'QExpr (Maybe a)', 'tbl (Nullable QExpr)', or
    --   'PrimaryKey t (Nullable QExpr)' respectively that contains the same
    --   values.
    just_ :: a -> b

    -- | Return either a 'QExpr (Maybe x)' representing 'Nothing' or a nullable 'Table' or
    --   'PrimaryKey' filled with 'Nothing'.
    nothing_ :: b

instance BeamSqlBackend be =>
    SqlJustable (QExpr be s a) (QExpr be s (Maybe a)) where

    just_ :: QExpr be s a -> QExpr be s (Maybe a)
just_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e
    nothing_ :: QExpr be s (Maybe a)
nothing_ = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax SqlNull
SqlNull)))

instance {-# OVERLAPPING #-} ( Table t, BeamSqlBackend be ) =>
    SqlJustable (PrimaryKey t (QExpr be s)) (PrimaryKey t (Nullable (QExpr be s))) where
    just_ :: PrimaryKey t (QExpr be s) -> PrimaryKey t (Nullable (QExpr be s))
just_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar (QExpr be s) a
q) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall a b. SqlJustable a b => a -> b
just_ Columnar (QExpr be s) a
q))
    nothing_ :: PrimaryKey t (Nullable (QExpr be s))
nothing_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar Ignored a
_) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' forall a b. SqlJustable a b => b
nothing_) (forall (table :: (* -> *) -> *) (column :: * -> *).
Table table =>
table column -> PrimaryKey table column
primaryKey (forall (table :: (* -> *) -> *).
Beamable table =>
TableSkeleton table
tblSkeleton :: TableSkeleton t))

instance {-# OVERLAPPING #-} ( Table t, BeamSqlBackend be ) =>
    SqlJustable (t (QExpr be s)) (t (Nullable (QExpr be s))) where
    just_ :: t (QExpr be s) -> t (Nullable (QExpr be s))
just_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar (QExpr be s) a
q) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall a b. SqlJustable a b => a -> b
just_ Columnar (QExpr be s) a
q))
    nothing_ :: t (Nullable (QExpr be s))
nothing_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar Ignored a
_) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' forall a b. SqlJustable a b => b
nothing_) (forall (table :: (* -> *) -> *).
Beamable table =>
TableSkeleton table
tblSkeleton :: TableSkeleton t)

instance {-# OVERLAPPING #-} Table t => SqlJustable (PrimaryKey t Identity) (PrimaryKey t (Nullable Identity)) where
    just_ :: PrimaryKey t Identity -> PrimaryKey t (Nullable Identity)
just_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar Identity a
q) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall a. a -> Maybe a
Just Columnar Identity a
q))
    nothing_ :: PrimaryKey t (Nullable Identity)
nothing_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar Ignored a
_) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' forall a. Maybe a
Nothing) (forall (table :: (* -> *) -> *) (column :: * -> *).
Table table =>
table column -> PrimaryKey table column
primaryKey (forall (table :: (* -> *) -> *).
Beamable table =>
TableSkeleton table
tblSkeleton :: TableSkeleton t))

instance {-# OVERLAPPING #-} Table t => SqlJustable (t Identity) (t (Nullable Identity)) where
    just_ :: t Identity -> t (Nullable Identity)
just_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar Identity a
q) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall a. a -> Maybe a
Just Columnar Identity a
q))
    nothing_ :: t (Nullable Identity)
nothing_ = forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' Columnar Ignored a
_) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' forall a. Maybe a
Nothing) (forall (table :: (* -> *) -> *).
Beamable table =>
TableSkeleton table
tblSkeleton :: TableSkeleton t)

-- * Nullable checking

data QIfCond context be s a = QIfCond (QGenExpr context be s SqlBool) (QGenExpr context be s a)
newtype QIfElse context be s a = QIfElse (QGenExpr context be s a)

then_ :: QGenExpr context be s Bool -> QGenExpr context be s a -> QIfCond context be s a
then_ :: forall context be s a.
QGenExpr context be s Bool
-> QGenExpr context be s a -> QIfCond context be s a
then_ QGenExpr context be s Bool
cond QGenExpr context be s a
res = forall context be s a.
QGenExpr context be s SqlBool
-> QGenExpr context be s a -> QIfCond context be s a
QIfCond (forall context syntax s.
QGenExpr context syntax s Bool -> QGenExpr context syntax s SqlBool
sqlBool_ QGenExpr context be s Bool
cond) QGenExpr context be s a
res

then_' :: QGenExpr context be s SqlBool -> QGenExpr context be s a -> QIfCond context be s a
then_' :: forall context be s a.
QGenExpr context be s SqlBool
-> QGenExpr context be s a -> QIfCond context be s a
then_' QGenExpr context be s SqlBool
cond QGenExpr context be s a
res = forall context be s a.
QGenExpr context be s SqlBool
-> QGenExpr context be s a -> QIfCond context be s a
QIfCond QGenExpr context be s SqlBool
cond QGenExpr context be s a
res

else_ :: QGenExpr context be s a -> QIfElse context be s a
else_ :: forall context be s a.
QGenExpr context be s a -> QIfElse context be s a
else_ = forall context be s a.
QGenExpr context be s a -> QIfElse context be s a
QIfElse

if_ :: BeamSqlBackend be
    => [ QIfCond context be s a ]
    -> QIfElse context be s a
    -> QGenExpr context be s a
if_ :: forall be context s a.
BeamSqlBackend be =>
[QIfCond context be s a]
-> QIfElse context be s a -> QGenExpr context be s a
if_ [QIfCond context be s a]
conds (QIfElse (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
elseExpr)) =
  forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> forall expr.
IsSql92ExpressionSyntax expr =>
[(expr, expr)] -> expr -> expr
caseE (forall a b. (a -> b) -> [a] -> [b]
map (\(QIfCond (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
cond) (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
res)) -> (TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
cond TablePrefix
tbl, TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
res TablePrefix
tbl)) [QIfCond context be s a]
conds) (TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
elseExpr TablePrefix
tbl))

ifThenElse_
  :: BeamSqlBackend be
  => QGenExpr context be s Bool
  -> QGenExpr context be s a
  -> QGenExpr context be s a
  -> QGenExpr context be s a
ifThenElse_ :: forall be context s a.
BeamSqlBackend be =>
QGenExpr context be s Bool
-> QGenExpr context be s a
-> QGenExpr context be s a
-> QGenExpr context be s a
ifThenElse_ QGenExpr context be s Bool
c QGenExpr context be s a
t QGenExpr context be s a
f = forall be context s a.
BeamSqlBackend be =>
[QIfCond context be s a]
-> QIfElse context be s a -> QGenExpr context be s a
if_ [QGenExpr context be s Bool
c forall context be s a.
QGenExpr context be s Bool
-> QGenExpr context be s a -> QIfCond context be s a
`then_` QGenExpr context be s a
t] (forall context be s a.
QGenExpr context be s a -> QIfElse context be s a
else_ QGenExpr context be s a
f)

bool_
  :: BeamSqlBackend be
  => QGenExpr context be s a
  -> QGenExpr context be s a
  -> QGenExpr context be s Bool
  -> QGenExpr context be s a
bool_ :: forall be context s a.
BeamSqlBackend be =>
QGenExpr context be s a
-> QGenExpr context be s a
-> QGenExpr context be s Bool
-> QGenExpr context be s a
bool_ QGenExpr context be s a
f QGenExpr context be s a
t QGenExpr context be s Bool
c = forall be context s a.
BeamSqlBackend be =>
QGenExpr context be s Bool
-> QGenExpr context be s a
-> QGenExpr context be s a
-> QGenExpr context be s a
ifThenElse_ QGenExpr context be s Bool
c QGenExpr context be s a
t QGenExpr context be s a
f

-- | SQL @COALESCE@ support
coalesce_ :: BeamSqlBackend be
          => [ QGenExpr ctxt be s (Maybe a) ] -> QGenExpr ctxt be s a -> QGenExpr ctxt be s a
coalesce_ :: forall be ctxt s a.
BeamSqlBackend be =>
[QGenExpr ctxt be s (Maybe a)]
-> QGenExpr ctxt be s a -> QGenExpr ctxt be s a
coalesce_ [QGenExpr ctxt be s (Maybe a)]
qs (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onNull) =
  forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr forall a b. (a -> b) -> a -> b
$ do
    Sql92UpdateExpressionSyntax
  (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
onNull' <- TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onNull
    forall expr. IsSql92ExpressionSyntax expr => [expr] -> expr
coalesceE forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<> [Sql92UpdateExpressionSyntax
  (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
onNull']) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\(QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
q) -> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
q) [QGenExpr ctxt be s (Maybe a)]
qs

-- | Convert a 'Maybe' value to a concrete value, by suppling a default
fromMaybe_ :: BeamSqlBackend be
           => QGenExpr ctxt be s a -> QGenExpr ctxt be s (Maybe a) -> QGenExpr ctxt be s a
fromMaybe_ :: forall be ctxt s a.
BeamSqlBackend be =>
QGenExpr ctxt be s a
-> QGenExpr ctxt be s (Maybe a) -> QGenExpr ctxt be s a
fromMaybe_ QGenExpr ctxt be s a
onNull QGenExpr ctxt be s (Maybe a)
q = forall be ctxt s a.
BeamSqlBackend be =>
[QGenExpr ctxt be s (Maybe a)]
-> QGenExpr ctxt be s a -> QGenExpr ctxt be s a
coalesce_ [QGenExpr ctxt be s (Maybe a)
q] QGenExpr ctxt be s a
onNull

-- | Type class for anything which can be checked for null-ness. This includes 'QExpr (Maybe a)' as
-- well as 'Table's or 'PrimaryKey's over 'Nullable QExpr'.
class BeamSqlBackend be => SqlDeconstructMaybe be a nonNullA s | a s -> be, a -> nonNullA, a -> s, nonNullA -> s where
    -- | Returns a 'QExpr' that evaluates to true when the first argument is not null
    isJust_ :: a -> QGenExpr ctxt be s Bool

    -- | Returns a 'QExpr' that evaluates to true when the first argument is null
    isNothing_ :: a -> QGenExpr ctxt be s Bool

    -- | Given an object (third argument) which may or may not be null, return the default value if
    -- null (first argument), or transform the value that could be null to yield the result of the
    -- expression (second argument)
    maybe_ :: QGenExpr ctxt be s y -> (nonNullA -> QGenExpr ctxt be s y) -> a -> QGenExpr ctxt be s y

instance BeamSqlBackend be => SqlDeconstructMaybe be (QGenExpr ctxt be s (Maybe x)) (QGenExpr ctxt be s x) s where
    isJust_ :: forall ctxt.
QGenExpr ctxt be s (Maybe x) -> QGenExpr ctxt be s Bool
isJust_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
x) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
isNotNullE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
x)
    isNothing_ :: forall ctxt.
QGenExpr ctxt be s (Maybe x) -> QGenExpr ctxt be s Bool
isNothing_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
x) = forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (forall expr. IsSql92ExpressionSyntax expr => expr -> expr
isNullE forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
x)

    maybe_ :: forall ctxt y.
QGenExpr ctxt be s y
-> (QGenExpr ctxt be s x -> QGenExpr ctxt be s y)
-> QGenExpr ctxt be s (Maybe x)
-> QGenExpr ctxt be s y
maybe_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onNothing) QGenExpr ctxt be s x -> QGenExpr ctxt be s y
onJust (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e) =
        let QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onJust' = QGenExpr ctxt be s x -> QGenExpr ctxt be s y
onJust (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e)
        in forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tbl -> forall expr.
IsSql92ExpressionSyntax expr =>
[(expr, expr)] -> expr -> expr
caseE [(forall expr. IsSql92ExpressionSyntax expr => expr -> expr
isNotNullE (TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e TablePrefix
tbl), TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onJust' TablePrefix
tbl)] (TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onNothing TablePrefix
tbl))

instance ( BeamSqlBackend be, Beamable t)
    => SqlDeconstructMaybe be (t (Nullable (QGenExpr ctxt be s))) (t (QGenExpr ctxt be s)) s where
    isJust_ :: forall ctxt.
t (Nullable (QGenExpr ctxt be s)) -> QGenExpr ctxt be s Bool
isJust_ t (Nullable (QGenExpr ctxt be s))
t = forall be context s.
BeamSqlBackend be =>
[QGenExpr context be s Bool] -> QGenExpr context be s Bool
allE (forall (table :: (* -> *) -> *) (f :: * -> *) b.
Beamable table =>
(forall a. Columnar' f a -> b) -> table f -> [b]
allBeamValues (\(Columnar' Columnar (Nullable (QGenExpr ctxt be s)) a
e) -> forall be a nonNullA s ctxt.
SqlDeconstructMaybe be a nonNullA s =>
a -> QGenExpr ctxt be s Bool
isJust_ Columnar (Nullable (QGenExpr ctxt be s)) a
e) t (Nullable (QGenExpr ctxt be s))
t)
    isNothing_ :: forall ctxt.
t (Nullable (QGenExpr ctxt be s)) -> QGenExpr ctxt be s Bool
isNothing_ t (Nullable (QGenExpr ctxt be s))
t = forall be context s.
BeamSqlBackend be =>
[QGenExpr context be s Bool] -> QGenExpr context be s Bool
allE (forall (table :: (* -> *) -> *) (f :: * -> *) b.
Beamable table =>
(forall a. Columnar' f a -> b) -> table f -> [b]
allBeamValues (\(Columnar' Columnar (Nullable (QGenExpr ctxt be s)) a
e) -> forall be a nonNullA s ctxt.
SqlDeconstructMaybe be a nonNullA s =>
a -> QGenExpr ctxt be s Bool
isNothing_ Columnar (Nullable (QGenExpr ctxt be s)) a
e) t (Nullable (QGenExpr ctxt be s))
t)
    maybe_ :: forall ctxt y.
QGenExpr ctxt be s y
-> (t (QGenExpr ctxt be s) -> QGenExpr ctxt be s y)
-> t (Nullable (QGenExpr ctxt be s))
-> QGenExpr ctxt be s y
maybe_ (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onNothing) t (QGenExpr ctxt be s) -> QGenExpr ctxt be s y
onJust t (Nullable (QGenExpr ctxt be s))
tbl =
      let QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onJust' = t (QGenExpr ctxt be s) -> QGenExpr ctxt be s y
onJust (forall (table :: (* -> *) -> *) (f :: * -> *) (g :: * -> *).
Beamable table =>
(forall a. Columnar' f a -> Columnar' g a) -> table f -> table g
changeBeamRep (\(Columnar' (QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e)) -> forall (f :: * -> *) a. Columnar f a -> Columnar' f a
Columnar' (forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e)) t (Nullable (QGenExpr ctxt be s))
tbl)
          QExpr TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
cond = forall be a nonNullA s ctxt.
SqlDeconstructMaybe be a nonNullA s =>
a -> QGenExpr ctxt be s Bool
isJust_ @be t (Nullable (QGenExpr ctxt be s))
tbl
      in forall context be s t.
(TablePrefix -> BeamSqlBackendExpressionSyntax be)
-> QGenExpr context be s t
QExpr (\TablePrefix
tblPfx -> forall expr.
IsSql92ExpressionSyntax expr =>
[(expr, expr)] -> expr -> expr
caseE [(TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
cond TablePrefix
tblPfx, TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onJust' TablePrefix
tblPfx)] (TablePrefix
-> Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
onNothing TablePrefix
tblPfx))

infixl 3 <|>.
(<|>.) :: ( SqlJustable a (QGenExpr ctxt syntax s y)
          , SqlDeconstructMaybe syntax (QGenExpr ctxt syntax s y) a s
          )
       => QGenExpr ctxt syntax s y
       -> QGenExpr ctxt syntax s y
       -> QGenExpr ctxt syntax s y
QGenExpr ctxt syntax s y
l <|>. :: forall a ctxt syntax s y.
(SqlJustable a (QGenExpr ctxt syntax s y),
 SqlDeconstructMaybe syntax (QGenExpr ctxt syntax s y) a s) =>
QGenExpr ctxt syntax s y
-> QGenExpr ctxt syntax s y -> QGenExpr ctxt syntax s y
<|>. QGenExpr ctxt syntax s y
r = forall be a nonNullA s ctxt y.
SqlDeconstructMaybe be a nonNullA s =>
QGenExpr ctxt be s y
-> (nonNullA -> QGenExpr ctxt be s y) -> a -> QGenExpr ctxt be s y
maybe_ QGenExpr ctxt syntax s y
r forall a b. SqlJustable a b => a -> b
just_ QGenExpr ctxt syntax s y
l