{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

-- | MSSQL Instances Metadata
--
-- Defines a 'Hasura.RQL.Types.Metadata.Backend.BackendMetadata' type class instance for MSSQL.
module Hasura.Backends.MSSQL.Instances.Metadata () where

import Hasura.Backends.MSSQL.DDL qualified as MSSQL
import Hasura.Backends.MSSQL.Schema.Introspection qualified as MSSQL (listAllTables)
import Hasura.Base.Error (Code (UnexpectedPayload), throw400, throw500)
import Hasura.NativeQuery.InterpolatedQuery (trimQueryEnd)
import Hasura.NativeQuery.Metadata (NativeQueryMetadata (..))
import Hasura.NativeQuery.Validation (validateArgumentDeclaration)
import Hasura.Prelude
import Hasura.RQL.DDL.Relationship (defaultBuildArrayRelationshipInfo, defaultBuildObjectRelationshipInfo)
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Metadata.Backend

instance BackendMetadata 'MSSQL where
  prepareCatalog :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'MSSQL
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
prepareCatalog = MSSQLSourceConfig
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
SourceConfig 'MSSQL
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
MSSQLSourceConfig
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
MSSQL.prepareCatalog
  buildComputedFieldInfo :: forall (m :: * -> *).
MonadError QErr m =>
HashSet (TableName 'MSSQL)
-> TableName 'MSSQL
-> HashSet (Column 'MSSQL)
-> ComputedFieldName
-> ComputedFieldDefinition 'MSSQL
-> RawFunctionInfo 'MSSQL
-> Comment
-> m (ComputedFieldInfo 'MSSQL)
buildComputedFieldInfo = HashSet (TableName 'MSSQL)
-> TableName 'MSSQL
-> HashSet (Column 'MSSQL)
-> ComputedFieldName
-> ComputedFieldDefinition 'MSSQL
-> RawFunctionInfo 'MSSQL
-> Comment
-> m (ComputedFieldInfo 'MSSQL)
forall (m :: * -> *).
MonadError QErr m =>
HashSet (TableName 'MSSQL)
-> TableName 'MSSQL
-> HashSet (Column 'MSSQL)
-> ComputedFieldName
-> ComputedFieldDefinition 'MSSQL
-> RawFunctionInfo 'MSSQL
-> Comment
-> m (ComputedFieldInfo 'MSSQL)
MSSQL.buildComputedFieldInfo
  fetchAndValidateEnumValues :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'MSSQL
-> TableName 'MSSQL
-> Maybe (PrimaryKey 'MSSQL (RawColumnInfo 'MSSQL))
-> [RawColumnInfo 'MSSQL]
-> m (Either QErr EnumValues)
fetchAndValidateEnumValues = SourceConfig 'MSSQL
-> TableName 'MSSQL
-> Maybe (PrimaryKey 'MSSQL (RawColumnInfo 'MSSQL))
-> [RawColumnInfo 'MSSQL]
-> m (Either QErr EnumValues)
forall (m :: * -> *).
Monad m =>
SourceConfig 'MSSQL
-> TableName 'MSSQL
-> Maybe (PrimaryKey 'MSSQL (RawColumnInfo 'MSSQL))
-> [RawColumnInfo 'MSSQL]
-> m (Either QErr EnumValues)
MSSQL.fetchAndValidateEnumValues
  resolveSourceConfig :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadResolveSource m) =>
SourceName
-> SourceConnConfiguration 'MSSQL
-> BackendSourceKind 'MSSQL
-> BackendInfo 'MSSQL
-> Environment
-> Manager
-> m (Either QErr (SourceConfig 'MSSQL))
resolveSourceConfig = SourceName
-> MSSQLConnConfiguration
-> BackendSourceKind 'MSSQL
-> BackendConfig 'MSSQL
-> Environment
-> Manager
-> m (Either QErr MSSQLSourceConfig)
SourceName
-> SourceConnConfiguration 'MSSQL
-> BackendSourceKind 'MSSQL
-> BackendInfo 'MSSQL
-> Environment
-> Manager
-> m (Either QErr (SourceConfig 'MSSQL))
forall (m :: * -> *) manager.
(MonadIO m, MonadResolveSource m) =>
SourceName
-> MSSQLConnConfiguration
-> BackendSourceKind 'MSSQL
-> BackendConfig 'MSSQL
-> Environment
-> manager
-> m (Either QErr MSSQLSourceConfig)
MSSQL.resolveSourceConfig
  resolveDatabaseMetadata :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadResolveSource m) =>
Logger Hasura
-> SourceMetadata 'MSSQL
-> SourceConfig 'MSSQL
-> m (Either QErr (DBObjectsIntrospection 'MSSQL))
resolveDatabaseMetadata Logger Hasura
_ SourceMetadata 'MSSQL
_ = MSSQLSourceConfig
-> m (Either QErr (DBObjectsIntrospection 'MSSQL))
SourceConfig 'MSSQL
-> m (Either QErr (DBObjectsIntrospection 'MSSQL))
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
MSSQLSourceConfig
-> m (Either QErr (DBObjectsIntrospection 'MSSQL))
MSSQL.resolveDatabaseMetadata
  parseBoolExpOperations :: forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'MSSQL m v
-> FieldInfoMap (FieldInfo 'MSSQL)
-> FieldInfoMap (FieldInfo 'MSSQL)
-> ColumnReference 'MSSQL
-> Value
-> m [OpExpG 'MSSQL v]
parseBoolExpOperations = ValueParser 'MSSQL m v
-> FieldInfoMap (FieldInfo 'MSSQL)
-> FieldInfoMap (FieldInfo 'MSSQL)
-> ColumnReference 'MSSQL
-> Value
-> m [OpExpG 'MSSQL v]
forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'MSSQL m v
-> FieldInfoMap (FieldInfo 'MSSQL)
-> FieldInfoMap (FieldInfo 'MSSQL)
-> ColumnReference 'MSSQL
-> Value
-> m [OpExpG 'MSSQL v]
MSSQL.parseBoolExpOperations
  buildArrayRelationshipInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig 'MSSQL
-> SourceName
-> HashMap (TableName 'MSSQL) (HashSet (ForeignKey 'MSSQL))
-> TableName 'MSSQL
-> ArrRelDef 'MSSQL
-> m (RelInfo 'MSSQL, Seq SchemaDependency)
buildArrayRelationshipInfo SourceConfig 'MSSQL
_ = SourceName
-> HashMap (TableName 'MSSQL) (HashSet (ForeignKey 'MSSQL))
-> TableName 'MSSQL
-> ArrRelDef 'MSSQL
-> m (RelInfo 'MSSQL, Seq SchemaDependency)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, Backend b) =>
SourceName
-> HashMap (TableName b) (HashSet (ForeignKey b))
-> TableName b
-> ArrRelDef b
-> m (RelInfo b, Seq SchemaDependency)
defaultBuildArrayRelationshipInfo
  buildObjectRelationshipInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig 'MSSQL
-> SourceName
-> HashMap (TableName 'MSSQL) (HashSet (ForeignKey 'MSSQL))
-> TableName 'MSSQL
-> ObjRelDef 'MSSQL
-> m (RelInfo 'MSSQL, Seq SchemaDependency)
buildObjectRelationshipInfo SourceConfig 'MSSQL
_ = SourceName
-> HashMap (TableName 'MSSQL) (HashSet (ForeignKey 'MSSQL))
-> TableName 'MSSQL
-> ObjRelDef 'MSSQL
-> m (RelInfo 'MSSQL, Seq SchemaDependency)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, Backend b) =>
SourceName
-> HashMap (TableName b) (HashSet (ForeignKey b))
-> TableName b
-> ObjRelDef b
-> m (RelInfo b, Seq SchemaDependency)
defaultBuildObjectRelationshipInfo
  buildFunctionInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName 'MSSQL
-> SystemDefined
-> FunctionConfig 'MSSQL
-> FunctionPermissionsMap
-> RawFunctionInfo 'MSSQL
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'MSSQL, SchemaDependency)
buildFunctionInfo = SourceName
-> FunctionName 'MSSQL
-> SystemDefined
-> FunctionConfig 'MSSQL
-> FunctionPermissionsMap
-> RawFunctionInfo 'MSSQL
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'MSSQL, SchemaDependency)
forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName 'MSSQL
-> SystemDefined
-> FunctionConfig 'MSSQL
-> FunctionPermissionsMap
-> RawFunctionInfo 'MSSQL
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'MSSQL, SchemaDependency)
MSSQL.buildFunctionInfo
  updateColumnInEventTrigger :: TableName 'MSSQL
-> Column 'MSSQL
-> Column 'MSSQL
-> TableName 'MSSQL
-> EventTriggerConf 'MSSQL
-> EventTriggerConf 'MSSQL
updateColumnInEventTrigger = TableName 'MSSQL
-> Column 'MSSQL
-> Column 'MSSQL
-> TableName 'MSSQL
-> EventTriggerConf 'MSSQL
-> EventTriggerConf 'MSSQL
MSSQL.updateColumnInEventTrigger
  parseCollectableType :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has (ScalarTypeParsingContext 'MSSQL) r) =>
CollectableType (ColumnType 'MSSQL)
-> Value -> m (PartialSQLExp 'MSSQL)
parseCollectableType = CollectableType (ColumnType 'MSSQL)
-> Value -> m (PartialSQLExp 'MSSQL)
forall (m :: * -> *).
MonadError QErr m =>
CollectableType (ColumnType 'MSSQL)
-> Value -> m (PartialSQLExp 'MSSQL)
MSSQL.parseCollectableType
  postDropSourceHook :: forall (m :: * -> *).
(MonadError QErr m, MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'MSSQL -> TableEventTriggers 'MSSQL -> m ()
postDropSourceHook = MSSQLSourceConfig -> TableEventTriggers 'MSSQL -> m ()
SourceConfig 'MSSQL -> TableEventTriggers 'MSSQL -> m ()
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
MSSQLSourceConfig -> TableEventTriggers 'MSSQL -> m ()
MSSQL.postDropSourceHook
  buildComputedFieldBooleanExp :: forall (m :: * -> *) v.
(MonadError QErr m, TableCoreInfoRM 'MSSQL m) =>
BoolExpResolver 'MSSQL m v
-> BoolExpRHSParser 'MSSQL m v
-> FieldInfoMap (FieldInfo 'MSSQL)
-> FieldInfoMap (FieldInfo 'MSSQL)
-> ComputedFieldInfo 'MSSQL
-> Value
-> m (AnnComputedFieldBoolExp 'MSSQL v)
buildComputedFieldBooleanExp BoolExpResolver 'MSSQL m v
_ BoolExpRHSParser 'MSSQL m v
_ FieldInfoMap (FieldInfo 'MSSQL)
_ FieldInfoMap (FieldInfo 'MSSQL)
_ ComputedFieldInfo 'MSSQL
_ Value
_ =
    Text -> m (AnnComputedFieldBoolExp 'MSSQL v)
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"Computed fields are not yet defined for MSSQL backends"
  supportsBeingRemoteRelationshipTarget :: SourceConfig 'MSSQL -> Bool
supportsBeingRemoteRelationshipTarget SourceConfig 'MSSQL
_ = Bool
True
  listAllTables :: forall (m :: * -> *) r.
(CacheRM m, MonadBaseControl IO m, MetadataM m, MonadError QErr m,
 MonadIO m, MonadReader r m, Has (Logger Hasura) r,
 ProvidesNetwork m) =>
SourceName -> m [TableName 'MSSQL]
listAllTables = SourceName -> m [TableName 'MSSQL]
SourceName -> m [TableName]
forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadBaseControl IO m, MonadError QErr m,
 MonadIO m) =>
SourceName -> m [TableName]
MSSQL.listAllTables
  listAllTrackables :: forall (m :: * -> *) r.
(CacheRM m, MonadBaseControl IO m, MetadataM m, MonadError QErr m,
 MonadIO m, MonadReader r m, Has (Logger Hasura) r,
 ProvidesNetwork m) =>
SourceName -> m (TrackableInfo 'MSSQL)
listAllTrackables SourceName
_ =
    Text -> m (TrackableInfo 'MSSQL)
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"Computed fields are not yet defined for MSSQL backends"
  getTableInfo :: forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m, MonadBaseControl IO m,
 MonadIO m) =>
SourceName
-> TableName 'MSSQL -> m (Maybe (SourceTableInfo 'MSSQL))
getTableInfo SourceName
_ TableName 'MSSQL
_ = Code -> Text -> m (Maybe (SourceTableInfo 'MSSQL))
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
UnexpectedPayload Text
"get_table_info not yet supported in MSSQL!"
  validateNativeQuery :: forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
Environment
-> SourceConnConfiguration 'MSSQL
-> LogicalModelInfo 'MSSQL
-> NativeQueryMetadata 'MSSQL
-> m (InterpolatedQuery ArgumentName)
validateNativeQuery Environment
_ SourceConnConfiguration 'MSSQL
_ LogicalModelInfo 'MSSQL
_ NativeQueryMetadata 'MSSQL
nq = do
    NativeQueryMetadata 'MSSQL -> m ()
forall (m :: * -> *) (b :: BackendType).
(MonadIO m, MonadError QErr m) =>
NativeQueryMetadata b -> m ()
validateArgumentDeclaration NativeQueryMetadata 'MSSQL
nq
    InterpolatedQuery ArgumentName
-> m (InterpolatedQuery ArgumentName)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InterpolatedQuery ArgumentName -> InterpolatedQuery ArgumentName
forall var. InterpolatedQuery var -> InterpolatedQuery var
trimQueryEnd (NativeQueryMetadata 'MSSQL -> InterpolatedQuery ArgumentName
forall (b :: BackendType).
NativeQueryMetadata b -> InterpolatedQuery ArgumentName
_nqmCode NativeQueryMetadata 'MSSQL
nq)) -- for now, all queries are valid
  validateStoredProcedure :: forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
Environment
-> SourceConnConfiguration 'MSSQL
-> LogicalModelInfo 'MSSQL
-> StoredProcedureMetadata 'MSSQL
-> m ()
validateStoredProcedure Environment
_ SourceConnConfiguration 'MSSQL
_ LogicalModelInfo 'MSSQL
_ StoredProcedureMetadata 'MSSQL
_ = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure () -- for now, all stored procedures are valid
  getStoredProcedureGraphqlName :: forall (m :: * -> *).
MonadError QErr m =>
FunctionName 'MSSQL -> StoredProcedureConfig -> m Name
getStoredProcedureGraphqlName = FunctionName 'MSSQL -> StoredProcedureConfig -> m Name
FunctionName -> StoredProcedureConfig -> m Name
forall (m :: * -> *).
MonadError QErr m =>
FunctionName -> StoredProcedureConfig -> m Name
MSSQL.getStoredProcedureGraphqlName