aboutsummaryrefslogtreecommitdiff
path: root/backup.go
diff options
context:
space:
mode:
authorPhilip O'Toole <philip.otoole@yahoo.com>2016-02-23 01:18:14 -0500
committerPhilip O'Toole <philip.otoole@yahoo.com>2016-02-23 01:18:14 -0500
commit3e97a4ca68500045276a2ba7051740bd53e40d06 (patch)
treed984cc8023dffd95fd37fc6ed293ba80f973592a /backup.go
parentMerge pull request #134 from antoine-lizee/patch-1 (diff)
parentMerge pull request #267 from ianlancetaylor/go16 (diff)
downloadgolite-3e97a4ca68500045276a2ba7051740bd53e40d06.tar.gz
golite-3e97a4ca68500045276a2ba7051740bd53e40d06.tar.xz
Merge pull request #1 from mattn/master
Bring master up-to-date
Diffstat (limited to 'backup.go')
-rw-r--r--backup.go34
1 files changed, 24 insertions, 10 deletions
diff --git a/backup.go b/backup.go
index ab56961..3807c60 100644
--- a/backup.go
+++ b/backup.go
@@ -1,26 +1,34 @@
+// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
+//
+// Use of this source code is governed by an MIT-style
+// license that can be found in the LICENSE file.
+
package sqlite3
/*
-#include <sqlite3.h>
+#include <sqlite3-binding.h>
#include <stdlib.h>
*/
import "C"
import (
+ "runtime"
"unsafe"
)
-type Backup struct {
+type SQLiteBackup struct {
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)
defer C.free(unsafe.Pointer(destptr))
srcptr := C.CString(src)
defer C.free(unsafe.Pointer(srcptr))
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()
}
@@ -29,28 +37,34 @@ func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*Backup,
// 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
// 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))
- if ret == 101 {
+ if ret == C.SQLITE_DONE {
return true, nil
- } else if ret != 0 {
+ } else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY {
return false, Error{Code: ErrNo(ret)}
}
return false, nil
}
-func (b *Backup) Remaining() int {
+func (b *SQLiteBackup) Remaining() int {
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))
}
-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)
if ret != 0 {
return Error{Code: ErrNo(ret)}
}
+ b.b = nil
+ runtime.SetFinalizer(b, nil)
return nil
}