mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
rustfmt backup.rs
This commit is contained in:
parent
8c2e0a0da7
commit
3781b8f47f
@ -84,7 +84,8 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
|
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
|
||||||
/// `NULL`.
|
/// `NULL`.
|
||||||
pub fn new(from: &'a SqliteConnection,
|
pub fn new(from: &'a SqliteConnection,
|
||||||
to: &'b mut SqliteConnection) -> SqliteResult<Backup<'a, 'b>> {
|
to: &'b mut SqliteConnection)
|
||||||
|
-> SqliteResult<Backup<'a, 'b>> {
|
||||||
Backup::new_with_names(from, DatabaseName::Main, to, DatabaseName::Main)
|
Backup::new_with_names(from, DatabaseName::Main, to, DatabaseName::Main)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,18 +98,21 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
|
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
|
||||||
/// `NULL`.
|
/// `NULL`.
|
||||||
pub fn new_with_names(from: &'a SqliteConnection, from_name: DatabaseName,
|
pub fn new_with_names(from: &'a SqliteConnection,
|
||||||
to: &'b mut SqliteConnection, to_name: DatabaseName)
|
from_name: DatabaseName,
|
||||||
-> SqliteResult<Backup<'a, 'b>>
|
to: &'b mut SqliteConnection,
|
||||||
{
|
to_name: DatabaseName)
|
||||||
|
-> SqliteResult<Backup<'a, 'b>> {
|
||||||
let to_name = try!(to_name.to_cstring());
|
let to_name = try!(to_name.to_cstring());
|
||||||
let from_name = try!(from_name.to_cstring());
|
let from_name = try!(from_name.to_cstring());
|
||||||
|
|
||||||
let to_db = to.db.borrow_mut().db;
|
let to_db = to.db.borrow_mut().db;
|
||||||
|
|
||||||
let b = unsafe {
|
let b = unsafe {
|
||||||
let b = ffi::sqlite3_backup_init(to_db, to_name.as_ptr(),
|
let b = ffi::sqlite3_backup_init(to_db,
|
||||||
from.db.borrow_mut().db, from_name.as_ptr());
|
to_name.as_ptr(),
|
||||||
|
from.db.borrow_mut().db,
|
||||||
|
from_name.as_ptr());
|
||||||
if b.is_null() {
|
if b.is_null() {
|
||||||
return Err(SqliteError::from_handle(to_db, ffi::sqlite3_errcode(to_db)));
|
return Err(SqliteError::from_handle(to_db, ffi::sqlite3_errcode(to_db)));
|
||||||
}
|
}
|
||||||
@ -147,16 +151,18 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
pub fn step(&self, num_pages: c_int) -> SqliteResult<StepResult> {
|
pub fn step(&self, num_pages: c_int) -> SqliteResult<StepResult> {
|
||||||
use self::StepResult::{Done, More, Busy, Locked};
|
use self::StepResult::{Done, More, Busy, Locked};
|
||||||
|
|
||||||
let rc = unsafe {
|
let rc = unsafe { ffi::sqlite3_backup_step(self.b, num_pages) };
|
||||||
ffi::sqlite3_backup_step(self.b, num_pages)
|
|
||||||
};
|
|
||||||
match rc {
|
match rc {
|
||||||
ffi::SQLITE_DONE => Ok(Done),
|
ffi::SQLITE_DONE => Ok(Done),
|
||||||
ffi::SQLITE_OK => Ok(More),
|
ffi::SQLITE_OK => Ok(More),
|
||||||
ffi::SQLITE_BUSY => Ok(Busy),
|
ffi::SQLITE_BUSY => Ok(Busy),
|
||||||
ffi::SQLITE_LOCKED => Ok(Locked),
|
ffi::SQLITE_LOCKED => Ok(Locked),
|
||||||
rc =>
|
rc => {
|
||||||
Err(SqliteError{ code: rc, message: ffi::code_to_str(rc).into() })
|
Err(SqliteError {
|
||||||
|
code: rc,
|
||||||
|
message: ffi::code_to_str(rc).into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,8 +181,11 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if any of the calls to `step` return `Err`.
|
/// Will return `Err` if any of the calls to `step` return `Err`.
|
||||||
pub fn run_to_completion(&self, pages_per_step: c_int, pause_between_pages: Duration,
|
pub fn run_to_completion(&self,
|
||||||
progress: Option<fn(Progress)>) -> SqliteResult<()> {
|
pages_per_step: c_int,
|
||||||
|
pause_between_pages: Duration,
|
||||||
|
progress: Option<fn(Progress)>)
|
||||||
|
-> SqliteResult<()> {
|
||||||
use self::StepResult::{Done, More, Busy, Locked};
|
use self::StepResult::{Done, More, Busy, Locked};
|
||||||
|
|
||||||
assert!(pages_per_step > 0, "pages_per_step must be positive");
|
assert!(pages_per_step > 0, "pages_per_step must be positive");
|
||||||
@ -207,6 +216,7 @@ mod test {
|
|||||||
use super::Backup;
|
use super::Backup;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
fn test_backup() {
|
fn test_backup() {
|
||||||
let src = SqliteConnection::open_in_memory().unwrap();
|
let src = SqliteConnection::open_in_memory().unwrap();
|
||||||
let sql = "BEGIN;
|
let sql = "BEGIN;
|
||||||
@ -237,6 +247,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
fn test_backup_temp() {
|
fn test_backup_temp() {
|
||||||
let src = SqliteConnection::open_in_memory().unwrap();
|
let src = SqliteConnection::open_in_memory().unwrap();
|
||||||
let sql = "BEGIN;
|
let sql = "BEGIN;
|
||||||
@ -248,7 +259,10 @@ mod test {
|
|||||||
let mut dst = SqliteConnection::open_in_memory().unwrap();
|
let mut dst = SqliteConnection::open_in_memory().unwrap();
|
||||||
|
|
||||||
{
|
{
|
||||||
let backup = Backup::new_with_names(&src, DatabaseName::Temp, &mut dst, DatabaseName::Main)
|
let backup = Backup::new_with_names(&src,
|
||||||
|
DatabaseName::Temp,
|
||||||
|
&mut dst,
|
||||||
|
DatabaseName::Main)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
backup.step(-1).unwrap();
|
backup.step(-1).unwrap();
|
||||||
}
|
}
|
||||||
@ -259,7 +273,10 @@ mod test {
|
|||||||
src.execute_batch("INSERT INTO foo VALUES(43)").unwrap();
|
src.execute_batch("INSERT INTO foo VALUES(43)").unwrap();
|
||||||
|
|
||||||
{
|
{
|
||||||
let backup = Backup::new_with_names(&src, DatabaseName::Temp, &mut dst, DatabaseName::Main)
|
let backup = Backup::new_with_names(&src,
|
||||||
|
DatabaseName::Temp,
|
||||||
|
&mut dst,
|
||||||
|
DatabaseName::Main)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
backup.run_to_completion(5, Duration::from_millis(250), None).unwrap();
|
backup.run_to_completion(5, Duration::from_millis(250), None).unwrap();
|
||||||
}
|
}
|
||||||
@ -269,6 +286,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
fn test_backup_attached() {
|
fn test_backup_attached() {
|
||||||
let src = SqliteConnection::open_in_memory().unwrap();
|
let src = SqliteConnection::open_in_memory().unwrap();
|
||||||
let sql = "ATTACH DATABASE ':memory:' AS my_attached;
|
let sql = "ATTACH DATABASE ':memory:' AS my_attached;
|
||||||
@ -281,8 +299,11 @@ mod test {
|
|||||||
let mut dst = SqliteConnection::open_in_memory().unwrap();
|
let mut dst = SqliteConnection::open_in_memory().unwrap();
|
||||||
|
|
||||||
{
|
{
|
||||||
let backup = Backup::new_with_names(&src, DatabaseName::Attached("my_attached"),
|
let backup = Backup::new_with_names(&src,
|
||||||
&mut dst, DatabaseName::Main).unwrap();
|
DatabaseName::Attached("my_attached"),
|
||||||
|
&mut dst,
|
||||||
|
DatabaseName::Main)
|
||||||
|
.unwrap();
|
||||||
backup.step(-1).unwrap();
|
backup.step(-1).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,8 +313,11 @@ mod test {
|
|||||||
src.execute_batch("INSERT INTO foo VALUES(43)").unwrap();
|
src.execute_batch("INSERT INTO foo VALUES(43)").unwrap();
|
||||||
|
|
||||||
{
|
{
|
||||||
let backup = Backup::new_with_names(&src, DatabaseName::Attached("my_attached"),
|
let backup = Backup::new_with_names(&src,
|
||||||
&mut dst, DatabaseName::Main).unwrap();
|
DatabaseName::Attached("my_attached"),
|
||||||
|
&mut dst,
|
||||||
|
DatabaseName::Main)
|
||||||
|
.unwrap();
|
||||||
backup.run_to_completion(5, Duration::from_millis(250), None).unwrap();
|
backup.run_to_completion(5, Duration::from_millis(250), None).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user