Fix error on Execer. Close #124

This commit is contained in:
mattn 2014-06-26 03:54:09 +09:00
parent d56eb93ecb
commit d9e7576acf
2 changed files with 66 additions and 65 deletions

View File

@ -144,52 +144,55 @@ func (c *SQLiteConn) lastError() Error {
// Implements Execer // Implements Execer
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) { func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
if len(args) == 0 { if len(args) == 0 {
return c.exec(query) return c.exec(query)
} }
for { for {
s, err := c.Prepare(query) s, err := c.Prepare(query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
na := s.NumInput() var res driver.Result
res, err := s.Exec(args[:na]) if s.(*SQLiteStmt).s != nil {
if err != nil && err != driver.ErrSkip { na := s.NumInput()
s.Close() res, err = s.Exec(args[:na])
return nil, err if err != nil && err != driver.ErrSkip {
} s.Close()
args = args[na:] return nil, err
tail := s.(*SQLiteStmt).t }
if tail == "" { args = args[na:]
return res, nil }
} tail := s.(*SQLiteStmt).t
s.Close() if tail == "" {
query = tail return res, nil
} }
s.Close()
query = tail
}
} }
// Implements Queryer // Implements Queryer
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) { func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
for { for {
s, err := c.Prepare(query) s, err := c.Prepare(query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
na := s.NumInput() na := s.NumInput()
rows, err := s.Query(args[:na]) rows, err := s.Query(args[:na])
if err != nil && err != driver.ErrSkip { if err != nil && err != driver.ErrSkip {
s.Close() s.Close()
return nil, err return nil, err
} }
args = args[na:] args = args[na:]
tail := s.(*SQLiteStmt).t tail := s.(*SQLiteStmt).t
if tail == "" { if tail == "" {
return rows, nil return rows, nil
} }
s.Close() s.Close()
query = tail query = tail
} }
} }
func (c *SQLiteConn) exec(cmd string) (driver.Result, error) { func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {

View File

@ -1,7 +1,6 @@
package sqlite3 package sqlite3
import ( import (
"./sqltest"
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"encoding/hex" "encoding/hex"
@ -9,6 +8,8 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
"./sqltest"
) )
func TempFilename() string { func TempFilename() string {
@ -639,29 +640,26 @@ func TestSuite(t *testing.T) {
// TODO: Execer & Queryer currently disabled // TODO: Execer & Queryer currently disabled
// https://github.com/mattn/go-sqlite3/issues/82 // https://github.com/mattn/go-sqlite3/issues/82
//func TestExecer(t *testing.T) { func TestExecer(t *testing.T) {
// tempFilename := TempFilename() tempFilename := TempFilename()
// db, err := sql.Open("sqlite3", tempFilename) db, err := sql.Open("sqlite3", tempFilename)
// if err != nil { if err != nil {
// t.Fatal("Failed to open database:", err) t.Fatal("Failed to open database:", err)
// } }
// defer os.Remove(tempFilename) defer os.Remove(tempFilename)
// defer db.Close() defer db.Close()
//
// _, err = db.Exec(` _, err = db.Exec(`
// create table foo (id integer); create table foo (id integer); -- one comment
// insert into foo(id) values(?); insert into foo(id) values(?);
// insert into foo(id) values(?); insert into foo(id) values(?);
// insert into foo(id) values(?); insert into foo(id) values(?); -- another comment
// `, 1, 2, 3) `, 1, 2, 3)
// if err != nil { if err != nil {
// t.Error("Failed to call db.Exec:", err) t.Error("Failed to call db.Exec:", err)
// } }
// if err != nil { }
// t.Error("Failed to call res.RowsAffected:", err)
// }
//}
//
//func TestQueryer(t *testing.T) { //func TestQueryer(t *testing.T) {
// tempFilename := TempFilename() // tempFilename := TempFilename()
// db, err := sql.Open("sqlite3", tempFilename) // db, err := sql.Open("sqlite3", tempFilename)