mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Make query_row
a synonym for query_row_safe
.
This is a breaking change for anyone using `query_row`. To update code that used the old `query_row`, you must now `.unwrap()` the returned result.
This commit is contained in:
parent
74496cd781
commit
03be8e0cd6
47
src/lib.rs
47
src/lib.rs
@ -274,28 +274,27 @@ impl SqliteConnection {
|
|||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use rusqlite::{SqliteConnection};
|
/// # use rusqlite::{SqliteResult,SqliteConnection};
|
||||||
/// fn preferred_locale(conn: &SqliteConnection) -> String {
|
/// fn preferred_locale(conn: &SqliteConnection) -> SqliteResult<String> {
|
||||||
/// conn.query_row("SELECT value FROM preferences WHERE name='locale'", &[], |row| {
|
/// conn.query_row_safe("SELECT value FROM preferences WHERE name='locale'", &[], |row| {
|
||||||
/// row.get(0)
|
/// row.get(0)
|
||||||
/// })
|
/// })
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ## Failure
|
|
||||||
///
|
|
||||||
/// Panics if:
|
|
||||||
///
|
|
||||||
/// * Preparing the query fails.
|
|
||||||
/// * Running the query fails (i.e., calling `query` on the prepared statement).
|
|
||||||
/// * The query does not successfully return at least one row.
|
|
||||||
///
|
|
||||||
/// If the query returns more than one row, all rows except the first are ignored.
|
/// If the query returns more than one row, all rows except the first are ignored.
|
||||||
pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> 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 = self.prepare(sql).unwrap();
|
let mut stmt = try!(self.prepare(sql));
|
||||||
let mut rows = stmt.query(params).unwrap();
|
let mut rows = try!(stmt.query(params));
|
||||||
f(rows.next().expect("Query did not return a row").unwrap())
|
|
||||||
|
match rows.next() {
|
||||||
|
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.
|
||||||
@ -312,18 +311,14 @@ impl SqliteConnection {
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// If the query returns more than one row, all rows except the first are ignored.
|
/// If the query returns more than one row, all rows except the first are ignored.
|
||||||
|
///
|
||||||
|
/// ## Deprecated
|
||||||
|
///
|
||||||
|
/// This method should be considered deprecated. Use `query_row` instead, which now
|
||||||
|
/// does exactly the same thing.
|
||||||
pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> SqliteResult<T>
|
pub fn query_row_safe<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));
|
self.query_row(sql, params, f)
|
||||||
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(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prepare a SQL statement for execution.
|
/// Prepare a SQL statement for execution.
|
||||||
@ -904,7 +899,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.get(0)));
|
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -181,7 +181,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.get(0)));
|
assert_eq!(2i32, db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +200,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.get(0)));
|
assert_eq!(2i32, db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,6 +228,6 @@ mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)));
|
assert_eq!(3i32, db.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0)).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,7 +246,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.get(0));
|
let v: Vec<u8> = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
assert_eq!(v, v1234);
|
assert_eq!(v, v1234);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,7 +257,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.get(0));
|
let from: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
assert_eq!(from, s);
|
assert_eq!(from, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,7 +268,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.get(0));
|
let from: time::Timespec = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap();
|
||||||
assert_eq!(from, ts);
|
assert_eq!(from, ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user