set/reset finalizer to free SQLite3 handles

This commit is contained in:
mattn 2014-11-14 17:13:35 +09:00
parent 1d8f24c39f
commit e63d2546a0
2 changed files with 23 additions and 9 deletions

View File

@ -11,21 +11,24 @@ package sqlite3
*/ */
import "C" import "C"
import ( import (
"runtime"
"unsafe" "unsafe"
) )
type Backup struct { type SQLiteBackup struct {
b *C.sqlite3_backup b *C.sqlite3_backup
} }
func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*Backup, error) { func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteBackup, error) {
destptr := C.CString(dest) destptr := C.CString(dest)
defer C.free(unsafe.Pointer(destptr)) defer C.free(unsafe.Pointer(destptr))
srcptr := C.CString(src) srcptr := C.CString(src)
defer C.free(unsafe.Pointer(srcptr)) defer C.free(unsafe.Pointer(srcptr))
if b := C.sqlite3_backup_init(c.db, destptr, conn.db, srcptr); b != nil { if b := C.sqlite3_backup_init(c.db, destptr, conn.db, srcptr); b != nil {
return &Backup{b: b}, nil bb := &SQLiteBackup{b: b}
runtime.SetFinalizer(bb, (*SQLiteBackup).Finish)
return bb, nil
} }
return nil, c.lastError() return nil, c.lastError()
} }
@ -34,7 +37,7 @@ func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*Backup,
// This function returns a boolean indicating if the backup is done and // This function returns a boolean indicating if the backup is done and
// an error signalling any other error. Done is returned if the underlying C // an error signalling any other error. Done is returned if the underlying C
// function returns SQLITE_DONE (Code 101) // function returns SQLITE_DONE (Code 101)
func (b *Backup) Step(p int) (bool, error) { func (b *SQLiteBackup) Step(p int) (bool, error) {
ret := C.sqlite3_backup_step(b.b, C.int(p)) ret := C.sqlite3_backup_step(b.b, C.int(p))
if ret == C.SQLITE_DONE { if ret == C.SQLITE_DONE {
return true, nil return true, nil
@ -44,18 +47,24 @@ func (b *Backup) Step(p int) (bool, error) {
return false, nil return false, nil
} }
func (b *Backup) Remaining() int { func (b *SQLiteBackup) Remaining() int {
return int(C.sqlite3_backup_remaining(b.b)) return int(C.sqlite3_backup_remaining(b.b))
} }
func (b *Backup) PageCount() int { func (b *SQLiteBackup) PageCount() int {
return int(C.sqlite3_backup_pagecount(b.b)) return int(C.sqlite3_backup_pagecount(b.b))
} }
func (b *Backup) Finish() error { func (b *SQLiteBackup) Finish() error {
return b.Close()
}
func (b *SQLiteBackup) Close() error {
ret := C.sqlite3_backup_finish(b.b) ret := C.sqlite3_backup_finish(b.b)
if ret != 0 { if ret != 0 {
return Error{Code: ErrNo(ret)} return Error{Code: ErrNo(ret)}
} }
b.b = nil
runtime.SetFinalizer(b, nil)
return nil return nil
} }

View File

@ -61,6 +61,7 @@ import (
"database/sql/driver" "database/sql/driver"
"errors" "errors"
"io" "io"
"runtime"
"strings" "strings"
"time" "time"
"unsafe" "unsafe"
@ -306,7 +307,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
return nil, err return nil, err
} }
} }
runtime.SetFinalizer(conn, (*SQLiteConn).Close)
return conn, nil return conn, nil
} }
@ -317,6 +318,7 @@ func (c *SQLiteConn) Close() error {
return c.lastError() return c.lastError()
} }
c.db = nil c.db = nil
runtime.SetFinalizer(c, nil)
return nil return nil
} }
@ -334,7 +336,9 @@ func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
if tail != nil && C.strlen(tail) > 0 { if tail != nil && C.strlen(tail) > 0 {
t = strings.TrimSpace(C.GoString(tail)) t = strings.TrimSpace(C.GoString(tail))
} }
return &SQLiteStmt{c: c, s: s, t: t}, nil ss := &SQLiteStmt{c: c, s: s, t: t}
runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
return ss, nil
} }
// Close the statement. // Close the statement.
@ -350,6 +354,7 @@ func (s *SQLiteStmt) Close() error {
if rv != C.SQLITE_OK { if rv != C.SQLITE_OK {
return s.c.lastError() return s.c.lastError()
} }
runtime.SetFinalizer(s, nil)
return nil return nil
} }