2019-03-07 13:24:07 +03:00
|
|
|
module Hasura.RQL.DML.Mutation
|
2020-05-27 18:02:58 +03:00
|
|
|
( Mutation
|
|
|
|
, mkMutation
|
|
|
|
, MutationRemoteJoinCtx
|
2019-03-07 13:24:07 +03:00
|
|
|
, runMutation
|
2020-05-27 18:02:58 +03:00
|
|
|
, executeMutationOutputQuery
|
2019-03-07 13:24:07 +03:00
|
|
|
, mutateAndFetchCols
|
2019-03-22 10:08:42 +03:00
|
|
|
, mkSelCTEFromColVals
|
2019-03-07 13:24:07 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2019-09-14 09:01:06 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Data.Sequence as DS
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Types as N
|
2019-09-14 09:01:06 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
import qualified Hasura.SQL.DML as S
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.RQL.DML.Internal
|
2020-05-27 18:02:58 +03:00
|
|
|
import Hasura.RQL.DML.RemoteJoin
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.RQL.DML.Returning
|
|
|
|
import Hasura.RQL.DML.Select
|
2020-05-27 18:02:58 +03:00
|
|
|
import Hasura.RQL.Instances ()
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-05-27 18:02:58 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
|
|
|
import Hasura.Session
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
import Hasura.SQL.Value
|
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
type MutationRemoteJoinCtx = (HTTP.Manager, [N.Header], UserInfo)
|
|
|
|
|
2019-03-07 13:24:07 +03:00
|
|
|
data Mutation
|
|
|
|
= Mutation
|
2020-05-27 18:02:58 +03:00
|
|
|
{ _mTable :: !QualifiedTable
|
|
|
|
, _mQuery :: !(S.CTE, DS.Seq Q.PrepArg)
|
|
|
|
, _mOutput :: !MutationOutput
|
|
|
|
, _mCols :: ![PGColumnInfo]
|
|
|
|
, _mRemoteJoins :: !(Maybe (RemoteJoins, MutationRemoteJoinCtx))
|
|
|
|
, _mStrfyNum :: !Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
mkMutation
|
|
|
|
:: Maybe MutationRemoteJoinCtx
|
|
|
|
-> QualifiedTable
|
|
|
|
-> (S.CTE, DS.Seq Q.PrepArg)
|
|
|
|
-> MutationOutput
|
|
|
|
-> [PGColumnInfo]
|
|
|
|
-> Bool
|
|
|
|
-> Mutation
|
|
|
|
mkMutation ctx table query output' allCols strfyNum =
|
|
|
|
let (output, remoteJoins) = getRemoteJoinsMutationOutput output'
|
|
|
|
remoteJoinsCtx = (,) <$> remoteJoins <*> ctx
|
|
|
|
in Mutation table query output allCols remoteJoinsCtx strfyNum
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
runMutation
|
|
|
|
:: (HasVersion, MonadTx m, MonadIO m)
|
|
|
|
=> Mutation -> m EncJSON
|
2019-03-07 13:24:07 +03:00
|
|
|
runMutation mut =
|
|
|
|
bool (mutateAndReturn mut) (mutateAndSel mut) $
|
2020-02-13 20:38:23 +03:00
|
|
|
hasNestedFld $ _mOutput mut
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
mutateAndReturn
|
|
|
|
:: (HasVersion, MonadTx m, MonadIO m)
|
|
|
|
=> Mutation -> m EncJSON
|
|
|
|
mutateAndReturn (Mutation qt (cte, p) mutationOutput allCols remoteJoins strfyNum) =
|
|
|
|
executeMutationOutputQuery sqlQuery (toList p) remoteJoins
|
2019-03-07 13:24:07 +03:00
|
|
|
where
|
2020-05-27 18:02:58 +03:00
|
|
|
sqlQuery = Q.fromBuilder $ toSQL $
|
|
|
|
mkMutationOutputExp qt allCols Nothing cte mutationOutput strfyNum
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
mutateAndSel
|
|
|
|
:: (HasVersion, MonadTx m, MonadIO m)
|
|
|
|
=> Mutation -> m EncJSON
|
|
|
|
mutateAndSel (Mutation qt q mutationOutput allCols remoteJoins strfyNum) = do
|
2019-03-07 13:24:07 +03:00
|
|
|
-- Perform mutation and fetch unique columns
|
2020-05-27 18:02:58 +03:00
|
|
|
MutateResp _ columnVals <- liftTx $ mutateAndFetchCols qt allCols q strfyNum
|
2020-02-04 18:34:17 +03:00
|
|
|
selCTE <- mkSelCTEFromColVals qt allCols columnVals
|
2020-03-10 15:45:47 +03:00
|
|
|
let selWith = mkMutationOutputExp qt allCols Nothing selCTE mutationOutput strfyNum
|
2019-03-07 13:24:07 +03:00
|
|
|
-- Perform select query and fetch returning fields
|
2020-05-27 18:02:58 +03:00
|
|
|
executeMutationOutputQuery (Q.fromBuilder $ toSQL selWith) [] remoteJoins
|
|
|
|
|
|
|
|
executeMutationOutputQuery
|
|
|
|
:: (HasVersion, MonadTx m, MonadIO m)
|
|
|
|
=> Q.Query -- ^ SQL query
|
|
|
|
-> [Q.PrepArg] -- ^ Prepared params
|
|
|
|
-> Maybe (RemoteJoins, MutationRemoteJoinCtx) -- ^ Remote joins context
|
|
|
|
-> m EncJSON
|
|
|
|
executeMutationOutputQuery query prepArgs = \case
|
|
|
|
Nothing ->
|
|
|
|
runIdentity . Q.getRow
|
|
|
|
<$> liftTx (Q.rawQE dmlTxErrorHandler query prepArgs True)
|
|
|
|
Just (remoteJoins, (httpManager, reqHeaders, userInfo)) ->
|
|
|
|
executeQueryWithRemoteJoins httpManager reqHeaders userInfo query prepArgs remoteJoins
|
2019-03-07 13:24:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
mutateAndFetchCols
|
2020-02-04 18:34:17 +03:00
|
|
|
:: QualifiedTable
|
2019-08-11 18:34:38 +03:00
|
|
|
-> [PGColumnInfo]
|
2019-03-07 13:24:07 +03:00
|
|
|
-> (S.CTE, DS.Seq Q.PrepArg)
|
|
|
|
-> Bool
|
2020-02-04 18:34:17 +03:00
|
|
|
-> Q.TxE QErr (MutateResp TxtEncodedPGVal)
|
2019-03-07 13:24:07 +03:00
|
|
|
mutateAndFetchCols qt cols (cte, p) strfyNum =
|
|
|
|
Q.getAltJ . runIdentity . Q.getRow
|
|
|
|
<$> Q.rawQE dmlTxErrorHandler (Q.fromBuilder sql) (toList p) True
|
|
|
|
where
|
|
|
|
aliasIden = Iden $ qualObjectToText qt <> "__mutation_result"
|
2019-10-18 11:29:47 +03:00
|
|
|
tabFrom = FromIden aliasIden
|
2019-03-07 13:24:07 +03:00
|
|
|
tabPerm = TablePerm annBoolExpTrue Nothing
|
|
|
|
selFlds = flip map cols $
|
2019-11-07 08:14:36 +03:00
|
|
|
\ci -> (fromPGCol $ pgiColumn ci, mkAnnColFieldAsText ci)
|
2019-03-07 13:24:07 +03:00
|
|
|
|
|
|
|
sql = toSQL selectWith
|
|
|
|
selectWith = S.SelectWith [(S.Alias aliasIden, cte)] select
|
|
|
|
select = S.mkSelect {S.selExtr = [S.Extractor extrExp Nothing]}
|
|
|
|
extrExp = S.applyJsonBuildObj
|
|
|
|
[ S.SELit "affected_rows", affRowsSel
|
|
|
|
, S.SELit "returning_columns", colSel
|
|
|
|
]
|
|
|
|
|
|
|
|
affRowsSel = S.SESelect $
|
|
|
|
S.mkSelect
|
|
|
|
{ S.selExtr = [S.Extractor S.countStar Nothing]
|
|
|
|
, S.selFrom = Just $ S.FromExp [S.FIIden aliasIden]
|
|
|
|
}
|
2020-02-13 20:38:23 +03:00
|
|
|
colSel = S.SESelect $ mkSQLSelect JASMultipleRows $
|
2019-03-07 13:24:07 +03:00
|
|
|
AnnSelG selFlds tabFrom tabPerm noTableArgs strfyNum
|
2019-03-22 10:08:42 +03:00
|
|
|
|
2020-02-04 18:34:17 +03:00
|
|
|
-- | Note:- Using sorted columns is necessary to enable casting the rows returned by VALUES expression to table type.
|
|
|
|
-- For example, let's consider the table, `CREATE TABLE test (id serial primary key, name text not null, age int)`.
|
|
|
|
-- The generated values expression should be in order of columns;
|
|
|
|
-- `SELECT ("row"::table).* VALUES (1, 'Robert', 23) AS "row"`.
|
2019-03-22 10:08:42 +03:00
|
|
|
mkSelCTEFromColVals
|
2019-09-14 09:01:06 +03:00
|
|
|
:: (MonadError QErr m)
|
2020-02-04 18:34:17 +03:00
|
|
|
=> QualifiedTable -> [PGColumnInfo] -> [ColumnValues TxtEncodedPGVal] -> m S.CTE
|
|
|
|
mkSelCTEFromColVals qt allCols colVals =
|
2019-03-22 10:08:42 +03:00
|
|
|
S.CTESelect <$> case colVals of
|
|
|
|
[] -> return selNoRows
|
|
|
|
_ -> do
|
|
|
|
tuples <- mapM mkTupsFromColVal colVals
|
2020-02-04 18:34:17 +03:00
|
|
|
let fromItem = S.FIValues (S.ValuesExp tuples) (S.Alias rowAlias) Nothing
|
2019-03-22 10:08:42 +03:00
|
|
|
return S.mkSelect
|
2020-02-04 18:34:17 +03:00
|
|
|
{ S.selExtr = [extractor]
|
2019-03-22 10:08:42 +03:00
|
|
|
, S.selFrom = Just $ S.FromExp [fromItem]
|
|
|
|
}
|
|
|
|
where
|
2020-02-04 18:34:17 +03:00
|
|
|
rowAlias = Iden "row"
|
|
|
|
extractor = S.selectStar' $ S.QualIden rowAlias $ Just $ S.TypeAnn $ toSQLTxt qt
|
2020-03-10 15:45:47 +03:00
|
|
|
sortedCols = sortCols allCols
|
2019-03-22 10:08:42 +03:00
|
|
|
mkTupsFromColVal colVal =
|
2020-02-04 18:34:17 +03:00
|
|
|
fmap S.TupleExp $ forM sortedCols $ \ci -> do
|
2019-09-19 07:47:36 +03:00
|
|
|
let pgCol = pgiColumn ci
|
2019-03-22 10:08:42 +03:00
|
|
|
val <- onNothing (Map.lookup pgCol colVal) $
|
|
|
|
throw500 $ "column " <> pgCol <<> " not found in returning values"
|
2020-02-04 18:34:17 +03:00
|
|
|
pure $ txtEncodedToSQLExp (pgiType ci) val
|
2019-03-22 10:08:42 +03:00
|
|
|
|
|
|
|
selNoRows =
|
|
|
|
S.mkSelect { S.selExtr = [S.selectStar]
|
|
|
|
, S.selFrom = Just $ S.mkSimpleFromExp qt
|
|
|
|
, S.selWhere = Just $ S.WhereFrag $ S.BELit False
|
|
|
|
}
|
2020-02-04 18:34:17 +03:00
|
|
|
|
|
|
|
txtEncodedToSQLExp colTy = \case
|
|
|
|
TENull -> S.SENull
|
|
|
|
TELit textValue ->
|
|
|
|
S.withTyAnn (unsafePGColumnToRepresentation colTy) $ S.SELit textValue
|