Skip to content

Commit 3db806a

Browse files
committed
Merge pull request #132 from fiber/master
implicitly close Stmt in Queryer, Close #131
2 parents f87f73c + deaffef commit 3db806a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

sqlite3.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type SQLiteStmt struct {
102102
s *C.sqlite3_stmt
103103
t string
104104
closed bool
105+
cls bool
105106
}
106107

107108
// Result struct.
@@ -116,6 +117,7 @@ type SQLiteRows struct {
116117
nc int
117118
cols []string
118119
decltype []string
120+
cls bool
119121
}
120122

121123
// Commit transaction.
@@ -165,10 +167,10 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err
165167
args = args[na:]
166168
}
167169
tail := s.(*SQLiteStmt).t
170+
s.Close()
168171
if tail == "" {
169172
return res, nil
170173
}
171-
s.Close()
172174
query = tail
173175
}
174176
}
@@ -180,6 +182,7 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro
180182
if err != nil {
181183
return nil, err
182184
}
185+
s.(*SQLiteStmt).cls = true
183186
na := s.NumInput()
184187
rows, err := s.Query(args[:na])
185188
if err != nil && err != driver.ErrSkip {
@@ -389,7 +392,7 @@ func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
389392
if err := s.bind(args); err != nil {
390393
return nil, err
391394
}
392-
return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil}, nil
395+
return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil
393396
}
394397

395398
// Return last inserted ID.
@@ -424,6 +427,9 @@ func (rc *SQLiteRows) Close() error {
424427
if rc.s.closed {
425428
return nil
426429
}
430+
if rc.cls {
431+
return rc.s.Close()
432+
}
427433
rv := C.sqlite3_reset(rc.s.s)
428434
if rv != C.SQLITE_OK {
429435
return rc.s.c.lastError()

0 commit comments

Comments
 (0)