-- | This module defines the top-level translation functions pertaining to
-- translating Connection (i.e. Relay) queries into Postgres AST.
module Hasura.Backends.Postgres.Translate.Select.Connection
  ( connectionSelectQuerySQL,
    mkConnectionSelect,
  )
where

import Control.Monad.Writer (runWriter)
import Database.PG.Query (Query, fromBuilder)
import Hasura.Backends.Postgres.SQL.DML qualified as S
import Hasura.Backends.Postgres.SQL.RenameIdentifiers (renameIdentifiersSelectWith)
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.Translate.Select.AnnotatedFieldJSON
import Hasura.Backends.Postgres.Translate.Select.Internal.GenerateSelect (connectionToSelectWith)
import Hasura.Backends.Postgres.Translate.Select.Internal.Process (processConnectionSelect)
import Hasura.Backends.Postgres.Translate.Types
import Hasura.Prelude
import Hasura.RQL.IR.Select
  ( AnnSelectG (_asnStrfyNum),
    ConnectionSelect (_csSelect),
  )
import Hasura.RQL.Types.Backend (Backend)
import Hasura.RQL.Types.Common (FieldName (FieldName))
import Hasura.SQL.Backend (BackendType (Postgres))
import Hasura.SQL.Types (ToSQL (toSQL))

-- | Translates IR to Postgres queries for "connection" queries (used for Relay).
--
-- See 'mkConnectionSelect' for the Postgres AST.
connectionSelectQuerySQL ::
  forall pgKind.
  ( Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  ConnectionSelect ('Postgres pgKind) Void S.SQLExp ->
  Query
connectionSelectQuerySQL :: ConnectionSelect ('Postgres pgKind) Void SQLExp -> Query
connectionSelectQuerySQL =
  Builder -> Query
fromBuilder (Builder -> Query)
-> (ConnectionSelect ('Postgres pgKind) Void SQLExp -> Builder)
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SelectWithG Select -> Builder
forall a. ToSQL a => a -> Builder
toSQL (SelectWithG Select -> Builder)
-> (ConnectionSelect ('Postgres pgKind) Void SQLExp
    -> SelectWithG Select)
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnectionSelect ('Postgres pgKind) Void SQLExp
-> SelectWithG Select
forall (pgKind :: PostgresKind).
(Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
ConnectionSelect ('Postgres pgKind) Void SQLExp
-> SelectWithG Select
mkConnectionSelect

mkConnectionSelect ::
  forall pgKind.
  ( Backend ('Postgres pgKind),
    PostgresAnnotatedFieldJSON pgKind
  ) =>
  ConnectionSelect ('Postgres pgKind) Void S.SQLExp ->
  S.SelectWithG S.Select
mkConnectionSelect :: ConnectionSelect ('Postgres pgKind) Void SQLExp
-> SelectWithG Select
mkConnectionSelect ConnectionSelect ('Postgres pgKind) Void SQLExp
connectionSelect =
  let ((ArrayConnectionSource
connectionSource, Extractor
topExtractor, HashMap ColumnAlias SQLExp
nodeExtractors), JoinTree
joinTree) =
        Writer
  JoinTree
  (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
-> ((ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp),
    JoinTree)
forall w a. Writer w a -> (a, w)
runWriter (Writer
   JoinTree
   (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
 -> ((ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp),
     JoinTree))
-> Writer
     JoinTree
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
-> ((ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp),
    JoinTree)
forall a b. (a -> b) -> a -> b
$
          (ReaderT
   StringifyNumbers
   (WriterT JoinTree Identity)
   (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
 -> StringifyNumbers
 -> Writer
      JoinTree
      (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp))
-> StringifyNumbers
-> ReaderT
     StringifyNumbers
     (WriterT JoinTree Identity)
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
-> Writer
     JoinTree
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
  StringifyNumbers
  (WriterT JoinTree Identity)
  (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
-> StringifyNumbers
-> Writer
     JoinTree
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT StringifyNumbers
strfyNum (ReaderT
   StringifyNumbers
   (WriterT JoinTree Identity)
   (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
 -> Writer
      JoinTree
      (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp))
-> ReaderT
     StringifyNumbers
     (WriterT JoinTree Identity)
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
-> Writer
     JoinTree
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
forall a b. (a -> b) -> a -> b
$
            SourcePrefixes
-> FieldName
-> TableAlias
-> HashMap PGCol PGCol
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> ReaderT
     StringifyNumbers
     (WriterT JoinTree Identity)
     (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadReader StringifyNumbers m, MonadWriter JoinTree m,
 Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
SourcePrefixes
-> FieldName
-> TableAlias
-> HashMap PGCol PGCol
-> ConnectionSelect ('Postgres pgKind) Void SQLExp
-> m (ArrayConnectionSource, Extractor, HashMap ColumnAlias SQLExp)
processConnectionSelect
              SourcePrefixes
sourcePrefixes
              FieldName
rootFieldName
              (Identifier -> TableAlias
forall a. IsIdentifier a => a -> TableAlias
S.toTableAlias Identifier
rootIdentifier)
              HashMap PGCol PGCol
forall a. Monoid a => a
mempty
              ConnectionSelect ('Postgres pgKind) Void SQLExp
connectionSelect
      selectNode :: MultiRowSelectNode
selectNode =
        [Extractor] -> SelectNode -> MultiRowSelectNode
MultiRowSelectNode [Extractor
topExtractor] (SelectNode -> MultiRowSelectNode)
-> SelectNode -> MultiRowSelectNode
forall a b. (a -> b) -> a -> b
$
          HashMap ColumnAlias SQLExp -> JoinTree -> SelectNode
SelectNode HashMap ColumnAlias SQLExp
nodeExtractors JoinTree
joinTree
   in SelectWithG Select -> SelectWithG Select
renameIdentifiersSelectWith (SelectWithG Select -> SelectWithG Select)
-> SelectWithG Select -> SelectWithG Select
forall a b. (a -> b) -> a -> b
$
        TableAlias
-> ArrayConnectionSource
-> MultiRowSelectNode
-> SelectWithG Select
connectionToSelectWith (Identifier -> TableAlias
forall a. IsIdentifier a => a -> TableAlias
S.toTableAlias Identifier
rootIdentifier) ArrayConnectionSource
connectionSource MultiRowSelectNode
selectNode
  where
    strfyNum :: StringifyNumbers
strfyNum = AnnSelectG
  ('Postgres pgKind) (ConnectionField ('Postgres pgKind) Void) SQLExp
-> StringifyNumbers
forall (b :: BackendType) (f :: * -> *) v.
AnnSelectG b f v -> StringifyNumbers
_asnStrfyNum (AnnSelectG
   ('Postgres pgKind) (ConnectionField ('Postgres pgKind) Void) SQLExp
 -> StringifyNumbers)
-> AnnSelectG
     ('Postgres pgKind) (ConnectionField ('Postgres pgKind) Void) SQLExp
-> StringifyNumbers
forall a b. (a -> b) -> a -> b
$ ConnectionSelect ('Postgres pgKind) Void SQLExp
-> AnnSelectG
     ('Postgres pgKind) (ConnectionField ('Postgres pgKind) Void) SQLExp
forall (b :: BackendType) r v.
ConnectionSelect b r v -> AnnSelectG b (ConnectionField b r) v
_csSelect ConnectionSelect ('Postgres pgKind) Void SQLExp
connectionSelect
    rootFieldName :: FieldName
rootFieldName = Text -> FieldName
FieldName Text
"root"
    rootIdentifier :: Identifier
rootIdentifier = FieldName -> Identifier
forall a. IsIdentifier a => a -> Identifier
toIdentifier FieldName
rootFieldName
    sourcePrefixes :: SourcePrefixes
sourcePrefixes = Identifier -> Identifier -> SourcePrefixes
SourcePrefixes Identifier
rootIdentifier Identifier
rootIdentifier