From c95a77965c55783416165ca638dec12c0f0a9fdb Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 4 Nov 2016 14:24:22 +0900 Subject: context features --- sqlite3_go18.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sqlite3_go18.go (limited to 'sqlite3_go18.go') diff --git a/sqlite3_go18.go b/sqlite3_go18.go new file mode 100644 index 0000000..1ee4c6a --- /dev/null +++ b/sqlite3_go18.go @@ -0,0 +1,32 @@ +package sqlite3 + +import ( + "database/sql/driver" + "errors" + + "golang.org/x/net/context" +) + +// Ping implement Pinger. +func (c *SQLiteConn) Ping(ctx context.Context) error { + if c.db == nil { + return errors.New("Connection was closed") + } + return nil +} + +func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { + list := make([]namedValue, len(args)) + for i, nv := range args { + list[i] = namedValue(nv) + } + return c.query(ctx, query, list) +} + +func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { + list := make([]namedValue, len(args)) + for i, nv := range args { + list[i] = namedValue(nv) + } + return s.query(ctx, list) +} -- cgit v1.2.3 From b23526fb3ce3365cb83fcb1f46ac278d70d8f934 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 4 Nov 2016 15:00:29 +0900 Subject: support named params --- sqlite3.go | 48 ++++++++++++++++++++---------------------------- sqlite3_go18.go | 16 ++++++++++++++++ sqlite3_test.go | 6 +++--- 3 files changed, 39 insertions(+), 31 deletions(-) (limited to 'sqlite3_go18.go') diff --git a/sqlite3.go b/sqlite3.go index 5087fad..3b80eeb 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -172,8 +172,6 @@ type SQLiteTx struct { type SQLiteStmt struct { c *SQLiteConn s *C.sqlite3_stmt - nv int - nn []string t string closed bool cls bool @@ -420,6 +418,7 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err } func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) { + start := 0 for { s, err := c.Prepare(query) if err != nil { @@ -431,12 +430,16 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) if len(args) < na { return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args)) } + for i := 0; i < na; i++ { + args[i].Ordinal -= start + } res, err = s.(*SQLiteStmt).exec(ctx, args[:na]) if err != nil && err != driver.ErrSkip { s.Close() return nil, err } args = args[na:] + start += na } tail := s.(*SQLiteStmt).t s.Close() @@ -466,6 +469,7 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro } func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) { + start := 0 for { s, err := c.Prepare(query) if err != nil { @@ -476,12 +480,16 @@ func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) if len(args) < na { return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args)) } + for i := 0; i < na; i++ { + args[i].Ordinal -= start + } rows, err := s.(*SQLiteStmt).query(ctx, args[:na]) if err != nil && err != driver.ErrSkip { s.Close() return nil, err } args = args[na:] + start += na tail := s.(*SQLiteStmt).t if tail == "" { return rows, nil @@ -648,15 +656,7 @@ func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) { if tail != nil && *tail != '\000' { t = strings.TrimSpace(C.GoString(tail)) } - nv := int(C.sqlite3_bind_parameter_count(s)) - var nn []string - for i := 0; i < nv; i++ { - pn := C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1))) - if len(pn) > 1 && pn[0] == '$' && 48 <= pn[1] && pn[1] <= 57 { - nn = append(nn, C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1)))) - } - } - ss := &SQLiteStmt{c: c, s: s, nv: nv, nn: nn, t: t} + ss := &SQLiteStmt{c: c, s: s, t: t} runtime.SetFinalizer(ss, (*SQLiteStmt).Close) return ss, nil } @@ -680,7 +680,7 @@ func (s *SQLiteStmt) Close() error { // Return a number of parameters. func (s *SQLiteStmt) NumInput() int { - return s.nv + return int(C.sqlite3_bind_parameter_count(s.s)) } type bindArg struct { @@ -694,25 +694,17 @@ func (s *SQLiteStmt) bind(args []namedValue) error { return s.c.lastError() } - var vargs []bindArg - narg := len(args) - vargs = make([]bindArg, narg) - if len(s.nn) > 0 { - for i, v := range s.nn { - if pi, err := strconv.Atoi(v[1:]); err == nil { - vargs[i] = bindArg{pi, args[i].Value} - } - } - } else { - for i, v := range args { - vargs[i] = bindArg{i + 1, v.Value} + for i, v := range args { + if v.Name != "" { + cname := C.CString(v.Name) + args[i].Ordinal = int(C.sqlite3_bind_parameter_index(s.s, cname)) + C.free(unsafe.Pointer(cname)) } } - for _, varg := range vargs { - n := C.int(varg.n) - v := varg.v - switch v := v.(type) { + for _, arg := range args { + n := C.int(arg.Ordinal) + switch v := arg.Value.(type) { case nil: rv = C.sqlite3_bind_null(s.s, n) case string: diff --git a/sqlite3_go18.go b/sqlite3_go18.go index 1ee4c6a..3993490 100644 --- a/sqlite3_go18.go +++ b/sqlite3_go18.go @@ -23,6 +23,14 @@ func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driv return c.query(ctx, query, list) } +func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { + list := make([]namedValue, len(args)) + for i, nv := range args { + list[i] = namedValue(nv) + } + return c.exec(ctx, query, list) +} + func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { list := make([]namedValue, len(args)) for i, nv := range args { @@ -30,3 +38,11 @@ func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) } return s.query(ctx, list) } + +func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { + list := make([]namedValue, len(args)) + for i, nv := range args { + list[i] = namedValue(nv) + } + return s.exec(ctx, list) +} diff --git a/sqlite3_test.go b/sqlite3_test.go index fc5c99c..b126bbd 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -993,7 +993,7 @@ func TestVersion(t *testing.T) { } } -func TestNumberNamedParams(t *testing.T) { +func TestNamedParams(t *testing.T) { tempFilename := TempFilename(t) defer os.Remove(tempFilename) db, err := sql.Open("sqlite3", tempFilename) @@ -1009,12 +1009,12 @@ func TestNumberNamedParams(t *testing.T) { t.Error("Failed to call db.Query:", err) } - _, err = db.Exec(`insert into foo(id, name, extra) values($1, $2, $2)`, 1, "foo") + _, err = db.Exec(`insert into foo(id, name, extra) values(:id, :name, :name)`, sql.Param(":name", "foo"), sql.Param(":id", 1)) if err != nil { t.Error("Failed to call db.Exec:", err) } - row := db.QueryRow(`select id, extra from foo where id = $1 and extra = $2`, 1, "foo") + row := db.QueryRow(`select id, extra from foo where id = :id and extra = :extra`, sql.Param(":id", 1), sql.Param(":extra", "foo")) if row == nil { t.Error("Failed to call db.QueryRow") } -- cgit v1.2.3 From 29b191f2068cf75cdb2f9e75274de95a3106b9a2 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 4 Nov 2016 15:07:51 +0900 Subject: build tag --- sqlite3_go18.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'sqlite3_go18.go') diff --git a/sqlite3_go18.go b/sqlite3_go18.go index 3993490..28a1bbf 100644 --- a/sqlite3_go18.go +++ b/sqlite3_go18.go @@ -1,3 +1,5 @@ +// +build go1.8 + package sqlite3 import ( -- cgit v1.2.3 From 025b917610cf628e9be1dcf9386525d0dbc22659 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 4 Nov 2016 15:11:24 +0900 Subject: add PrepareContext --- sqlite3.go | 4 ++++ sqlite3_go18.go | 4 ++++ 2 files changed, 8 insertions(+) (limited to 'sqlite3_go18.go') diff --git a/sqlite3.go b/sqlite3.go index 3b80eeb..bef5627 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -644,6 +644,10 @@ func (c *SQLiteConn) Close() error { // Prepare the query string. Return a new statement. func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) { + return c.prepare(context.Background(), query) +} + +func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) { pquery := C.CString(query) defer C.free(unsafe.Pointer(pquery)) var s *C.sqlite3_stmt diff --git a/sqlite3_go18.go b/sqlite3_go18.go index 28a1bbf..5832a92 100644 --- a/sqlite3_go18.go +++ b/sqlite3_go18.go @@ -33,6 +33,10 @@ func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []drive return c.exec(ctx, query, list) } +func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { + return c.prepare(ctx, query) +} + func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { list := make([]namedValue, len(args)) for i, nv := range args { -- cgit v1.2.3 From 755d5be32c971580a8ae5ed32a6bd5ff774d722d Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 4 Nov 2016 15:15:16 +0900 Subject: add BeginContext --- sqlite3.go | 4 ++++ sqlite3_go18.go | 4 ++++ 2 files changed, 8 insertions(+) (limited to 'sqlite3_go18.go') diff --git a/sqlite3.go b/sqlite3.go index bef5627..cf5e99a 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -514,6 +514,10 @@ func (c *SQLiteConn) execQuery(cmd string) (driver.Result, error) { // Begin transaction. func (c *SQLiteConn) Begin() (driver.Tx, error) { + return c.begin(context.Background()) +} + +func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) { if _, err := c.execQuery(c.txlock); err != nil { return nil, err } diff --git a/sqlite3_go18.go b/sqlite3_go18.go index 5832a92..7109d58 100644 --- a/sqlite3_go18.go +++ b/sqlite3_go18.go @@ -37,6 +37,10 @@ func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.S return c.prepare(ctx, query) } +func (c *SQLiteConn) BeginContext(ctx context.Context) (driver.Tx, error) { + return c.begin(ctx) +} + func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { list := make([]namedValue, len(args)) for i, nv := range args { -- cgit v1.2.3