Correct idx-checking behavior for SqliteRow::get_checked()

This commit is contained in:
Patrick Fernie 2015-08-27 10:44:24 -04:00
parent e8967388e6
commit e1532f5edf

View File

@ -866,9 +866,19 @@ impl<'stmt> SqliteRow<'stmt> {
/// Returns a `SQLITE_MISMATCH`-coded `SqliteError` if the underlying SQLite column /// Returns a `SQLITE_MISMATCH`-coded `SqliteError` if the underlying SQLite column
/// type is not a valid type as a source for `T`. /// type is not a valid type as a source for `T`.
/// ///
/// Panics if `idx` is outside the range of columns in the returned query or if this row /// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range
/// is stale. /// for this row or if this row is stale.
pub fn get_checked<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> { pub fn get_checked<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
if self.row_idx != self.current_row.get() {
return Err(SqliteError{ code: ffi::SQLITE_MISUSE,
message: "Cannot get values from a row after advancing to next row".to_string() });
}
unsafe {
if idx < 0 || idx >= ffi::sqlite3_column_count(self.stmt.stmt) {
return Err(SqliteError{ code: ffi::SQLITE_MISUSE,
message: "Invalid column index".to_string() });
}
}
let valid_column_type = unsafe { let valid_column_type = unsafe {
T::column_has_valid_sqlite_type(self.stmt.stmt, idx) T::column_has_valid_sqlite_type(self.stmt.stmt, idx)
}; };