Merge pull request #39 from jgallagher/add-get-checked

Add get_checked to SqliteRow.
This commit is contained in:
John Gallagher
2015-05-11 20:17:18 -04:00
5 changed files with 132 additions and 9 deletions

View File

@@ -835,6 +835,30 @@ impl<'stmt> SqliteRow<'stmt> {
self.get_opt(idx).unwrap()
}
/// Get the value of a particular column of the result row.
///
/// ## Failure
///
/// Returns a `SQLITE_MISMATCH`-coded `SqliteError` if the underlying SQLite column
/// 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
/// is stale.
pub fn get_checked<T: FromSql>(&self, idx: c_int) -> SqliteResult<T> {
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