{-# 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' :: Maybe expr -> Maybe expr -> Maybe expr
andE' Maybe expr
Nothing Maybe expr
Nothing = Maybe expr
forall a. Maybe a
Nothing
andE' (Just expr
x) Maybe expr
Nothing = expr -> Maybe expr
forall a. a -> Maybe a
Just expr
x
andE' Maybe expr
Nothing (Just expr
y) = expr -> Maybe expr
forall a. a -> Maybe a
Just expr
y
andE' (Just expr
x) (Just expr
y) = expr -> Maybe expr
forall a. a -> Maybe a
Just (expr -> expr -> expr
forall expr. IsSql92ExpressionSyntax expr => expr -> expr -> expr
andE expr
x expr
y)

newtype PreserveLeft a b = PreserveLeft { 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' :: 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)) =
    (a, b) -> PreserveLeft a b
forall a b. (a, b) -> PreserveLeft a b
PreserveLeft ((a, b) -> PreserveLeft a b)
-> (b -> (a, b)) -> b -> PreserveLeft a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
a,) (b -> PreserveLeft a b) -> m b -> m (PreserveLeft a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy c
-> Proxy (syntax, res)
-> (forall context.
    c context =>
    Proxy context -> Proxy syntax -> res -> m res)
-> b
-> m 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' :: 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 =
    (a, b) -> PreserveLeft a b
forall a b. (a, b) -> PreserveLeft a b
PreserveLeft ((a, b) -> PreserveLeft a b)
-> (b -> (a, b)) -> b -> PreserveLeft a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
forall a. Monoid a => a
mempty,) (b -> PreserveLeft a b) -> m b -> m (PreserveLeft a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy c
-> Proxy (syntax, res)
-> (forall context.
    c context =>
    Proxy context -> Proxy syntax -> m res)
-> m 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
  { QueryBuilder be -> Int
qbNextTblRef :: Int
  , QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom  :: Maybe (BeamSqlBackendFromSyntax 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 ::
    { SelectBuilder be db a -> Maybe Integer
sbLimit, SelectBuilder be db a -> Maybe Integer
sbOffset :: Maybe Integer
    , SelectBuilder be db a -> [BeamSqlBackendOrderingSyntax be]
sbOrdering        :: [ BeamSqlBackendOrderingSyntax be ]
    , SelectBuilder be db a -> SelectBuilder be db a
sbTable           :: SelectBuilder 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 :: 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 }) = SelectBuilder syntax db a -> Bool
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 :: (Text -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc Text -> Sql92ExpressionFieldNameSyntax expr
mkField Int
i = Sql92ExpressionFieldNameSyntax expr -> expr
forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionFieldNameSyntax expr -> expr
fieldE (Text -> Sql92ExpressionFieldNameSyntax expr
mkField (Text
"res" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. IsString a => String -> a
fromString (Int -> String
forall a. Show a => a -> String
show Int
i)))

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

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

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

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

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

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

sbProj :: SelectBuilder syntax db a -> a
sbProj :: 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)
_) = SelectBuilder syntax db a -> a
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 :: SelectBuilder be db a -> b -> SelectBuilder be db b
setSelectBuilderProjection (SelectBuilderQ a
_ QueryBuilder be
q) b
proj = b -> QueryBuilder be -> SelectBuilder be db b
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 = b
-> QueryBuilder be
-> Maybe (BeamSqlBackendGroupingSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (BeamSqlBackendSetQuantifierSyntax be)
-> SelectBuilder be db 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 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 = Bool
-> b -> BeamSqlBackendSelectTableSyntax be -> SelectBuilder be db b
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 =
    Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db b
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db b
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 (SelectBuilder be db a -> b -> SelectBuilder be db b
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 :: 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) =
    Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax syntax]
-> SelectBuilder syntax db a
-> Maybe (SelectStmtFn syntax)
-> SelectBuilder syntax db a
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Integer -> (Integer -> Integer) -> Maybe Integer -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
limit (Integer -> Integer -> Integer
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 = Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax syntax]
-> SelectBuilder syntax db a
-> Maybe (SelectStmtFn syntax)
-> SelectBuilder syntax db a
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
limit) Maybe Integer
forall a. Maybe a
Nothing [] SelectBuilder syntax db a
x Maybe (SelectStmtFn syntax)
forall a. Maybe a
Nothing

offsetSelectBuilder :: 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) =
    Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax syntax]
-> SelectBuilder syntax db a
-> Maybe (SelectStmtFn syntax)
-> SelectBuilder syntax db a
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing (Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Integer
offset Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer -> Maybe Integer -> Integer
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) =
    Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax syntax]
-> SelectBuilder syntax db a
-> Maybe (SelectStmtFn syntax)
-> SelectBuilder syntax db a
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
0 (Integer
limit Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
offset)) (Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer -> Maybe Integer) -> Integer -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ Integer
offset Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer -> Maybe Integer -> Integer
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 = Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax syntax]
-> SelectBuilder syntax db a
-> Maybe (SelectStmtFn syntax)
-> SelectBuilder syntax db a
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
offset) [] SelectBuilder syntax db a
x Maybe (SelectStmtFn syntax)
forall a. Maybe a
Nothing

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

buildJoinTableSourceQuery
  :: forall be x
   . ( BeamSqlBackend be, Projectible be x )
  => TablePrefix -> BeamSqlBackendSelectSyntax be
  -> x -> QueryBuilder be
  -> (x, QueryBuilder be)
buildJoinTableSourceQuery :: Text
-> BeamSqlBackendSelectSyntax be
-> x
-> QueryBuilder be
-> (x, QueryBuilder be)
buildJoinTableSourceQuery Text
tblPfx BeamSqlBackendSelectSyntax be
tblSource x
x QueryBuilder be
qb =
  let qb' :: QueryBuilder be
qb' = Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
forall be.
Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
QueryBuilder (Int
tblRef Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Maybe (BeamSqlBackendFromSyntax be)
from' (QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb)
      !tblRef :: Int
tblRef = QueryBuilder be -> Int
forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb
      from' :: Maybe (BeamSqlBackendFromSyntax be)
from' = case QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb of
                Maybe (BeamSqlBackendFromSyntax be)
Nothing -> BeamSqlBackendFromSyntax be -> Maybe (BeamSqlBackendFromSyntax be)
forall a. a -> Maybe a
Just BeamSqlBackendFromSyntax be
newSource
                Just BeamSqlBackendFromSyntax be
oldFrom -> BeamSqlBackendFromSyntax be -> Maybe (BeamSqlBackendFromSyntax be)
forall a. a -> Maybe a
Just (BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (Sql92FromExpressionSyntax (BeamSqlBackendFromSyntax be))
-> BeamSqlBackendFromSyntax be
forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
innerJoin BeamSqlBackendFromSyntax be
oldFrom BeamSqlBackendFromSyntax be
newSource Maybe (Sql92FromExpressionSyntax (BeamSqlBackendFromSyntax be))
forall a. Maybe a
Nothing)
      newSource :: BeamSqlBackendFromSyntax be
newSource = Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be)
-> Maybe (Text, Maybe [Text]) -> BeamSqlBackendFromSyntax be
forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (Text, Maybe [Text]) -> from
fromTable (Sql92TableSourceSelectSyntax
  (Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be))
-> Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be)
forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92TableSourceSelectSyntax
  (Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be))
BeamSqlBackendSelectSyntax be
tblSource) ((Text, Maybe [Text]) -> Maybe (Text, Maybe [Text])
forall a. a -> Maybe a
Just (Text
newTblNm, Maybe [Text]
forall a. Maybe a
Nothing))
      newTblNm :: Text
newTblNm = Text
tblPfx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. IsString a => String -> a
fromString (Int -> String
forall a. Show a => a -> String
show Int
tblRef)
  in (Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> x -> x
forall be a.
(BeamSqlBackend be, Projectible be a) =>
Proxy be -> (Int -> BeamSqlBackendExpressionSyntax be) -> a -> a
reproject (Proxy be
forall k (t :: k). Proxy t
Proxy @be) ((Text
 -> Sql92ExpressionFieldNameSyntax
      (Sql92UpdateExpressionSyntax
         (Sql92UpdateSyntax (BeamSqlBackendSyntax be))))
-> Int
-> Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
forall expr.
IsSql92ExpressionSyntax expr =>
(Text -> Sql92ExpressionFieldNameSyntax expr) -> Int -> expr
fieldNameFunc (Text
-> Text
-> Sql92ExpressionFieldNameSyntax
     (Sql92InsertValuesExpressionSyntax
        (Sql92InsertValuesSyntax
           (Sql92InsertSyntax (BeamSqlBackendSyntax be))))
forall fn. IsSql92FieldNameSyntax fn => Text -> Text -> fn
qualifiedField Text
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 :: Text
-> (Text -> Text -> BeamSqlBackendFromSyntax be)
-> (Text -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> QueryBuilder be
-> (Text, r, QueryBuilder be)
buildInnerJoinQuery Text
tblPfx Text -> Text -> BeamSqlBackendFromSyntax be
mkFrom Text -> r
mkTbl r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn QueryBuilder be
qb =
  let qb' :: QueryBuilder be
qb' = Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
forall be.
Int
-> Maybe (BeamSqlBackendFromSyntax be)
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> QueryBuilder be
QueryBuilder (Int
tblRef Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Maybe (BeamSqlBackendFromSyntax be)
from' Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
Maybe (BeamSqlBackendExpressionSyntax be)
where'
      tblRef :: Int
tblRef = QueryBuilder be -> Int
forall be. QueryBuilder be -> Int
qbNextTblRef QueryBuilder be
qb
      newTblNm :: Text
newTblNm = Text
tblPfx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. IsString a => String -> a
fromString (Int -> String
forall a. Show a => a -> String
show Int
tblRef)
      newSource :: BeamSqlBackendFromSyntax be
newSource = Text -> Text -> BeamSqlBackendFromSyntax be
mkFrom (Text -> Text
nextTblPfx Text
tblPfx) Text
newTblNm
      (Maybe (BeamSqlBackendFromSyntax be)
from', Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
where') =
        case QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
forall be. QueryBuilder be -> Maybe (BeamSqlBackendFromSyntax be)
qbFrom QueryBuilder be
qb of
          Maybe (BeamSqlBackendFromSyntax be)
Nothing -> (BeamSqlBackendFromSyntax be -> Maybe (BeamSqlBackendFromSyntax be)
forall a. a -> Maybe a
Just BeamSqlBackendFromSyntax be
newSource, Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' (QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb) (Text
-> WithExprContext
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
forall a. Text -> WithExprContext a -> a
exprWithContext Text
tblPfx (WithExprContext
   (Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
 -> Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (WithExprContext
        (Sql92UpdateExpressionSyntax
           (Sql92UpdateSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn r
newTbl))
          Just BeamSqlBackendFromSyntax be
oldFrom -> (BeamSqlBackendFromSyntax be -> Maybe (BeamSqlBackendFromSyntax be)
forall a. a -> Maybe a
Just (BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (Sql92FromExpressionSyntax (BeamSqlBackendFromSyntax be))
-> BeamSqlBackendFromSyntax be
forall from.
IsSql92FromSyntax from =>
from -> from -> Maybe (Sql92FromExpressionSyntax from) -> from
innerJoin BeamSqlBackendFromSyntax be
oldFrom BeamSqlBackendFromSyntax be
newSource (Text
-> WithExprContext
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
forall a. Text -> WithExprContext a -> a
exprWithContext Text
tblPfx (WithExprContext
   (Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
 -> Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (WithExprContext
        (Sql92UpdateExpressionSyntax
           (Sql92UpdateSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn r
newTbl)), QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb)

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

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

projOrder :: Projectible be x
          => Proxy be -> x -> WithExprContext [ BeamSqlBackendExpressionSyntax be ]
projOrder :: Proxy be
-> x -> WithExprContext [BeamSqlBackendExpressionSyntax be]
projOrder = Proxy be
-> x -> WithExprContext [BeamSqlBackendExpressionSyntax be]
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' :: Bool -> Text -> Q be db s a -> BeamSqlBackendSelectSyntax be
buildSql92Query' Bool
arbitrarilyNestedCombinations Text
tblPfx (Q QM be db s a
q) =
    Text -> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect Text
tblPfx (Free (QF be db s) a -> SelectBuilder be db a
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db s a -> Free (QF be db s) a
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 = Proxy be
forall k (t :: k). Proxy t
Proxy

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

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

    buildQuery (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 = Free (QF be db (QNested s)) r -> SelectBuilder be db r
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) r -> Free (QF be db (QNested s)) r
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q')
            proj :: r
proj = SelectBuilder be db r -> r
forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb
            ordering :: [BeamSqlBackendOrderingSyntax be]
ordering = Text
-> WithExprContext [BeamSqlBackendOrderingSyntax be]
-> [BeamSqlBackendOrderingSyntax be]
forall a. Text -> WithExprContext a -> a
exprWithContext Text
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 {} ->
                                Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db r
-> Maybe (SelectStmtFn be)
-> 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 Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                            SelectBuilderGrouping {} ->
                                Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db r
-> Maybe (SelectStmtFn be)
-> 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 Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                            SelectBuilderSelectSyntax {} ->
                                Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db r
-> Maybe (SelectStmtFn be)
-> 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 Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering SelectBuilder be db r
sb Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                            SelectBuilderTopLevel Maybe Integer
Nothing Maybe Integer
Nothing [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                                Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db r
-> Maybe (SelectStmtFn be)
-> 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 Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
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 ->
                                Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db r
-> Maybe (SelectStmtFn be)
-> 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 Maybe Integer
forall a. Maybe a
Nothing (Integer -> Maybe Integer
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) <- Text -> SelectBuilder be db r -> (r, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db r
sb ->
                                    Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db r
-> Maybe (SelectStmtFn be)
-> 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 Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing (Text
-> WithExprContext [BeamSqlBackendOrderingSyntax be]
-> [BeamSqlBackendOrderingSyntax be]
forall a. Text -> WithExprContext a -> a
exprWithContext Text
tblPfx (r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering r
proj'')) (r -> QueryBuilder be -> SelectBuilder be db r
forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ r
proj'' QueryBuilder be
qb) Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                                | Bool
otherwise -> String -> SelectBuilder be db r
forall a. HasCallStack => String -> a
error String
"buildQuery (Free (QOrderBy ...)): query inspected expression"

                    (r
joinedProj, QueryBuilder be
qb) = Text -> SelectBuilder be db r -> (r, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db r
sb'
                in Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery (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
                 [] -> SelectBuilder be db r -> x -> SelectBuilder be db 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
proj'
                 [BeamSqlBackendOrderingSyntax be]
ordering ->
                     case SelectBuilder be db r
sb of
                       SelectBuilderQ {} ->
                           Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilder be db r -> x -> SelectBuilder be db 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
proj') Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                       SelectBuilderGrouping {} ->
                           Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilder be db r -> x -> SelectBuilder be db 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
proj') Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                       SelectBuilderSelectSyntax {} ->
                           Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilder be db r -> x -> SelectBuilder be db 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
proj') Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                       SelectBuilderTopLevel Maybe Integer
Nothing Maybe Integer
Nothing [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                           Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilder be db r -> x -> SelectBuilder be db 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
proj') Maybe (SelectStmtFn be)
build
                       SelectBuilderTopLevel (Just Integer
0) (Just Integer
0) [] SelectBuilder be db r
sb' Maybe (SelectStmtFn be)
build ->
                           Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
0) (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
0) [BeamSqlBackendOrderingSyntax be]
ordering (SelectBuilder be db r -> x -> SelectBuilder be db 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
proj') Maybe (SelectStmtFn be)
build
                       SelectBuilderTopLevel {}
                           | (r
proj'', QueryBuilder be
qb) <- Text -> SelectBuilder be db r -> (r, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db r
sb,
                             Pure x
proj''' <- r -> Free (QF be db s) x
next r
proj'' ->
                               Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing (Text
-> WithExprContext [BeamSqlBackendOrderingSyntax be]
-> [BeamSqlBackendOrderingSyntax be]
forall a. Text -> WithExprContext a -> a
exprWithContext Text
tblPfx (r -> WithExprContext [BeamSqlBackendOrderingSyntax be]
mkOrdering r
proj'')) (x -> QueryBuilder be -> SelectBuilder be db x
forall be a (db :: (* -> *) -> *).
(BeamSqlBackend be, Projectible be a) =>
a -> QueryBuilder be -> SelectBuilder be db a
SelectBuilderQ x
proj''' QueryBuilder be
qb) Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
                           | Bool
otherwise -> String -> SelectBuilder be db x
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 (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 = Free (QF be db (QNested s)) r -> SelectBuilder be db r
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) r -> Free (QF be db (QNested s)) r
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q')

            x :: r
x = SelectBuilder be db r -> r
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 SelectBuilder be db r -> x -> SelectBuilder be db 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' of
                 sb' :: SelectBuilder be db x
sb'@SelectBuilderTopLevel {} -> SelectBuilder be db x
sb'
                 SelectBuilder be db x
sb' -> Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db x
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db x
forall be (db :: (* -> *) -> *) a.
Maybe Integer
-> Maybe Integer
-> [BeamSqlBackendOrderingSyntax be]
-> SelectBuilder be db a
-> Maybe (SelectStmtFn be)
-> SelectBuilder be db a
SelectBuilderTopLevel Maybe Integer
forall a. Maybe a
Nothing Maybe Integer
forall a. Maybe a
Nothing [] SelectBuilder be db x
sb' Maybe (SelectStmtFn be)
forall a. Maybe a
Nothing
             Free (QF be db s) x
_       ->
               let (a
x', QueryBuilder be
qb) = Text -> SelectBuilder be db a -> (a, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx (SelectBuilder be db r -> a -> SelectBuilder be db a
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 Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery (a -> Free (QF be db s) x
next a
x') QueryBuilder be
qb

    buildQuery (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 = Integer -> SelectBuilder be db r -> SelectBuilder be db r
forall syntax (db :: (* -> *) -> *) a.
Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
limitSelectBuilder Integer
limit (Free (QF be db (QNested s)) r -> SelectBuilder be db r
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) r -> Free (QF be db (QNested s)) r
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q'))
            x :: r
x = SelectBuilder be db r -> r
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' -> SelectBuilder be db r -> x -> SelectBuilder be db 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) = Text -> SelectBuilder be db r -> (r, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db r
sb
                  in Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery (r -> Free (QF be db s) x
next r
x') QueryBuilder be
qb

    buildQuery (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 = Integer -> SelectBuilder be db r -> SelectBuilder be db r
forall syntax (db :: (* -> *) -> *) a.
Integer -> SelectBuilder syntax db a -> SelectBuilder syntax db a
offsetSelectBuilder Integer
offset (Free (QF be db (QNested s)) r -> SelectBuilder be db r
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) r -> Free (QF be db (QNested s)) r
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
q'))
            x :: r
x = SelectBuilder be db r -> r
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' -> SelectBuilder be db r -> x -> SelectBuilder be db 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) = Text -> SelectBuilder be db r -> (r, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db r
sb
                  in Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery (r -> Free (QF be db s) x
next r
x') QueryBuilder be
qb

    buildQuery (Free (QSetOp Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
combine QM be db (QNested s) r
left QM be db (QNested s) r
right r -> Free (QF be db s) x
next)) =
      (Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be))
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> Free (QF be db s) x)
-> SelectBuilder be db x
forall s x r.
(Projectible be r, Projectible be x) =>
(Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax 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 Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax 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 (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 = Free (QF be db (QNested s)) r -> SelectBuilder be db r
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) r -> Free (QF be db (QNested s)) r
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) r
over)
          x :: r
x = SelectBuilder be db r -> r
forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db r
sb

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

    tryBuildGuardsOnly :: forall s x
                        . Free (QF be db s) x
                       -> Maybe (BeamSqlBackendExpressionSyntax be)
                       -> Maybe (x, Maybe (BeamSqlBackendExpressionSyntax be))
    tryBuildGuardsOnly :: Free (QF be db s) x
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> Maybe (x, Maybe (BeamSqlBackendExpressionSyntax be))
tryBuildGuardsOnly Free (QF be db s) x
next Maybe (BeamSqlBackendExpressionSyntax be)
having =
      case Free (QF be db s) x
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> (Free (QF be db s) x, Maybe (BeamSqlBackendExpressionSyntax be))
forall s x.
Free (QF be db s) x
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> (Free (QF be db s) x, Maybe (BeamSqlBackendExpressionSyntax be))
tryCollectHaving Free (QF be db s) x
next Maybe (BeamSqlBackendExpressionSyntax be)
having of
        (Pure x
x, Maybe (BeamSqlBackendExpressionSyntax be)
having') -> (x,
 Maybe
   (Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (x,
      Maybe
        (Sql92UpdateExpressionSyntax
           (Sql92UpdateSyntax (BeamSqlBackendSyntax be))))
forall a. a -> Maybe a
Just (x
x, Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
Maybe (BeamSqlBackendExpressionSyntax be)
having')
        (Free (QF be db s) x, Maybe (BeamSqlBackendExpressionSyntax be))
_ -> Maybe (x, Maybe (BeamSqlBackendExpressionSyntax be))
forall a. Maybe a
Nothing

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

    buildTableCombination
      :: forall s x r
       . ( Projectible be r, Projectible be x )
      => (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 :: (Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax 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 Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax 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 = Free (QF be db (QNested s)) x -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) x -> Free (QF be db (QNested s)) x
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) x
left)
            leftTb :: Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
leftTb = Text
-> SelectBuilder be db x
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource Text
tblPfx SelectBuilder be db x
leftSb
            rightSb :: SelectBuilder be db x
rightSb = Free (QF be db (QNested s)) x -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery (QM be db (QNested s) x -> Free (QF be db (QNested s)) x
forall (f :: * -> *) (m :: * -> *) a. MonadFree f m => F f a -> m a
fromF QM be db (QNested s) x
right)
            rightTb :: Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
rightTb = Text
-> SelectBuilder be db x
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource Text
tblPfx SelectBuilder be db x
rightSb

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

            leftTb' :: Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
leftTb' | Bool
arbitrarilyNestedCombinations = Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
leftTb
                    | SelectBuilder be db x -> Bool
forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> Bool
sbContainsSetOperation SelectBuilder be db x
leftSb =
                      let (x
x', QueryBuilder be
qb) = Text -> SelectBuilder be db x -> (x, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db x
leftSb
                      in Text
-> SelectBuilder be Any x
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource Text
tblPfx (x -> QueryBuilder be -> SelectBuilder be Any x
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 (BeamSqlBackendSelectSyntax be)
leftTb
            rightTb' :: Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
rightTb' | Bool
arbitrarilyNestedCombinations = Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
rightTb
                     | SelectBuilder be db x -> Bool
forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> Bool
sbContainsSetOperation SelectBuilder be db x
rightSb =
                       let (x
x', QueryBuilder be
qb) = Text -> SelectBuilder be db x -> (x, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db x
rightSb
                       in Text
-> SelectBuilder be Any x
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> BeamSqlBackendSelectTableSyntax be
selectBuilderToTableSource Text
tblPfx (x -> QueryBuilder be -> SelectBuilder be Any x
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 (BeamSqlBackendSelectSyntax be)
rightTb

            sb :: SelectBuilder be db x
sb = Bool
-> x
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> SelectBuilder be db x
forall a be (db :: (* -> *) -> *).
Bool
-> a -> BeamSqlBackendSelectTableSyntax be -> SelectBuilder be db a
SelectBuilderSelectSyntax Bool
True x
proj (Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
combineTables Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
leftTb' Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
rightTb')
        in case x -> Free (QF be db s) r
next x
proj of
             Pure r
proj'
               | Proxy be
-> x -> WithExprContext [BeamSqlBackendExpressionSyntax be]
forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
projOrder Proxy be
be x
proj (Text -> Text
nextTblPfx Text
tblPfx) [Sql92UpdateExpressionSyntax
   (Sql92UpdateSyntax (BeamSqlBackendSyntax be))]
-> [Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be))]
-> Bool
forall a. Eq a => a -> a -> Bool
== Proxy be
-> r -> WithExprContext [BeamSqlBackendExpressionSyntax be]
forall be a.
Projectible be a =>
Proxy be
-> a -> WithExprContext [BeamSqlBackendExpressionSyntax be]
projOrder Proxy be
be r
proj' (Text -> Text
nextTblPfx Text
tblPfx) ->
                   SelectBuilder be db x -> r -> SelectBuilder be db r
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) = Text -> SelectBuilder be db x -> (x, QueryBuilder be)
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> (a, QueryBuilder be)
selectBuilderToQueryBuilder Text
tblPfx SelectBuilder be db x
sb
                  in Free (QF be db s) r -> QueryBuilder be -> SelectBuilder be db r
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery (x -> Free (QF be db s) r
next x
x') QueryBuilder be
qb

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

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

                  newSource :: BeamSqlBackendFromSyntax be
newSource = Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be)
-> Maybe (Text, Maybe [Text]) -> BeamSqlBackendFromSyntax be
forall from.
IsSql92FromSyntax from =>
Sql92FromTableSourceSyntax from
-> Maybe (Text, Maybe [Text]) -> from
fromTable (Sql92TableSourceSelectSyntax
  (Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be))
-> Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be)
forall tblSource.
IsSql92TableSourceSyntax tblSource =>
Sql92TableSourceSelectSyntax tblSource -> tblSource
tableFromSubSelect Sql92TableSourceSelectSyntax
  (Sql92FromTableSourceSyntax (BeamSqlBackendFromSyntax be))
BeamSqlBackendSelectSyntax be
tblSource) ((Text, Maybe [Text]) -> Maybe (Text, Maybe [Text])
forall a. a -> Maybe a
Just (Text
newTblNm, Maybe [Text]
forall a. Maybe a
Nothing))

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

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

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

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

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

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

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

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

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

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

          abSource :: BeamSqlBackendFromSyntax be
abSource = BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> BeamSqlBackendFromSyntax be
mkJoin BeamSqlBackendFromSyntax be
aSource BeamSqlBackendFromSyntax be
bSource (Text
-> WithExprContext
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
forall a. Text -> WithExprContext a -> a
exprWithContext Text
tblPfx (WithExprContext
   (Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
 -> Sql92UpdateExpressionSyntax
      (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (WithExprContext
        (Sql92UpdateExpressionSyntax
           (Sql92UpdateSyntax (BeamSqlBackendSyntax be))))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a, b)
-> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
on (a
aProj, b
bProj))

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

      in Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery ((a, b) -> Free (QF be db s) x
next (a
aProj, b
bProj)) (QueryBuilder be
qb'' { qbFrom :: Maybe (BeamSqlBackendFromSyntax be)
qbFrom = Maybe (BeamSqlBackendFromSyntax be)
from' })
    buildJoinedQuery (Free (QGuard WithExprContext (BeamSqlBackendExpressionSyntax be)
cond Free (QF be db s) x
next)) QueryBuilder be
qb =
        Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery Free (QF be db s) x
next (QueryBuilder be
qb { qbWhere :: Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere = Maybe
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
forall expr.
IsSql92ExpressionSyntax expr =>
Maybe expr -> Maybe expr -> Maybe expr
andE' (QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
forall be.
QueryBuilder be -> Maybe (BeamSqlBackendExpressionSyntax be)
qbWhere QueryBuilder be
qb) (Sql92UpdateExpressionSyntax
  (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
-> Maybe
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
forall a. a -> Maybe a
Just (Text
-> WithExprContext
     (Sql92UpdateExpressionSyntax
        (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
-> Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be))
forall a. Text -> WithExprContext a -> a
exprWithContext Text
tblPfx WithExprContext
  (Sql92UpdateExpressionSyntax
     (Sql92UpdateSyntax (BeamSqlBackendSyntax be)))
WithExprContext (BeamSqlBackendExpressionSyntax be)
cond)) })
    buildJoinedQuery Free (QF be db s) x
now QueryBuilder be
qb =
      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
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 = Free (QF be db s) a' -> SelectBuilder be db a'
forall s x.
Projectible be x =>
Free (QF be db s) x -> SelectBuilder be db x
buildQuery Free (QF be db s) a'
now'
               tblSource :: BeamSqlBackendSelectSyntax be
tblSource = Text -> SelectBuilder be db a' -> BeamSqlBackendSelectSyntax be
forall be (db :: (* -> *) -> *) a.
(BeamSqlBackend be, Projectible be a) =>
Text -> SelectBuilder be db a -> BeamSqlBackendSelectSyntax be
buildSelect Text
tblPfx SelectBuilder be db a'
sb
               (a'
x', QueryBuilder be
qb') = Text
-> BeamSqlBackendSelectSyntax be
-> a'
-> QueryBuilder be
-> (a', QueryBuilder be)
forall be x.
(BeamSqlBackend be, Projectible be x) =>
Text
-> BeamSqlBackendSelectSyntax be
-> x
-> QueryBuilder be
-> (x, QueryBuilder be)
buildJoinTableSourceQuery Text
tblPfx BeamSqlBackendSelectSyntax be
tblSource (SelectBuilder be db a' -> a'
forall syntax (db :: (* -> *) -> *) a.
SelectBuilder syntax db a -> a
sbProj SelectBuilder be db a'
sb) QueryBuilder be
qb
           in Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
forall s x.
Projectible be x =>
Free (QF be db s) x -> QueryBuilder be -> SelectBuilder be db x
buildJoinedQuery (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 :: 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 Text -> Text -> BeamSqlBackendFromSyntax be
entityNm Text -> r
mkTbl r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn (Text, 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 =
      Free (QF be db s) (PreserveLeft Text r)
-> (PreserveLeft Text r -> Free (QF be db s) x)
-> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) (PreserveLeft Text r))
-> Free (QF be db s) (PreserveLeft Text r)
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((Text -> Text -> BeamSqlBackendFromSyntax be)
-> (Text -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((Text, r) -> Free (QF be db s) (PreserveLeft Text r))
-> QF be db s (Free (QF be db s) (PreserveLeft Text r))
forall be r next (db :: (* -> *) -> *) s.
Projectible be r =>
(Text -> Text -> BeamSqlBackendFromSyntax be)
-> (Text -> r)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((Text, r) -> next)
-> QF be db s next
QAll Text -> Text -> BeamSqlBackendFromSyntax be
entityNm Text -> r
mkTbl r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn (PreserveLeft Text r -> Free (QF be db s) (PreserveLeft Text r)
forall (f :: * -> *) a. a -> Free f a
Pure (PreserveLeft Text r -> Free (QF be db s) (PreserveLeft Text r))
-> ((Text, r) -> PreserveLeft Text r)
-> (Text, r)
-> Free (QF be db s) (PreserveLeft Text r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, r) -> PreserveLeft Text r
forall a b. (a, b) -> PreserveLeft a b
PreserveLeft))) ((Text, r) -> Free (QF be db s) x
next ((Text, r) -> Free (QF be db s) x)
-> (PreserveLeft Text r -> (Text, r))
-> PreserveLeft Text r
-> Free (QF be db s) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PreserveLeft Text r -> (Text, r)
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 BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> BeamSqlBackendFromSyntax be
mkJoin r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax 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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (QM be db (QNested s) r
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> (r
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
forall be r (db :: (* -> *) -> *) s next.
Projectible be r =>
QM be db (QNested s) r
-> (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 BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> BeamSqlBackendFromSyntax be
mkJoin r -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn r -> Free (QF be db s) r
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 BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> BeamSqlBackendFromSyntax be
mkJoin (a, b)
-> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax 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 =
      Free (QF be db s) (a, b)
-> ((a, b) -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) (a, b)) -> Free (QF be db s) (a, b)
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (QM be db (QNested s) a
-> QM be db (QNested s) b
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> ((a, b)
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((a, b) -> Free (QF be db s) (a, b))
-> QF be db s (Free (QF be db s) (a, b))
forall be a b (db :: (* -> *) -> *) s next.
(Projectible be a, Projectible be b) =>
QM be db (QNested s) a
-> QM be db (QNested s) b
-> (BeamSqlBackendFromSyntax be
    -> BeamSqlBackendFromSyntax be
    -> Maybe (BeamSqlBackendExpressionSyntax be)
    -> BeamSqlBackendFromSyntax be)
-> ((a, b)
    -> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be)))
-> ((a, b) -> next)
-> QF be db s next
QTwoWayJoin QM be db (QNested s) a
a QM be db (QNested s) b
b BeamSqlBackendFromSyntax be
-> BeamSqlBackendFromSyntax be
-> Maybe (BeamSqlBackendExpressionSyntax be)
-> BeamSqlBackendFromSyntax be
mkJoin (a, b)
-> Maybe (WithExprContext (BeamSqlBackendExpressionSyntax be))
mkOn (a, b) -> Free (QF be db s) (a, b)
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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (QM be db (QNested s) r
-> (r -> Free (QF be db s) r) -> QF be db s (Free (QF be db s) r)
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' r -> Free (QF be db s) r
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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (Integer
-> QM be db (QNested s) r
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
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' r -> Free (QF be db s) r
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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (Integer
-> QM be db (QNested s) r
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
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' r -> Free (QF be db s) r
forall (f :: * -> *) a. a -> Free f a
Pure)) r -> Free (QF be db s) x
next
    onlyQ (Free (QSetOp Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax 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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
 -> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be))
-> QM be db (QNested s) r
-> QM be db (QNested s) r
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
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 (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
-> Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be)
combine QM be db (QNested s) r
a QM be db (QNested s) r
b r -> Free (QF be db s) r
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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((r -> WithExprContext [BeamSqlBackendOrderingSyntax be])
-> QM be db (QNested s) r
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
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' r -> Free (QF be db s) r
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 =
      Free (QF be db s) a
-> (a -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) a) -> Free (QF be db s) a
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((r -> window)
-> (r -> window -> a)
-> QM be db (QNested s) r
-> (a -> Free (QF be db s) a)
-> QF be db s (Free (QF be db s) a)
forall be window r a (db :: (* -> *) -> *) s next.
(ProjectibleWithPredicate
   WindowFrameContext
   be
   (WithExprContext (BeamSqlBackendWindowFrameSyntax' be))
   window,
 Projectible be r, Projectible be a) =>
(r -> window)
-> (r -> window -> a)
-> QM be db (QNested s) r
-> (a -> next)
-> QF be db s next
QWindowOver r -> window
mkWindow r -> window -> a
mkProj QM be db (QNested s) r
q' a -> Free (QF be db s) a
forall (f :: * -> *) a. a -> Free f a
Pure)) a -> Free (QF be db s) x
next
    onlyQ (Free (QAggregate a
-> Text
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax 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 =
      Free (QF be db s) grouping
-> (grouping -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) grouping)
-> Free (QF be db s) grouping
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((a
 -> Text
 -> (Maybe
       (Sql92SelectTableGroupingSyntax
          (Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be))),
     grouping))
-> QM be db (QNested s) a
-> (grouping -> Free (QF be db s) grouping)
-> QF be db s (Free (QF be db s) grouping)
forall be grouping a (db :: (* -> *) -> *) s next.
(Projectible be grouping, Projectible be a) =>
(a -> Text -> (Maybe (BeamSqlBackendGroupingSyntax be), grouping))
-> QM be db (QNested s) a -> (grouping -> next) -> QF be db s next
QAggregate a
-> Text
-> (Maybe
      (Sql92SelectTableGroupingSyntax
         (Sql92SelectSelectTableSyntax (BeamSqlBackendSelectSyntax be))),
    grouping)
mkAgg QM be db (QNested s) a
q' grouping -> Free (QF be db s) grouping
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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((r -> WithExprContext (BeamSqlBackendSetQuantifierSyntax be))
-> QM be db s r
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
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' r -> Free (QF be db s) r
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 =
      Free (QF be db s) r
-> (r -> Free (QF be db s) x) -> SelectBuilder be db x
forall a'.
Projectible be a' =>
Free (QF be db s) a'
-> (a' -> Free (QF be db s) x) -> SelectBuilder be db x
f (QF be db s (Free (QF be db s) r) -> Free (QF be db s) r
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free ((r -> SelectStmtFn be)
-> QM be db (QNested s) r
-> (r -> Free (QF be db s) r)
-> QF be db s (Free (QF be db s) r)
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 r -> Free (QF be db s) r
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
_ = String -> SelectBuilder be db x
forall a. HasCallStack => String -> a
error String
"impossible"