Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bound parameters map to Query object #7688

Merged
merged 2 commits into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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