From deaffef7cbe2b9a13cfd8fd85ec194a5ba8a01ed Mon Sep 17 00:00:00 2001 From: Sven Engelhardt Date: Tue, 15 Jul 2014 18:11:07 +0200 Subject: [PATCH] implicitly close Stmt in Queryer, Close #131 the cls field tracks if the Stmt should be implicitly closed, in the Exec() call the generated statement is always closed --- sqlite3.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index d95d551..fc3e8ad 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -102,6 +102,7 @@ type SQLiteStmt struct { s *C.sqlite3_stmt t string closed bool + cls bool } // Result struct. @@ -116,6 +117,7 @@ type SQLiteRows struct { nc int cols []string decltype []string + cls bool } // Commit transaction. @@ -165,10 +167,10 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err args = args[na:] } tail := s.(*SQLiteStmt).t + s.Close() if tail == "" { return res, nil } - s.Close() query = tail } } @@ -180,6 +182,7 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro if err != nil { return nil, err } + s.(*SQLiteStmt).cls = true na := s.NumInput() rows, err := s.Query(args[:na]) 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 { 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. @@ -424,6 +427,9 @@ func (rc *SQLiteRows) Close() error { if rc.s.closed { return nil } + if rc.cls { + return rc.s.Close() + } rv := C.sqlite3_reset(rc.s.s) if rv != C.SQLITE_OK { return rc.s.c.lastError()