From 03be8e0cd6552af7fb46e3ae46d7c7b0c31b7181 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 4 May 2015 20:12:18 -0400 Subject: [PATCH] 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. --- src/lib.rs | 47 +++++++++++++++++++++------------------------- src/transaction.rs | 6 +++--- src/types.rs | 6 +++--- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 13007cc..465dd46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -274,28 +274,27 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection}; - /// fn preferred_locale(conn: &SqliteConnection) -> String { - /// conn.query_row("SELECT value FROM preferences WHERE name='locale'", &[], |row| { + /// # use rusqlite::{SqliteResult,SqliteConnection}; + /// fn preferred_locale(conn: &SqliteConnection) -> SqliteResult { + /// conn.query_row_safe("SELECT value FROM preferences WHERE name='locale'", &[], |row| { /// 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. - pub fn query_row(&self, sql: &str, params: &[&ToSql], f: F) -> T + pub fn query_row(&self, sql: &str, params: &[&ToSql], f: F) -> SqliteResult where F: FnOnce(SqliteRow) -> 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()) + let mut stmt = try!(self.prepare(sql)); + 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(), + }) + } } /// 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. + /// + /// ## Deprecated + /// + /// This method should be considered deprecated. Use `query_row` instead, which now + /// does exactly the same thing. pub fn query_row_safe(&self, sql: &str, params: &[&ToSql], f: F) -> SqliteResult where F: FnOnce(SqliteRow) -> T { - let mut stmt = try!(self.prepare(sql)); - 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(), - }) - } + self.query_row(sql, params, f) } /// 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 (?)", &[&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] diff --git a/src/transaction.rs b/src/transaction.rs index 1882539..30f4c0e 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -181,7 +181,7 @@ mod test { } { 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(); - 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()); } } diff --git a/src/types.rs b/src/types.rs index f902a59..efecd7c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -246,7 +246,7 @@ mod test { let v1234 = vec![1u8,2,3,4]; db.execute("INSERT INTO foo(b) VALUES (?)", &[&v1234]).unwrap(); - let v: Vec = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)); + let v: Vec = db.query_row("SELECT b FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(v, v1234); } @@ -257,7 +257,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.get(0)); + let from: String = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(from, s); } @@ -268,7 +268,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.get(0)); + let from: time::Timespec = db.query_row("SELECT t FROM foo", &[], |r| r.get(0)).unwrap(); assert_eq!(from, ts); }