Merge pull request #62 from hasura/issue-61

use row expressions instead of json_build_object, closes #61
This commit is contained in:
Vamshi Surabhi 2018-07-09 15:09:44 +05:30 committed by GitHub
commit 330bbbe495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 36 deletions

View File

@ -233,15 +233,11 @@ mkColExtr (c, pct) =
mkColExtrAl (Just c) (c, pct)
mkColExtrAl :: (IsIden a) => Maybe a -> (PGCol, PGColType) -> S.Extractor
mkColExtrAl alM colInfo =
S.mkAliasedExtrFromExp (mkColExp colInfo) alM
mkColExp :: (PGCol, PGColType) -> S.SQLExp
mkColExp (c, pct) =
mkColExtrAl alM (c, pct) =
if pct == PGGeometry || pct == PGGeography
then
(S.SEFnApp "ST_AsGeoJSON" [S.mkSIdenExp c] Nothing) `S.SETyAnn` "json"
else S.mkSIdenExp c
then S.mkAliasedExtrFromExp
((S.SEFnApp "ST_AsGeoJSON" [S.mkSIdenExp c] Nothing) `S.SETyAnn` "json") alM
else S.mkAliasedExtr c alM
-- validate headers
validateHeaders :: (P1C m) => [T.Text] -> m ()

View File

@ -5,8 +5,8 @@
module Hasura.RQL.DML.Returning where
import Hasura.RQL.DML.Internal
import Hasura.Prelude
import Hasura.RQL.DML.Internal
import Hasura.RQL.Types
import Hasura.SQL.Types
@ -37,8 +37,8 @@ mkRetFldsExp :: RetFlds -> S.SQLExp
mkRetFldsExp retFlds =
S.mkRowExp $ flip map (Map.toList retFlds) $ \(k, retFld) ->
case retFld of
RExp t -> (k, S.SELit t)
RCol colInfo -> (k, mkColExp colInfo)
RExp t -> S.mkAliasedExtrFromExp (S.SELit t) $ Just $ PGCol k
RCol colInfo -> mkColExtrAl (Just $ PGCol k) colInfo
data MutFld
= MCount

View File

@ -404,9 +404,10 @@ mkJoinCond annRel =
--
-- { "col1" : v1, "col2" : v2 }
mkInnerSelExtr :: (FieldName, AnnFld) -> (T.Text, S.SQLExp)
mkInnerSelExtr :: (FieldName, AnnFld) -> S.Extractor
mkInnerSelExtr (alias, annFld) =
(getFieldNameTxt alias, colExp)
S.mkAliasedExtrFromExp colExp $
Just alias
where
colExp = case annFld of
FCol (pgCol, _) -> S.mkQIdenExp (TableName "r") pgCol
@ -420,20 +421,18 @@ mkLJCol (RelName rTxt) (PGCol cTxt) =
-- | Generates
--
-- IF (r.__r_col IS NULL) THEN 'null' ELSE row_to_json(..)
mkObjRelExtr :: PGCol -> RelName -> [(T.Text, S.SQLExp)] -> S.Extractor
mkObjRelExtr :: PGCol -> RelName -> [S.Extractor] -> S.Extractor
mkObjRelExtr compCol relName flds =
let idCol = S.mkQIdenExp (TableName "r") compCol
rowExp = S.mkRowExp flds
-- objAgg = S.SEFnApp "row_to_json" [rowExp] Nothing
condExp = S.SECond (S.BENull idCol) (S.SELit "null") rowExp
objAgg = S.SEFnApp "row_to_json" [rowExp] Nothing
condExp = S.SECond (S.BENull idCol) (S.SELit "null") objAgg
in S.mkAliasedExtrFromExp condExp $ Just relName
-- | Generates
--
-- IF (first(r.__r_col) IS NULL) THEN '[]' ELSE json_agg(..)
mkArrRelExtr
:: (Maybe S.OrderByExp) -> PGCol
-> RelName -> [(T.Text, S.SQLExp)] -> S.Extractor
mkArrRelExtr :: (Maybe S.OrderByExp) -> PGCol -> RelName -> [S.Extractor] -> S.Extractor
mkArrRelExtr mOb compCol relName flds =
let refCol = S.SEFnApp "hdb_catalog.first"
[ S.mkQIdenExp (TableName "r") compCol ] Nothing

View File

@ -122,25 +122,20 @@ mkSelFromExp isLateral sel tn =
where
alias = Alias $ toIden tn
mkRowExp :: [(T.Text, SQLExp)] -> SQLExp
mkRowExp extrs =
SEFnApp "json_build_object" args Nothing
where
args = concat [[SELit t, r] | (t, r) <- extrs]
mkRowExp :: [Extractor] -> SQLExp
mkRowExp extrs = let
innerSel = mkSelect { selExtr = extrs }
-- let
-- innerSel = mkSelect { selExtr = extrs }
innerSelName = TableName "e"
-- innerSelName = TableName "e"
-- -- SELECT r FROM (SELECT col1, col2, .. ) AS r
-- outerSel = mkSelect
-- { selExtr = [mkExtr innerSelName]
-- , selFrom = Just $ FromExp
-- [mkSelFromExp False innerSel innerSelName]
-- }
-- in
-- SESelect outerSel
-- SELECT r FROM (SELECT col1, col2, .. ) AS r
outerSel = mkSelect
{ selExtr = [mkExtr innerSelName]
, selFrom = Just $ FromExp
[mkSelFromExp False innerSel innerSelName]
}
in
SESelect outerSel
newtype HavingExp
= HavingExp BoolExp