Remove get_opt (superceded by get_checked).

This commit is contained in:
John Gallagher 2015-09-21 10:31:11 -04:00
parent 1918dc14d0
commit 7ee69fe103

View File

@ -941,7 +941,7 @@ impl<'stmt> SqliteRow<'stmt> {
/// Panics if `idx` is outside the range of columns in the returned query or if this row /// Panics if `idx` is outside the range of columns in the returned query or if this row
/// is stale. /// is stale.
pub fn get<T: FromSql>(&self, idx: c_int) -> T { pub fn get<T: FromSql>(&self, idx: c_int) -> T {
self.get_opt(idx).unwrap() self.get_checked(idx).unwrap()
} }
/// Get the value of a particular column of the result row. /// Get the value of a particular column of the result row.
@ -954,37 +954,6 @@ impl<'stmt> SqliteRow<'stmt> {
/// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range /// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range
/// for this row or if this row 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 {
T::column_has_valid_sqlite_type(self.stmt.stmt, idx)
};
if valid_column_type {
Ok(self.get(idx))
} else {
Err(SqliteError{
code: ffi::SQLITE_MISMATCH,
message: "Invalid column type".to_string(),
})
}
}
/// Attempt to get the value of a particular column of the result row.
///
/// ## Failure
///
/// Returns a `SQLITE_MISUSE`-coded `SqliteError` if `idx` is outside the valid column range
/// for this row or if this row is stale.
pub fn get_opt<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
if self.row_idx != self.current_row.get() { if self.row_idx != self.current_row.get() {
return Err(SqliteError{ code: ffi::SQLITE_MISUSE, return Err(SqliteError{ code: ffi::SQLITE_MISUSE,
message: "Cannot get values from a row after advancing to next row".to_string() }); message: "Cannot get values from a row after advancing to next row".to_string() });
@ -994,7 +963,15 @@ impl<'stmt> SqliteRow<'stmt> {
return Err(SqliteError{ code: ffi::SQLITE_MISUSE, return Err(SqliteError{ code: ffi::SQLITE_MISUSE,
message: "Invalid column index".to_string() }); message: "Invalid column index".to_string() });
} }
FromSql::column_result(self.stmt.stmt, idx)
if T::column_has_valid_sqlite_type(self.stmt.stmt, idx) {
FromSql::column_result(self.stmt.stmt, idx)
} else {
Err(SqliteError{
code: ffi::SQLITE_MISMATCH,
message: "Invalid column type".to_string(),
})
}
} }
} }
} }
@ -1226,7 +1203,7 @@ mod test {
assert_eq!(2i32, second.get(0)); assert_eq!(2i32, second.get(0));
let result = first.get_opt::<i32>(0); let result = first.get_checked::<i32>(0);
assert!(result.unwrap_err().message.contains("advancing to next row")); assert!(result.unwrap_err().message.contains("advancing to next row"));
} }