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.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 11 deletions(-) (limited to 'sqlite3.go') diff --git a/sqlite3.go b/sqlite3.go index de9d7d2..5087fad 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -114,6 +114,8 @@ import ( "strings" "time" "unsafe" + + "golang.org/x/net/context" ) // Timestamp formats understood by both this module and SQLite. @@ -295,19 +297,19 @@ func (ai *aggInfo) Done(ctx *C.sqlite3_context) { // Commit transaction. func (tx *SQLiteTx) Commit() error { - _, err := tx.c.exec("COMMIT") + _, err := tx.c.execQuery("COMMIT") if err != nil && err.(Error).Code == C.SQLITE_BUSY { // sqlite3 will leave the transaction open in this scenario. // However, database/sql considers the transaction complete once we // return from Commit() - we must clean up to honour its semantics. - tx.c.exec("ROLLBACK") + tx.c.execQuery("ROLLBACK") } return err } // Rollback transaction. func (tx *SQLiteTx) Rollback() error { - _, err := tx.c.exec("ROLLBACK") + _, err := tx.c.execQuery("ROLLBACK") return err } @@ -404,9 +406,20 @@ func (c *SQLiteConn) lastError() Error { // Implements Execer func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) { if len(args) == 0 { - return c.exec(query) + return c.execQuery(query) } + list := make([]namedValue, len(args)) + for i, v := range args { + list[i] = namedValue{ + Ordinal: i + 1, + Value: v, + } + } + return c.exec(context.Background(), query, list) +} + +func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) { for { s, err := c.Prepare(query) if err != nil { @@ -418,7 +431,7 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err if len(args) < na { return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args)) } - res, err = s.Exec(args[:na]) + res, err = s.(*SQLiteStmt).exec(ctx, args[:na]) if err != nil && err != driver.ErrSkip { s.Close() return nil, err @@ -434,8 +447,25 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err } } +type namedValue struct { + Name string + Ordinal int + Value driver.Value +} + // Implements Queryer func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) { + list := make([]namedValue, len(args)) + for i, v := range args { + list[i] = namedValue{ + Ordinal: i + 1, + Value: v, + } + } + return c.query(context.Background(), query, list) +} + +func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) { for { s, err := c.Prepare(query) if err != nil { @@ -446,7 +476,7 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro if len(args) < na { return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args)) } - rows, err := s.Query(args[:na]) + rows, err := s.(*SQLiteStmt).query(ctx, args[:na]) if err != nil && err != driver.ErrSkip { s.Close() return nil, err @@ -462,7 +492,7 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro } } -func (c *SQLiteConn) exec(cmd string) (driver.Result, error) { +func (c *SQLiteConn) execQuery(cmd string) (driver.Result, error) { pcmd := C.CString(cmd) defer C.free(unsafe.Pointer(pcmd)) @@ -476,7 +506,7 @@ func (c *SQLiteConn) exec(cmd string) (driver.Result, error) { // Begin transaction. func (c *SQLiteConn) Begin() (driver.Tx, error) { - if _, err := c.exec(c.txlock); err != nil { + if _, err := c.execQuery(c.txlock); err != nil { return nil, err } return &SQLiteTx{c}, nil @@ -658,7 +688,7 @@ type bindArg struct { v driver.Value } -func (s *SQLiteStmt) bind(args []driver.Value) error { +func (s *SQLiteStmt) bind(args []namedValue) error { rv := C.sqlite3_reset(s.s) if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { return s.c.lastError() @@ -670,12 +700,12 @@ func (s *SQLiteStmt) bind(args []driver.Value) error { 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]} + vargs[i] = bindArg{pi, args[i].Value} } } } else { for i, v := range args { - vargs[i] = bindArg{i + 1, v} + vargs[i] = bindArg{i + 1, v.Value} } } @@ -722,6 +752,17 @@ func (s *SQLiteStmt) bind(args []driver.Value) error { // Query the statement with arguments. Return records. func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) { + list := make([]namedValue, len(args)) + for i, v := range args { + list[i] = namedValue{ + Ordinal: i + 1, + Value: v, + } + } + return s.query(context.Background(), list) +} + +func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) { if err := s.bind(args); err != nil { return nil, err } @@ -740,6 +781,17 @@ func (r *SQLiteResult) RowsAffected() (int64, error) { // Execute the statement with arguments. Return result object. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) { + list := make([]namedValue, len(args)) + for i, v := range args { + list[i] = namedValue{ + Ordinal: i + 1, + Value: v, + } + } + return s.exec(context.Background(), list) +} + +func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) { if err := s.bind(args); err != nil { C.sqlite3_reset(s.s) C.sqlite3_clear_bindings(s.s) -- 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.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 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.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.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 From 0d1d1a644e704714e7ccf5e28dc53601c4e9cf37 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 5 Nov 2016 00:40:06 +0900 Subject: go vet && golint --- backup.go | 14 +++++++++---- error.go | 1 + sqlite3.go | 50 +++++++++++++++++++++++------------------------ sqlite3_go18.go | 6 ++++++ sqlite3_load_extension.go | 1 + tracecallback_noimpl.go | 1 + 6 files changed, 44 insertions(+), 29 deletions(-) (limited to 'sqlite3.go') diff --git a/backup.go b/backup.go index 05f8038..5ab3a54 100644 --- a/backup.go +++ b/backup.go @@ -19,10 +19,12 @@ import ( "unsafe" ) +// SQLiteBackup implement interface of Backup. type SQLiteBackup struct { b *C.sqlite3_backup } +// Backup make backup from src to dest. func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteBackup, error) { destptr := C.CString(dest) defer C.free(unsafe.Pointer(destptr)) @@ -37,10 +39,10 @@ func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteB return nil, c.lastError() } -// Backs up for one step. Calls the underlying `sqlite3_backup_step` function. -// This function returns a boolean indicating if the backup is done and -// an error signalling any other error. Done is returned if the underlying C -// function returns SQLITE_DONE (Code 101) +// Step to backs up for one step. Calls the underlying `sqlite3_backup_step` +// function. This function returns a boolean indicating if the backup is done +// and an error signalling any other error. Done is returned if the underlying +// C function returns SQLITE_DONE (Code 101) func (b *SQLiteBackup) Step(p int) (bool, error) { ret := C.sqlite3_backup_step(b.b, C.int(p)) if ret == C.SQLITE_DONE { @@ -51,18 +53,22 @@ func (b *SQLiteBackup) Step(p int) (bool, error) { return false, nil } +// Remaining return whether have the rest for backup. func (b *SQLiteBackup) Remaining() int { return int(C.sqlite3_backup_remaining(b.b)) } +// PageCount return count of pages. func (b *SQLiteBackup) PageCount() int { return int(C.sqlite3_backup_pagecount(b.b)) } +// Finish close backup. func (b *SQLiteBackup) Finish() error { return b.Close() } +// Close close backup. func (b *SQLiteBackup) Close() error { ret := C.sqlite3_backup_finish(b.b) diff --git a/error.go b/error.go index b910108..474f8e6 100644 --- a/error.go +++ b/error.go @@ -7,6 +7,7 @@ package sqlite3 import "C" +// ErrNo inherit errno. type ErrNo int const ErrNoMask C.int = 0xff diff --git a/sqlite3.go b/sqlite3.go index cf5e99a..b018918 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -118,10 +118,10 @@ import ( "golang.org/x/net/context" ) -// Timestamp formats understood by both this module and SQLite. -// The first format in the slice will be used when saving time values -// into the database. When parsing a string from a timestamp or -// datetime column, the formats are tried in order. +// SQLiteTimestampFormats is timestamp formats understood by both this module +// and SQLite. The first format in the slice will be used when saving time +// values into the database. When parsing a string from a timestamp or datetime +// column, the formats are tried in order. var SQLiteTimestampFormats = []string{ // By default, store timestamps with whatever timezone they come with. // When parsed, they will be returned with the same timezone. @@ -141,20 +141,20 @@ func init() { } // Version returns SQLite library version information. -func Version() (libVersion string, libVersionNumber int, sourceId string) { +func Version() (libVersion string, libVersionNumber int, sourceID string) { libVersion = C.GoString(C.sqlite3_libversion()) libVersionNumber = int(C.sqlite3_libversion_number()) - sourceId = C.GoString(C.sqlite3_sourceid()) - return libVersion, libVersionNumber, sourceId + sourceID = C.GoString(C.sqlite3_sourceid()) + return libVersion, libVersionNumber, sourceID } -// Driver struct. +// SQLiteDriver implement sql.Driver. type SQLiteDriver struct { Extensions []string ConnectHook func(*SQLiteConn) error } -// Conn struct. +// SQLiteConn implement sql.Conn. type SQLiteConn struct { db *C.sqlite3 loc *time.Location @@ -163,12 +163,12 @@ type SQLiteConn struct { aggregators []*aggInfo } -// Tx struct. +// SQLiteTx implemen sql.Tx. type SQLiteTx struct { c *SQLiteConn } -// Stmt struct. +// SQLiteStmt implement sql.Stmt. type SQLiteStmt struct { c *SQLiteConn s *C.sqlite3_stmt @@ -177,13 +177,13 @@ type SQLiteStmt struct { cls bool } -// Result struct. +// SQLiteResult implement sql.Result. type SQLiteResult struct { id int64 changes int64 } -// Rows struct. +// SQLiteRows implement sql.Rows. type SQLiteRows struct { s *SQLiteStmt nc int @@ -401,7 +401,7 @@ func (c *SQLiteConn) lastError() Error { } } -// Implements Execer +// Exec implements Execer. func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) { if len(args) == 0 { return c.execQuery(query) @@ -456,7 +456,7 @@ type namedValue struct { Value driver.Value } -// Implements Queryer +// Query implements Queryer. func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) { list := make([]namedValue, len(args)) for i, v := range args { @@ -549,7 +549,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { var loc *time.Location txlock := "BEGIN" - busy_timeout := 5000 + busyTimeout := 5000 pos := strings.IndexRune(dsn, '?') if pos >= 1 { params, err := url.ParseQuery(dsn[pos+1:]) @@ -575,7 +575,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { if err != nil { return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err) } - busy_timeout = int(iv) + busyTimeout = int(iv) } // _txlock @@ -612,7 +612,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { return nil, errors.New("sqlite succeeded without returning a database") } - rv = C.sqlite3_busy_timeout(db, C.int(busy_timeout)) + rv = C.sqlite3_busy_timeout(db, C.int(busyTimeout)) if rv != C.SQLITE_OK { return nil, Error{Code: ErrNo(rv)} } @@ -686,7 +686,7 @@ func (s *SQLiteStmt) Close() error { return nil } -// Return a number of parameters. +// NumInput return a number of parameters. func (s *SQLiteStmt) NumInput() int { return int(C.sqlite3_bind_parameter_count(s.s)) } @@ -769,17 +769,17 @@ func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil } -// Return last inserted ID. +// LastInsertId teturn last inserted ID. func (r *SQLiteResult) LastInsertId() (int64, error) { return r.id, nil } -// Return how many rows affected. +// RowsAffected return how many rows affected. func (r *SQLiteResult) RowsAffected() (int64, error) { return r.changes, nil } -// Execute the statement with arguments. Return result object. +// Exec execute the statement with arguments. Return result object. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) { list := make([]namedValue, len(args)) for i, v := range args { @@ -823,7 +823,7 @@ func (rc *SQLiteRows) Close() error { return nil } -// Return column names. +// Columns return column names. func (rc *SQLiteRows) Columns() []string { if rc.nc != len(rc.cols) { rc.cols = make([]string, rc.nc) @@ -834,7 +834,7 @@ func (rc *SQLiteRows) Columns() []string { return rc.cols } -// Return column types. +// DeclTypes return column types. func (rc *SQLiteRows) DeclTypes() []string { if rc.decltype == nil { rc.decltype = make([]string, rc.nc) @@ -845,7 +845,7 @@ func (rc *SQLiteRows) DeclTypes() []string { return rc.decltype } -// Move cursor to next. +// Next move cursor to next. func (rc *SQLiteRows) Next(dest []driver.Value) error { rv := C.sqlite3_step(rc.s.s) if rv == C.SQLITE_DONE { diff --git a/sqlite3_go18.go b/sqlite3_go18.go index 7109d58..9ad7baf 100644 --- a/sqlite3_go18.go +++ b/sqlite3_go18.go @@ -17,6 +17,7 @@ func (c *SQLiteConn) Ping(ctx context.Context) error { return nil } +// QueryContext implement QueryerContext. 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 { @@ -25,6 +26,7 @@ func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driv return c.query(ctx, query, list) } +// ExecContext implement ExecerContext. 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 { @@ -33,14 +35,17 @@ func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []drive return c.exec(ctx, query, list) } +// PrepareContext implement ConnPrepareContext. func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { return c.prepare(ctx, query) } +// BeginContext implement ConnBeginContext. func (c *SQLiteConn) BeginContext(ctx context.Context) (driver.Tx, error) { return c.begin(ctx) } +// QueryContext implement QueryerContext. func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { list := make([]namedValue, len(args)) for i, nv := range args { @@ -49,6 +54,7 @@ func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) return s.query(ctx, list) } +// ExecContext implement ExecerContext. func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { list := make([]namedValue, len(args)) for i, nv := range args { diff --git a/sqlite3_load_extension.go b/sqlite3_load_extension.go index e6e0801..b5bccb1 100644 --- a/sqlite3_load_extension.go +++ b/sqlite3_load_extension.go @@ -42,6 +42,7 @@ func (c *SQLiteConn) loadExtensions(extensions []string) error { return nil } +// LoadExtension load the sqlite3 extension. func (c *SQLiteConn) LoadExtension(lib string, entry string) error { rv := C.sqlite3_enable_load_extension(c.db, 1) if rv != C.SQLITE_OK { diff --git a/tracecallback_noimpl.go b/tracecallback_noimpl.go index c0a49e9..f270415 100644 --- a/tracecallback_noimpl.go +++ b/tracecallback_noimpl.go @@ -4,6 +4,7 @@ package sqlite3 import "errors" +// RegisterAggregator register the aggregator. func (c *SQLiteConn) RegisterAggregator(name string, impl interface{}, pure bool) error { return errors.New("This feature is not implemented") } -- cgit v1.2.3 From 605d9d08513fd3936c3d4a8631abd1db14136074 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sun, 6 Nov 2016 20:43:53 +0900 Subject: cancel --- sqlite3.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'sqlite3.go') diff --git a/sqlite3.go b/sqlite3.go index b018918..c1c42f0 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -190,6 +190,7 @@ type SQLiteRows struct { cols []string decltype []string cls bool + done chan struct{} } type functionInfo struct { @@ -766,7 +767,26 @@ func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, if err := s.bind(args); err != nil { return nil, err } - return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil + + rows := &SQLiteRows{ + s: s, + nc: int(C.sqlite3_column_count(s.s)), + cols: nil, + decltype: nil, + cls: s.cls, + done: make(chan struct{}), + } + + go func() { + select { + case <-ctx.Done(): + C.sqlite3_interrupt(s.c.db) + rows.Close() + case <-rows.done: + } + }() + + return rows, nil } // LastInsertId teturn last inserted ID. @@ -813,6 +833,10 @@ func (rc *SQLiteRows) Close() error { if rc.s.closed { return nil } + if rc.done != nil { + close(rc.done) + rc.done = nil + } if rc.cls { return rc.s.Close() } -- cgit v1.2.3 From deed33aec73ebdd869ff5fc6dcb9a3bd5a039a7f Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sun, 6 Nov 2016 20:46:27 +0900 Subject: cancel --- sqlite3.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'sqlite3.go') diff --git a/sqlite3.go b/sqlite3.go index c1c42f0..ac069e3 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -817,6 +817,17 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result C.sqlite3_clear_bindings(s.s) return nil, err } + + done := make(chan struct{}) + defer close(done) + go func() { + select { + case <-ctx.Done(): + C.sqlite3_interrupt(s.c.db) + case <-done: + } + }() + var rowid, changes C.longlong rv := C._sqlite3_step(s.s, &rowid, &changes) if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { @@ -825,6 +836,7 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result C.sqlite3_clear_bindings(s.s) return nil, err } + return &SQLiteResult{int64(rowid), int64(changes)}, nil } -- cgit v1.2.3