2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.GraphQL.Resolve.Select
|
|
|
|
( convertSelect
|
2018-08-27 17:17:03 +03:00
|
|
|
, convertSelectByPKey
|
2018-10-26 12:02:44 +03:00
|
|
|
, convertAggSelect
|
2019-01-25 06:31:54 +03:00
|
|
|
, convertFuncQuery
|
2018-11-14 15:59:59 +03:00
|
|
|
, parseColumns
|
2018-10-26 12:02:44 +03:00
|
|
|
, withSelSet
|
2018-08-30 12:49:21 +03:00
|
|
|
, fromSelSet
|
2018-10-05 18:13:51 +03:00
|
|
|
, fieldAsPath
|
2018-10-19 05:15:28 +03:00
|
|
|
, fromField
|
|
|
|
, fromFieldByPKey
|
2018-10-26 12:02:44 +03:00
|
|
|
, fromAggField
|
2019-01-25 06:31:54 +03:00
|
|
|
, fromFuncQueryField
|
2018-06-27 16:11:32 +03:00
|
|
|
) where
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
import Control.Arrow (first)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Has
|
2019-03-25 16:45:35 +03:00
|
|
|
import Data.Parser.JSONPath
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
2018-10-26 14:57:33 +03:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as OMap
|
|
|
|
import qualified Data.List.NonEmpty as NE
|
2018-12-12 15:58:39 +03:00
|
|
|
import qualified Data.Text as T
|
2018-06-27 16:11:32 +03:00
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import qualified Hasura.RQL.DML.Select as RS
|
|
|
|
import qualified Hasura.SQL.DML as S
|
|
|
|
|
2019-02-22 13:27:38 +03:00
|
|
|
import Hasura.GraphQL.Context
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.GraphQL.Resolve.BoolExp
|
|
|
|
import Hasura.GraphQL.Resolve.Context
|
|
|
|
import Hasura.GraphQL.Resolve.InputValue
|
2018-11-14 15:59:59 +03:00
|
|
|
import Hasura.GraphQL.Schema (isAggFld)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.GraphQL.Validate.Field
|
|
|
|
import Hasura.GraphQL.Validate.Types
|
2018-08-06 15:15:08 +03:00
|
|
|
import Hasura.RQL.DML.Internal (onlyPositiveInt)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
2018-08-06 15:15:08 +03:00
|
|
|
import Hasura.SQL.Value
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-10-26 12:02:44 +03:00
|
|
|
withSelSet :: (Monad m) => SelSet -> (Field -> m a) -> m [(Text, a)]
|
|
|
|
withSelSet selSet f =
|
|
|
|
forM (toList selSet) $ \fld -> do
|
|
|
|
res <- f fld
|
|
|
|
return (G.unName $ G.unAlias $ _fAlias fld, res)
|
|
|
|
|
2019-03-25 16:45:35 +03:00
|
|
|
jsonPathToColExp :: (MonadError QErr m) => T.Text -> m S.SQLExp
|
|
|
|
jsonPathToColExp t = case parseJSONPath t of
|
|
|
|
Left s -> throw400 ParseFailed $ T.pack $ "parse json path error: " ++ s
|
|
|
|
Right jPaths -> return $ S.SEArray $ map elToColExp jPaths
|
|
|
|
where
|
|
|
|
elToColExp (Key k) = S.SELit k
|
|
|
|
elToColExp (Index i) = S.SELit $ T.pack (show i)
|
|
|
|
|
|
|
|
|
|
|
|
argsToColOp :: (MonadError QErr m) => ArgsMap -> m (Maybe RS.ColOp)
|
|
|
|
argsToColOp args = maybe (return Nothing) toOp $ Map.lookup "path" args
|
|
|
|
where
|
|
|
|
toJsonPathExp = fmap (RS.ColOp S.jsonbPathOp) . jsonPathToColExp
|
|
|
|
toOp v = asPGColTextM v >>= mapM toJsonPathExp
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
fromSelSet
|
2019-03-01 14:45:04 +03:00
|
|
|
:: ( MonadError QErr m, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
2019-01-25 06:31:54 +03:00
|
|
|
=> PrepFn m -> G.NamedType -> SelSet -> m RS.AnnFlds
|
2018-10-19 05:15:28 +03:00
|
|
|
fromSelSet f fldTy flds =
|
2018-10-05 11:56:47 +03:00
|
|
|
forM (toList flds) $ \fld -> do
|
2018-06-27 16:11:32 +03:00
|
|
|
let fldName = _fName fld
|
|
|
|
let rqlFldName = FieldName $ G.unName $ G.unAlias $ _fAlias fld
|
2018-10-05 11:56:47 +03:00
|
|
|
(rqlFldName,) <$> case fldName of
|
|
|
|
"__typename" -> return $ RS.FExp $ G.unName $ G.unNamedType fldTy
|
2018-06-27 16:11:32 +03:00
|
|
|
_ -> do
|
|
|
|
fldInfo <- getFldInfo fldTy fldName
|
|
|
|
case fldInfo of
|
2019-03-25 16:45:35 +03:00
|
|
|
Left colInfo ->
|
|
|
|
(RS.FCol colInfo) <$> (argsToColOp $ _fArguments fld)
|
|
|
|
-- let jsonCol = return $ RS.FCol $ colInfo { pgiName = PGCol $ T.pack "metadata->'name'" }
|
2018-10-26 12:02:44 +03:00
|
|
|
Right (relInfo, isAgg, tableFilter, tableLimit) -> do
|
2018-06-27 16:11:32 +03:00
|
|
|
let relTN = riRTable relInfo
|
2018-10-31 15:51:20 +03:00
|
|
|
colMapping = riMapping relInfo
|
2018-12-12 15:58:39 +03:00
|
|
|
rn = riName relInfo
|
2018-10-31 15:51:20 +03:00
|
|
|
if isAgg then do
|
|
|
|
aggSel <- fromAggField f relTN tableFilter tableLimit fld
|
2018-12-12 15:58:39 +03:00
|
|
|
return $ RS.FArr $ RS.ASAgg $ RS.AnnRelG rn colMapping aggSel
|
2018-10-31 15:51:20 +03:00
|
|
|
else do
|
|
|
|
annSel <- fromField f relTN tableFilter tableLimit fld
|
2018-12-12 15:58:39 +03:00
|
|
|
let annRel = RS.AnnRelG rn colMapping annSel
|
|
|
|
return $ case riType relInfo of
|
|
|
|
ObjRel -> RS.FObj annRel
|
|
|
|
ArrRel -> RS.FArr $ RS.ASSimple annRel
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
fromAggSelSet
|
2019-03-01 14:45:04 +03:00
|
|
|
:: ( MonadError QErr m, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
2019-01-25 06:31:54 +03:00
|
|
|
=> PrepFn m -> G.NamedType -> SelSet -> m RS.TableAggFlds
|
|
|
|
fromAggSelSet fn fldTy selSet = fmap toFields $
|
|
|
|
withSelSet selSet $ \f -> do
|
|
|
|
let fTy = _fType f
|
|
|
|
fSelSet = _fSelSet f
|
|
|
|
case _fName f of
|
|
|
|
"__typename" -> return $ RS.TAFExp $ G.unName $ G.unNamedType fldTy
|
|
|
|
"aggregate" -> RS.TAFAgg <$> convertAggFld fTy fSelSet
|
|
|
|
"nodes" -> RS.TAFNodes <$> fromSelSet fn fTy fSelSet
|
|
|
|
G.Name t -> throw500 $ "unexpected field in _agg node: " <> t
|
|
|
|
|
2018-08-06 15:15:08 +03:00
|
|
|
fieldAsPath :: (MonadError QErr m) => Field -> m a -> m a
|
2018-10-26 12:02:44 +03:00
|
|
|
fieldAsPath = nameAsPath . _fName
|
2018-08-06 15:15:08 +03:00
|
|
|
|
2018-10-05 11:56:47 +03:00
|
|
|
parseTableArgs
|
2019-03-01 14:45:04 +03:00
|
|
|
:: ( MonadError QErr m, MonadReader r m
|
|
|
|
, Has FieldMap r, Has OrdByCtx r
|
|
|
|
)
|
2018-12-12 15:58:39 +03:00
|
|
|
=> PrepFn m -> ArgsMap -> m RS.TableArgs
|
2018-11-16 15:40:23 +03:00
|
|
|
parseTableArgs f args = do
|
|
|
|
whereExpM <- withArgM args "where" $ parseBoolExp f
|
2018-10-26 14:57:33 +03:00
|
|
|
ordByExpML <- withArgM args "order_by" parseOrderBy
|
|
|
|
let ordByExpM = NE.nonEmpty =<< ordByExpML
|
2018-10-05 11:56:47 +03:00
|
|
|
limitExpM <- withArgM args "limit" parseLimit
|
2018-10-19 05:15:28 +03:00
|
|
|
offsetExpM <- withArgM args "offset" $ asPGColVal >=> f
|
2018-11-23 04:53:56 +03:00
|
|
|
distOnColsML <- withArgM args "distinct_on" parseColumns
|
|
|
|
let distOnColsM = NE.nonEmpty =<< distOnColsML
|
|
|
|
mapM_ (validateDistOn ordByExpM) distOnColsM
|
|
|
|
return $ RS.TableArgs whereExpM ordByExpM limitExpM offsetExpM distOnColsM
|
|
|
|
where
|
|
|
|
validateDistOn Nothing _ = return ()
|
|
|
|
validateDistOn (Just ordBys) cols = withPathK "args" $ do
|
|
|
|
let colsLen = length cols
|
|
|
|
initOrdBys = take colsLen $ toList ordBys
|
|
|
|
initOrdByCols = flip mapMaybe initOrdBys $ \ob ->
|
|
|
|
case obiColumn ob of
|
|
|
|
RS.AOCPG ci -> Just $ pgiName ci
|
|
|
|
_ -> Nothing
|
|
|
|
isValid = (colsLen == length initOrdByCols)
|
|
|
|
&& all (`elem` initOrdByCols) (toList cols)
|
|
|
|
|
|
|
|
unless isValid $ throwVE
|
|
|
|
"\"distinct_on\" columns must match initial \"order_by\" columns"
|
2018-10-05 11:56:47 +03:00
|
|
|
|
|
|
|
fromField
|
2019-03-01 14:45:04 +03:00
|
|
|
:: ( MonadError QErr m, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
2018-12-12 15:58:39 +03:00
|
|
|
=> PrepFn m -> QualifiedTable -> AnnBoolExpSQL
|
|
|
|
-> Maybe Int -> Field -> m RS.AnnSel
|
2019-01-25 06:31:54 +03:00
|
|
|
fromField f tn permFilter permLimitM fld = fieldAsPath fld $ do
|
2018-11-16 15:40:23 +03:00
|
|
|
tableArgs <- parseTableArgs f args
|
2018-10-19 05:15:28 +03:00
|
|
|
annFlds <- fromSelSet f (_fType fld) $ _fSelSet fld
|
2018-10-31 15:51:20 +03:00
|
|
|
let tabFrom = RS.TableFrom tn Nothing
|
2018-10-26 12:02:44 +03:00
|
|
|
tabPerm = RS.TablePerm permFilter permLimitM
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> asks getter
|
|
|
|
return $ RS.AnnSelG annFlds tabFrom tabPerm tableArgs strfyNum
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
args = _fArguments fld
|
|
|
|
|
2018-10-26 14:57:33 +03:00
|
|
|
getOrdByItemMap
|
2018-06-27 16:11:32 +03:00
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader r m
|
2018-10-26 14:57:33 +03:00
|
|
|
, Has OrdByCtx r
|
2018-06-27 16:11:32 +03:00
|
|
|
)
|
2018-10-26 14:57:33 +03:00
|
|
|
=> G.NamedType -> m OrdByItemMap
|
|
|
|
getOrdByItemMap nt = do
|
2018-06-27 16:11:32 +03:00
|
|
|
ordByCtx <- asks getter
|
2018-10-26 14:57:33 +03:00
|
|
|
onNothing (Map.lookup nt ordByCtx) $
|
|
|
|
throw500 $ "could not lookup " <> showNamedTy nt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
parseOrderBy
|
2018-10-26 14:57:33 +03:00
|
|
|
:: ( MonadError QErr m
|
2018-06-27 16:11:32 +03:00
|
|
|
, MonadReader r m
|
2018-10-26 14:57:33 +03:00
|
|
|
, Has OrdByCtx r
|
2018-06-27 16:11:32 +03:00
|
|
|
)
|
2019-03-20 09:31:49 +03:00
|
|
|
=> AnnInpVal -> m [RS.AnnOrderByItem]
|
2018-10-26 14:57:33 +03:00
|
|
|
parseOrderBy = fmap concat . withArray f
|
|
|
|
where
|
|
|
|
f _ = mapM (withObject (getAnnObItems id))
|
|
|
|
|
|
|
|
getAnnObItems
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader r m
|
|
|
|
, Has OrdByCtx r
|
|
|
|
)
|
|
|
|
=> (RS.AnnObCol -> RS.AnnObCol)
|
|
|
|
-> G.NamedType
|
|
|
|
-> AnnGObject
|
|
|
|
-> m [RS.AnnOrderByItem]
|
|
|
|
getAnnObItems f nt obj = do
|
|
|
|
ordByItemMap <- getOrdByItemMap nt
|
|
|
|
fmap concat $ forM (OMap.toList obj) $ \(k, v) -> do
|
|
|
|
ordByItem <- onNothing (Map.lookup k ordByItemMap) $ throw500 $
|
|
|
|
"cannot lookup " <> showName k <> " order by item in "
|
|
|
|
<> showNamedTy nt <> " map"
|
|
|
|
case ordByItem of
|
|
|
|
OBIPGCol ci -> do
|
|
|
|
let aobCol = f $ RS.AOCPG ci
|
|
|
|
(_, enumVal) <- asEnumVal v
|
|
|
|
(ordTy, nullsOrd) <- parseOrderByEnum enumVal
|
2018-12-13 10:26:15 +03:00
|
|
|
return [mkOrdByItemG ordTy aobCol nullsOrd]
|
2018-10-26 14:57:33 +03:00
|
|
|
OBIRel ri fltr -> do
|
2018-12-12 15:58:39 +03:00
|
|
|
let annObColFn = f . RS.AOCObj ri fltr
|
2018-10-26 14:57:33 +03:00
|
|
|
withObject (getAnnObItems annObColFn) v
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
OBIAgg ri fltr -> do
|
|
|
|
let aobColFn = f . RS.AOCAgg ri fltr
|
|
|
|
flip withObject v $ \_ o -> parseAggOrdBy aobColFn o
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
mkOrdByItemG :: S.OrderType -> a -> S.NullsOrder -> OrderByItemG a
|
|
|
|
mkOrdByItemG ordTy aobCol nullsOrd =
|
|
|
|
OrderByItemG (Just $ OrderType ordTy) aobCol (Just $ NullsOrder nullsOrd)
|
|
|
|
|
2018-12-12 15:58:39 +03:00
|
|
|
parseAggOrdBy
|
|
|
|
:: (MonadError QErr m)
|
|
|
|
=> (RS.AnnAggOrdBy -> RS.AnnObCol)
|
|
|
|
-> AnnGObject
|
|
|
|
-> m [RS.AnnOrderByItem]
|
|
|
|
parseAggOrdBy f annObj =
|
|
|
|
fmap concat <$> forM (OMap.toList annObj) $ \(op, obVal) ->
|
|
|
|
case op of
|
|
|
|
"count" -> do
|
|
|
|
(ordTy, nullsOrd) <- parseAsEnum obVal
|
2018-12-13 10:26:15 +03:00
|
|
|
return [mkOrdByItemG ordTy (f RS.AAOCount) nullsOrd]
|
2018-12-12 15:58:39 +03:00
|
|
|
|
|
|
|
G.Name opT ->
|
|
|
|
flip withObject obVal $ \_ opObObj ->
|
|
|
|
forM (OMap.toList opObObj) $ \(col, eVal) -> do
|
|
|
|
(ordTy, nullsOrd) <- parseAsEnum eVal
|
|
|
|
let aobCol = f $ RS.AAOOp opT $ PGCol $ G.unName col
|
2018-12-13 10:26:15 +03:00
|
|
|
return $ mkOrdByItemG ordTy aobCol nullsOrd
|
2018-12-12 15:58:39 +03:00
|
|
|
where
|
|
|
|
parseAsEnum v = do
|
|
|
|
(_, enumVal) <- asEnumVal v
|
|
|
|
parseOrderByEnum enumVal
|
|
|
|
|
2018-10-26 14:57:33 +03:00
|
|
|
parseOrderByEnum
|
|
|
|
:: (MonadError QErr m)
|
|
|
|
=> G.EnumValue
|
|
|
|
-> m (S.OrderType, S.NullsOrder)
|
|
|
|
parseOrderByEnum = \case
|
|
|
|
G.EnumValue "asc" -> return (S.OTAsc, S.NLast)
|
2018-11-22 07:58:18 +03:00
|
|
|
G.EnumValue "asc_nulls_last" -> return (S.OTAsc, S.NLast)
|
2018-10-26 14:57:33 +03:00
|
|
|
G.EnumValue "asc_nulls_first" -> return (S.OTAsc, S.NFirst)
|
2018-11-22 07:58:18 +03:00
|
|
|
G.EnumValue "desc" -> return (S.OTDesc, S.NFirst)
|
2018-10-26 14:57:33 +03:00
|
|
|
G.EnumValue "desc_nulls_first" -> return (S.OTDesc, S.NFirst)
|
2018-11-22 07:58:18 +03:00
|
|
|
G.EnumValue "desc_nulls_last" -> return (S.OTDesc, S.NLast)
|
2018-10-26 14:57:33 +03:00
|
|
|
G.EnumValue v -> throw500 $
|
|
|
|
"enum value " <> showName v <> " not found in type order_by"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-03-20 09:31:49 +03:00
|
|
|
parseLimit :: ( MonadError QErr m ) => AnnInpVal -> m Int
|
2018-08-06 15:15:08 +03:00
|
|
|
parseLimit v = do
|
2019-03-20 09:31:49 +03:00
|
|
|
pgColVal <- _apvValue <$> asPGColVal v
|
2018-08-06 15:15:08 +03:00
|
|
|
limit <- maybe noIntErr return $ pgColValueToInt pgColVal
|
|
|
|
-- validate int value
|
|
|
|
onlyPositiveInt limit
|
|
|
|
return limit
|
|
|
|
where
|
2018-12-04 16:37:38 +03:00
|
|
|
noIntErr = throwVE "expecting Integer value for \"limit\""
|
2018-08-06 15:15:08 +03:00
|
|
|
|
2018-10-05 11:56:47 +03:00
|
|
|
fromFieldByPKey
|
2019-02-22 13:27:38 +03:00
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadReader r m
|
|
|
|
, Has FieldMap r
|
|
|
|
, Has OrdByCtx r
|
2019-03-01 14:45:04 +03:00
|
|
|
, Has SQLGenCtx r
|
2019-02-22 13:27:38 +03:00
|
|
|
)
|
|
|
|
=> PrepFn m -> QualifiedTable -> PGColArgMap
|
|
|
|
-> AnnBoolExpSQL -> Field -> m RS.AnnSel
|
|
|
|
fromFieldByPKey f tn colArgMap permFilter fld = fieldAsPath fld $ do
|
|
|
|
boolExp <- pgColValToBoolExp f colArgMap $ _fArguments fld
|
|
|
|
annFlds <- fromSelSet f fldTy $ _fSelSet fld
|
2018-10-31 15:51:20 +03:00
|
|
|
let tabFrom = RS.TableFrom tn Nothing
|
2018-10-26 12:02:44 +03:00
|
|
|
tabPerm = RS.TablePerm permFilter Nothing
|
2019-03-01 14:45:04 +03:00
|
|
|
tabArgs = RS.noTableArgs { RS._taWhere = Just boolExp}
|
|
|
|
strfyNum <- stringifyNum <$> asks getter
|
|
|
|
return $ RS.AnnSelG annFlds tabFrom tabPerm tabArgs strfyNum
|
2019-02-22 13:27:38 +03:00
|
|
|
where
|
|
|
|
fldTy = _fType fld
|
2018-10-05 11:56:47 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
convertSelect
|
2019-02-22 13:27:38 +03:00
|
|
|
:: SelOpCtx -> Field -> Convert RespTx
|
|
|
|
convertSelect opCtx fld = do
|
2019-03-25 21:25:25 +03:00
|
|
|
(selData, prepArgs) <-
|
|
|
|
withPathK "selectionSet" $ withPrepArgs $
|
|
|
|
fromField prepare qt permFilter permLimit fld
|
2018-10-05 11:56:47 +03:00
|
|
|
return $ RS.selectP2 False (selData, prepArgs)
|
2019-02-22 13:27:38 +03:00
|
|
|
where
|
|
|
|
SelOpCtx qt _ permFilter permLimit = opCtx
|
2018-08-27 17:17:03 +03:00
|
|
|
|
|
|
|
convertSelectByPKey
|
2019-02-22 13:27:38 +03:00
|
|
|
:: SelPkOpCtx -> Field -> Convert RespTx
|
|
|
|
convertSelectByPKey opCtx fld = do
|
2019-03-25 21:25:25 +03:00
|
|
|
(selData, prepArgs) <-
|
|
|
|
withPathK "selectionSet" $ withPrepArgs $
|
|
|
|
fromFieldByPKey prepare qt colArgMap permFilter fld
|
2018-10-05 11:56:47 +03:00
|
|
|
return $ RS.selectP2 True (selData, prepArgs)
|
2019-02-22 13:27:38 +03:00
|
|
|
where
|
|
|
|
SelPkOpCtx qt _ permFilter colArgMap = opCtx
|
2018-10-26 12:02:44 +03:00
|
|
|
|
|
|
|
-- agg select related
|
2019-03-20 09:31:49 +03:00
|
|
|
parseColumns :: MonadError QErr m => AnnInpVal -> m [PGCol]
|
2018-11-14 15:59:59 +03:00
|
|
|
parseColumns val =
|
|
|
|
flip withArray val $ \_ vals ->
|
|
|
|
forM vals $ \v -> do
|
|
|
|
(_, enumVal) <- asEnumVal v
|
|
|
|
return $ PGCol $ G.unName $ G.unEnumValue enumVal
|
|
|
|
|
|
|
|
convertCount :: MonadError QErr m => ArgsMap -> m S.CountType
|
|
|
|
convertCount args = do
|
|
|
|
columnsM <- withArgM args "columns" parseColumns
|
|
|
|
isDistinct <- or <$> withArgM args "distinct" parseDistinct
|
|
|
|
maybe (return S.CTStar) (mkCType isDistinct) columnsM
|
|
|
|
where
|
|
|
|
parseDistinct v = do
|
2019-03-20 09:31:49 +03:00
|
|
|
val <- _apvValue <$> asPGColVal v
|
2018-11-14 15:59:59 +03:00
|
|
|
case val of
|
|
|
|
PGValBoolean b -> return b
|
|
|
|
_ ->
|
|
|
|
throw500 "expecting Boolean for \"distinct\""
|
|
|
|
|
|
|
|
mkCType isDistinct cols = return $
|
|
|
|
bool (S.CTSimple cols) (S.CTDistinct cols) isDistinct
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
toFields :: [(T.Text, a)] -> RS.Fields a
|
2018-12-12 15:58:39 +03:00
|
|
|
toFields = map (first FieldName)
|
|
|
|
|
2018-10-26 12:02:44 +03:00
|
|
|
convertColFlds
|
|
|
|
:: Monad m => G.NamedType -> SelSet -> m RS.ColFlds
|
2018-12-12 15:58:39 +03:00
|
|
|
convertColFlds ty selSet = fmap toFields $
|
2018-10-26 12:02:44 +03:00
|
|
|
withSelSet selSet $ \fld ->
|
|
|
|
case _fName fld of
|
|
|
|
"__typename" -> return $ RS.PCFExp $ G.unName $ G.unNamedType ty
|
|
|
|
n -> return $ RS.PCFCol $ PGCol $ G.unName n
|
|
|
|
|
|
|
|
convertAggFld
|
|
|
|
:: (Monad m, MonadError QErr m)
|
|
|
|
=> G.NamedType -> SelSet -> m RS.AggFlds
|
2018-12-12 15:58:39 +03:00
|
|
|
convertAggFld ty selSet = fmap toFields $
|
2018-10-26 12:02:44 +03:00
|
|
|
withSelSet selSet $ \fld -> do
|
|
|
|
let fType = _fType fld
|
|
|
|
fSelSet = _fSelSet fld
|
|
|
|
case _fName fld of
|
|
|
|
"__typename" -> return $ RS.AFExp $ G.unName $ G.unNamedType ty
|
2018-11-14 15:59:59 +03:00
|
|
|
"count" -> RS.AFCount <$> convertCount (_fArguments fld)
|
|
|
|
n -> do
|
|
|
|
colFlds <- convertColFlds fType fSelSet
|
|
|
|
unless (isAggFld n) $ throwInvalidFld n
|
|
|
|
return $ RS.AFOp $ RS.AggOp (G.unName n) colFlds
|
|
|
|
where
|
|
|
|
throwInvalidFld (G.Name t) =
|
|
|
|
throw500 $ "unexpected field in _aggregate node: " <> t
|
2018-10-26 12:02:44 +03:00
|
|
|
|
|
|
|
fromAggField
|
2019-03-01 14:45:04 +03:00
|
|
|
:: ( MonadError QErr m, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
2018-12-12 15:58:39 +03:00
|
|
|
=> PrepFn m -> QualifiedTable -> AnnBoolExpSQL
|
|
|
|
-> Maybe Int -> Field -> m RS.AnnAggSel
|
2019-01-25 06:31:54 +03:00
|
|
|
fromAggField f tn permFilter permLimit fld = fieldAsPath fld $ do
|
|
|
|
tableArgs <- parseTableArgs f args
|
|
|
|
aggSelFlds <- fromAggSelSet f (_fType fld) (_fSelSet fld)
|
2018-10-31 15:51:20 +03:00
|
|
|
let tabFrom = RS.TableFrom tn Nothing
|
2019-01-25 06:31:54 +03:00
|
|
|
tabPerm = RS.TablePerm permFilter permLimit
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> asks getter
|
|
|
|
return $ RS.AnnSelG aggSelFlds tabFrom tabPerm tableArgs strfyNum
|
2018-10-26 12:02:44 +03:00
|
|
|
where
|
|
|
|
args = _fArguments fld
|
|
|
|
|
2019-02-22 13:27:38 +03:00
|
|
|
convertAggSelect :: SelOpCtx -> Field -> Convert RespTx
|
|
|
|
convertAggSelect opCtx fld = do
|
2019-03-25 21:25:25 +03:00
|
|
|
(selData, prepArgs) <-
|
|
|
|
withPathK "selectionSet" $ withPrepArgs $
|
|
|
|
fromAggField prepare qt permFilter permLimit fld
|
2018-10-31 15:51:20 +03:00
|
|
|
return $ RS.selectAggP2 (selData, prepArgs)
|
2019-02-22 13:27:38 +03:00
|
|
|
where
|
|
|
|
SelOpCtx qt _ permFilter permLimit = opCtx
|
2019-01-25 06:31:54 +03:00
|
|
|
|
|
|
|
fromFuncQueryField
|
|
|
|
::( MonadError QErr m, MonadReader r m, Has FieldMap r
|
2019-03-01 14:45:04 +03:00
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
2019-01-25 06:31:54 +03:00
|
|
|
)
|
2019-02-22 13:27:38 +03:00
|
|
|
=> PrepFn m -> QualifiedFunction -> FuncArgSeq -> Bool -> Field
|
2019-01-25 06:31:54 +03:00
|
|
|
-> m (RS.TableArgs, Either RS.TableAggFlds RS.AnnFlds, S.FromItem)
|
2019-02-22 13:27:38 +03:00
|
|
|
fromFuncQueryField f qf argSeq isAgg fld = fieldAsPath fld $ do
|
|
|
|
funcArgsM <- withArgM args "args" $ parseFunctionArgs f argSeq
|
2019-01-25 06:31:54 +03:00
|
|
|
let funcArgs = fromMaybe [] funcArgsM
|
|
|
|
funcFrmItem = S.mkFuncFromItem qf funcArgs
|
|
|
|
tableArgs <- parseTableArgs f args
|
|
|
|
eSelFlds <- bool nonAggSel aggSel isAgg
|
|
|
|
return (tableArgs, eSelFlds, funcFrmItem)
|
|
|
|
where
|
|
|
|
args = _fArguments fld
|
|
|
|
|
|
|
|
nonAggSel = Right <$>
|
|
|
|
fromSelSet f (_fType fld) (_fSelSet fld)
|
|
|
|
aggSel = Left <$>
|
|
|
|
fromAggSelSet f (_fType fld) (_fSelSet fld)
|
|
|
|
|
|
|
|
parseFunctionArgs
|
2019-02-22 13:27:38 +03:00
|
|
|
::(MonadError QErr m)
|
2019-03-20 09:31:49 +03:00
|
|
|
=> PrepFn m -> FuncArgSeq -> AnnInpVal -> m [S.SQLExp]
|
2019-02-22 13:27:38 +03:00
|
|
|
parseFunctionArgs fn argSeq val =
|
|
|
|
flip withObject val $ \nTy obj ->
|
2019-01-25 06:31:54 +03:00
|
|
|
fmap toList $ forM argSeq $ \(FuncArgItem argName) -> do
|
|
|
|
argVal <- onNothing (OMap.lookup argName obj) $ throw500 $
|
|
|
|
"argument " <> showName argName <> " required in input type "
|
|
|
|
<> showNamedTy nTy
|
|
|
|
fn =<< asPGColVal argVal
|
|
|
|
|
|
|
|
convertFuncQuery
|
2019-02-22 13:27:38 +03:00
|
|
|
:: FuncQOpCtx -> Bool -> Field -> Convert RespTx
|
|
|
|
convertFuncQuery funcOpCtx isAgg fld = do
|
2019-03-25 21:25:25 +03:00
|
|
|
((tableArgs, sel, frmItem), prepArgs) <-
|
|
|
|
withPathK "selectionSet" $ withPrepArgs $
|
2019-02-22 13:27:38 +03:00
|
|
|
fromFuncQueryField prepare qf argSeq isAgg fld
|
2019-01-25 06:31:54 +03:00
|
|
|
let tabPerm = RS.TablePerm permFilter permLimit
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> asks getter
|
|
|
|
return $ RS.funcQueryTx frmItem qf qt tabPerm tableArgs strfyNum (sel, prepArgs)
|
2019-02-22 13:27:38 +03:00
|
|
|
where
|
|
|
|
FuncQOpCtx qt _ permFilter permLimit qf argSeq = funcOpCtx
|