diff --git a/builder/query/query_test.go b/builder/query/query_test.go new file mode 100644 index 0000000..3dbdf4a --- /dev/null +++ b/builder/query/query_test.go @@ -0,0 +1,83 @@ +package query + +import ( + "encoding/json" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSelectSQL(t *testing.T) { + var param []SQLParameter + param = append(param, NewSQLParameter("VARCHAR", "Salo Toraut")) + param = append(param, NewSQLParameter("VARCHAR", "NB")) + param = append(param, NewSQLParameter("BOOLEAN", "false")) + param = append(param, NewSQLParameter("INTEGER", 31)) + param = append(param, NewSQLParameter("TIMESTAMP", "2016-06-27T00:00:11.080Z")) + query := NewSQL().SetQuery( + `SELECT * FROM "wikipedia" WHERE page=? AND flags=? AND isUnpatrolled=? AND delta=? AND __time=?`).SetParameters(param) + expected := `{ + "queryType": "sql", + "query": "SELECT * FROM \"wikipedia\" WHERE page=? AND flags=? AND isUnpatrolled=? AND delta=? AND __time=?", + "parameters": [ + { + "type": "VARCHAR", + "value": "Salo Toraut" + }, + { + "type": "VARCHAR", + "value": "NB" + }, + { + "type": "BOOLEAN", + "value": "false" + }, + { + "type": "INTEGER", + "value": 31 + }, + { + "type": "TIMESTAMP", + "value": "2016-06-27T00:00:11.080Z" + } + ] +}` + expressionJSON, err := json.Marshal(query) + assert.Nil(t, err) + assert.JSONEq(t, expected, string(expressionJSON)) +} + +func TestAllSetterSQL(t *testing.T) { + var param []SQLParameter + param = append(param, NewSQLParameter("VARCHAR", "Salo Toraut")) + context := make(map[string]interface{}) + context["sqlTimeZone"] = "America/Los_Angeles" + + query := NewSQL().SetQuery(`select * from "wikipedia" WHERE page=?`). + SetParameters(param). + SetHeader(true). + SetTypesHeader(true). + SetResultFormat("array"). + SetSQLTypesHeader(true). + SetContext(context) + expected := `{ + "queryType": "sql", + "context": { + "sqlTimeZone": "America/Los_Angeles" + }, + "query": "select * from \"wikipedia\" WHERE page=?", + "resultFormat": "array", + "header": true, + "typesHeader": true, + "sqlTypesHeader": true, + "parameters": [ + { + "type": "VARCHAR", + "value": "Salo Toraut" + } + ] +}` + expressionJSON, err := json.Marshal(query) + assert.Nil(t, err) + assert.JSONEq(t, expected, string(expressionJSON)) + +} diff --git a/builder/query/sql.go b/builder/query/sql.go index 03f3daf..442fc19 100644 --- a/builder/query/sql.go +++ b/builder/query/sql.go @@ -15,8 +15,8 @@ type SQL struct { } type SQLParameter struct { - Type string `json:"type,omitempty"` - Value string `json:"value,omitempty"` + Type string `json:"type,omitempty"` + Value interface{} `json:"value,omitempty"` } func NewSQL() *SQL { @@ -25,8 +25,11 @@ func NewSQL() *SQL { return s } -func NewSQLParameter() *SQLParameter { - p := &SQLParameter{} +func NewSQLParameter(Type string, Value interface{}) SQLParameter { + p := SQLParameter{ + Type: Type, + Value: Value, + } return p } @@ -60,6 +63,11 @@ func (s *SQL) SetParameters(parameters []SQLParameter) *SQL { return s } +func (s *SQL) SetContext(context map[string]interface{}) *SQL { + s.Base.SetContext(context) + return s +} + func (s *SQL) UnmarshalJSON(data []byte) error { var err error var tmp struct { diff --git a/examples/querySqlExample.go b/examples/querySqlExample.go new file mode 100644 index 0000000..7ef14ce --- /dev/null +++ b/examples/querySqlExample.go @@ -0,0 +1,35 @@ +//go:build mage +// +build mage + +package main + +import ( + "github.com/davecgh/go-spew/spew" + "github.com/grafadruid/go-druid" + "github.com/grafadruid/go-druid/builder/query" + "log" +) + +func main() { + d, err := druid.NewClient("http://localhost:8888") + if err != nil { + log.Fatal(err) + } + var results []map[string]interface{} + var param []query.SQLParameter + param = append(param, query.NewSQLParameter("VARCHAR", "Salo Toraut")) + param = append(param, query.NewSQLParameter("VARCHAR", "NB")) + param = append(param, query.NewSQLParameter("VARCHAR", "false")) // BOOLEAN type fails the convent in api. + param = append(param, query.NewSQLParameter("INTEGER", 31)) + context := make(map[string]interface{}) + context["sqlTimeZone"] = "America/Los_Angeles" + query := query.NewSQL().SetQuery( + `SELECT * FROM "wikipedia" WHERE page=? AND flags=? AND isUnpatrolled=? AND delta=?`). + SetParameters(param). + SetContext(context) + _, err = d.Query().Execute(query, &results) + if err != nil { + log.Fatal(err) + } + spew.Dump(results) +}