2021-07-15 15:44:26 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Backends.MySQL.Instances.Execute () where
|
2021-07-15 15:44:26 +03:00
|
|
|
|
2021-10-18 19:04:30 +03:00
|
|
|
import Data.Aeson as J
|
2021-10-23 14:42:20 +03:00
|
|
|
import Data.Bifunctor
|
|
|
|
import Data.Coerce
|
2022-07-19 04:51:42 +03:00
|
|
|
import Data.Environment qualified as Env
|
2021-10-18 19:04:30 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.Text qualified as T
|
2021-10-23 14:42:20 +03:00
|
|
|
import Data.Text.Encoding qualified as T
|
2021-10-18 19:04:30 +03:00
|
|
|
import Data.Tree
|
2021-10-23 14:42:20 +03:00
|
|
|
import Database.MySQL.Base (fetchFields, query, storeResult)
|
|
|
|
import Hasura.Backends.MySQL.Connection
|
2021-10-18 19:04:30 +03:00
|
|
|
import Hasura.Backends.MySQL.DataLoader.Execute (OutputValue (..), RecordSet (..))
|
|
|
|
import Hasura.Backends.MySQL.DataLoader.Execute qualified as DataLoader
|
|
|
|
import Hasura.Backends.MySQL.DataLoader.Plan qualified as DataLoader
|
|
|
|
import Hasura.Backends.MySQL.Plan
|
2021-10-23 14:42:20 +03:00
|
|
|
import Hasura.Backends.MySQL.ToQuery as ToQuery
|
|
|
|
import Hasura.Backends.MySQL.Types qualified as MySQL
|
2021-10-18 19:04:30 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
2021-09-22 13:43:05 +03:00
|
|
|
import Hasura.GraphQL.Execute.Backend
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.Namespace
|
2021-10-23 14:42:20 +03:00
|
|
|
import Hasura.Prelude hiding (first, second)
|
2021-10-18 19:04:30 +03:00
|
|
|
import Hasura.RQL.IR
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Common
|
2021-10-23 14:42:20 +03:00
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.SQL.Backend
|
2021-10-18 19:04:30 +03:00
|
|
|
import Hasura.Session
|
2023-01-25 10:12:53 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
import Network.HTTP.Types qualified as HTTP
|
2021-07-15 15:44:26 +03:00
|
|
|
|
|
|
|
instance BackendExecute 'MySQL where
|
|
|
|
type PreparedQuery 'MySQL = Text
|
|
|
|
type MultiplexedQuery 'MySQL = Void
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
type ExecutionMonad 'MySQL = IdentityT
|
2022-07-19 04:51:42 +03:00
|
|
|
|
2021-08-04 14:42:24 +03:00
|
|
|
mkDBQueryPlan = mysqlDBQueryPlan
|
|
|
|
mkDBMutationPlan = error "mkDBMutationPlan: MySQL backend does not support this operation yet."
|
2023-01-25 10:12:53 +03:00
|
|
|
mkLiveQuerySubscriptionPlan _ _ _ _ _ _ = error "mkLiveQuerySubscriptionPlan: MySQL backend does not support this operation yet."
|
|
|
|
mkDBStreamingSubscriptionPlan _ _ _ _ _ = error "mkDBStreamingSubscriptionPlan: MySQL backend does not support this operation yet."
|
2021-10-23 14:42:20 +03:00
|
|
|
mkDBQueryExplain = mysqlDBQueryExplain
|
2022-03-21 13:39:49 +03:00
|
|
|
mkSubscriptionExplain _ = error "mkSubscriptionExplain: MySQL backend does not support this operation yet."
|
2021-10-18 19:04:30 +03:00
|
|
|
mkDBRemoteRelationshipPlan = error "mkDBRemoteRelationshipPlan: MySQL does not support this operation yet."
|
2021-09-22 13:43:05 +03:00
|
|
|
|
2021-08-04 14:42:24 +03:00
|
|
|
mysqlDBQueryPlan ::
|
|
|
|
forall m.
|
|
|
|
( MonadError QErr m
|
|
|
|
) =>
|
|
|
|
UserInfo ->
|
2022-07-19 04:51:42 +03:00
|
|
|
Env.Environment ->
|
2021-10-18 19:04:30 +03:00
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MySQL ->
|
2021-12-07 16:12:02 +03:00
|
|
|
QueryDB 'MySQL Void (UnpreparedValue 'MySQL) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2021-08-04 14:42:24 +03:00
|
|
|
m (DBStepInfo 'MySQL)
|
2023-01-25 10:12:53 +03:00
|
|
|
mysqlDBQueryPlan userInfo _env sourceName sourceConfig qrf _ _ = do
|
2021-10-18 19:04:30 +03:00
|
|
|
(headAndTail, actionsForest) <- queryToActionForest userInfo qrf
|
|
|
|
pure
|
|
|
|
( DBStepInfo
|
|
|
|
@'MySQL
|
|
|
|
sourceName
|
|
|
|
sourceConfig
|
|
|
|
(Just (T.pack (drawForest (fmap (fmap show) actionsForest))))
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
( OnBaseMonad do
|
2021-10-18 19:04:30 +03:00
|
|
|
result <-
|
|
|
|
DataLoader.runExecute
|
|
|
|
sourceConfig
|
|
|
|
headAndTail
|
|
|
|
(DataLoader.execute actionsForest)
|
|
|
|
either
|
|
|
|
(throw500WithDetail "MySQL DataLoader Error" . toJSON . show)
|
|
|
|
(pure . encJFromRecordSet)
|
|
|
|
result
|
|
|
|
)
|
2023-01-25 10:12:53 +03:00
|
|
|
()
|
2021-10-18 19:04:30 +03:00
|
|
|
)
|
2021-09-22 13:43:05 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2021-10-18 19:04:30 +03:00
|
|
|
-- Encoding for Hasura's GraphQL JSON representation
|
2021-09-22 13:43:05 +03:00
|
|
|
|
2021-10-23 14:42:20 +03:00
|
|
|
mysqlDBQueryExplain ::
|
|
|
|
MonadError QErr m =>
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-10-23 14:42:20 +03:00
|
|
|
UserInfo ->
|
|
|
|
SourceName ->
|
|
|
|
SourceConfig 'MySQL ->
|
2021-12-07 16:12:02 +03:00
|
|
|
QueryDB 'MySQL Void (UnpreparedValue 'MySQL) ->
|
2023-01-25 10:12:53 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
Maybe G.Name ->
|
2021-10-23 14:42:20 +03:00
|
|
|
m (AB.AnyBackend DBStepInfo)
|
2023-01-25 10:12:53 +03:00
|
|
|
mysqlDBQueryExplain fieldName userInfo sourceName sourceConfig qrf _ _ = do
|
2021-10-23 14:42:20 +03:00
|
|
|
select :: MySQL.Select <- planQuery (_uiSession userInfo) qrf
|
|
|
|
let sqlQuery = selectSQLTextForQuery select
|
|
|
|
sqlQueryText = (T.decodeUtf8 . unQuery . toQueryPretty) (ToQuery.fromSelect select)
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
explainResult = OnBaseMonad $
|
2021-10-23 14:42:20 +03:00
|
|
|
withMySQLPool
|
|
|
|
(MySQL.scConnectionPool sourceConfig)
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
\conn -> do
|
|
|
|
query conn ("EXPLAIN FORMAT=JSON " <> (unQuery sqlQuery))
|
|
|
|
result <- storeResult conn
|
|
|
|
fields <- fetchFields result
|
|
|
|
rows <- fetchAllRows result
|
|
|
|
let texts = concat $ parseTextRows fields rows
|
|
|
|
pure $ encJFromJValue $ ExplainPlan fieldName (Just sqlQueryText) (Just texts)
|
2021-10-23 14:42:20 +03:00
|
|
|
pure $
|
|
|
|
AB.mkAnyBackend $
|
2023-01-25 10:12:53 +03:00
|
|
|
DBStepInfo @'MySQL sourceName sourceConfig Nothing explainResult ()
|
2021-10-23 14:42:20 +03:00
|
|
|
|
|
|
|
selectSQLTextForQuery :: MySQL.Select -> ToQuery.Query
|
|
|
|
selectSQLTextForQuery select = toQueryFlat $ ToQuery.fromSelect select
|
|
|
|
|
2021-10-18 19:04:30 +03:00
|
|
|
encJFromRecordSet :: RecordSet -> EncJSON
|
|
|
|
encJFromRecordSet RecordSet {rows} =
|
|
|
|
encJFromList
|
|
|
|
( map
|
|
|
|
( encJFromAssocList
|
|
|
|
. map (first coerce . second encJFromOutputValue)
|
|
|
|
. OMap.toList
|
|
|
|
)
|
|
|
|
(toList rows)
|
|
|
|
)
|
|
|
|
|
|
|
|
encJFromOutputValue :: DataLoader.OutputValue -> EncJSON
|
|
|
|
encJFromOutputValue =
|
|
|
|
\case
|
|
|
|
ArrayOutputValue array -> encJFromList (map encJFromOutputValue (toList array))
|
|
|
|
RecordOutputValue m ->
|
|
|
|
encJFromAssocList
|
|
|
|
. map (first coerce . second encJFromOutputValue)
|
|
|
|
. OMap.toList
|
|
|
|
$ m
|
|
|
|
ScalarOutputValue value -> encJFromJValue value
|
|
|
|
NullOutputValue {} -> encJFromJValue J.Null
|