2019-11-18 12:03:31 +03:00
|
|
|
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
2014-08-18 11:56:31 +04:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-08-18 12:00:59 +04:00
|
|
|
|
2014-01-30 14:45:09 +04:00
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
/*
|
2016-04-02 13:32:47 +03:00
|
|
|
#ifndef USE_LIBSQLITE3
|
2022-02-24 02:43:59 +03:00
|
|
|
#include "amalgamation/sqlite3-binding.h"
|
2016-04-02 13:32:47 +03:00
|
|
|
#else
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#endif
|
2014-01-30 14:45:09 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2014-11-14 11:13:35 +03:00
|
|
|
"runtime"
|
2014-01-30 14:45:09 +04:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2016-11-04 18:40:06 +03:00
|
|
|
// SQLiteBackup implement interface of Backup.
|
2014-11-14 11:13:35 +03:00
|
|
|
type SQLiteBackup struct {
|
2014-01-30 14:45:09 +04:00
|
|
|
b *C.sqlite3_backup
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:40:06 +03:00
|
|
|
// Backup make backup from src to dest.
|
2019-10-31 17:26:56 +03:00
|
|
|
func (destConn *SQLiteConn) Backup(dest string, srcConn *SQLiteConn, src string) (*SQLiteBackup, error) {
|
2014-01-30 14:45:09 +04:00
|
|
|
destptr := C.CString(dest)
|
|
|
|
defer C.free(unsafe.Pointer(destptr))
|
|
|
|
srcptr := C.CString(src)
|
|
|
|
defer C.free(unsafe.Pointer(srcptr))
|
|
|
|
|
2019-10-31 17:26:56 +03:00
|
|
|
if b := C.sqlite3_backup_init(destConn.db, destptr, srcConn.db, srcptr); b != nil {
|
2014-11-14 11:13:35 +03:00
|
|
|
bb := &SQLiteBackup{b: b}
|
|
|
|
runtime.SetFinalizer(bb, (*SQLiteBackup).Finish)
|
|
|
|
return bb, nil
|
2014-01-30 14:45:09 +04:00
|
|
|
}
|
2019-10-31 17:26:56 +03:00
|
|
|
return nil, destConn.lastError()
|
2014-01-30 14:45:09 +04:00
|
|
|
}
|
|
|
|
|
2016-11-04 18:40:06 +03: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)
|
2014-11-14 11:13:35 +03:00
|
|
|
func (b *SQLiteBackup) Step(p int) (bool, error) {
|
2014-05-27 05:35:20 +04:00
|
|
|
ret := C.sqlite3_backup_step(b.b, C.int(p))
|
2014-08-18 11:52:06 +04:00
|
|
|
if ret == C.SQLITE_DONE {
|
2014-07-11 18:31:28 +04:00
|
|
|
return true, nil
|
2014-11-14 13:37:23 +03:00
|
|
|
} else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY {
|
2017-03-21 03:14:48 +03:00
|
|
|
return false, Error{Code: ErrNo(ret)}
|
2014-05-27 05:35:20 +04:00
|
|
|
}
|
2014-07-11 18:31:28 +04:00
|
|
|
return false, nil
|
2014-01-30 14:45:09 +04:00
|
|
|
}
|
|
|
|
|
2016-11-04 18:40:06 +03:00
|
|
|
// Remaining return whether have the rest for backup.
|
2014-11-14 11:13:35 +03:00
|
|
|
func (b *SQLiteBackup) Remaining() int {
|
2014-01-30 14:45:09 +04:00
|
|
|
return int(C.sqlite3_backup_remaining(b.b))
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:40:06 +03:00
|
|
|
// PageCount return count of pages.
|
2014-11-14 11:13:35 +03:00
|
|
|
func (b *SQLiteBackup) PageCount() int {
|
2014-01-30 14:45:09 +04:00
|
|
|
return int(C.sqlite3_backup_pagecount(b.b))
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:40:06 +03:00
|
|
|
// Finish close backup.
|
2014-11-14 11:13:35 +03:00
|
|
|
func (b *SQLiteBackup) Finish() error {
|
|
|
|
return b.Close()
|
|
|
|
}
|
|
|
|
|
2016-11-04 18:40:06 +03:00
|
|
|
// Close close backup.
|
2014-11-14 11:13:35 +03:00
|
|
|
func (b *SQLiteBackup) Close() error {
|
2014-05-27 05:35:20 +04:00
|
|
|
ret := C.sqlite3_backup_finish(b.b)
|
2016-10-28 02:44:56 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2014-05-27 05:35:20 +04:00
|
|
|
if ret != 0 {
|
2017-03-21 03:14:48 +03:00
|
|
|
return Error{Code: ErrNo(ret)}
|
2014-05-27 05:35:20 +04:00
|
|
|
}
|
|
|
|
return nil
|
2014-01-30 14:45:09 +04:00
|
|
|
}
|