-- | This module defines the translation functions for update mutations.
module Hasura.Backends.MSSQL.FromIr.Update
  ( fromUpdate,
  )
where

import Hasura.Backends.MSSQL.FromIr
  ( FromIr,
    NameTemplate (TableTemplate),
    generateAlias,
  )
import Hasura.Backends.MSSQL.FromIr.Constants (tempTableNameUpdated)
import Hasura.Backends.MSSQL.FromIr.Expression (fromGBoolExp)
import Hasura.Backends.MSSQL.Instances.Types ()
import Hasura.Backends.MSSQL.Types.Internal
import Hasura.Backends.MSSQL.Types.Update
import Hasura.Prelude
import Hasura.RQL.IR qualified as IR
import Hasura.RQL.IR.Update.Batch qualified as IR
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Column qualified as IR

fromUpdate :: IR.AnnotatedUpdate 'MSSQL -> FromIr Update
fromUpdate :: AnnotatedUpdate 'MSSQL -> FromIr Update
fromUpdate (IR.AnnotatedUpdateG TableName 'MSSQL
table AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
updatePermFilter AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
_ (IR.UpdateBatch HashMap (Column 'MSSQL) (UpdateOperator Expression)
updateOperations AnnBoolExp 'MSSQL Expression
whereClause) MutationOutputG 'MSSQL Void (SQLExpression 'MSSQL)
_ [ColumnInfo 'MSSQL]
allColumns Maybe NamingCase
_tCase Maybe (ValidateInput ResolvedWebhook)
_validateInput) = do
  Text
tableAlias <- NameTemplate -> FromIr Text
generateAlias (Text -> NameTemplate
TableTemplate (TableName -> Text
tableName TableName 'MSSQL
TableName
table))
  ReaderT EntityAlias FromIr Update -> EntityAlias -> FromIr Update
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT
    ( do
        Expression
permissionsFilter <- AnnBoolExp 'MSSQL Expression
-> ReaderT EntityAlias FromIr Expression
fromGBoolExp AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
AnnBoolExp 'MSSQL Expression
updatePermFilter
        Expression
whereExpression <- AnnBoolExp 'MSSQL Expression
-> ReaderT EntityAlias FromIr Expression
fromGBoolExp AnnBoolExp 'MSSQL Expression
whereClause
        let columnNames :: [ColumnName]
columnNames = (ColumnInfo 'MSSQL -> ColumnName)
-> [ColumnInfo 'MSSQL] -> [ColumnName]
forall a b. (a -> b) -> [a] -> [b]
map ColumnInfo 'MSSQL -> Column 'MSSQL
ColumnInfo 'MSSQL -> ColumnName
forall (b :: BackendType). ColumnInfo b -> Column b
IR.ciColumn [ColumnInfo 'MSSQL]
allColumns
        Update -> ReaderT EntityAlias FromIr Update
forall a. a -> ReaderT EntityAlias FromIr a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          Update
            { updateTable :: Aliased TableName
updateTable =
                Aliased
                  { $sel:aliasedAlias:Aliased :: Text
aliasedAlias = Text
tableAlias,
                    $sel:aliasedThing:Aliased :: TableName
aliasedThing = TableName 'MSSQL
TableName
table
                  },
              updateSet :: UpdateSet
updateSet = HashMap (Column 'MSSQL) (UpdateOperator Expression)
UpdateSet
updateOperations,
              updateOutput :: UpdateOutput
updateOutput = Inserted -> [OutputColumn] -> UpdateOutput
forall t. t -> [OutputColumn] -> Output t
Output Inserted
Inserted ((ColumnName -> OutputColumn) -> [ColumnName] -> [OutputColumn]
forall a b. (a -> b) -> [a] -> [b]
map ColumnName -> OutputColumn
OutputColumn [ColumnName]
columnNames),
              updateTempTable :: TempTable
updateTempTable = TempTableName -> [ColumnName] -> TempTable
TempTable TempTableName
tempTableNameUpdated [ColumnName]
columnNames,
              updateWhere :: Where
updateWhere = [Expression] -> Where
Where [Expression
permissionsFilter, Expression
whereExpression]
            }
    )
    (Text -> EntityAlias
EntityAlias Text
tableAlias)