This commit is contained in:
gwenn
2016-02-02 19:12:00 +01:00
parent 55dde134e1
commit f529d130b9
7 changed files with 197 additions and 200 deletions

View File

@@ -37,11 +37,7 @@ impl Connection {
///
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
/// underlying SQLite call fails.
pub fn query_row_named<T, F>(&self,
sql: &str,
params: &[(&str, &ToSql)],
f: F)
-> Result<T>
pub fn query_row_named<T, F>(&self, sql: &str, params: &[(&str, &ToSql)], f: F) -> Result<T>
where F: FnOnce(Row) -> T
{
let mut stmt = try!(self.prepare(sql));
@@ -91,9 +87,7 @@ impl<'conn> Statement<'conn> {
/// which case `query` should be used instead), or the underling SQLite call fails.
pub fn execute_named(&mut self, params: &[(&str, &ToSql)]) -> Result<c_int> {
try!(self.bind_parameters_named(params));
unsafe {
self.execute_()
}
unsafe { self.execute_() }
}
/// Execute the prepared statement with named parameter(s), returning an iterator over the
@@ -118,9 +112,7 @@ impl<'conn> Statement<'conn> {
/// # Failure
///
/// Will return `Err` if binding parameters fails.
pub fn query_named<'a>(&'a mut self,
params: &[(&str, &ToSql)])
-> Result<Rows<'a>> {
pub fn query_named<'a>(&'a mut self, params: &[(&str, &ToSql)]) -> Result<Rows<'a>> {
self.reset_if_needed();
try!(self.bind_parameters_named(params));
@@ -198,8 +190,10 @@ mod test {
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)").unwrap();
stmt.execute_named(&[(":x", &"one")]).unwrap();
let result: Option<String> = db.query_row("SELECT y FROM test WHERE x = 'one'", &[],
|row| row.get(0)).unwrap();
let result: Option<String> = db.query_row("SELECT y FROM test WHERE x = 'one'",
&[],
|row| row.get(0))
.unwrap();
assert!(result.is_none());
}
@@ -213,8 +207,10 @@ mod test {
stmt.execute_named(&[(":x", &"one")]).unwrap();
stmt.execute_named(&[(":y", &"two")]).unwrap();
let result: String = db.query_row("SELECT x FROM test WHERE y = 'two'", &[],
|row| row.get(0)).unwrap();
let result: String = db.query_row("SELECT x FROM test WHERE y = 'two'",
&[],
|row| row.get(0))
.unwrap();
assert_eq!(result, "one");
}
}