2022-01-11 01:54:51 +03:00
|
|
|
-- | MSSQL DDL
|
|
|
|
--
|
|
|
|
-- Implements the DDL related methods of the
|
|
|
|
-- 'Hasura.RQL.Types.Metadata.Backend.BackendMetadata' type class
|
|
|
|
-- for the MSSQL backend, which provides an interface for fetching information about
|
|
|
|
-- the objects in the database, such as tables, relationships, etc.
|
|
|
|
--
|
|
|
|
-- The actual instance is defined in "Hasura.Backends.MSSQL.Instances.Metadata".
|
2021-02-23 20:37:27 +03:00
|
|
|
module Hasura.Backends.MSSQL.DDL
|
2021-09-24 01:56:37 +03:00
|
|
|
( buildComputedFieldInfo,
|
|
|
|
fetchAndValidateEnumValues,
|
|
|
|
buildFunctionInfo,
|
|
|
|
updateColumnInEventTrigger,
|
|
|
|
parseCollectableType,
|
|
|
|
module M,
|
2021-02-23 20:37:27 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Hasura.Backends.MSSQL.DDL.BoolExp as M
|
|
|
|
import Hasura.Backends.MSSQL.DDL.Source as M
|
2021-11-26 16:47:12 +03:00
|
|
|
import Hasura.Backends.MSSQL.Types.Internal qualified as MT
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.ComputedField
|
|
|
|
import Hasura.RQL.Types.EventTrigger
|
|
|
|
import Hasura.RQL.Types.Function
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
2022-05-26 14:54:30 +03:00
|
|
|
import Hasura.RQL.Types.SourceCustomization (NamingCase)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.Types.Table
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
import Hasura.Server.Utils
|
|
|
|
import Hasura.Session
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
buildComputedFieldInfo ::
|
|
|
|
(MonadError QErr m) =>
|
|
|
|
HashSet (TableName 'MSSQL) ->
|
|
|
|
TableName 'MSSQL ->
|
2022-05-25 13:24:41 +03:00
|
|
|
HashSet (Column 'MSSQL) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
ComputedFieldName ->
|
|
|
|
ComputedFieldDefinition 'MSSQL ->
|
|
|
|
RawFunctionInfo 'MSSQL ->
|
2022-02-16 02:16:34 +03:00
|
|
|
Comment ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (ComputedFieldInfo 'MSSQL)
|
2022-05-25 13:24:41 +03:00
|
|
|
buildComputedFieldInfo _ _ _ _ _ _ _ =
|
2021-02-23 20:37:27 +03:00
|
|
|
throw400 NotSupported "Computed fields aren't supported for MSSQL sources"
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
fetchAndValidateEnumValues ::
|
|
|
|
(Monad m) =>
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
Maybe (PrimaryKey 'MSSQL (RawColumnInfo 'MSSQL)) ->
|
|
|
|
[RawColumnInfo 'MSSQL] ->
|
|
|
|
m (Either QErr EnumValues)
|
|
|
|
fetchAndValidateEnumValues _ _ _ _ =
|
|
|
|
runExceptT $
|
|
|
|
throw400 NotSupported "Enum tables are not supported for MSSQL sources"
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
buildFunctionInfo ::
|
|
|
|
(MonadError QErr m) =>
|
|
|
|
SourceName ->
|
|
|
|
FunctionName 'MSSQL ->
|
|
|
|
SystemDefined ->
|
|
|
|
FunctionConfig ->
|
|
|
|
FunctionPermissionsMap ->
|
|
|
|
RawFunctionInfo 'MSSQL ->
|
2021-09-24 12:18:40 +03:00
|
|
|
Maybe Text ->
|
2022-05-26 14:54:30 +03:00
|
|
|
NamingCase ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (FunctionInfo 'MSSQL, SchemaDependency)
|
2022-05-26 14:54:30 +03:00
|
|
|
buildFunctionInfo _ _ _ _ _ _ _ _ =
|
2021-02-23 20:37:27 +03:00
|
|
|
throw400 NotSupported "SQL Functions are not supported for MSSQL source"
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
updateColumnInEventTrigger ::
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
Column 'MSSQL ->
|
|
|
|
Column 'MSSQL ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
EventTriggerConf 'MSSQL ->
|
|
|
|
EventTriggerConf 'MSSQL
|
2021-02-23 20:37:27 +03:00
|
|
|
updateColumnInEventTrigger _ _ _ _ = id
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
parseCollectableType ::
|
|
|
|
(MonadError QErr m) =>
|
|
|
|
CollectableType (ColumnType 'MSSQL) ->
|
|
|
|
Value ->
|
|
|
|
m (PartialSQLExp 'MSSQL)
|
2021-02-23 20:37:27 +03:00
|
|
|
parseCollectableType collectableType = \case
|
|
|
|
String t
|
|
|
|
| isSessionVariable t -> pure $ mkTypedSessionVar collectableType $ mkSessionVariable t
|
2021-09-24 01:56:37 +03:00
|
|
|
| isReqUserId t -> pure $ mkTypedSessionVar collectableType userIdHeader
|
2021-02-23 20:37:27 +03:00
|
|
|
val -> case collectableType of
|
|
|
|
CollectableTypeScalar scalarType ->
|
|
|
|
PSESQLExp . MT.ValueExpression <$> parseScalarValueColumnType scalarType val
|
2021-09-24 01:56:37 +03:00
|
|
|
CollectableTypeArray _ ->
|
2021-02-23 20:37:27 +03:00
|
|
|
throw400 NotSupported "Array types are not supported in MSSQL backend"
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mkTypedSessionVar ::
|
|
|
|
CollectableType (ColumnType 'MSSQL) ->
|
|
|
|
SessionVariable ->
|
|
|
|
PartialSQLExp 'MSSQL
|
2021-02-23 20:37:27 +03:00
|
|
|
mkTypedSessionVar columnType =
|
|
|
|
PSESessVar (msColumnTypeToScalarType <$> columnType)
|
|
|
|
|
|
|
|
msColumnTypeToScalarType :: ColumnType 'MSSQL -> ScalarType 'MSSQL
|
|
|
|
msColumnTypeToScalarType = \case
|
|
|
|
ColumnScalar scalarType -> scalarType
|
2021-09-24 01:56:37 +03:00
|
|
|
ColumnEnumReference _ -> MT.TextType
|