Refactor: Extract match to get an expected row into its own method.

This commit is contained in:
John Gallagher 2015-12-10 20:48:38 -05:00
parent b7468b2c4b
commit 0051ff47a6
2 changed files with 23 additions and 35 deletions

View File

@ -373,15 +373,7 @@ impl SqliteConnection {
let mut stmt = try!(self.prepare(sql));
let mut rows = try!(stmt.query(params));
match rows.next() {
Some(row) => row.map(f),
None => {
Err(SqliteError {
code: ffi::SQLITE_NOTICE,
message: "Query did not return a row".to_string(),
})
}
}
rows.get_expected_row().map(f)
}
/// Convenience method to execute a query that is expected to return a single row,
@ -412,15 +404,7 @@ impl SqliteConnection {
let mut stmt = try!(self.prepare(sql));
let mut rows = try!(stmt.query(params));
match rows.next() {
Some(row) => row.map_err(E::from).and_then(f),
None => {
Err(E::from(SqliteError {
code: ffi::SQLITE_NOTICE,
message: "Query did not return a row".to_string(),
}))
}
}
rows.get_expected_row().map_err(E::from).and_then(f)
}
/// Convenience method to execute a query that is expected to return a single row.
@ -1049,6 +1033,18 @@ impl<'stmt> SqliteRows<'stmt> {
failed: false,
}
}
fn get_expected_row(&mut self) -> SqliteResult<SqliteRow<'stmt>> {
match self.next() {
Some(row) => row,
None => {
Err(SqliteError {
code: ffi::SQLITE_NOTICE,
message: "Query did not return a row".to_string(),
})
}
}
}
}
impl<'stmt> Iterator for SqliteRows<'stmt> {

View File

@ -48,15 +48,7 @@ impl SqliteConnection {
let mut stmt = try!(self.prepare(sql));
let mut rows = try!(stmt.query_named(params));
match rows.next() {
Some(row) => row.map(f),
None => {
Err(SqliteError {
code: ffi::SQLITE_NOTICE,
message: "Query did not return a row".to_string(),
})
}
}
rows.get_expected_row().map(f)
}
}