2022-04-22 16:38:35 +03:00
|
|
|
-- | Stuff gutted from Translate.Select
|
2022-04-22 20:18:20 +03:00
|
|
|
module Hasura.Backends.Postgres.Translate.Select.Internal.Aliases
|
|
|
|
( mkAggregateOrderByAlias,
|
|
|
|
mkAnnOrderByAlias,
|
|
|
|
mkArrayRelationAlias,
|
|
|
|
mkArrayRelationSourcePrefix,
|
|
|
|
mkBaseTableAlias,
|
2022-10-11 13:42:15 +03:00
|
|
|
mkBaseTableIdentifier,
|
|
|
|
contextualizeBaseTableColumn,
|
|
|
|
mkComputedFieldTableIdentifier,
|
2022-04-22 20:18:20 +03:00
|
|
|
mkObjectRelationTableAlias,
|
|
|
|
mkOrderByFieldName,
|
|
|
|
)
|
|
|
|
where
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
import Control.Monad.Writer.Strict
|
2023-04-26 18:42:13 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-04-22 16:38:35 +03:00
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.Translate.Types
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.Select
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.ComputedField
|
|
|
|
import Hasura.RQL.Types.Relationships.Local
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
-- | Generate alias for order by extractors
|
|
|
|
mkAnnOrderByAlias ::
|
2022-10-11 13:42:15 +03:00
|
|
|
TableIdentifier -> FieldName -> SimilarArrayFields -> AnnotatedOrderByElement ('Postgres pgKind) v -> S.ColumnAlias
|
|
|
|
mkAnnOrderByAlias tablePrefix parAls similarFields = \case
|
2022-04-22 16:38:35 +03:00
|
|
|
AOCColumn pgColumnInfo ->
|
|
|
|
let pgColumn = ciColumn pgColumnInfo
|
2022-10-11 13:42:15 +03:00
|
|
|
obColAls = contextualizeBaseTableColumn tablePrefix pgColumn
|
2022-07-18 12:44:17 +03:00
|
|
|
in obColAls
|
2022-04-22 16:38:35 +03:00
|
|
|
-- "pfx.or.relname"."pfx.ob.or.relname.rest" AS "pfx.ob.or.relname.rest"
|
|
|
|
AOCObjectRelation relInfo _ rest ->
|
|
|
|
let rn = riName relInfo
|
2022-10-11 13:42:15 +03:00
|
|
|
relPfx = mkObjectRelationTableAlias tablePrefix rn
|
2022-04-22 16:38:35 +03:00
|
|
|
ordByFldName = mkOrderByFieldName rn
|
|
|
|
nesAls = mkAnnOrderByAlias relPfx ordByFldName mempty rest
|
|
|
|
in nesAls
|
|
|
|
AOCArrayAggregation relInfo _ aggOrderBy ->
|
|
|
|
let rn = riName relInfo
|
|
|
|
arrPfx =
|
2022-10-11 13:42:15 +03:00
|
|
|
mkArrayRelationSourcePrefix tablePrefix parAls similarFields $
|
2022-04-22 16:38:35 +03:00
|
|
|
mkOrderByFieldName rn
|
2022-10-11 13:42:15 +03:00
|
|
|
obAls = S.tableIdentifierToColumnAlias arrPfx <> "." <> mkAggregateOrderByAlias aggOrderBy
|
2022-07-18 12:44:17 +03:00
|
|
|
in S.toColumnAlias obAls
|
2022-04-22 16:38:35 +03:00
|
|
|
AOCComputedField cfOrderBy ->
|
|
|
|
let fieldName = fromComputedField $ _cfobName cfOrderBy
|
|
|
|
in case _cfobOrderByElement cfOrderBy of
|
2022-10-11 13:42:15 +03:00
|
|
|
CFOBEScalar _ -> S.tableIdentifierToColumnAlias $ mkComputedFieldTableIdentifier tablePrefix fieldName
|
2022-04-22 16:38:35 +03:00
|
|
|
CFOBETableAggregation _ _ aggOrderBy ->
|
2022-10-11 13:42:15 +03:00
|
|
|
let cfPfx = mkComputedFieldTableIdentifier tablePrefix fieldName
|
|
|
|
obAls = S.tableIdentifierToColumnAlias cfPfx <> "." <> mkAggregateOrderByAlias aggOrderBy
|
2022-07-18 12:44:17 +03:00
|
|
|
in S.toColumnAlias obAls
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
mkObjectRelationTableAlias :: TableIdentifier -> RelName -> TableIdentifier
|
2022-04-22 16:38:35 +03:00
|
|
|
mkObjectRelationTableAlias pfx relName =
|
2022-10-11 13:42:15 +03:00
|
|
|
pfx <> TableIdentifier (".or." <> relNameToTxt relName)
|
|
|
|
|
|
|
|
mkComputedFieldTableIdentifier :: TableIdentifier -> FieldName -> TableIdentifier
|
|
|
|
mkComputedFieldTableIdentifier pfx fldAls =
|
|
|
|
pfx <> TableIdentifier ".cf." <> TableIdentifier (getFieldNameTxt fldAls)
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
mkBaseTableIdentifier :: TableIdentifier -> TableIdentifier
|
|
|
|
mkBaseTableIdentifier pfx = pfx <> TableIdentifier ".base"
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
mkBaseTableAlias :: S.TableAlias -> S.TableAlias
|
|
|
|
mkBaseTableAlias pfx = pfx <> ".base"
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
contextualizeBaseTableColumn :: TableIdentifier -> PGCol -> S.ColumnAlias
|
|
|
|
contextualizeBaseTableColumn pfx pgColumn =
|
|
|
|
S.tableIdentifierToColumnAlias pfx <> ".pg." <> S.mkColumnAlias (getPGColTxt pgColumn)
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-07-18 12:44:17 +03:00
|
|
|
mkAggregateOrderByAlias :: AnnotatedAggregateOrderBy ('Postgres pgKind) -> S.ColumnAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
mkAggregateOrderByAlias =
|
2022-07-18 12:44:17 +03:00
|
|
|
(S.toColumnAlias . Identifier) . \case
|
2022-04-22 16:38:35 +03:00
|
|
|
AAOCount -> "count"
|
2023-02-06 07:18:54 +03:00
|
|
|
AAOOp opText _resultType col -> opText <> "." <> getPGColTxt (ciColumn col)
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
mkOrderByFieldName :: ToTxt a => a -> FieldName
|
|
|
|
mkOrderByFieldName name =
|
|
|
|
FieldName $ toTxt name <> "." <> "order_by"
|
|
|
|
|
|
|
|
mkArrayRelationSourcePrefix ::
|
2022-10-11 13:42:15 +03:00
|
|
|
TableIdentifier ->
|
2022-04-22 16:38:35 +03:00
|
|
|
FieldName ->
|
2023-04-26 18:42:13 +03:00
|
|
|
HashMap.HashMap FieldName [FieldName] ->
|
2022-04-22 16:38:35 +03:00
|
|
|
FieldName ->
|
2022-10-11 13:42:15 +03:00
|
|
|
TableIdentifier
|
2022-04-22 16:38:35 +03:00
|
|
|
mkArrayRelationSourcePrefix parentSourcePrefix parentFieldName similarFieldsMap fieldName =
|
2022-10-11 13:42:15 +03:00
|
|
|
mkArrayRelationTableIdentifier parentSourcePrefix parentFieldName $
|
2023-04-26 18:42:13 +03:00
|
|
|
HashMap.lookupDefault [fieldName] fieldName similarFieldsMap
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
mkArrayRelationTableIdentifier :: TableIdentifier -> FieldName -> [FieldName] -> TableIdentifier
|
|
|
|
mkArrayRelationTableIdentifier pfx parAls flds =
|
|
|
|
pfx <> TableIdentifier ".ar." <> TableIdentifier uniqArrRelAls
|
|
|
|
where
|
|
|
|
uniqArrRelAls = mkUniqArrayRelationAlias parAls flds
|
|
|
|
|
2022-04-22 16:38:35 +03:00
|
|
|
mkArrayRelationAlias ::
|
|
|
|
FieldName ->
|
2023-04-26 18:42:13 +03:00
|
|
|
HashMap.HashMap FieldName [FieldName] ->
|
2022-04-22 16:38:35 +03:00
|
|
|
FieldName ->
|
2022-07-18 12:44:17 +03:00
|
|
|
S.TableAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
mkArrayRelationAlias parentFieldName similarFieldsMap fieldName =
|
2022-10-11 13:42:15 +03:00
|
|
|
S.mkTableAlias $
|
2022-04-22 16:38:35 +03:00
|
|
|
mkUniqArrayRelationAlias parentFieldName $
|
2023-04-26 18:42:13 +03:00
|
|
|
HashMap.lookupDefault [fieldName] fieldName similarFieldsMap
|
2022-10-11 13:42:15 +03:00
|
|
|
|
|
|
|
-- array relationships are not grouped, so have to be prefixed by
|
|
|
|
-- parent's alias
|
|
|
|
mkUniqArrayRelationAlias :: FieldName -> [FieldName] -> Text
|
|
|
|
mkUniqArrayRelationAlias parAls flds =
|
|
|
|
let sortedFields = sort flds
|
|
|
|
in getFieldNameTxt parAls
|
|
|
|
<> "."
|
|
|
|
<> T.intercalate "." (map getFieldNameTxt sortedFields)
|