mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Merge pull request #1251 from gwenn/one_column
Intoduce one_column for tests
This commit is contained in:
commit
12a6d3c1b1
@ -336,7 +336,7 @@ mod test {
|
|||||||
backup.step(-1)?;
|
backup.step(-1)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst.query_row("SELECT x FROM foo", [], |r| r.get(0))?;
|
let the_answer: i64 = dst.one_column("SELECT x FROM foo")?;
|
||||||
assert_eq!(42, the_answer);
|
assert_eq!(42, the_answer);
|
||||||
|
|
||||||
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
||||||
@ -346,7 +346,7 @@ mod test {
|
|||||||
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo")?;
|
||||||
assert_eq!(42 + 43, the_answer);
|
assert_eq!(42 + 43, the_answer);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -368,7 +368,7 @@ mod test {
|
|||||||
backup.step(-1)?;
|
backup.step(-1)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst.query_row("SELECT x FROM foo", [], |r| r.get(0))?;
|
let the_answer: i64 = dst.one_column("SELECT x FROM foo")?;
|
||||||
assert_eq!(42, the_answer);
|
assert_eq!(42, the_answer);
|
||||||
|
|
||||||
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
||||||
@ -379,7 +379,7 @@ mod test {
|
|||||||
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo")?;
|
||||||
assert_eq!(42 + 43, the_answer);
|
assert_eq!(42 + 43, the_answer);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -406,7 +406,7 @@ mod test {
|
|||||||
backup.step(-1)?;
|
backup.step(-1)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst.query_row("SELECT x FROM foo", [], |r| r.get(0))?;
|
let the_answer: i64 = dst.one_column("SELECT x FROM foo")?;
|
||||||
assert_eq!(42, the_answer);
|
assert_eq!(42, the_answer);
|
||||||
|
|
||||||
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
||||||
@ -421,7 +421,7 @@ mod test {
|
|||||||
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo")?;
|
||||||
assert_eq!(42 + 43, the_answer);
|
assert_eq!(42 + 43, the_answer);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -820,9 +820,9 @@ mod test {
|
|||||||
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|
||||||
half,
|
half,
|
||||||
)?;
|
)?;
|
||||||
let result: Result<f64> = db.query_row("SELECT half(6)", [], |r| r.get(0));
|
let result: f64 = db.one_column("SELECT half(6)")?;
|
||||||
|
|
||||||
assert!((3f64 - result?).abs() < f64::EPSILON);
|
assert!((3f64 - result).abs() < f64::EPSILON);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -835,11 +835,11 @@ mod test {
|
|||||||
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|
||||||
half,
|
half,
|
||||||
)?;
|
)?;
|
||||||
let result: Result<f64> = db.query_row("SELECT half(6)", [], |r| r.get(0));
|
let result: f64 = db.one_column("SELECT half(6)")?;
|
||||||
assert!((3f64 - result?).abs() < f64::EPSILON);
|
assert!((3f64 - result).abs() < f64::EPSILON);
|
||||||
|
|
||||||
db.remove_function("half", 1)?;
|
db.remove_function("half", 1)?;
|
||||||
let result: Result<f64> = db.query_row("SELECT half(6)", [], |r| r.get(0));
|
let result: Result<f64> = db.one_column("SELECT half(6)");
|
||||||
result.unwrap_err();
|
result.unwrap_err();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -885,18 +885,14 @@ mod test {
|
|||||||
regexp_with_auxilliary,
|
regexp_with_auxilliary,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let result: Result<bool> =
|
let result: bool = db.one_column("SELECT regexp('l.s[aeiouy]', 'lisa')")?;
|
||||||
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", [], |r| r.get(0));
|
|
||||||
|
|
||||||
assert!(result?);
|
assert!(result);
|
||||||
|
|
||||||
let result: Result<i64> = db.query_row(
|
let result: i64 =
|
||||||
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
db.one_column("SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1")?;
|
||||||
[],
|
|
||||||
|r| r.get(0),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(2, result?);
|
assert_eq!(2, result);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -924,7 +920,7 @@ mod test {
|
|||||||
("onetwo", "SELECT my_concat('one', 'two')"),
|
("onetwo", "SELECT my_concat('one', 'two')"),
|
||||||
("abc", "SELECT my_concat('a', 'b', 'c')"),
|
("abc", "SELECT my_concat('a', 'b', 'c')"),
|
||||||
] {
|
] {
|
||||||
let result: String = db.query_row(query, [], |r| r.get(0))?;
|
let result: String = db.one_column(query)?;
|
||||||
assert_eq!(expected, result);
|
assert_eq!(expected, result);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -943,11 +939,8 @@ mod test {
|
|||||||
Ok(true)
|
Ok(true)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let res: bool = db.query_row(
|
let res: bool =
|
||||||
"SELECT example(0, i) FROM (SELECT 0 as i UNION SELECT 1)",
|
db.one_column("SELECT example(0, i) FROM (SELECT 0 as i UNION SELECT 1)")?;
|
||||||
[],
|
|
||||||
|r| r.get(0),
|
|
||||||
)?;
|
|
||||||
// Doesn't actually matter, we'll assert in the function if there's a problem.
|
// Doesn't actually matter, we'll assert in the function if there's a problem.
|
||||||
assert!(res);
|
assert!(res);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -998,11 +991,11 @@ mod test {
|
|||||||
|
|
||||||
// sum should return NULL when given no columns (contrast with count below)
|
// sum should return NULL when given no columns (contrast with count below)
|
||||||
let no_result = "SELECT my_sum(i) FROM (SELECT 2 AS i WHERE 1 <> 1)";
|
let no_result = "SELECT my_sum(i) FROM (SELECT 2 AS i WHERE 1 <> 1)";
|
||||||
let result: Option<i64> = db.query_row(no_result, [], |r| r.get(0))?;
|
let result: Option<i64> = db.one_column(no_result)?;
|
||||||
assert!(result.is_none());
|
assert!(result.is_none());
|
||||||
|
|
||||||
let single_sum = "SELECT my_sum(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)";
|
let single_sum = "SELECT my_sum(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)";
|
||||||
let result: i64 = db.query_row(single_sum, [], |r| r.get(0))?;
|
let result: i64 = db.one_column(single_sum)?;
|
||||||
assert_eq!(4, result);
|
assert_eq!(4, result);
|
||||||
|
|
||||||
let dual_sum = "SELECT my_sum(i), my_sum(j) FROM (SELECT 2 AS i, 1 AS j UNION ALL SELECT \
|
let dual_sum = "SELECT my_sum(i), my_sum(j) FROM (SELECT 2 AS i, 1 AS j UNION ALL SELECT \
|
||||||
@ -1024,11 +1017,11 @@ mod test {
|
|||||||
|
|
||||||
// count should return 0 when given no columns (contrast with sum above)
|
// count should return 0 when given no columns (contrast with sum above)
|
||||||
let no_result = "SELECT my_count(i) FROM (SELECT 2 AS i WHERE 1 <> 1)";
|
let no_result = "SELECT my_count(i) FROM (SELECT 2 AS i WHERE 1 <> 1)";
|
||||||
let result: i64 = db.query_row(no_result, [], |r| r.get(0))?;
|
let result: i64 = db.one_column(no_result)?;
|
||||||
assert_eq!(result, 0);
|
assert_eq!(result, 0);
|
||||||
|
|
||||||
let single_sum = "SELECT my_count(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)";
|
let single_sum = "SELECT my_count(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)";
|
||||||
let result: i64 = db.query_row(single_sum, [], |r| r.get(0))?;
|
let result: i64 = db.one_column(single_sum)?;
|
||||||
assert_eq!(2, result);
|
assert_eq!(2, result);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
44
src/lib.rs
44
src/lib.rs
@ -665,6 +665,12 @@ impl Connection {
|
|||||||
stmt.query_row(params, f)
|
stmt.query_row(params, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://sqlite.org/tclsqlite.html#onecolumn
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) fn one_column<T: crate::types::FromSql>(&self, sql: &str) -> Result<T> {
|
||||||
|
self.query_row(sql, [], |r| r.get(0))
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience method to execute a query with named parameter(s) that is
|
/// Convenience method to execute a query with named parameter(s) that is
|
||||||
/// expected to return a single row.
|
/// expected to return a single row.
|
||||||
///
|
///
|
||||||
@ -1261,9 +1267,9 @@ mod test {
|
|||||||
|
|
||||||
let path_string = path.to_str().unwrap();
|
let path_string = path.to_str().unwrap();
|
||||||
let db = Connection::open(path_string)?;
|
let db = Connection::open(path_string)?;
|
||||||
let the_answer: Result<i64> = db.query_row("SELECT x FROM foo", [], |r| r.get(0));
|
let the_answer: i64 = db.one_column("SELECT x FROM foo")?;
|
||||||
|
|
||||||
assert_eq!(42i64, the_answer?);
|
assert_eq!(42i64, the_answer);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1318,9 +1324,9 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let db = Connection::open(&db_path)?;
|
let db = Connection::open(&db_path)?;
|
||||||
let the_answer: Result<i64> = db.query_row("SELECT x FROM foo", [], |r| r.get(0));
|
let the_answer: i64 = db.one_column("SELECT x FROM foo")?;
|
||||||
|
|
||||||
assert_eq!(42i64, the_answer?);
|
assert_eq!(42i64, the_answer);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1403,10 +1409,7 @@ mod test {
|
|||||||
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?);
|
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?);
|
||||||
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [2i32])?);
|
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [2i32])?);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(3i32, db.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||||
3i32,
|
|
||||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1541,12 +1544,9 @@ mod test {
|
|||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql)?;
|
db.execute_batch(sql)?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(10i64, db.one_column::<i64>("SELECT SUM(x) FROM foo")?);
|
||||||
10i64,
|
|
||||||
db.query_row::<i64, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
|
|
||||||
let result: Result<i64> = db.query_row("SELECT x FROM foo WHERE x > 5", [], |r| r.get(0));
|
let result: Result<i64> = db.one_column("SELECT x FROM foo WHERE x > 5");
|
||||||
match result.unwrap_err() {
|
match result.unwrap_err() {
|
||||||
Error::QueryReturnedNoRows => (),
|
Error::QueryReturnedNoRows => (),
|
||||||
err => panic!("Unexpected error {}", err),
|
err => panic!("Unexpected error {}", err),
|
||||||
@ -1562,21 +1562,21 @@ mod test {
|
|||||||
fn test_optional() -> Result<()> {
|
fn test_optional() -> Result<()> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
|
|
||||||
let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 <> 0", [], |r| r.get(0));
|
let result: Result<i64> = db.one_column("SELECT 1 WHERE 0 <> 0");
|
||||||
let result = result.optional();
|
let result = result.optional();
|
||||||
match result? {
|
match result? {
|
||||||
None => (),
|
None => (),
|
||||||
_ => panic!("Unexpected result"),
|
_ => panic!("Unexpected result"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 == 0", [], |r| r.get(0));
|
let result: Result<i64> = db.one_column("SELECT 1 WHERE 0 == 0");
|
||||||
let result = result.optional();
|
let result = result.optional();
|
||||||
match result? {
|
match result? {
|
||||||
Some(1) => (),
|
Some(1) => (),
|
||||||
_ => panic!("Unexpected result"),
|
_ => panic!("Unexpected result"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let bad_query_result: Result<i64> = db.query_row("NOT A PROPER QUERY", [], |r| r.get(0));
|
let bad_query_result: Result<i64> = db.one_column("NOT A PROPER QUERY");
|
||||||
let bad_query_result = bad_query_result.optional();
|
let bad_query_result = bad_query_result.optional();
|
||||||
bad_query_result.unwrap_err();
|
bad_query_result.unwrap_err();
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1585,11 +1585,8 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_pragma_query_row() -> Result<()> {
|
fn test_pragma_query_row() -> Result<()> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
assert_eq!(
|
assert_eq!("memory", db.one_column::<String>("PRAGMA journal_mode")?);
|
||||||
"memory",
|
let mode = db.one_column::<String>("PRAGMA journal_mode=off")?;
|
||||||
db.query_row::<String, _, _>("PRAGMA journal_mode", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
let mode = db.query_row::<String, _, _>("PRAGMA journal_mode=off", [], |r| r.get(0))?;
|
|
||||||
if cfg!(features = "bundled") {
|
if cfg!(features = "bundled") {
|
||||||
assert_eq!(mode, "off");
|
assert_eq!(mode, "off");
|
||||||
} else {
|
} else {
|
||||||
@ -2079,10 +2076,7 @@ mod test {
|
|||||||
fn test_returning() -> Result<()> {
|
fn test_returning() -> Result<()> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?;
|
db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?;
|
||||||
let row_id =
|
let row_id = db.one_column::<i64>("INSERT INTO foo DEFAULT VALUES RETURNING ROWID")?;
|
||||||
db.query_row::<i64, _, _>("INSERT INTO foo DEFAULT VALUES RETURNING ROWID", [], |r| {
|
|
||||||
r.get(0)
|
|
||||||
})?;
|
|
||||||
assert_eq!(row_id, 1);
|
assert_eq!(row_id, 1);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -333,10 +333,7 @@ mod test {
|
|||||||
#[cfg(feature = "modern_sqlite")]
|
#[cfg(feature = "modern_sqlite")]
|
||||||
fn pragma_func_query_value() -> Result<()> {
|
fn pragma_func_query_value() -> Result<()> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
let user_version: i32 =
|
let user_version: i32 = db.one_column("SELECT user_version FROM pragma_user_version")?;
|
||||||
db.query_row("SELECT user_version FROM pragma_user_version", [], |row| {
|
|
||||||
row.get(0)
|
|
||||||
})?;
|
|
||||||
assert_eq!(0, user_version);
|
assert_eq!(0, user_version);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1212,8 +1212,7 @@ mod test {
|
|||||||
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?;
|
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?;
|
||||||
stmt.execute_named(&[(":x", &"one")])?;
|
stmt.execute_named(&[(":x", &"one")])?;
|
||||||
|
|
||||||
let result: Option<String> =
|
let result: Option<String> = db.one_column("SELECT y FROM test WHERE x = 'one'")?;
|
||||||
db.query_row("SELECT y FROM test WHERE x = 'one'", [], |row| row.get(0))?;
|
|
||||||
assert!(result.is_none());
|
assert!(result.is_none());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -1259,8 +1258,7 @@ mod test {
|
|||||||
stmt.execute(&[(":x", "one")])?;
|
stmt.execute(&[(":x", "one")])?;
|
||||||
stmt.execute(&[(":y", "two")])?;
|
stmt.execute(&[(":y", "two")])?;
|
||||||
|
|
||||||
let result: String =
|
let result: String = db.one_column("SELECT x FROM test WHERE y = 'two'")?;
|
||||||
db.query_row("SELECT x FROM test WHERE y = 'two'", [], |row| row.get(0))?;
|
|
||||||
assert_eq!(result, "one");
|
assert_eq!(result, "one");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -1500,7 +1498,7 @@ mod test {
|
|||||||
db.execute_batch("CREATE TABLE foo(x TEXT)")?;
|
db.execute_batch("CREATE TABLE foo(x TEXT)")?;
|
||||||
let expected = "テスト";
|
let expected = "テスト";
|
||||||
db.execute("INSERT INTO foo(x) VALUES (?)", [&expected])?;
|
db.execute("INSERT INTO foo(x) VALUES (?)", [&expected])?;
|
||||||
let actual: String = db.query_row("SELECT x FROM foo", [], |row| row.get(0))?;
|
let actual: String = db.one_column("SELECT x FROM foo")?;
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -552,10 +552,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
let tx = db.transaction()?;
|
let tx = db.transaction()?;
|
||||||
assert_eq!(
|
assert_eq!(2i32, tx.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||||
2i32,
|
|
||||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -591,10 +588,7 @@ mod test {
|
|||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(2i32, db.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||||
2i32,
|
|
||||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -619,10 +613,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
let tx = db.transaction()?;
|
let tx = db.transaction()?;
|
||||||
assert_eq!(
|
assert_eq!(6i32, tx.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||||
6i32,
|
|
||||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -731,7 +722,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn assert_current_sum(x: i32, conn: &Connection) -> Result<()> {
|
fn assert_current_sum(x: i32, conn: &Connection) -> Result<()> {
|
||||||
let i = conn.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
let i = conn.one_column::<i32>("SELECT SUM(x) FROM foo")?;
|
||||||
assert_eq!(x, i);
|
assert_eq!(x, i);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -178,9 +178,9 @@ mod test {
|
|||||||
let date = NaiveDate::from_ymd(2016, 2, 23);
|
let date = NaiveDate::from_ymd(2016, 2, 23);
|
||||||
db.execute("INSERT INTO foo (t) VALUES (?)", [date])?;
|
db.execute("INSERT INTO foo (t) VALUES (?)", [date])?;
|
||||||
|
|
||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!("2016-02-23", s);
|
assert_eq!("2016-02-23", s);
|
||||||
let t: NaiveDate = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let t: NaiveDate = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(date, t);
|
assert_eq!(date, t);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -191,9 +191,9 @@ mod test {
|
|||||||
let time = NaiveTime::from_hms(23, 56, 4);
|
let time = NaiveTime::from_hms(23, 56, 4);
|
||||||
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?;
|
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?;
|
||||||
|
|
||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!("23:56:04", s);
|
assert_eq!("23:56:04", s);
|
||||||
let v: NaiveTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let v: NaiveTime = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(time, v);
|
assert_eq!(time, v);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -207,13 +207,13 @@ mod test {
|
|||||||
|
|
||||||
db.execute("INSERT INTO foo (t) VALUES (?)", [dt])?;
|
db.execute("INSERT INTO foo (t) VALUES (?)", [dt])?;
|
||||||
|
|
||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!("2016-02-23 23:56:04", s);
|
assert_eq!("2016-02-23 23:56:04", s);
|
||||||
let v: NaiveDateTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let v: NaiveDateTime = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(dt, v);
|
assert_eq!(dt, v);
|
||||||
|
|
||||||
db.execute("UPDATE foo set b = datetime(t)", [])?; // "YYYY-MM-DD HH:MM:SS"
|
db.execute("UPDATE foo set b = datetime(t)", [])?; // "YYYY-MM-DD HH:MM:SS"
|
||||||
let hms: NaiveDateTime = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
let hms: NaiveDateTime = db.one_column("SELECT b FROM foo")?;
|
||||||
assert_eq!(dt, hms);
|
assert_eq!(dt, hms);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -228,21 +228,19 @@ mod test {
|
|||||||
|
|
||||||
db.execute("INSERT INTO foo (t) VALUES (?)", [utc])?;
|
db.execute("INSERT INTO foo (t) VALUES (?)", [utc])?;
|
||||||
|
|
||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!("2016-02-23 23:56:04.789+00:00", s);
|
assert_eq!("2016-02-23 23:56:04.789+00:00", s);
|
||||||
|
|
||||||
let v1: DateTime<Utc> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let v1: DateTime<Utc> = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(utc, v1);
|
assert_eq!(utc, v1);
|
||||||
|
|
||||||
let v2: DateTime<Utc> =
|
let v2: DateTime<Utc> = db.one_column("SELECT '2016-02-23 23:56:04.789'")?;
|
||||||
db.query_row("SELECT '2016-02-23 23:56:04.789'", [], |r| r.get(0))?;
|
|
||||||
assert_eq!(utc, v2);
|
assert_eq!(utc, v2);
|
||||||
|
|
||||||
let v3: DateTime<Utc> = db.query_row("SELECT '2016-02-23 23:56:04'", [], |r| r.get(0))?;
|
let v3: DateTime<Utc> = db.one_column("SELECT '2016-02-23 23:56:04'")?;
|
||||||
assert_eq!(utc - Duration::milliseconds(789), v3);
|
assert_eq!(utc - Duration::milliseconds(789), v3);
|
||||||
|
|
||||||
let v4: DateTime<Utc> =
|
let v4: DateTime<Utc> = db.one_column("SELECT '2016-02-23 23:56:04.789+00:00'")?;
|
||||||
db.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", [], |r| r.get(0))?;
|
|
||||||
assert_eq!(utc, v4);
|
assert_eq!(utc, v4);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -258,10 +256,10 @@ mod test {
|
|||||||
db.execute("INSERT INTO foo (t) VALUES (?)", [local])?;
|
db.execute("INSERT INTO foo (t) VALUES (?)", [local])?;
|
||||||
|
|
||||||
// Stored string should be in UTC
|
// Stored string should be in UTC
|
||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert!(s.ends_with("+00:00"));
|
assert!(s.ends_with("+00:00"));
|
||||||
|
|
||||||
let v: DateTime<Local> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let v: DateTime<Local> = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(local, v);
|
assert_eq!(local, v);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -274,10 +272,10 @@ mod test {
|
|||||||
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?;
|
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?;
|
||||||
|
|
||||||
// Stored string should preserve timezone offset
|
// Stored string should preserve timezone offset
|
||||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert!(s.ends_with("+04:00"));
|
assert!(s.ends_with("+04:00"));
|
||||||
|
|
||||||
let v: DateTime<FixedOffset> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let v: DateTime<FixedOffset> = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(time.offset(), v.offset());
|
assert_eq!(time.offset(), v.offset());
|
||||||
assert_eq!(time, v);
|
assert_eq!(time, v);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -286,15 +284,13 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_sqlite_functions() -> Result<()> {
|
fn test_sqlite_functions() -> Result<()> {
|
||||||
let db = checked_memory_handle()?;
|
let db = checked_memory_handle()?;
|
||||||
let result: Result<NaiveTime> = db.query_row("SELECT CURRENT_TIME", [], |r| r.get(0));
|
let result: Result<NaiveTime> = db.one_column("SELECT CURRENT_TIME");
|
||||||
result.unwrap();
|
result.unwrap();
|
||||||
let result: Result<NaiveDate> = db.query_row("SELECT CURRENT_DATE", [], |r| r.get(0));
|
let result: Result<NaiveDate> = db.one_column("SELECT CURRENT_DATE");
|
||||||
result.unwrap();
|
result.unwrap();
|
||||||
let result: Result<NaiveDateTime> =
|
let result: Result<NaiveDateTime> = db.one_column("SELECT CURRENT_TIMESTAMP");
|
||||||
db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0));
|
|
||||||
result.unwrap();
|
result.unwrap();
|
||||||
let result: Result<DateTime<Utc>> =
|
let result: Result<DateTime<Utc>> = db.one_column("SELECT CURRENT_TIMESTAMP");
|
||||||
db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0));
|
|
||||||
result.unwrap();
|
result.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,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])?;
|
db.execute("INSERT INTO foo(b) VALUES (?)", [&v1234])?;
|
||||||
|
|
||||||
let v: Vec<u8> = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
let v: Vec<u8> = db.one_column("SELECT b FROM foo")?;
|
||||||
assert_eq!(v, v1234);
|
assert_eq!(v, v1234);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ mod test {
|
|||||||
let empty = vec![];
|
let empty = vec![];
|
||||||
db.execute("INSERT INTO foo(b) VALUES (?)", [&empty])?;
|
db.execute("INSERT INTO foo(b) VALUES (?)", [&empty])?;
|
||||||
|
|
||||||
let v: Vec<u8> = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
let v: Vec<u8> = db.one_column("SELECT b FROM foo")?;
|
||||||
assert_eq!(v, empty);
|
assert_eq!(v, empty);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -179,7 +179,7 @@ mod test {
|
|||||||
let s = "hello, world!";
|
let s = "hello, world!";
|
||||||
db.execute("INSERT INTO foo(t) VALUES (?)", [&s])?;
|
db.execute("INSERT INTO foo(t) VALUES (?)", [&s])?;
|
||||||
|
|
||||||
let from: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let from: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(from, s);
|
assert_eq!(from, s);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -191,7 +191,7 @@ mod test {
|
|||||||
let s = "hello, world!";
|
let s = "hello, world!";
|
||||||
db.execute("INSERT INTO foo(t) VALUES (?)", [s.to_owned()])?;
|
db.execute("INSERT INTO foo(t) VALUES (?)", [s.to_owned()])?;
|
||||||
|
|
||||||
let from: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let from: String = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(from, s);
|
assert_eq!(from, s);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -202,10 +202,7 @@ mod test {
|
|||||||
|
|
||||||
db.execute("INSERT INTO foo(i) VALUES (?)", [Value::Integer(10)])?;
|
db.execute("INSERT INTO foo(i) VALUES (?)", [Value::Integer(10)])?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(10i64, db.one_column::<i64>("SELECT i FROM foo")?);
|
||||||
10i64,
|
|
||||||
db.query_row::<i64, _, _>("SELECT i FROM foo", [], |r| r.get(0))?
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ impl FromSql for Value {
|
|||||||
mod test {
|
mod test {
|
||||||
use crate::types::ToSql;
|
use crate::types::ToSql;
|
||||||
use crate::{Connection, Result};
|
use crate::{Connection, Result};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
fn checked_memory_handle() -> Result<Connection> {
|
fn checked_memory_handle() -> Result<Connection> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
@ -38,15 +39,15 @@ mod test {
|
|||||||
let db = checked_memory_handle()?;
|
let db = checked_memory_handle()?;
|
||||||
|
|
||||||
let json = r#"{"foo": 13, "bar": "baz"}"#;
|
let json = r#"{"foo": 13, "bar": "baz"}"#;
|
||||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
let data: Value = serde_json::from_str(json).unwrap();
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
||||||
[&data as &dyn ToSql, &json.as_bytes()],
|
[&data as &dyn ToSql, &json.as_bytes()],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let t: serde_json::Value = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let t: Value = db.one_column("SELECT t FROM foo")?;
|
||||||
assert_eq!(data, t);
|
assert_eq!(data, t);
|
||||||
let b: serde_json::Value = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
let b: Value = db.one_column("SELECT b FROM foo")?;
|
||||||
assert_eq!(data, b);
|
assert_eq!(data, b);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ mod test {
|
|||||||
for ts in ts_vec {
|
for ts in ts_vec {
|
||||||
db.execute("INSERT INTO foo(t) VALUES (?)", [ts])?;
|
db.execute("INSERT INTO foo(t) VALUES (?)", [ts])?;
|
||||||
|
|
||||||
let from: OffsetDateTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
let from: OffsetDateTime = db.one_column("SELECT t FROM foo")?;
|
||||||
|
|
||||||
db.execute("DELETE FROM foo", [])?;
|
db.execute("DELETE FROM foo", [])?;
|
||||||
|
|
||||||
@ -152,8 +152,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_sqlite_functions() -> Result<()> {
|
fn test_sqlite_functions() -> Result<()> {
|
||||||
let db = Connection::open_in_memory()?;
|
let db = Connection::open_in_memory()?;
|
||||||
let result: Result<OffsetDateTime> =
|
let result: Result<OffsetDateTime> = db.one_column("SELECT CURRENT_TIMESTAMP");
|
||||||
db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0));
|
|
||||||
result.unwrap();
|
result.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -109,8 +109,8 @@ mod test {
|
|||||||
tx2.commit().unwrap();
|
tx2.commit().unwrap();
|
||||||
});
|
});
|
||||||
assert_eq!(tx.recv().unwrap(), 1);
|
assert_eq!(tx.recv().unwrap(), 1);
|
||||||
let the_answer: Result<i64> = db1.query_row("SELECT x FROM foo", [], |r| r.get(0));
|
let the_answer: i64 = db1.one_column("SELECT x FROM foo")?;
|
||||||
assert_eq!(42i64, the_answer?);
|
assert_eq!(42i64, the_answer);
|
||||||
child.join().unwrap();
|
child.join().unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user