aboutsummaryrefslogtreecommitdiff
path: root/sqlite3.go
diff options
context:
space:
mode:
authorYasuhiro Matsumoto <mattn.jp@gmail.com>2016-11-04 14:24:22 +0900
committerYasuhiro Matsumoto <mattn.jp@gmail.com>2016-11-04 14:24:22 +0900
commitc95a77965c55783416165ca638dec12c0f0a9fdb (patch)
tree69e42bf5616a95814d99e764056c8898e4892f46 /sqlite3.go
parentimplement go18 Pinger (diff)
downloadgolite-c95a77965c55783416165ca638dec12c0f0a9fdb.tar.gz
golite-c95a77965c55783416165ca638dec12c0f0a9fdb.tar.xz
context features
Diffstat (limited to 'sqlite3.go')
-rw-r--r--sqlite3.go74
1 files changed, 63 insertions, 11 deletions
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)