Skip to content

Commit 82a5138

Browse files
authored
Merge pull request #10 from agoncear-mwb/main
Add arguments compilation trough sqlbuilder
2 parents e7afac4 + e0b2d8d commit 82a5138

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

chdb/driver/driver.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/chdb-io/chdb-go/chdb"
1313
"github.com/chdb-io/chdb-go/chdbstable"
14+
"github.com/huandu/go-sqlbuilder"
1415
"github.com/parquet-go/parquet-go"
1516

1617
"github.com/apache/arrow/go/v15/arrow/ipc"
@@ -196,9 +197,30 @@ func (c *conn) Query(query string, values []driver.Value) (driver.Rows, error) {
196197
return c.QueryContext(context.Background(), query, namedValues)
197198
}
198199

199-
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
200+
func (c *conn) compileArguments(query string, args []driver.NamedValue) (string, error) {
201+
var compiledQuery string
202+
if len(args) > 0 {
203+
compiledArgs := make([]interface{}, len(args))
204+
for idx := range args {
205+
compiledArgs[idx] = args[idx].Value
206+
}
207+
compiled, err := sqlbuilder.ClickHouse.Interpolate(query, compiledArgs)
208+
if err != nil {
209+
return "", err
210+
}
211+
compiledQuery = compiled
212+
} else {
213+
compiledQuery = query
214+
}
215+
return compiledQuery, nil
216+
}
200217

201-
result, err := c.QueryFun(query, c.driverType.String(), c.udfPath)
218+
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
219+
compiledQuery, err := c.compileArguments(query, args)
220+
if err != nil {
221+
return nil, err
222+
}
223+
result, err := c.QueryFun(compiledQuery, c.driverType.String(), c.udfPath)
202224
if err != nil {
203225
return nil, err
204226
}

chdb/driver/driver_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ func TestDb(t *testing.T) {
4747
}
4848
}
4949

50+
func TestDbWithCompiledArgs(t *testing.T) {
51+
db, err := sql.Open("chdb", "")
52+
if err != nil {
53+
t.Errorf("open db fail, err:%s", err)
54+
}
55+
if db.Ping() != nil {
56+
t.Errorf("ping db fail")
57+
}
58+
rows, err := db.Query(`SELECT ?, ?`, 1, "abc")
59+
if err != nil {
60+
t.Errorf("run Query fail, err:%s", err)
61+
}
62+
cols, err := rows.Columns()
63+
if err != nil {
64+
t.Errorf("get result columns fail, err: %s", err)
65+
}
66+
if len(cols) != 2 {
67+
t.Errorf("select result columns length should be 2")
68+
}
69+
var (
70+
bar int
71+
foo string
72+
)
73+
defer rows.Close()
74+
for rows.Next() {
75+
err := rows.Scan(&bar, &foo)
76+
if err != nil {
77+
t.Errorf("scan fail, err: %s", err)
78+
}
79+
if bar != 1 {
80+
t.Errorf("expected error")
81+
}
82+
if foo != "abc" {
83+
t.Errorf("expected error")
84+
}
85+
}
86+
}
87+
5088
func TestDbWithOpt(t *testing.T) {
5189
for _, kv := range []struct {
5290
opt string

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ require (
1313
github.com/goccy/go-json v0.10.3 // indirect
1414
github.com/google/flatbuffers v24.3.25+incompatible // indirect
1515
github.com/google/uuid v1.6.0 // indirect
16+
github.com/huandu/go-sqlbuilder v1.27.3 // indirect
17+
github.com/huandu/xstrings v1.4.0 // indirect
1618
github.com/klauspost/compress v1.17.9 // indirect
1719
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
1820
github.com/mattn/go-colorable v0.1.13 // indirect

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1414
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1515
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
1616
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
17+
github.com/huandu/go-assert v1.1.6/go.mod h1:JuIfbmYG9ykwvuxoJ3V8TB5QP+3+ajIA54Y44TmkMxs=
18+
github.com/huandu/go-sqlbuilder v1.27.3 h1:cNVF9vQP4i7rTk6XXJIEeMbGkZbxfjcITeJzobJK44k=
19+
github.com/huandu/go-sqlbuilder v1.27.3/go.mod h1:mS0GAtrtW+XL6nM2/gXHRJax2RwSW1TraavWDFAc1JA=
20+
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
21+
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
1722
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
1823
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
1924
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=

0 commit comments

Comments
 (0)