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)
|
2021-01-09 02:09:15 +03:00
|
|
|
import Hasura.Session
|
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
|
2021-01-07 12:04:22 +03:00
|
|
|
(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" $
|
2021-02-14 09:07:52 +03:00
|
|
|
convBoolExp fieldInfoMap selPerm rqlBE sessVarBldr (valueParserWithCollectableType prepValBldr)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
|
|
|
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)
|
2021-01-07 12:04:22 +03:00
|
|
|
=> DeleteQuery -> m (AnnDel 'Postgres, DS.Seq Q.PrepArg)
|
|
|
|
validateDeleteQ query = do
|
|
|
|
let source = doSource query
|
2021-02-14 09:07:52 +03:00
|
|
|
tableCache :: TableCache 'Postgres <- askTableCache source
|
2020-12-28 15:56:00 +03:00
|
|
|
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
|
2021-01-29 08:48:17 +03:00
|
|
|
, HasServerConfigCtx m, MonadIO m
|
2021-01-07 12:04:22 +03:00
|
|
|
, Tracing.MonadTrace m, MonadBaseControl IO m
|
2021-03-02 07:26:31 +03:00
|
|
|
, MetadataM m
|
2020-05-27 18:02:58 +03:00
|
|
|
)
|
2020-07-14 22:00:58 +03:00
|
|
|
=> Env.Environment
|
|
|
|
-> DeleteQuery
|
|
|
|
-> m EncJSON
|
2021-01-07 12:04:22 +03:00
|
|
|
runDelete env q = do
|
2021-01-20 03:31:53 +03:00
|
|
|
sourceConfig <- askSourceConfig (doSource q)
|
2021-01-29 08:48:17 +03:00
|
|
|
strfyNum <- stringifyNum . _sccSQLGenCtx <$> askServerConfigCtx
|
2021-01-07 12:04:22 +03:00
|
|
|
validateDeleteQ q
|
|
|
|
>>= runQueryLazyTx (_pscExecCtx sourceConfig) Q.ReadWrite
|
|
|
|
. execDeleteQuery env strfyNum Nothing
|