2022-01-06 12:49:03 +03:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
|
|
|
-- | Responsible for translating and building an MSSQL execution plan for
|
|
|
|
-- update mutations.
|
|
|
|
--
|
|
|
|
-- This module is used by "Hasura.Backends.MSSQL.Instances.Execute".
|
|
|
|
module Hasura.Backends.MSSQL.Execute.Update
|
|
|
|
( executeUpdate,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Database.MSSQL.Transaction qualified as Tx
|
|
|
|
import Hasura.Backends.MSSQL.Connection
|
|
|
|
import Hasura.Backends.MSSQL.FromIr as TSQL
|
2022-03-10 13:33:55 +03:00
|
|
|
import Hasura.Backends.MSSQL.FromIr.Constants (tempTableNameUpdated)
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.Expression (fromGBoolExp)
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.MutationResponse
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.SelectIntoTempTable qualified as TSQL
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.Update qualified as TSQL
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Backends.MSSQL.Plan
|
2022-02-07 17:11:49 +03:00
|
|
|
import Hasura.Backends.MSSQL.SQL.Error
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Backends.MSSQL.ToQuery as TQ
|
|
|
|
import Hasura.Backends.MSSQL.Types.Internal as TSQL
|
|
|
|
import Hasura.Backends.MSSQL.Types.Update
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.GraphQL.Parser
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR
|
|
|
|
import Hasura.RQL.IR qualified as IR
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.Session
|
|
|
|
|
|
|
|
-- | Executes an Update IR AST and return results as JSON.
|
|
|
|
executeUpdate ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
UserInfo ->
|
2022-02-23 23:17:58 +03:00
|
|
|
StringifyNumbers ->
|
2022-01-06 12:49:03 +03:00
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
AnnotatedUpdateG 'MSSQL Void (UnpreparedValue 'MSSQL) ->
|
|
|
|
m (ExceptT QErr IO EncJSON)
|
|
|
|
executeUpdate userInfo stringifyNum sourceConfig updateOperation = do
|
|
|
|
let mssqlExecCtx = (_mscExecCtx sourceConfig)
|
|
|
|
preparedUpdate <- traverse (prepareValueQuery $ _uiSession userInfo) updateOperation
|
|
|
|
if null $ updateOperations . _auBackend $ updateOperation
|
|
|
|
then pure $ pure $ IR.buildEmptyMutResp $ _auOutput preparedUpdate
|
2022-01-14 17:08:17 +03:00
|
|
|
else pure $ (mssqlRunReadWrite mssqlExecCtx) (buildUpdateTx preparedUpdate stringifyNum)
|
2022-01-06 12:49:03 +03:00
|
|
|
|
|
|
|
-- | Converts an Update IR AST to a transaction of three update sql statements.
|
|
|
|
--
|
|
|
|
-- A GraphQL update mutation does two things:
|
|
|
|
--
|
|
|
|
-- 1. Update rows in a table according to some predicate
|
|
|
|
-- 2. (Potentially) returns the updated rows (including relationships) as JSON
|
|
|
|
--
|
|
|
|
-- In order to complete these 2 things we need 3 SQL statements:
|
|
|
|
--
|
|
|
|
-- 1. @SELECT INTO <temp_table> WHERE <false>@ - creates a temporary table
|
|
|
|
-- with the same schema as the original table in which we'll store the updated rows
|
|
|
|
-- from the table we are deleting
|
|
|
|
-- 2. @UPDATE SET FROM with OUTPUT@ - updates the rows from the table and inserts the
|
|
|
|
-- updated rows to the temporary table from (1)
|
|
|
|
-- 3. @SELECT@ - constructs the @returning@ query from the temporary table, including
|
|
|
|
-- relationships with other tables.
|
|
|
|
buildUpdateTx ::
|
|
|
|
AnnotatedUpdate 'MSSQL ->
|
2022-02-23 23:17:58 +03:00
|
|
|
StringifyNumbers ->
|
2022-01-06 12:49:03 +03:00
|
|
|
Tx.TxET QErr IO EncJSON
|
|
|
|
buildUpdateTx updateOperation stringifyNum = do
|
|
|
|
let withAlias = "with_alias"
|
|
|
|
createInsertedTempTableQuery =
|
|
|
|
toQueryFlat $
|
|
|
|
TQ.fromSelectIntoTempTable $
|
|
|
|
TSQL.toSelectIntoTempTable tempTableNameUpdated (_auTable updateOperation) (_auAllCols updateOperation) RemoveConstraints
|
|
|
|
-- Create a temp table
|
2022-02-07 17:11:49 +03:00
|
|
|
Tx.unitQueryE defaultMSSQLTxErrorHandler createInsertedTempTableQuery
|
2022-01-06 12:49:03 +03:00
|
|
|
let updateQuery = TQ.fromUpdate <$> TSQL.fromUpdate updateOperation
|
2022-03-10 13:33:55 +03:00
|
|
|
updateQueryValidated <- toQueryFlat <$> runFromIr updateQuery
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Execute UPDATE statement
|
2022-02-07 17:11:49 +03:00
|
|
|
Tx.unitQueryE mutationMSSQLTxErrorHandler updateQueryValidated
|
2022-03-10 13:33:55 +03:00
|
|
|
mutationOutputSelect <- runFromIr $ mkMutationOutputSelect stringifyNum withAlias $ _auOutput updateOperation
|
2022-01-06 12:49:03 +03:00
|
|
|
let checkCondition = _auCheck updateOperation
|
|
|
|
-- The check constraint is translated to boolean expression
|
2022-03-10 13:33:55 +03:00
|
|
|
checkBoolExp <- runFromIr $ runReaderT (fromGBoolExp checkCondition) (EntityAlias withAlias)
|
2022-01-06 12:49:03 +03:00
|
|
|
|
|
|
|
let withSelect =
|
|
|
|
emptySelect
|
|
|
|
{ selectProjections = [StarProjection],
|
|
|
|
selectFrom = Just $ FromTempTable $ Aliased tempTableNameUpdated "updated_alias"
|
|
|
|
}
|
|
|
|
mutationOutputCheckConstraintSelect = selectMutationOutputAndCheckCondition withAlias mutationOutputSelect checkBoolExp
|
|
|
|
finalSelect = mutationOutputCheckConstraintSelect {selectWith = Just $ With $ pure $ Aliased withSelect withAlias}
|
|
|
|
|
|
|
|
-- Execute SELECT query to fetch mutation response and check constraint result
|
2022-02-07 17:11:49 +03:00
|
|
|
(responseText, checkConditionInt) <- Tx.singleRowQueryE defaultMSSQLTxErrorHandler (toQueryFlat $ TQ.fromSelect finalSelect)
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Drop the temp table
|
2022-02-07 17:11:49 +03:00
|
|
|
Tx.unitQueryE defaultMSSQLTxErrorHandler $ toQueryFlat $ dropTempTableQuery tempTableNameUpdated
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Raise an exception if the check condition is not met
|
|
|
|
unless (checkConditionInt == (0 :: Int)) $
|
|
|
|
throw400 PermissionError "check constraint of an update permission has failed"
|
|
|
|
pure $ encJFromText responseText
|