2021-02-23 20:37:27 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.MSSQL.Instances.Transport () where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
import qualified Data.Aeson as J
|
2021-02-23 20:37:27 +03:00
|
|
|
import qualified Data.ByteString as B
|
2021-04-20 19:57:14 +03:00
|
|
|
import qualified Database.ODBC.SQLServer as ODBC
|
2021-02-23 20:37:27 +03:00
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
2021-04-20 19:57:14 +03:00
|
|
|
import Data.String (fromString)
|
2021-02-23 20:37:27 +03:00
|
|
|
import Data.Text.Encoding (encodeUtf8)
|
|
|
|
import Data.Text.Extended
|
|
|
|
|
|
|
|
import qualified Hasura.Logging as L
|
|
|
|
|
2021-02-25 21:15:55 +03:00
|
|
|
import Hasura.Backends.MSSQL.Connection
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.Backends.MSSQL.Instances.Execute
|
2021-04-20 19:57:14 +03:00
|
|
|
import Hasura.Backends.MSSQL.ToQuery
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
2021-03-13 17:40:50 +03:00
|
|
|
import Hasura.GraphQL.Logging
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.GraphQL.Transport.Backend
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.Server.Types (RequestId)
|
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing
|
|
|
|
|
|
|
|
|
|
|
|
instance BackendTransport 'MSSQL where
|
|
|
|
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
|
|
|
|
cohortId <- o J..: "result_id"
|
|
|
|
cohortData <- o J..: "result"
|
|
|
|
pure $ CohortResult (cohortId, cohortData)
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
runQuery
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadQueryLog m
|
|
|
|
, MonadTrace m
|
|
|
|
, MonadError QErr m
|
|
|
|
)
|
|
|
|
=> RequestId
|
|
|
|
-> GQLReqUnparsed
|
|
|
|
-> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> L.Logger L.Hasura
|
|
|
|
-> SourceConfig 'MSSQL
|
2021-02-25 21:15:55 +03:00
|
|
|
-> ExceptT QErr IO EncJSON
|
2021-02-23 20:37:27 +03:00
|
|
|
-> Maybe Text
|
|
|
|
-> m (DiffTime, EncJSON)
|
|
|
|
-- ^ Also return the time spent in the PG query; for telemetry.
|
2021-03-13 17:40:50 +03:00
|
|
|
runQuery reqId query fieldName _userInfo logger _sourceConfig tx genSql = do
|
|
|
|
logQueryLog logger $ mkQueryLog query fieldName genSql reqId
|
2021-02-23 20:37:27 +03:00
|
|
|
withElapsedTime
|
|
|
|
$ trace ("MSSQL Query for root field " <>> fieldName)
|
|
|
|
$ run tx
|
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
runQueryExplain
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadError QErr m
|
|
|
|
)
|
|
|
|
=> DBStepInfo 'MSSQL
|
|
|
|
-> m EncJSON
|
|
|
|
runQueryExplain (DBStepInfo _ _ _ action) = run action
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
runMutation
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadQueryLog m
|
|
|
|
, MonadTrace m
|
|
|
|
, MonadError QErr m
|
|
|
|
)
|
|
|
|
=> RequestId
|
|
|
|
-> GQLReqUnparsed
|
|
|
|
-> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> L.Logger L.Hasura
|
|
|
|
-> SourceConfig 'MSSQL
|
2021-02-25 21:15:55 +03:00
|
|
|
-> ExceptT QErr IO EncJSON
|
2021-02-23 20:37:27 +03:00
|
|
|
-> Maybe Text
|
|
|
|
-> m (DiffTime, EncJSON)
|
|
|
|
-- ^ Also return 'Mutation' when the operation was a mutation, and the time
|
|
|
|
-- spent in the PG query; for telemetry.
|
|
|
|
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-02-23 20:37:27 +03:00
|
|
|
withElapsedTime
|
|
|
|
$ trace ("MSSQL Mutation for root field " <>> fieldName)
|
|
|
|
$ run tx
|
|
|
|
|
|
|
|
runSubscription
|
2021-04-20 19:57:14 +03:00
|
|
|
:: MonadIO m
|
2021-02-23 20:37:27 +03:00
|
|
|
=> 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
|
|
|
|
|
|
|
|
executeMultiplexedQuery
|
|
|
|
:: MonadIO m
|
|
|
|
=> MSSQLPool
|
|
|
|
-> ODBC.Query
|
|
|
|
-> ExceptT QErr m [(CohortId, B.ByteString)]
|
|
|
|
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)
|
|
|
|
textResult <- run $ runJSONPathQuery pool query
|
|
|
|
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
|
|
|
|
|
|
|
mkQueryLog
|
|
|
|
:: GQLReqUnparsed
|
|
|
|
-> G.Name
|
|
|
|
-> Maybe Text
|
|
|
|
-> RequestId
|
|
|
|
-> QueryLog
|
|
|
|
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
|
|
|
|
generatedQuery = preparedSql <&> \qs -> GeneratedQuery qs J.Null
|