Skip to content

Commit

Permalink
Merge pull request #7688 from harryrose/master
Browse files Browse the repository at this point in the history
Add bound parameters map to Query object
  • Loading branch information
jsternberg committed Dec 23, 2016
2 parents 759d78f + 0cb456b commit 0a04499
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- [#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.
- [#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

Expand Down
38 changes: 32 additions & 6 deletions client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,35 @@ func (c *client) Write(bp BatchPoints) error {

// Query defines a query to send to the server
type Query struct {
Command string
Database string
Precision string
Command string
Database string
Precision string
Parameters map[string]interface{}
}

// NewQuery returns a query object
// database and precision strings can be empty strings if they are not needed
// for the query.
func NewQuery(command, database, precision string) Query {
return Query{
Command: command,
Database: database,
Precision: precision,
Command: command,
Database: database,
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,
}
}

Expand Down Expand Up @@ -454,19 +470,29 @@ func (c *client) Query(q Query) (*Response, error) {
u := c.url
u.Path = "query"

jsonParameters, err := json.Marshal(q.Parameters)

if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", u.String(), nil)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "")
req.Header.Set("User-Agent", c.useragent)

if c.username != "" {
req.SetBasicAuth(c.username, c.password)
}

params := req.URL.Query()
params.Set("q", q.Command)
params.Set("db", q.Database)
params.Set("params", string(jsonParameters))

if q.Precision != "" {
params.Set("epoch", q.Precision)
}
Expand Down
41 changes: 41 additions & 0 deletions client/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
Expand Down

0 comments on commit 0a04499

Please sign in to comment.