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

feat: add helper for query params #176

Merged
merged 3 commits into from
Aug 21, 2022
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
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(),
}))
}