From c6f4ae632a84eb1d8197cfbd418b27643a73c0fa Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 23 May 2018 21:04:13 +0200 Subject: [PATCH] Replace column index/count type (i32) with usize Breaking change --- src/error.rs | 8 ++++---- src/functions.rs | 2 +- src/lib.rs | 2 +- src/raw_statement.rs | 20 ++++++++++---------- src/row.rs | 12 ++++++------ src/statement.rs | 38 +++++++++++++++++++------------------- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/error.rs b/src/error.rs index 7bfe057..09e2e1d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -26,9 +26,9 @@ pub enum Error { FromSqlConversionFailure(usize, Type, Box), /// Error when SQLite gives us an integral value outside the range of the requested type (e.g., - /// trying to get the value 1000 into a `u8`). The associated `c_int` is the column index, and + /// trying to get the value 1000 into a `u8`). The associated `usize` is the column index, and /// the associated `i64` is the value returned by SQLite. - IntegralValueOutOfRange(c_int, i64), + IntegralValueOutOfRange(usize, i64), /// Error converting a string to UTF-8. Utf8Error(str::Utf8Error), @@ -51,7 +51,7 @@ pub enum Error { /// Error when the value of a particular column is requested, but the index is out of range /// for the statement. - InvalidColumnIndex(c_int), + InvalidColumnIndex(usize), /// Error when the value of a named column is requested, but no column matches the name /// for the statement. @@ -59,7 +59,7 @@ pub enum Error { /// Error when the value of a particular column is requested, but the type of the result in /// that column cannot be converted to the requested Rust type. - InvalidColumnType(c_int, Type), + InvalidColumnType(usize, Type), /// Error when a query that was expected to insert one row did not insert any or insert many. StatementChangedRows(c_int), diff --git a/src/functions.rs b/src/functions.rs index a79b3e6..91aecf2 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -216,7 +216,7 @@ impl<'a> Context<'a> { Error::InvalidFunctionParameterType(idx, value.data_type()) } FromSqlError::OutOfRange(i) => { - Error::IntegralValueOutOfRange(idx as c_int, + Error::IntegralValueOutOfRange(idx, i) } FromSqlError::Other(err) => { diff --git a/src/lib.rs b/src/lib.rs index 21cfa15..ee8cd63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -931,7 +931,7 @@ impl InnerConnection { stmt = ffi::sqlite3_next_stmt(db, stmt); } } - return false; + false } #[cfg(not(feature = "hooks"))] diff --git a/src/raw_statement.rs b/src/raw_statement.rs index 3c33ea3..5b99b8c 100644 --- a/src/raw_statement.rs +++ b/src/raw_statement.rs @@ -17,16 +17,16 @@ impl RawStatement { self.0 } - pub fn column_count(&self) -> c_int { - unsafe { ffi::sqlite3_column_count(self.0) } + pub fn column_count(&self) -> usize { + unsafe { ffi::sqlite3_column_count(self.0) as usize } } - pub fn column_type(&self, idx: c_int) -> c_int { - unsafe { ffi::sqlite3_column_type(self.0, idx) } + pub fn column_type(&self, idx: usize) -> c_int { + unsafe { ffi::sqlite3_column_type(self.0, idx as c_int) } } - pub fn column_name(&self, idx: c_int) -> &CStr { - unsafe { CStr::from_ptr(ffi::sqlite3_column_name(self.0, idx)) } + pub fn column_name(&self, idx: usize) -> &CStr { + unsafe { CStr::from_ptr(ffi::sqlite3_column_name(self.0, idx as c_int)) } } pub fn step(&self) -> c_int { @@ -54,15 +54,15 @@ impl RawStatement { unsafe { ffi::sqlite3_reset(self.0) } } - pub fn bind_parameter_count(&self) -> c_int { - unsafe { ffi::sqlite3_bind_parameter_count(self.0) } + pub fn bind_parameter_count(&self) -> usize { + unsafe { ffi::sqlite3_bind_parameter_count(self.0) as usize } } - pub fn bind_parameter_index(&self, name: &CStr) -> Option { + pub fn bind_parameter_index(&self, name: &CStr) -> Option { let r = unsafe { ffi::sqlite3_bind_parameter_index(self.0, name.as_ptr()) }; match r { 0 => None, - i => Some(i), + i => Some(i as usize), } } diff --git a/src/row.rs b/src/row.rs index 2042616..775e69a 100644 --- a/src/row.rs +++ b/src/row.rs @@ -192,7 +192,7 @@ impl<'a, 'stmt> Row<'a, 'stmt> { } /// Return the number of columns in the current row. - pub fn column_count(&self) -> i32 { + pub fn column_count(&self) -> usize { self.stmt.column_count() } } @@ -201,13 +201,13 @@ impl<'a, 'stmt> Row<'a, 'stmt> { pub trait RowIndex { /// Returns the index of the appropriate column, or `None` if no such /// column exists. - fn idx(&self, stmt: &Statement) -> Result; + fn idx(&self, stmt: &Statement) -> Result; } -impl RowIndex for i32 { +impl RowIndex for usize { #[inline] - fn idx(&self, stmt: &Statement) -> Result { - if *self < 0 || *self >= stmt.column_count() { + fn idx(&self, stmt: &Statement) -> Result { + if *self >= stmt.column_count() { Err(Error::InvalidColumnIndex(*self)) } else { Ok(*self) @@ -217,7 +217,7 @@ impl RowIndex for i32 { impl<'a> RowIndex for &'a str { #[inline] - fn idx(&self, stmt: &Statement) -> Result { + fn idx(&self, stmt: &Statement) -> Result { stmt.column_index(*self) } } diff --git a/src/statement.rs b/src/statement.rs index c867ad4..4f194a6 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -29,7 +29,7 @@ impl<'conn> Statement<'conn> { } /// Return the number of columns in the result set returned by the prepared statement. - pub fn column_count(&self) -> i32 { + pub fn column_count(&self) -> usize { self.stmt.column_count() } @@ -41,7 +41,7 @@ impl<'conn> Statement<'conn> { /// # Failure /// /// Will return an `Error::InvalidColumnName` when there is no column with the specified `name`. - pub fn column_index(&self, name: &str) -> Result { + pub fn column_index(&self, name: &str) -> Result { let bytes = name.as_bytes(); let n = self.column_count(); for i in 0..n { @@ -362,19 +362,19 @@ impl<'conn> Statement<'conn> { /// /// Will return Err if `name` is invalid. Will return Ok(None) if the name /// is valid but not a bound parameter of this statement. - pub fn parameter_index(&self, name: &str) -> Result> { + pub fn parameter_index(&self, name: &str) -> Result> { let c_name = try!(str_to_cstring(name)); Ok(self.stmt.bind_parameter_index(&c_name)) } fn bind_parameters(&mut self, params: &[&ToSql]) -> Result<()> { - assert_eq!(params.len() as c_int, self.stmt.bind_parameter_count(), + assert_eq!(params.len(), self.stmt.bind_parameter_count(), "incorrect number of parameters to query(): expected {}, got {}", self.stmt.bind_parameter_count(), params.len()); for (i, p) in params.iter().enumerate() { - try!(self.bind_parameter(*p, (i + 1) as c_int)); + try!(self.bind_parameter(*p, i + 1)); } Ok(()) @@ -391,7 +391,7 @@ impl<'conn> Statement<'conn> { Ok(()) } - fn bind_parameter(&self, param: &ToSql, col: c_int) -> Result<()> { + fn bind_parameter(&self, param: &ToSql, col: usize) -> Result<()> { let value = try!(param.to_sql()); let ptr = unsafe { self.stmt.ptr() }; @@ -402,17 +402,17 @@ impl<'conn> Statement<'conn> { #[cfg(feature = "blob")] ToSqlOutput::ZeroBlob(len) => { return self.conn - .decode_result(unsafe { ffi::sqlite3_bind_zeroblob(ptr, col, len) }); + .decode_result(unsafe { ffi::sqlite3_bind_zeroblob(ptr, col as c_int, len) }); } }; self.conn .decode_result(match value { - ValueRef::Null => unsafe { ffi::sqlite3_bind_null(ptr, col) }, + ValueRef::Null => unsafe { ffi::sqlite3_bind_null(ptr, col as c_int) }, ValueRef::Integer(i) => unsafe { - ffi::sqlite3_bind_int64(ptr, col, i) + ffi::sqlite3_bind_int64(ptr, col as c_int, i) }, ValueRef::Real(r) => unsafe { - ffi::sqlite3_bind_double(ptr, col, r) + ffi::sqlite3_bind_double(ptr, col as c_int, r) }, ValueRef::Text(s) => unsafe { let length = s.len(); @@ -425,7 +425,7 @@ impl<'conn> Statement<'conn> { } else { ffi::SQLITE_STATIC() }; - ffi::sqlite3_bind_text(ptr, col, c_str.as_ptr(), length as c_int, destructor) + ffi::sqlite3_bind_text(ptr, col as c_int, c_str.as_ptr(), length as c_int, destructor) } }, ValueRef::Blob(b) => unsafe { @@ -433,10 +433,10 @@ impl<'conn> Statement<'conn> { if length > ::std::i32::MAX as usize { ffi::SQLITE_TOOBIG } else if length == 0 { - ffi::sqlite3_bind_zeroblob(ptr, col, 0) + ffi::sqlite3_bind_zeroblob(ptr, col as c_int, 0) } else { ffi::sqlite3_bind_blob(ptr, - col, + col as c_int, b.as_ptr() as *const c_void, length as c_int, ffi::SQLITE_TRANSIENT()) @@ -498,7 +498,7 @@ impl<'conn> Drop for Statement<'conn> { // once pub(crate) is stable. pub trait StatementCrateImpl<'conn> { fn new(conn: &'conn Connection, stmt: RawStatement) -> Self; - fn value_ref(&self, col: c_int) -> ValueRef; + fn value_ref(&self, col: usize) -> ValueRef; fn step(&self) -> Result; fn reset(&self) -> c_int; } @@ -511,18 +511,18 @@ impl<'conn> StatementCrateImpl<'conn> for Statement<'conn> { } } - fn value_ref(&self, col: c_int) -> ValueRef { + fn value_ref(&self, col: usize) -> ValueRef { let raw = unsafe { self.stmt.ptr() }; match self.stmt.column_type(col) { ffi::SQLITE_NULL => ValueRef::Null, ffi::SQLITE_INTEGER => { - ValueRef::Integer(unsafe { ffi::sqlite3_column_int64(raw, col) }) + ValueRef::Integer(unsafe { ffi::sqlite3_column_int64(raw, col as c_int) }) } - ffi::SQLITE_FLOAT => ValueRef::Real(unsafe { ffi::sqlite3_column_double(raw, col) }), + ffi::SQLITE_FLOAT => ValueRef::Real(unsafe { ffi::sqlite3_column_double(raw, col as c_int) }), ffi::SQLITE_TEXT => { let s = unsafe { - let text = ffi::sqlite3_column_text(raw, col); + let text = ffi::sqlite3_column_text(raw, col as c_int); assert!(!text.is_null(), "unexpected SQLITE_TEXT column type with NULL data"); CStr::from_ptr(text as *const c_char) @@ -535,7 +535,7 @@ impl<'conn> StatementCrateImpl<'conn> for Statement<'conn> { } ffi::SQLITE_BLOB => { let (blob, len) = unsafe { - (ffi::sqlite3_column_blob(raw, col), ffi::sqlite3_column_bytes(raw, col)) + (ffi::sqlite3_column_blob(raw, col as c_int), ffi::sqlite3_column_bytes(raw, col as c_int)) }; assert!(len >= 0,