2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.RQL.DML.Delete
|
|
|
|
( validateDeleteQWith
|
|
|
|
, validateDeleteQ
|
2019-04-17 12:48:41 +03:00
|
|
|
, AnnDelG(..)
|
|
|
|
, traverseAnnDel
|
|
|
|
, AnnDel
|
2020-05-27 18:02:58 +03:00
|
|
|
, execDeleteQuery
|
2018-12-13 10:26:15 +03:00
|
|
|
, runDelete
|
|
|
|
) where
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import qualified Data.Environment as Env
|
|
|
|
import qualified Data.Sequence as DS
|
|
|
|
import qualified Database.PG.Query as Q
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Aeson
|
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import qualified Hasura.Backends.Postgres.SQL.DML as S
|
|
|
|
import qualified Hasura.Tracing as Tracing
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Backends.Postgres.Execute.Mutation
|
|
|
|
import Hasura.Backends.Postgres.Translate.Returning
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.DML.Internal
|
2020-11-12 12:25:48 +03:00
|
|
|
import Hasura.RQL.DML.Types
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.RQL.IR.Delete
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-12-28 15:56:00 +03:00
|
|
|
import Hasura.RQL.Types.Run
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
validateDeleteQWith
|
2020-12-28 15:56:00 +03:00
|
|
|
:: (UserInfoM m, QErrM m, TableInfoRM 'Postgres m)
|
2020-10-22 23:42:27 +03:00
|
|
|
=> SessVarBldr 'Postgres m
|
|
|
|
-> (ColumnType 'Postgres -> Value -> m S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
-> DeleteQuery
|
2020-10-22 23:42:27 +03:00
|
|
|
-> m (AnnDel 'Postgres)
|
2019-04-17 12:48:41 +03:00
|
|
|
validateDeleteQWith sessVarBldr prepValBldr
|
|
|
|
(DeleteQuery tableName rqlBE mRetCols) = do
|
2020-12-28 15:56:00 +03:00
|
|
|
tableInfo <- 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 table is view then check if it deletable
|
|
|
|
mutableView tableName viIsDeletable
|
2019-11-20 21:21:30 +03:00
|
|
|
(_tciViewInfo coreInfo) "deletable"
|
2018-10-12 15:06:12 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
-- Check if the role has delete permissions
|
|
|
|
delPerm <- askDelPermInfo tableInfo
|
|
|
|
|
|
|
|
-- Check if all dependent headers are present
|
|
|
|
validateHeaders $ dpiRequiredHeaders delPerm
|
|
|
|
|
|
|
|
-- Check if select is allowed
|
|
|
|
selPerm <- modifyErr (<> selNecessaryMsg) $
|
|
|
|
askSelPermInfo tableInfo
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
let fieldInfoMap = _tciFieldInfoMap coreInfo
|
2019-03-22 10:08:42 +03:00
|
|
|
allCols = getCols fieldInfoMap
|
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
|
|
|
|
|
|
|
-- convert the where clause
|
|
|
|
annSQLBoolExp <- withPathK "where" $
|
2019-04-17 12:48:41 +03:00
|
|
|
convBoolExp fieldInfoMap selPerm rqlBE sessVarBldr prepValBldr
|
|
|
|
|
|
|
|
resolvedDelFltr <- convAnnBoolExpPartialSQL sessVarBldr $
|
|
|
|
dpiFilter delPerm
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
return $ AnnDel tableName
|
|
|
|
(resolvedDelFltr, annSQLBoolExp)
|
2019-03-22 10:08:42 +03:00
|
|
|
(mkDefaultMutFlds mAnnRetCols) allCols
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
where
|
|
|
|
selNecessaryMsg =
|
|
|
|
"; \"delete\" is only allowed if the role "
|
|
|
|
<> "has \"select\" permission as \"where\" can't be used "
|
|
|
|
<> "without \"select\" permission on the table"
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
validateDeleteQ
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (QErrM m, UserInfoM m, CacheRM m)
|
2020-12-28 15:56:00 +03:00
|
|
|
=> SourceName -> DeleteQuery -> m (AnnDel 'Postgres, DS.Seq Q.PrepArg)
|
|
|
|
validateDeleteQ source query = do
|
|
|
|
tableCache <- askTableCache source
|
|
|
|
flip runTableCacheRT (source, tableCache) $ runDMLP1T $
|
|
|
|
validateDeleteQWith sessVarFromCurrentSetting binRHSBuilder query
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runDelete
|
2020-05-27 18:02:58 +03:00
|
|
|
:: ( HasVersion, QErrM m, UserInfoM m, CacheRM m
|
2020-12-28 15:56:00 +03:00
|
|
|
, HasSQLGenCtx m, MonadIO m
|
|
|
|
, MonadBaseControl IO m, Tracing.MonadTrace m
|
2020-05-27 18:02:58 +03:00
|
|
|
)
|
2020-07-14 22:00:58 +03:00
|
|
|
=> Env.Environment
|
2020-12-28 15:56:00 +03:00
|
|
|
-> SourceName
|
2020-07-14 22:00:58 +03:00
|
|
|
-> DeleteQuery
|
|
|
|
-> m EncJSON
|
2020-12-28 15:56:00 +03:00
|
|
|
runDelete env source q = do
|
|
|
|
sourceConfig <- _pcConfiguration <$> askPGSourceCache source
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> askSQLGenCtx
|
2020-12-28 15:56:00 +03:00
|
|
|
validateDeleteQ source q >>= liftEitherM . runExceptT .
|
|
|
|
runQueryLazyTx (_pscExecCtx sourceConfig) Q.ReadWrite . execDeleteQuery env strfyNum Nothing
|