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,
|
|
|
|
mkBaseTableColumnAlias,
|
|
|
|
mkComputedFieldTableAlias,
|
|
|
|
mkObjectRelationTableAlias,
|
|
|
|
mkOrderByFieldName,
|
|
|
|
)
|
|
|
|
where
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
import Control.Monad.Writer.Strict
|
|
|
|
import Data.HashMap.Strict qualified as HM
|
|
|
|
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
|
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
|
|
|
|
import Hasura.SQL.Backend
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
-- | Generate alias for order by extractors
|
|
|
|
mkAnnOrderByAlias ::
|
2022-07-18 12:44:17 +03:00
|
|
|
Identifier -> FieldName -> SimilarArrayFields -> AnnotatedOrderByElement ('Postgres pgKind) v -> S.ColumnAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
mkAnnOrderByAlias pfx parAls similarFields = \case
|
|
|
|
AOCColumn pgColumnInfo ->
|
|
|
|
let pgColumn = ciColumn pgColumnInfo
|
|
|
|
obColAls = mkBaseTableColumnAlias pfx 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
|
|
|
|
relPfx = mkObjectRelationTableAlias pfx rn
|
|
|
|
ordByFldName = mkOrderByFieldName rn
|
|
|
|
nesAls = mkAnnOrderByAlias relPfx ordByFldName mempty rest
|
|
|
|
in nesAls
|
|
|
|
AOCArrayAggregation relInfo _ aggOrderBy ->
|
|
|
|
let rn = riName relInfo
|
|
|
|
arrPfx =
|
|
|
|
mkArrayRelationSourcePrefix pfx parAls similarFields $
|
|
|
|
mkOrderByFieldName rn
|
|
|
|
obAls = arrPfx <> Identifier "." <> toIdentifier (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-07-18 12:44:17 +03:00
|
|
|
CFOBEScalar _ -> S.toColumnAlias $ mkComputedFieldTableAlias pfx fieldName
|
2022-04-22 16:38:35 +03:00
|
|
|
CFOBETableAggregation _ _ aggOrderBy ->
|
|
|
|
let cfPfx = mkComputedFieldTableAlias pfx fieldName
|
|
|
|
obAls = cfPfx <> Identifier "." <> toIdentifier (mkAggregateOrderByAlias aggOrderBy)
|
2022-07-18 12:44:17 +03:00
|
|
|
in S.toColumnAlias obAls
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
-- array relationships are not grouped, so have to be prefixed by
|
|
|
|
-- parent's alias
|
|
|
|
mkUniqArrayRelationAlias :: FieldName -> [FieldName] -> Identifier
|
|
|
|
mkUniqArrayRelationAlias parAls flds =
|
|
|
|
let sortedFields = sort flds
|
|
|
|
in Identifier $
|
|
|
|
getFieldNameTxt parAls <> "."
|
|
|
|
<> T.intercalate "." (map getFieldNameTxt sortedFields)
|
|
|
|
|
|
|
|
mkArrayRelationTableAlias :: Identifier -> FieldName -> [FieldName] -> Identifier
|
|
|
|
mkArrayRelationTableAlias pfx parAls flds =
|
|
|
|
pfx <> Identifier ".ar." <> uniqArrRelAls
|
|
|
|
where
|
|
|
|
uniqArrRelAls = mkUniqArrayRelationAlias parAls flds
|
|
|
|
|
|
|
|
mkObjectRelationTableAlias :: Identifier -> RelName -> Identifier
|
|
|
|
mkObjectRelationTableAlias pfx relName =
|
|
|
|
pfx <> Identifier ".or." <> toIdentifier relName
|
|
|
|
|
|
|
|
mkComputedFieldTableAlias :: Identifier -> FieldName -> Identifier
|
|
|
|
mkComputedFieldTableAlias pfx fldAls =
|
|
|
|
pfx <> Identifier ".cf." <> toIdentifier fldAls
|
|
|
|
|
2022-07-18 12:44:17 +03:00
|
|
|
mkBaseTableAlias :: Identifier -> S.TableAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
mkBaseTableAlias pfx =
|
2022-07-18 12:44:17 +03:00
|
|
|
S.toTableAlias $ pfx <> Identifier ".base"
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-07-18 12:44:17 +03:00
|
|
|
mkBaseTableColumnAlias :: Identifier -> PGCol -> S.ColumnAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
mkBaseTableColumnAlias pfx pgColumn =
|
2022-07-18 12:44:17 +03:00
|
|
|
S.toColumnAlias $ pfx <> Identifier ".pg." <> toIdentifier 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"
|
|
|
|
AAOOp opText col -> opText <> "." <> getPGColTxt (ciColumn col)
|
|
|
|
|
|
|
|
mkOrderByFieldName :: ToTxt a => a -> FieldName
|
|
|
|
mkOrderByFieldName name =
|
|
|
|
FieldName $ toTxt name <> "." <> "order_by"
|
|
|
|
|
|
|
|
mkArrayRelationSourcePrefix ::
|
|
|
|
Identifier ->
|
|
|
|
FieldName ->
|
|
|
|
HM.HashMap FieldName [FieldName] ->
|
|
|
|
FieldName ->
|
|
|
|
Identifier
|
|
|
|
mkArrayRelationSourcePrefix parentSourcePrefix parentFieldName similarFieldsMap fieldName =
|
|
|
|
mkArrayRelationTableAlias parentSourcePrefix parentFieldName $
|
|
|
|
HM.lookupDefault [fieldName] fieldName similarFieldsMap
|
|
|
|
|
|
|
|
mkArrayRelationAlias ::
|
|
|
|
FieldName ->
|
|
|
|
HM.HashMap FieldName [FieldName] ->
|
|
|
|
FieldName ->
|
2022-07-18 12:44:17 +03:00
|
|
|
S.TableAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
mkArrayRelationAlias parentFieldName similarFieldsMap fieldName =
|
2022-07-18 12:44:17 +03:00
|
|
|
S.toTableAlias $
|
2022-04-22 16:38:35 +03:00
|
|
|
mkUniqArrayRelationAlias parentFieldName $
|
|
|
|
HM.lookupDefault [fieldName] fieldName similarFieldsMap
|