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.Helpers
|
|
|
|
( mkFirstElementExp,
|
|
|
|
mkLastElementExp,
|
|
|
|
cursorIdentifier,
|
|
|
|
startCursorIdentifier,
|
|
|
|
endCursorIdentifier,
|
|
|
|
hasNextPageIdentifier,
|
|
|
|
hasPreviousPageIdentifier,
|
2022-10-05 13:03:36 +03:00
|
|
|
pageInfoSelectAlias,
|
2022-04-22 20:18:20 +03:00
|
|
|
pageInfoSelectAliasIdentifier,
|
2022-10-05 13:03:36 +03:00
|
|
|
cursorsSelectAlias,
|
2022-04-22 20:18:20 +03:00
|
|
|
cursorsSelectAliasIdentifier,
|
|
|
|
encodeBase64,
|
|
|
|
fromTableRowArgs,
|
|
|
|
selectFromToFromItem,
|
|
|
|
functionToIdentifier,
|
|
|
|
withJsonBuildObj,
|
|
|
|
withForceAggregation,
|
|
|
|
)
|
|
|
|
where
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
2022-10-11 13:42:15 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
( Identifier (..),
|
|
|
|
QualifiedFunction,
|
|
|
|
TableIdentifier (..),
|
|
|
|
qualifiedObjectToText,
|
|
|
|
tableIdentifierToIdentifier,
|
|
|
|
)
|
2022-04-22 20:18:20 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select.Internal.Aliases
|
2022-05-25 13:24:41 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Function
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR
|
|
|
|
import Hasura.RQL.Types.Common (FieldName)
|
2022-05-25 13:24:41 +03:00
|
|
|
import Hasura.RQL.Types.Function
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.SQL.Backend
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
-- | First element extractor expression from given record set
|
|
|
|
-- For example:- To get first "id" column from given row set,
|
|
|
|
-- the function generates the SQL expression AS `(array_agg("id"))[1]`
|
|
|
|
mkFirstElementExp :: S.SQLExp -> S.SQLExp
|
|
|
|
mkFirstElementExp expIdentifier =
|
|
|
|
-- For Example
|
|
|
|
S.SEArrayIndex (S.SEFnApp "array_agg" [expIdentifier] Nothing) (S.intToSQLExp 1)
|
|
|
|
|
|
|
|
-- | Last element extractor expression from given record set.
|
|
|
|
-- For example:- To get first "id" column from given row set,
|
|
|
|
-- the function generates the SQL expression AS `(array_agg("id"))[array_length(array_agg("id"), 1)]`
|
|
|
|
mkLastElementExp :: S.SQLExp -> S.SQLExp
|
|
|
|
mkLastElementExp expIdentifier =
|
|
|
|
let arrayExp = S.SEFnApp "array_agg" [expIdentifier] Nothing
|
|
|
|
in S.SEArrayIndex arrayExp $
|
|
|
|
S.SEFnApp "array_length" [arrayExp, S.intToSQLExp 1] Nothing
|
|
|
|
|
|
|
|
cursorIdentifier :: Identifier
|
|
|
|
cursorIdentifier = Identifier "__cursor"
|
|
|
|
|
|
|
|
startCursorIdentifier :: Identifier
|
|
|
|
startCursorIdentifier = Identifier "__start_cursor"
|
|
|
|
|
|
|
|
endCursorIdentifier :: Identifier
|
|
|
|
endCursorIdentifier = Identifier "__end_cursor"
|
|
|
|
|
|
|
|
hasPreviousPageIdentifier :: Identifier
|
|
|
|
hasPreviousPageIdentifier = Identifier "__has_previous_page"
|
|
|
|
|
|
|
|
hasNextPageIdentifier :: Identifier
|
|
|
|
hasNextPageIdentifier = Identifier "__has_next_page"
|
|
|
|
|
2022-10-05 13:03:36 +03:00
|
|
|
pageInfoSelectAlias :: S.TableAlias
|
|
|
|
pageInfoSelectAlias = S.mkTableAlias "__page_info"
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-05 13:03:36 +03:00
|
|
|
pageInfoSelectAliasIdentifier :: TableIdentifier
|
|
|
|
pageInfoSelectAliasIdentifier = S.tableAliasToIdentifier pageInfoSelectAlias
|
|
|
|
|
|
|
|
cursorsSelectAlias :: S.TableAlias
|
|
|
|
cursorsSelectAlias = S.mkTableAlias "__cursors_select"
|
|
|
|
|
|
|
|
cursorsSelectAliasIdentifier :: TableIdentifier
|
|
|
|
cursorsSelectAliasIdentifier = S.tableAliasToIdentifier cursorsSelectAlias
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
encodeBase64 :: S.SQLExp -> S.SQLExp
|
|
|
|
encodeBase64 =
|
|
|
|
removeNewline . bytesToBase64Text . convertToBytes
|
|
|
|
where
|
|
|
|
convertToBytes e =
|
|
|
|
S.SEFnApp "convert_to" [e, S.SELit "UTF8"] Nothing
|
|
|
|
bytesToBase64Text e =
|
|
|
|
S.SEFnApp "encode" [e, S.SELit "base64"] Nothing
|
|
|
|
removeNewline e =
|
|
|
|
S.SEFnApp "regexp_replace" [e, S.SELit "\\n", S.SELit "", S.SELit "g"] Nothing
|
|
|
|
|
|
|
|
fromTableRowArgs ::
|
2022-10-11 13:42:15 +03:00
|
|
|
TableIdentifier -> FunctionArgsExpG (ArgumentExp S.SQLExp) -> S.FunctionArgs
|
2022-04-22 16:38:35 +03:00
|
|
|
fromTableRowArgs prefix = toFunctionArgs . fmap toSQLExp
|
|
|
|
where
|
|
|
|
toFunctionArgs (FunctionArgsExp positional named) =
|
|
|
|
S.FunctionArgs positional named
|
|
|
|
toSQLExp =
|
|
|
|
onArgumentExp
|
2022-10-11 13:42:15 +03:00
|
|
|
(S.SERowIdentifier (tableIdentifierToIdentifier baseTableIdentifier))
|
|
|
|
(S.mkQIdenExp baseTableIdentifier . Identifier)
|
|
|
|
baseTableIdentifier = mkBaseTableIdentifier prefix
|
2022-04-22 16:38:35 +03:00
|
|
|
|
2022-10-11 13:42:15 +03:00
|
|
|
selectFromToFromItem :: TableIdentifier -> SelectFrom ('Postgres pgKind) -> S.FromItem
|
2022-10-05 13:03:36 +03:00
|
|
|
selectFromToFromItem prefix = \case
|
2022-04-22 16:38:35 +03:00
|
|
|
FromTable tn -> S.FISimple tn Nothing
|
2022-10-05 13:03:36 +03:00
|
|
|
FromIdentifier i -> S.FIIdentifier $ TableIdentifier $ unFIIdentifier i
|
2022-04-22 16:38:35 +03:00
|
|
|
FromFunction qf args defListM ->
|
|
|
|
S.FIFunc $
|
2022-10-05 13:03:36 +03:00
|
|
|
S.FunctionExp qf (fromTableRowArgs prefix args) $
|
2022-07-22 18:27:42 +03:00
|
|
|
Just $
|
|
|
|
S.mkFunctionAlias
|
2022-10-05 13:03:36 +03:00
|
|
|
qf
|
2022-07-22 18:27:42 +03:00
|
|
|
(fmap (fmap (first S.toColumnAlias)) defListM)
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
-- | Converts a function name to an 'Identifier'.
|
|
|
|
--
|
|
|
|
-- If the schema name is public, it will just use its name, otherwise it will
|
|
|
|
-- prefix it by the schema name.
|
|
|
|
functionToIdentifier :: QualifiedFunction -> Identifier
|
|
|
|
functionToIdentifier = Identifier . qualifiedObjectToText
|
|
|
|
|
|
|
|
-- uses json_build_object to build a json object
|
|
|
|
withJsonBuildObj ::
|
2022-07-18 12:44:17 +03:00
|
|
|
FieldName -> [S.SQLExp] -> (S.ColumnAlias, S.SQLExp)
|
2022-04-22 16:38:35 +03:00
|
|
|
withJsonBuildObj parAls exps =
|
2022-07-18 12:44:17 +03:00
|
|
|
(S.toColumnAlias parAls, jsonRow)
|
2022-04-22 16:38:35 +03:00
|
|
|
where
|
|
|
|
jsonRow = S.applyJsonBuildObj exps
|
|
|
|
|
|
|
|
-- | Forces aggregation
|
|
|
|
withForceAggregation :: S.TypeAnn -> S.SQLExp -> S.SQLExp
|
|
|
|
withForceAggregation tyAnn e =
|
|
|
|
-- bool_or to force aggregation
|
|
|
|
S.SEFnApp "coalesce" [e, S.SETyAnn (S.SEUnsafe "bool_or('true')") tyAnn] Nothing
|