Skip to content

Commit

Permalink
Merge pull request #16 from goccy/support-create-tmp-func
Browse files Browse the repository at this point in the history
Support create temp function
  • Loading branch information
goccy authored Aug 7, 2022
2 parents 6248dcd + 2280ca4 commit 499ec89
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 140 deletions.
75 changes: 54 additions & 21 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,46 +112,79 @@ func (s *ZetaSQLiteConn) CheckNamedValue(value *driver.NamedValue) error {
}

func (c *ZetaSQLiteConn) Prepare(query string) (driver.Stmt, error) {
ctx := context.Background()
conn := internal.NewConn(c.conn, c.tx)
out, err := c.analyzer.Analyze(context.Background(), conn, query)
it, err := c.analyzer.AnalyzeIterator(ctx, conn, query, nil)
if err != nil {
return nil, fmt.Errorf("failed to analyze query: %w", err)
return nil, err
}

var stmt driver.Stmt
for it.Next() {
out, err := it.Analyze(ctx)
if err != nil {
return nil, err
}
s, err := out.Prepare(ctx, conn)
if err != nil {
return nil, err
}
stmt = s
}
return out.Prepare(context.Background(), conn)
if err := it.Err(); err != nil {
return nil, err
}
return stmt, nil
}

func (c *ZetaSQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
conn := internal.NewConn(c.conn, c.tx)
out, err := c.analyzer.Analyze(ctx, conn, query)
if err != nil {
return nil, fmt.Errorf("failed to analyze query: %w", err)
}
newNamedValues, err := internal.EncodeNamedValues(args, out.Params())
it, err := c.analyzer.AnalyzeIterator(ctx, conn, query, args)
if err != nil {
return nil, err
}
newArgs := make([]interface{}, 0, len(args))
for _, newNamedValue := range newNamedValues {
newArgs = append(newArgs, newNamedValue)

var result driver.Result
for it.Next() {
out, err := it.Analyze(ctx)
if err != nil {
return nil, err
}
r, err := out.ExecContext(ctx, conn)
if err != nil {
return nil, err
}
result = r
}
if err := it.Err(); err != nil {
return nil, err
}
return out.ExecContext(ctx, conn, newArgs...)
return result, nil
}

func (c *ZetaSQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
conn := internal.NewConn(c.conn, c.tx)
out, err := c.analyzer.Analyze(ctx, conn, query)
if err != nil {
return nil, fmt.Errorf("failed to analyze query: %w", err)
}
newNamedValues, err := internal.EncodeNamedValues(args, out.Params())
it, err := c.analyzer.AnalyzeIterator(ctx, conn, query, args)
if err != nil {
return nil, err
}
newArgs := make([]interface{}, 0, len(args))
for _, newNamedValue := range newNamedValues {
newArgs = append(newArgs, newNamedValue)

var rows driver.Rows
for it.Next() {
out, err := it.Analyze(ctx)
if err != nil {
return nil, err
}
r, err := out.QueryContext(ctx, conn)
if err != nil {
return nil, err
}
rows = r
}
if err := it.Err(); err != nil {
return nil, err
}
return out.QueryContext(ctx, conn, newArgs...)
return rows, nil
}

func (c *ZetaSQLiteConn) Close() error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/goccy/go-zetasqlite
go 1.17

require (
github.com/goccy/go-zetasql v0.2.9
github.com/goccy/go-zetasql v0.3.0
github.com/mattn/go-sqlite3 v1.14.14
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WA
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-zetasql v0.2.9 h1:ztMBkv0WvyPQ7u6dY3gpiWJK354tQU9WnBNHmkjDSCA=
github.com/goccy/go-zetasql v0.2.9/go.mod h1:6W14CJVKh7crrSPyj6NPk4c49L2NWnxvyDLsRkOm4BI=
github.com/goccy/go-zetasql v0.3.0 h1:8hmfu5ALDnnkiUfeamfNoMo/C/h3iogXgDXSvQCvaO4=
github.com/goccy/go-zetasql v0.3.0/go.mod h1:6W14CJVKh7crrSPyj6NPk4c49L2NWnxvyDLsRkOm4BI=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
Expand Down
Loading

0 comments on commit 499ec89

Please sign in to comment.