{-# OPTIONS_GHC -fno-warn-orphans #-}
module Hasura.Backends.MySQL.Instances.Transport (runQuery) where
import Data.Aeson qualified as J
import Data.Text.Extended
import Hasura.Backends.MySQL.Instances.Execute ()
import Hasura.Base.Error
import Hasura.EncJSON
import Hasura.GraphQL.Execute.Backend
import Hasura.GraphQL.Logging
import Hasura.GraphQL.Namespace (RootFieldAlias)
import Hasura.GraphQL.Transport.Backend
import Hasura.GraphQL.Transport.HTTP.Protocol
import Hasura.Logging qualified as L
import Hasura.Prelude
import Hasura.RQL.Types.Backend
import Hasura.SQL.Backend
import Hasura.Server.Types (RequestId)
import Hasura.Session
import Hasura.Tracing
import Hasura.Tracing qualified as Tracing
instance BackendTransport 'MySQL where
runDBQuery :: RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> SourceConfig 'MySQL
-> ExecutionMonad 'MySQL EncJSON
-> Maybe (PreparedQuery 'MySQL)
-> m (DiffTime, EncJSON)
runDBQuery = RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> SourceConfig 'MySQL
-> ExecutionMonad 'MySQL EncJSON
-> Maybe (PreparedQuery 'MySQL)
-> m (DiffTime, EncJSON)
forall (m :: * -> *).
(MonadIO m, MonadQueryLog m, MonadTrace m, MonadError QErr m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> SourceConfig 'MySQL
-> TraceT (ExceptT QErr IO) EncJSON
-> Maybe (PreparedQuery 'MySQL)
-> m (DiffTime, EncJSON)
runQuery
runDBQueryExplain :: DBStepInfo 'MySQL -> m EncJSON
runDBQueryExplain = DBStepInfo 'MySQL -> m EncJSON
forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
DBStepInfo 'MySQL -> m EncJSON
runQueryExplain
runDBMutation :: RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> SourceConfig 'MySQL
-> ExecutionMonad 'MySQL EncJSON
-> Maybe (PreparedQuery 'MySQL)
-> m (DiffTime, EncJSON)
runDBMutation = [Char]
-> RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> SourceConfig
-> TraceT (ExceptT QErr IO) EncJSON
-> Maybe Text
-> m (DiffTime, EncJSON)
forall a. HasCallStack => [Char] -> a
error [Char]
"runDBMutation: MySQL backend does not support this operation yet."
runDBSubscription :: SourceConfig 'MySQL
-> MultiplexedQuery 'MySQL
-> [(CohortId, CohortVariables)]
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
runDBSubscription = [Char]
-> SourceConfig
-> Void
-> [(CohortId, CohortVariables)]
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall a. HasCallStack => [Char] -> a
error [Char]
"runDBSubscription: MySQL backend does not support this operation yet."
runDBStreamingSubscription :: SourceConfig 'MySQL
-> MultiplexedQuery 'MySQL
-> [(CohortId, CohortVariables)]
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
runDBStreamingSubscription = [Char]
-> SourceConfig
-> Void
-> [(CohortId, CohortVariables)]
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall a. HasCallStack => [Char] -> a
error [Char]
"runDBStreamingSubscription: MySQL backend does not support this operation yet"
runQuery ::
( MonadIO m,
MonadQueryLog m,
MonadTrace m,
MonadError QErr m
) =>
RequestId ->
GQLReqUnparsed ->
RootFieldAlias ->
UserInfo ->
L.Logger L.Hasura ->
SourceConfig 'MySQL ->
Tracing.TraceT (ExceptT QErr IO) EncJSON ->
Maybe (PreparedQuery 'MySQL) ->
m (DiffTime, EncJSON)
runQuery :: RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> SourceConfig 'MySQL
-> TraceT (ExceptT QErr IO) EncJSON
-> Maybe (PreparedQuery 'MySQL)
-> m (DiffTime, EncJSON)
runQuery RequestId
reqId GQLReqUnparsed
query RootFieldAlias
fieldName UserInfo
_userInfo Logger Hasura
logger SourceConfig 'MySQL
_sourceConfig TraceT (ExceptT QErr IO) EncJSON
tx Maybe (PreparedQuery 'MySQL)
genSql = do
Logger Hasura -> QueryLog -> m ()
forall (m :: * -> *).
MonadQueryLog m =>
Logger Hasura -> QueryLog -> m ()
logQueryLog Logger Hasura
logger (QueryLog -> m ()) -> QueryLog -> m ()
forall a b. (a -> b) -> a -> b
$ GQLReqUnparsed
-> RootFieldAlias
-> Maybe (PreparedQuery 'MySQL)
-> RequestId
-> QueryLog
mkQueryLog GQLReqUnparsed
query RootFieldAlias
fieldName Maybe (PreparedQuery 'MySQL)
genSql RequestId
reqId
m EncJSON -> m (DiffTime, EncJSON)
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime (m EncJSON -> m (DiffTime, EncJSON))
-> m EncJSON -> m (DiffTime, EncJSON)
forall a b. (a -> b) -> a -> b
$
Text -> m EncJSON -> m EncJSON
forall (m :: * -> *) a. MonadTrace m => Text -> m a -> m a
trace (Text
"MySQL Query for root field " Text -> RootFieldAlias -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RootFieldAlias
fieldName) (m EncJSON -> m EncJSON) -> m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$
(ExceptT QErr IO (EncJSON, TracingMetadata)
-> m (EncJSON, TracingMetadata))
-> TraceT (ExceptT QErr IO) EncJSON -> m EncJSON
forall (n :: * -> *) (m :: * -> *) a b.
MonadTrace n =>
(m (a, TracingMetadata) -> n (b, TracingMetadata))
-> TraceT m a -> n b
Tracing.interpTraceT ExceptT QErr IO (EncJSON, TracingMetadata)
-> m (EncJSON, TracingMetadata)
forall (m :: * -> *) a.
(MonadIO m, MonadError QErr m) =>
ExceptT QErr IO a -> m a
run TraceT (ExceptT QErr IO) EncJSON
tx
run :: (MonadIO m, MonadError QErr m) => ExceptT QErr IO a -> m a
run :: ExceptT QErr IO a -> m a
run ExceptT QErr IO a
action = do
Either QErr a
result <- IO (Either QErr a) -> m (Either QErr a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either QErr a) -> m (Either QErr a))
-> IO (Either QErr a) -> m (Either QErr a)
forall a b. (a -> b) -> a -> b
$ ExceptT QErr IO a -> IO (Either QErr a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT QErr IO a
action
Either QErr a
result Either QErr a -> (QErr -> m a) -> m a
forall (m :: * -> *) e a.
Applicative m =>
Either e a -> (e -> m a) -> m a
`onLeft` QErr -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
runQueryExplain ::
( MonadIO m,
MonadError QErr m
) =>
DBStepInfo 'MySQL ->
m EncJSON
runQueryExplain :: DBStepInfo 'MySQL -> m EncJSON
runQueryExplain (DBStepInfo SourceName
_ SourceConfig 'MySQL
_ Maybe (PreparedQuery 'MySQL)
_ ExecutionMonad 'MySQL EncJSON
action) = ExceptT QErr IO EncJSON -> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadError QErr m) =>
ExceptT QErr IO a -> m a
run (ExceptT QErr IO EncJSON -> m EncJSON)
-> ExceptT QErr IO EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ Reporter
-> Text
-> TraceT (ExceptT QErr IO) EncJSON
-> ExceptT QErr IO EncJSON
forall (m :: * -> *) a.
MonadIO m =>
Reporter -> Text -> TraceT m a -> m a
runTraceTWithReporter Reporter
noReporter Text
"explain" TraceT (ExceptT QErr IO) EncJSON
ExecutionMonad 'MySQL EncJSON
action
mkQueryLog ::
GQLReqUnparsed ->
RootFieldAlias ->
Maybe (PreparedQuery 'MySQL) ->
RequestId ->
QueryLog
mkQueryLog :: GQLReqUnparsed
-> RootFieldAlias
-> Maybe (PreparedQuery 'MySQL)
-> RequestId
-> QueryLog
mkQueryLog GQLReqUnparsed
gqlQuery RootFieldAlias
fieldName Maybe (PreparedQuery 'MySQL)
preparedSql RequestId
requestId =
GQLReqUnparsed
-> Maybe (RootFieldAlias, GeneratedQuery)
-> RequestId
-> QueryLogKind
-> QueryLog
QueryLog GQLReqUnparsed
gqlQuery ((RootFieldAlias
fieldName,) (GeneratedQuery -> (RootFieldAlias, GeneratedQuery))
-> Maybe GeneratedQuery -> Maybe (RootFieldAlias, GeneratedQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GeneratedQuery
generatedQuery) RequestId
requestId QueryLogKind
QueryLogKindDatabase
where
generatedQuery :: Maybe GeneratedQuery
generatedQuery =
Maybe Text
Maybe (PreparedQuery 'MySQL)
preparedSql Maybe Text -> (Text -> GeneratedQuery) -> Maybe GeneratedQuery
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Text
queryString ->
Text -> Value -> GeneratedQuery
GeneratedQuery Text
queryString Value
J.Null