github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/mattn/go-sqlite3/backup.go (about) 1 // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>. 2 // 3 // Use of this source code is governed by an MIT-style 4 // license that can be found in the LICENSE file. 5 6 package sqlite3 7 8 /* 9 #include <sqlite3-binding.h> 10 #include <stdlib.h> 11 */ 12 import "C" 13 import ( 14 "runtime" 15 "unsafe" 16 ) 17 18 type SQLiteBackup struct { 19 b *C.sqlite3_backup 20 } 21 22 func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteBackup, error) { 23 destptr := C.CString(dest) 24 defer C.free(unsafe.Pointer(destptr)) 25 srcptr := C.CString(src) 26 defer C.free(unsafe.Pointer(srcptr)) 27 28 if b := C.sqlite3_backup_init(c.db, destptr, conn.db, srcptr); b != nil { 29 bb := &SQLiteBackup{b: b} 30 runtime.SetFinalizer(bb, (*SQLiteBackup).Finish) 31 return bb, nil 32 } 33 return nil, c.lastError() 34 } 35 36 // Backs up for one step. Calls the underlying `sqlite3_backup_step` function. 37 // This function returns a boolean indicating if the backup is done and 38 // an error signalling any other error. Done is returned if the underlying C 39 // function returns SQLITE_DONE (Code 101) 40 func (b *SQLiteBackup) Step(p int) (bool, error) { 41 ret := C.sqlite3_backup_step(b.b, C.int(p)) 42 if ret == C.SQLITE_DONE { 43 return true, nil 44 } else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY { 45 return false, Error{Code: ErrNo(ret)} 46 } 47 return false, nil 48 } 49 50 func (b *SQLiteBackup) Remaining() int { 51 return int(C.sqlite3_backup_remaining(b.b)) 52 } 53 54 func (b *SQLiteBackup) PageCount() int { 55 return int(C.sqlite3_backup_pagecount(b.b)) 56 } 57 58 func (b *SQLiteBackup) Finish() error { 59 return b.Close() 60 } 61 62 func (b *SQLiteBackup) Close() error { 63 ret := C.sqlite3_backup_finish(b.b) 64 if ret != 0 { 65 return Error{Code: ErrNo(ret)} 66 } 67 b.b = nil 68 runtime.SetFinalizer(b, nil) 69 return nil 70 }