1 // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
10 #include <sqlite3-binding.h>
22 // SQLiteBackup implement interface of Backup.
23 type SQLiteBackup struct {
27 // Backup make backup from src to dest.
28 func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteBackup, error) {
29 destptr := C.CString(dest)
30 defer C.free(unsafe.Pointer(destptr))
31 srcptr := C.CString(src)
32 defer C.free(unsafe.Pointer(srcptr))
34 if b := C.sqlite3_backup_init(c.db, destptr, conn.db, srcptr); b != nil {
35 bb := &SQLiteBackup{b: b}
36 runtime.SetFinalizer(bb, (*SQLiteBackup).Finish)
39 return nil, c.lastError()
42 // Step to backs up for one step. Calls the underlying `sqlite3_backup_step`
43 // function. This function returns a boolean indicating if the backup is done
44 // and an error signalling any other error. Done is returned if the underlying
45 // C function returns SQLITE_DONE (Code 101)
46 func (b *SQLiteBackup) Step(p int) (bool, error) {
47 ret := C.sqlite3_backup_step(b.b, C.int(p))
48 if ret == C.SQLITE_DONE {
50 } else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY {
51 return false, Error{Code: ErrNo(ret)}
56 // Remaining return whether have the rest for backup.
57 func (b *SQLiteBackup) Remaining() int {
58 return int(C.sqlite3_backup_remaining(b.b))
61 // PageCount return count of pages.
62 func (b *SQLiteBackup) PageCount() int {
63 return int(C.sqlite3_backup_pagecount(b.b))
66 // Finish close backup.
67 func (b *SQLiteBackup) Finish() error {
71 // Close close backup.
72 func (b *SQLiteBackup) Close() error {
73 ret := C.sqlite3_backup_finish(b.b)
75 // sqlite3_backup_finish() never fails, it just returns the
76 // error code from previous operations, so clean up before
77 // checking and returning an error
79 runtime.SetFinalizer(b, nil)
82 return Error{Code: ErrNo(ret)}