mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 19:41:37 +08:00
Refactor: Extract match to get an expected row into its own method.
This commit is contained in:
parent
b7468b2c4b
commit
0051ff47a6
48
src/lib.rs
48
src/lib.rs
@ -369,20 +369,12 @@ impl SqliteConnection {
|
|||||||
/// underlying SQLite call fails.
|
/// underlying SQLite call fails.
|
||||||
pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> SqliteResult<T>
|
pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> SqliteResult<T>
|
||||||
where F: FnOnce(SqliteRow) -> T
|
where F: FnOnce(SqliteRow) -> T
|
||||||
{
|
{
|
||||||
let mut stmt = try!(self.prepare(sql));
|
let mut stmt = try!(self.prepare(sql));
|
||||||
let mut rows = try!(stmt.query(params));
|
let mut rows = try!(stmt.query(params));
|
||||||
|
|
||||||
match rows.next() {
|
rows.get_expected_row().map(f)
|
||||||
Some(row) => row.map(f),
|
}
|
||||||
None => {
|
|
||||||
Err(SqliteError {
|
|
||||||
code: ffi::SQLITE_NOTICE,
|
|
||||||
message: "Query did not return a row".to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience method to execute a query that is expected to return a single row,
|
/// Convenience method to execute a query that is expected to return a single row,
|
||||||
/// and execute a mapping via `f` on that returned row with the possibility of failure.
|
/// and execute a mapping via `f` on that returned row with the possibility of failure.
|
||||||
@ -408,20 +400,12 @@ impl SqliteConnection {
|
|||||||
pub fn query_row_and_then<T, E, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T, E>
|
pub fn query_row_and_then<T, E, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T, E>
|
||||||
where F: FnOnce(SqliteRow) -> Result<T, E>,
|
where F: FnOnce(SqliteRow) -> Result<T, E>,
|
||||||
E: convert::From<SqliteError>
|
E: convert::From<SqliteError>
|
||||||
{
|
{
|
||||||
let mut stmt = try!(self.prepare(sql));
|
let mut stmt = try!(self.prepare(sql));
|
||||||
let mut rows = try!(stmt.query(params));
|
let mut rows = try!(stmt.query(params));
|
||||||
|
|
||||||
match rows.next() {
|
rows.get_expected_row().map_err(E::from).and_then(f)
|
||||||
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(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience method to execute a query that is expected to return a single row.
|
/// Convenience method to execute a query that is expected to return a single row.
|
||||||
///
|
///
|
||||||
@ -1049,6 +1033,18 @@ impl<'stmt> SqliteRows<'stmt> {
|
|||||||
failed: false,
|
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> {
|
impl<'stmt> Iterator for SqliteRows<'stmt> {
|
||||||
|
@ -48,15 +48,7 @@ impl SqliteConnection {
|
|||||||
let mut stmt = try!(self.prepare(sql));
|
let mut stmt = try!(self.prepare(sql));
|
||||||
let mut rows = try!(stmt.query_named(params));
|
let mut rows = try!(stmt.query_named(params));
|
||||||
|
|
||||||
match rows.next() {
|
rows.get_expected_row().map(f)
|
||||||
Some(row) => row.map(f),
|
|
||||||
None => {
|
|
||||||
Err(SqliteError {
|
|
||||||
code: ffi::SQLITE_NOTICE,
|
|
||||||
message: "Query did not return a row".to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user