2021-02-23 20:37:27 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2022-01-11 01:54:51 +03:00
|
|
|
-- | MSSQL Instances Transport
|
|
|
|
--
|
|
|
|
-- Defines the MSSQL instance of 'BackendTransport' and how to
|
|
|
|
-- interact with the database for running queries, mutations, subscriptions,
|
|
|
|
-- and so on.
|
2021-02-23 20:37:27 +03:00
|
|
|
module Hasura.Backends.MSSQL.Instances.Transport () where
|
|
|
|
|
2022-04-07 17:41:43 +03:00
|
|
|
import Control.Exception.Safe (throwIO)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.ByteString qualified as B
|
|
|
|
import Data.String (fromString)
|
|
|
|
import Data.Text.Encoding (encodeUtf8)
|
|
|
|
import Data.Text.Extended
|
2022-03-21 15:14:52 +03:00
|
|
|
import Database.MSSQL.Transaction (forJsonQueryE)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Database.ODBC.SQLServer qualified as ODBC
|
|
|
|
import Hasura.Backends.MSSQL.Connection
|
2022-04-28 22:33:33 +03:00
|
|
|
import Hasura.Backends.MSSQL.Execute.QueryTags (withQueryTags)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.MSSQL.Instances.Execute
|
2022-02-07 17:11:49 +03:00
|
|
|
import Hasura.Backends.MSSQL.SQL.Error
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.MSSQL.ToQuery
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
2022-03-21 13:39:49 +03:00
|
|
|
import Hasura.GraphQL.Execute.Subscription.Plan
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Logging
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.Namespace (RootFieldAlias)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Transport.Backend
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol
|
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.SQL.Backend
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Server.Types (RequestId)
|
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing
|
|
|
|
|
|
|
|
instance BackendTransport 'MSSQL where
|
2021-02-23 20:37:27 +03:00
|
|
|
runDBQuery = runQuery
|
2021-04-13 14:10:08 +03:00
|
|
|
runDBQueryExplain = runQueryExplain
|
2021-02-23 20:37:27 +03:00
|
|
|
runDBMutation = runMutation
|
|
|
|
runDBSubscription = runSubscription
|
2022-04-07 17:41:43 +03:00
|
|
|
runDBStreamingSubscription _ _ _ =
|
|
|
|
liftIO . throwIO $ userError "runDBSubscription: not implemented for MS-SQL sources."
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-04-20 19:57:14 +03:00
|
|
|
newtype CohortResult = CohortResult (CohortId, Text)
|
|
|
|
|
|
|
|
instance J.FromJSON CohortResult where
|
|
|
|
parseJSON = J.withObject "CohortResult" \o -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
cohortId <- o J..: "result_id"
|
2021-04-20 19:57:14 +03:00
|
|
|
cohortData <- o J..: "result"
|
|
|
|
pure $ CohortResult (cohortId, cohortData)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runQuery ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadQueryLog m,
|
|
|
|
MonadTrace m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
ExceptT QErr IO EncJSON ->
|
|
|
|
Maybe (PreparedQuery 'MSSQL) ->
|
|
|
|
-- | Also return the time spent in the PG query; for telemetry.
|
|
|
|
m (DiffTime, EncJSON)
|
|
|
|
runQuery reqId query fieldName _userInfo logger _sourceConfig tx genSql = do
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger $ mkQueryLog query fieldName genSql reqId
|
2021-09-24 01:56:37 +03:00
|
|
|
withElapsedTime $
|
|
|
|
trace ("MSSQL Query for root field " <>> fieldName) $
|
|
|
|
run tx
|
|
|
|
|
|
|
|
runQueryExplain ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
DBStepInfo 'MSSQL ->
|
|
|
|
m EncJSON
|
2021-04-13 14:10:08 +03:00
|
|
|
runQueryExplain (DBStepInfo _ _ _ action) = run action
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runMutation ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadQueryLog m,
|
|
|
|
MonadTrace m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
ExceptT QErr IO EncJSON ->
|
|
|
|
Maybe (PreparedQuery 'MSSQL) ->
|
|
|
|
-- | Also return 'Mutation' when the operation was a mutation, and the time
|
2021-02-23 20:37:27 +03:00
|
|
|
-- spent in the PG query; for telemetry.
|
2021-09-24 01:56:37 +03:00
|
|
|
m (DiffTime, EncJSON)
|
|
|
|
runMutation reqId query fieldName _userInfo logger _sourceConfig tx _genSql = do
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger $ mkQueryLog query fieldName Nothing reqId
|
2021-09-24 01:56:37 +03:00
|
|
|
withElapsedTime $
|
|
|
|
trace ("MSSQL Mutation for root field " <>> fieldName) $
|
|
|
|
run tx
|
|
|
|
|
|
|
|
runSubscription ::
|
|
|
|
MonadIO m =>
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
MultiplexedQuery 'MSSQL ->
|
|
|
|
[(CohortId, CohortVariables)] ->
|
|
|
|
m (DiffTime, Either QErr [(CohortId, B.ByteString)])
|
2022-04-28 22:33:33 +03:00
|
|
|
runSubscription sourceConfig (MultiplexedQuery' reselect queryTags) variables = do
|
2022-01-04 14:53:50 +03:00
|
|
|
let mssqlExecCtx = _mscExecCtx sourceConfig
|
2021-04-20 19:57:14 +03:00
|
|
|
multiplexed = multiplexRootReselect variables reselect
|
2022-04-28 22:33:33 +03:00
|
|
|
query = toQueryFlat (fromSelect multiplexed)
|
|
|
|
-- Append query tags comment to the query. We cannot use 'toSQL' to convert
|
|
|
|
-- QueryTagsComment to Query, because it escapes the query tags comment which
|
|
|
|
-- will create a badly formatted query. Hence we use the 'rawUnescapedText' to
|
|
|
|
-- append the comment without any escaping.
|
|
|
|
queryWithQueryTags = query `withQueryTags` queryTags
|
|
|
|
withElapsedTime $ runExceptT $ executeMultiplexedQuery mssqlExecCtx queryWithQueryTags
|
2021-04-20 19:57:14 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
executeMultiplexedQuery ::
|
|
|
|
MonadIO m =>
|
2022-01-04 14:53:50 +03:00
|
|
|
MSSQLExecCtx ->
|
2021-09-24 01:56:37 +03:00
|
|
|
ODBC.Query ->
|
|
|
|
ExceptT QErr m [(CohortId, B.ByteString)]
|
2022-01-04 14:53:50 +03:00
|
|
|
executeMultiplexedQuery mssqlExecCtx query = do
|
2021-04-20 19:57:14 +03:00
|
|
|
let parseResult r = J.eitherDecodeStrict (encodeUtf8 r) `onLeft` \s -> throw400 ParseFailed (fromString s)
|
|
|
|
convertFromJSON :: [CohortResult] -> [(CohortId, B.ByteString)]
|
|
|
|
convertFromJSON = map \(CohortResult (cid, cresult)) -> (cid, encodeUtf8 cresult)
|
2022-03-21 15:14:52 +03:00
|
|
|
-- Because the 'query' will have a @FOR JSON@ clause at the toplevel it will
|
|
|
|
-- be split across multiple rows, hence use of 'forJsonQueryE' which takes
|
|
|
|
-- care of concatenating the results.
|
|
|
|
textResult <- run $ mssqlRunReadOnly mssqlExecCtx $ forJsonQueryE defaultMSSQLTxErrorHandler query
|
2021-04-20 19:57:14 +03:00
|
|
|
parsedResult <- parseResult textResult
|
|
|
|
pure $ convertFromJSON parsedResult
|
2021-03-13 17:40:50 +03:00
|
|
|
|
2021-02-25 21:15:55 +03:00
|
|
|
run :: (MonadIO m, MonadError QErr m) => ExceptT QErr IO a -> m a
|
2021-02-23 20:37:27 +03:00
|
|
|
run action = do
|
2021-02-25 21:15:55 +03:00
|
|
|
result <- liftIO $ runExceptT action
|
|
|
|
result `onLeft` throwError
|
2021-03-13 17:40:50 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mkQueryLog ::
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Maybe (PreparedQuery 'MSSQL) ->
|
|
|
|
RequestId ->
|
|
|
|
QueryLog
|
2021-03-13 17:40:50 +03:00
|
|
|
mkQueryLog gqlQuery fieldName preparedSql requestId =
|
2021-04-28 20:38:05 +03:00
|
|
|
QueryLog gqlQuery ((fieldName,) <$> generatedQuery) requestId QueryLogKindDatabase
|
2021-03-13 17:40:50 +03:00
|
|
|
where
|
2021-05-21 14:37:34 +03:00
|
|
|
generatedQuery =
|
2021-09-24 01:56:37 +03:00
|
|
|
preparedSql <&> \queryString ->
|
|
|
|
GeneratedQuery queryString J.Null
|