From 8f079819f76d4510ad8896fff682f50186780ffb Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 13 Aug 2016 13:55:30 +0200 Subject: [PATCH] Change VTabCursor::filter signature --- src/blob.rs | 6 +----- src/functions.rs | 10 +++------- src/lib.rs | 33 ++++++++++++++++++++------------- src/named_params.rs | 4 +--- src/types/serde_json.rs | 9 +++++---- src/vtab/csvtab.rs | 11 ++++------- src/vtab/int_array.rs | 6 +++--- src/vtab/mod.rs | 22 ++++++++++++++-------- 8 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/blob.rs b/src/blob.rs index 548e5c9..fd20262 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -90,11 +90,7 @@ impl Connection { table.as_ptr(), column.as_ptr(), row, - if read_only { - 0 - } else { - 1 - }, + if read_only { 0 } else { 1 }, &mut blob) }; c.decode_result(rc).map(|_| { diff --git a/src/functions.rs b/src/functions.rs index 1f02a90..47bfe3e 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -202,7 +202,7 @@ impl<'a> ValueRef<'a> { ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) } - _ => unreachable!("sqlite3_value_type returned invalid value") + _ => unreachable!("sqlite3_value_type returned invalid value"), } } } @@ -239,7 +239,7 @@ impl<'a> Context<'a> { let value = unsafe { ValueRef::from_value(arg) }; FromSql::column_result(value).map_err(|err| match err { Error::InvalidColumnType => Error::InvalidFunctionParameterType, - _ => err + _ => err, }) } @@ -266,11 +266,7 @@ impl<'a> Context<'a> { /// types must be identical. pub unsafe fn get_aux(&self, arg: c_int) -> Option<&T> { let p = ffi::sqlite3_get_auxdata(self.ctx, arg) as *mut T; - if p.is_null() { - None - } else { - Some(&*p) - } + if p.is_null() { None } else { Some(&*p) } } } diff --git a/src/lib.rs b/src/lib.rs index 50100f7..9d9519e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,12 +93,18 @@ mod named_params; mod error; mod convenient; mod raw_statement; -#[cfg(feature = "load_extension")]mod load_extension_guard; -#[cfg(feature = "trace")]pub mod trace; -#[cfg(feature = "backup")]pub mod backup; -#[cfg(feature = "functions")]pub mod functions; -#[cfg(feature = "blob")]pub mod blob; -#[cfg(all(feature = "vtab", feature = "functions"))]pub mod vtab; +#[cfg(feature = "load_extension")] +mod load_extension_guard; +#[cfg(feature = "trace")] +pub mod trace; +#[cfg(feature = "backup")] +pub mod backup; +#[cfg(feature = "functions")] +pub mod functions; +#[cfg(feature = "blob")] +pub mod blob; +#[cfg(all(feature = "vtab", feature = "functions"))] +pub mod vtab; // Number of cached prepared statements we'll hold on to. const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16; @@ -886,9 +892,7 @@ impl<'conn> Statement<'conn> { for (i, p) in params.iter().enumerate() { try!(unsafe { - self.conn.decode_result( - p.bind_parameter(self.stmt.ptr(), (i + 1) as c_int) - ) + self.conn.decode_result(p.bind_parameter(self.stmt.ptr(), (i + 1) as c_int)) }); } @@ -1115,7 +1119,8 @@ impl<'a> ValueRef<'a> { ffi::SQLITE_FLOAT => ValueRef::Real(ffi::sqlite3_column_double(raw, col)), ffi::SQLITE_TEXT => { let text = ffi::sqlite3_column_text(raw, col); - assert!(!text.is_null(), "unexpected SQLITE_TEXT column type with NULL data"); + assert!(!text.is_null(), + "unexpected SQLITE_TEXT column type with NULL data"); let s = CStr::from_ptr(text as *const c_char); // sqlite3_column_text returns UTF8 data, so our unwrap here should be fine. @@ -1126,9 +1131,11 @@ impl<'a> ValueRef<'a> { let blob = ffi::sqlite3_column_blob(raw, col); let len = ffi::sqlite3_column_bytes(raw, col); - assert!(len >= 0, "unexpected negative return from sqlite3_column_bytes"); + assert!(len >= 0, + "unexpected negative return from sqlite3_column_bytes"); if len > 0 { - assert!(!blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data"); + assert!(!blob.is_null(), + "unexpected SQLITE_BLOB column type with NULL data"); ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) } else { // The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. @@ -1136,7 +1143,7 @@ impl<'a> ValueRef<'a> { } } - _ => unreachable!("sqlite3_column_type returned invalid value") + _ => unreachable!("sqlite3_column_type returned invalid value"), } } } diff --git a/src/named_params.rs b/src/named_params.rs index 5486074..d89cc6e 100644 --- a/src/named_params.rs +++ b/src/named_params.rs @@ -204,9 +204,7 @@ impl<'conn> Statement<'conn> { fn bind_parameters_named(&mut self, params: &[(&str, &ToSql)]) -> Result<()> { for &(name, value) in params { if let Some(i) = try!(self.parameter_index(name)) { - try!(self.conn.decode_result(unsafe { - value.bind_parameter(self.stmt.ptr(), i) - })); + try!(self.conn.decode_result(unsafe { value.bind_parameter(self.stmt.ptr(), i) })); } else { return Err(Error::InvalidParameterName(name.into())); } diff --git a/src/types/serde_json.rs b/src/types/serde_json.rs index 937f914..6fbe66e 100644 --- a/src/types/serde_json.rs +++ b/src/types/serde_json.rs @@ -21,10 +21,11 @@ impl ToSql for Value { impl FromSql for Value { fn column_result(value: ValueRef) -> Result { match value { - ValueRef::Text(ref s) => serde_json::from_str(s), - ValueRef::Blob(ref b) => serde_json::from_slice(b), - _ => return Err(Error::InvalidColumnType), - }.map_err(|err| Error::FromSqlConversionFailure(Box::new(err))) + ValueRef::Text(ref s) => serde_json::from_str(s), + ValueRef::Blob(ref b) => serde_json::from_slice(b), + _ => return Err(Error::InvalidColumnType), + } + .map_err(|err| Error::FromSqlConversionFailure(Box::new(err))) } } diff --git a/src/vtab/csvtab.rs b/src/vtab/csvtab.rs index b80bd3d..ebb6a7f 100644 --- a/src/vtab/csvtab.rs +++ b/src/vtab/csvtab.rs @@ -56,10 +56,7 @@ impl CSVTab { } impl VTab for CSVTab { - fn create(db: *mut ffi::sqlite3, - _aux: *mut libc::c_void, - args: &[&[u8]]) - -> Result { + fn create(db: *mut ffi::sqlite3, _aux: *mut libc::c_void, args: &[&[u8]]) -> Result { if args.len() < 4 { return Err(Error::ModuleError("no CSV file specified".to_owned())); } @@ -173,8 +170,8 @@ impl VTabCursor for CSVTabCursor { fn filter(&mut self, _idx_num: libc::c_int, - _idx_str: *const libc::c_char, - _args: &mut[*mut ffi::sqlite3_value]) + _idx_str: Option<&str>, + _args: &mut [*mut ffi::sqlite3_value]) -> Result<()> { { let offset_first_row = self.vtab().offset_first_row; @@ -196,7 +193,7 @@ impl VTabCursor for CSVTabCursor { } } - self.row_number = self.row_number + 1; + self.row_number += 1; Ok(()) } fn eof(&self) -> bool { diff --git a/src/vtab/int_array.rs b/src/vtab/int_array.rs index 2052463..4f043c8 100644 --- a/src/vtab/int_array.rs +++ b/src/vtab/int_array.rs @@ -102,14 +102,14 @@ impl VTabCursor for IntArrayVTabCursor { } fn filter(&mut self, _idx_num: libc::c_int, - _idx_str: *const libc::c_char, - _args: &mut[*mut ffi::sqlite3_value]) + _idx_str: Option<&str>, + _args: &mut [*mut ffi::sqlite3_value]) -> Result<()> { self.i = 0; Ok(()) } fn next(&mut self) -> Result<()> { - self.i = self.i + 1; + self.i += 1; Ok(()) } fn eof(&self) -> bool { diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 46b7e8a..c657651 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -42,10 +42,7 @@ use ffi; pub trait VTab>: Sized { /// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement. /// The `db` parameter is a pointer to the SQLite database connection that is executing the CREATE VIRTUAL TABLE statement. - fn create(db: *mut ffi::sqlite3, - aux: *mut libc::c_void, - args: &[&[u8]]) - -> Result; + fn create(db: *mut ffi::sqlite3, aux: *mut libc::c_void, args: &[&[u8]]) -> Result; /// Determine the best way to access the virtual table. fn best_index(&self, info: *mut ffi::sqlite3_index_info); /// Create a new cursor used for accessing a virtual table. @@ -59,8 +56,8 @@ pub trait VTabCursor>: Sized { /// Begin a search of a virtual table. fn filter(&mut self, idx_num: libc::c_int, - idx_str: *const libc::c_char, - args: &mut[*mut ffi::sqlite3_value]) + idx_str: Option<&str>, + args: &mut [*mut ffi::sqlite3_value]) -> Result<()>; /// Advance cursor to the next row of a result set initiated by `filter`. fn next(&mut self) -> Result<()>; @@ -258,11 +255,19 @@ unsafe extern "C" fn $filter(cursor: *mut ffi::sqlite3_vtab_cursor, argc: libc::c_int, argv: *mut *mut ffi::sqlite3_value) -> libc::c_int { + use std::ffi::CStr; use std::slice; + use std::str; use vtab::cursor_error; + let idx_name = if idx_str.is_null() { + None + } else { + let c_slice = CStr::from_ptr(idx_str).to_bytes(); + Some(str::from_utf8_unchecked(c_slice)) + }; let mut args = slice::from_raw_parts_mut(argv, argc as usize); let cr = cursor as *mut $cursor; - cursor_error(cursor, (*cr).filter(idx_num, idx_str, &mut args)) + cursor_error(cursor, (*cr).filter(idx_num, idx_name, &mut args)) } unsafe extern "C" fn $next(cursor: *mut ffi::sqlite3_vtab_cursor) -> libc::c_int { use vtab::cursor_error; @@ -365,4 +370,5 @@ pub fn mprintf(err_msg: &str) -> *mut ::libc::c_char { } pub mod int_array; -#[cfg(feature = "csvtab")]pub mod csvtab; +#[cfg(feature = "csvtab")] +pub mod csvtab;