Skip to content

Commit 6690238

Browse files
committed
extract finalizeCachedStmt helper and drop redundant tail reset
1 parent 59e8e75 commit 6690238

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

sqlite3.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,9 +1934,11 @@ func (c *SQLiteConn) takeCachedStmt(query string) *SQLiteStmt {
19341934
copy(c.stmtCache[i:n-1], c.stmtCache[i+1:n])
19351935
c.stmtCache[n-1] = nil
19361936
c.stmtCache = c.stmtCache[:n-1]
1937+
// The stmt was marked closed by Close before being cached, and
1938+
// cls may have been set if Query opened it; reset both so the
1939+
// caller gets a stmt equivalent to a fresh Prepare.
19371940
s.closed = false
19381941
s.cls = false
1939-
s.t = ""
19401942
return s
19411943
}
19421944
return nil
@@ -1960,14 +1962,7 @@ func (c *SQLiteConn) putCachedStmt(s *SQLiteStmt) bool {
19601962
// If full, finalize the LRU entry at index 0 and shift left; the
19611963
// freed tail slot is immediately reused by the append below.
19621964
if len(c.stmtCache) == cap(c.stmtCache) {
1963-
victim := c.stmtCache[0]
1964-
runtime.SetFinalizer(victim, nil)
1965-
if victim.s != nil {
1966-
C.sqlite3_finalize(victim.s)
1967-
victim.s = nil
1968-
}
1969-
victim.c = nil
1970-
victim.closed = true
1965+
finalizeCachedStmt(c.stmtCache[0])
19711966
copy(c.stmtCache, c.stmtCache[1:])
19721967
c.stmtCache = c.stmtCache[:len(c.stmtCache)-1]
19731968
}
@@ -1978,15 +1973,25 @@ func (c *SQLiteConn) putCachedStmt(s *SQLiteStmt) bool {
19781973
func (c *SQLiteConn) closeCachedStmtsLocked() {
19791974
for i, s := range c.stmtCache {
19801975
c.stmtCache[i] = nil
1981-
if s == nil || s.s == nil {
1982-
continue
1983-
}
1984-
runtime.SetFinalizer(s, nil)
1976+
finalizeCachedStmt(s)
1977+
}
1978+
c.stmtCache = c.stmtCache[:0]
1979+
}
1980+
1981+
// finalizeCachedStmt tears down a stmt that was sitting in the connection's
1982+
// stmt cache. The caller must hold c.mu. It is safe to pass a nil stmt or a
1983+
// stmt whose handle has already been released.
1984+
func finalizeCachedStmt(s *SQLiteStmt) {
1985+
if s == nil {
1986+
return
1987+
}
1988+
runtime.SetFinalizer(s, nil)
1989+
if s.s != nil {
19851990
C.sqlite3_finalize(s.s)
19861991
s.s = nil
1987-
s.c = nil
19881992
}
1989-
c.stmtCache = c.stmtCache[:0]
1993+
s.c = nil
1994+
s.closed = true
19901995
}
19911996

19921997
// Prepare the query string. Return a new statement.

0 commit comments

Comments
 (0)