-
Notifications
You must be signed in to change notification settings - Fork 24
/
backup.go
62 lines (53 loc) · 1.71 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sqlite3
// #include <sqlite3.h>
// #include <stdlib.h>
import "C"
import (
"unsafe"
)
// Backup implements the SQLite Online Backup API.
//
// The backup API copies the content of one database to another. It is
// useful either for creating backups of databases or for copying in-memory
// databases to or from persistent files.
type Backup struct {
cptr *C.sqlite3_backup
db *Database
}
// NewBackup initializes and returns the handle to a backup.
func NewBackup(d *Database, ddb string, s *Database, sdb string) (b *Backup, e error) {
dname := C.CString(ddb)
defer C.free(unsafe.Pointer(dname))
sname := C.CString(sdb)
defer C.free(unsafe.Pointer(sname))
if cptr := C.sqlite3_backup_init(d.handle, dname, s.handle, sname); cptr != nil {
b = &Backup{cptr: cptr, db: d}
} else {
e = d.Error()
}
return
}
// Step will copy up to `pages` between the source and destination database.
// If `pages` is negative, all remaining source pages are copied.
func (b *Backup) Step(pages int) error {
return SQLiteError(C.sqlite3_backup_step(b.cptr, C.int(pages)))
}
// Remaining returns the number of pages still to be backed up.
func (b *Backup) Remaining() int {
return int(C.sqlite3_backup_remaining(b.cptr))
}
// PageCount returns the total number of pages in the source database.
func (b *Backup) PageCount() int {
return int(C.sqlite3_backup_pagecount(b.cptr))
}
// Finish should be called when the backup is done, an error occured or when
// the application wants to abandon the backup operation.
func (b *Backup) Finish() error {
return SQLiteError(C.sqlite3_backup_finish(b.cptr))
}
// Full creates a full backup of the database.
func (b *Backup) Full() error {
b.Step(-1)
b.Finish()
return b.db.Error()
}