From e52d4b08c11d178166bdb6fb1b2d7742e57f2a6b Mon Sep 17 00:00:00 2001 From: mattn Date: Thu, 30 Jan 2014 19:45:09 +0900 Subject: [PATCH] Add backup. Close #104 --- _example/hook/hook.go | 71 +++++++++++++++++++++++++++++++++++++++++++ backup.go | 42 +++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 _example/hook/hook.go create mode 100644 backup.go diff --git a/_example/hook/hook.go b/_example/hook/hook.go new file mode 100644 index 0000000..59f8cd4 --- /dev/null +++ b/_example/hook/hook.go @@ -0,0 +1,71 @@ +package main + +import ( + "database/sql" + "github.com/mattn/go-sqlite3" + "log" + "os" +) + +func main() { + sqlite3conn := []*sqlite3.SQLiteConn{} + sql.Register("sqlite3_with_hook_example", + &sqlite3.SQLiteDriver{ + ConnectHook: func(conn *sqlite3.SQLiteConn) error { + sqlite3conn = append(sqlite3conn, conn) + return nil + }, + }) + os.Remove("./foo.db") + os.Remove("./bar.db") + + destDb, err := sql.Open("sqlite3_with_hook_example", "./foo.db") + if err != nil { + log.Fatal(err) + } + defer destDb.Close() + destDb.Ping() + + _, err = destDb.Exec("create table foo(id int, value text)") + if err != nil { + log.Fatal(err) + } + _, err = destDb.Exec("insert into foo values(1, 'foo')") + if err != nil { + log.Fatal(err) + } + _, err = destDb.Exec("insert into foo values(2, 'bar')") + if err != nil { + log.Fatal(err) + } + _, err = destDb.Query("select * from foo") + if err != nil { + log.Fatal(err) + } + srcDb, err := sql.Open("sqlite3_with_hook_example", "./bar.db") + if err != nil { + log.Fatal(err) + } + defer srcDb.Close() + srcDb.Ping() + + bk, err := sqlite3conn[1].Backup("main", sqlite3conn[0], "main") + if err != nil { + log.Fatal(err) + } + + bk.Step(-1) + if err != nil { + log.Fatal(err) + } + _, err = destDb.Query("select * from foo") + if err != nil { + log.Fatal(err) + } + _, err = destDb.Exec("insert into foo values(3, 'bar')") + if err != nil { + log.Fatal(err) + } + + bk.Finish() +} diff --git a/backup.go b/backup.go new file mode 100644 index 0000000..6de4499 --- /dev/null +++ b/backup.go @@ -0,0 +1,42 @@ +package sqlite3 + +/* +#include +#include +*/ +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() +} + +func (b *Backup) Step(p int) error { + return Error{Code: ErrNo(C.sqlite3_backup_step(b.b, C.int(p)))} +} + +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 { + return Error{Code: ErrNo(C.sqlite3_backup_finish(b.b))} +}