Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add backup. Close #104

  • Loading branch information...
commit e52d4b08c11d178166bdb6fb1b2d7742e57f2a6b 1 parent ba91ba9
@mattn authored
Showing with 113 additions and 0 deletions.
  1. +71 −0 _example/hook/hook.go
  2. +42 −0 backup.go
View
71 _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()
+}
View
42 backup.go
@@ -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))}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.