2021-02-23 20:37:27 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2021-05-21 14:37:34 +03:00
|
|
|
module Hasura.Backends.MSSQL.Instances.Execute
|
|
|
|
(
|
|
|
|
MultiplexedQuery'(..),
|
|
|
|
multiplexRootReselect,
|
|
|
|
)
|
|
|
|
where
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2021-04-20 19:57:14 +03:00
|
|
|
import qualified Data.Aeson.Extended as J
|
2021-06-08 06:50:24 +03:00
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Data.HashSet as Set
|
2021-04-20 19:57:14 +03:00
|
|
|
import qualified Data.List.NonEmpty as NE
|
|
|
|
import qualified Data.Text.Extended as T
|
2021-02-23 20:37:27 +03:00
|
|
|
import qualified Database.ODBC.SQLServer as ODBC
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
import qualified Hasura.RQL.Types.Column as RQL
|
2021-03-15 16:02:58 +03:00
|
|
|
import qualified Hasura.SQL.AnyBackend as AB
|
|
|
|
|
2021-02-25 21:15:55 +03:00
|
|
|
import Hasura.Backends.MSSQL.Connection
|
2021-04-20 19:57:14 +03:00
|
|
|
import Hasura.Backends.MSSQL.FromIr as TSQL
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.Backends.MSSQL.Plan
|
2021-06-08 06:50:24 +03:00
|
|
|
import Hasura.Backends.MSSQL.SQL.Value (txtEncodedColVal)
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.Backends.MSSQL.ToQuery
|
2021-04-20 19:57:14 +03:00
|
|
|
import Hasura.Backends.MSSQL.Types as TSQL
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
|
|
|
import Hasura.GraphQL.Parser
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.RQL.IR
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.Session
|
|
|
|
|
|
|
|
|
|
|
|
instance BackendExecute 'MSSQL where
|
2021-06-01 13:04:29 +03:00
|
|
|
type PreparedQuery 'MSSQL = Text
|
2021-04-20 19:57:14 +03:00
|
|
|
type MultiplexedQuery 'MSSQL = MultiplexedQuery'
|
2021-02-25 21:15:55 +03:00
|
|
|
type ExecutionMonad 'MSSQL = ExceptT QErr IO
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
mkDBQueryPlan = msDBQueryPlan
|
|
|
|
mkDBMutationPlan = msDBMutationPlan
|
|
|
|
mkDBSubscriptionPlan = msDBSubscriptionPlan
|
2021-04-13 14:10:08 +03:00
|
|
|
mkDBQueryExplain = msDBQueryExplain
|
|
|
|
mkLiveQueryExplain = msDBLiveQueryExplain
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
2021-06-01 13:04:29 +03:00
|
|
|
-- Multiplexed query
|
2021-05-21 14:37:34 +03:00
|
|
|
|
2021-04-20 19:57:14 +03:00
|
|
|
newtype MultiplexedQuery' = MultiplexedQuery' Reselect
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-04-20 19:57:14 +03:00
|
|
|
instance T.ToTxt MultiplexedQuery' where
|
|
|
|
toTxt (MultiplexedQuery' reselect) = T.toTxt $ toQueryPretty $ fromReselect reselect
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
2021-06-01 13:04:29 +03:00
|
|
|
-- Query
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
msDBQueryPlan
|
|
|
|
:: forall m.
|
|
|
|
( MonadError QErr m
|
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-02-23 20:37:27 +03:00
|
|
|
-> SourceConfig 'MSSQL
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB 'MSSQL (Const Void) (UnpreparedValue 'MSSQL)
|
2021-06-11 06:26:50 +03:00
|
|
|
-> m (DBStepInfo 'MSSQL)
|
|
|
|
msDBQueryPlan userInfo sourceName sourceConfig qrf = do
|
2021-05-21 14:37:34 +03:00
|
|
|
let sessionVariables = _uiSession userInfo
|
2021-06-01 13:04:29 +03:00
|
|
|
statement <- planQuery sessionVariables qrf
|
|
|
|
let printer = fromSelect statement
|
2021-05-21 14:37:34 +03:00
|
|
|
queryString = ODBC.renderQuery $ toQueryPretty printer
|
2021-02-25 21:15:55 +03:00
|
|
|
pool = _mscConnectionPool sourceConfig
|
2021-05-21 14:37:34 +03:00
|
|
|
odbcQuery = encJFromText <$> runJSONPathQuery pool (toQueryFlat printer)
|
2021-06-11 06:26:50 +03:00
|
|
|
pure $ DBStepInfo @'MSSQL sourceName sourceConfig (Just $ queryString) odbcQuery
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-05-11 13:04:38 +03:00
|
|
|
runShowplan
|
|
|
|
:: ODBC.Query -> ODBC.Connection -> IO [Text]
|
|
|
|
runShowplan query conn = do
|
|
|
|
ODBC.exec conn "SET SHOWPLAN_TEXT ON"
|
|
|
|
texts <- ODBC.query conn query
|
|
|
|
ODBC.exec conn "SET SHOWPLAN_TEXT OFF"
|
|
|
|
-- we don't need to use 'finally' here - if an exception occurs,
|
|
|
|
-- the connection is removed from the resource pool in 'withResource'.
|
|
|
|
pure texts
|
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
msDBQueryExplain
|
2021-04-20 19:57:14 +03:00
|
|
|
:: MonadError QErr m
|
2021-04-13 14:10:08 +03:00
|
|
|
=> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> SourceName
|
|
|
|
-> SourceConfig 'MSSQL
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB 'MSSQL (Const Void) (UnpreparedValue 'MSSQL)
|
2021-04-13 14:10:08 +03:00
|
|
|
-> m (AB.AnyBackend DBStepInfo)
|
|
|
|
msDBQueryExplain fieldName userInfo sourceName sourceConfig qrf = do
|
2021-05-21 14:37:34 +03:00
|
|
|
let sessionVariables = _uiSession userInfo
|
2021-06-01 13:04:29 +03:00
|
|
|
statement <- planQuery sessionVariables qrf
|
|
|
|
let query = toQueryPretty (fromSelect statement)
|
2021-05-21 14:37:34 +03:00
|
|
|
queryString = ODBC.renderQuery $ query
|
|
|
|
pool = _mscConnectionPool sourceConfig
|
|
|
|
odbcQuery =
|
2021-05-11 13:04:38 +03:00
|
|
|
withMSSQLPool
|
|
|
|
pool
|
|
|
|
(\conn -> do
|
|
|
|
showplan <- runShowplan query conn
|
|
|
|
pure (encJFromJValue $
|
|
|
|
ExplainPlan
|
|
|
|
fieldName
|
|
|
|
(Just queryString)
|
|
|
|
(Just showplan)))
|
2021-04-13 14:10:08 +03:00
|
|
|
pure
|
|
|
|
$ AB.mkAnyBackend
|
2021-04-22 00:44:37 +03:00
|
|
|
$ DBStepInfo @'MSSQL sourceName sourceConfig Nothing odbcQuery
|
2021-04-13 14:10:08 +03:00
|
|
|
|
|
|
|
msDBLiveQueryExplain
|
2021-05-11 13:04:38 +03:00
|
|
|
:: (MonadIO m, MonadError QErr m)
|
2021-04-13 14:10:08 +03:00
|
|
|
=> LiveQueryPlan 'MSSQL (MultiplexedQuery 'MSSQL) -> m LiveQueryPlanExplanation
|
2021-05-11 13:04:38 +03:00
|
|
|
msDBLiveQueryExplain (LiveQueryPlan plan sourceConfig variables) = do
|
|
|
|
let (MultiplexedQuery' reselect) = _plqpQuery plan
|
|
|
|
query = toQueryPretty $ fromSelect $ multiplexRootReselect [(dummyCohortId, variables)] reselect
|
|
|
|
pool = _mscConnectionPool sourceConfig
|
|
|
|
explainInfo <- withMSSQLPool pool (runShowplan query)
|
|
|
|
pure $ LiveQueryPlanExplanation (T.toTxt query) explainInfo variables
|
2021-04-20 19:57:14 +03:00
|
|
|
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
2021-04-20 19:57:14 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Producing the correct SQL-level list comprehension to multiplex a query
|
|
|
|
|
|
|
|
-- Problem description:
|
|
|
|
--
|
|
|
|
-- Generate a query that repeats the same query N times but with
|
|
|
|
-- certain slots replaced:
|
|
|
|
--
|
|
|
|
-- [ Select x y | (x,y) <- [..] ]
|
|
|
|
--
|
|
|
|
|
|
|
|
multiplexRootReselect
|
|
|
|
:: [(CohortId, CohortVariables)]
|
|
|
|
-> TSQL.Reselect
|
|
|
|
-> TSQL.Select
|
|
|
|
multiplexRootReselect variables rootReselect =
|
|
|
|
Select
|
|
|
|
{ selectTop = NoTop
|
|
|
|
, selectProjections =
|
|
|
|
[ FieldNameProjection
|
|
|
|
Aliased
|
|
|
|
{ aliasedThing =
|
|
|
|
TSQL.FieldName
|
|
|
|
{fieldNameEntity = rowAlias, fieldName = resultIdAlias}
|
|
|
|
, aliasedAlias = resultIdAlias
|
|
|
|
}
|
|
|
|
, ExpressionProjection
|
|
|
|
Aliased
|
|
|
|
{ aliasedThing =
|
|
|
|
ColumnExpression
|
|
|
|
(TSQL.FieldName
|
|
|
|
{ fieldNameEntity = resultAlias
|
|
|
|
, fieldName = TSQL.jsonFieldName
|
|
|
|
})
|
|
|
|
, aliasedAlias = resultAlias
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, selectFrom =
|
2021-06-08 06:50:24 +03:00
|
|
|
Just $ FromOpenJson
|
2021-04-20 19:57:14 +03:00
|
|
|
Aliased
|
|
|
|
{ aliasedThing =
|
|
|
|
OpenJson
|
|
|
|
{ openJsonExpression =
|
|
|
|
ValueExpression (ODBC.TextValue $ lbsToTxt $ J.encode variables)
|
2021-07-08 23:49:10 +03:00
|
|
|
, openJsonWith = Just $
|
2021-04-20 19:57:14 +03:00
|
|
|
NE.fromList
|
|
|
|
[ UuidField resultIdAlias (Just $ IndexPath RootPath 0)
|
|
|
|
, JsonField resultVarsAlias (Just $ IndexPath RootPath 1)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
, aliasedAlias = rowAlias
|
|
|
|
}
|
|
|
|
, selectJoins =
|
|
|
|
[ Join
|
|
|
|
{ joinSource = JoinReselect rootReselect
|
|
|
|
, joinJoinAlias =
|
|
|
|
JoinAlias
|
|
|
|
{ joinAliasEntity = resultAlias
|
|
|
|
, joinAliasField = Just TSQL.jsonFieldName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, selectWhere = Where mempty
|
|
|
|
, selectFor =
|
|
|
|
JsonFor ForJson {jsonCardinality = JsonArray, jsonRoot = NoRoot}
|
|
|
|
, selectOrderBy = Nothing
|
|
|
|
, selectOffset = Nothing
|
|
|
|
}
|
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
-- mutation
|
|
|
|
|
|
|
|
msDBMutationPlan
|
|
|
|
:: forall m.
|
|
|
|
( MonadError QErr m
|
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-02-23 20:37:27 +03:00
|
|
|
-> Bool
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-02-23 20:37:27 +03:00
|
|
|
-> SourceConfig 'MSSQL
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> MutationDB 'MSSQL (Const Void) (UnpreparedValue 'MSSQL)
|
2021-06-11 06:26:50 +03:00
|
|
|
-> m (DBStepInfo 'MSSQL)
|
|
|
|
msDBMutationPlan _userInfo _stringifyNum _sourceName _sourceConfig _mrf =
|
2021-02-23 20:37:27 +03:00
|
|
|
throw500 "mutations are not supported in MSSQL; this should be unreachable"
|
|
|
|
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
-- subscription
|
|
|
|
|
|
|
|
msDBSubscriptionPlan
|
|
|
|
:: forall m.
|
|
|
|
( MonadError QErr m
|
2021-06-08 06:50:24 +03:00
|
|
|
, MonadIO m
|
2021-02-23 20:37:27 +03:00
|
|
|
)
|
|
|
|
=> UserInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-02-23 20:37:27 +03:00
|
|
|
-> SourceConfig 'MSSQL
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> InsOrdHashMap G.Name (QueryDB 'MSSQL (Const Void) (UnpreparedValue 'MSSQL))
|
2021-02-23 20:37:27 +03:00
|
|
|
-> m (LiveQueryPlan 'MSSQL (MultiplexedQuery 'MSSQL))
|
2021-04-20 19:57:14 +03:00
|
|
|
msDBSubscriptionPlan UserInfo {_uiSession, _uiRole} _sourceName sourceConfig rootFields = do
|
2021-05-21 14:37:34 +03:00
|
|
|
(reselect, prepareState) <- planSubscription rootFields _uiSession
|
|
|
|
|
2021-06-08 06:50:24 +03:00
|
|
|
cohortVariables <- prepareStateCohortVariables sourceConfig _uiSession prepareState
|
|
|
|
let parameterizedPlan = ParameterizedLiveQueryPlan _uiRole $ MultiplexedQuery' reselect
|
2021-05-21 14:37:34 +03:00
|
|
|
|
|
|
|
pure
|
|
|
|
$ LiveQueryPlan parameterizedPlan sourceConfig cohortVariables
|
|
|
|
|
2021-06-08 06:50:24 +03:00
|
|
|
prepareStateCohortVariables :: (MonadError QErr m, MonadIO m) => SourceConfig 'MSSQL -> SessionVariables -> PrepareState -> m CohortVariables
|
|
|
|
prepareStateCohortVariables sourceConfig session prepState = do
|
|
|
|
(namedVars, posVars) <- validateVariables sourceConfig session prepState
|
|
|
|
let PrepareState{sessionVariables} = prepState
|
|
|
|
pure $ mkCohortVariables
|
2021-04-20 19:57:14 +03:00
|
|
|
sessionVariables
|
2021-05-21 14:37:34 +03:00
|
|
|
session
|
2021-06-08 06:50:24 +03:00
|
|
|
namedVars
|
|
|
|
posVars
|
|
|
|
|
|
|
|
|
|
|
|
-- | Ensure that the set of variables (with value instantiations) that occur in
|
|
|
|
-- a (RQL) query produce a well-formed and executable (SQL) query when
|
|
|
|
-- considered in isolation.
|
|
|
|
--
|
|
|
|
-- This helps avoiding cascading failures in multiplexed queries.
|
|
|
|
--
|
|
|
|
-- c.f. https://github.com/hasura/graphql-engine-mono/issues/1210.
|
|
|
|
validateVariables ::
|
|
|
|
(MonadError QErr m, MonadIO m) =>
|
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
SessionVariables ->
|
|
|
|
PrepareState ->
|
|
|
|
m (ValidatedQueryVariables, ValidatedSyntheticVariables)
|
|
|
|
validateVariables sourceConfig sessionVariableValues prepState = do
|
|
|
|
let PrepareState{sessionVariables, namedArguments, positionalArguments} = prepState
|
|
|
|
|
|
|
|
-- We generate a single 'canary' query in the form:
|
|
|
|
--
|
|
|
|
-- SELECT ... [session].[x-hasura-foo] as [x-hasura-foo], ... as a, ... as b, ...
|
|
|
|
-- FROM OPENJSON('...')
|
|
|
|
-- WITH ([x-hasura-foo] NVARCHAR(MAX)) as [session]
|
|
|
|
--
|
|
|
|
-- where 'a', 'b', etc. are aliases given to positional arguments.
|
|
|
|
-- Named arguments and session variables are aliased to themselves.
|
|
|
|
--
|
|
|
|
-- The idea being that if the canary query succeeds we can be
|
|
|
|
-- reasonably confident that adding these variables to a query being
|
|
|
|
-- polled will not crash the poller.
|
|
|
|
|
|
|
|
occSessionVars = filterSessionVariables
|
|
|
|
(\k _ -> Set.member k sessionVariables)
|
|
|
|
sessionVariableValues
|
|
|
|
|
|
|
|
expSes, expNamed, expPos :: [Aliased Expression]
|
|
|
|
expSes = sessionReference <$> getSessionVariables occSessionVars
|
|
|
|
expNamed = map (
|
|
|
|
\(n, v) -> Aliased (ValueExpression (RQL.cvValue v)) (G.unName n)
|
|
|
|
)
|
|
|
|
$ Map.toList $ namedArguments
|
|
|
|
|
|
|
|
-- For positional args we need to be a bit careful not to capture names
|
|
|
|
-- from expNamed and expSes (however unlikely)
|
|
|
|
expPos = zipWith (\n v -> Aliased (ValueExpression (RQL.cvValue v)) n)
|
|
|
|
(freshVars (expNamed <> expSes)) positionalArguments
|
|
|
|
|
|
|
|
projAll :: [Projection]
|
|
|
|
projAll = map ExpressionProjection (expSes <> expNamed <> expPos)
|
|
|
|
|
|
|
|
canaryQuery = if null projAll
|
|
|
|
then Nothing
|
2021-07-08 23:49:10 +03:00
|
|
|
else Just $ renderQuery emptySelect {
|
2021-06-08 06:50:24 +03:00
|
|
|
selectProjections = projAll,
|
|
|
|
selectFrom = sessionOpenJson occSessionVars
|
|
|
|
}
|
|
|
|
|
|
|
|
onJust canaryQuery (\q -> do
|
|
|
|
_ :: [[ODBC.Value]] <- withMSSQLPool (_mscConnectionPool sourceConfig) (`ODBC.query` q)
|
|
|
|
pure ()
|
|
|
|
)
|
|
|
|
|
|
|
|
pure (ValidatedVariables $ txtEncodedColVal <$> namedArguments,
|
|
|
|
ValidatedVariables $ txtEncodedColVal <$> positionalArguments)
|
|
|
|
|
|
|
|
where
|
|
|
|
renderQuery :: Select -> ODBC.Query
|
|
|
|
renderQuery = toQueryFlat . fromSelect
|
|
|
|
|
|
|
|
freshVars :: [Aliased a] -> [Text]
|
|
|
|
freshVars boundNames = filter (not . (`elem` map aliasedAlias boundNames)) chars
|
|
|
|
|
|
|
|
-- Infinite list of expression aliases.
|
|
|
|
chars :: [Text]
|
|
|
|
chars = [ y T.<>> x | y <- [""] <|> chars, x <- ['a'..'z']]
|
|
|
|
|
|
|
|
sessionOpenJson :: SessionVariables -> Maybe From
|
|
|
|
sessionOpenJson occSessionVars =
|
|
|
|
nonEmpty (getSessionVariables occSessionVars)
|
|
|
|
<&> \fields -> FromOpenJson $
|
|
|
|
Aliased
|
|
|
|
(
|
|
|
|
OpenJson
|
|
|
|
(ValueExpression $ ODBC.TextValue $ lbsToTxt $ J.encode occSessionVars)
|
2021-07-08 23:49:10 +03:00
|
|
|
(pure (sessField <$> fields))
|
2021-06-08 06:50:24 +03:00
|
|
|
)
|
|
|
|
"session"
|
|
|
|
|
|
|
|
sessField :: Text -> JsonFieldSpec
|
|
|
|
sessField var = StringField var Nothing
|
|
|
|
|
|
|
|
sessionReference :: Text -> Aliased Expression
|
|
|
|
sessionReference var = Aliased (ColumnExpression (TSQL.FieldName var "session")) var
|