Merge pull request #7688 from harryrose/master

Add bound parameters map to Query object
pull/7761/head
Jonathan A. Sternberg 2016-12-23 17:08:14 -06:00 committed by GitHub
commit 0a04499992
3 changed files with 74 additions and 6 deletions

View File

@ -19,6 +19,7 @@ The stress tool `influx_stress` will be removed in a subsequent release. We reco
- [#7036](https://github.com/influxdata/influxdb/issues/7036): Switch logging to use structured logging everywhere. - [#7036](https://github.com/influxdata/influxdb/issues/7036): Switch logging to use structured logging everywhere.
- [#3188](https://github.com/influxdata/influxdb/issues/3188): [CLI feature request] USE retention policy for queries. - [#3188](https://github.com/influxdata/influxdb/issues/3188): [CLI feature request] USE retention policy for queries.
- [#7709](https://github.com/influxdata/influxdb/pull/7709): Add clear command to cli. - [#7709](https://github.com/influxdata/influxdb/pull/7709): Add clear command to cli.
- [#7688](https://github.com/influxdata/influxdb/pull/7688): Adding ability to use parameters in queries in the v2 client using the `Parameters` map in the `Query` struct.
### Bugfixes ### Bugfixes

View File

@ -400,9 +400,10 @@ func (c *client) Write(bp BatchPoints) error {
// Query defines a query to send to the server // Query defines a query to send to the server
type Query struct { type Query struct {
Command string Command string
Database string Database string
Precision string Precision string
Parameters map[string]interface{}
} }
// NewQuery returns a query object // NewQuery returns a query object
@ -410,9 +411,24 @@ type Query struct {
// for the query. // for the query.
func NewQuery(command, database, precision string) Query { func NewQuery(command, database, precision string) Query {
return Query{ return Query{
Command: command, Command: command,
Database: database, Database: database,
Precision: precision, Precision: precision,
Parameters: make(map[string]interface{}),
}
}
// NewQueryWithParameters returns a query object
// database and precision strings can be empty strings if they are not needed
// for the query.
// parameters is a map of the parameter names used in the command to their
// values.
func NewQueryWithParameters(command, database, precision string, parameters map[string]interface{}) Query {
return Query{
Command: command,
Database: database,
Precision: precision,
Parameters: parameters,
} }
} }
@ -454,12 +470,20 @@ func (c *client) Query(q Query) (*Response, error) {
u := c.url u := c.url
u.Path = "query" u.Path = "query"
jsonParameters, err := json.Marshal(q.Parameters)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", u.String(), nil) req, err := http.NewRequest("POST", u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Content-Type", "") req.Header.Set("Content-Type", "")
req.Header.Set("User-Agent", c.useragent) req.Header.Set("User-Agent", c.useragent)
if c.username != "" { if c.username != "" {
req.SetBasicAuth(c.username, c.password) req.SetBasicAuth(c.username, c.password)
} }
@ -467,6 +491,8 @@ func (c *client) Query(q Query) (*Response, error) {
params := req.URL.Query() params := req.URL.Query()
params.Set("q", q.Command) params.Set("q", q.Command)
params.Set("db", q.Database) params.Set("db", q.Database)
params.Set("params", string(jsonParameters))
if q.Precision != "" { if q.Precision != "" {
params.Set("epoch", q.Precision) params.Set("epoch", q.Precision)
} }

View File

@ -153,6 +153,47 @@ func TestClient_Query(t *testing.T) {
} }
} }
func TestClient_BoundParameters(t *testing.T) {
var parameterString string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data Response
r.ParseForm()
parameterString = r.FormValue("params")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
defer ts.Close()
config := HTTPConfig{Addr: ts.URL}
c, _ := NewHTTPClient(config)
defer c.Close()
expectedParameters := map[string]interface{}{
"testStringParameter": "testStringValue",
"testNumberParameter": 12.3,
}
query := Query{
Parameters: expectedParameters,
}
_, err := c.Query(query)
if err != nil {
t.Errorf("unexpected error. expected %v, actual %v", nil, err)
}
var actualParameters map[string]interface{}
err = json.Unmarshal([]byte(parameterString), &actualParameters)
if err != nil {
t.Errorf("unexpected error. expected %v, actual %v", nil, err)
}
if !reflect.DeepEqual(expectedParameters, actualParameters) {
t.Errorf("unexpected parameters. expected %v, actual %v", expectedParameters, actualParameters)
}
}
func TestClient_BasicAuth(t *testing.T) { func TestClient_BasicAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth() u, p, ok := r.BasicAuth()