Fixes Execer/Queryer

This commit is contained in:
mattn 2013-09-09 12:28:34 +09:00
parent fc9f8cab24
commit 77ebf39cf9
2 changed files with 37 additions and 34 deletions

View File

@ -136,50 +136,48 @@ func (c *SQLiteConn) AutoCommit() bool {
// 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) {
var res driver.Result
for { for {
ds, err := c.Prepare(query) s, err := c.Prepare(query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := ds.(*SQLiteStmt)
na := s.NumInput() na := s.NumInput()
res, err := s.Exec(args[:na]) res, err = s.Exec(args[:na])
args = args[na:] if err != nil && err != driver.ErrSkip {
if err != nil {
s.Close() s.Close()
return nil, err return nil, err
} }
if s.t == "" { args = args[na:]
tail := s.(*SQLiteStmt).t
if tail == "" {
return res, nil return res, nil
} }
s.Close() s.Close()
query = s.t 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) {
var rows driver.Rows
for { for {
ds, err := c.Prepare(query) s, err := c.Prepare(query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := ds.(*SQLiteStmt)
na := s.NumInput() na := s.NumInput()
rows, err := s.Query(args[:na]) rows, err = s.Query(args[:na])
args = args[na:] if err != nil && err != driver.ErrSkip {
if err != nil {
s.Close() s.Close()
return nil, err return nil, err
} }
if s.t == "" { args = args[na:]
tail := s.(*SQLiteStmt).t
if tail == "" {
return rows, nil return rows, nil
} }
if rows != nil { query = tail
rows.Close()
}
s.Close()
query = s.t
} }
} }
@ -418,6 +416,9 @@ func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
// Close the rows. // Close the rows.
func (rc *SQLiteRows) Close() error { func (rc *SQLiteRows) Close() error {
if rc.s.closed {
return nil
}
rv := C.sqlite3_reset(rc.s.s) rv := C.sqlite3_reset(rc.s.s)
if rv != C.SQLITE_OK { if rv != C.SQLITE_OK {
return ErrNo(rv) return ErrNo(rv)

View File

@ -591,9 +591,9 @@ func TestExecer(t *testing.T) {
_, err = db.Exec(` _, err = db.Exec(`
create table foo (id integer); create table foo (id integer);
insert into foo values(1); insert into foo(id) values(1);
insert into foo values(2); insert into foo(id) values(2);
insert into foo values(3); insert into foo(id) values(3);
`) `)
if err != nil { if err != nil {
t.Error("Failed to call db.Exec:", err) t.Error("Failed to call db.Exec:", err)
@ -614,24 +614,26 @@ func TestQueryer(t *testing.T) {
rows, err := db.Query(` rows, err := db.Query(`
create table foo (id integer); create table foo (id integer);
insert into foo values(1); insert into foo(id) values(?);
insert into foo values(2); insert into foo(id) values(?);
insert into foo values(3); insert into foo(id) values(?);
select id from foo order by id; select id from foo order by id;
`) `, 3, 2, 1)
if err != nil { if err != nil {
t.Error("Failed to call db.Exec:", err) t.Error("Failed to call db.Query:", err)
} }
defer rows.Close() defer rows.Close()
n := 1 n := 1
for rows.Next() { if rows != nil {
var id int for rows.Next() {
err = rows.Scan(&id) var id int
if err != nil { err = rows.Scan(&id)
t.Error("Failed to db.Query:", err) if err != nil {
} t.Error("Failed to db.Query:", err)
if id != n { }
t.Error("Failed to db.Query: not matched results") if id != n {
t.Error("Failed to db.Query: not matched results")
}
} }
} }
} }