2021-09-22 18:34:53 +03:00
|
|
|
{-# LANGUAGE ApplicativeDo #-}
|
2021-02-23 20:37:27 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.MSSQL.Instances.Schema () where
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Has
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
|
|
|
import Data.List.NonEmpty qualified as NE
|
|
|
|
import Data.Text.Encoding (encodeUtf8)
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Database.ODBC.SQLServer qualified as ODBC
|
|
|
|
import Hasura.Backends.MSSQL.Types qualified as MSSQL
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.GraphQL.Parser hiding (EnumValueInfo, field)
|
|
|
|
import Hasura.GraphQL.Parser qualified as P
|
|
|
|
import Hasura.GraphQL.Parser.Internal.Parser hiding (field)
|
|
|
|
import Hasura.GraphQL.Schema.Backend
|
|
|
|
import Hasura.GraphQL.Schema.BoolExp
|
|
|
|
import Hasura.GraphQL.Schema.Build qualified as GSB
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
import Hasura.GraphQL.Schema.Select
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR
|
2021-10-01 15:52:19 +03:00
|
|
|
import Hasura.RQL.IR.Insert qualified as IR
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.IR.Select qualified as IR
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
-- BackendSchema instance
|
|
|
|
|
|
|
|
instance BackendSchema 'MSSQL where
|
|
|
|
-- top level parsers
|
2021-09-24 01:56:37 +03:00
|
|
|
buildTableQueryFields = GSB.buildTableQueryFields
|
|
|
|
buildTableRelayQueryFields = msBuildTableRelayQueryFields
|
2021-10-01 15:52:19 +03:00
|
|
|
buildTableInsertMutationFields = msBuildTableInsertMutationFields
|
2021-02-23 20:37:27 +03:00
|
|
|
buildTableUpdateMutationFields = msBuildTableUpdateMutationFields
|
|
|
|
buildTableDeleteMutationFields = msBuildTableDeleteMutationFields
|
2021-09-24 01:56:37 +03:00
|
|
|
buildFunctionQueryFields = msBuildFunctionQueryFields
|
|
|
|
buildFunctionRelayQueryFields = msBuildFunctionRelayQueryFields
|
|
|
|
buildFunctionMutationFields = msBuildFunctionMutationFields
|
2021-06-15 18:53:20 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
-- backend extensions
|
2021-09-24 01:56:37 +03:00
|
|
|
relayExtension = Nothing
|
|
|
|
nodesAggExtension = Just ()
|
2021-06-15 18:53:20 +03:00
|
|
|
|
|
|
|
-- table arguments
|
|
|
|
tableArguments = msTableArgs
|
2021-10-01 15:52:19 +03:00
|
|
|
mkRelationshipParser = msMkRelationshipParser
|
2021-06-15 18:53:20 +03:00
|
|
|
|
|
|
|
-- individual components
|
2021-09-24 01:56:37 +03:00
|
|
|
columnParser = msColumnParser
|
|
|
|
jsonPathArg = msJsonPathArg
|
|
|
|
orderByOperators = msOrderByOperators
|
|
|
|
comparisonExps = msComparisonExps
|
|
|
|
mkCountType = msMkCountType
|
2021-02-23 20:37:27 +03:00
|
|
|
aggregateOrderByCountType = MSSQL.IntegerType
|
2021-09-24 01:56:37 +03:00
|
|
|
computedField = msComputedField
|
|
|
|
node = msNode
|
2021-06-15 18:53:20 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
-- SQL literals
|
|
|
|
columnDefaultValue = msColumnDefaultValue
|
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
-- Extra insert data
|
|
|
|
getExtraInsertData tableInfo =
|
|
|
|
let pkeyColumns = fmap (map pgiColumn . toList . _pkColumns) . _tciPrimaryKey . _tiCoreInfo $ tableInfo
|
|
|
|
identityColumns = _tciExtraTableMetadata $ _tiCoreInfo tableInfo
|
|
|
|
in MSSQL.MSSQLExtraInsertData (fromMaybe [] pkeyColumns) identityColumns
|
|
|
|
|
|
|
|
-- | MSSQL only supports inserts into tables that have a primary key defined.
|
|
|
|
supportsInserts :: TableInfo 'MSSQL -> Bool
|
|
|
|
supportsInserts = isJust . _tciPrimaryKey . _tiCoreInfo
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
----------------------------------------------------------------
|
|
|
|
-- Top level parsers
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msBuildTableRelayQueryFields ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
TableInfo 'MSSQL ->
|
|
|
|
G.Name ->
|
|
|
|
NESeq (ColumnInfo 'MSSQL) ->
|
|
|
|
SelPermInfo 'MSSQL ->
|
|
|
|
m [FieldParser n (QueryRootField UnpreparedValue)]
|
2021-09-23 15:37:56 +03:00
|
|
|
msBuildTableRelayQueryFields _sourceName _sourceInfo _queryTagsConfig _tableName _tableInfo _gqlName _pkeyColumns _selPerms =
|
2021-06-15 18:53:20 +03:00
|
|
|
pure []
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
msBuildTableInsertMutationFields ::
|
|
|
|
forall r m n.
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
TableInfo 'MSSQL ->
|
|
|
|
G.Name ->
|
|
|
|
InsPermInfo 'MSSQL ->
|
|
|
|
Maybe (SelPermInfo 'MSSQL) ->
|
|
|
|
Maybe (UpdPermInfo 'MSSQL) ->
|
|
|
|
m [FieldParser n (MutationRootField UnpreparedValue)]
|
|
|
|
msBuildTableInsertMutationFields
|
|
|
|
sourceName
|
|
|
|
sourceInfo
|
|
|
|
queryTagsConfig
|
|
|
|
tableName
|
|
|
|
tableInfo
|
|
|
|
gqlName
|
|
|
|
insPerms
|
|
|
|
mSelPerms
|
|
|
|
mUpdPerms
|
|
|
|
| supportsInserts tableInfo =
|
|
|
|
GSB.buildTableInsertMutationFields
|
|
|
|
sourceName
|
|
|
|
sourceInfo
|
|
|
|
queryTagsConfig
|
|
|
|
tableName
|
|
|
|
tableInfo
|
|
|
|
gqlName
|
|
|
|
insPerms
|
|
|
|
mSelPerms
|
|
|
|
mUpdPerms
|
|
|
|
| otherwise = return []
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msBuildTableUpdateMutationFields ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
TableInfo 'MSSQL ->
|
|
|
|
G.Name ->
|
|
|
|
UpdPermInfo 'MSSQL ->
|
|
|
|
Maybe (SelPermInfo 'MSSQL) ->
|
|
|
|
m [FieldParser n (MutationRootField UnpreparedValue)]
|
2021-09-23 15:37:56 +03:00
|
|
|
msBuildTableUpdateMutationFields _sourceName _sourceInfo _queryTagsConfig _tableName _tableInfo _gqlName _updPerns _selPerms =
|
2021-02-23 20:37:27 +03:00
|
|
|
pure []
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msBuildTableDeleteMutationFields ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
TableInfo 'MSSQL ->
|
|
|
|
G.Name ->
|
|
|
|
DelPermInfo 'MSSQL ->
|
|
|
|
Maybe (SelPermInfo 'MSSQL) ->
|
|
|
|
m [FieldParser n (MutationRootField UnpreparedValue)]
|
2021-09-23 15:37:56 +03:00
|
|
|
msBuildTableDeleteMutationFields _sourceName _sourceInfo _queryTagsConfig _tableName _tableInfo _gqlName _delPerns _selPerms =
|
2021-02-23 20:37:27 +03:00
|
|
|
pure []
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msBuildFunctionQueryFields ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
FunctionName 'MSSQL ->
|
|
|
|
FunctionInfo 'MSSQL ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
SelPermInfo 'MSSQL ->
|
|
|
|
m [FieldParser n (QueryRootField UnpreparedValue)]
|
2021-09-23 15:37:56 +03:00
|
|
|
msBuildFunctionQueryFields _ _ _ _ _ _ _ =
|
2021-02-23 20:37:27 +03:00
|
|
|
pure []
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msBuildFunctionRelayQueryFields ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
FunctionName 'MSSQL ->
|
|
|
|
FunctionInfo 'MSSQL ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
NESeq (ColumnInfo 'MSSQL) ->
|
|
|
|
SelPermInfo 'MSSQL ->
|
|
|
|
m [FieldParser n (QueryRootField UnpreparedValue)]
|
2021-09-23 15:37:56 +03:00
|
|
|
msBuildFunctionRelayQueryFields _sourceName _sourceInfo _queryTagsConfig _functionName _functionInfo _tableName _pkeyColumns _selPerms =
|
2021-06-15 18:53:20 +03:00
|
|
|
pure []
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msBuildFunctionMutationFields ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
Maybe QueryTagsConfig ->
|
|
|
|
FunctionName 'MSSQL ->
|
|
|
|
FunctionInfo 'MSSQL ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
SelPermInfo 'MSSQL ->
|
|
|
|
m [FieldParser n (MutationRootField UnpreparedValue)]
|
2021-09-23 15:37:56 +03:00
|
|
|
msBuildFunctionMutationFields _ _ _ _ _ _ _ =
|
2021-02-23 20:37:27 +03:00
|
|
|
pure []
|
|
|
|
|
2021-06-15 18:53:20 +03:00
|
|
|
----------------------------------------------------------------
|
|
|
|
-- Table arguments
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msTableArgs ::
|
|
|
|
forall r m n.
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
TableInfo 'MSSQL ->
|
|
|
|
SelPermInfo 'MSSQL ->
|
|
|
|
m (InputFieldsParser n (IR.SelectArgsG 'MSSQL (UnpreparedValue 'MSSQL)))
|
2021-06-15 18:53:20 +03:00
|
|
|
msTableArgs sourceName tableInfo selectPermissions = do
|
2021-09-24 01:56:37 +03:00
|
|
|
whereParser <- tableWhereArg sourceName tableInfo selectPermissions
|
2021-06-15 18:53:20 +03:00
|
|
|
orderByParser <- tableOrderByArg sourceName tableInfo selectPermissions
|
|
|
|
pure do
|
2021-09-24 01:56:37 +03:00
|
|
|
whereArg <- whereParser
|
2021-06-15 18:53:20 +03:00
|
|
|
orderByArg <- orderByParser
|
2021-09-24 01:56:37 +03:00
|
|
|
limitArg <- tableLimitArg
|
|
|
|
offsetArg <- tableOffsetArg
|
|
|
|
pure $
|
|
|
|
IR.SelectArgs
|
|
|
|
{ IR._saWhere = whereArg,
|
|
|
|
IR._saOrderBy = orderByArg,
|
|
|
|
IR._saLimit = limitArg,
|
|
|
|
IR._saOffset = offsetArg,
|
|
|
|
-- not supported on MSSQL for now
|
|
|
|
IR._saDistinct = Nothing
|
|
|
|
}
|
2021-06-15 18:53:20 +03:00
|
|
|
|
2021-10-01 15:52:19 +03:00
|
|
|
msMkRelationshipParser ::
|
|
|
|
forall r m n.
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
RelInfo 'MSSQL ->
|
|
|
|
m (Maybe (InputFieldsParser n (Maybe (IR.AnnotatedInsert 'MSSQL (UnpreparedValue 'MSSQL)))))
|
|
|
|
msMkRelationshipParser _sourceName _relationshipInfo = do
|
|
|
|
-- When we support nested inserts, we also need to ensure we limit ourselves
|
|
|
|
-- to inserting into tables whch supports inserts:
|
|
|
|
{-
|
|
|
|
import Hasura.GraphQL.Schema.Mutation qualified as GSB
|
|
|
|
|
|
|
|
runMaybeT $ do
|
|
|
|
let otherTableName = riRTable relationshipInfo
|
|
|
|
otherTableInfo <- lift $ askTableInfo sourceName otherTableName
|
|
|
|
guard (supportsInserts otherTableInfo)
|
|
|
|
-}
|
|
|
|
return Nothing
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
----------------------------------------------------------------
|
|
|
|
-- Individual components
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msColumnParser ::
|
2021-10-29 17:42:07 +03:00
|
|
|
(MonadSchema n m, MonadError QErr m, MonadReader r m, Has MkTypename r) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
ColumnType 'MSSQL ->
|
|
|
|
G.Nullability ->
|
|
|
|
m (Parser 'Both n (ValueWithOrigin (ColumnValue 'MSSQL)))
|
2021-02-23 20:37:27 +03:00
|
|
|
msColumnParser columnType (G.Nullability isNullable) =
|
server: remove remnants of query plan caching (fix #1795)
Query plan caching was introduced by - I believe - hasura/graphql-engine#1934 in order to reduce the query response latency. During the development of PDV in hasura/graphql-engine#4111, it was found out that the new architecture (for which query plan caching wasn't implemented) performed comparably to the pre-PDV architecture with caching. Hence, it was decided to leave query plan caching until some day in the future when it was deemed necessary.
Well, we're in the future now, and there still isn't a convincing argument for query plan caching. So the time has come to remove some references to query plan caching from the codebase. For the most part, any code being removed would probably not be very well suited to the post-PDV architecture of query execution, so arguably not much is lost.
Apart from simplifying the code, this PR will contribute towards making the GraphQL schema generation more modular, testable, and easier to profile. I'd like to eventually work towards a situation in which it's easy to generate a GraphQL schema parser *in isolation*, without being connected to a database, and then parse a GraphQL query *in isolation*, without even listening any HTTP port. It is important that both of these operations can be examined in detail, and in isolation, since they are two major performance bottlenecks, as well as phases where many important upcoming features hook into.
Implementation
The following have been removed:
- The entirety of `server/src-lib/Hasura/GraphQL/Execute/Plan.hs`
- The core phases of query parsing and execution no longer have any references to query plan caching. Note that this is not to be confused with query *response* caching, which is not affected by this PR. This includes removal of the types:
- - `Opaque`, which is replaced by a tuple. Note that the old implementation was broken and did not adequately hide the constructors.
- - `QueryReusability` (and the `markNotReusable` method). Notably, the implementation of the `ParseT` monad now consists of two, rather than three, monad transformers.
- Cache-related tests (in `server/src-test/Hasura/CacheBoundedSpec.hs`) have been removed .
- References to query plan caching in the documentation.
- The `planCacheOptions` in the `TenantConfig` type class was removed. However, during parsing, unrecognized fields in the YAML config get ignored, so this does not cause a breaking change. (Confirmed manually, as well as in consultation with @sordina.)
- The metrics no longer send cache hit/miss messages.
There are a few places in which one can still find references to query plan caching:
- We still accept the `--query-plan-cache-size` command-line option for backwards compatibility. The `HASURA_QUERY_PLAN_CACHE_SIZE` environment variable is not read.
https://github.com/hasura/graphql-engine-mono/pull/1815
GitOrigin-RevId: 17d92b254ec093c62a7dfeec478658ede0813eb7
2021-07-27 14:51:52 +03:00
|
|
|
peelWithOrigin . fmap (ColumnValue columnType) <$> case columnType of
|
2021-06-11 06:26:50 +03:00
|
|
|
-- TODO: the mapping here is not consistent with mkMSSQLScalarTypeName. For
|
|
|
|
-- example, exposing all the float types as a GraphQL Float type is
|
|
|
|
-- incorrect, similarly exposing all the integer types as a GraphQL Int
|
2021-09-24 01:56:37 +03:00
|
|
|
ColumnScalar scalarType ->
|
|
|
|
possiblyNullable scalarType <$> case scalarType of
|
|
|
|
-- bytestring
|
|
|
|
MSSQL.CharType -> pure $ ODBC.ByteStringValue . encodeUtf8 <$> P.string
|
|
|
|
MSSQL.VarcharType -> pure $ ODBC.ByteStringValue . encodeUtf8 <$> P.string
|
|
|
|
-- text
|
|
|
|
MSSQL.WcharType -> pure $ ODBC.TextValue <$> P.string
|
|
|
|
MSSQL.WvarcharType -> pure $ ODBC.TextValue <$> P.string
|
|
|
|
MSSQL.WtextType -> pure $ ODBC.TextValue <$> P.string
|
|
|
|
MSSQL.TextType -> pure $ ODBC.TextValue <$> P.string
|
|
|
|
-- integer
|
|
|
|
MSSQL.IntegerType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
|
|
|
|
MSSQL.SmallintType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
|
|
|
|
MSSQL.BigintType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
|
|
|
|
MSSQL.TinyintType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
|
|
|
|
-- float
|
|
|
|
MSSQL.NumericType -> pure $ ODBC.DoubleValue <$> P.float
|
|
|
|
MSSQL.DecimalType -> pure $ ODBC.DoubleValue <$> P.float
|
|
|
|
MSSQL.FloatType -> pure $ ODBC.DoubleValue <$> P.float
|
|
|
|
MSSQL.RealType -> pure $ ODBC.DoubleValue <$> P.float
|
|
|
|
-- boolean
|
|
|
|
MSSQL.BitType -> pure $ ODBC.BoolValue <$> P.boolean
|
|
|
|
_ -> do
|
|
|
|
name <- MSSQL.mkMSSQLScalarTypeName scalarType
|
2021-10-29 17:42:07 +03:00
|
|
|
let schemaType = P.NonNullable $ P.TNamed $ P.mkDefinition (P.Typename name) Nothing P.TIScalar
|
2021-09-24 01:56:37 +03:00
|
|
|
pure $
|
|
|
|
Parser
|
|
|
|
{ pType = schemaType,
|
|
|
|
pParser =
|
|
|
|
valueToJSON (P.toGraphQLType schemaType)
|
|
|
|
>=> either (parseErrorWith ParseFailed . qeError) pure . (MSSQL.parseScalarValue scalarType)
|
|
|
|
}
|
2021-02-23 20:37:27 +03:00
|
|
|
ColumnEnumReference (EnumReference tableName enumValues) ->
|
|
|
|
case nonEmpty (Map.toList enumValues) of
|
|
|
|
Just enumValuesList -> do
|
2021-04-22 00:44:37 +03:00
|
|
|
tableGQLName <- tableGraphQLName @'MSSQL tableName `onLeft` throwError
|
2021-10-29 17:42:07 +03:00
|
|
|
enumName <- P.mkTypename $ tableGQLName <> $$(G.litName "_enum")
|
2021-02-23 20:37:27 +03:00
|
|
|
pure $ possiblyNullable MSSQL.VarcharType $ P.enum enumName Nothing (mkEnumValue <$> enumValuesList)
|
|
|
|
Nothing -> throw400 ValidationFailed "empty enum values"
|
|
|
|
where
|
|
|
|
possiblyNullable _scalarType
|
|
|
|
| isNullable = fmap (fromMaybe ODBC.NullValue) . P.nullable
|
2021-09-24 01:56:37 +03:00
|
|
|
| otherwise = id
|
2021-02-23 20:37:27 +03:00
|
|
|
mkEnumValue :: (EnumValue, EnumValueInfo) -> (P.Definition P.EnumValueInfo, ScalarValue 'MSSQL)
|
|
|
|
mkEnumValue (EnumValue value, EnumValueInfo description) =
|
2021-09-24 01:56:37 +03:00
|
|
|
( P.mkDefinition value (G.Description <$> description) P.EnumValueInfo,
|
|
|
|
ODBC.TextValue $ G.unName value
|
2021-02-23 20:37:27 +03:00
|
|
|
)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msJsonPathArg ::
|
|
|
|
MonadParse n =>
|
|
|
|
ColumnType 'MSSQL ->
|
|
|
|
InputFieldsParser n (Maybe (IR.ColumnOp 'MSSQL))
|
2021-02-23 20:37:27 +03:00
|
|
|
msJsonPathArg _columnType = pure Nothing
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msOrderByOperators ::
|
|
|
|
NonEmpty
|
|
|
|
( Definition P.EnumValueInfo,
|
|
|
|
(BasicOrderType 'MSSQL, NullsOrderType 'MSSQL)
|
2021-02-23 20:37:27 +03:00
|
|
|
)
|
2021-09-24 01:56:37 +03:00
|
|
|
msOrderByOperators =
|
|
|
|
NE.fromList
|
|
|
|
[ ( define $$(G.litName "asc") "in ascending order, nulls first",
|
|
|
|
(MSSQL.AscOrder, MSSQL.NullsFirst)
|
|
|
|
),
|
|
|
|
( define $$(G.litName "asc_nulls_first") "in ascending order, nulls first",
|
|
|
|
(MSSQL.AscOrder, MSSQL.NullsFirst)
|
|
|
|
),
|
|
|
|
( define $$(G.litName "asc_nulls_last") "in ascending order, nulls last",
|
|
|
|
(MSSQL.AscOrder, MSSQL.NullsLast)
|
|
|
|
),
|
|
|
|
( define $$(G.litName "desc") "in descending order, nulls last",
|
|
|
|
(MSSQL.DescOrder, MSSQL.NullsLast)
|
|
|
|
),
|
|
|
|
( define $$(G.litName "desc_nulls_first") "in descending order, nulls first",
|
|
|
|
(MSSQL.DescOrder, MSSQL.NullsFirst)
|
|
|
|
),
|
|
|
|
( define $$(G.litName "desc_nulls_last") "in descending order, nulls last",
|
|
|
|
(MSSQL.DescOrder, MSSQL.NullsLast)
|
|
|
|
)
|
|
|
|
]
|
2021-02-23 20:37:27 +03:00
|
|
|
where
|
|
|
|
define name desc = P.mkDefinition name (Just desc) P.EnumValueInfo
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msComparisonExps ::
|
|
|
|
forall m n r.
|
|
|
|
( BackendSchema 'MSSQL,
|
|
|
|
MonadSchema n m,
|
|
|
|
MonadError QErr m,
|
|
|
|
MonadReader r m,
|
2021-10-29 17:42:07 +03:00
|
|
|
Has QueryContext r,
|
|
|
|
Has MkTypename r
|
2021-09-24 01:56:37 +03:00
|
|
|
) =>
|
|
|
|
ColumnType 'MSSQL ->
|
|
|
|
m (Parser 'Input n [ComparisonExp 'MSSQL])
|
2021-02-23 20:37:27 +03:00
|
|
|
msComparisonExps = P.memoize 'comparisonExps \columnType -> do
|
|
|
|
-- see Note [Columns in comparison expression are never nullable]
|
2021-04-08 11:25:11 +03:00
|
|
|
collapseIfNull <- asks $ qcDangerousBooleanCollapse . getter
|
|
|
|
|
|
|
|
-- parsers used for individual values
|
2021-09-24 01:56:37 +03:00
|
|
|
typedParser <- columnParser columnType (G.Nullability False)
|
2021-04-22 00:44:37 +03:00
|
|
|
nullableTextParser <- columnParser (ColumnScalar @'MSSQL MSSQL.VarcharType) (G.Nullability True)
|
2021-09-24 01:56:37 +03:00
|
|
|
textParser <- columnParser (ColumnScalar @'MSSQL MSSQL.VarcharType) (G.Nullability False)
|
|
|
|
let columnListParser = fmap openValueOrigin <$> P.list typedParser
|
|
|
|
textListParser = fmap openValueOrigin <$> P.list textParser
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
|
|
-- field info
|
2021-10-29 17:42:07 +03:00
|
|
|
let name = P.Typename $ P.getName typedParser <> $$(G.litName "_MSSQL_comparison_exp")
|
2021-09-24 01:56:37 +03:00
|
|
|
desc =
|
|
|
|
G.Description $
|
|
|
|
"Boolean expression to compare columns of type "
|
|
|
|
<> P.getName typedParser
|
|
|
|
<<> ". All fields are combined with logical 'AND'."
|
|
|
|
|
|
|
|
pure $
|
|
|
|
P.object name (Just desc) $
|
|
|
|
fmap catMaybes $
|
|
|
|
sequenceA $
|
|
|
|
concat
|
|
|
|
[ -- Common ops for all types
|
|
|
|
equalityOperators
|
|
|
|
collapseIfNull
|
|
|
|
(mkParameter <$> typedParser)
|
|
|
|
(mkListLiteral <$> columnListParser),
|
|
|
|
comparisonOperators
|
|
|
|
collapseIfNull
|
|
|
|
(mkParameter <$> typedParser),
|
|
|
|
-- Ops for String like types
|
|
|
|
guard (isScalarColumnWhere (`elem` MSSQL.stringTypes) columnType)
|
|
|
|
*> [ P.fieldOptional
|
|
|
|
$$(G.litName "_like")
|
|
|
|
(Just "does the column match the given pattern")
|
|
|
|
(ALIKE . mkParameter <$> typedParser),
|
|
|
|
P.fieldOptional
|
|
|
|
$$(G.litName "_nlike")
|
|
|
|
(Just "does the column NOT match the given pattern")
|
|
|
|
(ANLIKE . mkParameter <$> typedParser)
|
|
|
|
],
|
|
|
|
-- Ops for Geometry/Geography types
|
|
|
|
guard (isScalarColumnWhere (`elem` MSSQL.geoTypes) columnType)
|
|
|
|
*> [ P.fieldOptional
|
|
|
|
$$(G.litName "_st_contains")
|
|
|
|
(Just "does the column contain the given value")
|
|
|
|
(ABackendSpecific . MSSQL.ASTContains . mkParameter <$> typedParser),
|
|
|
|
P.fieldOptional
|
|
|
|
$$(G.litName "_st_equals")
|
|
|
|
(Just "is the column equal to given value (directionality is ignored)")
|
|
|
|
(ABackendSpecific . MSSQL.ASTEquals . mkParameter <$> typedParser),
|
|
|
|
P.fieldOptional
|
|
|
|
$$(G.litName "_st_intersects")
|
|
|
|
(Just "does the column spatially intersect the given value")
|
|
|
|
(ABackendSpecific . MSSQL.ASTIntersects . mkParameter <$> typedParser),
|
|
|
|
P.fieldOptional
|
|
|
|
$$(G.litName "_st_overlaps")
|
|
|
|
(Just "does the column 'spatially overlap' (intersect but not completely contain) the given value")
|
|
|
|
(ABackendSpecific . MSSQL.ASTOverlaps . mkParameter <$> typedParser),
|
|
|
|
P.fieldOptional
|
|
|
|
$$(G.litName "_st_within")
|
|
|
|
(Just "is the column contained in the given value")
|
|
|
|
(ABackendSpecific . MSSQL.ASTWithin . mkParameter <$> typedParser)
|
|
|
|
],
|
|
|
|
-- Ops for Geometry types
|
|
|
|
guard (isScalarColumnWhere (MSSQL.GeometryType ==) columnType)
|
|
|
|
*> [ P.fieldOptional
|
|
|
|
$$(G.litName "_st_crosses")
|
|
|
|
(Just "does the column cross the given geometry value")
|
|
|
|
(ABackendSpecific . MSSQL.ASTCrosses . mkParameter <$> typedParser),
|
|
|
|
P.fieldOptional
|
|
|
|
$$(G.litName "_st_touches")
|
|
|
|
(Just "does the column have at least one point in common with the given geometry value")
|
|
|
|
(ABackendSpecific . MSSQL.ASTTouches . mkParameter <$> typedParser)
|
|
|
|
]
|
|
|
|
]
|
2021-03-19 15:42:09 +03:00
|
|
|
where
|
|
|
|
mkListLiteral :: [ColumnValue 'MSSQL] -> UnpreparedValue 'MSSQL
|
|
|
|
mkListLiteral =
|
|
|
|
P.UVLiteral . MSSQL.ListExpression . fmap (MSSQL.ValueExpression . cvValue)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
msMkCountType ::
|
|
|
|
-- | distinct values
|
|
|
|
Maybe Bool ->
|
|
|
|
Maybe [Column 'MSSQL] ->
|
|
|
|
CountType 'MSSQL
|
|
|
|
msMkCountType _ Nothing = MSSQL.StarCountable
|
2021-02-23 20:37:27 +03:00
|
|
|
msMkCountType (Just True) (Just cols) =
|
|
|
|
maybe MSSQL.StarCountable MSSQL.DistinctCountable $ nonEmpty cols
|
2021-09-24 01:56:37 +03:00
|
|
|
msMkCountType _ (Just cols) =
|
2021-02-23 20:37:27 +03:00
|
|
|
maybe MSSQL.StarCountable MSSQL.NonNullFieldCountable $ nonEmpty cols
|
|
|
|
|
|
|
|
-- | Computed field parser.
|
|
|
|
-- Currently unsupported: returns Nothing for now.
|
2021-09-24 01:56:37 +03:00
|
|
|
msComputedField ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
SourceName ->
|
|
|
|
ComputedFieldInfo 'MSSQL ->
|
|
|
|
TableName 'MSSQL ->
|
|
|
|
SelPermInfo 'MSSQL ->
|
|
|
|
m (Maybe (FieldParser n (AnnotatedField 'MSSQL)))
|
2021-05-20 09:28:35 +03:00
|
|
|
msComputedField _sourceName _fieldInfo _table _selectPemissions = pure Nothing
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
-- | Remote join field parser.
|
|
|
|
-- Currently unsupported: returns Nothing for now.
|
2021-09-24 01:56:37 +03:00
|
|
|
msRemoteRelationshipField ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
RemoteFieldInfo 'MSSQL ->
|
|
|
|
m (Maybe [FieldParser n (AnnotatedField 'MSSQL)])
|
2021-02-23 20:37:27 +03:00
|
|
|
msRemoteRelationshipField _remoteFieldInfo = pure Nothing
|
|
|
|
|
|
|
|
-- | The 'node' root field of a Relay request. Relay is currently unsupported on MSSQL,
|
|
|
|
-- meaning this parser will never be called: any attempt to create this parser should
|
|
|
|
-- therefore fail.
|
2021-09-24 01:56:37 +03:00
|
|
|
msNode ::
|
|
|
|
MonadBuildSchema 'MSSQL r m n =>
|
|
|
|
m
|
|
|
|
( Parser
|
|
|
|
'Output
|
|
|
|
n
|
|
|
|
( HashMap
|
|
|
|
(TableName 'MSSQL)
|
|
|
|
( SourceName,
|
|
|
|
SourceConfig 'MSSQL,
|
|
|
|
SelPermInfo 'MSSQL,
|
|
|
|
PrimaryKeyColumns 'MSSQL,
|
|
|
|
AnnotatedFields 'MSSQL
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2021-02-23 20:37:27 +03:00
|
|
|
msNode = throw500 "MSSQL does not support relay; `node` should never be exposed in the schema."
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
-- SQL literals
|
|
|
|
|
|
|
|
-- FIXME: this is nonsensical for MSSQL, we'll need to adjust the corresponding mutation
|
|
|
|
-- and its representation.
|
|
|
|
msColumnDefaultValue :: Column 'MSSQL -> SQLExpression 'MSSQL
|
|
|
|
msColumnDefaultValue = const $ MSSQL.ValueExpression ODBC.NullValue
|