2018-12-12 15:58:39 +03:00
|
|
|
module Hasura.RQL.DML.Select.Internal
|
|
|
|
( mkSQLSelect
|
|
|
|
, mkAggSelect
|
2019-01-25 06:31:54 +03:00
|
|
|
, mkFuncSelectWith
|
2018-12-12 15:58:39 +03:00
|
|
|
, module Hasura.RQL.DML.Select.Types
|
|
|
|
)
|
|
|
|
where
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
import Control.Arrow ((&&&))
|
|
|
|
import Data.List (delete, sort)
|
|
|
|
import Instances.TH.Lift ()
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
import qualified Data.HashMap.Strict as HM
|
|
|
|
import qualified Data.List.NonEmpty as NE
|
|
|
|
import qualified Data.Text as T
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DML.Internal
|
2018-12-12 15:58:39 +03:00
|
|
|
import Hasura.RQL.DML.Select.Types
|
2018-10-31 15:51:20 +03:00
|
|
|
import Hasura.RQL.GBoolExp
|
|
|
|
import Hasura.RQL.Types
|
2018-12-12 15:58:39 +03:00
|
|
|
import Hasura.Server.Utils
|
|
|
|
import Hasura.SQL.Rewrite (prefixNumToAliases)
|
2018-10-31 15:51:20 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
import qualified Hasura.SQL.DML as S
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
-- Conversion of SelectQ happens in 2 Stages.
|
|
|
|
-- Stage 1 : Convert input query into an annotated AST
|
|
|
|
-- Stage 2 : Convert annotated AST to SQL Select
|
|
|
|
|
2018-11-16 15:40:23 +03:00
|
|
|
tableFromToFromItem :: TableFrom -> S.FromItem
|
|
|
|
tableFromToFromItem = \case
|
|
|
|
TableFrom tn Nothing -> S.FISimple tn Nothing
|
|
|
|
TableFrom _ (Just i) -> S.FIIden i
|
|
|
|
|
|
|
|
tableFromToQual :: TableFrom -> S.Qual
|
|
|
|
tableFromToQual = \case
|
|
|
|
TableFrom tn Nothing -> S.QualTable tn
|
|
|
|
TableFrom _ (Just i) -> S.QualIden i
|
|
|
|
|
2018-11-12 10:28:46 +03:00
|
|
|
aggFldToExp :: AggFlds -> S.SQLExp
|
|
|
|
aggFldToExp aggFlds = jsonRow
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
|
|
|
jsonRow = S.applyJsonBuildObj (concatMap aggToFlds aggFlds)
|
|
|
|
withAls fldName sqlExp = [S.SELit fldName, sqlExp]
|
2018-12-12 15:58:39 +03:00
|
|
|
aggToFlds (FieldName t, fld) = withAls t $ case fld of
|
2018-11-14 15:59:59 +03:00
|
|
|
AFCount cty -> S.SECount cty
|
|
|
|
AFOp aggOp -> aggOpToObj aggOp
|
|
|
|
AFExp e -> S.SELit e
|
|
|
|
|
|
|
|
aggOpToObj (AggOp op flds) =
|
2018-10-31 15:51:20 +03:00
|
|
|
S.applyJsonBuildObj $ concatMap (colFldsToExtr op) flds
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
colFldsToExtr op (FieldName t, PCFCol col) =
|
2018-10-31 15:51:20 +03:00
|
|
|
[ S.SELit t
|
2018-11-12 10:28:46 +03:00
|
|
|
, S.SEFnApp op [S.SEIden $ toIden col] Nothing
|
2018-10-31 15:51:20 +03:00
|
|
|
]
|
2018-12-12 15:58:39 +03:00
|
|
|
colFldsToExtr _ (FieldName t, PCFExp e) =
|
2018-10-31 15:51:20 +03:00
|
|
|
[ S.SELit t , S.SELit e]
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
arrNodeToSelect :: BaseNode -> [S.Extractor] -> S.BoolExp -> S.Select
|
|
|
|
arrNodeToSelect bn extrs joinCond =
|
2018-10-31 15:51:20 +03:00
|
|
|
S.mkSelect
|
2018-12-12 15:58:39 +03:00
|
|
|
{ S.selExtr = extrs
|
|
|
|
, S.selFrom = Just $ S.FromExp [selFrom]
|
|
|
|
}
|
|
|
|
where
|
|
|
|
selFrom = S.mkSelFromItem (baseNodeToSel joinCond bn) $ S.Alias $
|
|
|
|
_bnPrefix bn
|
|
|
|
|
2019-04-26 11:19:59 +03:00
|
|
|
asSingleRowExtr :: S.Alias -> S.SQLExp
|
|
|
|
asSingleRowExtr col =
|
|
|
|
S.SEFnApp "coalesce" [jsonAgg, S.SELit "null"] Nothing
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
|
|
|
jsonAgg = S.SEOpApp (S.SQLOp "->")
|
|
|
|
[ S.SEFnApp "json_agg" [S.SEIden $ toIden col] Nothing
|
|
|
|
, S.SEUnsafe "0"
|
|
|
|
]
|
|
|
|
|
2019-04-26 11:19:59 +03:00
|
|
|
withJsonAggExtr :: Maybe Int -> Maybe S.OrderByExp -> S.Alias -> S.SQLExp
|
|
|
|
withJsonAggExtr permLimitM ordBy alias =
|
|
|
|
maybe (mkSimpleJsonAgg rowIdenExp ordBy) withPermLimit permLimitM
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
2019-04-26 11:19:59 +03:00
|
|
|
rowIdenExp = S.SEIden $ S.getAlias alias
|
|
|
|
subSelAls = Iden "sub_query"
|
|
|
|
unnestTable = Iden "unnest_table"
|
|
|
|
|
|
|
|
mkSimpleJsonAgg rowExp ob =
|
|
|
|
let jsonAggExp = S.SEFnApp "json_agg" [rowExp] ob
|
|
|
|
in S.SEFnApp "coalesce" [jsonAggExp, S.SELit "[]"] Nothing
|
|
|
|
|
|
|
|
withPermLimit limit =
|
|
|
|
let subSelect = mkSubSelect limit
|
|
|
|
rowIden = S.mkQIdenExp subSelAls alias
|
|
|
|
extr = S.Extractor (mkSimpleJsonAgg rowIden newOrdBy) Nothing
|
|
|
|
fromExp = S.FromExp $ pure $
|
|
|
|
S.mkSelFromItem subSelect $ S.Alias subSelAls
|
|
|
|
in S.SESelect $ S.mkSelect { S.selExtr = pure extr
|
|
|
|
, S.selFrom = Just fromExp
|
|
|
|
}
|
|
|
|
|
|
|
|
mkSubSelect limit =
|
|
|
|
let jsonRowExtr = flip S.Extractor (Just alias) $
|
|
|
|
S.mkQIdenExp unnestTable alias
|
|
|
|
obExtrs = flip map newOBAliases $ \a ->
|
|
|
|
S.Extractor (S.mkQIdenExp unnestTable a) $ Just $ S.Alias a
|
|
|
|
in S.mkSelect { S.selExtr = jsonRowExtr : obExtrs
|
|
|
|
, S.selFrom = Just $ S.FromExp $ pure unnestFromItem
|
|
|
|
, S.selLimit = Just $ S.LimitExp $ S.intToSQLExp limit
|
|
|
|
, S.selOrderBy = newOrdBy
|
|
|
|
}
|
|
|
|
|
|
|
|
unnestFromItem =
|
|
|
|
let arrayAggItems = flip map (rowIdenExp : obCols) $
|
|
|
|
\s -> S.SEFnApp "array_agg" [s] Nothing
|
|
|
|
in S.FIUnnest arrayAggItems (S.Alias unnestTable) $
|
|
|
|
rowIdenExp : map S.SEIden newOBAliases
|
|
|
|
|
|
|
|
newOrdBy = bool (Just $ S.OrderByExp newOBItems) Nothing $ null newOBItems
|
|
|
|
|
|
|
|
(newOBItems, obCols, newOBAliases) = maybe ([], [], []) transformOrdBy ordBy
|
|
|
|
transformOrdBy (S.OrderByExp l) = unzip3 $
|
|
|
|
flip map (zip l [1..]) $ \(obItem, i::Int) ->
|
|
|
|
let iden = Iden $ "ob_col_" <> T.pack (show i)
|
|
|
|
in ( obItem{S.oColumn = S.SEIden iden}
|
|
|
|
, S.oColumn obItem
|
|
|
|
, iden
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
asJsonAggExtr
|
|
|
|
:: Bool -> S.Alias -> Maybe Int -> Maybe S.OrderByExp -> S.Extractor
|
|
|
|
asJsonAggExtr singleObj als permLimit ordByExpM =
|
|
|
|
flip S.Extractor (Just als) $
|
|
|
|
bool (withJsonAggExtr permLimit ordByExpM als) (asSingleRowExtr als) singleObj
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
-- array relationships are not grouped, so have to be prefixed by
|
|
|
|
-- parent's alias
|
2018-12-12 15:58:39 +03:00
|
|
|
mkUniqArrRelAls :: FieldName -> [FieldName] -> Iden
|
|
|
|
mkUniqArrRelAls parAls flds =
|
2018-10-31 15:51:20 +03:00
|
|
|
Iden $
|
2018-12-12 15:58:39 +03:00
|
|
|
getFieldNameTxt parAls <> "."
|
|
|
|
<> T.intercalate "." (map getFieldNameTxt flds)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkArrRelTableAls :: Iden -> FieldName -> [FieldName] -> Iden
|
|
|
|
mkArrRelTableAls pfx parAls flds =
|
2018-10-31 15:51:20 +03:00
|
|
|
pfx <> Iden ".ar." <> uniqArrRelAls
|
|
|
|
where
|
2018-12-12 15:58:39 +03:00
|
|
|
uniqArrRelAls = mkUniqArrRelAls parAls flds
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
mkObjRelTableAls :: Iden -> RelName -> Iden
|
|
|
|
mkObjRelTableAls pfx relName =
|
|
|
|
pfx <> Iden ".or." <> toIden relName
|
|
|
|
|
|
|
|
mkBaseTableAls :: Iden -> Iden
|
|
|
|
mkBaseTableAls pfx =
|
|
|
|
pfx <> Iden ".base"
|
|
|
|
|
|
|
|
mkBaseTableColAls :: Iden -> PGCol -> Iden
|
|
|
|
mkBaseTableColAls pfx pgCol =
|
|
|
|
pfx <> Iden ".pg." <> toIden pgCol
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
ordByFldName :: FieldName
|
|
|
|
ordByFldName = FieldName "order_by"
|
|
|
|
|
2018-10-31 15:51:20 +03:00
|
|
|
-- posttgres ignores anything beyond 63 chars for an iden
|
|
|
|
-- in this case, we'll need to use json_build_object function
|
|
|
|
-- json_build_object is slower than row_to_json hence it is only
|
|
|
|
-- used when needed
|
|
|
|
buildJsonObject
|
2019-03-01 14:45:04 +03:00
|
|
|
:: Iden -> FieldName -> ArrRelCtx -> Bool
|
2018-10-31 15:51:20 +03:00
|
|
|
-> [(FieldName, AnnFld)] -> (S.Alias, S.SQLExp)
|
2019-03-01 14:45:04 +03:00
|
|
|
buildJsonObject pfx parAls arrRelCtx strfyNum flds =
|
2018-10-31 15:51:20 +03:00
|
|
|
if any ( (> 63) . T.length . getFieldNameTxt . fst ) flds
|
|
|
|
then withJsonBuildObj parAls jsonBuildObjExps
|
|
|
|
else withRowToJSON parAls rowToJsonExtrs
|
|
|
|
where
|
|
|
|
jsonBuildObjExps = concatMap (toSQLFld withAlsExp) flds
|
|
|
|
rowToJsonExtrs = map (toSQLFld withAlsExtr) flds
|
|
|
|
|
|
|
|
withAlsExp fldName sqlExp =
|
|
|
|
[S.SELit $ getFieldNameTxt fldName, sqlExp]
|
2018-12-13 10:26:15 +03:00
|
|
|
|
2018-10-31 15:51:20 +03:00
|
|
|
withAlsExtr fldName sqlExp =
|
|
|
|
S.Extractor sqlExp $ Just $ S.toAlias fldName
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
toSQLFld :: (FieldName -> S.SQLExp -> f)
|
|
|
|
-> (FieldName, AnnFld) -> f
|
2018-10-31 15:51:20 +03:00
|
|
|
toSQLFld f (fldAls, fld) = f fldAls $ case fld of
|
2019-03-25 16:45:35 +03:00
|
|
|
FCol col args -> toSQLCol col args
|
2018-10-31 15:51:20 +03:00
|
|
|
FExp e -> S.SELit e
|
2018-12-12 15:58:39 +03:00
|
|
|
FObj objSel ->
|
|
|
|
let qual = mkObjRelTableAls pfx $ aarName objSel
|
2018-10-31 15:51:20 +03:00
|
|
|
in S.mkQIdenExp qual fldAls
|
2018-12-12 15:58:39 +03:00
|
|
|
FArr arrSel ->
|
|
|
|
let arrPfx = snd $ mkArrNodePfx pfx parAls arrRelCtx $
|
|
|
|
ANIField (fldAls, arrSel)
|
|
|
|
in S.mkQIdenExp arrPfx fldAls
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2019-03-25 16:45:35 +03:00
|
|
|
toSQLCol :: PGColInfo -> Maybe ColOp -> S.SQLExp
|
|
|
|
toSQLCol col colOpM =
|
|
|
|
toJSONableExp strfyNum (pgiType col) $ case colOpM of
|
|
|
|
Nothing -> colNameExp
|
|
|
|
Just (ColOp op cExp) -> S.mkSQLOpExp op colNameExp cExp
|
|
|
|
where
|
|
|
|
colNameExp = S.mkQIdenExp (mkBaseTableAls pfx) $ pgiName col
|
|
|
|
|
2018-10-31 15:51:20 +03:00
|
|
|
-- uses row_to_json to build a json object
|
|
|
|
withRowToJSON
|
|
|
|
:: FieldName -> [S.Extractor] -> (S.Alias, S.SQLExp)
|
|
|
|
withRowToJSON parAls extrs =
|
|
|
|
(S.toAlias parAls, jsonRow)
|
|
|
|
where
|
|
|
|
jsonRow = S.applyRowToJson extrs
|
|
|
|
|
|
|
|
-- uses json_build_object to build a json object
|
|
|
|
withJsonBuildObj
|
|
|
|
:: FieldName -> [S.SQLExp] -> (S.Alias, S.SQLExp)
|
|
|
|
withJsonBuildObj parAls exps =
|
|
|
|
(S.toAlias parAls, jsonRow)
|
|
|
|
where
|
|
|
|
jsonRow = S.applyJsonBuildObj exps
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkAggObFld :: AnnAggOrdBy -> FieldName
|
|
|
|
mkAggObFld = \case
|
|
|
|
AAOCount -> FieldName "count"
|
|
|
|
AAOOp op col -> FieldName $ op <> "." <> getPGColTxt col
|
|
|
|
|
|
|
|
mkAggObExtrAndFlds :: AnnAggOrdBy -> (S.Extractor, AggFlds)
|
|
|
|
mkAggObExtrAndFlds annAggOb = case annAggOb of
|
|
|
|
AAOCount ->
|
|
|
|
( S.Extractor S.countStar als
|
|
|
|
, [(FieldName "count", AFCount S.CTStar)]
|
|
|
|
)
|
|
|
|
AAOOp op pgCol ->
|
|
|
|
( S.Extractor (S.SEFnApp op [S.SEIden $ toIden pgCol] Nothing) als
|
|
|
|
, [(FieldName op, AFOp $ AggOp op [(fromPGCol pgCol, PCFCol pgCol)])]
|
|
|
|
)
|
|
|
|
where
|
|
|
|
als = Just $ S.toAlias $ mkAggObFld annAggOb
|
|
|
|
|
2018-10-31 15:51:20 +03:00
|
|
|
processAnnOrderByItem
|
|
|
|
:: Iden
|
2018-12-12 15:58:39 +03:00
|
|
|
-> FieldName
|
|
|
|
-> ArrRelCtx
|
2019-03-01 14:45:04 +03:00
|
|
|
-> Bool
|
2018-10-31 15:51:20 +03:00
|
|
|
-> AnnOrderByItem
|
|
|
|
-- the extractors which will select the needed columns
|
|
|
|
-> ( (S.Alias, S.SQLExp)
|
|
|
|
-- the sql order by item that is attached to the final select
|
|
|
|
, S.OrderByItem
|
2018-12-12 15:58:39 +03:00
|
|
|
-- extra nodes for order by
|
|
|
|
, OrderByNode
|
2018-10-31 15:51:20 +03:00
|
|
|
)
|
2019-03-01 14:45:04 +03:00
|
|
|
processAnnOrderByItem pfx parAls arrRelCtx strfyNum obItemG =
|
2018-10-31 15:51:20 +03:00
|
|
|
( (obColAls, obColExp)
|
|
|
|
, sqlOrdByItem
|
|
|
|
, relNodeM
|
|
|
|
)
|
|
|
|
where
|
2019-03-01 14:45:04 +03:00
|
|
|
OrderByItemG obTyM annObCol obNullsM = obItemG
|
|
|
|
((obColAls, obColExp), relNodeM) =
|
|
|
|
processAnnOrderByCol pfx parAls arrRelCtx strfyNum annObCol
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
sqlOrdByItem =
|
2018-12-13 10:26:15 +03:00
|
|
|
S.OrderByItem (S.SEIden $ toIden obColAls)
|
|
|
|
(unOrderType <$> obTyM) (unNullsOrder <$> obNullsM)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
processAnnOrderByCol
|
|
|
|
:: Iden
|
2018-12-12 15:58:39 +03:00
|
|
|
-> FieldName
|
|
|
|
-> ArrRelCtx
|
2019-03-01 14:45:04 +03:00
|
|
|
-> Bool
|
2018-10-31 15:51:20 +03:00
|
|
|
-> AnnObCol
|
|
|
|
-- the extractors which will select the needed columns
|
|
|
|
-> ( (S.Alias, S.SQLExp)
|
2018-12-12 15:58:39 +03:00
|
|
|
-- extra nodes for order by
|
|
|
|
, OrderByNode
|
2018-10-31 15:51:20 +03:00
|
|
|
)
|
2019-03-01 14:45:04 +03:00
|
|
|
processAnnOrderByCol pfx parAls arrRelCtx strfyNum = \case
|
2018-10-31 15:51:20 +03:00
|
|
|
AOCPG colInfo ->
|
|
|
|
let
|
|
|
|
qualCol = S.mkQIdenExp (mkBaseTableAls pfx) (toIden $ pgiName colInfo)
|
|
|
|
obColAls = mkBaseTableColAls pfx $ pgiName colInfo
|
|
|
|
in ( (S.Alias obColAls, qualCol)
|
2018-12-12 15:58:39 +03:00
|
|
|
, OBNNothing
|
2018-10-31 15:51:20 +03:00
|
|
|
)
|
|
|
|
-- "pfx.or.relname"."pfx.ob.or.relname.rest" AS "pfx.ob.or.relname.rest"
|
2018-12-12 15:58:39 +03:00
|
|
|
AOCObj (RelInfo rn _ colMapping relTab _) relFltr rest ->
|
2018-10-31 15:51:20 +03:00
|
|
|
let relPfx = mkObjRelTableAls pfx rn
|
2018-12-12 15:58:39 +03:00
|
|
|
((nesAls, nesCol), ordByNode) =
|
2019-03-01 14:45:04 +03:00
|
|
|
processAnnOrderByCol relPfx ordByFldName emptyArrRelCtx strfyNum rest
|
2018-12-12 15:58:39 +03:00
|
|
|
(objNodeM, arrNodeM) = case ordByNode of
|
|
|
|
OBNNothing -> (Nothing, Nothing)
|
|
|
|
OBNObjNode name node -> (Just (name, node), Nothing)
|
|
|
|
OBNArrNode als node -> (Nothing, Just (als, node))
|
2018-10-31 15:51:20 +03:00
|
|
|
qualCol = S.mkQIdenExp relPfx nesAls
|
|
|
|
relBaseNode =
|
2018-11-23 04:53:56 +03:00
|
|
|
BaseNode relPfx Nothing (S.FISimple relTab Nothing)
|
2018-11-16 15:40:23 +03:00
|
|
|
(toSQLBoolExp (S.QualTable relTab) relFltr)
|
2018-10-31 15:51:20 +03:00
|
|
|
Nothing Nothing Nothing
|
|
|
|
(HM.singleton nesAls nesCol)
|
2018-12-12 15:58:39 +03:00
|
|
|
(maybe HM.empty (uncurry HM.singleton) objNodeM)
|
|
|
|
(maybe HM.empty (uncurry HM.singleton) arrNodeM)
|
|
|
|
relNode = ObjNode colMapping relBaseNode
|
2018-10-31 15:51:20 +03:00
|
|
|
in ( (nesAls, qualCol)
|
2018-12-12 15:58:39 +03:00
|
|
|
, OBNObjNode rn relNode
|
|
|
|
)
|
|
|
|
AOCAgg (RelInfo rn _ colMapping relTab _ ) relFltr annAggOb ->
|
|
|
|
let (arrAls, arrPfx) =
|
|
|
|
mkArrNodePfx pfx parAls arrRelCtx $ ANIAggOrdBy rn
|
|
|
|
fldName = mkAggObFld annAggOb
|
|
|
|
qOrdBy = S.mkQIdenExp arrPfx $ toIden fldName
|
|
|
|
tabFrom = TableFrom relTab Nothing
|
|
|
|
tabPerm = TablePerm relFltr Nothing
|
|
|
|
(extr, arrFlds) = mkAggObExtrAndFlds annAggOb
|
|
|
|
selFld = TAFAgg arrFlds
|
2019-03-01 14:45:04 +03:00
|
|
|
bn = mkBaseNode arrPfx fldName selFld tabFrom tabPerm noTableArgs strfyNum
|
2018-12-12 15:58:39 +03:00
|
|
|
aggNode = ArrNode [extr] colMapping $ mergeBaseNodes bn $
|
|
|
|
mkEmptyBaseNode arrPfx tabFrom
|
|
|
|
obAls = arrPfx <> Iden "." <> toIden fldName
|
|
|
|
in ( (S.Alias obAls, qOrdBy)
|
|
|
|
, OBNArrNode arrAls aggNode
|
2018-10-31 15:51:20 +03:00
|
|
|
)
|
|
|
|
|
2018-11-23 04:53:56 +03:00
|
|
|
processDistinctOnCol
|
|
|
|
:: Iden
|
|
|
|
-> NE.NonEmpty PGCol
|
|
|
|
-> ( S.DistinctExpr
|
|
|
|
-- additional column extractors
|
|
|
|
, [(S.Alias, S.SQLExp)]
|
|
|
|
)
|
|
|
|
processDistinctOnCol pfx neCols = (distOnExp, colExtrs)
|
|
|
|
where
|
|
|
|
cols = toList neCols
|
|
|
|
distOnExp = S.DistinctOn $ map (S.SEIden . toIden . mkQColAls) cols
|
|
|
|
mkQCol c = S.mkQIdenExp (mkBaseTableAls pfx) $ toIden c
|
|
|
|
mkQColAls = S.Alias . mkBaseTableColAls pfx
|
|
|
|
colExtrs = flip map cols $ mkQColAls &&& mkQCol
|
|
|
|
|
|
|
|
|
2018-10-31 15:51:20 +03:00
|
|
|
mkEmptyBaseNode :: Iden -> TableFrom -> BaseNode
|
|
|
|
mkEmptyBaseNode pfx tableFrom =
|
2018-12-12 15:58:39 +03:00
|
|
|
BaseNode pfx Nothing fromItem (S.BELit True) Nothing Nothing
|
|
|
|
Nothing selOne HM.empty HM.empty
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
|
|
|
selOne = HM.singleton (S.Alias $ pfx <> Iden "__one") (S.SEUnsafe "1")
|
2018-11-16 15:40:23 +03:00
|
|
|
fromItem = tableFromToFromItem tableFrom
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
aggSelToArrNode :: Iden -> FieldName -> ArrRelAgg -> ArrNode
|
|
|
|
aggSelToArrNode pfx als aggSel =
|
|
|
|
ArrNode [extr] colMapping mergedBN
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
2018-12-12 15:58:39 +03:00
|
|
|
AnnRelG _ colMapping annSel = aggSel
|
2019-03-01 14:45:04 +03:00
|
|
|
AnnSelG aggFlds tabFrm tabPerm tabArgs strfyNum = annSel
|
2018-10-31 15:51:20 +03:00
|
|
|
fldAls = S.Alias $ toIden als
|
|
|
|
|
|
|
|
extr = flip S.Extractor (Just fldAls) $ S.applyJsonBuildObj $
|
|
|
|
concatMap selFldToExtr aggFlds
|
|
|
|
|
2019-04-26 11:19:59 +03:00
|
|
|
permLimit = _tpLimit tabPerm
|
2018-10-31 15:51:20 +03:00
|
|
|
ordBy = _bnOrderBy mergedBN
|
|
|
|
|
|
|
|
allBNs = map mkAggBaseNode aggFlds
|
|
|
|
emptyBN = mkEmptyBaseNode pfx tabFrm
|
|
|
|
mergedBN = foldr mergeBaseNodes emptyBN allBNs
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkAggBaseNode (fn, selFld) =
|
2019-03-01 14:45:04 +03:00
|
|
|
mkBaseNode pfx fn selFld tabFrm tabPerm tabArgs strfyNum
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
selFldToExtr (FieldName t, fld) = (:) (S.SELit t) $ pure $ case fld of
|
2018-11-12 10:28:46 +03:00
|
|
|
TAFAgg flds -> aggFldToExp flds
|
2019-04-26 11:19:59 +03:00
|
|
|
TAFNodes _ -> withJsonAggExtr permLimit ordBy $ S.Alias $ Iden t
|
|
|
|
TAFExp e ->
|
2018-10-31 15:51:20 +03:00
|
|
|
-- bool_or to force aggregation
|
|
|
|
S.SEFnApp "coalesce"
|
|
|
|
[ S.SELit e , S.SEUnsafe "bool_or('true')::text"] Nothing
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkArrNodePfx
|
|
|
|
:: Iden
|
|
|
|
-> FieldName
|
|
|
|
-> ArrRelCtx
|
|
|
|
-> ArrNodeItem
|
|
|
|
-> (S.Alias, Iden)
|
|
|
|
mkArrNodePfx pfx parAls (ArrRelCtx arrFlds obRels) = \case
|
|
|
|
ANIField aggFld@(fld, annArrSel) ->
|
|
|
|
let (rn, tabArgs) = fetchRNAndTArgs annArrSel
|
|
|
|
similarFlds = getSimilarAggFlds rn tabArgs $ delete aggFld
|
|
|
|
similarOrdByFound = rn `elem` obRels && tabArgs == noTableArgs
|
|
|
|
extraOrdByFlds = bool [] [ordByFldName] similarOrdByFound
|
|
|
|
sortedFlds = sort $ fld : (similarFlds <> extraOrdByFlds)
|
|
|
|
in ( S.Alias $ mkUniqArrRelAls parAls sortedFlds
|
|
|
|
, mkArrRelTableAls pfx parAls sortedFlds
|
|
|
|
)
|
|
|
|
ANIAggOrdBy rn ->
|
|
|
|
let similarFlds = getSimilarAggFlds rn noTableArgs id
|
|
|
|
sortedFlds = sort $ ordByFldName:similarFlds
|
|
|
|
in ( S.Alias $ mkUniqArrRelAls parAls sortedFlds
|
|
|
|
, mkArrRelTableAls pfx parAls sortedFlds
|
|
|
|
)
|
|
|
|
where
|
|
|
|
getSimilarAggFlds rn tabArgs f = map fst $
|
|
|
|
flip filter (f arrFlds) $ \(_, annArrSel) ->
|
|
|
|
let (lrn, lTabArgs) = fetchRNAndTArgs annArrSel
|
|
|
|
in (lrn == rn) && (lTabArgs == tabArgs)
|
|
|
|
|
|
|
|
fetchRNAndTArgs (ASSimple (AnnRelG rn _ annSel)) =
|
|
|
|
(rn, _asnArgs annSel)
|
|
|
|
fetchRNAndTArgs (ASAgg (AnnRelG rn _ annSel)) =
|
|
|
|
(rn, _asnArgs annSel)
|
|
|
|
|
|
|
|
fetchOrdByAggRels
|
|
|
|
:: Maybe (NE.NonEmpty AnnOrderByItem)
|
|
|
|
-> [RelName]
|
|
|
|
fetchOrdByAggRels orderByM = fromMaybe [] relNamesM
|
|
|
|
where
|
|
|
|
relNamesM =
|
|
|
|
mapMaybe (fetchAggOrdByRels . obiColumn) . toList <$> orderByM
|
|
|
|
|
|
|
|
fetchAggOrdByRels (AOCAgg ri _ _) = Just $ riName ri
|
|
|
|
fetchAggOrdByRels _ = Nothing
|
|
|
|
|
|
|
|
mkOrdByItems
|
|
|
|
:: Iden -> FieldName
|
|
|
|
-> Maybe (NE.NonEmpty AnnOrderByItem)
|
2019-03-01 14:45:04 +03:00
|
|
|
-> Bool
|
2018-12-12 15:58:39 +03:00
|
|
|
-> ArrRelCtx
|
|
|
|
-- extractors
|
|
|
|
-> ( [(S.Alias, S.SQLExp)]
|
|
|
|
-- object relation nodes
|
|
|
|
, HM.HashMap RelName ObjNode
|
|
|
|
-- array relation aggregate nodes
|
|
|
|
, HM.HashMap S.Alias ArrNode
|
|
|
|
-- final order by expression
|
|
|
|
, Maybe S.OrderByExp
|
|
|
|
)
|
2019-03-01 14:45:04 +03:00
|
|
|
mkOrdByItems pfx fldAls orderByM strfyNum arrRelCtx =
|
2018-12-12 15:58:39 +03:00
|
|
|
(obExtrs, ordByObjsMap, ordByArrsMap, ordByExpM)
|
|
|
|
where
|
2019-03-01 14:45:04 +03:00
|
|
|
procAnnOrdBy' = processAnnOrderByItem pfx fldAls arrRelCtx strfyNum
|
2018-12-12 15:58:39 +03:00
|
|
|
procOrdByM =
|
|
|
|
unzip3 . map procAnnOrdBy' . toList <$> orderByM
|
|
|
|
|
|
|
|
obExtrs = maybe [] _1 procOrdByM
|
|
|
|
ordByExpM = S.OrderByExp . _2 <$> procOrdByM
|
|
|
|
|
|
|
|
ordByObjs = mapMaybe getOrdByRelNode $ maybe [] _3 procOrdByM
|
|
|
|
ordByObjsMap = HM.fromListWith mergeObjNodes ordByObjs
|
|
|
|
|
|
|
|
ordByAggArrs = mapMaybe getOrdByAggNode $ maybe [] _3 procOrdByM
|
|
|
|
ordByArrsMap = HM.fromListWith mergeArrNodes ordByAggArrs
|
|
|
|
|
|
|
|
getOrdByRelNode (OBNObjNode name node) = Just (name, node)
|
|
|
|
getOrdByRelNode _ = Nothing
|
|
|
|
|
|
|
|
getOrdByAggNode (OBNArrNode als node) = Just (als, node)
|
|
|
|
getOrdByAggNode _ = Nothing
|
|
|
|
|
2018-10-31 15:51:20 +03:00
|
|
|
mkBaseNode
|
|
|
|
:: Iden -> FieldName -> TableAggFld -> TableFrom
|
2019-03-01 14:45:04 +03:00
|
|
|
-> TablePerm -> TableArgs -> Bool -> BaseNode
|
|
|
|
mkBaseNode pfx fldAls annSelFlds tableFrom tablePerm tableArgs strfyNum =
|
2019-04-26 11:19:59 +03:00
|
|
|
BaseNode pfx distExprM fromItem finalWhere ordByExpM limitM offsetM
|
2018-12-12 15:58:39 +03:00
|
|
|
allExtrs allObjsWithOb allArrsWithOb
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
2019-04-26 11:19:59 +03:00
|
|
|
fltr = _tpFilter tablePerm
|
2018-11-23 04:53:56 +03:00
|
|
|
TableArgs whereM orderByM limitM offsetM distM = tableArgs
|
2018-12-12 15:58:39 +03:00
|
|
|
aggOrdByRelNames = fetchOrdByAggRels orderByM
|
|
|
|
|
|
|
|
(allExtrs, allObjsWithOb, allArrsWithOb, ordByExpM) =
|
|
|
|
case annSelFlds of
|
|
|
|
TAFNodes flds ->
|
|
|
|
let arrFlds = mapMaybe getAnnArr flds
|
|
|
|
arrRelCtx = mkArrRelCtx arrFlds
|
2019-03-01 14:45:04 +03:00
|
|
|
selExtr = buildJsonObject pfx fldAls arrRelCtx strfyNum flds
|
2018-12-12 15:58:39 +03:00
|
|
|
-- all object relationships
|
|
|
|
objNodes = HM.fromListWith mergeObjNodes $
|
|
|
|
map mkObjItem (mapMaybe getAnnObj flds)
|
|
|
|
-- all array items (array relationships + aggregates)
|
|
|
|
arrNodes = HM.fromListWith mergeArrNodes $
|
|
|
|
map (mkArrItem arrRelCtx) arrFlds
|
|
|
|
|
|
|
|
(obExtrs, ordByObjs, ordByArrs, obeM)
|
|
|
|
= mkOrdByItems' arrRelCtx
|
|
|
|
allObjs = HM.unionWith mergeObjNodes objNodes ordByObjs
|
|
|
|
allArrs = HM.unionWith mergeArrNodes arrNodes ordByArrs
|
|
|
|
|
|
|
|
in ( HM.fromList $ selExtr:obExtrs <> distExtrs
|
|
|
|
, allObjs
|
|
|
|
, allArrs
|
|
|
|
, obeM
|
|
|
|
)
|
|
|
|
TAFAgg tabAggs ->
|
|
|
|
let extrs = concatMap (fetchExtrFromAggFld . snd) tabAggs
|
|
|
|
(obExtrs, ordByObjs, ordByArrs, obeM)
|
|
|
|
= mkOrdByItems' emptyArrRelCtx
|
|
|
|
in ( HM.fromList $ extrs <> obExtrs <> distExtrs
|
|
|
|
, ordByObjs
|
|
|
|
, ordByArrs
|
|
|
|
, obeM
|
|
|
|
)
|
|
|
|
TAFExp _ ->
|
|
|
|
let (obExtrs, ordByObjs, ordByArrs, obeM)
|
|
|
|
= mkOrdByItems' emptyArrRelCtx
|
|
|
|
in (HM.fromList obExtrs, ordByObjs, ordByArrs, obeM)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-11-14 15:59:59 +03:00
|
|
|
fetchExtrFromAggFld (AFCount cty) = countTyToExps cty
|
|
|
|
fetchExtrFromAggFld (AFOp aggOp) = aggOpToExps aggOp
|
|
|
|
fetchExtrFromAggFld (AFExp _) = []
|
|
|
|
|
|
|
|
countTyToExps S.CTStar = []
|
|
|
|
countTyToExps (S.CTSimple cols) = colsToExps cols
|
|
|
|
countTyToExps (S.CTDistinct cols) = colsToExps cols
|
|
|
|
|
|
|
|
colsToExps = mapMaybe (mkColExp . PCFCol)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-11-14 15:59:59 +03:00
|
|
|
aggOpToExps = mapMaybe (mkColExp . snd) . _aoFlds
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
mkColExp (PCFCol c) =
|
|
|
|
let qualCol = S.mkQIdenExp (mkBaseTableAls pfx) (toIden c)
|
2018-11-12 10:28:46 +03:00
|
|
|
colAls = toIden c
|
2018-10-31 15:51:20 +03:00
|
|
|
in Just (S.Alias colAls, qualCol)
|
|
|
|
mkColExp _ = Nothing
|
|
|
|
|
2018-11-16 15:40:23 +03:00
|
|
|
finalWhere =
|
|
|
|
toSQLBoolExp tableQual $ maybe fltr (andAnnBoolExps fltr) whereM
|
|
|
|
fromItem = tableFromToFromItem tableFrom
|
|
|
|
tableQual = tableFromToQual tableFrom
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkArrRelCtx arrSels = ArrRelCtx arrSels aggOrdByRelNames
|
|
|
|
|
2019-03-01 14:45:04 +03:00
|
|
|
mkOrdByItems' = mkOrdByItems pfx fldAls orderByM strfyNum
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-11-23 04:53:56 +03:00
|
|
|
distItemsM = processDistinctOnCol pfx <$> distM
|
|
|
|
distExprM = fst <$> distItemsM
|
|
|
|
distExtrs = fromMaybe [] (snd <$> distItemsM)
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
-- process an object relationship
|
|
|
|
mkObjItem (fld, objSel) =
|
|
|
|
let relName = aarName objSel
|
|
|
|
objNodePfx = mkObjRelTableAls pfx $ aarName objSel
|
|
|
|
objNode = mkObjNode objNodePfx (fld, objSel)
|
|
|
|
in (relName, objNode)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
-- process an array/array-aggregate item
|
|
|
|
mkArrItem arrRelCtx (fld, arrSel) =
|
|
|
|
let (arrAls, arrPfx) = mkArrNodePfx pfx fldAls arrRelCtx $
|
|
|
|
ANIField (fld, arrSel)
|
|
|
|
arrNode = mkArrNode arrPfx (fld, arrSel)
|
|
|
|
in (arrAls, arrNode)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
getAnnObj (f, annFld) = case annFld of
|
|
|
|
FObj ob -> Just (f, ob)
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
getAnnArr (f, annFld) = case annFld of
|
|
|
|
FArr ar -> Just (f, ar)
|
|
|
|
_ -> Nothing
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
annSelToBaseNode :: Iden -> FieldName -> AnnSimpleSel -> BaseNode
|
2018-10-31 15:51:20 +03:00
|
|
|
annSelToBaseNode pfx fldAls annSel =
|
2019-03-01 14:45:04 +03:00
|
|
|
mkBaseNode pfx fldAls (TAFNodes selFlds) tabFrm tabPerm tabArgs strfyNum
|
2018-10-31 15:51:20 +03:00
|
|
|
where
|
2019-03-01 14:45:04 +03:00
|
|
|
AnnSelG selFlds tabFrm tabPerm tabArgs strfyNum = annSel
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkObjNode :: Iden -> (FieldName, ObjSel) -> ObjNode
|
|
|
|
mkObjNode pfx (fldName, AnnRelG _ rMapn rAnnSel) =
|
|
|
|
ObjNode rMapn $ annSelToBaseNode pfx fldName rAnnSel
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkArrNode :: Iden -> (FieldName, ArrSel) -> ArrNode
|
|
|
|
mkArrNode pfx (fldName, annArrSel) = case annArrSel of
|
|
|
|
ASSimple annArrRel ->
|
|
|
|
let bn = annSelToBaseNode pfx fldName $ aarAnnSel annArrRel
|
2019-04-26 11:19:59 +03:00
|
|
|
permLimit = getPermLimit $ aarAnnSel annArrRel
|
|
|
|
extr = asJsonAggExtr False (S.toAlias fldName) permLimit $
|
2018-12-12 15:58:39 +03:00
|
|
|
_bnOrderBy bn
|
|
|
|
in ArrNode [extr] (aarMapping annArrRel) bn
|
|
|
|
|
|
|
|
ASAgg annAggSel -> aggSelToArrNode pfx fldName annAggSel
|
2018-10-31 15:51:20 +03:00
|
|
|
|
|
|
|
injectJoinCond :: S.BoolExp -- ^ Join condition
|
|
|
|
-> S.BoolExp -- ^ Where condition
|
|
|
|
-> S.WhereFrag -- ^ New where frag
|
|
|
|
injectJoinCond joinCond whereCond =
|
|
|
|
S.WhereFrag $ S.simplifyBoolExp $ S.BEBin S.AndOp joinCond whereCond
|
|
|
|
|
|
|
|
mkJoinCond :: S.Alias -> [(PGCol, PGCol)] -> S.BoolExp
|
|
|
|
mkJoinCond baseTableAls colMapn =
|
|
|
|
foldl' (S.BEBin S.AndOp) (S.BELit True) $ flip map colMapn $
|
|
|
|
\(lCol, rCol) ->
|
|
|
|
S.BECompare S.SEQ (S.mkQIdenExp baseTableAls lCol) (S.mkSIdenExp rCol)
|
|
|
|
|
|
|
|
baseNodeToSel :: S.BoolExp -> BaseNode -> S.Select
|
2018-11-23 04:53:56 +03:00
|
|
|
baseNodeToSel joinCond baseNode =
|
2018-10-31 15:51:20 +03:00
|
|
|
S.mkSelect
|
|
|
|
{ S.selExtr = [S.Extractor e $ Just a | (a, e) <- HM.toList extrs]
|
|
|
|
, S.selFrom = Just $ S.FromExp [joinedFrom]
|
|
|
|
, S.selOrderBy = ordByM
|
|
|
|
, S.selLimit = S.LimitExp . S.intToSQLExp <$> limitM
|
|
|
|
, S.selOffset = S.OffsetExp <$> offsetM
|
2018-11-23 04:53:56 +03:00
|
|
|
, S.selDistinct = dExp
|
2018-10-31 15:51:20 +03:00
|
|
|
}
|
|
|
|
where
|
2018-11-23 04:53:56 +03:00
|
|
|
BaseNode pfx dExp fromItem whr ordByM limitM
|
2018-12-12 15:58:39 +03:00
|
|
|
offsetM extrs objRels arrRels
|
2018-11-23 04:53:56 +03:00
|
|
|
= baseNode
|
2018-10-31 15:51:20 +03:00
|
|
|
-- this is the table which is aliased as "pfx.base"
|
|
|
|
baseSel = S.mkSelect
|
|
|
|
{ S.selExtr = [S.Extractor S.SEStar Nothing]
|
|
|
|
, S.selFrom = Just $ S.FromExp [fromItem]
|
|
|
|
, S.selWhere = Just $ injectJoinCond joinCond whr
|
|
|
|
}
|
|
|
|
baseSelAls = S.Alias $ mkBaseTableAls pfx
|
|
|
|
baseFromItem = S.FISelect (S.Lateral False) baseSel baseSelAls
|
|
|
|
|
|
|
|
-- function to create a joined from item from two from items
|
|
|
|
leftOuterJoin current new =
|
|
|
|
S.FIJoin $ S.JoinExpr current S.LeftOuter new $
|
|
|
|
S.JoinOn $ S.BELit True
|
|
|
|
|
|
|
|
-- this is the from eexp for the final select
|
|
|
|
joinedFrom :: S.FromItem
|
|
|
|
joinedFrom = foldl' leftOuterJoin baseFromItem $
|
2018-12-12 15:58:39 +03:00
|
|
|
map objNodeToFromItem (HM.elems objRels) <>
|
|
|
|
map arrNodeToFromItem (HM.elems arrRels)
|
2018-10-31 15:51:20 +03:00
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
objNodeToFromItem :: ObjNode -> S.FromItem
|
|
|
|
objNodeToFromItem (ObjNode relMapn relBaseNode) =
|
2018-10-31 15:51:20 +03:00
|
|
|
let als = S.Alias $ _bnPrefix relBaseNode
|
|
|
|
sel = baseNodeToSel (mkJoinCond baseSelAls relMapn) relBaseNode
|
|
|
|
in S.mkLateralFromItem sel als
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
arrNodeToFromItem :: ArrNode -> S.FromItem
|
|
|
|
arrNodeToFromItem (ArrNode es colMapn bn) =
|
|
|
|
let sel = arrNodeToSelect bn es (mkJoinCond baseSelAls colMapn)
|
|
|
|
als = S.Alias $ _bnPrefix bn
|
2018-10-31 15:51:20 +03:00
|
|
|
in S.mkLateralFromItem sel als
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
mkAggSelect :: AnnAggSel -> S.Select
|
|
|
|
mkAggSelect annAggSel =
|
|
|
|
prefixNumToAliases $ arrNodeToSelect bn extr $ S.BELit True
|
|
|
|
where
|
|
|
|
aggSel = AnnRelG (RelName "root") [] annAggSel
|
|
|
|
ArrNode extr _ bn =
|
|
|
|
aggSelToArrNode (Iden "root") (FieldName "root") aggSel
|
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
mkSQLSelect :: Bool -> AnnSimpleSel -> S.Select
|
2018-12-12 15:58:39 +03:00
|
|
|
mkSQLSelect isSingleObject annSel =
|
|
|
|
prefixNumToAliases $ arrNodeToSelect baseNode extrs $ S.BELit True
|
|
|
|
where
|
2019-04-26 11:19:59 +03:00
|
|
|
permLimit = getPermLimit annSel
|
|
|
|
extrs = pure $ asJsonAggExtr isSingleObject rootFldAls permLimit
|
|
|
|
$ _bnOrderBy baseNode
|
2018-12-12 15:58:39 +03:00
|
|
|
baseNode = annSelToBaseNode (toIden rootFldName) rootFldName annSel
|
|
|
|
rootFldName = FieldName "root"
|
|
|
|
rootFldAls = S.Alias $ toIden rootFldName
|
2019-01-25 06:31:54 +03:00
|
|
|
|
|
|
|
mkFuncSelectWith
|
2019-04-17 12:48:41 +03:00
|
|
|
:: (AnnSelG a S.SQLExp -> S.Select)
|
|
|
|
-> AnnFnSelG (AnnSelG a S.SQLExp) S.SQLExp
|
|
|
|
-> S.SelectWith
|
|
|
|
mkFuncSelectWith f annFn =
|
|
|
|
S.SelectWith [(funcAls, S.CTESelect funcSel)] $
|
|
|
|
-- we'll need to modify the table from of the underlying
|
|
|
|
-- select to the alias of the select from function
|
|
|
|
f annSel { _asnFrom = newTabFrom }
|
2019-01-25 06:31:54 +03:00
|
|
|
where
|
2019-04-17 12:48:41 +03:00
|
|
|
AnnFnSel qf fnArgs annSel = annFn
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
-- SELECT * FROM function_name(args)
|
|
|
|
funcSel = S.mkSelect { S.selFrom = Just $ S.FromExp [frmItem]
|
|
|
|
, S.selExtr = [S.Extractor S.SEStar Nothing]
|
|
|
|
}
|
2019-04-17 12:48:41 +03:00
|
|
|
frmItem = S.mkFuncFromItem qf fnArgs
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
newTabFrom = (_asnFrom annSel) {_tfIden = Just $ toIden funcAls}
|
2019-01-25 06:31:54 +03:00
|
|
|
|
|
|
|
QualifiedObject sn fn = qf
|
|
|
|
funcAls = S.Alias $ Iden $
|
|
|
|
getSchemaTxt sn <> "_" <> getFunctionTxt fn <> "__result"
|