Add backup. Close #104

This commit is contained in:
mattn 2014-01-30 19:45:09 +09:00
parent ba91ba98f9
commit e52d4b08c1
2 changed files with 113 additions and 0 deletions

71
_example/hook/hook.go Normal file
View File

@ -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()
}

42
backup.go Normal file
View File

@ -0,0 +1,42 @@
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()
}
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))}
}