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
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Instances.TH.Lift ()
|
|
|
|
|
|
|
|
import qualified Data.Sequence as DS
|
2020-07-14 22:00:58 +03:00
|
|
|
import qualified Data.Environment as Env
|
2020-07-15 13:40:48 +03:00
|
|
|
import qualified Hasura.Tracing as Tracing
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DML.Internal
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.RQL.DML.Mutation
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.DML.Returning
|
|
|
|
import Hasura.RQL.GBoolExp
|
|
|
|
import Hasura.RQL.Types
|
2020-05-27 18:02:58 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Hasura.SQL.DML as S
|
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
data AnnDelG v
|
|
|
|
= AnnDel
|
2019-03-22 10:08:42 +03:00
|
|
|
{ dqp1Table :: !QualifiedTable
|
2019-04-17 12:48:41 +03:00
|
|
|
, dqp1Where :: !(AnnBoolExp v, AnnBoolExp v)
|
2020-02-13 20:38:23 +03:00
|
|
|
, dqp1Output :: !(MutationOutputG v)
|
2019-08-11 18:34:38 +03:00
|
|
|
, dqp1AllCols :: ![PGColumnInfo]
|
2018-06-27 16:11:32 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
traverseAnnDel
|
|
|
|
:: (Applicative f)
|
|
|
|
=> (a -> f b)
|
|
|
|
-> AnnDelG a
|
|
|
|
-> f (AnnDelG b)
|
|
|
|
traverseAnnDel f annUpd =
|
|
|
|
AnnDel tn
|
|
|
|
<$> ((,) <$> traverseAnnBoolExp f whr <*> traverseAnnBoolExp f fltr)
|
2020-02-13 20:38:23 +03:00
|
|
|
<*> traverseMutationOutput f mutOutput
|
2019-04-17 12:48:41 +03:00
|
|
|
<*> pure allCols
|
|
|
|
where
|
2020-02-13 20:38:23 +03:00
|
|
|
AnnDel tn (whr, fltr) mutOutput allCols = annUpd
|
2019-04-17 12:48:41 +03:00
|
|
|
|
|
|
|
type AnnDel = AnnDelG S.SQLExp
|
|
|
|
|
2019-03-07 13:24:07 +03:00
|
|
|
mkDeleteCTE
|
2019-04-17 12:48:41 +03:00
|
|
|
:: AnnDel -> S.CTE
|
|
|
|
mkDeleteCTE (AnnDel tn (fltr, wc) _ _) =
|
2019-03-07 13:24:07 +03:00
|
|
|
S.CTEDelete delete
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
delete = S.SQLDelete tn Nothing tableFltr $ Just S.returningStar
|
2018-11-16 15:40:23 +03:00
|
|
|
tableFltr = Just $ S.WhereFrag $
|
|
|
|
toSQLBoolExp (S.QualTable tn) $ andAnnBoolExps fltr wc
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
validateDeleteQWith
|
|
|
|
:: (UserInfoM m, QErrM m, CacheRM m)
|
2019-04-17 12:48:41 +03:00
|
|
|
=> SessVarBldr m
|
2019-07-22 15:47:13 +03:00
|
|
|
-> (PGColumnType -> Value -> m S.SQLExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
-> DeleteQuery
|
2019-04-17 12:48:41 +03:00
|
|
|
-> m AnnDel
|
|
|
|
validateDeleteQWith sessVarBldr prepValBldr
|
|
|
|
(DeleteQuery tableName rqlBE mRetCols) = do
|
2018-06-27 16:11:32 +03:00
|
|
|
tableInfo <- askTabInfo 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)
|
2019-04-17 12:48:41 +03:00
|
|
|
=> DeleteQuery -> m (AnnDel, DS.Seq Q.PrepArg)
|
2018-12-13 10:26:15 +03:00
|
|
|
validateDeleteQ =
|
2019-11-20 21:21:30 +03:00
|
|
|
runDMLP1T . validateDeleteQWith sessVarFromCurrentSetting binRHSBuilder
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
execDeleteQuery
|
2020-07-14 22:00:58 +03:00
|
|
|
::
|
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.MonadTrace m
|
2020-07-14 22:00:58 +03:00
|
|
|
)
|
|
|
|
=> Env.Environment
|
|
|
|
-> Bool
|
2020-05-27 18:02:58 +03:00
|
|
|
-> Maybe MutationRemoteJoinCtx
|
|
|
|
-> (AnnDel, DS.Seq Q.PrepArg)
|
|
|
|
-> m EncJSON
|
2020-07-14 22:00:58 +03:00
|
|
|
execDeleteQuery env strfyNum remoteJoinCtx (u, p) =
|
|
|
|
runMutation env $ mkMutation remoteJoinCtx (dqp1Table u) (deleteCTE, p)
|
2020-02-13 20:38:23 +03:00
|
|
|
(dqp1Output u) (dqp1AllCols u) strfyNum
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-03-07 13:24:07 +03:00
|
|
|
deleteCTE = mkDeleteCTE u
|
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
|
|
|
|
, MonadTx m, HasSQLGenCtx m, MonadIO m
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.MonadTrace m
|
2020-05-27 18:02:58 +03:00
|
|
|
)
|
2020-07-14 22:00:58 +03:00
|
|
|
=> Env.Environment
|
|
|
|
-> DeleteQuery
|
|
|
|
-> m EncJSON
|
|
|
|
runDelete env q = do
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> askSQLGenCtx
|
2020-07-14 22:00:58 +03:00
|
|
|
validateDeleteQ q >>= execDeleteQuery env strfyNum Nothing
|