2021-02-23 20:37:27 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.MSSQL.Instances.Transport () where
|
|
|
|
|
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
|
|
|
|
import Database.ODBC.SQLServer qualified as ODBC
|
|
|
|
import Hasura.Backends.MSSQL.Connection
|
|
|
|
import Hasura.Backends.MSSQL.Instances.Execute
|
|
|
|
import Hasura.Backends.MSSQL.ToQuery
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
|
|
|
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
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
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
|
|
|
|
|
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)])
|
2021-04-20 19:57:14 +03:00
|
|
|
runSubscription sourceConfig (MultiplexedQuery' reselect) variables = do
|
2021-02-25 21:15:55 +03:00
|
|
|
let pool = _mscConnectionPool sourceConfig
|
2021-04-20 19:57:14 +03:00
|
|
|
multiplexed = multiplexRootReselect variables reselect
|
|
|
|
query = toQueryFlat $ fromSelect multiplexed
|
|
|
|
withElapsedTime $ runExceptT $ executeMultiplexedQuery pool query
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
executeMultiplexedQuery ::
|
|
|
|
MonadIO m =>
|
|
|
|
MSSQLPool ->
|
|
|
|
ODBC.Query ->
|
|
|
|
ExceptT QErr m [(CohortId, B.ByteString)]
|
2021-04-20 19:57:14 +03:00
|
|
|
executeMultiplexedQuery pool query = do
|
|
|
|
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)
|
2021-09-24 01:56:37 +03:00
|
|
|
textResult <- run $ runJSONPathQuery pool 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
|