go-sqlite3-hak5/backup.go

57 lines
1.3 KiB
Go
Raw Normal View History

2014-01-30 10:45:09 +00:00
package sqlite3
/*
#include <sqlite3.h>
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)
type Backup struct {
b *C.sqlite3_backup
}
func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*Backup, 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
}
return nil, c.lastError()
}
2014-07-11 14:31:28 +00:00
// Backs up for one step. Calls the underlying `sqlite3_backup_step` function.
// 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) {
2014-05-27 01:35:20 +00:00
ret := C.sqlite3_backup_step(b.b, C.int(p))
2014-07-11 14:31:28 +00:00
if ret == 101 {
return true, nil
} else if ret != 0 {
return false, Error{Code: ErrNo(ret)}
2014-05-27 01:35:20 +00:00
}
2014-07-11 14:31:28 +00:00
return false, nil
2014-01-30 10:45:09 +00:00
}
func (b *Backup) Remaining() int {
return int(C.sqlite3_backup_remaining(b.b))
}
func (b *Backup) PageCount() int {
return int(C.sqlite3_backup_pagecount(b.b))
}
func (b *Backup) Finish() error {
2014-05-27 01:35:20 +00:00
ret := C.sqlite3_backup_finish(b.b)
if ret != 0 {
return Error{Code: ErrNo(ret)}
}
return nil
2014-01-30 10:45:09 +00:00
}