Merge pull request #986 from gwenn/remove_check_macro

[Cosmetic] Remove check macro
This commit is contained in:
gwenn 2021-07-27 18:59:53 +02:00 committed by GitHub
commit 6450565cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 69 deletions

View File

@ -2,6 +2,7 @@
use std::os::raw::c_int; use std::os::raw::c_int;
use crate::error::check;
use crate::ffi; use crate::ffi;
use crate::{Connection, Result}; use crate::{Connection, Result};
@ -80,12 +81,12 @@ impl Connection {
let c = self.db.borrow(); let c = self.db.borrow();
unsafe { unsafe {
let mut val = 0; let mut val = 0;
check!(ffi::sqlite3_db_config( check(ffi::sqlite3_db_config(
c.db(), c.db(),
config as c_int, config as c_int,
-1, -1,
&mut val &mut val,
)); ))?;
Ok(val != 0) Ok(val != 0)
} }
} }
@ -109,12 +110,12 @@ impl Connection {
let c = self.db.borrow_mut(); let c = self.db.borrow_mut();
unsafe { unsafe {
let mut val = 0; let mut val = 0;
check!(ffi::sqlite3_db_config( check(ffi::sqlite3_db_config(
c.db(), c.db(),
config as c_int, config as c_int,
if new_val { 1 } else { 0 }, if new_val { 1 } else { 0 },
&mut val &mut val,
)); ))?;
Ok(val != 0) Ok(val != 0)
} }
} }

View File

@ -1,6 +1,6 @@
use crate::types::FromSqlError; use crate::types::FromSqlError;
use crate::types::Type; use crate::types::Type;
use crate::{errmsg_to_string, ffi}; use crate::{errmsg_to_string, ffi, Result};
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::os::raw::c_int; use std::os::raw::c_int;
@ -357,11 +357,10 @@ pub unsafe fn error_from_handle(db: *mut ffi::sqlite3, code: c_int) -> Error {
error_from_sqlite_code(code, message) error_from_sqlite_code(code, message)
} }
macro_rules! check { pub fn check(code: c_int) -> Result<()> {
($funcall:expr) => {{ if code != crate::ffi::SQLITE_OK {
let rc = $funcall; Err(crate::error::error_from_sqlite_code(code, None))
if rc != crate::ffi::SQLITE_OK { } else {
return Err(crate::error::error_from_sqlite_code(rc, None).into()); Ok(())
} }
}};
} }

View File

@ -304,8 +304,7 @@ impl InnerConnection {
#[cfg(feature = "modern_sqlite")] // 3.10.0 #[cfg(feature = "modern_sqlite")] // 3.10.0
pub fn cache_flush(&mut self) -> Result<()> { pub fn cache_flush(&mut self) -> Result<()> {
check!(unsafe { ffi::sqlite3_db_cacheflush(self.db()) }); crate::error::check(unsafe { ffi::sqlite3_db_cacheflush(self.db()) })
Ok(())
} }
#[cfg(not(feature = "hooks"))] #[cfg(not(feature = "hooks"))]

View File

@ -83,7 +83,6 @@ pub use crate::transaction::{DropBehavior, Savepoint, Transaction, TransactionBe
pub use crate::types::ToSql; pub use crate::types::ToSql;
pub use crate::version::*; pub use crate::version::*;
#[macro_use]
mod error; mod error;
#[cfg(feature = "backup")] #[cfg(feature = "backup")]

View File

@ -11,7 +11,7 @@ use std::slice::{from_raw_parts, from_raw_parts_mut};
use fallible_streaming_iterator::FallibleStreamingIterator; use fallible_streaming_iterator::FallibleStreamingIterator;
use crate::error::error_from_sqlite_code; use crate::error::{check, error_from_sqlite_code};
use crate::ffi; use crate::ffi;
use crate::hooks::Action; use crate::hooks::Action;
use crate::types::ValueRef; use crate::types::ValueRef;
@ -45,7 +45,7 @@ impl Session<'_> {
let db = db.db.borrow_mut().db; let db = db.db.borrow_mut().db;
let mut s: *mut ffi::sqlite3_session = ptr::null_mut(); let mut s: *mut ffi::sqlite3_session = ptr::null_mut();
check!(unsafe { ffi::sqlite3session_create(db, name.as_ptr(), &mut s) }); check(unsafe { ffi::sqlite3session_create(db, name.as_ptr(), &mut s) })?;
Ok(Session { Ok(Session {
phantom: PhantomData, phantom: PhantomData,
@ -109,15 +109,14 @@ impl Session<'_> {
None None
}; };
let table = table.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null()); let table = table.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
unsafe { check!(ffi::sqlite3session_attach(self.s, table)) }; check(unsafe { ffi::sqlite3session_attach(self.s, table) })
Ok(())
} }
/// Generate a Changeset /// Generate a Changeset
pub fn changeset(&mut self) -> Result<Changeset> { pub fn changeset(&mut self) -> Result<Changeset> {
let mut n = 0; let mut n = 0;
let mut cs: *mut c_void = ptr::null_mut(); let mut cs: *mut c_void = ptr::null_mut();
check!(unsafe { ffi::sqlite3session_changeset(self.s, &mut n, &mut cs) }); check(unsafe { ffi::sqlite3session_changeset(self.s, &mut n, &mut cs) })?;
Ok(Changeset { cs, n }) Ok(Changeset { cs, n })
} }
@ -125,14 +124,13 @@ impl Session<'_> {
#[inline] #[inline]
pub fn changeset_strm(&mut self, output: &mut dyn Write) -> Result<()> { pub fn changeset_strm(&mut self, output: &mut dyn Write) -> Result<()> {
let output_ref = &output; let output_ref = &output;
check!(unsafe { check(unsafe {
ffi::sqlite3session_changeset_strm( ffi::sqlite3session_changeset_strm(
self.s, self.s,
Some(x_output), Some(x_output),
output_ref as *const &mut dyn Write as *mut c_void, output_ref as *const &mut dyn Write as *mut c_void,
) )
}); })
Ok(())
} }
/// Generate a Patchset /// Generate a Patchset
@ -140,7 +138,7 @@ impl Session<'_> {
pub fn patchset(&mut self) -> Result<Changeset> { pub fn patchset(&mut self) -> Result<Changeset> {
let mut n = 0; let mut n = 0;
let mut ps: *mut c_void = ptr::null_mut(); let mut ps: *mut c_void = ptr::null_mut();
check!(unsafe { ffi::sqlite3session_patchset(self.s, &mut n, &mut ps) }); check(unsafe { ffi::sqlite3session_patchset(self.s, &mut n, &mut ps) })?;
// TODO Validate: same struct // TODO Validate: same struct
Ok(Changeset { cs: ps, n }) Ok(Changeset { cs: ps, n })
} }
@ -149,14 +147,13 @@ impl Session<'_> {
#[inline] #[inline]
pub fn patchset_strm(&mut self, output: &mut dyn Write) -> Result<()> { pub fn patchset_strm(&mut self, output: &mut dyn Write) -> Result<()> {
let output_ref = &output; let output_ref = &output;
check!(unsafe { check(unsafe {
ffi::sqlite3session_patchset_strm( ffi::sqlite3session_patchset_strm(
self.s, self.s,
Some(x_output), Some(x_output),
output_ref as *const &mut dyn Write as *mut c_void, output_ref as *const &mut dyn Write as *mut c_void,
) )
}); })
Ok(())
} }
/// Load the difference between tables. /// Load the difference between tables.
@ -228,15 +225,14 @@ impl Drop for Session<'_> {
pub fn invert_strm(input: &mut dyn Read, output: &mut dyn Write) -> Result<()> { pub fn invert_strm(input: &mut dyn Read, output: &mut dyn Write) -> Result<()> {
let input_ref = &input; let input_ref = &input;
let output_ref = &output; let output_ref = &output;
check!(unsafe { check(unsafe {
ffi::sqlite3changeset_invert_strm( ffi::sqlite3changeset_invert_strm(
Some(x_input), Some(x_input),
input_ref as *const &mut dyn Read as *mut c_void, input_ref as *const &mut dyn Read as *mut c_void,
Some(x_output), Some(x_output),
output_ref as *const &mut dyn Write as *mut c_void, output_ref as *const &mut dyn Write as *mut c_void,
) )
}); })
Ok(())
} }
/// Combine two changesets /// Combine two changesets
@ -249,7 +245,7 @@ pub fn concat_strm(
let input_a_ref = &input_a; let input_a_ref = &input_a;
let input_b_ref = &input_b; let input_b_ref = &input_b;
let output_ref = &output; let output_ref = &output;
check!(unsafe { check(unsafe {
ffi::sqlite3changeset_concat_strm( ffi::sqlite3changeset_concat_strm(
Some(x_input), Some(x_input),
input_a_ref as *const &mut dyn Read as *mut c_void, input_a_ref as *const &mut dyn Read as *mut c_void,
@ -258,8 +254,7 @@ pub fn concat_strm(
Some(x_output), Some(x_output),
output_ref as *const &mut dyn Write as *mut c_void, output_ref as *const &mut dyn Write as *mut c_void,
) )
}); })
Ok(())
} }
/// Changeset or Patchset /// Changeset or Patchset
@ -274,9 +269,9 @@ impl Changeset {
pub fn invert(&self) -> Result<Changeset> { pub fn invert(&self) -> Result<Changeset> {
let mut n = 0; let mut n = 0;
let mut cs = ptr::null_mut(); let mut cs = ptr::null_mut();
check!(unsafe { check(unsafe {
ffi::sqlite3changeset_invert(self.n, self.cs, &mut n, &mut cs as *mut *mut _) ffi::sqlite3changeset_invert(self.n, self.cs, &mut n, &mut cs as *mut *mut _)
}); })?;
Ok(Changeset { cs, n }) Ok(Changeset { cs, n })
} }
@ -284,7 +279,7 @@ impl Changeset {
#[inline] #[inline]
pub fn iter(&self) -> Result<ChangesetIter<'_>> { pub fn iter(&self) -> Result<ChangesetIter<'_>> {
let mut it = ptr::null_mut(); let mut it = ptr::null_mut();
check!(unsafe { ffi::sqlite3changeset_start(&mut it as *mut *mut _, self.n, self.cs) }); check(unsafe { ffi::sqlite3changeset_start(&mut it as *mut *mut _, self.n, self.cs) })?;
Ok(ChangesetIter { Ok(ChangesetIter {
phantom: PhantomData, phantom: PhantomData,
it, it,
@ -297,9 +292,9 @@ impl Changeset {
pub fn concat(a: &Changeset, b: &Changeset) -> Result<Changeset> { pub fn concat(a: &Changeset, b: &Changeset) -> Result<Changeset> {
let mut n = 0; let mut n = 0;
let mut cs = ptr::null_mut(); let mut cs = ptr::null_mut();
check!(unsafe { check(unsafe {
ffi::sqlite3changeset_concat(a.n, a.cs, b.n, b.cs, &mut n, &mut cs as *mut *mut _) ffi::sqlite3changeset_concat(a.n, a.cs, b.n, b.cs, &mut n, &mut cs as *mut *mut _)
}); })?;
Ok(Changeset { cs, n }) Ok(Changeset { cs, n })
} }
} }
@ -326,13 +321,13 @@ impl ChangesetIter<'_> {
#[inline] #[inline]
pub fn start_strm<'input>(input: &&'input mut dyn Read) -> Result<ChangesetIter<'input>> { pub fn start_strm<'input>(input: &&'input mut dyn Read) -> Result<ChangesetIter<'input>> {
let mut it = ptr::null_mut(); let mut it = ptr::null_mut();
check!(unsafe { check(unsafe {
ffi::sqlite3changeset_start_strm( ffi::sqlite3changeset_start_strm(
&mut it as *mut *mut _, &mut it as *mut *mut _,
Some(x_input), Some(x_input),
input as *const &mut dyn Read as *mut c_void, input as *const &mut dyn Read as *mut c_void,
) )
}); })?;
Ok(ChangesetIter { Ok(ChangesetIter {
phantom: PhantomData, phantom: PhantomData,
it, it,
@ -427,11 +422,11 @@ impl ChangesetItem {
pub fn conflict(&self, col: usize) -> Result<ValueRef<'_>> { pub fn conflict(&self, col: usize) -> Result<ValueRef<'_>> {
unsafe { unsafe {
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
check!(ffi::sqlite3changeset_conflict( check(ffi::sqlite3changeset_conflict(
self.it, self.it,
col as i32, col as i32,
&mut p_value, &mut p_value,
)); ))?;
Ok(ValueRef::from_value(p_value)) Ok(ValueRef::from_value(p_value))
} }
} }
@ -444,7 +439,7 @@ impl ChangesetItem {
pub fn fk_conflicts(&self) -> Result<i32> { pub fn fk_conflicts(&self) -> Result<i32> {
unsafe { unsafe {
let mut p_out = 0; let mut p_out = 0;
check!(ffi::sqlite3changeset_fk_conflicts(self.it, &mut p_out)); check(ffi::sqlite3changeset_fk_conflicts(self.it, &mut p_out))?;
Ok(p_out) Ok(p_out)
} }
} }
@ -457,7 +452,7 @@ impl ChangesetItem {
pub fn new_value(&self, col: usize) -> Result<ValueRef<'_>> { pub fn new_value(&self, col: usize) -> Result<ValueRef<'_>> {
unsafe { unsafe {
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
check!(ffi::sqlite3changeset_new(self.it, col as i32, &mut p_value,)); check(ffi::sqlite3changeset_new(self.it, col as i32, &mut p_value))?;
Ok(ValueRef::from_value(p_value)) Ok(ValueRef::from_value(p_value))
} }
} }
@ -470,7 +465,7 @@ impl ChangesetItem {
pub fn old_value(&self, col: usize) -> Result<ValueRef<'_>> { pub fn old_value(&self, col: usize) -> Result<ValueRef<'_>> {
unsafe { unsafe {
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut(); let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
check!(ffi::sqlite3changeset_old(self.it, col as i32, &mut p_value,)); check(ffi::sqlite3changeset_old(self.it, col as i32, &mut p_value))?;
Ok(ValueRef::from_value(p_value)) Ok(ValueRef::from_value(p_value))
} }
} }
@ -483,13 +478,13 @@ impl ChangesetItem {
let mut indirect = 0; let mut indirect = 0;
let tab = unsafe { let tab = unsafe {
let mut pz_tab: *const c_char = ptr::null(); let mut pz_tab: *const c_char = ptr::null();
check!(ffi::sqlite3changeset_op( check(ffi::sqlite3changeset_op(
self.it, self.it,
&mut pz_tab, &mut pz_tab,
&mut number_of_columns, &mut number_of_columns,
&mut code, &mut code,
&mut indirect &mut indirect,
)); ))?;
CStr::from_ptr(pz_tab) CStr::from_ptr(pz_tab)
}; };
let table_name = tab.to_str()?; let table_name = tab.to_str()?;
@ -507,11 +502,11 @@ impl ChangesetItem {
let mut number_of_columns = 0; let mut number_of_columns = 0;
unsafe { unsafe {
let mut pks: *mut c_uchar = ptr::null_mut(); let mut pks: *mut c_uchar = ptr::null_mut();
check!(ffi::sqlite3changeset_pk( check(ffi::sqlite3changeset_pk(
self.it, self.it,
&mut pks, &mut pks,
&mut number_of_columns &mut number_of_columns,
)); ))?;
Ok(from_raw_parts(pks, number_of_columns as usize)) Ok(from_raw_parts(pks, number_of_columns as usize))
} }
} }
@ -528,29 +523,27 @@ impl Changegroup {
#[inline] #[inline]
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
let mut cg = ptr::null_mut(); let mut cg = ptr::null_mut();
check!(unsafe { ffi::sqlite3changegroup_new(&mut cg) }); check(unsafe { ffi::sqlite3changegroup_new(&mut cg) })?;
Ok(Changegroup { cg }) Ok(Changegroup { cg })
} }
/// Add a changeset /// Add a changeset
#[inline] #[inline]
pub fn add(&mut self, cs: &Changeset) -> Result<()> { pub fn add(&mut self, cs: &Changeset) -> Result<()> {
check!(unsafe { ffi::sqlite3changegroup_add(self.cg, cs.n, cs.cs) }); check(unsafe { ffi::sqlite3changegroup_add(self.cg, cs.n, cs.cs) })
Ok(())
} }
/// Add a changeset read from `input` to this change group. /// Add a changeset read from `input` to this change group.
#[inline] #[inline]
pub fn add_stream(&mut self, input: &mut dyn Read) -> Result<()> { pub fn add_stream(&mut self, input: &mut dyn Read) -> Result<()> {
let input_ref = &input; let input_ref = &input;
check!(unsafe { check(unsafe {
ffi::sqlite3changegroup_add_strm( ffi::sqlite3changegroup_add_strm(
self.cg, self.cg,
Some(x_input), Some(x_input),
input_ref as *const &mut dyn Read as *mut c_void, input_ref as *const &mut dyn Read as *mut c_void,
) )
}); })
Ok(())
} }
/// Obtain a composite Changeset /// Obtain a composite Changeset
@ -558,7 +551,7 @@ impl Changegroup {
pub fn output(&mut self) -> Result<Changeset> { pub fn output(&mut self) -> Result<Changeset> {
let mut n = 0; let mut n = 0;
let mut output: *mut c_void = ptr::null_mut(); let mut output: *mut c_void = ptr::null_mut();
check!(unsafe { ffi::sqlite3changegroup_output(self.cg, &mut n, &mut output) }); check(unsafe { ffi::sqlite3changegroup_output(self.cg, &mut n, &mut output) })?;
Ok(Changeset { cs: output, n }) Ok(Changeset { cs: output, n })
} }
@ -566,14 +559,13 @@ impl Changegroup {
#[inline] #[inline]
pub fn output_strm(&mut self, output: &mut dyn Write) -> Result<()> { pub fn output_strm(&mut self, output: &mut dyn Write) -> Result<()> {
let output_ref = &output; let output_ref = &output;
check!(unsafe { check(unsafe {
ffi::sqlite3changegroup_output_strm( ffi::sqlite3changegroup_output_strm(
self.cg, self.cg,
Some(x_output), Some(x_output),
output_ref as *const &mut dyn Write as *mut c_void, output_ref as *const &mut dyn Write as *mut c_void,
) )
}); })
Ok(())
} }
} }
@ -597,7 +589,7 @@ impl Connection {
let filtered = filter.is_some(); let filtered = filter.is_some();
let tuple = &mut (filter, conflict); let tuple = &mut (filter, conflict);
check!(unsafe { check(unsafe {
if filtered { if filtered {
ffi::sqlite3changeset_apply( ffi::sqlite3changeset_apply(
db, db,
@ -617,8 +609,7 @@ impl Connection {
tuple as *mut (Option<F>, C) as *mut c_void, tuple as *mut (Option<F>, C) as *mut c_void,
) )
} }
}); })
Ok(())
} }
/// Apply a changeset to a database /// Apply a changeset to a database
@ -637,7 +628,7 @@ impl Connection {
let filtered = filter.is_some(); let filtered = filter.is_some();
let tuple = &mut (filter, conflict); let tuple = &mut (filter, conflict);
check!(unsafe { check(unsafe {
if filtered { if filtered {
ffi::sqlite3changeset_apply_strm( ffi::sqlite3changeset_apply_strm(
db, db,
@ -657,8 +648,7 @@ impl Connection {
tuple as *mut (Option<F>, C) as *mut c_void, tuple as *mut (Option<F>, C) as *mut c_void,
) )
} }
}); })
Ok(())
} }
} }