{-# OPTIONS_GHC -fno-warn-name-shadowing -fno-warn-unused-binds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}

module Database.Beam.Query.SQL92
    ( buildSql92Query' ) where

import           Database.Beam.Query.Internal
import           Database.Beam.Backend.SQL

import           Control.Monad.Free.Church
import           Control.Monad.Free

import           Data.Kind (Type)
import           Data.Maybe
import           Data.Proxy (Proxy(Proxy))
import           Data.String
import qualified Data.Text as T

-- * Beam queries

andE' :: IsSql92ExpressionSyntax expr =>
         Maybe expr -> Maybe expr -> Maybe expr
andE' :: forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' Maybe expr
Nothing Maybe expr
Nothing = forall a. Maybe a
Nothing
andE' (Just expr
x) Maybe expr
Nothing = forall a. a -> Maybe a
Just expr
x
andE' Maybe expr
Nothing (Just expr
y) = forall a. a -> Maybe a
Just expr
y
andE' (Just expr
x) (Just expr
y) = forall a. a -> Maybe a
Just (forall expr. IsSql92ExpressionSyntax expr => expr -> expr -> expr
andE expr
x expr
y)

newtype PreserveLeft a b = PreserveLeft { forall a b. PreserveLeft a b -> (a, b)
unPreserveLeft :: (a, b) }
instance (Monoid a, ProjectibleWithPredicate c syntax res b) => ProjectibleWithPredicate c syntax res (PreserveLeft a b) where
  project' :: forall (m :: * -> *).
Monad m =>
Proxy c
-> Proxy (syntax, res)
-> (forall context.
    c context =>
    Proxy context -> Proxy syntax -> res -> m res)
-> PreserveLeft a b
-> m (PreserveLeft a b)
project' Proxy c
context Proxy (syntax, res)
be forall context.
c context =>
Proxy context -> Proxy syntax -> res -> m res
f (PreserveLeft (a
a, b
b)) =
    forall a b. (a, b) -> PreserveLeft a b
PreserveLeft forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
a,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (contextPredicate :: * -> Constraint) be res a
       (m :: * -> *).
(ProjectibleWithPredicate contextPredicate be res a, Monad m) =>
Proxy contextPredicate
-> Proxy (be, res)
-> (forall context.
    contextPredicate context =>
    Proxy context -> Proxy be -> res -> m res)
-> a
-> m a
project' Proxy c
context Proxy (syntax, res)
be forall context.
c context =>
Proxy context -> Proxy syntax -> res -> m res
f b
b

  projectSkeleton' :: forall (m :: * -> *).
Monad m =>
Proxy c
-> Proxy (syntax, res)
-> (forall context.
    c context =>
    Proxy context -> Proxy syntax -> m res)
-> m (PreserveLeft a b)
projectSkeleton' Proxy c
ctxt Proxy (syntax, res)
be forall context. c context => Proxy context -> Proxy syntax -> m res
mkM =
    forall a b. (a, b) -> PreserveLeft a b
PreserveLeft forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Monoid a => a
mempty,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (contextPredicate :: * -> Constraint) be res a
       (m :: * -> *).
(ProjectibleWithPredicate contextPredicate be res a, Monad m) =>
Proxy contextPredicate
-> Proxy (be, res)
-> (forall context.
    contextPredicate context =>
    Proxy context -> Proxy be -> m res)
-> m a
projectSkeleton' Proxy c
ctxt Proxy (syntax, res)
be forall context. c context => Proxy context -> Proxy syntax -> m res
mkM

type SelectStmtFn be
  =  BeamSqlBackendSelectTableSyntax be
 -> [BeamSqlBackendOrderingSyntax be]
 -> Maybe Integer {-^ LIMIT -}
 -> Maybe Integer {-^ OFFSET -}
 -> BeamSqlBackendSelectSyntax be

data QueryBuilder be
  = QueryBuilder
  { forall be. QueryBuilder be -> Int
qbNextTblRef :: Int
  , forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom  :: Maybe (BeamSqlBackendFromSyntax be)
  , forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere :: Maybe (BeamSqlBackendExpressionSyntax be) }

data SelectBuilder be (db :: (Type -> Type) -> Type) a where
  SelectBuilderQ :: ( BeamSqlBackend be
                    , Projectible be a )
                 => a -> QueryBuilder be -> SelectBuilder be db a
  SelectBuilderGrouping
      :: ( BeamSqlBackend be
         , Projectible be a )
      => a -> QueryBuilder be
      -> Maybe (BeamSqlBackendGroupingSyntax be)
      -> Maybe (BeamSqlBackendExpressionSyntax be)
      -> Maybe (BeamSqlBackendSetQuantifierSyntax be)
      -> SelectBuilder be db a
  SelectBuilderSelectSyntax :: Bool {- Whether or not this contains UNION, INTERSECT, EXCEPT, etc -}
                            -> a -> BeamSqlBackendSelectTableSyntax be
                            -> SelectBuilder be db a
  SelectBuilderTopLevel ::
    { forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> Maybe Integer
sbLimit, forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> Maybe Integer
sbOffset :: Maybe Integer
    , forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> [BeamSqlBackendOrderingSyntax be]
sbOrdering        :: [ BeamSqlBackendOrderingSyntax be ]
    , forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> SelectBuilder be db a
sbTable           :: SelectBuilder be db a
    , forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> Maybe (SelectStmtFn be)
sbSelectFn        :: Maybe (SelectStmtFn be)
                        -- ^ Which 'SelectStmtFn' to use to build this select. If 'Nothing', use the default
    } -> SelectBuilder be db a

sbContainsSetOperation :: SelectBuilder syntax db a -> Bool
sbContainsSetOperation :: forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> Bool
sbContainsSetOperation (SelectBuilderSelectSyntax Bool
contains a
_ BeamSqlBackendSelectTableSyntax syntax
_) = Bool
contains
sbContainsSetOperation (SelectBuilderTopLevel { sbTable :: forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> SelectBuilder be db a
sbTable = SelectBuilder syntax db a
tbl }) = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> Bool
sbContainsSetOperation SelectBuilder syntax db a
tbl
sbContainsSetOperation SelectBuilder syntax db a
_ = Bool
False

fieldNameFunc :: IsSql92ExpressionSyntax expr =>
                 (T.Text -> Sql92ExpressionFieldNameSyntax expr) -> Int
              -> expr
fieldNameFunc :: forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc TablePrefix -> Sql92ExpressionFieldNameSyntax expr
mkField Int
i = forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionFieldNameSyntax expr -> expr
fieldE (TablePrefix -> Sql92ExpressionFieldNameSyntax expr
mkField (TablePrefix
"res" forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
i)))

nextTblPfx :: TablePrefix -> TablePrefix
nextTblPfx :: TablePrefix -> TablePrefix
nextTblPfx = (TablePrefix
"sub_" forall a. Semigroup a => a -> a -> a
<>)

defaultProjection :: Projectible be x
                  => Proxy be -> TablePrefix -> x -> [ ( BeamSqlBackendExpressionSyntax be , Maybe T.Text ) ]
defaultProjection :: forall be x.
Projectible be x =>
Proxy be
-> TablePrefix
-> x
-> [(BeamSqlBackendExpressionSyntax be, Maybe TablePrefix)]
defaultProjection Proxy be
be TablePrefix
pfx =
    forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Integer
i Sql92SelectTableExpressionSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e -> (Sql92SelectTableExpressionSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
e, forall a. a -> Maybe a
Just (forall a. IsString a => String -> a
fromString String
"res" forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show (Integer
i :: Integer)))))
            [Integer
0..] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
project Proxy be
be) (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
pfx)

buildSelect :: forall be db a
             . ( BeamSqlBackend be, Projectible be a )
            => TablePrefix -> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect :: forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
_ (SelectBuilderTopLevel Maybe Integer
limit Maybe Integer
offset [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilderSelectSyntax Bool
_ a
_ Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
table) Maybe (SelectStmtFn be)
selectStmt') =
    (forall a. a -> Maybe a -> a
fromMaybe forall select.
IsSql92SelectSyntax select =>
Sql92SelectSelectTableSyntax select
-> [Sql92SelectOrderingSyntax select]
-> Maybe Integer
-> Maybe Integer
-> select
selectStmt Maybe (SelectStmtFn be)
selectStmt') Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
table [BeamSqlBackendOrderingSyntax be]
ordering Maybe Integer
limit Maybe Integer
offset
buildSelect TablePrefix
pfx (SelectBuilderTopLevel Maybe Integer
limit Maybe Integer
offset [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilderQ a
proj (QueryBuilder Int
_ Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_)) Maybe (SelectStmtFn be)
selectStmt') =
    (forall a. a -> Maybe a -> a
fromMaybe forall select.
IsSql92SelectSyntax select =>
Sql92SelectSelectTableSyntax select
-> [Sql92SelectOrderingSyntax select]
-> Maybe Integer
-> Maybe Integer
-> select
selectStmt Maybe (SelectStmtFn be)
selectStmt') (forall select.
IsSql92SelectTableSyntax select =>
Maybe (Sql92SelectTableSetQuantifierSyntax select)
-> Sql92SelectTableProjectionSyntax select
-> Maybe (Sql92SelectTableFromSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> Maybe (Sql92SelectTableGroupingSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> select
selectTableStmt forall a. Maybe a
Nothing (forall proj.
IsSql92ProjectionSyntax proj =>
[(Sql92ProjectionExpressionSyntax proj, Maybe TablePrefix)] -> proj
projExprs (forall be x.
Projectible be x =>
Proxy be
-> TablePrefix
-> x
-> [(BeamSqlBackendExpressionSyntax be, Maybe TablePrefix)]
defaultProjection (forall {k} (t :: k). Proxy t
Proxy @be) TablePrefix
pfx a
proj)) Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_ forall a. Maybe a
Nothing forall a. Maybe a
Nothing) [BeamSqlBackendOrderingSyntax be]
ordering Maybe Integer
limit Maybe Integer
offset
buildSelect TablePrefix
pfx (SelectBuilderTopLevel Maybe Integer
limit Maybe Integer
offset [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilderGrouping a
proj (QueryBuilder Int
_ Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_) Maybe (BeamSqlBackendGroupingSyntax be)
grouping Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having Maybe (BeamSqlBackendSetQuantifierSyntax be)
distinct) Maybe (SelectStmtFn be)
selectStmt') =
    (forall a. a -> Maybe a -> a
fromMaybe forall select.
IsSql92SelectSyntax select =>
Sql92SelectSelectTableSyntax select
-> [Sql92SelectOrderingSyntax select]
-> Maybe Integer
-> Maybe Integer
-> select
selectStmt Maybe (SelectStmtFn be)
selectStmt') (forall select.
IsSql92SelectTableSyntax select =>
Maybe (Sql92SelectTableSetQuantifierSyntax select)
-> Sql92SelectTableProjectionSyntax select
-> Maybe (Sql92SelectTableFromSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> Maybe (Sql92SelectTableGroupingSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> select
selectTableStmt Maybe (BeamSqlBackendSetQuantifierSyntax be)
distinct (forall proj.
IsSql92ProjectionSyntax proj =>
[(Sql92ProjectionExpressionSyntax proj, Maybe TablePrefix)] -> proj
projExprs (forall be x.
Projectible be x =>
Proxy be
-> TablePrefix
-> x
-> [(BeamSqlBackendExpressionSyntax be, Maybe TablePrefix)]
defaultProjection (forall {k} (t :: k). Proxy t
Proxy @be) TablePrefix
pfx a
proj)) Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_ Maybe (BeamSqlBackendGroupingSyntax be)
grouping Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having) [BeamSqlBackendOrderingSyntax be]
ordering Maybe Integer
limit Maybe Integer
offset
buildSelect TablePrefix
pfx SelectBuilder be db a
x = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
pfx (forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [] SelectBuilder be db a
x forall a. Maybe a
Nothing)

selectBuilderToTableSource :: forall be db a
                            . ( BeamSqlBackend be, Projectible be a )
                           => TablePrefix -> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource :: forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource TablePrefix
_ (SelectBuilderSelectSyntax Bool
_ a
_ Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
x) = Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
x
selectBuilderToTableSource TablePrefix
pfx (SelectBuilderQ a
x (QueryBuilder Int
_ Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_)) =
  forall select.
IsSql92SelectTableSyntax select =>
Maybe (Sql92SelectTableSetQuantifierSyntax select)
-> Sql92SelectTableProjectionSyntax select
-> Maybe (Sql92SelectTableFromSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> Maybe (Sql92SelectTableGroupingSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> select
selectTableStmt forall a. Maybe a
Nothing (forall proj.
IsSql92ProjectionSyntax proj =>
[(Sql92ProjectionExpressionSyntax proj, Maybe TablePrefix)] -> proj
projExprs (forall be x.
Projectible be x =>
Proxy be
-> TablePrefix
-> x
-> [(BeamSqlBackendExpressionSyntax be, Maybe TablePrefix)]
defaultProjection (forall {k} (t :: k). Proxy t
Proxy @be) TablePrefix
pfx a
x)) Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_ forall a. Maybe a
Nothing forall a. Maybe a
Nothing
selectBuilderToTableSource TablePrefix
pfx (SelectBuilderGrouping a
x (QueryBuilder Int
_ Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_) Maybe (BeamSqlBackendGroupingSyntax be)
grouping Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having Maybe (BeamSqlBackendSetQuantifierSyntax be)
distinct) =
  forall select.
IsSql92SelectTableSyntax select =>
Maybe (Sql92SelectTableSetQuantifierSyntax select)
-> Sql92SelectTableProjectionSyntax select
-> Maybe (Sql92SelectTableFromSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> Maybe (Sql92SelectTableGroupingSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> select
selectTableStmt Maybe (BeamSqlBackendSetQuantifierSyntax be)
distinct (forall proj.
IsSql92ProjectionSyntax proj =>
[(Sql92ProjectionExpressionSyntax proj, Maybe TablePrefix)] -> proj
projExprs (forall be x.
Projectible be x =>
Proxy be
-> TablePrefix
-> x
-> [(BeamSqlBackendExpressionSyntax be, Maybe TablePrefix)]
defaultProjection (forall {k} (t :: k). Proxy t
Proxy @be) TablePrefix
pfx a
x)) Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_ Maybe (BeamSqlBackendGroupingSyntax be)
grouping Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having
selectBuilderToTableSource TablePrefix
pfx SelectBuilder be db a
sb =
    let (a
x, QueryBuilder Int
_ Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
pfx SelectBuilder be db a
sb
    in forall select.
IsSql92SelectTableSyntax select =>
Maybe (Sql92SelectTableSetQuantifierSyntax select)
-> Sql92SelectTableProjectionSyntax select
-> Maybe (Sql92SelectTableFromSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> Maybe (Sql92SelectTableGroupingSyntax select)
-> Maybe (Sql92SelectTableExpressionSyntax select)
-> select
selectTableStmt forall a. Maybe a
Nothing (forall proj.
IsSql92ProjectionSyntax proj =>
[(Sql92ProjectionExpressionSyntax proj, Maybe TablePrefix)] -> proj
projExprs (forall be x.
Projectible be x =>
Proxy be
-> TablePrefix
-> x
-> [(BeamSqlBackendExpressionSyntax be, Maybe TablePrefix)]
defaultProjection (forall {k} (t :: k). Proxy t
Proxy @be) TablePrefix
pfx a
x)) Maybe (BeamSqlBackendFromSyntax be)
from Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
where_ forall a. Maybe a
Nothing forall a. Maybe a
Nothing

selectBuilderToQueryBuilder :: forall be db a
                             . ( BeamSqlBackend be, Projectible be a)
                            => TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder :: forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
pfx SelectBuilder be db a
sb =
    let select :: Sql92SelectSyntax (BeamSqlBackendSyntax be)
select = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
pfx SelectBuilder be db a
sb
        x' :: a
x' = forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject (forall {k} (t :: k). Proxy t
Proxy @be) (forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
t0)) (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db a
sb)
        t0 :: TablePrefix
t0 = TablePrefix
pfx forall a. Semigroup a => a -> a -> a
<> TablePrefix
"0"
    in (a
x', forall be.
Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
QueryBuilder Int
1 (forall a. a -> Maybe a
Just (forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92SelectSyntax (BeamSqlBackendSyntax be)
select) (forall a. a -> Maybe a
Just (TablePrefix
t0, forall a. Maybe a
Nothing)))) forall a. Maybe a
Nothing)

emptyQb :: QueryBuilder select
emptyQb :: forall select. QueryBuilder select
emptyQb = forall be.
Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
QueryBuilder Int
0 forall a. Maybe a
Nothing forall a. Maybe a
Nothing

sbProj :: SelectBuilder syntax db a -> a
sbProj :: forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj (SelectBuilderQ a
proj QueryBuilder syntax
_) = a
proj
sbProj (SelectBuilderGrouping a
proj QueryBuilder syntax
_ Maybe (BeamSqlBackendGroupingSyntax syntax)
_ Maybe (BeamSqlBackendExpressionSyntax syntax)
_ Maybe (BeamSqlBackendSetQuantifierSyntax syntax)
_) = a
proj
sbProj (SelectBuilderSelectSyntax Bool
_ a
proj BeamSqlBackendSelectTableSyntax syntax
_) = a
proj
sbProj (SelectBuilderTopLevel Maybe Integer
_ Maybe Integer
_ [BeamSqlBackendOrderingSyntax syntax]
_ SelectBuilder syntax db a
sb Maybe (SelectStmtFn syntax)
_) = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder syntax db a
sb

setSelectBuilderProjection :: Projectible be b
                           => SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection :: forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection (SelectBuilderQ a
_ QueryBuilder be
q) b
proj = forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ b
proj QueryBuilder be
q
setSelectBuilderProjection (SelectBuilderGrouping a
_ QueryBuilder be
q Maybe (BeamSqlBackendGroupingSyntax be)
grouping Maybe (BeamSqlBackendExpressionSyntax be)
having Maybe (BeamSqlBackendSetQuantifierSyntax be)
d) b
proj = forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db a
SelectBuilderGrouping b
proj QueryBuilder be
q Maybe (BeamSqlBackendGroupingSyntax be)
grouping Maybe (BeamSqlBackendExpressionSyntax be)
having Maybe (BeamSqlBackendSetQuantifierSyntax be)
d
setSelectBuilderProjection (SelectBuilderSelectSyntax Bool
containsSetOp a
_ BeamSqlBackendSelectTableSyntax be
q) b
proj = forall a be (db :: (* -> *) -> *).
Bool
-> a -> BeamSqlBackendSelectTableSyntax be -> SelectBuilder be db a
SelectBuilderSelectSyntax Bool
containsSetOp b
proj BeamSqlBackendSelectTableSyntax be
q
setSelectBuilderProjection (SelectBuilderTopLevel Maybe Integer
limit Maybe Integer
offset [BeamSqlBackendOrderingSyntax be]
ord SelectBuilder be db a
sb Maybe (SelectStmtFn be)
s) b
proj =
    forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
limit Maybe Integer
offset [BeamSqlBackendOrderingSyntax be]
ord (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db a
sb b
proj) Maybe (SelectStmtFn be)
s

limitSelectBuilder, offsetSelectBuilder :: Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
limitSelectBuilder :: forall syntax (db :: (* -> *) -> *) a.
Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
limitSelectBuilder Integer
limit (SelectBuilderTopLevel Maybe Integer
limit' Maybe Integer
offset [BeamSqlBackendOrderingSyntax syntax]
ordering SelectBuilder syntax db a
tbl Maybe (SelectStmtFn syntax)
build) =
    forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
limit (forall a. Ord a => a -> a -> a
min Integer
limit) Maybe Integer
limit') Maybe Integer
offset [BeamSqlBackendOrderingSyntax syntax]
ordering SelectBuilder syntax db a
tbl Maybe (SelectStmtFn syntax)
build
limitSelectBuilder Integer
limit SelectBuilder syntax db a
x = forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (forall a. a -> Maybe a
Just Integer
limit) forall a. Maybe a
Nothing [] SelectBuilder syntax db a
x forall a. Maybe a
Nothing

offsetSelectBuilder :: forall syntax (db :: (* -> *) -> *) a.
Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
offsetSelectBuilder Integer
offset (SelectBuilderTopLevel Maybe Integer
Nothing Maybe Integer
offset' [BeamSqlBackendOrderingSyntax syntax]
ordering SelectBuilder syntax db a
tbl Maybe (SelectStmtFn syntax)
build) =
    forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer
offset forall a. Num a => a -> a -> a
+ forall a. a -> Maybe a -> a
fromMaybe Integer
0 Maybe Integer
offset') [BeamSqlBackendOrderingSyntax syntax]
ordering SelectBuilder syntax db a
tbl Maybe (SelectStmtFn syntax)
build
offsetSelectBuilder Integer
offset (SelectBuilderTopLevel (Just Integer
limit) Maybe Integer
offset' [BeamSqlBackendOrderingSyntax syntax]
ordering SelectBuilder syntax db a
tbl Maybe (SelectStmtFn syntax)
build) =
    forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
max Integer
0 (Integer
limit forall a. Num a => a -> a -> a
- Integer
offset)) (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer
offset forall a. Num a => a -> a -> a
+ forall a. a -> Maybe a -> a
fromMaybe Integer
0 Maybe Integer
offset') [BeamSqlBackendOrderingSyntax syntax]
ordering SelectBuilder syntax db a
tbl Maybe (SelectStmtFn syntax)
build
offsetSelectBuilder Integer
offset SelectBuilder syntax db a
x = forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just Integer
offset) [] SelectBuilder syntax db a
x forall a. Maybe a
Nothing

exprWithContext :: TablePrefix -> WithExprContext a -> a
exprWithContext :: forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
pfx = (forall a b. (a -> b) -> a -> b
$ TablePrefix -> TablePrefix
nextTblPfx TablePrefix
pfx)

buildJoinTableSourceQuery
  :: forall be x
   . ( BeamSqlBackend be, Projectible be x )
  => TablePrefix -> BeamSqlBackendSelectSyntax be
  -> x -> QueryBuilder be
  -> (x, QueryBuilder be)
buildJoinTableSourceQuery :: forall be x.
(BeamSqlBackend be, Projectible be x) =>
TablePrefix
-> BeamSqlBackendSelectSyntax be
-> x
-> QueryBuilder be
-> (x, QueryBuilder be)
buildJoinTableSourceQuery TablePrefix
tblPfx Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource x
x QueryBuilder be
qb =
  let qb' :: QueryBuilder be
qb' = forall be.
Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
QueryBuilder (Int
tblRef forall a. Num a => a -> a -> a
+ Int
1) Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from' (forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb)
      !tblRef :: Int
tblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb
      from' :: Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from' = case forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb of
                Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
Nothing -> forall a. a -> Maybe a
Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource
                Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom -> forall a. a -> Maybe a
Just (forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
innerJoin Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource forall a. Maybe a
Nothing)
      newSource :: Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource = forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource) (forall a. a -> Maybe a
Just (TablePrefix
newTblNm, forall a. Maybe a
Nothing))
      newTblNm :: TablePrefix
newTblNm = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
tblRef)
  in (forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject (forall {k} (t :: k). Proxy t
Proxy @be) (forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
newTblNm)) x
x, QueryBuilder be
qb')

buildInnerJoinQuery
  :: forall be r
   . BeamSqlBackend be
  => TablePrefix -> (TablePrefix -> T.Text -> BeamSqlBackendFromSyntax be)
  -> (T.Text -> r)
  -> (r-> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
  -> QueryBuilder be -> (T.Text, r, QueryBuilder be)
buildInnerJoinQuery :: forall be r.
BeamSqlBackend be =>
TablePrefix
-> (TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> QueryBuilder be
-> (TablePrefix, r, QueryBuilder be)
buildInnerJoinQuery TablePrefix
tblPfx TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkFrom TablePrefix -> r
mkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn QueryBuilder be
qb =
  let qb' :: QueryBuilder be
qb' = forall be.
Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
QueryBuilder (Int
tblRef forall a. Num a => a -> a -> a
+ Int
1) Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from' Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where'
      tblRef :: Int
tblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb
      newTblNm :: TablePrefix
newTblNm = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
tblRef)
      newSource :: Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource = TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkFrom (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx) TablePrefix
newTblNm
      (Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from', Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where') =
        case forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb of
          Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
Nothing -> (forall a. a -> Maybe a
Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource, forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' (forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb) (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn r
newTbl))
          Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom -> (forall a. a -> Maybe a
Just (forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
innerJoin Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn r
newTbl)), forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb)

      newTbl :: r
newTbl = TablePrefix -> r
mkTbl TablePrefix
newTblNm
  in (TablePrefix
newTblNm, r
newTbl, QueryBuilder be
qb')

nextTbl :: BeamSqlBackend be
        => QueryBuilder be -> TablePrefix
        -> (T.Text -> r)
        -> ( r, T.Text, QueryBuilder be )
nextTbl :: forall be r.
BeamSqlBackend be =>
QueryBuilder be
-> TablePrefix
-> (TablePrefix -> r)
-> (r, TablePrefix, QueryBuilder be)
nextTbl QueryBuilder be
qb TablePrefix
tblPfx TablePrefix -> r
mkTbl =
  let tblRef :: Int
tblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb
      newTblNm :: TablePrefix
newTblNm = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
tblRef)
      newTbl :: r
newTbl = TablePrefix -> r
mkTbl TablePrefix
newTblNm
  in (r
newTbl, TablePrefix
newTblNm, QueryBuilder be
qb { qbNextTblRef :: Int
qbNextTblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb forall a. Num a => a -> a -> a
+ Int
1})

projOrder :: Projectible be x
          => Proxy be -> x -> WithExprContext [ BeamSqlBackendExpressionSyntax be ]
projOrder :: forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
projOrder = forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
project -- (Proxy @AnyType) (\_ x -> tell [x] >> pure x)

-- | Convenience functions to construct an arbitrary SQL92 select syntax type
-- from a 'Q'. Used by most backends as the default implementation of
-- 'buildSqlQuery' in 'HasQBuilder'.
buildSql92Query' :: forall be db s a
                  . ( BeamSqlBackend be, Projectible be a)
                 => Bool {-^ Whether this backend supports arbitrary nested UNION, INTERSECT, EXCEPT -}
                 -> T.Text {-^ Table prefix -}
                 -> Q be db s a
                 -> BeamSqlBackendSelectSyntax be
buildSql92Query' :: forall be (db :: (* -> *) -> *) s a.
(BeamSqlBackend be, Projectible be a) =>
Bool -> TablePrefix -> Q be db s a -> BeamSqlBackendSelectSyntax be
buildSql92Query' Bool
arbitrarilyNestedCombinations TablePrefix
baseTblPfx (Q QM be db s a
q) =
    forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
baseTblPfx (forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
baseTblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db s a
q))
  where
    be :: Proxy be
    be :: Proxy be
be = forall {k} (t :: k). Proxy t
Proxy

    buildQuery :: forall s x
                . Projectible be x
               => T.Text
               -> Free (QF be db s) x
               -> SelectBuilder be db x
    buildQuery :: forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
_ (Pure x
x) = forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ x
x forall select. QueryBuilder select
emptyQb
    buildQuery TablePrefix
tblPfx (Free (QGuard WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
_ Free (QF be db s) x
next)) = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx Free (QF be db s) x
next
    buildQuery TablePrefix
tblPfx f :: Free (QF be db s) x
f@(Free QAll {}) = forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx Free (QF be db s) x
f forall select. QueryBuilder select
emptyQb
    buildQuery TablePrefix
tblPfx f :: Free (QF be db s) x
f@(Free QArbitraryJoin {}) = forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx Free (QF be db s) x
f forall select. QueryBuilder select
emptyQb
    buildQuery TablePrefix
tblPfx f :: Free (QF be db s) x
f@(Free QTwoWayJoin {}) = forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx Free (QF be db s) x
f forall select. QueryBuilder select
emptyQb
    buildQuery TablePrefix
tblPfx (Free (QSubSelect QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) =
        let sb :: SelectBuilder be db r
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q')
            (r
proj, QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb
        in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
proj) QueryBuilder be
qb
    buildQuery TablePrefix
tblPfx (Free (QDistinct r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be)
nubType QM be db s r
q' r -> Free (QF be db s) x
next)) =
      let (r
proj, QueryBuilder be
qb, Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
gp, Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
hv) =
            case forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db s r
q') of
              SelectBuilderQ r
proj QueryBuilder be
qb ->
                ( r
proj, QueryBuilder be
qb, forall a. Maybe a
Nothing, forall a. Maybe a
Nothing)
              SelectBuilderGrouping r
proj QueryBuilder be
qb Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
gp Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
hv Maybe (BeamSqlBackendSetQuantifierSyntax be)
Nothing ->
                ( r
proj, QueryBuilder be
qb, Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
gp, Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
hv)
              SelectBuilder be db r
sb ->
                let (r
proj, QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb
                in ( r
proj, QueryBuilder be
qb, forall a. Maybe a
Nothing, forall a. Maybe a
Nothing)
      in case r -> Free (QF be db s) x
next r
proj of
           Pure x
x -> forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db a
SelectBuilderGrouping x
x QueryBuilder be
qb Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
gp Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
hv (forall a. a -> Maybe a
Just (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx (r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be)
nubType r
proj)))
           Free (QF be db s) x
_ -> let ( r
proj', QueryBuilder be
qb' ) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx (forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db a
SelectBuilderGrouping r
proj QueryBuilder be
qb Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
gp Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
hv (forall a. a -> Maybe a
Just (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx (r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be)
nubType r
proj))))
                in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
proj') QueryBuilder be
qb'
    buildQuery TablePrefix
tblPfx (Free (QAggregate a
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    grouping)
mkAgg QM be db (QNested s) a
q' grouping -> Free (QF be db s) x
next)) =
        let sb :: SelectBuilder be db a
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) a
q')
            (Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax, grouping
aggProj) = a
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    grouping)
mkAgg (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db a
sb) (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx)
        in case forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (x,
      Maybe
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryBuildGuardsOnly TablePrefix
tblPfx (grouping -> Free (QF be db s) x
next grouping
aggProj) forall a. Maybe a
Nothing of
            Just (x
proj, Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having) ->
                case SelectBuilder be db a
sb of
                  SelectBuilderQ a
_ QueryBuilder be
q'' -> forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db a
SelectBuilderGrouping x
proj QueryBuilder be
q'' Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having forall a. Maybe a
Nothing

                  -- We'll have to generate a subselect
                  SelectBuilder be db a
_ -> let (a
subProj, QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db a
sb --(setSelectBuilderProjection sb aggProj)
                           (Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax, grouping
aggProj') = a
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    grouping)
mkAgg a
subProj (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx)
                       in case forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (x,
      Maybe
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryBuildGuardsOnly TablePrefix
tblPfx (grouping -> Free (QF be db s) x
next grouping
aggProj') forall a. Maybe a
Nothing of
                            Maybe
  (x,
   Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
Nothing -> forall a. HasCallStack => String -> a
error String
"buildQuery (Free (QAggregate ...)): Impossible"
                            Just (x
aggProj'', Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having') ->
                              forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db a
SelectBuilderGrouping x
aggProj'' QueryBuilder be
qb Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having' forall a. Maybe a
Nothing
            Maybe
  (x,
   Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
Nothing ->
              let (Free (QF be db s) x
_, Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having) = forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> (Free (QF be db s) x,
    Maybe
      (Sql92SelectTableExpressionSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryCollectHaving TablePrefix
tblPfx (grouping -> Free (QF be db s) x
next grouping
aggProj') forall a. Maybe a
Nothing
                  (Free (QF be db s) x
next', Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
_) = forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> (Free (QF be db s) x,
    Maybe
      (Sql92SelectTableExpressionSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryCollectHaving TablePrefix
tblPfx (grouping -> Free (QF be db s) x
next grouping
x') forall a. Maybe a
Nothing
                  (Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax', grouping
aggProj', QueryBuilder be
qb) =
                    case SelectBuilder be db a
sb of
                      SelectBuilderQ a
_ QueryBuilder be
q'' -> (Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax, grouping
aggProj, QueryBuilder be
q'')
                      SelectBuilder be db a
_ -> let (a
proj', QueryBuilder be
qb''') = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db a
sb
                               (Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax', grouping
aggProj') = a
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    grouping)
mkAgg a
proj' (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx)
                           in (Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax', grouping
aggProj', QueryBuilder be
qb''')
                  (grouping
x', QueryBuilder be
qb') = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx forall a b. (a -> b) -> a -> b
$
                              forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db a
SelectBuilderGrouping grouping
aggProj' QueryBuilder be
qb Maybe
  (Sql92SelectTableGroupingSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
groupingSyntax' Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having forall a. Maybe a
Nothing
              in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx Free (QF be db s) x
next' QueryBuilder be
qb'

    buildQuery TablePrefix
tblPfx (Free (QOrderBy r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) =
        let sb :: SelectBuilder be db r
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q')
            proj :: r
proj = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb
            ordering :: [BeamSqlBackendOrderingSyntax be]
ordering = forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx (r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering r
proj)

            doJoined :: SelectBuilder be db x
doJoined =
                let sb' :: SelectBuilder be db r
sb' = case SelectBuilder be db r
sb of
                            SelectBuilderQ {} ->
                                forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb forall a. Maybe a
Nothing
                            SelectBuilderGrouping {} ->
                                forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb forall a. Maybe a
Nothing
                            SelectBuilderSelectSyntax {} ->
                                forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb forall a. Maybe a
Nothing
                            SelectBuilderTopLevel Maybe Integer
Nothing Maybe Integer
Nothing [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                                forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build
                            SelectBuilderTopLevel Maybe Integer
Nothing (Just Integer
0) [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                                forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just Integer
0) [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build
                            SelectBuilderTopLevel {}
                                | (r
proj'', QueryBuilder be
qb) <- forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb ->
                                    forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx (r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering r
proj'')) (forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ r
proj'' QueryBuilder be
qb) forall a. Maybe a
Nothing
                                | Bool
otherwise -> forall a. HasCallStack => String -> a
error String
"buildQuery (Free (QOrderBy ...)): query inspected expression"

                    (r
joinedProj, QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb'
                in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
joinedProj) QueryBuilder be
qb
        in case r -> Free (QF be db s) x
next r
proj of
             Pure x
proj' ->
               case [BeamSqlBackendOrderingSyntax be]
ordering of
                 [] -> forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
proj'
                 [BeamSqlBackendOrderingSyntax be]
ordering ->
                     case SelectBuilder be db r
sb of
                       SelectBuilderQ {} ->
                           forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
proj') forall a. Maybe a
Nothing
                       SelectBuilderGrouping {} ->
                           forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
proj') forall a. Maybe a
Nothing
                       SelectBuilderSelectSyntax {} ->
                           forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
proj') forall a. Maybe a
Nothing
                       SelectBuilderTopLevel Maybe Integer
Nothing Maybe Integer
Nothing [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                           forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb' x
proj') Maybe (SelectStmtFn be)
build
                       SelectBuilderTopLevel (Just Integer
0) (Just Integer
0) [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                           forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (forall a. a -> Maybe a
Just Integer
0) (forall a. a -> Maybe a
Just Integer
0) [BeamSqlBackendOrderingSyntax be]
ordering (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb' x
proj') Maybe (SelectStmtFn be)
build
                       SelectBuilderTopLevel {}
                           | (r
proj'', QueryBuilder be
qb) <- forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb,
                             Pure x
proj''' <- r -> Free (QF be db s) x
next r
proj'' ->
                               forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx (r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering r
proj'')) (forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ x
proj''' QueryBuilder be
qb) forall a. Maybe a
Nothing
                           | Bool
otherwise -> forall a. HasCallStack => String -> a
error String
"buildQuery (Free (QOrderBy ...)): query inspected expression"
             Free (QF be db s) x
_ -> SelectBuilder be db x
doJoined

    buildQuery TablePrefix
tblPfx (Free (QWindowOver r -> window
mkWindows r -> window -> a
mkProjection QM be db (QNested s) r
q' a -> Free (QF be db s) x
next)) =
        let sb :: SelectBuilder be db r
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q')

            x :: r
x = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb
            windows :: window
windows = r -> window
mkWindows r
x
            projection :: a
projection = r -> window -> a
mkProjection r
x window
windows
        in case a -> Free (QF be db s) x
next a
projection of
             Pure x
x' ->
               -- Windowing makes this automatically a top-level (this prevents aggregates from being added directly)
               case forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
x' of
                 sb' :: SelectBuilder be db x
sb'@SelectBuilderTopLevel {} -> SelectBuilder be db x
sb'
                 SelectBuilder be db x
sb' -> forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [] SelectBuilder be db x
sb' forall a. Maybe a
Nothing
             Free (QF be db s) x
_       ->
               let (a
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx (forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb a
projection)
               in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (a -> Free (QF be db s) x
next a
x') QueryBuilder be
qb

    buildQuery TablePrefix
tblPfx (Free (QLimit Integer
limit QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) =
        let sb :: SelectBuilder be db r
sb = forall syntax (db :: (* -> *) -> *) a.
Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
limitSelectBuilder Integer
limit (forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q'))
            x :: r
x = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb
        -- In the case of limit, we must directly return whatever was given
        in case r -> Free (QF be db s) x
next r
x of
             Pure x
x' -> forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
x'

             -- Otherwise, this is going to be part of a join...
             Free (QF be db s) x
_ -> let (r
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb
                  in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
x') QueryBuilder be
qb

    buildQuery TablePrefix
tblPfx (Free (QOffset Integer
offset QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) =
        let sb :: SelectBuilder be db r
sb = forall syntax (db :: (* -> *) -> *) a.
Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
offsetSelectBuilder Integer
offset (forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q'))
            x :: r
x = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb
        -- In the case of limit, we must directly return whatever was given
        in case r -> Free (QF be db s) x
next r
x of
             Pure x
x' -> forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb x
x'
             -- Otherwise, this is going to be part of a join...
             Free (QF be db s) x
_ -> let (r
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb
                  in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
x') QueryBuilder be
qb

    buildQuery TablePrefix
tblPfx (Free (QSetOp Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
combine QM be db (QNested s) r
left QM be db (QNested s) r
right r -> Free (QF be db s) x
next)) =
      forall s x r.
(Projectible be r, Projectible be x) =>
TablePrefix
-> (Sql92SelectSelectTableSyntax
      (Sql92SelectSyntax (BeamSqlBackendSyntax be))
    -> Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be))
    -> Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> QM be db (QNested s) x
-> QM be db (QNested s) x
-> (x -> Free (QF be db s) r)
-> SelectBuilder be db r
buildTableCombination TablePrefix
tblPfx Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
combine QM be db (QNested s) r
left QM be db (QNested s) r
right r -> Free (QF be db s) x
next

    buildQuery TablePrefix
tblPfx (Free (QForceSelect r -> SelectStmtFn be
selectStmt' QM be db (QNested s) r
over r -> Free (QF be db s) x
next)) =
      let sb :: SelectBuilder be db r
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
over)
          x :: r
x = forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb

          selectStmt'' :: SelectStmtFn be
selectStmt'' = r -> SelectStmtFn be
selectStmt' (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb)

          sb' :: SelectBuilder be db r
sb' = case SelectBuilder be db r
sb of
                  SelectBuilderTopLevel { sbSelectFn :: forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> Maybe (SelectStmtFn be)
sbSelectFn = Maybe (SelectStmtFn be)
Nothing } ->
                    SelectBuilder be db r
sb { sbSelectFn :: Maybe (SelectStmtFn be)
sbSelectFn = forall a. a -> Maybe a
Just SelectStmtFn be
selectStmt'' }
                  SelectBuilderTopLevel { sbSelectFn :: forall be (db :: (* -> *) -> *) a.
SelectBuilder be db a -> Maybe (SelectStmtFn be)
sbSelectFn = Just {} } ->
                    forall a. HasCallStack => String -> a
error String
"Force select too hard"
                  SelectBuilder be db r
_ -> forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel forall a. Maybe a
Nothing forall a. Maybe a
Nothing [] SelectBuilder be db r
sb (forall a. a -> Maybe a
Just SelectStmtFn be
selectStmt'')
      in case r -> Free (QF be db s) x
next (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb') of
           Pure x
x' -> forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db r
sb' x
x'
           Free (QF be db s) x
_ -> let (r
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db r
sb'
                in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
x') QueryBuilder be
qb

    tryBuildGuardsOnly :: forall s x
                        . T.Text
                       -> Free (QF be db s) x
                       -> Maybe (BeamSqlBackendExpressionSyntax be)
                       -> Maybe (x, Maybe (BeamSqlBackendExpressionSyntax be))
    tryBuildGuardsOnly :: forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (x,
      Maybe
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryBuildGuardsOnly TablePrefix
tblPfx Free (QF be db s) x
next Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having =
      case forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> (Free (QF be db s) x,
    Maybe
      (Sql92SelectTableExpressionSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryCollectHaving TablePrefix
tblPfx Free (QF be db s) x
next Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having of
        (Pure x
x, Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having') -> forall a. a -> Maybe a
Just (x
x, Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having')
        (Free (QF be db s) x,
 Maybe
   (Sql92SelectTableExpressionSyntax
      (Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
_ -> forall a. Maybe a
Nothing

    tryCollectHaving :: forall s x
                      . T.Text
                     -> Free (QF be db s) x
                     -> Maybe (BeamSqlBackendExpressionSyntax be)
                     -> (Free (QF be db s) x, Maybe (BeamSqlBackendExpressionSyntax be))
    tryCollectHaving :: forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> (Free (QF be db s) x,
    Maybe
      (Sql92SelectTableExpressionSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryCollectHaving TablePrefix
tblPfx (Free (QGuard WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
cond Free (QF be db s) x
next)) Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having = forall s x.
TablePrefix
-> Free (QF be db s) x
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> (Free (QF be db s) x,
    Maybe
      (Sql92SelectTableExpressionSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
tryCollectHaving TablePrefix
tblPfx Free (QF be db s) x
next (forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having (forall a. a -> Maybe a
Just (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
cond)))
    tryCollectHaving TablePrefix
_ Free (QF be db s) x
next Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having = (Free (QF be db s) x
next, Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
having)

    buildTableCombination
      :: forall s x r
       . ( Projectible be r, Projectible be x )
      => T.Text
      -> (BeamSqlBackendSelectTableSyntax be -> BeamSqlBackendSelectTableSyntax be -> BeamSqlBackendSelectTableSyntax be)
      -> QM be db (QNested s) x -> QM be db (QNested s) x -> (x -> Free (QF be db s) r) -> SelectBuilder be db r
    buildTableCombination :: forall s x r.
(Projectible be r, Projectible be x) =>
TablePrefix
-> (Sql92SelectSelectTableSyntax
      (Sql92SelectSyntax (BeamSqlBackendSyntax be))
    -> Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be))
    -> Sql92SelectSelectTableSyntax
         (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> QM be db (QNested s) x
-> QM be db (QNested s) x
-> (x -> Free (QF be db s) r)
-> SelectBuilder be db r
buildTableCombination TablePrefix
tblPfx Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
combineTables QM be db (QNested s) x
left QM be db (QNested s) x
right x -> Free (QF be db s) r
next =
        let leftSb :: SelectBuilder be db x
leftSb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) x
left)
            leftTb :: Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
leftTb = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource TablePrefix
tblPfx SelectBuilder be db x
leftSb
            rightSb :: SelectBuilder be db x
rightSb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx (forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) x
right)
            rightTb :: Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
rightTb = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource TablePrefix
tblPfx SelectBuilder be db x
rightSb

            proj :: x
proj = forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject Proxy be
be (forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc forall fn. IsSql92FieldNameSyntax fn => TablePrefix -> fn
unqualifiedField) (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db x
leftSb)

            leftTb' :: Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
leftTb' | Bool
arbitrarilyNestedCombinations = Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
leftTb
                    | forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> Bool
sbContainsSetOperation SelectBuilder be db x
leftSb =
                      let (x
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db x
leftSb
                      in forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource TablePrefix
tblPfx (forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ x
x' QueryBuilder be
qb)
                    | Bool
otherwise = Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
leftTb
            rightTb' :: Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
rightTb' | Bool
arbitrarilyNestedCombinations = Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
rightTb
                     | forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> Bool
sbContainsSetOperation SelectBuilder be db x
rightSb =
                       let (x
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db x
rightSb
                       in forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource TablePrefix
tblPfx (forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ x
x' QueryBuilder be
qb)
                     | Bool
otherwise = Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
rightTb

            sb :: SelectBuilder be db x
sb = forall a be (db :: (* -> *) -> *).
Bool
-> a -> BeamSqlBackendSelectTableSyntax be -> SelectBuilder be db a
SelectBuilderSelectSyntax Bool
True x
proj (Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
combineTables Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
leftTb' Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
rightTb')
        in case x -> Free (QF be db s) r
next x
proj of
             Pure r
proj'
               | forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
projOrder Proxy be
be x
proj (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx) forall a. Eq a => a -> a -> Bool
== forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
projOrder Proxy be
be r
proj' (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx) ->
                   forall be b (db :: (* -> *) -> *) a.
Projectible be b =>
SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection SelectBuilder be db x
sb r
proj'
             Free (QF be db s) r
_ -> let (x
x', QueryBuilder be
qb) = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder TablePrefix
tblPfx SelectBuilder be db x
sb
                  in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (x -> Free (QF be db s) r
next x
x') QueryBuilder be
qb

    buildJoinedQuery :: forall s x
                      . Projectible be x
                     => T.Text -> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
    buildJoinedQuery :: forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
_ (Pure x
x) QueryBuilder be
qb = forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ x
x QueryBuilder be
qb
    buildJoinedQuery TablePrefix
tblPfx (Free (QAll TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkFrom TablePrefix -> r
mkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on (TablePrefix, r) -> Free (QF be db s) x
next)) QueryBuilder be
qb =
        let (TablePrefix
newTblNm, r
newTbl, QueryBuilder be
qb') = forall be r.
BeamSqlBackend be =>
TablePrefix
-> (TablePrefix -> TablePrefix -> BeamSqlBackendFromSyntax be)
-> (TablePrefix -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> QueryBuilder be
-> (TablePrefix, r, QueryBuilder be)
buildInnerJoinQuery TablePrefix
tblPfx TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkFrom TablePrefix -> r
mkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on QueryBuilder be
qb
        in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx ((TablePrefix, r) -> Free (QF be db s) x
next (TablePrefix
newTblNm, r
newTbl)) QueryBuilder be
qb'
    buildJoinedQuery TablePrefix
tblPfx (Free (QArbitraryJoin QM be db (QNested s) r
q TablePrefix
tblNs Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on r -> Free (QF be db s) x
next)) QueryBuilder be
qb =
      case forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q of
        Free (QAll TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkDbFrom TablePrefix -> r
dbMkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on' (TablePrefix, r) -> Free (QF be db (QNested s)) r
next')
          | (r
newTbl, TablePrefix
newTblNm, QueryBuilder be
qb') <- forall be r.
BeamSqlBackend be =>
QueryBuilder be
-> TablePrefix
-> (TablePrefix -> r)
-> (r, TablePrefix, QueryBuilder be)
nextTbl QueryBuilder be
qb TablePrefix
tblPfx TablePrefix -> r
dbMkTbl,
            Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
Nothing <- forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on' r
newTbl,
            Pure r
proj <- (TablePrefix, r) -> Free (QF be db (QNested s)) r
next' (TablePrefix
newTblNm, r
newTbl) ->
            let newSource :: Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource = TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkDbFrom (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx) TablePrefix
newTblNm
                on'' :: Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
on'' = forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on r
proj
                (Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from', Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where') =
                  case forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb' of
                    Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
Nothing -> (forall a. a -> Maybe a
Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource, forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' (forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb) Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
on'')
                    Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom -> (forall a. a -> Maybe a
Just (Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
on''), forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb)
            in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
proj) (QueryBuilder be
qb' { qbFrom :: Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
qbFrom = Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from', qbWhere :: Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
qbWhere = Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where' })

        Free (QF be db (QNested s)) r
q' -> let tblPfx' :: TablePrefix
tblPfx' = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> TablePrefix
tblNs

                  sb :: SelectBuilder be db r
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx' Free (QF be db (QNested s)) r
q'
                  tblSource :: Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
tblPfx' SelectBuilder be db r
sb
                  newTblNm :: TablePrefix
newTblNm = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show (forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb))

                  newSource :: Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource = forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource) (forall a. a -> Maybe a
Just (TablePrefix
newTblNm, forall a. Maybe a
Nothing))

                  proj' :: r
proj' = forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject Proxy be
be (forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
newTblNm)) (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb)
                  on' :: Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
on' = forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on r
proj'

                  (Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from', Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where') =
                    case forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb of
                      Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
Nothing -> (forall a. a -> Maybe a
Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource, forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' (forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb) Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
on')
                      Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom -> (forall a. a -> Maybe a
Just (Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
newSource Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
on'), forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb)

              in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (r -> Free (QF be db s) x
next r
proj') (QueryBuilder be
qb { qbNextTblRef :: Int
qbNextTblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb forall a. Num a => a -> a -> a
+ Int
1
                                                          , qbFrom :: Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
qbFrom = Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from', qbWhere :: Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
qbWhere = Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where' })
    buildJoinedQuery TablePrefix
tblPfx (Free (QTwoWayJoin QM be db (QNested s) a
a QM be db (QNested s) b
b Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin (a, b)
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on (a, b) -> Free (QF be db s) x
next)) QueryBuilder be
qb =
      let (a
aProj, Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
aSource, QueryBuilder be
qb') =
            case forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) a
a of
              Free (QAll TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkDbFrom TablePrefix -> r
dbMkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on' (TablePrefix, r) -> Free (QF be db (QNested s)) a
next')
                | (r
newTbl, TablePrefix
newTblNm, QueryBuilder be
qb') <- forall be r.
BeamSqlBackend be =>
QueryBuilder be
-> TablePrefix
-> (TablePrefix -> r)
-> (r, TablePrefix, QueryBuilder be)
nextTbl QueryBuilder be
qb TablePrefix
tblPfx TablePrefix -> r
dbMkTbl,
                  Maybe
  (WithExprContext
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
Nothing <- r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on' r
newTbl, Pure a
proj <- (TablePrefix, r) -> Free (QF be db (QNested s)) a
next' (TablePrefix
newTblNm, r
newTbl) ->
                    (a
proj, TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkDbFrom (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx) TablePrefix
newTblNm, QueryBuilder be
qb')

              Free (QF be db (QNested s)) a
a -> let sb :: SelectBuilder be db a
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx Free (QF be db (QNested s)) a
a
                       tblSource :: Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
tblPfx SelectBuilder be db a
sb

                       newTblNm :: TablePrefix
newTblNm = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show (forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb))

                       proj' :: a
proj' = forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject Proxy be
be (forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
newTblNm)) (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db a
sb)
                   in (a
proj', forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource) (forall a. a -> Maybe a
Just (TablePrefix
newTblNm, forall a. Maybe a
Nothing)), QueryBuilder be
qb { qbNextTblRef :: Int
qbNextTblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb forall a. Num a => a -> a -> a
+ Int
1 })

          (b
bProj, Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
bSource, QueryBuilder be
qb'') =
            case forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) b
b of
              Free (QAll TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkDbFrom TablePrefix -> r
dbMkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on' (TablePrefix, r) -> Free (QF be db (QNested s)) b
next')
                | (r
newTbl, TablePrefix
newTblNm, QueryBuilder be
qb'') <- forall be r.
BeamSqlBackend be =>
QueryBuilder be
-> TablePrefix
-> (TablePrefix -> r)
-> (r, TablePrefix, QueryBuilder be)
nextTbl QueryBuilder be
qb' TablePrefix
tblPfx TablePrefix -> r
dbMkTbl,
                  Maybe
  (WithExprContext
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
Nothing <- r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on' r
newTbl, Pure b
proj <- (TablePrefix, r) -> Free (QF be db (QNested s)) b
next' (TablePrefix
newTblNm, r
newTbl) ->
                    (b
proj, TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkDbFrom (TablePrefix -> TablePrefix
nextTblPfx TablePrefix
tblPfx) TablePrefix
newTblNm, QueryBuilder be
qb'')

              Free (QF be db (QNested s)) b
b -> let sb :: SelectBuilder be db b
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx Free (QF be db (QNested s)) b
b
                       tblSource :: Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
tblPfx SelectBuilder be db b
sb

                       newTblNm :: TablePrefix
newTblNm = TablePrefix
tblPfx forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show (forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb))

                       proj' :: b
proj' = forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject Proxy be
be (forall expr.
IsSql92ExpressionSyntax expr =>
(TablePrefix -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc (forall fn.
IsSql92FieldNameSyntax fn =>
TablePrefix -> TablePrefix -> fn
qualifiedField TablePrefix
newTblNm)) (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db b
sb)
                   in (b
proj', forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (TablePrefix, Maybe [TablePrefix]) -> from
fromTable (forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource) (forall a. a -> Maybe a
Just (TablePrefix
newTblNm, forall a. Maybe a
Nothing)), QueryBuilder be
qb { qbNextTblRef :: Int
qbNextTblRef = forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb forall a. Num a => a -> a -> a
+ Int
1 })

          abSource :: Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
abSource = Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
aSource Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
bSource (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a, b)
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
on (a
aProj, b
bProj))

          from' :: Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from' =
            case forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb'' of
              Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
Nothing -> forall a. a -> Maybe a
Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
abSource
              Just Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom -> forall a. a -> Maybe a
Just (forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
innerJoin Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
oldFrom Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
abSource forall a. Maybe a
Nothing)

      in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx ((a, b) -> Free (QF be db s) x
next (a
aProj, b
bProj)) (QueryBuilder be
qb'' { qbFrom :: Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
qbFrom = Maybe
  (Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
from' })
    buildJoinedQuery TablePrefix
tblPfx (Free (QGuard WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
cond Free (QF be db s) x
next)) QueryBuilder be
qb =
        forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx Free (QF be db s) x
next (QueryBuilder be
qb { qbWhere :: Maybe
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
qbWhere = forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' (forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb) (forall a. a -> Maybe a
Just (forall a. TablePrefix -> WithExprContext a -> a
exprWithContext TablePrefix
tblPfx WithExprContext
  (Sql92SelectTableExpressionSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
cond)) })
    buildJoinedQuery TablePrefix
tblPfx Free (QF be db s) x
now QueryBuilder be
qb =
      forall s x.
Free (QF be db s) x
-> (forall a'.
    Projectible be a' =>
    Free (QF be db s) a'
    -> (a' -> Free (QF be db s) x) -> SelectBuilder be db x)
-> SelectBuilder be db x
onlyQ Free (QF be db s) x
now
        (\Free (QF be db s) a'
now' a' -> Free (QF be db s) x
next ->
           let sb :: SelectBuilder be db a'
sb = forall s x.
Projectible be x =>
TablePrefix -> Free (QF be db s) x -> SelectBuilder be db x
buildQuery TablePrefix
tblPfx Free (QF be db s) a'
now'
               tblSource :: Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource = forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
TablePrefix
-> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect TablePrefix
tblPfx SelectBuilder be db a'
sb
               (a'
x', QueryBuilder be
qb') = forall be x.
(BeamSqlBackend be, Projectible be x) =>
TablePrefix
-> BeamSqlBackendSelectSyntax be
-> x
-> QueryBuilder be
-> (x, QueryBuilder be)
buildJoinTableSourceQuery TablePrefix
tblPfx Sql92SelectSyntax (BeamSqlBackendSyntax be)
tblSource (forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db a'
sb) QueryBuilder be
qb
           in forall s x.
Projectible be x =>
TablePrefix
-> Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery TablePrefix
tblPfx (a' -> Free (QF be db s) x
next a'
x') QueryBuilder be
qb')

    onlyQ :: forall s x.
             Free (QF be db s) x
          -> (forall a'. Projectible be a' => Free (QF be db s) a' -> (a' -> Free (QF be db s) x) -> SelectBuilder be db x)
          -> SelectBuilder be db x
    onlyQ :: forall s x.
Free (QF be db s) x
-> (forall a'.
    Projectible be a' =>
    Free (QF be db s) a'
    -> (a' -> Free (QF be db s) x) -> SelectBuilder be db x)
-> SelectBuilder be db x
onlyQ (Free (QAll TablePrefix
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
entityNm TablePrefix -> r
mkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn (TablePrefix, r) -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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
-> TablePrefix
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
entityNm TablePrefix -> r
mkTbl r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn (forall (f :: * -> *) a. a -> Free f a
Pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> PreserveLeft a b
PreserveLeft))) ((TablePrefix, r) -> Free (QF be db s) x
next forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. PreserveLeft a b -> (a, b)
unPreserveLeft)
--      f (Free (QAll entityNm mkTbl mkOn (Pure . PreserveLeft))) (next . unPreserveLeft)
    onlyQ (Free (QArbitraryJoin QM be db (QNested s) r
entity TablePrefix
tblNs Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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
entity TablePrefix
tblNs Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin r
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QTwoWayJoin QM be db (QNested s) a
a QM be db (QNested s) b
b Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin (a, b)
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn (a, b) -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (forall be r a (db :: (* -> *) -> *) s next.
(Projectible be r, Projectible be a) =>
QM be db (QNested s) r
-> QM be db (QNested s) a
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> ((r, a)
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((r, a) -> next)
-> QF be db s next
QTwoWayJoin QM be db (QNested s) a
a QM be db (QNested s) b
b Sql92SelectTableFromSyntax
  (Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92SelectTableExpressionSyntax
        (Sql92SelectSelectTableSyntax
           (Sql92SelectSyntax (BeamSqlBackendSyntax be))))
-> Sql92SelectTableFromSyntax
     (Sql92SelectSelectTableSyntax
        (Sql92SelectSyntax (BeamSqlBackendSyntax be)))
mkJoin (a, b)
-> Maybe
     (WithExprContext
        (Sql92SelectTableExpressionSyntax
           (Sql92SelectSelectTableSyntax
              (Sql92SelectSyntax (BeamSqlBackendSyntax be)))))
mkOn forall (f :: * -> *) a. a -> Free f a
Pure)) (a, b) -> Free (QF be db s) x
next
    onlyQ (Free (QSubSelect QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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 (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QLimit Integer
limit QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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) r
q' forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QOffset Integer
offset QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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) r
q' forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QSetOp Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
combine QM be db (QNested s) r
a QM be db (QNested s) r
b r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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 Sql92SelectSelectTableSyntax
  (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
-> Sql92SelectSelectTableSyntax
     (Sql92SelectSyntax (BeamSqlBackendSyntax be))
combine QM be db (QNested s) r
a QM be db (QNested s) r
b forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QOrderBy r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering QM be db (QNested s) r
q' r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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 r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering QM be db (QNested s) r
q' forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QWindowOver r -> window
mkWindow r -> window -> a
mkProj QM be db (QNested s) r
q' a -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (forall be r a a (db :: (* -> *) -> *) s next.
(ProjectibleWithPredicate
   WindowFrameContext
   be
   (WithExprContext (BeamSqlBackendWindowFrameSyntax' be))
   r,
 Projectible be a, Projectible be a) =>
(a -> r)
-> (a -> r -> a)
-> QM be db (QNested s) a
-> (a -> next)
-> QF be db s next
QWindowOver r -> window
mkWindow r -> window -> a
mkProj QM be db (QNested s) r
q' forall (f :: * -> *) a. a -> Free f a
Pure)) a -> Free (QF be db s) x
next
    onlyQ (Free (QAggregate a
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    grouping)
mkAgg QM be db (QNested s) a
q' grouping -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (forall be r a (db :: (* -> *) -> *) s next.
(Projectible be r, Projectible be a) =>
(a -> TablePrefix -> (Maybe (BeamSqlBackendGroupingSyntax be), r))
-> QM be db (QNested s) a -> (r -> next) -> QF be db s next
QAggregate a
-> TablePrefix
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax
            (Sql92SelectSyntax (BeamSqlBackendSyntax be)))),
    grouping)
mkAgg QM be db (QNested s) a
q' forall (f :: * -> *) a. a -> Free f a
Pure)) grouping -> Free (QF be db s) x
next
    onlyQ (Free (QDistinct r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be)
d QM be db s r
q' r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (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 -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be)
d QM be db s r
q' forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QForceSelect r -> SelectStmtFn be
s QM be db (QNested s) r
over r -> Free (QF be db s) x
next)) forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f =
      forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
(r
 -> BeamSqlBackendSelectTableSyntax be
 -> [BeamSqlBackendOrderingSyntax be]
 -> Maybe Integer
 -> Maybe Integer
 -> BeamSqlBackendSelectSyntax be)
-> QM be db (QNested s) r -> (r -> next) -> QF be db s next
QForceSelect r -> SelectStmtFn be
s QM be db (QNested s) r
over forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ Free (QF be db s) x
_ forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
_ = forall a. HasCallStack => String -> a
error String
"impossible"