Skip to content

Commit

Permalink
Merge pull request #10 from agoncear-mwb/main
Browse files Browse the repository at this point in the history
Add arguments compilation trough sqlbuilder
  • Loading branch information
auxten authored Jul 22, 2024
2 parents e7afac4 + e0b2d8d commit 82a5138
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
26 changes: 24 additions & 2 deletions chdb/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/chdb-io/chdb-go/chdb"
"github.com/chdb-io/chdb-go/chdbstable"
"github.com/huandu/go-sqlbuilder"
"github.com/parquet-go/parquet-go"

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

func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
func (c *conn) compileArguments(query string, args []driver.NamedValue) (string, error) {
var compiledQuery string
if len(args) > 0 {
compiledArgs := make([]interface{}, len(args))
for idx := range args {
compiledArgs[idx] = args[idx].Value
}
compiled, err := sqlbuilder.ClickHouse.Interpolate(query, compiledArgs)
if err != nil {
return "", err
}
compiledQuery = compiled
} else {
compiledQuery = query
}
return compiledQuery, nil
}

result, err := c.QueryFun(query, c.driverType.String(), c.udfPath)
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
compiledQuery, err := c.compileArguments(query, args)
if err != nil {
return nil, err
}
result, err := c.QueryFun(compiledQuery, c.driverType.String(), c.udfPath)
if err != nil {
return nil, err
}
Expand Down
38 changes: 38 additions & 0 deletions chdb/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,44 @@ func TestDb(t *testing.T) {
}
}

func TestDbWithCompiledArgs(t *testing.T) {
db, err := sql.Open("chdb", "")
if err != nil {
t.Errorf("open db fail, err:%s", err)
}
if db.Ping() != nil {
t.Errorf("ping db fail")
}
rows, err := db.Query(`SELECT ?, ?`, 1, "abc")
if err != nil {
t.Errorf("run Query fail, err:%s", err)
}
cols, err := rows.Columns()
if err != nil {
t.Errorf("get result columns fail, err: %s", err)
}
if len(cols) != 2 {
t.Errorf("select result columns length should be 2")
}
var (
bar int
foo string
)
defer rows.Close()
for rows.Next() {
err := rows.Scan(&bar, &foo)
if err != nil {
t.Errorf("scan fail, err: %s", err)
}
if bar != 1 {
t.Errorf("expected error")
}
if foo != "abc" {
t.Errorf("expected error")
}
}
}

func TestDbWithOpt(t *testing.T) {
for _, kv := range []struct {
opt string
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ require (
github.com/goccy/go-json v0.10.3 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/huandu/go-sqlbuilder v1.27.3 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/huandu/go-assert v1.1.6/go.mod h1:JuIfbmYG9ykwvuxoJ3V8TB5QP+3+ajIA54Y44TmkMxs=
github.com/huandu/go-sqlbuilder v1.27.3 h1:cNVF9vQP4i7rTk6XXJIEeMbGkZbxfjcITeJzobJK44k=
github.com/huandu/go-sqlbuilder v1.27.3/go.mod h1:mS0GAtrtW+XL6nM2/gXHRJax2RwSW1TraavWDFAc1JA=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
Expand Down

0 comments on commit 82a5138

Please sign in to comment.