Skip to content

Commit

Permalink
Merge pull request #176 from ClickHouse/feat/add-query-params-helper
Browse files Browse the repository at this point in the history
feat: add helper for query params
  • Loading branch information
ernado authored Aug 21, 2022
2 parents d259430 + f379769 commit 3a72bb3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
6 changes: 6 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func Conn(t testing.TB) *Client {
return ConnOpt(t, Options{})
}

func SkipNoFeature(t *testing.T, client *Client, feature proto.Feature) {
if !client.ServerInfo().Has(feature) {
t.Skipf("Skipping (feature %q not supported)", feature)
}
}

func TestDial(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
conn := Conn(t)
Expand Down
27 changes: 27 additions & 0 deletions query_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ch

import (
"fmt"
"sort"

"github.com/ClickHouse/ch-go/proto"
)

// Parameters is helper for building Query.Parameters.
//
// EXPERIMENTAL.
func Parameters(m map[string]any) []proto.Parameter {
var out []proto.Parameter
for k, v := range m {
out = append(out, proto.Parameter{
Key: k,
Value: fmt.Sprintf("'%v'", v),
})
}
// Sorting to make output deterministic.
sort.Slice(out, func(i, j int) bool {
return out[i].Key < out[j].Key
})

return out
}
20 changes: 8 additions & 12 deletions query_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ import (
)

func TestQueryParameters(t *testing.T) {
conn := ConnOpt(t, Options{QuotaKey: ""})
if !conn.ServerInfo().Has(proto.FeatureParameters) {
t.Skip("Skipping (not supported)")
}
conn := Conn(t)
SkipNoFeature(t, conn, proto.FeatureParameters)
ctx := context.Background()
data := new(proto.ColUInt8)
require.NoError(t, conn.Do(ctx, Query{
Body: "select {num:UInt8} v",
Parameters: []proto.Parameter{
{Key: "num", Value: `'1'`},
},
Result: proto.Results{
{Name: "v", Data: data},
},
Body: "select {num:UInt8} v, {str:String} s",
Parameters: Parameters(map[string]any{
"num": 100,
"str": "foo",
}),
Result: discardResult(),
}))
}

0 comments on commit 3a72bb3

Please sign in to comment.