Merge pull request #132 from fiber/master

implicitly close Stmt in Queryer, Close #131
systemlib
mattn 2014-07-16 17:35:13 +09:00
commit 3db806ab95
1 changed files with 8 additions and 2 deletions

View File

@ -102,6 +102,7 @@ type SQLiteStmt struct {
s *C.sqlite3_stmt s *C.sqlite3_stmt
t string t string
closed bool closed bool
cls bool
} }
// Result struct. // Result struct.
@ -116,6 +117,7 @@ type SQLiteRows struct {
nc int nc int
cols []string cols []string
decltype []string decltype []string
cls bool
} }
// Commit transaction. // Commit transaction.
@ -165,10 +167,10 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err
args = args[na:] args = args[na:]
} }
tail := s.(*SQLiteStmt).t tail := s.(*SQLiteStmt).t
s.Close()
if tail == "" { if tail == "" {
return res, nil return res, nil
} }
s.Close()
query = tail query = tail
} }
} }
@ -180,6 +182,7 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.(*SQLiteStmt).cls = true
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 {
@ -389,7 +392,7 @@ func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
if err := s.bind(args); err != nil { if err := s.bind(args); err != nil {
return nil, err return nil, err
} }
return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil}, nil return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil
} }
// Return last inserted ID. // Return last inserted ID.
@ -424,6 +427,9 @@ func (rc *SQLiteRows) Close() error {
if rc.s.closed { if rc.s.closed {
return nil return nil
} }
if rc.cls {
return rc.s.Close()
}
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 rc.s.c.lastError() return rc.s.c.lastError()