-- | This module defines functions that translate from the Postgres IR into
-- Postgres SQL AST.
--
-- NOTE: These functions 'processAnnAggregateSelect', 'processAnnSimpleSelect',
-- 'processConnectionSelect', are all mutually recursive.
--
-- These functions are generally called from the top level functions in
-- Translate.Select, and the call stack looks like:
--
--     * 'selectQuerySQL' -> 'mkSQLSelect' -> 'processAnnSimpleSelect' -> 'processSelectParams'/'processAnnFields'
--
--     * 'selectAggregateQuerySQL' -> 'mkAggregateSelect' -> 'processAnnAggregateSelect' -> 'processSelectParams'/'processAnnFields'
--
--     * 'connetionSelectQuerySQL' -> 'mkConnectionSelect' -> 'processConnectionSelection' -> 'processSelectParams'
--
-- 'SelectSource' consists of a prefix, a source, a boolean conditional
-- expression, and info on whether sorting or slicing is done (needed to handle
-- the LIMIT optimisation)
module Hasura.Backends.Postgres.Translate.Select.Internal.Process
  ( processAnnAggregateSelect,
    processAnnSimpleSelect,
    processConnectionSelect,
  )
where

import Data.HashMap.Strict qualified as HashMap
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.List.NonEmpty qualified as NE
import Data.Text.Extended (ToTxt (toTxt))
import Data.Text.NonEmpty qualified as TNE
import Hasura.Backends.Postgres.SQL.DML qualified as S
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.Translate.BoolExp (toSQLBoolExp)
import Hasura.Backends.Postgres.Translate.Column (toJSONableExp)
import Hasura.Backends.Postgres.Translate.Select.AnnotatedFieldJSON
import Hasura.Backends.Postgres.Translate.Select.Internal.Aliases
  ( contextualizeAggregateInput,
    contextualizeBaseTableColumn,
    contextualizeField,
    mkAnnOrderByAlias,
    mkArrayRelationAlias,
    mkArrayRelationSourcePrefix,
    mkBaseTableIdentifier,
    mkComputedFieldTableIdentifier,
    mkObjectRelationTableAlias,
    mkOrderByFieldName,
  )
import Hasura.Backends.Postgres.Translate.Select.Internal.Extractor
  ( aggregateFieldsToExtractorExps,
    asJsonAggExtr,
    mkRawComputedFieldExpression,
    withColumnOp,
    withJsonAggExtr,
    withRedactionExp,
  )
import Hasura.Backends.Postgres.Translate.Select.Internal.Helpers
  ( cursorIdentifier,
    cursorsSelectAliasIdentifier,
    encodeBase64,
    endCursorIdentifier,
    fromTableRowArgs,
    hasNextPageIdentifier,
    hasPreviousPageIdentifier,
    nativeQueryNameToAlias,
    pageInfoSelectAliasIdentifier,
    startCursorIdentifier,
    withForceAggregation,
  )
import Hasura.Backends.Postgres.Translate.Select.Internal.JoinTree
  ( withWriteArrayConnection,
    withWriteArrayRelation,
    withWriteComputedFieldTableSet,
    withWriteObjectRelation,
  )
import Hasura.Backends.Postgres.Translate.Select.Internal.OrderBy (processOrderByItems)
import Hasura.Backends.Postgres.Translate.Types
import Hasura.Backends.Postgres.Types.Aggregates
import Hasura.GraphQL.Schema.Node (currentNodeIdVersion, nodeIdVersionInt)
import Hasura.NativeQuery.IR (NativeQuery (..))
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.IR.OrderBy (OrderByItemG (OrderByItemG, obiColumn))
import Hasura.RQL.IR.Select
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.NamingCase (NamingCase)
import Hasura.RQL.Types.Relationships.Local
import Hasura.RQL.Types.Schema.Options qualified as Options

processSelectParams ::
  forall pgKind m.
  ( MonadReader Options.StringifyNumbers m,
    MonadState NativeQueryFreshIdStore m,
    MonadWriter SelectWriter m,
    Backend ('Postgres pgKind)
  ) =>
  SourcePrefixes ->
  FieldName ->
  SimilarArrayFields ->
  SelectFrom ('Postgres pgKind) ->
  PermissionLimitSubQuery ->
  TablePerm ('Postgres pgKind) ->
  SelectArgs ('Postgres pgKind) ->
  m
    ( SelectSource,
      [(S.ColumnAlias, S.SQLExp)],
      Maybe S.SQLExp -- Order by cursor
    )
processSelectParams :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind)) =>
SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
processSelectParams
  SourcePrefixes
sourcePrefixes
  FieldName
fieldAlias
  SimilarArrayFields
similarArrFields
  SelectFrom ('Postgres pgKind)
selectFrom
  PermissionLimitSubQuery
permLimitSubQ
  TablePerm ('Postgres pgKind)
tablePermissions
  SelectArgs ('Postgres pgKind)
tableArgs = do
    let prefix :: TableIdentifier
prefix = Identifier -> TableIdentifier
identifierToTableIdentifier (Identifier -> TableIdentifier) -> Identifier -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ SourcePrefixes -> Identifier
_pfBase SourcePrefixes
sourcePrefixes
    (Qual
selectSourceQual, FromItem
fromItem) <- TableIdentifier
-> SelectFrom ('Postgres pgKind) -> m (Qual, FromItem)
selectFromToQual TableIdentifier
prefix SelectFrom ('Postgres pgKind)
selectFrom
    ([(ColumnAlias, SQLExp)]
additionalExtrs, SelectSorting
selectSorting, Maybe SQLExp
cursorExp) <-
      TableIdentifier
-> Qual
-> FieldName
-> SimilarArrayFields
-> Maybe (NonEmpty (AnnDistinctColumn ('Postgres pgKind) SQLExp))
-> Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
-> m ([(ColumnAlias, SQLExp)], SelectSorting, Maybe SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind)) =>
TableIdentifier
-> Qual
-> FieldName
-> SimilarArrayFields
-> Maybe (NonEmpty (AnnDistinctColumn ('Postgres pgKind) SQLExp))
-> Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
-> m ([(ColumnAlias, SQLExp)], SelectSorting, Maybe SQLExp)
processOrderByItems (Identifier -> TableIdentifier
identifierToTableIdentifier Identifier
thisSourcePrefix) Qual
selectSourceQual FieldName
fieldAlias SimilarArrayFields
similarArrFields Maybe
  (NonEmpty
     (AnnDistinctColumn
        ('Postgres pgKind) (SQLExpression ('Postgres pgKind))))
Maybe (NonEmpty (AnnDistinctColumn ('Postgres pgKind) SQLExp))
distM Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
orderByM
    let finalWhere :: BoolExp
finalWhere =
          Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
toSQLBoolExp Qual
selectSourceQual
            (AnnBoolExpSQL ('Postgres pgKind) -> BoolExp)
-> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
forall a b. (a -> b) -> a -> b
$ AnnBoolExp ('Postgres pgKind) SQLExp
-> (AnnBoolExp ('Postgres pgKind) SQLExp
    -> AnnBoolExp ('Postgres pgKind) SQLExp)
-> Maybe (AnnBoolExp ('Postgres pgKind) SQLExp)
-> AnnBoolExp ('Postgres pgKind) SQLExp
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AnnBoolExpSQL ('Postgres pgKind)
AnnBoolExp ('Postgres pgKind) SQLExp
permFilter (AnnBoolExp ('Postgres pgKind) SQLExp
-> AnnBoolExp ('Postgres pgKind) SQLExp
-> AnnBoolExp ('Postgres pgKind) SQLExp
forall (backend :: BackendType) scalar.
AnnBoolExp backend scalar
-> AnnBoolExp backend scalar -> AnnBoolExp backend scalar
andAnnBoolExps AnnBoolExpSQL ('Postgres pgKind)
AnnBoolExp ('Postgres pgKind) SQLExp
permFilter) Maybe (AnnBoolExpSQL ('Postgres pgKind))
Maybe (AnnBoolExp ('Postgres pgKind) SQLExp)
whereM
        sortingAndSlicing :: SortingAndSlicing
sortingAndSlicing = SelectSorting -> SelectSlicing -> SortingAndSlicing
SortingAndSlicing SelectSorting
selectSorting SelectSlicing
selectSlicing
        selectSource :: SelectSource
selectSource =
          Identifier
-> FromItem -> BoolExp -> SortingAndSlicing -> SelectSource
SelectSource
            Identifier
thisSourcePrefix
            FromItem
fromItem
            BoolExp
finalWhere
            SortingAndSlicing
sortingAndSlicing
    (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      ( SelectSource
selectSource,
        [(ColumnAlias, SQLExp)]
additionalExtrs,
        Maybe SQLExp
cursorExp
      )
    where
      thisSourcePrefix :: Identifier
thisSourcePrefix = SourcePrefixes -> Identifier
_pfThis SourcePrefixes
sourcePrefixes
      SelectArgs Maybe (AnnBoolExpSQL ('Postgres pgKind))
whereM Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
orderByM Maybe Int
inpLimitM Maybe Int64
offsetM Maybe
  (NonEmpty
     (AnnDistinctColumn
        ('Postgres pgKind) (SQLExpression ('Postgres pgKind))))
distM = SelectArgs ('Postgres pgKind)
tableArgs
      TablePerm AnnBoolExpSQL ('Postgres pgKind)
permFilter Maybe Int
permLimit = TablePerm ('Postgres pgKind)
tablePermissions
      selectSlicing :: SelectSlicing
selectSlicing = Maybe Int -> Maybe Int64 -> SelectSlicing
SelectSlicing Maybe Int
finalLimit Maybe Int64
offsetM
      finalLimit :: Maybe Int
finalLimit =
        -- if sub query is required, then only use input limit
        --    because permission limit is being applied in subquery
        -- else compare input and permission limits
        case PermissionLimitSubQuery
permLimitSubQ of
          PLSQRequired Int
_ -> Maybe Int
inpLimitM
          PermissionLimitSubQuery
PLSQNotRequired -> Maybe Int
compareLimits

      compareLimits :: Maybe Int
compareLimits =
        case (Maybe Int
inpLimitM, Maybe Int
permLimit) of
          (Maybe Int
inpLim, Maybe Int
Nothing) -> Maybe Int
inpLim
          (Maybe Int
Nothing, Maybe Int
permLim) -> Maybe Int
permLim
          (Just Int
inp, Just Int
perm) -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
inp Int
perm)

      selectFromToQual :: TableIdentifier -> SelectFrom ('Postgres pgKind) -> m (S.Qual, S.FromItem)
      selectFromToQual :: TableIdentifier
-> SelectFrom ('Postgres pgKind) -> m (Qual, FromItem)
selectFromToQual TableIdentifier
prefix = \case
        FromTable TableName ('Postgres pgKind)
table -> (Qual, FromItem) -> m (Qual, FromItem)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Qual, FromItem) -> m (Qual, FromItem))
-> (Qual, FromItem) -> m (Qual, FromItem)
forall a b. (a -> b) -> a -> b
$ (QualifiedTable -> Qual
S.QualTable TableName ('Postgres pgKind)
QualifiedTable
table, QualifiedTable -> Maybe TableAlias -> FromItem
S.FISimple TableName ('Postgres pgKind)
QualifiedTable
table Maybe TableAlias
forall a. Maybe a
Nothing)
        FromIdentifier FIIdentifier
i -> do
          let ti :: TableIdentifier
ti = Text -> TableIdentifier
TableIdentifier (Text -> TableIdentifier) -> Text -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ FIIdentifier -> Text
unFIIdentifier FIIdentifier
i
          (Qual, FromItem) -> m (Qual, FromItem)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Qual, FromItem) -> m (Qual, FromItem))
-> (Qual, FromItem) -> m (Qual, FromItem)
forall a b. (a -> b) -> a -> b
$ (TableIdentifier -> Maybe TypeAnn -> Qual
S.QualifiedIdentifier TableIdentifier
ti Maybe TypeAnn
forall a. Maybe a
Nothing, TableIdentifier -> FromItem
S.FIIdentifier TableIdentifier
ti)
        FromFunction FunctionName ('Postgres pgKind)
qf FunctionArgsExp
  ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
args Maybe [(Column ('Postgres pgKind), ScalarType ('Postgres pgKind))]
defListM -> do
          let fi :: FromItem
fi =
                FunctionExp -> FromItem
S.FIFunc
                  (FunctionExp -> FromItem) -> FunctionExp -> FromItem
forall a b. (a -> b) -> a -> b
$ QualifiedFunction
-> FunctionArgs -> Maybe FunctionAlias -> FunctionExp
S.FunctionExp FunctionName ('Postgres pgKind)
QualifiedFunction
qf (TableIdentifier
-> FunctionArgsExpG (ArgumentExp SQLExp) -> FunctionArgs
fromTableRowArgs TableIdentifier
prefix FunctionArgsExp
  ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
FunctionArgsExpG (ArgumentExp SQLExp)
args)
                  (Maybe FunctionAlias -> FunctionExp)
-> Maybe FunctionAlias -> FunctionExp
forall a b. (a -> b) -> a -> b
$ FunctionAlias -> Maybe FunctionAlias
forall a. a -> Maybe a
Just
                  (FunctionAlias -> Maybe FunctionAlias)
-> FunctionAlias -> Maybe FunctionAlias
forall a b. (a -> b) -> a -> b
$ QualifiedFunction
-> Maybe [(ColumnAlias, PGScalarType)] -> FunctionAlias
S.mkFunctionAlias
                    FunctionName ('Postgres pgKind)
QualifiedFunction
qf
                    (([(PGCol, PGScalarType)] -> [(ColumnAlias, PGScalarType)])
-> Maybe [(PGCol, PGScalarType)]
-> Maybe [(ColumnAlias, PGScalarType)]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((PGCol, PGScalarType) -> (ColumnAlias, PGScalarType))
-> [(PGCol, PGScalarType)] -> [(ColumnAlias, PGScalarType)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((PGCol -> ColumnAlias)
-> (PGCol, PGScalarType) -> (ColumnAlias, PGScalarType)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first PGCol -> ColumnAlias
forall a. IsIdentifier a => a -> ColumnAlias
S.toColumnAlias)) Maybe [(Column ('Postgres pgKind), ScalarType ('Postgres pgKind))]
Maybe [(PGCol, PGScalarType)]
defListM)
          (Qual, FromItem) -> m (Qual, FromItem)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Qual, FromItem) -> m (Qual, FromItem))
-> (Qual, FromItem) -> m (Qual, FromItem)
forall a b. (a -> b) -> a -> b
$ (TableIdentifier -> Maybe TypeAnn -> Qual
S.QualifiedIdentifier (Text -> TableIdentifier
TableIdentifier (Text -> TableIdentifier) -> Text -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ QualifiedFunction -> Text
forall a. ToTxt a => QualifiedObject a -> Text
qualifiedObjectToText FunctionName ('Postgres pgKind)
QualifiedFunction
qf) Maybe TypeAnn
forall a. Maybe a
Nothing, FromItem
fi)
        FromStoredProcedure {} -> [Char] -> m (Qual, FromItem)
forall a. HasCallStack => [Char] -> a
error [Char]
"selectFromToQual: FromStoredProcedure"
        FromNativeQuery NativeQuery ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
nq -> do
          TableAlias
cteName <- NativeQuery ('Postgres pgKind) SQLExp -> m TableAlias
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m) =>
NativeQuery ('Postgres pgKind) SQLExp -> m TableAlias
fromNativeQuery NativeQuery ('Postgres pgKind) (SQLExpression ('Postgres pgKind))
NativeQuery ('Postgres pgKind) SQLExp
nq
          let ta :: TableIdentifier
ta = TableAlias -> TableIdentifier
S.tableAliasToIdentifier TableAlias
cteName
          (Qual, FromItem) -> m (Qual, FromItem)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Qual, FromItem) -> m (Qual, FromItem))
-> (Qual, FromItem) -> m (Qual, FromItem)
forall a b. (a -> b) -> a -> b
$ (TableIdentifier -> Maybe TypeAnn -> Qual
S.QualifiedIdentifier TableIdentifier
ta Maybe TypeAnn
forall a. Maybe a
Nothing, TableIdentifier -> FromItem
S.FIIdentifier TableIdentifier
ta)

fromNativeQuery ::
  forall pgKind m.
  ( MonadWriter SelectWriter m,
    MonadState NativeQueryFreshIdStore m
  ) =>
  NativeQuery ('Postgres pgKind) S.SQLExp ->
  m S.TableAlias
fromNativeQuery :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m) =>
NativeQuery ('Postgres pgKind) SQLExp -> m TableAlias
fromNativeQuery NativeQuery ('Postgres pgKind) SQLExp
nq = do
  Int
freshId <- NativeQueryFreshIdStore -> Int
nqNextFreshId (NativeQueryFreshIdStore -> Int)
-> m NativeQueryFreshIdStore -> m Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m NativeQueryFreshIdStore
forall s (m :: * -> *). MonadState s m => m s
get
  (NativeQueryFreshIdStore -> NativeQueryFreshIdStore) -> m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify NativeQueryFreshIdStore -> NativeQueryFreshIdStore
forall a. Enum a => a -> a
succ

  -- we are going to cram our SQL in a CTE, and this is what we will call it
  let cteName :: TableAlias
cteName = NativeQueryName -> Int -> TableAlias
nativeQueryNameToAlias (NativeQuery ('Postgres pgKind) SQLExp -> NativeQueryName
forall (b :: BackendType) field.
NativeQuery b field -> NativeQueryName
nqRootFieldName NativeQuery ('Postgres pgKind) SQLExp
nq) Int
freshId

  -- emit the query itself to the Writer
  SelectWriter -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    (SelectWriter -> m ()) -> SelectWriter -> m ()
forall a b. (a -> b) -> a -> b
$ SelectWriter
forall a. Monoid a => a
mempty
      { _swCustomSQLCTEs :: CustomSQLCTEs
_swCustomSQLCTEs =
          HashMap TableAlias (InterpolatedQuery SQLExp) -> CustomSQLCTEs
CustomSQLCTEs (TableAlias
-> InterpolatedQuery SQLExp
-> HashMap TableAlias (InterpolatedQuery SQLExp)
forall k v. Hashable k => k -> v -> HashMap k v
HashMap.singleton TableAlias
cteName (NativeQuery ('Postgres pgKind) SQLExp -> InterpolatedQuery SQLExp
forall (b :: BackendType) field.
NativeQuery b field -> InterpolatedQuery field
nqInterpolatedQuery NativeQuery ('Postgres pgKind) SQLExp
nq))
      }

  TableAlias -> m TableAlias
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return TableAlias
cteName

processAnnAggregateSelect ::
  forall pgKind m.
  ( MonadReader Options.StringifyNumbers m,
    MonadWriter SelectWriter m,
    MonadState NativeQueryFreshIdStore m,
    Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  SourcePrefixes ->
  FieldName ->
  AnnAggregateSelect ('Postgres pgKind) ->
  m
    ( SelectSource,
      InsOrdHashMap S.ColumnAlias S.SQLExp,
      S.Extractor
    )
processAnnAggregateSelect :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> AnnAggregateSelect ('Postgres pgKind)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp, Extractor)
processAnnAggregateSelect SourcePrefixes
sourcePrefixes FieldName
fieldAlias AnnAggregateSelect ('Postgres pgKind)
annAggSel = do
  (SelectSource
selectSource, [(ColumnAlias, SQLExp)]
orderByAndDistinctExtrs, Maybe SQLExp
_) <-
    SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind)) =>
SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
processSelectParams
      SourcePrefixes
sourcePrefixes
      FieldName
fieldAlias
      SimilarArrayFields
similarArrayFields
      SelectFrom ('Postgres pgKind)
tableFrom
      PermissionLimitSubQuery
permLimitSubQuery
      TablePerm ('Postgres pgKind)
tablePermissions
      SelectArgs ('Postgres pgKind)
tableArgs
  let thisSourcePrefix :: TableIdentifier
thisSourcePrefix = Identifier -> TableIdentifier
identifierToTableIdentifier (Identifier -> TableIdentifier) -> Identifier -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ SourcePrefixes -> Identifier
_pfThis SourcePrefixes
sourcePrefixes
  [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
processedFields <- [(FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)]
-> ((FieldName,
     TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
    -> m (FieldName, ([(ColumnAlias, SQLExp)], SQLExp)))
-> m [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Fields
  (TableAggregateFieldG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
[(FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)]
aggSelFields (((FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
  -> m (FieldName, ([(ColumnAlias, SQLExp)], SQLExp)))
 -> m [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))])
-> ((FieldName,
     TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
    -> m (FieldName, ([(ColumnAlias, SQLExp)], SQLExp)))
-> m [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
forall a b. (a -> b) -> a -> b
$ \(FieldName
fieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp
field) ->
    (FieldName
fieldName,)
      (([(ColumnAlias, SQLExp)], SQLExp)
 -> (FieldName, ([(ColumnAlias, SQLExp)], SQLExp)))
-> m ([(ColumnAlias, SQLExp)], SQLExp)
-> m (FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case TableAggregateFieldG ('Postgres pgKind) Void SQLExp
field of
        TAFAgg AggregateFields ('Postgres pgKind) SQLExp
aggFields ->
          ([(ColumnAlias, SQLExp)], SQLExp)
-> m ([(ColumnAlias, SQLExp)], SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( TableIdentifier
-> AggregateFields ('Postgres pgKind) SQLExp
-> [(ColumnAlias, SQLExp)]
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
TableIdentifier
-> AggregateFields ('Postgres pgKind) SQLExp
-> [(ColumnAlias, SQLExp)]
aggregateFieldsToExtractorExps TableIdentifier
thisSourcePrefix AggregateFields ('Postgres pgKind) SQLExp
aggFields,
              TableIdentifier
-> AggregateFields ('Postgres pgKind) SQLExp
-> StringifyNumbers
-> SQLExp
forall (pgKind :: PostgresKind).
TableIdentifier
-> AggregateFields ('Postgres pgKind) SQLExp
-> StringifyNumbers
-> SQLExp
aggregateFieldToExp TableIdentifier
thisSourcePrefix AggregateFields ('Postgres pgKind) SQLExp
aggFields StringifyNumbers
strfyNum
            )
        TAFNodes XNodesAgg ('Postgres pgKind)
_ AnnFieldsG ('Postgres pgKind) Void SQLExp
annFields -> do
          let nodesColumnAlias :: ColumnAlias
nodesColumnAlias = TableIdentifier -> FieldName -> ColumnAlias
contextualizeField TableIdentifier
thisSourcePrefix FieldName
fieldName
          (ColumnAlias
_, SQLExp
nodesExtractorExp) <- TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
processAnnFields TableIdentifier
thisSourcePrefix FieldName
fieldName AnnFields ('Postgres pgKind)
AnnFieldsG ('Postgres pgKind) Void SQLExp
annFields Maybe NamingCase
tCase
          ([(ColumnAlias, SQLExp)], SQLExp)
-> m ([(ColumnAlias, SQLExp)], SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( [(ColumnAlias
nodesColumnAlias, SQLExp
nodesExtractorExp)],
              PermissionLimitSubQuery
-> Maybe OrderByExp -> ColumnAlias -> SQLExp
withJsonAggExtr PermissionLimitSubQuery
permLimitSubQuery (SelectSource -> Maybe OrderByExp
orderByForJsonAgg SelectSource
selectSource) ColumnAlias
nodesColumnAlias
            )
        TAFGroupBy XGroupBy ('Postgres pgKind)
_xGroupBy GroupByG ('Postgres pgKind) Void SQLExp
_groupBy -> [Char] -> m ([(ColumnAlias, SQLExp)], SQLExp)
forall a. HasCallStack => [Char] -> a
error [Char]
"processAnnAggregateSelect: group_by is not yet supported"
        TAFExp Text
e ->
          ([(ColumnAlias, SQLExp)], SQLExp)
-> m ([(ColumnAlias, SQLExp)], SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( [],
              TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.textTypeAnn (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> SQLExp
S.SELit Text
e
            )

  let topLevelExtractor :: Extractor
topLevelExtractor =
        (SQLExp -> Maybe ColumnAlias -> Extractor)
-> Maybe ColumnAlias -> SQLExp -> Extractor
forall a b c. (a -> b -> c) -> b -> a -> c
flip SQLExp -> Maybe ColumnAlias -> Extractor
S.Extractor (ColumnAlias -> Maybe ColumnAlias
forall a. a -> Maybe a
Just (ColumnAlias -> Maybe ColumnAlias)
-> ColumnAlias -> Maybe ColumnAlias
forall a b. (a -> b) -> a -> b
$ Identifier -> ColumnAlias
forall a. IsIdentifier a => a -> ColumnAlias
S.toColumnAlias (Identifier -> ColumnAlias) -> Identifier -> ColumnAlias
forall a b. (a -> b) -> a -> b
$ FieldName -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier FieldName
fieldAlias)
          (SQLExp -> Extractor) -> SQLExp -> Extractor
forall a b. (a -> b) -> a -> b
$ [SQLExp] -> SQLExp
S.applyJsonBuildObj
          ([SQLExp] -> SQLExp) -> [SQLExp] -> SQLExp
forall a b. (a -> b) -> a -> b
$ (((FieldName, SQLExp) -> [SQLExp])
 -> [(FieldName, SQLExp)] -> [SQLExp])
-> [(FieldName, SQLExp)]
-> ((FieldName, SQLExp) -> [SQLExp])
-> [SQLExp]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((FieldName, SQLExp) -> [SQLExp])
-> [(FieldName, SQLExp)] -> [SQLExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (((FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
 -> (FieldName, SQLExp))
-> [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
-> [(FieldName, SQLExp)]
forall a b. (a -> b) -> [a] -> [b]
map ((([(ColumnAlias, SQLExp)], SQLExp) -> SQLExp)
-> (FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
-> (FieldName, SQLExp)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ([(ColumnAlias, SQLExp)], SQLExp) -> SQLExp
forall a b. (a, b) -> b
snd) [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
processedFields)
          (((FieldName, SQLExp) -> [SQLExp]) -> [SQLExp])
-> ((FieldName, SQLExp) -> [SQLExp]) -> [SQLExp]
forall a b. (a -> b) -> a -> b
$ \(FieldName Text
fieldText, SQLExp
fieldExp) -> [Text -> SQLExp
S.SELit Text
fieldText, SQLExp
fieldExp]
      nodeExtractors :: InsOrdHashMap ColumnAlias SQLExp
nodeExtractors =
        [(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList
          ([(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp)
-> [(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp
forall a b. (a -> b) -> a -> b
$ ((FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
 -> [(ColumnAlias, SQLExp)])
-> [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
-> [(ColumnAlias, SQLExp)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (([(ColumnAlias, SQLExp)], SQLExp) -> [(ColumnAlias, SQLExp)]
forall a b. (a, b) -> a
fst (([(ColumnAlias, SQLExp)], SQLExp) -> [(ColumnAlias, SQLExp)])
-> ((FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
    -> ([(ColumnAlias, SQLExp)], SQLExp))
-> (FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
-> [(ColumnAlias, SQLExp)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FieldName, ([(ColumnAlias, SQLExp)], SQLExp))
-> ([(ColumnAlias, SQLExp)], SQLExp)
forall a b. (a, b) -> b
snd) [(FieldName, ([(ColumnAlias, SQLExp)], SQLExp))]
processedFields
          [(ColumnAlias, SQLExp)]
-> [(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]
forall a. Semigroup a => a -> a -> a
<> [(ColumnAlias, SQLExp)]
orderByAndDistinctExtrs

  (SelectSource, InsOrdHashMap ColumnAlias SQLExp, Extractor)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp, Extractor)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SelectSource
selectSource, InsOrdHashMap ColumnAlias SQLExp
nodeExtractors, Extractor
topLevelExtractor)
  where
    AnnSelectG Fields
  (TableAggregateFieldG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
aggSelFields SelectFrom ('Postgres pgKind)
tableFrom TablePerm ('Postgres pgKind)
tablePermissions SelectArgs ('Postgres pgKind)
tableArgs StringifyNumbers
strfyNum Maybe NamingCase
tCase = AnnAggregateSelect ('Postgres pgKind)
annAggSel
    permLimit :: Maybe Int
permLimit = TablePermG ('Postgres pgKind) SQLExp -> Maybe Int
forall (b :: BackendType) v. TablePermG b v -> Maybe Int
_tpLimit TablePerm ('Postgres pgKind)
TablePermG ('Postgres pgKind) SQLExp
tablePermissions
    orderBy :: Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
orderBy = SelectArgsG ('Postgres pgKind) SQLExp
-> Maybe
     (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
forall (b :: BackendType) v.
SelectArgsG b v -> Maybe (NonEmpty (AnnotatedOrderByItemG b v))
_saOrderBy SelectArgs ('Postgres pgKind)
SelectArgsG ('Postgres pgKind) SQLExp
tableArgs
    permLimitSubQuery :: PermissionLimitSubQuery
permLimitSubQuery = Maybe Int
-> Fields
     (TableAggregateFieldG
        ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
-> Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
-> PermissionLimitSubQuery
mkPermissionLimitSubQuery Maybe Int
permLimit Fields
  (TableAggregateFieldG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
aggSelFields Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
orderBy
    similarArrayFields :: SimilarArrayFields
similarArrayFields = [SimilarArrayFields] -> SimilarArrayFields
forall k v. (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
HashMap.unions
      ([SimilarArrayFields] -> SimilarArrayFields)
-> [SimilarArrayFields] -> SimilarArrayFields
forall a b. (a -> b) -> a -> b
$ ((TableAggregateFieldG ('Postgres pgKind) Void SQLExp
  -> SimilarArrayFields)
 -> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp]
 -> [SimilarArrayFields])
-> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp]
-> (TableAggregateFieldG ('Postgres pgKind) Void SQLExp
    -> SimilarArrayFields)
-> [SimilarArrayFields]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (TableAggregateFieldG ('Postgres pgKind) Void SQLExp
 -> SimilarArrayFields)
-> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp]
-> [SimilarArrayFields]
forall a b. (a -> b) -> [a] -> [b]
map (((FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
 -> TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
-> [(FieldName,
     TableAggregateFieldG ('Postgres pgKind) Void SQLExp)]
-> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp]
forall a b. (a -> b) -> [a] -> [b]
map (FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
-> TableAggregateFieldG ('Postgres pgKind) Void SQLExp
forall a b. (a, b) -> b
snd Fields
  (TableAggregateFieldG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
[(FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)]
aggSelFields)
      ((TableAggregateFieldG ('Postgres pgKind) Void SQLExp
  -> SimilarArrayFields)
 -> [SimilarArrayFields])
-> (TableAggregateFieldG ('Postgres pgKind) Void SQLExp
    -> SimilarArrayFields)
-> [SimilarArrayFields]
forall a b. (a -> b) -> a -> b
$ \case
        TAFAgg AggregateFields ('Postgres pgKind) SQLExp
_ -> SimilarArrayFields
forall a. Monoid a => a
mempty
        TAFNodes XNodesAgg ('Postgres pgKind)
_ AnnFieldsG ('Postgres pgKind) Void SQLExp
annFlds ->
          AnnFieldsG ('Postgres pgKind) Void SQLExp
-> Maybe
     (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
-> SimilarArrayFields
forall (pgKind :: PostgresKind) v.
(Backend ('Postgres pgKind), Eq v) =>
AnnFieldsG ('Postgres pgKind) Void v
-> Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
-> SimilarArrayFields
mkSimilarArrayFields AnnFieldsG ('Postgres pgKind) Void SQLExp
annFlds Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
orderBy
        TAFGroupBy XGroupBy ('Postgres pgKind)
_xGroupBy GroupByG ('Postgres pgKind) Void SQLExp
_groupBy -> [Char] -> SimilarArrayFields
forall a. HasCallStack => [Char] -> a
error [Char]
"similarArrayFields: group_by is not yet supported"
        TAFExp Text
_ -> SimilarArrayFields
forall a. Monoid a => a
mempty

    mkPermissionLimitSubQuery ::
      Maybe Int ->
      TableAggregateFields ('Postgres pgKind) ->
      Maybe (NE.NonEmpty (AnnotatedOrderByItem ('Postgres pgKind))) ->
      PermissionLimitSubQuery
    mkPermissionLimitSubQuery :: Maybe Int
-> Fields
     (TableAggregateFieldG
        ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
-> Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
-> PermissionLimitSubQuery
mkPermissionLimitSubQuery Maybe Int
permLimit' Fields
  (TableAggregateFieldG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
aggFields Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
orderBys =
      case Maybe Int
permLimit' of
        Maybe Int
Nothing -> PermissionLimitSubQuery
PLSQNotRequired
        Just Int
limit ->
          if Bool
hasAggregateField Bool -> Bool -> Bool
|| Bool
hasAggOrderBy
            then Int -> PermissionLimitSubQuery
PLSQRequired Int
limit
            else PermissionLimitSubQuery
PLSQNotRequired
      where
        hasAggregateField :: Bool
hasAggregateField = ((TableAggregateFieldG ('Postgres pgKind) Void SQLExp -> Bool)
 -> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp] -> Bool)
-> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp]
-> (TableAggregateFieldG ('Postgres pgKind) Void SQLExp -> Bool)
-> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip (TableAggregateFieldG ('Postgres pgKind) Void SQLExp -> Bool)
-> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (((FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
 -> TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
-> [(FieldName,
     TableAggregateFieldG ('Postgres pgKind) Void SQLExp)]
-> [TableAggregateFieldG ('Postgres pgKind) Void SQLExp]
forall a b. (a -> b) -> [a] -> [b]
map (FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)
-> TableAggregateFieldG ('Postgres pgKind) Void SQLExp
forall a b. (a, b) -> b
snd Fields
  (TableAggregateFieldG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind)))
[(FieldName, TableAggregateFieldG ('Postgres pgKind) Void SQLExp)]
aggFields)
          ((TableAggregateFieldG ('Postgres pgKind) Void SQLExp -> Bool)
 -> Bool)
-> (TableAggregateFieldG ('Postgres pgKind) Void SQLExp -> Bool)
-> Bool
forall a b. (a -> b) -> a -> b
$ \case
            TAFAgg AggregateFields ('Postgres pgKind) SQLExp
_ -> Bool
True
            TableAggregateFieldG ('Postgres pgKind) Void SQLExp
_ -> Bool
False

        hasAggOrderBy :: Bool
hasAggOrderBy = case Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
orderBys of
          Maybe (NonEmpty (AnnotatedOrderByItem ('Postgres pgKind)))
Nothing -> Bool
False
          Just NonEmpty (AnnotatedOrderByItem ('Postgres pgKind))
l -> ((AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> Bool)
 -> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp] -> Bool)
-> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp]
-> (AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> Bool)
-> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip (AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> Bool)
-> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((AnnotatedOrderByItemG ('Postgres pgKind) SQLExp
 -> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp])
-> [AnnotatedOrderByItemG ('Postgres pgKind) SQLExp]
-> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap AnnotatedOrderByItemG ('Postgres pgKind) SQLExp
-> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp]
forall a. OrderByItemG ('Postgres pgKind) a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList ([AnnotatedOrderByItemG ('Postgres pgKind) SQLExp]
 -> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp])
-> [AnnotatedOrderByItemG ('Postgres pgKind) SQLExp]
-> [AnnotatedOrderByElement ('Postgres pgKind) SQLExp]
forall a b. (a -> b) -> a -> b
$ NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp)
-> [AnnotatedOrderByItemG ('Postgres pgKind) SQLExp]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty (AnnotatedOrderByItem ('Postgres pgKind))
NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp)
l)
            ((AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> Bool)
 -> Bool)
-> (AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> Bool)
-> Bool
forall a b. (a -> b) -> a -> b
$ \case
              AOCArrayAggregation {} -> Bool
True
              AnnotatedOrderByElement ('Postgres pgKind) SQLExp
_ -> Bool
False

processAnnFields ::
  forall pgKind m.
  ( MonadReader Options.StringifyNumbers m,
    MonadWriter SelectWriter m,
    MonadState NativeQueryFreshIdStore m,
    Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  TableIdentifier ->
  FieldName ->
  AnnFields ('Postgres pgKind) ->
  Maybe NamingCase ->
  m (S.ColumnAlias, S.SQLExp)
processAnnFields :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
processAnnFields TableIdentifier
sourcePrefix FieldName
fieldAlias AnnFields ('Postgres pgKind)
annFields Maybe NamingCase
tCase = do
  [(FieldName, SQLExp)]
fieldExps <- [(FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)]
-> ((FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)
    -> m (FieldName, SQLExp))
-> m [(FieldName, SQLExp)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM AnnFields ('Postgres pgKind)
[(FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)]
annFields (((FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)
  -> m (FieldName, SQLExp))
 -> m [(FieldName, SQLExp)])
-> ((FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)
    -> m (FieldName, SQLExp))
-> m [(FieldName, SQLExp)]
forall a b. (a -> b) -> a -> b
$ \(FieldName
fieldName, AnnFieldG ('Postgres pgKind) Void SQLExp
field) ->
    (FieldName
fieldName,)
      (SQLExp -> (FieldName, SQLExp))
-> m SQLExp -> m (FieldName, SQLExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case AnnFieldG ('Postgres pgKind) Void SQLExp
field of
        AFExpression Text
t -> SQLExp -> m SQLExp
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> m SQLExp) -> SQLExp -> m SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> SQLExp
S.SELit Text
t
        AFNodeId XRelay ('Postgres pgKind)
_ SourceName
sn TableName ('Postgres pgKind)
tn PrimaryKeyColumns ('Postgres pgKind)
pKeys -> SQLExp -> m SQLExp
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> m SQLExp) -> SQLExp -> m SQLExp
forall a b. (a -> b) -> a -> b
$ SourceName
-> QualifiedTable -> PrimaryKeyColumns ('Postgres pgKind) -> SQLExp
mkNodeId SourceName
sn TableName ('Postgres pgKind)
QualifiedTable
tn PrimaryKeyColumns ('Postgres pgKind)
pKeys
        AFColumn AnnColumnField ('Postgres pgKind) SQLExp
c -> AnnColumnField ('Postgres pgKind) SQLExp -> m SQLExp
toSQLCol AnnColumnField ('Postgres pgKind) SQLExp
c
        AFObjectRelation ObjectRelationSelectG ('Postgres pgKind) Void SQLExp
objSel -> m (ObjectRelationSource, InsOrdHashMap ColumnAlias SQLExp, SQLExp)
-> m SQLExp
forall (m :: * -> *) a.
MonadWriter SelectWriter m =>
m (ObjectRelationSource, InsOrdHashMap ColumnAlias SQLExp, a)
-> m a
withWriteObjectRelation (m (ObjectRelationSource, InsOrdHashMap ColumnAlias SQLExp, SQLExp)
 -> m SQLExp)
-> m (ObjectRelationSource, InsOrdHashMap ColumnAlias SQLExp,
      SQLExp)
-> m SQLExp
forall a b. (a -> b) -> a -> b
$ do
          let AnnRelationSelectG RelName
relName HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
relMapping Nullable
nullable AnnObjectSelectG ('Postgres pgKind) Void SQLExp
annObjSel = ObjectRelationSelectG ('Postgres pgKind) Void SQLExp
objSel
              AnnObjectSelectG [(FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)]
objAnnFields SelectFromG ('Postgres pgKind) SQLExp
target AnnBoolExp ('Postgres pgKind) SQLExp
targetFilter = AnnObjectSelectG ('Postgres pgKind) Void SQLExp
annObjSel
          (TableIdentifier
objRelSourcePrefix, FromItem
ident, BoolExp
filterExp) <- case SelectFromG ('Postgres pgKind) SQLExp
target of
            FromNativeQuery NativeQuery ('Postgres pgKind) SQLExp
nq -> do
              TableAlias
cteName <- NativeQuery ('Postgres pgKind) SQLExp -> m TableAlias
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m) =>
NativeQuery ('Postgres pgKind) SQLExp -> m TableAlias
fromNativeQuery NativeQuery ('Postgres pgKind) SQLExp
nq
              let nativeQueryIdentifier :: TableIdentifier
nativeQueryIdentifier = TableAlias -> TableIdentifier
S.tableAliasToIdentifier TableAlias
cteName

              (TableIdentifier, FromItem, BoolExp)
-> m (TableIdentifier, FromItem, BoolExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
                ( TableIdentifier -> RelName -> TableIdentifier
mkObjectRelationTableAlias
                    TableIdentifier
sourcePrefix
                    ( RelName
relName
                        { getRelTxt :: NonEmptyText
getRelTxt =
                            RelName -> NonEmptyText
getRelTxt RelName
relName
                              NonEmptyText -> NonEmptyText -> NonEmptyText
forall a. Semigroup a => a -> a -> a
<> Text -> NonEmptyText
TNE.mkNonEmptyTextUnsafe
                                ( Identifier -> Text
getIdenTxt
                                    (Identifier -> Text) -> Identifier -> Text
forall a b. (a -> b) -> a -> b
$ TableAlias -> Identifier
S.getTableAlias TableAlias
cteName
                                )
                        }
                    ),
                  TableIdentifier -> FromItem
S.FIIdentifier TableIdentifier
nativeQueryIdentifier,
                  Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
toSQLBoolExp (TableIdentifier -> Maybe TypeAnn -> Qual
S.QualifiedIdentifier TableIdentifier
nativeQueryIdentifier Maybe TypeAnn
forall a. Maybe a
Nothing) AnnBoolExpSQL ('Postgres pgKind)
AnnBoolExp ('Postgres pgKind) SQLExp
targetFilter
                )
            FromTable TableName ('Postgres pgKind)
tableFrom -> do
              (TableIdentifier, FromItem, BoolExp)
-> m (TableIdentifier, FromItem, BoolExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
                ( TableIdentifier -> RelName -> TableIdentifier
mkObjectRelationTableAlias TableIdentifier
sourcePrefix RelName
relName,
                  QualifiedTable -> Maybe TableAlias -> FromItem
S.FISimple TableName ('Postgres pgKind)
QualifiedTable
tableFrom Maybe TableAlias
forall a. Maybe a
Nothing,
                  Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExp
toSQLBoolExp (QualifiedTable -> Qual
S.QualTable TableName ('Postgres pgKind)
QualifiedTable
tableFrom) AnnBoolExpSQL ('Postgres pgKind)
AnnBoolExp ('Postgres pgKind) SQLExp
targetFilter
                )
            SelectFromG ('Postgres pgKind) SQLExp
other -> [Char] -> m (TableIdentifier, FromItem, BoolExp)
forall a. HasCallStack => [Char] -> a
error ([Char] -> m (TableIdentifier, FromItem, BoolExp))
-> [Char] -> m (TableIdentifier, FromItem, BoolExp)
forall a b. (a -> b) -> a -> b
$ [Char]
"processAnnFields: " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> SelectFromG ('Postgres pgKind) SQLExp -> [Char]
forall a. Show a => a -> [Char]
show SelectFromG ('Postgres pgKind) SQLExp
other
          let sourcePrefixes :: SourcePrefixes
sourcePrefixes = TableIdentifier -> SourcePrefixes
mkSourcePrefixes TableIdentifier
objRelSourcePrefix
              selectSource :: ObjectSelectSource
selectSource = Identifier -> FromItem -> BoolExp -> ObjectSelectSource
ObjectSelectSource (SourcePrefixes -> Identifier
_pfThis SourcePrefixes
sourcePrefixes) FromItem
ident BoolExp
filterExp
              objRelSource :: ObjectRelationSource
objRelSource = RelName
-> HashMap PGCol PGCol
-> ObjectSelectSource
-> Nullable
-> ObjectRelationSource
ObjectRelationSource RelName
relName HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
HashMap PGCol PGCol
relMapping ObjectSelectSource
selectSource Nullable
nullable
          (ColumnAlias, SQLExp)
annFieldsExtr <- TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
processAnnFields (Identifier -> TableIdentifier
identifierToTableIdentifier (Identifier -> TableIdentifier) -> Identifier -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ SourcePrefixes -> Identifier
_pfThis SourcePrefixes
sourcePrefixes) FieldName
fieldName AnnFields ('Postgres pgKind)
[(FieldName, AnnFieldG ('Postgres pgKind) Void SQLExp)]
objAnnFields Maybe NamingCase
tCase
          (ObjectRelationSource, InsOrdHashMap ColumnAlias SQLExp, SQLExp)
-> m (ObjectRelationSource, InsOrdHashMap ColumnAlias SQLExp,
      SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( ObjectRelationSource
objRelSource,
              (ColumnAlias -> SQLExp -> InsOrdHashMap ColumnAlias SQLExp)
-> (ColumnAlias, SQLExp) -> InsOrdHashMap ColumnAlias SQLExp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ColumnAlias -> SQLExp -> InsOrdHashMap ColumnAlias SQLExp
forall k v. Hashable k => k -> v -> InsOrdHashMap k v
InsOrdHashMap.singleton (ColumnAlias, SQLExp)
annFieldsExtr,
              TableIdentifier -> FieldName -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp TableIdentifier
objRelSourcePrefix FieldName
fieldName
            )
        AFArrayRelation ArraySelectG ('Postgres pgKind) Void SQLExp
arrSel -> do
          let arrRelSourcePrefix :: TableIdentifier
arrRelSourcePrefix = TableIdentifier
-> FieldName -> SimilarArrayFields -> FieldName -> TableIdentifier
mkArrayRelationSourcePrefix TableIdentifier
sourcePrefix FieldName
fieldAlias SimilarArrayFields
forall k v. HashMap k v
HashMap.empty FieldName
fieldName
              arrRelAlias :: TableAlias
arrRelAlias = FieldName -> SimilarArrayFields -> FieldName -> TableAlias
mkArrayRelationAlias FieldName
fieldAlias SimilarArrayFields
forall k v. HashMap k v
HashMap.empty FieldName
fieldName
          SourcePrefixes
-> FieldName
-> TableAlias
-> ArraySelect ('Postgres pgKind)
-> Maybe NamingCase
-> m ()
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> TableAlias
-> ArraySelect ('Postgres pgKind)
-> Maybe NamingCase
-> m ()
processArrayRelation (TableIdentifier -> SourcePrefixes
mkSourcePrefixes TableIdentifier
arrRelSourcePrefix) FieldName
fieldName TableAlias
arrRelAlias ArraySelect ('Postgres pgKind)
ArraySelectG ('Postgres pgKind) Void SQLExp
arrSel Maybe NamingCase
tCase
          SQLExp -> m SQLExp
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> m SQLExp) -> SQLExp -> m SQLExp
forall a b. (a -> b) -> a -> b
$ TableIdentifier -> FieldName -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp TableIdentifier
arrRelSourcePrefix FieldName
fieldName
        AFComputedField XComputedField ('Postgres pgKind)
_ ComputedFieldName
_ (CFSScalar ComputedFieldScalarSelect ('Postgres pgKind) SQLExp
scalar) -> do
          ComputedFieldScalarSelect ('Postgres pgKind) SQLExp -> m SQLExp
fromScalarComputedField ComputedFieldScalarSelect ('Postgres pgKind) SQLExp
scalar
        AFComputedField XComputedField ('Postgres pgKind)
_ ComputedFieldName
_ (CFSTable JsonAggSelect
selectTy AnnSimpleSelectG ('Postgres pgKind) Void SQLExp
sel) -> m (ComputedFieldTableSetSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, SQLExp)
-> m SQLExp
forall (m :: * -> *) a.
MonadWriter SelectWriter m =>
m (ComputedFieldTableSetSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, a)
-> m a
withWriteComputedFieldTableSet (m (ComputedFieldTableSetSource, Extractor,
    InsOrdHashMap ColumnAlias SQLExp, SQLExp)
 -> m SQLExp)
-> m (ComputedFieldTableSetSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, SQLExp)
-> m SQLExp
forall a b. (a -> b) -> a -> b
$ do
          let computedFieldSourcePrefix :: TableIdentifier
computedFieldSourcePrefix =
                TableIdentifier -> FieldName -> TableIdentifier
mkComputedFieldTableIdentifier TableIdentifier
sourcePrefix FieldName
fieldName
          (SelectSource
selectSource, InsOrdHashMap ColumnAlias SQLExp
nodeExtractors) <-
            SourcePrefixes
-> FieldName
-> PermissionLimitSubQuery
-> AnnSimpleSelect ('Postgres pgKind)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> PermissionLimitSubQuery
-> AnnSimpleSelect ('Postgres pgKind)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
processAnnSimpleSelect
              (TableIdentifier -> SourcePrefixes
mkSourcePrefixes TableIdentifier
computedFieldSourcePrefix)
              FieldName
fieldName
              PermissionLimitSubQuery
PLSQNotRequired
              AnnSimpleSelect ('Postgres pgKind)
AnnSimpleSelectG ('Postgres pgKind) Void SQLExp
sel
          let computedFieldTableSetSource :: ComputedFieldTableSetSource
computedFieldTableSetSource = FieldName -> SelectSource -> ComputedFieldTableSetSource
ComputedFieldTableSetSource FieldName
fieldName SelectSource
selectSource
              extractor :: Extractor
extractor =
                JsonAggSelect
-> ColumnAlias
-> PermissionLimitSubQuery
-> Maybe OrderByExp
-> Extractor
asJsonAggExtr JsonAggSelect
selectTy (FieldName -> ColumnAlias
forall a. IsIdentifier a => a -> ColumnAlias
S.toColumnAlias FieldName
fieldName) PermissionLimitSubQuery
PLSQNotRequired
                  (Maybe OrderByExp -> Extractor) -> Maybe OrderByExp -> Extractor
forall a b. (a -> b) -> a -> b
$ SelectSource -> Maybe OrderByExp
orderByForJsonAgg SelectSource
selectSource
          (ComputedFieldTableSetSource, Extractor,
 InsOrdHashMap ColumnAlias SQLExp, SQLExp)
-> m (ComputedFieldTableSetSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            ( ComputedFieldTableSetSource
computedFieldTableSetSource,
              Extractor
extractor,
              InsOrdHashMap ColumnAlias SQLExp
nodeExtractors,
              TableIdentifier -> FieldName -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp TableIdentifier
computedFieldSourcePrefix FieldName
fieldName
            )

  (ColumnAlias, SQLExp) -> m (ColumnAlias, SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((ColumnAlias, SQLExp) -> m (ColumnAlias, SQLExp))
-> (ColumnAlias, SQLExp) -> m (ColumnAlias, SQLExp)
forall a b. (a -> b) -> a -> b
$ forall (pgKind :: PostgresKind).
PostgresAnnotatedFieldJSON pgKind =>
FieldName -> [(FieldName, SQLExp)] -> (ColumnAlias, SQLExp)
annRowToJson @pgKind FieldName
fieldAlias [(FieldName, SQLExp)]
fieldExps
  where
    mkSourcePrefixes :: TableIdentifier -> SourcePrefixes
mkSourcePrefixes TableIdentifier
newPrefix = Identifier -> Identifier -> SourcePrefixes
SourcePrefixes (TableIdentifier -> Identifier
tableIdentifierToIdentifier TableIdentifier
newPrefix) (TableIdentifier -> Identifier
tableIdentifierToIdentifier TableIdentifier
sourcePrefix)

    baseTableIdentifier :: TableIdentifier
baseTableIdentifier = TableIdentifier -> TableIdentifier
mkBaseTableIdentifier TableIdentifier
sourcePrefix

    toSQLCol :: AnnColumnField ('Postgres pgKind) S.SQLExp -> m S.SQLExp
    toSQLCol :: AnnColumnField ('Postgres pgKind) SQLExp -> m SQLExp
toSQLCol (AnnColumnField Column ('Postgres pgKind)
col ColumnType ('Postgres pgKind)
typ Bool
asText Maybe (ScalarSelectionArguments ('Postgres pgKind))
colOpM AnnRedactionExp ('Postgres pgKind) SQLExp
redactionExp) = do
      StringifyNumbers
strfyNum <- m StringifyNumbers
forall r (m :: * -> *). MonadReader r m => m r
ask
      let sqlExpression :: SQLExp
sqlExpression =
            Qual
-> AnnRedactionExp ('Postgres pgKind) SQLExp -> SQLExp -> SQLExp
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
Qual
-> AnnRedactionExp ('Postgres pgKind) SQLExp -> SQLExp -> SQLExp
withRedactionExp (TableIdentifier -> Maybe TypeAnn -> Qual
S.QualifiedIdentifier TableIdentifier
baseTableIdentifier Maybe TypeAnn
forall a. Maybe a
Nothing) AnnRedactionExp ('Postgres pgKind) SQLExp
redactionExp
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Maybe ColumnOp -> SQLExp -> SQLExp
withColumnOp Maybe (ScalarSelectionArguments ('Postgres pgKind))
Maybe ColumnOp
colOpM
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ TableIdentifier -> PGCol -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp TableIdentifier
baseTableIdentifier Column ('Postgres pgKind)
PGCol
col
      SQLExp -> m SQLExp
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> m SQLExp) -> SQLExp -> m SQLExp
forall a b. (a -> b) -> a -> b
$ StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
forall (pgKind :: PostgresKind).
StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
toJSONableExp StringifyNumbers
strfyNum ColumnType ('Postgres pgKind)
typ Bool
asText Maybe NamingCase
tCase SQLExp
sqlExpression

    fromScalarComputedField :: ComputedFieldScalarSelect ('Postgres pgKind) S.SQLExp -> m S.SQLExp
    fromScalarComputedField :: ComputedFieldScalarSelect ('Postgres pgKind) SQLExp -> m SQLExp
fromScalarComputedField computedFieldScalar :: ComputedFieldScalarSelect ('Postgres pgKind) SQLExp
computedFieldScalar@ComputedFieldScalarSelect {Maybe (ScalarSelectionArguments ('Postgres pgKind))
FunctionName ('Postgres pgKind)
ScalarType ('Postgres pgKind)
FunctionArgsExp ('Postgres pgKind) SQLExp
AnnRedactionExp ('Postgres pgKind) SQLExp
_cfssFunction :: FunctionName ('Postgres pgKind)
_cfssArguments :: FunctionArgsExp ('Postgres pgKind) SQLExp
_cfssType :: ScalarType ('Postgres pgKind)
_cfssScalarArguments :: Maybe (ScalarSelectionArguments ('Postgres pgKind))
_cfssRedactionExpression :: AnnRedactionExp ('Postgres pgKind) SQLExp
$sel:_cfssFunction:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> FunctionName b
$sel:_cfssArguments:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> FunctionArgsExp b v
$sel:_cfssType:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> ScalarType b
$sel:_cfssScalarArguments:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> Maybe (ScalarSelectionArguments b)
$sel:_cfssRedactionExpression:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> AnnRedactionExp b v
..} = do
      StringifyNumbers
strfyNum <- m StringifyNumbers
forall r (m :: * -> *). MonadReader r m => m r
ask
      SQLExp -> m SQLExp
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (SQLExp -> m SQLExp) -> SQLExp -> m SQLExp
forall a b. (a -> b) -> a -> b
$ StringifyNumbers
-> ColumnType ('Postgres Any)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
forall (pgKind :: PostgresKind).
StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
toJSONableExp StringifyNumbers
strfyNum (ScalarType ('Postgres Any) -> ColumnType ('Postgres Any)
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType ('Postgres pgKind)
ScalarType ('Postgres Any)
_cfssType) Bool
False Maybe NamingCase
forall a. Maybe a
Nothing
        (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ TableIdentifier
-> ComputedFieldScalarSelect ('Postgres pgKind) SQLExp -> SQLExp
forall (pgKind :: PostgresKind).
Backend ('Postgres pgKind) =>
TableIdentifier
-> ComputedFieldScalarSelect ('Postgres pgKind) SQLExp -> SQLExp
mkRawComputedFieldExpression TableIdentifier
sourcePrefix ComputedFieldScalarSelect ('Postgres pgKind) SQLExp
computedFieldScalar

    mkNodeId :: SourceName -> QualifiedTable -> PrimaryKeyColumns ('Postgres pgKind) -> S.SQLExp
    mkNodeId :: SourceName
-> QualifiedTable -> PrimaryKeyColumns ('Postgres pgKind) -> SQLExp
mkNodeId SourceName
_sourceName (QualifiedObject SchemaName
tableSchema TableName
tableName) PrimaryKeyColumns ('Postgres pgKind)
pkeyColumns =
      let columnInfoToSQLExp :: ColumnInfo ('Postgres pgKind) -> SQLExp
columnInfoToSQLExp ColumnInfo ('Postgres pgKind)
pgColumnInfo =
            StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
forall (pgKind :: PostgresKind).
StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
toJSONableExp StringifyNumbers
Options.Don'tStringifyNumbers (ColumnInfo ('Postgres pgKind) -> ColumnType ('Postgres pgKind)
forall (b :: BackendType). ColumnInfo b -> ColumnType b
ciType ColumnInfo ('Postgres pgKind)
pgColumnInfo) Bool
False Maybe NamingCase
forall a. Maybe a
Nothing
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ TableIdentifier -> PGCol -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp (TableIdentifier -> TableIdentifier
mkBaseTableIdentifier TableIdentifier
sourcePrefix)
              (PGCol -> SQLExp) -> PGCol -> SQLExp
forall a b. (a -> b) -> a -> b
$ ColumnInfo ('Postgres pgKind) -> Column ('Postgres pgKind)
forall (b :: BackendType). ColumnInfo b -> Column b
ciColumn ColumnInfo ('Postgres pgKind)
pgColumnInfo
       in -- See Note [Relay Node id].
          SQLExp -> SQLExp
encodeBase64
            (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ (SQLExp -> TypeAnn -> SQLExp) -> TypeAnn -> SQLExp -> SQLExp
forall a b c. (a -> b -> c) -> b -> a -> c
flip SQLExp -> TypeAnn -> SQLExp
S.SETyAnn TypeAnn
S.textTypeAnn
            (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ [SQLExp] -> SQLExp
S.applyJsonBuildArray
            ([SQLExp] -> SQLExp) -> [SQLExp] -> SQLExp
forall a b. (a -> b) -> a -> b
$ [ Int -> SQLExp
S.intToSQLExp (Int -> SQLExp) -> Int -> SQLExp
forall a b. (a -> b) -> a -> b
$ NodeIdVersion -> Int
nodeIdVersionInt NodeIdVersion
currentNodeIdVersion,
                Text -> SQLExp
S.SELit (SchemaName -> Text
getSchemaTxt SchemaName
tableSchema),
                Text -> SQLExp
S.SELit (TableName -> Text
forall a. ToTxt a => a -> Text
toTxt TableName
tableName)
              ]
            [SQLExp] -> [SQLExp] -> [SQLExp]
forall a. Semigroup a => a -> a -> a
<> (ColumnInfo ('Postgres pgKind) -> SQLExp)
-> [ColumnInfo ('Postgres pgKind)] -> [SQLExp]
forall a b. (a -> b) -> [a] -> [b]
map ColumnInfo ('Postgres pgKind) -> SQLExp
columnInfoToSQLExp (PrimaryKeyColumns ('Postgres pgKind)
-> [ColumnInfo ('Postgres pgKind)]
forall a. NESeq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList PrimaryKeyColumns ('Postgres pgKind)
pkeyColumns)

mkSimilarArrayFields ::
  forall pgKind v.
  (Backend ('Postgres pgKind), Eq v) =>
  AnnFieldsG ('Postgres pgKind) Void v ->
  Maybe (NE.NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v)) ->
  SimilarArrayFields
mkSimilarArrayFields :: forall (pgKind :: PostgresKind) v.
(Backend ('Postgres pgKind), Eq v) =>
AnnFieldsG ('Postgres pgKind) Void v
-> Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
-> SimilarArrayFields
mkSimilarArrayFields AnnFieldsG ('Postgres pgKind) Void v
annFields Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
maybeOrderBys =
  [(FieldName, [FieldName])] -> SimilarArrayFields
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    ([(FieldName, [FieldName])] -> SimilarArrayFields)
-> [(FieldName, [FieldName])] -> SimilarArrayFields
forall a b. (a -> b) -> a -> b
$ ((((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
  -> (FieldName, [FieldName]))
 -> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
 -> [(FieldName, [FieldName])])
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> (((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
    -> (FieldName, [FieldName]))
-> [(FieldName, [FieldName])]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
 -> (FieldName, [FieldName]))
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> [(FieldName, [FieldName])]
forall a b. (a -> b) -> [a] -> [b]
map [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
allTuples
    ((((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
  -> (FieldName, [FieldName]))
 -> [(FieldName, [FieldName])])
-> (((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
    -> (FieldName, [FieldName]))
-> [(FieldName, [FieldName])]
forall a b. (a -> b) -> a -> b
$ \((RelName, SelectArgsG ('Postgres pgKind) v)
relNameAndArgs, FieldName
fieldName) -> (FieldName
fieldName, (RelName, SelectArgsG ('Postgres pgKind) v) -> [FieldName]
getSimilarFields (RelName, SelectArgsG ('Postgres pgKind) v)
relNameAndArgs)
  where
    getSimilarFields :: (RelName, SelectArgsG ('Postgres pgKind) v) -> [FieldName]
getSimilarFields (RelName, SelectArgsG ('Postgres pgKind) v)
relNameAndArgs = (((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
 -> FieldName)
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> [FieldName]
forall a b. (a -> b) -> [a] -> [b]
map ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
-> FieldName
forall a b. (a, b) -> b
snd ([((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
 -> [FieldName])
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> [FieldName]
forall a b. (a -> b) -> a -> b
$ (((RelName, SelectArgsG ('Postgres pgKind) v), FieldName) -> Bool)
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a. (a -> Bool) -> [a] -> [a]
filter (((RelName, SelectArgsG ('Postgres pgKind) v)
-> (RelName, SelectArgsG ('Postgres pgKind) v) -> Bool
forall a. Eq a => a -> a -> Bool
== (RelName, SelectArgsG ('Postgres pgKind) v)
relNameAndArgs) ((RelName, SelectArgsG ('Postgres pgKind) v) -> Bool)
-> (((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
    -> (RelName, SelectArgsG ('Postgres pgKind) v))
-> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
-> (RelName, SelectArgsG ('Postgres pgKind) v)
forall a b. (a, b) -> a
fst) [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
allTuples
    allTuples :: [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
allTuples = [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
arrayRelationTuples [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a. Semigroup a => a -> a -> a
<> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
aggOrderByRelationTuples
    arrayRelationTuples :: [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
arrayRelationTuples =
      let arrayFields :: [(FieldName, ArraySelectG ('Postgres pgKind) Void v)]
arrayFields = ((FieldName, AnnFieldG ('Postgres pgKind) Void v)
 -> Maybe (FieldName, ArraySelectG ('Postgres pgKind) Void v))
-> AnnFieldsG ('Postgres pgKind) Void v
-> [(FieldName, ArraySelectG ('Postgres pgKind) Void v)]
forall a b. (a -> Maybe b) -> [a] -> [b]
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (FieldName, AnnFieldG ('Postgres pgKind) Void v)
-> Maybe (FieldName, ArraySelectG ('Postgres pgKind) Void v)
forall a r.
(a, AnnFieldG ('Postgres pgKind) r v)
-> Maybe (a, ArraySelectG ('Postgres pgKind) r v)
getAnnArr AnnFieldsG ('Postgres pgKind) Void v
annFields
       in (((FieldName, ArraySelectG ('Postgres pgKind) Void v)
  -> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName))
 -> [(FieldName, ArraySelectG ('Postgres pgKind) Void v)]
 -> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)])
-> [(FieldName, ArraySelectG ('Postgres pgKind) Void v)]
-> ((FieldName, ArraySelectG ('Postgres pgKind) Void v)
    -> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName))
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((FieldName, ArraySelectG ('Postgres pgKind) Void v)
 -> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName))
-> [(FieldName, ArraySelectG ('Postgres pgKind) Void v)]
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a b. (a -> b) -> [a] -> [b]
map [(FieldName, ArraySelectG ('Postgres pgKind) Void v)]
arrayFields
            (((FieldName, ArraySelectG ('Postgres pgKind) Void v)
  -> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName))
 -> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)])
-> ((FieldName, ArraySelectG ('Postgres pgKind) Void v)
    -> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName))
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a b. (a -> b) -> a -> b
$ \(FieldName
f, ArraySelectG ('Postgres pgKind) Void v
relSel) -> (ArraySelectG ('Postgres pgKind) Void v
-> (RelName, SelectArgsG ('Postgres pgKind) v)
forall r.
ArraySelectG ('Postgres pgKind) r v
-> (RelName, SelectArgsG ('Postgres pgKind) v)
getArrayRelNameAndSelectArgs ArraySelectG ('Postgres pgKind) Void v
relSel, FieldName
f)

    getAnnArr ::
      (a, AnnFieldG ('Postgres pgKind) r v) ->
      Maybe (a, ArraySelectG ('Postgres pgKind) r v)
    getAnnArr :: forall a r.
(a, AnnFieldG ('Postgres pgKind) r v)
-> Maybe (a, ArraySelectG ('Postgres pgKind) r v)
getAnnArr (a
f, AnnFieldG ('Postgres pgKind) r v
annFld) = case AnnFieldG ('Postgres pgKind) r v
annFld of
      AFArrayRelation (ASConnection ArrayConnectionSelect ('Postgres pgKind) r v
_) -> Maybe (a, ArraySelectG ('Postgres pgKind) r v)
forall a. Maybe a
Nothing
      AFArrayRelation ArraySelectG ('Postgres pgKind) r v
ar -> (a, ArraySelectG ('Postgres pgKind) r v)
-> Maybe (a, ArraySelectG ('Postgres pgKind) r v)
forall a. a -> Maybe a
Just (a
f, ArraySelectG ('Postgres pgKind) r v
ar)
      AnnFieldG ('Postgres pgKind) r v
_ -> Maybe (a, ArraySelectG ('Postgres pgKind) r v)
forall a. Maybe a
Nothing

    aggOrderByRelationTuples :: [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
aggOrderByRelationTuples =
      let mkItem :: (a, b) -> ((a, SelectArgsG backend v), b)
mkItem (a
relName, b
fieldName) =
            ( (a
relName, SelectArgsG backend v
forall (backend :: BackendType) v. SelectArgsG backend v
noSelectArgs),
              b
fieldName
            )
       in ((RelName, FieldName)
 -> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName))
-> [(RelName, FieldName)]
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a b. (a -> b) -> [a] -> [b]
map (RelName, FieldName)
-> ((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)
forall {a} {b} {backend :: BackendType} {v}.
(a, b) -> ((a, SelectArgsG backend v), b)
mkItem
            ([(RelName, FieldName)]
 -> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)])
-> [(RelName, FieldName)]
-> [((RelName, SelectArgsG ('Postgres pgKind) v), FieldName)]
forall a b. (a -> b) -> a -> b
$ [(RelName, FieldName)]
-> (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v)
    -> [(RelName, FieldName)])
-> Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
-> [(RelName, FieldName)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
              []
              ((AnnotatedOrderByItemG ('Postgres pgKind) v
 -> Maybe (RelName, FieldName))
-> [AnnotatedOrderByItemG ('Postgres pgKind) v]
-> [(RelName, FieldName)]
forall a b. (a -> Maybe b) -> [a] -> [b]
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (AnnotatedOrderByElement ('Postgres pgKind) v
-> Maybe (RelName, FieldName)
forall {b :: BackendType} {v}.
AnnotatedOrderByElement b v -> Maybe (RelName, FieldName)
fetchAggOrderByRels (AnnotatedOrderByElement ('Postgres pgKind) v
 -> Maybe (RelName, FieldName))
-> (AnnotatedOrderByItemG ('Postgres pgKind) v
    -> AnnotatedOrderByElement ('Postgres pgKind) v)
-> AnnotatedOrderByItemG ('Postgres pgKind) v
-> Maybe (RelName, FieldName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnotatedOrderByItemG ('Postgres pgKind) v
-> AnnotatedOrderByElement ('Postgres pgKind) v
forall (b :: BackendType) a. OrderByItemG b a -> a
obiColumn) ([AnnotatedOrderByItemG ('Postgres pgKind) v]
 -> [(RelName, FieldName)])
-> (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v)
    -> [AnnotatedOrderByItemG ('Postgres pgKind) v])
-> NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v)
-> [(RelName, FieldName)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v)
-> [AnnotatedOrderByItemG ('Postgres pgKind) v]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList)
              Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
maybeOrderBys

    fetchAggOrderByRels :: AnnotatedOrderByElement b v -> Maybe (RelName, FieldName)
fetchAggOrderByRels (AOCArrayAggregation RelInfo b
ri AnnBoolExp b v
_ AnnotatedAggregateOrderBy b v
_) =
      (RelName, FieldName) -> Maybe (RelName, FieldName)
forall a. a -> Maybe a
Just (RelInfo b -> RelName
forall (b :: BackendType). RelInfo b -> RelName
riName RelInfo b
ri, RelName -> FieldName
forall a. ToTxt a => a -> FieldName
mkOrderByFieldName (RelName -> FieldName) -> RelName -> FieldName
forall a b. (a -> b) -> a -> b
$ RelInfo b -> RelName
forall (b :: BackendType). RelInfo b -> RelName
riName RelInfo b
ri)
    fetchAggOrderByRels AnnotatedOrderByElement b v
_ = Maybe (RelName, FieldName)
forall a. Maybe a
Nothing

    getArrayRelNameAndSelectArgs ::
      ArraySelectG ('Postgres pgKind) r v ->
      (RelName, SelectArgsG ('Postgres pgKind) v)
    getArrayRelNameAndSelectArgs :: forall r.
ArraySelectG ('Postgres pgKind) r v
-> (RelName, SelectArgsG ('Postgres pgKind) v)
getArrayRelNameAndSelectArgs = \case
      ASSimple ArrayRelationSelectG ('Postgres pgKind) r v
r -> (ArrayRelationSelectG ('Postgres pgKind) r v -> RelName
forall (b :: BackendType) a. AnnRelationSelectG b a -> RelName
_aarRelationshipName ArrayRelationSelectG ('Postgres pgKind) r v
r, AnnSelectG ('Postgres pgKind) (AnnFieldG ('Postgres pgKind) r) v
-> SelectArgsG ('Postgres pgKind) v
forall (b :: BackendType) (f :: * -> *) v.
AnnSelectG b f v -> SelectArgsG b v
_asnArgs (AnnSelectG ('Postgres pgKind) (AnnFieldG ('Postgres pgKind) r) v
 -> SelectArgsG ('Postgres pgKind) v)
-> AnnSelectG ('Postgres pgKind) (AnnFieldG ('Postgres pgKind) r) v
-> SelectArgsG ('Postgres pgKind) v
forall a b. (a -> b) -> a -> b
$ ArrayRelationSelectG ('Postgres pgKind) r v
-> AnnSelectG ('Postgres pgKind) (AnnFieldG ('Postgres pgKind) r) v
forall (b :: BackendType) a. AnnRelationSelectG b a -> a
_aarAnnSelect ArrayRelationSelectG ('Postgres pgKind) r v
r)
      ASAggregate ArrayAggregateSelectG ('Postgres pgKind) r v
r -> (ArrayAggregateSelectG ('Postgres pgKind) r v -> RelName
forall (b :: BackendType) a. AnnRelationSelectG b a -> RelName
_aarRelationshipName ArrayAggregateSelectG ('Postgres pgKind) r v
r, AnnSelectG
  ('Postgres pgKind) (TableAggregateFieldG ('Postgres pgKind) r) v
-> SelectArgsG ('Postgres pgKind) v
forall (b :: BackendType) (f :: * -> *) v.
AnnSelectG b f v -> SelectArgsG b v
_asnArgs (AnnSelectG
   ('Postgres pgKind) (TableAggregateFieldG ('Postgres pgKind) r) v
 -> SelectArgsG ('Postgres pgKind) v)
-> AnnSelectG
     ('Postgres pgKind) (TableAggregateFieldG ('Postgres pgKind) r) v
-> SelectArgsG ('Postgres pgKind) v
forall a b. (a -> b) -> a -> b
$ ArrayAggregateSelectG ('Postgres pgKind) r v
-> AnnSelectG
     ('Postgres pgKind) (TableAggregateFieldG ('Postgres pgKind) r) v
forall (b :: BackendType) a. AnnRelationSelectG b a -> a
_aarAnnSelect ArrayAggregateSelectG ('Postgres pgKind) r v
r)
      ASConnection ArrayConnectionSelect ('Postgres pgKind) r v
r -> (ArrayConnectionSelect ('Postgres pgKind) r v -> RelName
forall (b :: BackendType) a. AnnRelationSelectG b a -> RelName
_aarRelationshipName ArrayConnectionSelect ('Postgres pgKind) r v
r, AnnSelectG
  ('Postgres pgKind) (ConnectionField ('Postgres pgKind) r) v
-> SelectArgsG ('Postgres pgKind) v
forall (b :: BackendType) (f :: * -> *) v.
AnnSelectG b f v -> SelectArgsG b v
_asnArgs (AnnSelectG
   ('Postgres pgKind) (ConnectionField ('Postgres pgKind) r) v
 -> SelectArgsG ('Postgres pgKind) v)
-> AnnSelectG
     ('Postgres pgKind) (ConnectionField ('Postgres pgKind) r) v
-> SelectArgsG ('Postgres pgKind) v
forall a b. (a -> b) -> a -> b
$ ConnectionSelect ('Postgres pgKind) r v
-> AnnSelectG
     ('Postgres pgKind) (ConnectionField ('Postgres pgKind) r) v
forall (b :: BackendType) r v.
ConnectionSelect b r v -> AnnSelectG b (ConnectionField b r) v
_csSelect (ConnectionSelect ('Postgres pgKind) r v
 -> AnnSelectG
      ('Postgres pgKind) (ConnectionField ('Postgres pgKind) r) v)
-> ConnectionSelect ('Postgres pgKind) r v
-> AnnSelectG
     ('Postgres pgKind) (ConnectionField ('Postgres pgKind) r) v
forall a b. (a -> b) -> a -> b
$ ArrayConnectionSelect ('Postgres pgKind) r v
-> ConnectionSelect ('Postgres pgKind) r v
forall (b :: BackendType) a. AnnRelationSelectG b a -> a
_aarAnnSelect ArrayConnectionSelect ('Postgres pgKind) r v
r)

processArrayRelation ::
  forall pgKind m.
  ( MonadReader Options.StringifyNumbers m,
    MonadWriter SelectWriter m,
    MonadState NativeQueryFreshIdStore m,
    Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  SourcePrefixes ->
  FieldName ->
  S.TableAlias ->
  ArraySelect ('Postgres pgKind) ->
  Maybe NamingCase ->
  m ()
processArrayRelation :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> TableAlias
-> ArraySelect ('Postgres pgKind)
-> Maybe NamingCase
-> m ()
processArrayRelation SourcePrefixes
sourcePrefixes FieldName
fieldAlias TableAlias
relAlias ArraySelect ('Postgres pgKind)
arrSel Maybe NamingCase
_tCase =
  case ArraySelect ('Postgres pgKind)
arrSel of
    ASSimple ArrayRelationSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
annArrRel -> m (ArrayRelationSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, ())
-> m ()
forall (m :: * -> *) a.
MonadWriter SelectWriter m =>
m (ArrayRelationSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, a)
-> m a
withWriteArrayRelation (m (ArrayRelationSource, Extractor,
    InsOrdHashMap ColumnAlias SQLExp, ())
 -> m ())
-> m (ArrayRelationSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, ())
-> m ()
forall a b. (a -> b) -> a -> b
$ do
      let AnnRelationSelectG RelName
_ HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
colMapping Nullable
_ AnnSimpleSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
sel = ArrayRelationSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
annArrRel
          permLimitSubQuery :: PermissionLimitSubQuery
permLimitSubQuery =
            PermissionLimitSubQuery
-> (Int -> PermissionLimitSubQuery)
-> Maybe Int
-> PermissionLimitSubQuery
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PermissionLimitSubQuery
PLSQNotRequired Int -> PermissionLimitSubQuery
PLSQRequired (Maybe Int -> PermissionLimitSubQuery)
-> Maybe Int -> PermissionLimitSubQuery
forall a b. (a -> b) -> a -> b
$ TablePermG ('Postgres pgKind) SQLExp -> Maybe Int
forall (b :: BackendType) v. TablePermG b v -> Maybe Int
_tpLimit (TablePermG ('Postgres pgKind) SQLExp -> Maybe Int)
-> TablePermG ('Postgres pgKind) SQLExp -> Maybe Int
forall a b. (a -> b) -> a -> b
$ AnnSelectG
  ('Postgres pgKind) (AnnFieldG ('Postgres pgKind) Void) SQLExp
-> TablePermG ('Postgres pgKind) SQLExp
forall (b :: BackendType) (f :: * -> *) v.
AnnSelectG b f v -> TablePermG b v
_asnPerm AnnSimpleSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
AnnSelectG
  ('Postgres pgKind) (AnnFieldG ('Postgres pgKind) Void) SQLExp
sel
      (SelectSource
source, InsOrdHashMap ColumnAlias SQLExp
nodeExtractors) <-
        SourcePrefixes
-> FieldName
-> PermissionLimitSubQuery
-> AnnSimpleSelectG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> PermissionLimitSubQuery
-> AnnSimpleSelect ('Postgres pgKind)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
processAnnSimpleSelect SourcePrefixes
sourcePrefixes FieldName
fieldAlias PermissionLimitSubQuery
permLimitSubQuery AnnSimpleSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
sel
      let topExtr :: Extractor
topExtr =
            JsonAggSelect
-> ColumnAlias
-> PermissionLimitSubQuery
-> Maybe OrderByExp
-> Extractor
asJsonAggExtr
              JsonAggSelect
JASMultipleRows
              (FieldName -> ColumnAlias
forall a. IsIdentifier a => a -> ColumnAlias
S.toColumnAlias FieldName
fieldAlias)
              PermissionLimitSubQuery
permLimitSubQuery
              (Maybe OrderByExp -> Extractor) -> Maybe OrderByExp -> Extractor
forall a b. (a -> b) -> a -> b
$ SelectSource -> Maybe OrderByExp
orderByForJsonAgg SelectSource
source
      (ArrayRelationSource, Extractor, InsOrdHashMap ColumnAlias SQLExp,
 ())
-> m (ArrayRelationSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        ( TableAlias
-> HashMap PGCol PGCol -> SelectSource -> ArrayRelationSource
ArrayRelationSource TableAlias
relAlias HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
HashMap PGCol PGCol
colMapping SelectSource
source,
          Extractor
topExtr,
          InsOrdHashMap ColumnAlias SQLExp
nodeExtractors,
          ()
        )
    ASAggregate ArrayAggregateSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
aggSel -> m (ArrayRelationSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, ())
-> m ()
forall (m :: * -> *) a.
MonadWriter SelectWriter m =>
m (ArrayRelationSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, a)
-> m a
withWriteArrayRelation (m (ArrayRelationSource, Extractor,
    InsOrdHashMap ColumnAlias SQLExp, ())
 -> m ())
-> m (ArrayRelationSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, ())
-> m ()
forall a b. (a -> b) -> a -> b
$ do
      let AnnRelationSelectG RelName
_ HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
colMapping Nullable
_ AnnAggregateSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
sel = ArrayAggregateSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
aggSel
      (SelectSource
source, InsOrdHashMap ColumnAlias SQLExp
nodeExtractors, Extractor
topExtr) <-
        SourcePrefixes
-> FieldName
-> AnnAggregateSelectG
     ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp, Extractor)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> AnnAggregateSelect ('Postgres pgKind)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp, Extractor)
processAnnAggregateSelect SourcePrefixes
sourcePrefixes FieldName
fieldAlias AnnAggregateSelectG
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
sel
      (ArrayRelationSource, Extractor, InsOrdHashMap ColumnAlias SQLExp,
 ())
-> m (ArrayRelationSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        ( TableAlias
-> HashMap PGCol PGCol -> SelectSource -> ArrayRelationSource
ArrayRelationSource TableAlias
relAlias HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
HashMap PGCol PGCol
colMapping SelectSource
source,
          Extractor
topExtr,
          InsOrdHashMap ColumnAlias SQLExp
nodeExtractors,
          ()
        )
    ASConnection ArrayConnectionSelect
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
connSel -> m (ArrayConnectionSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, ())
-> m ()
forall (m :: * -> *) a.
MonadWriter SelectWriter m =>
m (ArrayConnectionSource, Extractor,
   InsOrdHashMap ColumnAlias SQLExp, a)
-> m a
withWriteArrayConnection (m (ArrayConnectionSource, Extractor,
    InsOrdHashMap ColumnAlias SQLExp, ())
 -> m ())
-> m (ArrayConnectionSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, ())
-> m ()
forall a b. (a -> b) -> a -> b
$ do
      let AnnRelationSelectG RelName
_ HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
colMapping Nullable
_ ConnectionSelect
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
sel = ArrayConnectionSelect
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
connSel
      (ArrayConnectionSource
source, Extractor
topExtractor, InsOrdHashMap ColumnAlias SQLExp
nodeExtractors) <-
        SourcePrefixes
-> FieldName
-> TableAlias
-> HashMap PGCol PGCol
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> m (ArrayConnectionSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> TableAlias
-> HashMap PGCol PGCol
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> m (ArrayConnectionSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp)
processConnectionSelect SourcePrefixes
sourcePrefixes FieldName
fieldAlias TableAlias
relAlias HashMap (Column ('Postgres pgKind)) (Column ('Postgres pgKind))
HashMap PGCol PGCol
colMapping ConnectionSelect
  ('Postgres pgKind) Void (SQLExpression ('Postgres pgKind))
ConnectionSelect ('Postgres pgKind) Void SQLExp
sel
      (ArrayConnectionSource, Extractor,
 InsOrdHashMap ColumnAlias SQLExp, ())
-> m (ArrayConnectionSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp, ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        ( ArrayConnectionSource
source,
          Extractor
topExtractor,
          InsOrdHashMap ColumnAlias SQLExp
nodeExtractors,
          ()
        )

aggregateFieldToExp ::
  forall pgKind.
  TableIdentifier ->
  AggregateFields ('Postgres pgKind) S.SQLExp ->
  Options.StringifyNumbers ->
  S.SQLExp
aggregateFieldToExp :: forall (pgKind :: PostgresKind).
TableIdentifier
-> AggregateFields ('Postgres pgKind) SQLExp
-> StringifyNumbers
-> SQLExp
aggregateFieldToExp TableIdentifier
sourcePrefix AggregateFields ('Postgres pgKind) SQLExp
aggregateFields StringifyNumbers
strfyNum =
  [SQLExp] -> SQLExp
S.applyJsonBuildObj (((FieldName, AggregateField ('Postgres pgKind) SQLExp) -> [SQLExp])
-> AggregateFields ('Postgres pgKind) SQLExp -> [SQLExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (FieldName, AggregateField ('Postgres pgKind) SQLExp) -> [SQLExp]
aggregateFieldToExtractor AggregateFields ('Postgres pgKind) SQLExp
aggregateFields)
  where
    withAlias :: FieldName -> S.SQLExp -> [S.SQLExp]
    withAlias :: FieldName -> SQLExp -> [SQLExp]
withAlias (FieldName Text
fldName) SQLExp
sqlExp = [Text -> SQLExp
S.SELit Text
fldName, SQLExp
sqlExp]

    aggregateFieldToExtractor ::
      (FieldName, AggregateField ('Postgres pgKind) S.SQLExp) ->
      [S.SQLExp]
    aggregateFieldToExtractor :: (FieldName, AggregateField ('Postgres pgKind) SQLExp) -> [SQLExp]
aggregateFieldToExtractor (FieldName
aggregateFieldName, AggregateField ('Postgres pgKind) SQLExp
field) = FieldName -> SQLExp -> [SQLExp]
withAlias FieldName
aggregateFieldName (SQLExp -> [SQLExp]) -> SQLExp -> [SQLExp]
forall a b. (a -> b) -> a -> b
$ case AggregateField ('Postgres pgKind) SQLExp
field of
      AFCount CountType ('Postgres pgKind) SQLExp
countAggregate -> CountType QIdentifier -> SQLExp
S.SECount (CountType QIdentifier -> SQLExp)
-> CountType QIdentifier -> SQLExp
forall a b. (a -> b) -> a -> b
$ PGCol -> QIdentifier
columnToQIdentifier (PGCol -> QIdentifier)
-> ((PGCol, AnnRedactionExp ('Postgres pgKind) SQLExp) -> PGCol)
-> (PGCol, AnnRedactionExp ('Postgres pgKind) SQLExp)
-> QIdentifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PGCol, AnnRedactionExp ('Postgres pgKind) SQLExp) -> PGCol
forall a b. (a, b) -> a
fst ((PGCol, AnnRedactionExp ('Postgres pgKind) SQLExp) -> QIdentifier)
-> CountType (PGCol, AnnRedactionExp ('Postgres pgKind) SQLExp)
-> CountType QIdentifier
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CountAggregate pgKind SQLExp
-> CountType (PGCol, AnnRedactionExp ('Postgres pgKind) SQLExp)
forall (pgKind :: PostgresKind) v.
CountAggregate pgKind v
-> CountType (PGCol, AnnRedactionExp ('Postgres pgKind) v)
getCountType CountType ('Postgres pgKind) SQLExp
CountAggregate pgKind SQLExp
countAggregate
      AFOp AggregateOp ('Postgres pgKind) SQLExp
aggOp -> FieldName -> AggregateOp ('Postgres pgKind) SQLExp -> SQLExp
aggregateOpToObj FieldName
aggregateFieldName AggregateOp ('Postgres pgKind) SQLExp
aggOp
      AFExp Text
e -> Text -> SQLExp
S.SELit Text
e

    aggregateOpToObj :: FieldName -> AggregateOp ('Postgres pgKind) S.SQLExp -> S.SQLExp
    aggregateOpToObj :: FieldName -> AggregateOp ('Postgres pgKind) SQLExp -> SQLExp
aggregateOpToObj FieldName
aggregateFieldName (AggregateOp Text
opText SelectionFields ('Postgres pgKind) SQLExp
selectionFields) =
      [SQLExp] -> SQLExp
S.applyJsonBuildObj ([SQLExp] -> SQLExp) -> [SQLExp] -> SQLExp
forall a b. (a -> b) -> a -> b
$ ((FieldName, SelectionField ('Postgres pgKind) SQLExp) -> [SQLExp])
-> SelectionFields ('Postgres pgKind) SQLExp -> [SQLExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (FieldName
-> Text
-> (FieldName, SelectionField ('Postgres pgKind) SQLExp)
-> [SQLExp]
selectionFieldToExtractor FieldName
aggregateFieldName Text
opText) SelectionFields ('Postgres pgKind) SQLExp
selectionFields

    selectionFieldToExtractor ::
      FieldName ->
      Text ->
      (FieldName, SelectionField ('Postgres pgKind) S.SQLExp) ->
      [S.SQLExp]
    selectionFieldToExtractor :: FieldName
-> Text
-> (FieldName, SelectionField ('Postgres pgKind) SQLExp)
-> [SQLExp]
selectionFieldToExtractor FieldName
aggregateFieldName Text
opText (FieldName
fieldName, SelectionField ('Postgres pgKind) SQLExp
selectionField) =
      FieldName -> SQLExp -> [SQLExp]
withAlias FieldName
fieldName (SQLExp -> [SQLExp]) -> SQLExp -> [SQLExp]
forall a b. (a -> b) -> a -> b
$ case SelectionField ('Postgres pgKind) SQLExp
selectionField of
        SFCol Column ('Postgres pgKind)
col ColumnType ('Postgres pgKind)
ty AnnRedactionExp ('Postgres pgKind) SQLExp
_redactionExp ->
          StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
forall (pgKind :: PostgresKind).
StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
toJSONableExp StringifyNumbers
strfyNum ColumnType ('Postgres pgKind)
ty Bool
False Maybe NamingCase
forall a. Maybe a
Nothing
            (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> [SQLExp] -> Maybe OrderByExp -> SQLExp
S.SEFnApp Text
opText [QIdentifier -> SQLExp
S.SEQIdentifier (QIdentifier -> SQLExp) -> QIdentifier -> SQLExp
forall a b. (a -> b) -> a -> b
$ PGCol -> QIdentifier
columnToQIdentifier Column ('Postgres pgKind)
PGCol
col] Maybe OrderByExp
forall a. Maybe a
Nothing
        SFComputedField ComputedFieldName
_cfName ComputedFieldScalarSelect {Maybe (ScalarSelectionArguments ('Postgres pgKind))
FunctionName ('Postgres pgKind)
ScalarType ('Postgres pgKind)
FunctionArgsExp ('Postgres pgKind) SQLExp
AnnRedactionExp ('Postgres pgKind) SQLExp
$sel:_cfssFunction:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> FunctionName b
$sel:_cfssArguments:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> FunctionArgsExp b v
$sel:_cfssType:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> ScalarType b
$sel:_cfssScalarArguments:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> Maybe (ScalarSelectionArguments b)
$sel:_cfssRedactionExpression:ComputedFieldScalarSelect :: forall (b :: BackendType) v.
ComputedFieldScalarSelect b v -> AnnRedactionExp b v
_cfssFunction :: FunctionName ('Postgres pgKind)
_cfssArguments :: FunctionArgsExp ('Postgres pgKind) SQLExp
_cfssType :: ScalarType ('Postgres pgKind)
_cfssScalarArguments :: Maybe (ScalarSelectionArguments ('Postgres pgKind))
_cfssRedactionExpression :: AnnRedactionExp ('Postgres pgKind) SQLExp
..} ->
          StringifyNumbers
-> ColumnType ('Postgres Any)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
forall (pgKind :: PostgresKind).
StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
toJSONableExp StringifyNumbers
strfyNum (ScalarType ('Postgres Any) -> ColumnType ('Postgres Any)
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType ('Postgres pgKind)
ScalarType ('Postgres Any)
_cfssType) Bool
False Maybe NamingCase
forall a. Maybe a
Nothing
            (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> [SQLExp] -> Maybe OrderByExp -> SQLExp
S.SEFnApp Text
opText [QIdentifier -> SQLExp
S.SEQIdentifier (QIdentifier -> SQLExp) -> QIdentifier -> SQLExp
forall a b. (a -> b) -> a -> b
$ FieldName -> FieldName -> QIdentifier
computedFieldToQIdentifier FieldName
aggregateFieldName FieldName
fieldName] Maybe OrderByExp
forall a. Maybe a
Nothing
        SFExp Text
e ->
          Text -> SQLExp
S.SELit Text
e

    columnToQIdentifier :: PGCol -> S.QIdentifier
    columnToQIdentifier :: PGCol -> QIdentifier
columnToQIdentifier = TableIdentifier -> ColumnAlias -> QIdentifier
forall b. IsIdentifier b => TableIdentifier -> b -> QIdentifier
S.mkQIdentifier TableIdentifier
sourcePrefix (ColumnAlias -> QIdentifier)
-> (PGCol -> ColumnAlias) -> PGCol -> QIdentifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableIdentifier -> PGCol -> ColumnAlias
contextualizeBaseTableColumn TableIdentifier
sourcePrefix

    computedFieldToQIdentifier :: FieldName -> FieldName -> S.QIdentifier
    computedFieldToQIdentifier :: FieldName -> FieldName -> QIdentifier
computedFieldToQIdentifier FieldName
aggregateFieldName FieldName
fieldName = TableIdentifier -> ColumnAlias -> QIdentifier
forall b. IsIdentifier b => TableIdentifier -> b -> QIdentifier
S.mkQIdentifier TableIdentifier
sourcePrefix (ColumnAlias -> QIdentifier) -> ColumnAlias -> QIdentifier
forall a b. (a -> b) -> a -> b
$ TableIdentifier -> FieldName -> FieldName -> ColumnAlias
contextualizeAggregateInput TableIdentifier
sourcePrefix FieldName
aggregateFieldName FieldName
fieldName

processAnnSimpleSelect ::
  forall pgKind m.
  ( MonadReader Options.StringifyNumbers m,
    MonadState NativeQueryFreshIdStore m,
    MonadWriter SelectWriter m,
    Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  SourcePrefixes ->
  FieldName ->
  PermissionLimitSubQuery ->
  AnnSimpleSelect ('Postgres pgKind) ->
  m
    ( SelectSource,
      InsOrdHashMap S.ColumnAlias S.SQLExp
    )
processAnnSimpleSelect :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> PermissionLimitSubQuery
-> AnnSimpleSelect ('Postgres pgKind)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
processAnnSimpleSelect SourcePrefixes
sourcePrefixes FieldName
fieldAlias PermissionLimitSubQuery
permLimitSubQuery AnnSimpleSelect ('Postgres pgKind)
annSimpleSel = do
  (SelectSource
selectSource, [(ColumnAlias, SQLExp)]
orderByAndDistinctExtrs, Maybe SQLExp
_) <-
    SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind)) =>
SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
processSelectParams
      SourcePrefixes
sourcePrefixes
      FieldName
fieldAlias
      SimilarArrayFields
similarArrayFields
      SelectFrom ('Postgres pgKind)
tableFrom
      PermissionLimitSubQuery
permLimitSubQuery
      TablePerm ('Postgres pgKind)
tablePermissions
      SelectArgs ('Postgres pgKind)
tableArgs
  (ColumnAlias, SQLExp)
annFieldsExtr <-
    TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
processAnnFields
      (Identifier -> TableIdentifier
identifierToTableIdentifier (Identifier -> TableIdentifier) -> Identifier -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ SourcePrefixes -> Identifier
_pfThis SourcePrefixes
sourcePrefixes)
      FieldName
fieldAlias
      AnnFields ('Postgres pgKind)
annSelFields
      Maybe NamingCase
tCase
  let allExtractors :: InsOrdHashMap ColumnAlias SQLExp
allExtractors = [(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList ([(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp)
-> [(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp
forall a b. (a -> b) -> a -> b
$ (ColumnAlias, SQLExp)
annFieldsExtr (ColumnAlias, SQLExp)
-> [(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]
forall a. a -> [a] -> [a]
: [(ColumnAlias, SQLExp)]
orderByAndDistinctExtrs
  (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
-> m (SelectSource, InsOrdHashMap ColumnAlias SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SelectSource
selectSource, InsOrdHashMap ColumnAlias SQLExp
allExtractors)
  where
    AnnSelectG AnnFields ('Postgres pgKind)
annSelFields SelectFrom ('Postgres pgKind)
tableFrom TablePerm ('Postgres pgKind)
tablePermissions SelectArgs ('Postgres pgKind)
tableArgs StringifyNumbers
_ Maybe NamingCase
tCase = AnnSimpleSelect ('Postgres pgKind)
annSimpleSel
    similarArrayFields :: SimilarArrayFields
similarArrayFields =
      AnnFieldsG ('Postgres pgKind) Void SQLExp
-> Maybe
     (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
-> SimilarArrayFields
forall (pgKind :: PostgresKind) v.
(Backend ('Postgres pgKind), Eq v) =>
AnnFieldsG ('Postgres pgKind) Void v
-> Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
-> SimilarArrayFields
mkSimilarArrayFields AnnFields ('Postgres pgKind)
AnnFieldsG ('Postgres pgKind) Void SQLExp
annSelFields (Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
 -> SimilarArrayFields)
-> Maybe
     (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
-> SimilarArrayFields
forall a b. (a -> b) -> a -> b
$ SelectArgsG ('Postgres pgKind) SQLExp
-> Maybe
     (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) SQLExp))
forall (b :: BackendType) v.
SelectArgsG b v -> Maybe (NonEmpty (AnnotatedOrderByItemG b v))
_saOrderBy SelectArgs ('Postgres pgKind)
SelectArgsG ('Postgres pgKind) SQLExp
tableArgs

processConnectionSelect ::
  forall pgKind m.
  ( MonadReader Options.StringifyNumbers m,
    MonadWriter SelectWriter m,
    MonadState NativeQueryFreshIdStore m,
    Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  SourcePrefixes ->
  FieldName ->
  S.TableAlias ->
  HashMap.HashMap PGCol PGCol ->
  ConnectionSelect ('Postgres pgKind) Void S.SQLExp ->
  m
    ( ArrayConnectionSource,
      S.Extractor,
      InsOrdHashMap S.ColumnAlias S.SQLExp
    )
processConnectionSelect :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> TableAlias
-> HashMap PGCol PGCol
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> m (ArrayConnectionSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp)
processConnectionSelect SourcePrefixes
sourcePrefixes FieldName
fieldAlias TableAlias
relAlias HashMap PGCol PGCol
colMapping ConnectionSelect ('Postgres pgKind) Void SQLExp
connectionSelect = do
  (SelectSource
selectSource, [(ColumnAlias, SQLExp)]
orderByAndDistinctExtrs, Maybe SQLExp
maybeOrderByCursor) <-
    SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m,
 MonadState NativeQueryFreshIdStore m, MonadWriter SelectWriter m,
 Backend ('Postgres pgKind)) =>
SourcePrefixes
-> FieldName
-> SimilarArrayFields
-> SelectFrom ('Postgres pgKind)
-> PermissionLimitSubQuery
-> TablePerm ('Postgres pgKind)
-> SelectArgs ('Postgres pgKind)
-> m (SelectSource, [(ColumnAlias, SQLExp)], Maybe SQLExp)
processSelectParams
      SourcePrefixes
sourcePrefixes
      FieldName
fieldAlias
      SimilarArrayFields
similarArrayFields
      SelectFrom ('Postgres pgKind)
SelectFromG ('Postgres pgKind) SQLExp
selectFrom
      PermissionLimitSubQuery
permLimitSubQuery
      TablePerm ('Postgres pgKind)
TablePermG ('Postgres pgKind) SQLExp
tablePermissions
      SelectArgs ('Postgres pgKind)
SelectArgsG ('Postgres pgKind) SQLExp
tableArgs

  let mkCursorExtractor :: SQLExp -> (ColumnAlias, SQLExp)
mkCursorExtractor = (Identifier -> ColumnAlias
forall a. IsIdentifier a => a -> ColumnAlias
S.toColumnAlias Identifier
cursorIdentifier,) (SQLExp -> (ColumnAlias, SQLExp))
-> (SQLExp -> SQLExp) -> SQLExp -> (ColumnAlias, SQLExp)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SQLExp -> TypeAnn -> SQLExp
`S.SETyAnn` TypeAnn
S.textTypeAnn)
      cursorExtractors :: [(ColumnAlias, SQLExp)]
cursorExtractors = case Maybe SQLExp
maybeOrderByCursor of
        Just SQLExp
orderByCursor -> [SQLExp -> (ColumnAlias, SQLExp)
mkCursorExtractor SQLExp
orderByCursor]
        Maybe SQLExp
Nothing ->
          -- Extract primary key columns from base select along with cursor expression.
          -- Those columns are required to perform connection split via a WHERE clause.
          SQLExp -> (ColumnAlias, SQLExp)
mkCursorExtractor SQLExp
primaryKeyColumnsObjectExp (ColumnAlias, SQLExp)
-> [(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]
forall a. a -> [a] -> [a]
: [(ColumnAlias, SQLExp)]
primaryKeyColumnExtractors
  (SQLExp
topExtractorExp, [(ColumnAlias, SQLExp)]
exps) <- (StateT [(ColumnAlias, SQLExp)] m SQLExp
 -> [(ColumnAlias, SQLExp)] -> m (SQLExp, [(ColumnAlias, SQLExp)]))
-> [(ColumnAlias, SQLExp)]
-> StateT [(ColumnAlias, SQLExp)] m SQLExp
-> m (SQLExp, [(ColumnAlias, SQLExp)])
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT [(ColumnAlias, SQLExp)] m SQLExp
-> [(ColumnAlias, SQLExp)] -> m (SQLExp, [(ColumnAlias, SQLExp)])
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT [] (StateT [(ColumnAlias, SQLExp)] m SQLExp
 -> m (SQLExp, [(ColumnAlias, SQLExp)]))
-> StateT [(ColumnAlias, SQLExp)] m SQLExp
-> m (SQLExp, [(ColumnAlias, SQLExp)])
forall a b. (a -> b) -> a -> b
$ SelectSource -> StateT [(ColumnAlias, SQLExp)] m SQLExp
forall (n :: * -> *) (n' :: * -> *) (t :: (* -> *) -> * -> *).
(MonadState [(ColumnAlias, SQLExp)] n, n ~ t n', MonadTrans t,
 MonadState NativeQueryFreshIdStore n', MonadWriter SelectWriter n',
 MonadReader StringifyNumbers n') =>
SelectSource -> n SQLExp
processFields SelectSource
selectSource
  let topExtractor :: Extractor
topExtractor = SQLExp -> Maybe ColumnAlias -> Extractor
S.Extractor SQLExp
topExtractorExp (Maybe ColumnAlias -> Extractor) -> Maybe ColumnAlias -> Extractor
forall a b. (a -> b) -> a -> b
$ ColumnAlias -> Maybe ColumnAlias
forall a. a -> Maybe a
Just (ColumnAlias -> Maybe ColumnAlias)
-> ColumnAlias -> Maybe ColumnAlias
forall a b. (a -> b) -> a -> b
$ Identifier -> ColumnAlias
forall a. IsIdentifier a => a -> ColumnAlias
S.toColumnAlias Identifier
fieldIdentifier
      allExtractors :: InsOrdHashMap ColumnAlias SQLExp
allExtractors = [(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
InsOrdHashMap.fromList ([(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp)
-> [(ColumnAlias, SQLExp)] -> InsOrdHashMap ColumnAlias SQLExp
forall a b. (a -> b) -> a -> b
$ [(ColumnAlias, SQLExp)]
cursorExtractors [(ColumnAlias, SQLExp)]
-> [(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]
forall a. Semigroup a => a -> a -> a
<> [(ColumnAlias, SQLExp)]
exps [(ColumnAlias, SQLExp)]
-> [(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]
forall a. Semigroup a => a -> a -> a
<> [(ColumnAlias, SQLExp)]
orderByAndDistinctExtrs
      arrayConnectionSource :: ArrayConnectionSource
arrayConnectionSource =
        TableAlias
-> HashMap PGCol PGCol
-> Maybe BoolExp
-> Maybe ConnectionSlice
-> SelectSource
-> ArrayConnectionSource
ArrayConnectionSource
          TableAlias
relAlias
          HashMap PGCol PGCol
colMapping
          (NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp) -> BoolExp
mkSplitBoolExp (NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp) -> BoolExp)
-> Maybe (NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp))
-> Maybe BoolExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp))
maybeSplit)
          Maybe ConnectionSlice
maybeSlice
          SelectSource
selectSource
  (ArrayConnectionSource, Extractor,
 InsOrdHashMap ColumnAlias SQLExp)
-> m (ArrayConnectionSource, Extractor,
      InsOrdHashMap ColumnAlias SQLExp)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    ( ArrayConnectionSource
arrayConnectionSource,
      Extractor
topExtractor,
      InsOrdHashMap ColumnAlias SQLExp
allExtractors
    )
  where
    ConnectionSelect XRelay ('Postgres pgKind)
_ PrimaryKeyColumns ('Postgres pgKind)
primaryKeyColumns Maybe (NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp))
maybeSplit Maybe ConnectionSlice
maybeSlice AnnSelectG
  ('Postgres pgKind) (ConnectionField ('Postgres pgKind) Void) SQLExp
select = ConnectionSelect ('Postgres pgKind) Void SQLExp
connectionSelect
    AnnSelectG Fields (ConnectionField ('Postgres pgKind) Void SQLExp)
fields SelectFromG ('Postgres pgKind) SQLExp
selectFrom TablePermG ('Postgres pgKind) SQLExp
tablePermissions SelectArgsG ('Postgres pgKind) SQLExp
tableArgs StringifyNumbers
_ Maybe NamingCase
tCase = AnnSelectG
  ('Postgres pgKind) (ConnectionField ('Postgres pgKind) Void) SQLExp
select
    fieldIdentifier :: Identifier
fieldIdentifier = FieldName -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier FieldName
fieldAlias
    thisPrefix :: TableIdentifier
thisPrefix = Identifier -> TableIdentifier
identifierToTableIdentifier (Identifier -> TableIdentifier) -> Identifier -> TableIdentifier
forall a b. (a -> b) -> a -> b
$ SourcePrefixes -> Identifier
_pfThis SourcePrefixes
sourcePrefixes
    permLimitSubQuery :: PermissionLimitSubQuery
permLimitSubQuery = PermissionLimitSubQuery
PLSQNotRequired

    primaryKeyColumnsObjectExp :: SQLExp
primaryKeyColumnsObjectExp =
      [SQLExp] -> SQLExp
S.applyJsonBuildObj
        ([SQLExp] -> SQLExp) -> [SQLExp] -> SQLExp
forall a b. (a -> b) -> a -> b
$ ((ColumnInfo ('Postgres pgKind) -> [SQLExp])
 -> [ColumnInfo ('Postgres pgKind)] -> [SQLExp])
-> [ColumnInfo ('Postgres pgKind)]
-> (ColumnInfo ('Postgres pgKind) -> [SQLExp])
-> [SQLExp]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (ColumnInfo ('Postgres pgKind) -> [SQLExp])
-> [ColumnInfo ('Postgres pgKind)] -> [SQLExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (PrimaryKeyColumns ('Postgres pgKind)
-> [ColumnInfo ('Postgres pgKind)]
forall a. NESeq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList PrimaryKeyColumns ('Postgres pgKind)
primaryKeyColumns)
        ((ColumnInfo ('Postgres pgKind) -> [SQLExp]) -> [SQLExp])
-> (ColumnInfo ('Postgres pgKind) -> [SQLExp]) -> [SQLExp]
forall a b. (a -> b) -> a -> b
$ \ColumnInfo ('Postgres pgKind)
pgColumnInfo ->
          [ Text -> SQLExp
S.SELit (Text -> SQLExp) -> Text -> SQLExp
forall a b. (a -> b) -> a -> b
$ PGCol -> Text
getPGColTxt (PGCol -> Text) -> PGCol -> Text
forall a b. (a -> b) -> a -> b
$ ColumnInfo ('Postgres pgKind) -> Column ('Postgres pgKind)
forall (b :: BackendType). ColumnInfo b -> Column b
ciColumn ColumnInfo ('Postgres pgKind)
pgColumnInfo,
            StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
forall (pgKind :: PostgresKind).
StringifyNumbers
-> ColumnType ('Postgres pgKind)
-> Bool
-> Maybe NamingCase
-> SQLExp
-> SQLExp
toJSONableExp StringifyNumbers
Options.Don'tStringifyNumbers (ColumnInfo ('Postgres pgKind) -> ColumnType ('Postgres pgKind)
forall (b :: BackendType). ColumnInfo b -> ColumnType b
ciType ColumnInfo ('Postgres pgKind)
pgColumnInfo) Bool
False Maybe NamingCase
tCase
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ TableIdentifier -> PGCol -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp (TableIdentifier -> TableIdentifier
mkBaseTableIdentifier TableIdentifier
thisPrefix)
              (PGCol -> SQLExp) -> PGCol -> SQLExp
forall a b. (a -> b) -> a -> b
$ ColumnInfo ('Postgres pgKind) -> Column ('Postgres pgKind)
forall (b :: BackendType). ColumnInfo b -> Column b
ciColumn ColumnInfo ('Postgres pgKind)
pgColumnInfo
          ]

    primaryKeyColumnExtractors :: [(ColumnAlias, SQLExp)]
primaryKeyColumnExtractors =
      ((ColumnInfo ('Postgres pgKind) -> (ColumnAlias, SQLExp))
 -> [ColumnInfo ('Postgres pgKind)] -> [(ColumnAlias, SQLExp)])
-> [ColumnInfo ('Postgres pgKind)]
-> (ColumnInfo ('Postgres pgKind) -> (ColumnAlias, SQLExp))
-> [(ColumnAlias, SQLExp)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (ColumnInfo ('Postgres pgKind) -> (ColumnAlias, SQLExp))
-> [ColumnInfo ('Postgres pgKind)] -> [(ColumnAlias, SQLExp)]
forall a b. (a -> b) -> [a] -> [b]
map (PrimaryKeyColumns ('Postgres pgKind)
-> [ColumnInfo ('Postgres pgKind)]
forall a. NESeq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList PrimaryKeyColumns ('Postgres pgKind)
primaryKeyColumns)
        ((ColumnInfo ('Postgres pgKind) -> (ColumnAlias, SQLExp))
 -> [(ColumnAlias, SQLExp)])
-> (ColumnInfo ('Postgres pgKind) -> (ColumnAlias, SQLExp))
-> [(ColumnAlias, SQLExp)]
forall a b. (a -> b) -> a -> b
$ \ColumnInfo ('Postgres pgKind)
pgColumnInfo ->
          let pgColumn :: Column ('Postgres pgKind)
pgColumn = ColumnInfo ('Postgres pgKind) -> Column ('Postgres pgKind)
forall (b :: BackendType). ColumnInfo b -> Column b
ciColumn ColumnInfo ('Postgres pgKind)
pgColumnInfo
           in ( TableIdentifier -> PGCol -> ColumnAlias
contextualizeBaseTableColumn TableIdentifier
thisPrefix Column ('Postgres pgKind)
PGCol
pgColumn,
                TableIdentifier -> PGCol -> SQLExp
forall b. IsIdentifier b => TableIdentifier -> b -> SQLExp
S.mkQIdenExp (TableIdentifier -> TableIdentifier
mkBaseTableIdentifier TableIdentifier
thisPrefix) Column ('Postgres pgKind)
PGCol
pgColumn
              )

    mkSplitBoolExp :: NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp) -> BoolExp
mkSplitBoolExp (ConnectionSplit ('Postgres pgKind) SQLExp
firstSplit NE.:| [ConnectionSplit ('Postgres pgKind) SQLExp]
rest) =
      BinOp -> BoolExp -> BoolExp -> BoolExp
S.BEBin BinOp
S.OrOp (ConnectionSplit ('Postgres pgKind) SQLExp -> BoolExp
mkSplitCompareExp ConnectionSplit ('Postgres pgKind) SQLExp
firstSplit) (BoolExp -> BoolExp) -> BoolExp -> BoolExp
forall a b. (a -> b) -> a -> b
$ ConnectionSplit ('Postgres pgKind) SQLExp
-> [ConnectionSplit ('Postgres pgKind) SQLExp] -> BoolExp
mkBoolExpFromRest ConnectionSplit ('Postgres pgKind) SQLExp
firstSplit [ConnectionSplit ('Postgres pgKind) SQLExp]
rest
      where
        mkBoolExpFromRest :: ConnectionSplit ('Postgres pgKind) SQLExp
-> [ConnectionSplit ('Postgres pgKind) SQLExp] -> BoolExp
mkBoolExpFromRest ConnectionSplit ('Postgres pgKind) SQLExp
previousSplit =
          BinOp -> BoolExp -> BoolExp -> BoolExp
S.BEBin BinOp
S.AndOp (ConnectionSplit ('Postgres pgKind) SQLExp -> BoolExp
mkEqualityCompareExp ConnectionSplit ('Postgres pgKind) SQLExp
previousSplit) (BoolExp -> BoolExp)
-> ([ConnectionSplit ('Postgres pgKind) SQLExp] -> BoolExp)
-> [ConnectionSplit ('Postgres pgKind) SQLExp]
-> BoolExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
            [] -> Bool -> BoolExp
S.BELit Bool
False
            (ConnectionSplit ('Postgres pgKind) SQLExp
thisSplit : [ConnectionSplit ('Postgres pgKind) SQLExp]
remainingSplit) -> NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp) -> BoolExp
mkSplitBoolExp (ConnectionSplit ('Postgres pgKind) SQLExp
thisSplit ConnectionSplit ('Postgres pgKind) SQLExp
-> [ConnectionSplit ('Postgres pgKind) SQLExp]
-> NonEmpty (ConnectionSplit ('Postgres pgKind) SQLExp)
forall a. a -> [a] -> NonEmpty a
NE.:| [ConnectionSplit ('Postgres pgKind) SQLExp]
remainingSplit)

        mkSplitCompareExp :: ConnectionSplit ('Postgres pgKind) SQLExp -> BoolExp
mkSplitCompareExp (ConnectionSplit ConnectionSplitKind
kind SQLExp
v (OrderByItemG Maybe (BasicOrderType ('Postgres pgKind))
obTyM AnnotatedOrderByElement ('Postgres pgKind) SQLExp
obCol Maybe (NullsOrderType ('Postgres pgKind))
_)) =
          let obAlias :: ColumnAlias
obAlias = TableIdentifier
-> FieldName
-> SimilarArrayFields
-> AnnotatedOrderByElement ('Postgres pgKind) SQLExp
-> ColumnAlias
forall (pgKind :: PostgresKind) v.
TableIdentifier
-> FieldName
-> SimilarArrayFields
-> AnnotatedOrderByElement ('Postgres pgKind) v
-> ColumnAlias
mkAnnOrderByAlias TableIdentifier
thisPrefix FieldName
fieldAlias SimilarArrayFields
similarArrayFields AnnotatedOrderByElement ('Postgres pgKind) SQLExp
obCol
              obTy :: OrderType
obTy = OrderType -> Maybe OrderType -> OrderType
forall a. a -> Maybe a -> a
fromMaybe OrderType
S.OTAsc Maybe (BasicOrderType ('Postgres pgKind))
Maybe OrderType
obTyM
              compareOp :: CompareOp
compareOp = case (ConnectionSplitKind
kind, OrderType
obTy) of
                (ConnectionSplitKind
CSKAfter, OrderType
S.OTAsc) -> CompareOp
S.SGT
                (ConnectionSplitKind
CSKAfter, OrderType
S.OTDesc) -> CompareOp
S.SLT
                (ConnectionSplitKind
CSKBefore, OrderType
S.OTAsc) -> CompareOp
S.SLT
                (ConnectionSplitKind
CSKBefore, OrderType
S.OTDesc) -> CompareOp
S.SGT
           in CompareOp -> SQLExp -> SQLExp -> BoolExp
S.BECompare CompareOp
compareOp (Identifier -> SQLExp
S.SEIdentifier (Identifier -> SQLExp) -> Identifier -> SQLExp
forall a b. (a -> b) -> a -> b
$ ColumnAlias -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier ColumnAlias
obAlias) SQLExp
v

        mkEqualityCompareExp :: ConnectionSplit ('Postgres pgKind) SQLExp -> BoolExp
mkEqualityCompareExp (ConnectionSplit ConnectionSplitKind
_ SQLExp
v OrderByItemG
  ('Postgres pgKind)
  (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)
orderByItem) =
          let obAlias :: ColumnAlias
obAlias =
                TableIdentifier
-> FieldName
-> SimilarArrayFields
-> AnnotatedOrderByElement ('Postgres pgKind) SQLExp
-> ColumnAlias
forall (pgKind :: PostgresKind) v.
TableIdentifier
-> FieldName
-> SimilarArrayFields
-> AnnotatedOrderByElement ('Postgres pgKind) v
-> ColumnAlias
mkAnnOrderByAlias TableIdentifier
thisPrefix FieldName
fieldAlias SimilarArrayFields
similarArrayFields
                  (AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> ColumnAlias)
-> AnnotatedOrderByElement ('Postgres pgKind) SQLExp -> ColumnAlias
forall a b. (a -> b) -> a -> b
$ OrderByItemG
  ('Postgres pgKind)
  (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)
-> AnnotatedOrderByElement ('Postgres pgKind) SQLExp
forall (b :: BackendType) a. OrderByItemG b a -> a
obiColumn OrderByItemG
  ('Postgres pgKind)
  (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)
orderByItem
           in CompareOp -> SQLExp -> SQLExp -> BoolExp
S.BECompare CompareOp
S.SEQ (Identifier -> SQLExp
S.SEIdentifier (Identifier -> SQLExp) -> Identifier -> SQLExp
forall a b. (a -> b) -> a -> b
$ ColumnAlias -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier ColumnAlias
obAlias) SQLExp
v

    similarArrayFields :: SimilarArrayFields
similarArrayFields = [SimilarArrayFields] -> SimilarArrayFields
forall k v. (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
HashMap.unions
      ([SimilarArrayFields] -> SimilarArrayFields)
-> [SimilarArrayFields] -> SimilarArrayFields
forall a b. (a -> b) -> a -> b
$ ((ConnectionField ('Postgres pgKind) Void SQLExp
  -> SimilarArrayFields)
 -> [ConnectionField ('Postgres pgKind) Void SQLExp]
 -> [SimilarArrayFields])
-> [ConnectionField ('Postgres pgKind) Void SQLExp]
-> (ConnectionField ('Postgres pgKind) Void SQLExp
    -> SimilarArrayFields)
-> [SimilarArrayFields]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (ConnectionField ('Postgres pgKind) Void SQLExp
 -> SimilarArrayFields)
-> [ConnectionField ('Postgres pgKind) Void SQLExp]
-> [SimilarArrayFields]
forall a b. (a -> b) -> [a] -> [b]
map (((FieldName, ConnectionField ('Postgres pgKind) Void SQLExp)
 -> ConnectionField ('Postgres pgKind) Void SQLExp)
-> Fields (ConnectionField ('Postgres pgKind) Void SQLExp)
-> [ConnectionField ('Postgres pgKind) Void SQLExp]
forall a b. (a -> b) -> [a] -> [b]
map (FieldName, ConnectionField ('Postgres pgKind) Void SQLExp)
-> ConnectionField ('Postgres pgKind) Void SQLExp
forall a b. (a, b) -> b
snd Fields (ConnectionField ('Postgres pgKind) Void SQLExp)
fields)
      ((ConnectionField ('Postgres pgKind) Void SQLExp
  -> SimilarArrayFields)
 -> [SimilarArrayFields])
-> (ConnectionField ('Postgres pgKind) Void SQLExp
    -> SimilarArrayFields)
-> [SimilarArrayFields]
forall a b. (a -> b) -> a -> b
$ \case
        ConnectionTypename {} -> SimilarArrayFields
forall a. Monoid a => a
mempty
        ConnectionPageInfo {} -> SimilarArrayFields
forall a. Monoid a => a
mempty
        ConnectionEdges EdgeFields ('Postgres pgKind) Void SQLExp
edges -> [SimilarArrayFields] -> SimilarArrayFields
forall k v. (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
HashMap.unions
          ([SimilarArrayFields] -> SimilarArrayFields)
-> [SimilarArrayFields] -> SimilarArrayFields
forall a b. (a -> b) -> a -> b
$ ((EdgeField ('Postgres pgKind) Void SQLExp -> SimilarArrayFields)
 -> [EdgeField ('Postgres pgKind) Void SQLExp]
 -> [SimilarArrayFields])
-> [EdgeField ('Postgres pgKind) Void SQLExp]
-> (EdgeField ('Postgres pgKind) Void SQLExp -> SimilarArrayFields)
-> [SimilarArrayFields]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (EdgeField ('Postgres pgKind) Void SQLExp -> SimilarArrayFields)
-> [EdgeField ('Postgres pgKind) Void SQLExp]
-> [SimilarArrayFields]
forall a b. (a -> b) -> [a] -> [b]
map (((FieldName, EdgeField ('Postgres pgKind) Void SQLExp)
 -> EdgeField ('Postgres pgKind) Void SQLExp)
-> EdgeFields ('Postgres pgKind) Void SQLExp
-> [EdgeField ('Postgres pgKind) Void SQLExp]
forall a b. (a -> b) -> [a] -> [b]
map (FieldName, EdgeField ('Postgres pgKind) Void SQLExp)
-> EdgeField ('Postgres pgKind) Void SQLExp
forall a b. (a, b) -> b
snd EdgeFields ('Postgres pgKind) Void SQLExp
edges)
          ((EdgeField ('Postgres pgKind) Void SQLExp -> SimilarArrayFields)
 -> [SimilarArrayFields])
-> (EdgeField ('Postgres pgKind) Void SQLExp -> SimilarArrayFields)
-> [SimilarArrayFields]
forall a b. (a -> b) -> a -> b
$ \case
            EdgeTypename {} -> SimilarArrayFields
forall a. Monoid a => a
mempty
            EdgeCursor {} -> SimilarArrayFields
forall a. Monoid a => a
mempty
            EdgeNode AnnFieldsG ('Postgres pgKind) Void SQLExp
annFields ->
              AnnFieldsG ('Postgres pgKind) Void SQLExp
-> Maybe
     (NonEmpty
        (OrderByItemG
           ('Postgres pgKind)
           (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)))
-> SimilarArrayFields
forall (pgKind :: PostgresKind) v.
(Backend ('Postgres pgKind), Eq v) =>
AnnFieldsG ('Postgres pgKind) Void v
-> Maybe (NonEmpty (AnnotatedOrderByItemG ('Postgres pgKind) v))
-> SimilarArrayFields
mkSimilarArrayFields AnnFieldsG ('Postgres pgKind) Void SQLExp
annFields (Maybe
   (NonEmpty
      (OrderByItemG
         ('Postgres pgKind)
         (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)))
 -> SimilarArrayFields)
-> Maybe
     (NonEmpty
        (OrderByItemG
           ('Postgres pgKind)
           (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)))
-> SimilarArrayFields
forall a b. (a -> b) -> a -> b
$ SelectArgsG ('Postgres pgKind) SQLExp
-> Maybe
     (NonEmpty
        (OrderByItemG
           ('Postgres pgKind)
           (AnnotatedOrderByElement ('Postgres pgKind) SQLExp)))
forall (b :: BackendType) v.
SelectArgsG b v -> Maybe (NonEmpty (AnnotatedOrderByItemG b v))
_saOrderBy SelectArgsG ('Postgres pgKind) SQLExp
tableArgs

    mkSimpleJsonAgg :: SQLExp -> Maybe OrderByExp -> SQLExp
mkSimpleJsonAgg SQLExp
rowExp Maybe OrderByExp
ob =
      let jsonAggExp :: SQLExp
jsonAggExp = Text -> [SQLExp] -> Maybe OrderByExp -> SQLExp
S.SEFnApp Text
"json_agg" [SQLExp
rowExp] Maybe OrderByExp
ob
       in Text -> [SQLExp] -> Maybe OrderByExp -> SQLExp
S.SEFnApp Text
"coalesce" [SQLExp
jsonAggExp, Text -> SQLExp
S.SELit Text
"[]"] Maybe OrderByExp
forall a. Maybe a
Nothing

    processFields ::
      forall n n' t.
      ( MonadState [(S.ColumnAlias, S.SQLExp)] n,
        -- Constraints for 'processAnnFields':
        n ~ (t n'),
        MonadTrans t,
        MonadState NativeQueryFreshIdStore n',
        MonadWriter SelectWriter n',
        MonadReader Options.StringifyNumbers n'
      ) =>
      SelectSource ->
      n S.SQLExp
    processFields :: forall (n :: * -> *) (n' :: * -> *) (t :: (* -> *) -> * -> *).
(MonadState [(ColumnAlias, SQLExp)] n, n ~ t n', MonadTrans t,
 MonadState NativeQueryFreshIdStore n', MonadWriter SelectWriter n',
 MonadReader StringifyNumbers n') =>
SelectSource -> n SQLExp
processFields SelectSource
selectSource =
      ([[SQLExp]] -> SQLExp) -> n [[SQLExp]] -> n SQLExp
forall a b. (a -> b) -> n a -> n b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([SQLExp] -> SQLExp
S.applyJsonBuildObj ([SQLExp] -> SQLExp)
-> ([[SQLExp]] -> [SQLExp]) -> [[SQLExp]] -> SQLExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[SQLExp]] -> [SQLExp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat)
        (n [[SQLExp]] -> n SQLExp) -> n [[SQLExp]] -> n SQLExp
forall a b. (a -> b) -> a -> b
$ Fields (ConnectionField ('Postgres pgKind) Void SQLExp)
-> ((FieldName, ConnectionField ('Postgres pgKind) Void SQLExp)
    -> n [SQLExp])
-> n [[SQLExp]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Fields (ConnectionField ('Postgres pgKind) Void SQLExp)
fields
        (((FieldName, ConnectionField ('Postgres pgKind) Void SQLExp)
  -> n [SQLExp])
 -> n [[SQLExp]])
-> ((FieldName, ConnectionField ('Postgres pgKind) Void SQLExp)
    -> n [SQLExp])
-> n [[SQLExp]]
forall a b. (a -> b) -> a -> b
$ \(FieldName Text
fieldText, ConnectionField ('Postgres pgKind) Void SQLExp
field) ->
          (Text -> SQLExp
S.SELit Text
fieldText SQLExp -> [SQLExp] -> [SQLExp]
forall a. a -> [a] -> [a]
:)
            ([SQLExp] -> [SQLExp])
-> (SQLExp -> [SQLExp]) -> SQLExp -> [SQLExp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SQLExp -> [SQLExp]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            (SQLExp -> [SQLExp]) -> n SQLExp -> n [SQLExp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case ConnectionField ('Postgres pgKind) Void SQLExp
field of
              ConnectionTypename Text
t -> SQLExp -> n SQLExp
forall a. a -> n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> n SQLExp) -> SQLExp -> n SQLExp
forall a b. (a -> b) -> a -> b
$ TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.textTypeAnn (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> SQLExp
S.SELit Text
t
              ConnectionPageInfo PageInfoFields
pageInfoFields -> SQLExp -> n SQLExp
forall a. a -> n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> n SQLExp) -> SQLExp -> n SQLExp
forall a b. (a -> b) -> a -> b
$ PageInfoFields -> SQLExp
forall {t :: * -> *}.
Foldable t =>
t (FieldName, PageInfoField) -> SQLExp
processPageInfoFields PageInfoFields
pageInfoFields
              ConnectionEdges EdgeFields ('Postgres pgKind) Void SQLExp
edges ->
                ([[SQLExp]] -> SQLExp) -> n [[SQLExp]] -> n SQLExp
forall a b. (a -> b) -> n a -> n b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((SQLExp -> Maybe OrderByExp -> SQLExp)
-> Maybe OrderByExp -> SQLExp -> SQLExp
forall a b c. (a -> b -> c) -> b -> a -> c
flip SQLExp -> Maybe OrderByExp -> SQLExp
mkSimpleJsonAgg (SelectSource -> Maybe OrderByExp
orderByForJsonAgg SelectSource
selectSource) (SQLExp -> SQLExp)
-> ([[SQLExp]] -> SQLExp) -> [[SQLExp]] -> SQLExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SQLExp] -> SQLExp
S.applyJsonBuildObj ([SQLExp] -> SQLExp)
-> ([[SQLExp]] -> [SQLExp]) -> [[SQLExp]] -> SQLExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[SQLExp]] -> [SQLExp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat)
                  (n [[SQLExp]] -> n SQLExp) -> n [[SQLExp]] -> n SQLExp
forall a b. (a -> b) -> a -> b
$ EdgeFields ('Postgres pgKind) Void SQLExp
-> ((FieldName, EdgeField ('Postgres pgKind) Void SQLExp)
    -> n [SQLExp])
-> n [[SQLExp]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM EdgeFields ('Postgres pgKind) Void SQLExp
edges
                  (((FieldName, EdgeField ('Postgres pgKind) Void SQLExp)
  -> n [SQLExp])
 -> n [[SQLExp]])
-> ((FieldName, EdgeField ('Postgres pgKind) Void SQLExp)
    -> n [SQLExp])
-> n [[SQLExp]]
forall a b. (a -> b) -> a -> b
$ \(FieldName Text
edgeText, EdgeField ('Postgres pgKind) Void SQLExp
edge) ->
                    (Text -> SQLExp
S.SELit Text
edgeText SQLExp -> [SQLExp] -> [SQLExp]
forall a. a -> [a] -> [a]
:)
                      ([SQLExp] -> [SQLExp])
-> (SQLExp -> [SQLExp]) -> SQLExp -> [SQLExp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SQLExp -> [SQLExp]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
                      (SQLExp -> [SQLExp]) -> n SQLExp -> n [SQLExp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case EdgeField ('Postgres pgKind) Void SQLExp
edge of
                        EdgeTypename Text
t -> SQLExp -> n SQLExp
forall a. a -> n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> n SQLExp) -> SQLExp -> n SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> SQLExp
S.SELit Text
t
                        EdgeField ('Postgres pgKind) Void SQLExp
EdgeCursor -> SQLExp -> n SQLExp
forall a. a -> n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> n SQLExp) -> SQLExp -> n SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> SQLExp
encodeBase64 (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Identifier -> SQLExp
S.SEIdentifier (Identifier -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier Identifier
cursorIdentifier)
                        EdgeNode AnnFieldsG ('Postgres pgKind) Void SQLExp
annFields -> do
                          let edgeFieldName :: FieldName
edgeFieldName =
                                Text -> FieldName
FieldName
                                  (Text -> FieldName) -> Text -> FieldName
forall a b. (a -> b) -> a -> b
$ FieldName -> Text
getFieldNameTxt FieldName
fieldAlias
                                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
                                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
fieldText
                                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
                                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
edgeText
                              edgeFieldIdentifier :: Identifier
edgeFieldIdentifier = FieldName -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier FieldName
edgeFieldName
                          (ColumnAlias, SQLExp)
annFieldsExtrExp <- n' (ColumnAlias, SQLExp) -> t n' (ColumnAlias, SQLExp)
forall (m :: * -> *) a. Monad m => m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (n' (ColumnAlias, SQLExp) -> t n' (ColumnAlias, SQLExp))
-> n' (ColumnAlias, SQLExp) -> t n' (ColumnAlias, SQLExp)
forall a b. (a -> b) -> a -> b
$ TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> n' (ColumnAlias, SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter SelectWriter m,
 MonadState NativeQueryFreshIdStore m, Backend ('Postgres pgKind),
 PostgresAnnotatedFieldJSON pgKind) =>
TableIdentifier
-> FieldName
-> AnnFields ('Postgres pgKind)
-> Maybe NamingCase
-> m (ColumnAlias, SQLExp)
processAnnFields TableIdentifier
thisPrefix FieldName
edgeFieldName AnnFields ('Postgres pgKind)
AnnFieldsG ('Postgres pgKind) Void SQLExp
annFields Maybe NamingCase
tCase
                          ([(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]) -> n ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' ([(ColumnAlias, SQLExp)]
-> [(ColumnAlias, SQLExp)] -> [(ColumnAlias, SQLExp)]
forall a. Semigroup a => a -> a -> a
<> [(ColumnAlias, SQLExp)
annFieldsExtrExp])
                          SQLExp -> n SQLExp
forall a. a -> n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLExp -> n SQLExp) -> SQLExp -> n SQLExp
forall a b. (a -> b) -> a -> b
$ Identifier -> SQLExp
S.SEIdentifier Identifier
edgeFieldIdentifier

    processPageInfoFields :: t (FieldName, PageInfoField) -> SQLExp
processPageInfoFields t (FieldName, PageInfoField)
infoFields =
      [SQLExp] -> SQLExp
S.applyJsonBuildObj
        ([SQLExp] -> SQLExp) -> [SQLExp] -> SQLExp
forall a b. (a -> b) -> a -> b
$ (((FieldName, PageInfoField) -> [SQLExp])
 -> t (FieldName, PageInfoField) -> [SQLExp])
-> t (FieldName, PageInfoField)
-> ((FieldName, PageInfoField) -> [SQLExp])
-> [SQLExp]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((FieldName, PageInfoField) -> [SQLExp])
-> t (FieldName, PageInfoField) -> [SQLExp]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap t (FieldName, PageInfoField)
infoFields
        (((FieldName, PageInfoField) -> [SQLExp]) -> [SQLExp])
-> ((FieldName, PageInfoField) -> [SQLExp]) -> [SQLExp]
forall a b. (a -> b) -> a -> b
$ \(FieldName Text
fieldText, PageInfoField
field) -> (:) (Text -> SQLExp
S.SELit Text
fieldText) ([SQLExp] -> [SQLExp]) -> [SQLExp] -> [SQLExp]
forall a b. (a -> b) -> a -> b
$ SQLExp -> [SQLExp]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure case PageInfoField
field of
          PageInfoTypename Text
t -> TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.textTypeAnn (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ Text -> SQLExp
S.SELit Text
t
          PageInfoField
PageInfoHasNextPage ->
            TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.boolTypeAnn
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> TableIdentifier -> SQLExp
mkSingleFieldSelect (Identifier -> SQLExp
S.SEIdentifier Identifier
hasNextPageIdentifier) TableIdentifier
pageInfoSelectAliasIdentifier
          PageInfoField
PageInfoHasPreviousPage ->
            TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.boolTypeAnn
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> TableIdentifier -> SQLExp
mkSingleFieldSelect (Identifier -> SQLExp
S.SEIdentifier Identifier
hasPreviousPageIdentifier) TableIdentifier
pageInfoSelectAliasIdentifier
          PageInfoField
PageInfoStartCursor ->
            TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.textTypeAnn
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> SQLExp
encodeBase64
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> TableIdentifier -> SQLExp
mkSingleFieldSelect (Identifier -> SQLExp
S.SEIdentifier Identifier
startCursorIdentifier) TableIdentifier
cursorsSelectAliasIdentifier
          PageInfoField
PageInfoEndCursor ->
            TypeAnn -> SQLExp -> SQLExp
withForceAggregation TypeAnn
S.textTypeAnn
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> SQLExp
encodeBase64
              (SQLExp -> SQLExp) -> SQLExp -> SQLExp
forall a b. (a -> b) -> a -> b
$ SQLExp -> TableIdentifier -> SQLExp
mkSingleFieldSelect (Identifier -> SQLExp
S.SEIdentifier Identifier
endCursorIdentifier) TableIdentifier
cursorsSelectAliasIdentifier
      where
        mkSingleFieldSelect :: SQLExp -> TableIdentifier -> SQLExp
mkSingleFieldSelect SQLExp
field TableIdentifier
fromIdentifier =
          Select -> SQLExp
S.SESelect
            Select
S.mkSelect
              { selExtr :: [Extractor]
S.selExtr = [SQLExp -> Maybe ColumnAlias -> Extractor
S.Extractor SQLExp
field Maybe ColumnAlias
forall a. Maybe a
Nothing],
                selFrom :: Maybe FromExp
S.selFrom = FromExp -> Maybe FromExp
forall a. a -> Maybe a
Just (FromExp -> Maybe FromExp) -> FromExp -> Maybe FromExp
forall a b. (a -> b) -> a -> b
$ [FromItem] -> FromExp
S.FromExp [TableIdentifier -> FromItem
S.FIIdentifier TableIdentifier
fromIdentifier]
              }