2021-07-29 11:29:12 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.RQL.DML.Update
|
2021-09-24 01:56:37 +03:00
|
|
|
( runUpdate,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Data.Aeson.Types
|
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.Sequence qualified as DS
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Backends.Postgres.Execute.Mutation
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.Translate.Returning
|
|
|
|
import Hasura.Backends.Postgres.Types.Table
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.QueryTags
|
|
|
|
import Hasura.RQL.DML.Internal
|
|
|
|
import Hasura.RQL.DML.Types
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.IR.Update
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
|
|
|
|
convInc ::
|
|
|
|
(QErrM m) =>
|
|
|
|
ValueParser ('Postgres 'Vanilla) m S.SQLExp ->
|
|
|
|
PGCol ->
|
|
|
|
ColumnType ('Postgres 'Vanilla) ->
|
|
|
|
Value ->
|
|
|
|
m (PGCol, S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
convInc f col colType val = do
|
2021-02-14 09:07:52 +03:00
|
|
|
prepExp <- f (CollectableTypeScalar colType) val
|
2018-07-20 13:51:20 +03:00
|
|
|
return (col, S.SEOpApp S.incOp [S.mkSIdenExp col, prepExp])
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
convMul ::
|
|
|
|
(QErrM m) =>
|
|
|
|
ValueParser ('Postgres 'Vanilla) m S.SQLExp ->
|
|
|
|
PGCol ->
|
|
|
|
ColumnType ('Postgres 'Vanilla) ->
|
|
|
|
Value ->
|
|
|
|
m (PGCol, S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
convMul f col colType val = do
|
2021-02-14 09:07:52 +03:00
|
|
|
prepExp <- f (CollectableTypeScalar colType) val
|
2018-07-20 13:51:20 +03:00
|
|
|
return (col, S.SEOpApp S.mulOp [S.mkSIdenExp col, prepExp])
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
convSet ::
|
|
|
|
(QErrM m) =>
|
|
|
|
ValueParser ('Postgres 'Vanilla) m S.SQLExp ->
|
|
|
|
PGCol ->
|
|
|
|
ColumnType ('Postgres 'Vanilla) ->
|
|
|
|
Value ->
|
|
|
|
m (PGCol, S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
convSet f col colType val = do
|
2021-02-14 09:07:52 +03:00
|
|
|
prepExp <- f (CollectableTypeScalar colType) val
|
2018-06-27 16:11:32 +03:00
|
|
|
return (col, prepExp)
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
convDefault :: (Monad m) => PGCol -> ColumnType ('Postgres 'Vanilla) -> () -> m (PGCol, S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
convDefault col _ _ = return (col, S.SEUnsafe "DEFAULT")
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
convOp ::
|
|
|
|
(UserInfoM m, QErrM m) =>
|
|
|
|
FieldInfoMap (FieldInfo ('Postgres 'Vanilla)) ->
|
|
|
|
[PGCol] ->
|
|
|
|
UpdPermInfo ('Postgres 'Vanilla) ->
|
|
|
|
[(PGCol, a)] ->
|
|
|
|
(PGCol -> ColumnType ('Postgres 'Vanilla) -> a -> m (PGCol, S.SQLExp)) ->
|
|
|
|
m [(PGCol, S.SQLExp)]
|
2019-02-11 15:45:30 +03:00
|
|
|
convOp fieldInfoMap preSetCols updPerm objs conv =
|
2018-06-27 16:11:32 +03:00
|
|
|
forM objs $ \(pgCol, a) -> do
|
2019-02-11 15:45:30 +03:00
|
|
|
-- if column has predefined value then throw error
|
|
|
|
when (pgCol `elem` preSetCols) $ throwNotUpdErr pgCol
|
2021-04-22 00:44:37 +03:00
|
|
|
checkPermOnCol @('Postgres 'Vanilla) PTUpdate allowedCols pgCol
|
2021-02-14 09:07:52 +03:00
|
|
|
colType <- askColumnType fieldInfoMap pgCol relWhenPgErr
|
2018-06-27 16:11:32 +03:00
|
|
|
res <- conv pgCol colType a
|
|
|
|
-- build a set expression's entry
|
|
|
|
withPathK (getPGColTxt pgCol) $ return res
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
allowedCols = upiCols updPerm
|
2018-06-27 16:11:32 +03:00
|
|
|
relWhenPgErr = "relationships can't be updated"
|
2019-02-11 15:45:30 +03:00
|
|
|
throwNotUpdErr c = do
|
2020-04-24 12:10:53 +03:00
|
|
|
roleName <- _uiRole <$> askUserInfo
|
2021-09-24 01:56:37 +03:00
|
|
|
throw400 NotSupported $
|
|
|
|
"column " <> c <<> " is not updatable"
|
|
|
|
<> " for role "
|
|
|
|
<> roleName <<> "; its value is predefined in permission"
|
|
|
|
|
|
|
|
validateUpdateQueryWith ::
|
|
|
|
(UserInfoM m, QErrM m, TableInfoRM ('Postgres 'Vanilla) m) =>
|
|
|
|
SessionVariableBuilder ('Postgres 'Vanilla) m ->
|
|
|
|
ValueParser ('Postgres 'Vanilla) m S.SQLExp ->
|
|
|
|
UpdateQuery ->
|
|
|
|
m (AnnUpd ('Postgres 'Vanilla))
|
2019-04-17 12:48:41 +03:00
|
|
|
validateUpdateQueryWith sessVarBldr prepValBldr uq = do
|
2018-06-27 16:11:32 +03:00
|
|
|
let tableName = uqTable uq
|
2020-12-28 15:56:00 +03:00
|
|
|
tableInfo <- withPathK "table" $ askTabInfoSource tableName
|
2019-11-20 21:21:30 +03:00
|
|
|
let coreInfo = _tiCoreInfo tableInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-10-12 15:06:12 +03:00
|
|
|
-- If it is view then check if it is updatable
|
2021-09-24 01:56:37 +03:00
|
|
|
mutableView
|
|
|
|
tableName
|
|
|
|
viIsUpdatable
|
|
|
|
(_tciViewInfo coreInfo)
|
|
|
|
"updatable"
|
2018-10-12 15:06:12 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
-- Check if the role has update permissions
|
|
|
|
updPerm <- askUpdPermInfo tableInfo
|
|
|
|
|
|
|
|
-- Check if all dependent headers are present
|
|
|
|
validateHeaders $ upiRequiredHeaders updPerm
|
|
|
|
|
|
|
|
-- Check if select is allowed
|
2021-09-24 01:56:37 +03:00
|
|
|
selPerm <-
|
|
|
|
modifyErr (<> selNecessaryMsg) $
|
|
|
|
askSelPermInfo tableInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
let fieldInfoMap = _tciFieldInfoMap coreInfo
|
2019-03-22 10:08:42 +03:00
|
|
|
allCols = getCols fieldInfoMap
|
2019-02-11 15:45:30 +03:00
|
|
|
preSetObj = upiSet updPerm
|
|
|
|
preSetCols = M.keys preSetObj
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- convert the object to SQL set expression
|
2021-09-24 01:56:37 +03:00
|
|
|
setItems <-
|
|
|
|
withPathK "$set" $
|
|
|
|
convOp fieldInfoMap preSetCols updPerm (M.toList $ uqSet uq) $ convSet prepValBldr
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
incItems <-
|
|
|
|
withPathK "$inc" $
|
|
|
|
convOp fieldInfoMap preSetCols updPerm (M.toList $ uqInc uq) $ convInc prepValBldr
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mulItems <-
|
|
|
|
withPathK "$mul" $
|
|
|
|
convOp fieldInfoMap preSetCols updPerm (M.toList $ uqMul uq) $ convMul prepValBldr
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
defItems <-
|
|
|
|
withPathK "$default" $
|
|
|
|
convOp fieldInfoMap preSetCols updPerm ((,()) <$> uqDefault uq) convDefault
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- convert the returning cols into sql returing exp
|
|
|
|
mAnnRetCols <- forM mRetCols $ \retCols ->
|
2018-10-05 11:56:47 +03:00
|
|
|
withPathK "returning" $ checkRetCols fieldInfoMap selPerm retCols
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
resolvedPreSetItems <-
|
|
|
|
M.toList
|
|
|
|
<$> mapM (convPartialSQLExp sessVarBldr) preSetObj
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
let setExpItems =
|
|
|
|
resolvedPreSetItems
|
|
|
|
++ setItems
|
|
|
|
++ incItems
|
|
|
|
++ mulItems
|
|
|
|
++ defItems
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
when (null setExpItems) $
|
|
|
|
throw400 UnexpectedPayload "atleast one of $set, $inc, $mul has to be present"
|
|
|
|
|
|
|
|
-- convert the where clause
|
2021-09-24 01:56:37 +03:00
|
|
|
annSQLBoolExp <-
|
|
|
|
withPathK "where" $
|
|
|
|
convBoolExp fieldInfoMap selPerm (uqWhere uq) sessVarBldr tableName prepValBldr
|
|
|
|
|
|
|
|
resolvedUpdFltr <-
|
|
|
|
convAnnBoolExpPartialSQL sessVarBldr $
|
|
|
|
upiFilter updPerm
|
|
|
|
resolvedUpdCheck <-
|
|
|
|
fromMaybe gBoolExpTrue
|
|
|
|
<$> traverse
|
|
|
|
(convAnnBoolExpPartialSQL sessVarBldr)
|
|
|
|
(upiCheck updPerm)
|
|
|
|
|
|
|
|
return $
|
|
|
|
AnnUpd
|
|
|
|
tableName
|
|
|
|
(fmap UpdSet <$> setExpItems)
|
|
|
|
(resolvedUpdFltr, annSQLBoolExp)
|
|
|
|
resolvedUpdCheck
|
|
|
|
(mkDefaultMutFlds mAnnRetCols)
|
|
|
|
allCols
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
mRetCols = uqReturning uq
|
|
|
|
selNecessaryMsg =
|
|
|
|
"; \"update\" is only allowed if the role "
|
2021-09-24 01:56:37 +03:00
|
|
|
<> "has \"select\" permission as \"where\" can't be used "
|
|
|
|
<> "without \"select\" permission on the table"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
validateUpdateQuery ::
|
|
|
|
(QErrM m, UserInfoM m, CacheRM m) =>
|
|
|
|
UpdateQuery ->
|
|
|
|
m (AnnUpd ('Postgres 'Vanilla), DS.Seq Q.PrepArg)
|
2021-01-07 12:04:22 +03:00
|
|
|
validateUpdateQuery query = do
|
|
|
|
let source = uqSource query
|
2021-04-22 00:44:37 +03:00
|
|
|
tableCache :: TableCache ('Postgres 'Vanilla) <- askTableCache source
|
2021-09-24 01:56:37 +03:00
|
|
|
flip runTableCacheRT (source, tableCache) $
|
|
|
|
runDMLP1T $
|
|
|
|
validateUpdateQueryWith sessVarFromCurrentSetting (valueParserWithCollectableType binRHSBuilder) query
|
|
|
|
|
|
|
|
runUpdate ::
|
|
|
|
forall m.
|
|
|
|
( QErrM m,
|
|
|
|
UserInfoM m,
|
|
|
|
CacheRM m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadBaseControl IO m,
|
|
|
|
MonadIO m,
|
|
|
|
Tracing.MonadTrace m,
|
|
|
|
MetadataM m
|
|
|
|
) =>
|
|
|
|
UpdateQuery ->
|
|
|
|
m EncJSON
|
2021-06-11 06:26:50 +03:00
|
|
|
runUpdate q = do
|
2021-04-22 00:44:37 +03:00
|
|
|
sourceConfig <- askSourceConfig @('Postgres 'Vanilla) (uqSource q)
|
2021-06-11 06:26:50 +03:00
|
|
|
userInfo <- askUserInfo
|
2021-01-29 08:48:17 +03:00
|
|
|
strfyNum <- stringifyNum . _sccSQLGenCtx <$> askServerConfigCtx
|
2021-01-07 12:04:22 +03:00
|
|
|
validateUpdateQuery q
|
2021-09-15 23:45:49 +03:00
|
|
|
>>= runTxWithCtx (_pscExecCtx sourceConfig) Q.ReadWrite
|
2021-09-24 01:56:37 +03:00
|
|
|
. flip runReaderT emptyQueryTagsComment
|
|
|
|
. execUpdateQuery strfyNum userInfo
|