Skip to content

Commit 4d0eef4

Browse files
authored
sqlds - Allow ctx mod (#127)
* adds QueryDataMutator hook * adds checkhealth pre-hook * checkhealthmutator
1 parent e147223 commit 4d0eef4

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

datasource.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func (ds *SQLDatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
9898

9999
wg.Add(len(req.Queries))
100100

101+
if queryDataMutator, ok := ds.driver().(QueryDataMutator); ok {
102+
ctx, req = queryDataMutator.MutateQueryData(ctx, req)
103+
}
104+
101105
// Execute each query and store the results by query RefID
102106
for _, q := range req.Queries {
103107
go func(query backend.DataQuery) {
@@ -243,6 +247,9 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
243247

244248
// CheckHealth pings the connected SQL database
245249
func (ds *SQLDatasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
250+
if checkHealthMutator, ok := ds.driver().(CheckHealthMutator); ok {
251+
ctx, req = checkHealthMutator.MutateCheckHealth(ctx, req)
252+
}
246253
healthChecker := &HealthChecker{
247254
Connector: ds.connector,
248255
Metrics: ds.metrics.WithEndpoint(EndpointHealth),

driver.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ type Connection interface {
4242
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
4343
}
4444

45+
// QueryDataMutator is an additional interface that could be implemented by driver.
46+
// This adds ability to the driver to optionally mutate the query before it's run
47+
// with the QueryDataRequest.
48+
type QueryDataMutator interface {
49+
MutateQueryData(ctx context.Context, req *backend.QueryDataRequest) (context.Context, *backend.QueryDataRequest)
50+
}
51+
52+
// CheckHealthMutator is an additional interface that could be implemented by driver.
53+
// This adds ability to the driver to optionally mutate the CheckHealth before it's run
54+
type CheckHealthMutator interface {
55+
MutateCheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (context.Context, *backend.CheckHealthRequest)
56+
}
57+
4558
// QueryMutator is an additional interface that could be implemented by driver.
4659
// This adds ability to the driver it can mutate query before run.
4760
type QueryMutator interface {

0 commit comments

Comments
 (0)