-
Notifications
You must be signed in to change notification settings - Fork 33
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
Added sql test code and modified some code of sql.go #80
Changes from all commits
1d806e2
f704b1f
8fdc6d0
92cb740
c495af2
6b78070
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a comparison test when all methods are changed to true |
||
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)) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were cases where the type of value was INTEGER or FLOAT, so I changed it to an interface. |
||
} | ||
|
||
func NewSQL() *SQL { | ||
|
@@ -25,8 +25,11 @@ func NewSQL() *SQL { | |
return s | ||
} | ||
|
||
func NewSQLParameter() *SQLParameter { | ||
p := &SQLParameter{} | ||
func NewSQLParameter(Type string, Value interface{}) SQLParameter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified to allow use of SQLParameter's constructor. |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the SetContext method of the SQL struct because it did not exist. |
||
s.Base.SetContext(context) | ||
return s | ||
} | ||
|
||
func (s *SQL) UnmarshalJSON(data []byte) error { | ||
var err error | ||
var tmp struct { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example of using SQL query |
||
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a comparison test of query builder and actual output.