grafana / sqlds

A package that assists writing SQL-driven datasources
Apache License 2.0
17 stars 12 forks source link

Make it possible to pass args to driver query method #20

Closed sunker closed 3 years ago

sunker commented 3 years ago

Some data sources plugin, such as the one for Athena, will need to be able to switch database inside the query editor without having to change the connection in the config editor. The query method in the go sql package takes a variadic function parameter for args, so we should be able to use that to pass additional arguments to the driver.

Unfortunately, the args might differ from plugin to plugin, making it hard to provide proper typings for this. For that reason, I think the Query type needs to take the args as json. Something like this:

type Query struct {
    RawSQL string            `json:"rawSql"`
    Format FormatQueryOption `json:"format"`
    Args   json.RawMessage   `json:args`
...
}
andresmgot commented 3 years ago

The query method in the go sql package takes a variadic function parameter for args, so we should be able to use that to pass additional arguments to the driver.

@sunker I am taking a look to this and I think we should not use that args parameter to do this. These arguments are SQL arguments, the ones used to run the query (e.g. "select foo from $1", arg1") . The sql package is reading those. We would need those parameter to pass arguments to the SQL query (although that's not supported).

In any case, we can still use this package Query to add some args so the targeting datasource can act on it. I will give that a try.