2020-06-17 19:07:58 +00:00
|
|
|
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
2016-11-03 22:16:01 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
/*
|
|
|
|
#ifndef USE_LIBSQLITE3
|
|
|
|
#include <sqlite3-binding.h>
|
|
|
|
#else
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// SQLiteBackup implement interface of Backup.
|
2016-11-03 22:16:01 +00:00
|
|
|
type SQLiteBackup struct {
|
|
|
|
b *C.sqlite3_backup
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// Backup make backup from src to dest.
|
2020-06-17 19:07:58 +00:00
|
|
|
func (destConn *SQLiteConn) Backup(dest string, srcConn *SQLiteConn, src string) (*SQLiteBackup, error) {
|
2016-11-03 22:16:01 +00:00
|
|
|
destptr := C.CString(dest)
|
|
|
|
defer C.free(unsafe.Pointer(destptr))
|
|
|
|
srcptr := C.CString(src)
|
|
|
|
defer C.free(unsafe.Pointer(srcptr))
|
|
|
|
|
2020-06-17 19:07:58 +00:00
|
|
|
if b := C.sqlite3_backup_init(destConn.db, destptr, srcConn.db, srcptr); b != nil {
|
2016-11-03 22:16:01 +00:00
|
|
|
bb := &SQLiteBackup{b: b}
|
|
|
|
runtime.SetFinalizer(bb, (*SQLiteBackup).Finish)
|
|
|
|
return bb, nil
|
|
|
|
}
|
2020-06-17 19:07:58 +00:00
|
|
|
return nil, destConn.lastError()
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// Step to backs up for one step. Calls the underlying `sqlite3_backup_step`
|
|
|
|
// function. This function returns a boolean indicating if the backup is done
|
|
|
|
// and an error signalling any other error. Done is returned if the underlying
|
|
|
|
// C function returns SQLITE_DONE (Code 101)
|
2016-11-03 22:16:01 +00:00
|
|
|
func (b *SQLiteBackup) Step(p int) (bool, error) {
|
|
|
|
ret := C.sqlite3_backup_step(b.b, C.int(p))
|
|
|
|
if ret == C.SQLITE_DONE {
|
|
|
|
return true, nil
|
|
|
|
} else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY {
|
|
|
|
return false, Error{Code: ErrNo(ret)}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// Remaining return whether have the rest for backup.
|
2016-11-03 22:16:01 +00:00
|
|
|
func (b *SQLiteBackup) Remaining() int {
|
|
|
|
return int(C.sqlite3_backup_remaining(b.b))
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// PageCount return count of pages.
|
2016-11-03 22:16:01 +00:00
|
|
|
func (b *SQLiteBackup) PageCount() int {
|
|
|
|
return int(C.sqlite3_backup_pagecount(b.b))
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// Finish close backup.
|
2016-11-03 22:16:01 +00:00
|
|
|
func (b *SQLiteBackup) Finish() error {
|
|
|
|
return b.Close()
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:43:30 +00:00
|
|
|
// Close close backup.
|
2016-11-03 22:16:01 +00:00
|
|
|
func (b *SQLiteBackup) Close() error {
|
|
|
|
ret := C.sqlite3_backup_finish(b.b)
|
|
|
|
|
|
|
|
// sqlite3_backup_finish() never fails, it just returns the
|
|
|
|
// error code from previous operations, so clean up before
|
|
|
|
// checking and returning an error
|
|
|
|
b.b = nil
|
|
|
|
runtime.SetFinalizer(b, nil)
|
|
|
|
|
|
|
|
if ret != 0 {
|
|
|
|
return Error{Code: ErrNo(ret)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|