mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Change query_row
to unwrap the SqliteResult
This commit is contained in:
parent
68e9d81283
commit
949260046c
@ -85,9 +85,10 @@ impl SqliteConnection {
|
|||||||
self.db.borrow_mut().last_insert_rowid()
|
self.db.borrow_mut().last_insert_rowid()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn query_row<T>(&self, sql: &str, params: &[&ToSql],
|
pub fn query_row<T>(&self, sql: &str, params: &[&ToSql], f: |SqliteRow| -> T) -> T {
|
||||||
f: |SqliteResult<SqliteRow>| -> T) -> T {
|
let mut stmt = self.prepare(sql).unwrap();
|
||||||
f(self.prepare(sql).unwrap().query(params).unwrap().next().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>> {
|
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 (?)", &[&1i32]).unwrap(), 1);
|
||||||
assert_eq!(db.execute("INSERT INTO foo(x) VALUES (?)", &[&2i32]).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]
|
#[test]
|
||||||
|
@ -115,7 +115,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
let _tx = db.transaction().unwrap();
|
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();
|
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]
|
#[bench]
|
||||||
|
@ -165,7 +165,7 @@ mod test {
|
|||||||
let v1234 = vec![1u8,2,3,4];
|
let v1234 = vec![1u8,2,3,4];
|
||||||
db.execute("INSERT INTO foo(b) VALUES (?)", &[&v1234]).unwrap();
|
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);
|
assert_eq!(v, v1234);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +176,7 @@ mod test {
|
|||||||
let s = "hello, world!";
|
let s = "hello, world!";
|
||||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s.to_string()]).unwrap();
|
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);
|
assert_eq!(from.as_slice(), s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ mod test {
|
|||||||
let ts = time::Timespec{sec: 10_000, nsec: 0 };
|
let ts = time::Timespec{sec: 10_000, nsec: 0 };
|
||||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
|
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);
|
assert_eq!(from, ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user