Change query_row to unwrap the SqliteResult

This commit is contained in:
John Gallagher 2014-10-28 15:14:52 -04:00
parent 68e9d81283
commit 949260046c
3 changed files with 11 additions and 10 deletions

View File

@ -85,9 +85,10 @@ impl SqliteConnection {
self.db.borrow_mut().last_insert_rowid()
}
pub fn query_row<T>(&self, sql: &str, params: &[&ToSql],
f: |SqliteResult<SqliteRow>| -> T) -> T {
f(self.prepare(sql).unwrap().query(params).unwrap().next().unwrap())
pub fn query_row<T>(&self, sql: &str, params: &[&ToSql], f: |SqliteRow| -> T) -> T {
let mut stmt = self.prepare(sql).unwrap();
let mut rows = stmt.query(params).unwrap();
f(rows.next().expect("Query did not return a row").unwrap())
}
pub fn prepare<'a>(&'a self, sql: &str) -> SqliteResult<SqliteStatement<'a>> {
@ -409,7 +410,7 @@ mod test {
assert_eq!(db.execute("INSERT INTO foo(x) VALUES (?)", &[&1i32]).unwrap(), 1);
assert_eq!(db.execute("INSERT INTO foo(x) VALUES (?)", &[&2i32]).unwrap(), 1);
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.unwrap().get(0)));
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0)));
}
#[test]

View File

@ -115,7 +115,7 @@ mod test {
}
{
let _tx = db.transaction().unwrap();
assert_eq!(2i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.unwrap().get(0)));
assert_eq!(2i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0)));
}
}
@ -134,7 +134,7 @@ mod test {
}
{
let _tx = db.transaction().unwrap();
assert_eq!(2i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.unwrap().get(0)));
assert_eq!(2i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0)));
}
}
@ -162,7 +162,7 @@ mod test {
}
}
}
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.unwrap().get(0)));
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0)));
}
#[bench]

View File

@ -165,7 +165,7 @@ mod test {
let v1234 = vec![1u8,2,3,4];
db.execute("INSERT INTO foo(b) VALUES (?)", &[&v1234]).unwrap();
let v: Vec<u8> = db.query_row("SELECT b FROM foo", [], |r| r.unwrap().get(0));
let v: Vec<u8> = db.query_row("SELECT b FROM foo", [], |r| r.get(0));
assert_eq!(v, v1234);
}
@ -176,7 +176,7 @@ mod test {
let s = "hello, world!";
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s.to_string()]).unwrap();
let from: String = db.query_row("SELECT t FROM foo", [], |r| r.unwrap().get(0));
let from: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0));
assert_eq!(from.as_slice(), s);
}
@ -187,7 +187,7 @@ mod test {
let ts = time::Timespec{sec: 10_000, nsec: 0 };
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
let from: time::Timespec = db.query_row("SELECT t FROM foo", [], |r| r.unwrap().get(0));
let from: time::Timespec = db.query_row("SELECT t FROM foo", [], |r| r.get(0));
assert_eq!(from, ts);
}