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

Added sql test code and modified some code of sql.go #80

Merged
merged 6 commits into from
Feb 7, 2023
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
83 changes: 83 additions & 0 deletions builder/query/query_test.go
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) {
Copy link
Contributor Author

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.

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) {
Copy link
Contributor Author

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 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))

}
16 changes: 12 additions & 4 deletions builder/query/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand All @@ -25,8 +25,11 @@ func NewSQL() *SQL {
return s
}

func NewSQLParameter() *SQLParameter {
p := &SQLParameter{}
func NewSQLParameter(Type string, Value interface{}) SQLParameter {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}

Expand Down Expand Up @@ -60,6 +63,11 @@ func (s *SQL) SetParameters(parameters []SQLParameter) *SQL {
return s
}

func (s *SQL) SetContext(context map[string]interface{}) *SQL {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down
35 changes: 35 additions & 0 deletions examples/querySqlExample.go
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}