-- | 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 as TSQL
import Hasura.Backends.MSSQL.Types.Update as TSQL (BackendUpdate (..), Update (..))
import Hasura.Prelude
import Hasura.RQL.IR qualified as IR
import Hasura.RQL.Types.Column qualified as IR
import Hasura.SQL.Backend

fromUpdate :: IR.AnnotatedUpdate 'MSSQL -> FromIr Update
fromUpdate :: AnnotatedUpdate 'MSSQL -> FromIr Update
fromUpdate (IR.AnnotatedUpdateG TableName 'MSSQL
table (AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
permFilter, AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
whereClause) AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
_ BackendUpdate 'MSSQL (SQLExpression 'MSSQL)
backendUpdate MutationOutputG 'MSSQL Void (SQLExpression 'MSSQL)
_ [ColumnInfo 'MSSQL]
allColumns Maybe NamingCase
_tCase) = 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 <- GBoolExp 'MSSQL (AnnBoolExpFld 'MSSQL Expression)
-> ReaderT EntityAlias FromIr Expression
fromGBoolExp AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
GBoolExp 'MSSQL (AnnBoolExpFld 'MSSQL Expression)
permFilter
        Expression
whereExpression <- GBoolExp 'MSSQL (AnnBoolExpFld 'MSSQL Expression)
-> ReaderT EntityAlias FromIr Expression
fromGBoolExp AnnBoolExp 'MSSQL (SQLExpression 'MSSQL)
GBoolExp 'MSSQL (AnnBoolExpFld 'MSSQL Expression)
whereClause
        let columnNames :: [ColumnName]
columnNames = (ColumnInfo 'MSSQL -> ColumnName)
-> [ColumnInfo 'MSSQL] -> [ColumnName]
forall a b. (a -> b) -> [a] -> [b]
map ColumnInfo 'MSSQL -> ColumnName
forall (b :: BackendType). ColumnInfo b -> Column b
IR.ciColumn [ColumnInfo 'MSSQL]
allColumns
        Update -> ReaderT EntityAlias FromIr Update
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          Update :: Aliased TableName
-> UpdateSet -> UpdateOutput -> TempTable -> Where -> Update
Update
            { updateTable :: Aliased TableName
updateTable =
                Aliased :: forall a. a -> Text -> Aliased a
Aliased
                  { $sel:aliasedAlias:Aliased :: Text
aliasedAlias = Text
tableAlias,
                    $sel:aliasedThing:Aliased :: TableName
aliasedThing = TableName 'MSSQL
TableName
table
                  },
              updateSet :: UpdateSet
updateSet = BackendUpdate Expression -> UpdateSet
forall v. BackendUpdate v -> HashMap ColumnName (UpdateOperator v)
updateOperations BackendUpdate 'MSSQL (SQLExpression 'MSSQL)
BackendUpdate Expression
backendUpdate,
              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)