mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 07:09:20 +08:00
Take IntoIterator rather than &[&ToSql]
(#312)
This commit is contained in:
parent
bd1756adef
commit
c4ae541eac
@ -309,6 +309,7 @@ impl<'a, 'b> Drop for Backup<'a, 'b> {
|
||||
mod test {
|
||||
use super::Backup;
|
||||
use std::time::Duration;
|
||||
use types::ToSql;
|
||||
use {Connection, DatabaseName};
|
||||
|
||||
#[test]
|
||||
@ -328,7 +329,7 @@ mod test {
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst
|
||||
.query_row("SELECT x FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT x FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(42, the_answer);
|
||||
|
||||
@ -342,7 +343,7 @@ mod test {
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst
|
||||
.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(42 + 43, the_answer);
|
||||
}
|
||||
@ -366,7 +367,7 @@ mod test {
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst
|
||||
.query_row("SELECT x FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT x FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(42, the_answer);
|
||||
|
||||
@ -382,7 +383,7 @@ mod test {
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst
|
||||
.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(42 + 43, the_answer);
|
||||
}
|
||||
@ -410,7 +411,7 @@ mod test {
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst
|
||||
.query_row("SELECT x FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT x FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(42, the_answer);
|
||||
|
||||
@ -429,7 +430,7 @@ mod test {
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst
|
||||
.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(42 + 43, the_answer);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
//! extern crate rusqlite;
|
||||
//!
|
||||
//! use rusqlite::blob::ZeroBlob;
|
||||
//! use rusqlite::types::ToSql;
|
||||
//! use rusqlite::{Connection, DatabaseName};
|
||||
//! use std::io::{Read, Seek, SeekFrom, Write};
|
||||
//!
|
||||
@ -25,8 +26,10 @@
|
||||
//! let db = Connection::open_in_memory().unwrap();
|
||||
//! db.execute_batch("CREATE TABLE test (content BLOB);")
|
||||
//! .unwrap();
|
||||
//! db.execute("INSERT INTO test (content) VALUES (ZEROBLOB(10))", &[])
|
||||
//! .unwrap();
|
||||
//! db.execute(
|
||||
//! "INSERT INTO test (content) VALUES (ZEROBLOB(10))",
|
||||
//! &[] as &[&ToSql],
|
||||
//! ).unwrap();
|
||||
//!
|
||||
//! let rowid = db.last_insert_rowid();
|
||||
//! let mut blob = db
|
||||
@ -44,7 +47,7 @@
|
||||
//! let bytes_read = blob.read(&mut buf[..]).unwrap();
|
||||
//! assert_eq!(bytes_read, 10); // note we read 10 bytes because the blob has size 10
|
||||
//!
|
||||
//! db.execute("INSERT INTO test (content) VALUES (?)", &[&ZeroBlob(64)])
|
||||
//! db.execute("INSERT INTO test (content) VALUES (?)", &[ZeroBlob(64)])
|
||||
//! .unwrap();
|
||||
//!
|
||||
//! // given a new row ID, we can reopen the blob on that row
|
||||
|
13
src/busy.rs
13
src/busy.rs
@ -14,7 +14,7 @@ impl Connection {
|
||||
///
|
||||
/// Calling this routine with an argument equal to zero turns off all busy
|
||||
/// handlers.
|
||||
//
|
||||
///
|
||||
/// There can only be a single busy handler for a particular database
|
||||
/// connection at any given moment. If another busy handler was defined
|
||||
/// (using `busy_handler`) prior to calling this routine, that other
|
||||
@ -81,6 +81,7 @@ mod test {
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use types::ToSql;
|
||||
use {Connection, Error, ErrorCode, TransactionBehavior};
|
||||
|
||||
#[test]
|
||||
@ -93,7 +94,11 @@ mod test {
|
||||
.transaction_with_behavior(TransactionBehavior::Exclusive)
|
||||
.unwrap();
|
||||
let db2 = Connection::open(&path).unwrap();
|
||||
let r = db2.query_row("PRAGMA schema_version", &[], |_| unreachable!());
|
||||
let r = db2.query_row(
|
||||
"PRAGMA schema_version",
|
||||
&[] as &[&ToSql],
|
||||
|_| unreachable!(),
|
||||
);
|
||||
match r.unwrap_err() {
|
||||
Error::SqliteFailure(err, _) => {
|
||||
assert_eq!(err.code, ErrorCode::DatabaseBusy);
|
||||
@ -125,7 +130,7 @@ mod test {
|
||||
|
||||
assert_eq!(tx.recv().unwrap(), 1);
|
||||
let _ = db2
|
||||
.query_row("PRAGMA schema_version", &[], |row| {
|
||||
.query_row("PRAGMA schema_version", &[] as &[&ToSql], |row| {
|
||||
row.get_checked::<_, i32>(0)
|
||||
}).expect("unexpected error");
|
||||
|
||||
@ -163,7 +168,7 @@ mod test {
|
||||
|
||||
assert_eq!(tx.recv().unwrap(), 1);
|
||||
let _ = db2
|
||||
.query_row("PRAGMA schema_version", &[], |row| {
|
||||
.query_row("PRAGMA schema_version", &[] as &[&ToSql], |row| {
|
||||
row.get_checked::<_, i32>(0)
|
||||
}).expect("unexpected error");
|
||||
assert_eq!(CALLED.load(Ordering::Relaxed), true);
|
||||
|
57
src/cache.rs
57
src/cache.rs
@ -17,13 +17,13 @@ impl Connection {
|
||||
/// fn insert_new_people(conn: &Connection) -> Result<()> {
|
||||
/// {
|
||||
/// let mut stmt = try!(conn.prepare_cached("INSERT INTO People (name) VALUES (?)"));
|
||||
/// try!(stmt.execute(&[&"Joe Smith"]));
|
||||
/// try!(stmt.execute(&["Joe Smith"]));
|
||||
/// }
|
||||
/// {
|
||||
/// // This will return the same underlying SQLite statement handle without
|
||||
/// // having to prepare it again.
|
||||
/// let mut stmt = try!(conn.prepare_cached("INSERT INTO People (name) VALUES (?)"));
|
||||
/// try!(stmt.execute(&[&"Bob Jones"]));
|
||||
/// try!(stmt.execute(&["Bob Jones"]));
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
@ -152,6 +152,7 @@ impl StatementCache {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::StatementCache;
|
||||
use types::ToSql;
|
||||
use Connection;
|
||||
|
||||
impl StatementCache {
|
||||
@ -180,14 +181,22 @@ mod test {
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(1, cache.len());
|
||||
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(1, cache.len());
|
||||
|
||||
@ -205,7 +214,11 @@ mod test {
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(1, cache.len());
|
||||
|
||||
@ -215,7 +228,11 @@ mod test {
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(0, cache.len());
|
||||
|
||||
@ -223,7 +240,11 @@ mod test {
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(1, cache.len());
|
||||
}
|
||||
@ -237,7 +258,11 @@ mod test {
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
stmt.discard();
|
||||
}
|
||||
assert_eq!(0, cache.len());
|
||||
@ -259,7 +284,7 @@ mod test {
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(
|
||||
1i32,
|
||||
stmt.query_map::<i32, _>(&[], |r| r.get(0))
|
||||
stmt.query_map::<i32, _, _>(&[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
@ -278,7 +303,7 @@ mod test {
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(
|
||||
(1i32, 2i32),
|
||||
stmt.query_map(&[], |r| (r.get(0), r.get(1)))
|
||||
stmt.query_map(&[] as &[&ToSql], |r| (r.get(0), r.get(1)))
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
@ -306,14 +331,22 @@ mod test {
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(1, cache.len());
|
||||
|
||||
{
|
||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||
assert_eq!(0, cache.len());
|
||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
||||
assert_eq!(
|
||||
0,
|
||||
stmt.query_row(&[] as &[&ToSql], |r| r.get::<_, i64>(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(1, cache.len());
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
//! extern crate regex;
|
||||
//!
|
||||
//! use regex::Regex;
|
||||
//! use rusqlite::types::ToSql;
|
||||
//! use rusqlite::{Connection, Error, Result};
|
||||
//! use std::collections::HashMap;
|
||||
//!
|
||||
@ -43,9 +44,11 @@
|
||||
//! add_regexp_function(&db).unwrap();
|
||||
//!
|
||||
//! let is_match: bool = db
|
||||
//! .query_row("SELECT regexp('[aeiou]*', 'aaaaeeeiii')", &[], |row| {
|
||||
//! row.get(0)
|
||||
//! }).unwrap();
|
||||
//! .query_row(
|
||||
//! "SELECT regexp('[aeiou]*', 'aaaaeeeiii')",
|
||||
//! &[] as &[&ToSql],
|
||||
//! |row| row.get(0),
|
||||
//! ).unwrap();
|
||||
//!
|
||||
//! assert!(is_match);
|
||||
//! }
|
||||
@ -209,6 +212,7 @@ impl Connection {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use rusqlite::types::ToSql;
|
||||
/// # use rusqlite::{Connection, Result};
|
||||
/// fn scalar_function_example(db: Connection) -> Result<()> {
|
||||
/// try!(db.create_scalar_function("halve", 1, true, |ctx| {
|
||||
@ -216,7 +220,7 @@ impl Connection {
|
||||
/// Ok(value / 2f64)
|
||||
/// }));
|
||||
///
|
||||
/// let six_halved: f64 = try!(db.query_row("SELECT halve(6)", &[], |r| r.get(0)));
|
||||
/// let six_halved: f64 = try!(db.query_row("SELECT halve(6)", &[] as &[&ToSql], |r| r.get(0)));
|
||||
/// assert_eq!(six_halved, 3f64);
|
||||
/// Ok(())
|
||||
/// }
|
||||
@ -478,6 +482,7 @@ mod test {
|
||||
use std::os::raw::c_double;
|
||||
|
||||
use functions::{Aggregate, Context};
|
||||
use types::ToSql;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
fn half(ctx: &Context) -> Result<c_double> {
|
||||
@ -490,7 +495,7 @@ mod test {
|
||||
fn test_function_half() {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
db.create_scalar_function("half", 1, true, half).unwrap();
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", &[], |r| r.get(0));
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", &[] as &[&ToSql], |r| r.get(0));
|
||||
|
||||
assert!((3f64 - result.unwrap()).abs() < EPSILON);
|
||||
}
|
||||
@ -499,11 +504,11 @@ mod test {
|
||||
fn test_remove_function() {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
db.create_scalar_function("half", 1, true, half).unwrap();
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", &[], |r| r.get(0));
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", &[] as &[&ToSql], |r| r.get(0));
|
||||
assert!((3f64 - result.unwrap()).abs() < EPSILON);
|
||||
|
||||
db.remove_function("half", 1).unwrap();
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", &[], |r| r.get(0));
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", &[] as &[&ToSql], |r| r.get(0));
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
@ -553,14 +558,17 @@ mod test {
|
||||
db.create_scalar_function("regexp", 2, true, regexp_with_auxilliary)
|
||||
.unwrap();
|
||||
|
||||
let result: Result<bool> =
|
||||
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", &[], |r| r.get(0));
|
||||
let result: Result<bool> = db.query_row(
|
||||
"SELECT regexp('l.s[aeiouy]', 'lisa')",
|
||||
&[] as &[&ToSql],
|
||||
|r| r.get(0),
|
||||
);
|
||||
|
||||
assert_eq!(true, result.unwrap());
|
||||
|
||||
let result: Result<i64> = db.query_row(
|
||||
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
||||
&[],
|
||||
&[] as &[&ToSql],
|
||||
|r| r.get(0),
|
||||
);
|
||||
|
||||
@ -603,14 +611,17 @@ mod test {
|
||||
Ok(regex.is_match(&text))
|
||||
}).unwrap();
|
||||
|
||||
let result: Result<bool> =
|
||||
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", &[], |r| r.get(0));
|
||||
let result: Result<bool> = db.query_row(
|
||||
"SELECT regexp('l.s[aeiouy]', 'lisa')",
|
||||
&[] as &[&ToSql],
|
||||
|r| r.get(0),
|
||||
);
|
||||
|
||||
assert_eq!(true, result.unwrap());
|
||||
|
||||
let result: Result<i64> = db.query_row(
|
||||
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
||||
&[],
|
||||
&[] as &[&ToSql],
|
||||
|r| r.get(0),
|
||||
);
|
||||
|
||||
@ -636,7 +647,7 @@ mod test {
|
||||
("onetwo", "SELECT my_concat('one', 'two')"),
|
||||
("abc", "SELECT my_concat('a', 'b', 'c')"),
|
||||
] {
|
||||
let result: String = db.query_row(query, &[], |r| r.get(0)).unwrap();
|
||||
let result: String = db.query_row(query, &[] as &[&ToSql], |r| r.get(0)).unwrap();
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
}
|
||||
@ -682,17 +693,21 @@ mod test {
|
||||
|
||||
// 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 result: Option<i64> = db.query_row(no_result, &[], |r| r.get(0)).unwrap();
|
||||
let result: Option<i64> = db
|
||||
.query_row(no_result, &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert!(result.is_none());
|
||||
|
||||
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)).unwrap();
|
||||
let result: i64 = db
|
||||
.query_row(single_sum, &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
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 \
|
||||
2, 1)";
|
||||
let result: (i64, i64) = db
|
||||
.query_row(dual_sum, &[], |r| (r.get(0), r.get(1)))
|
||||
.query_row(dual_sum, &[] as &[&ToSql], |r| (r.get(0), r.get(1)))
|
||||
.unwrap();
|
||||
assert_eq!((4, 2), result);
|
||||
}
|
||||
@ -705,11 +720,15 @@ mod test {
|
||||
|
||||
// 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 result: i64 = db.query_row(no_result, &[], |r| r.get(0)).unwrap();
|
||||
let result: i64 = db
|
||||
.query_row(no_result, &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(result, 0);
|
||||
|
||||
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)).unwrap();
|
||||
let result: i64 = db
|
||||
.query_row(single_sum, &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(2, result);
|
||||
}
|
||||
}
|
||||
|
178
src/lib.rs
178
src/lib.rs
@ -5,6 +5,7 @@
|
||||
//! extern crate rusqlite;
|
||||
//! extern crate time;
|
||||
//!
|
||||
//! use rusqlite::types::ToSql;
|
||||
//! use rusqlite::Connection;
|
||||
//! use time::Timespec;
|
||||
//!
|
||||
@ -26,7 +27,7 @@
|
||||
//! time_created TEXT NOT NULL,
|
||||
//! data BLOB
|
||||
//! )",
|
||||
//! &[],
|
||||
//! &[] as &[&ToSql],
|
||||
//! ).unwrap();
|
||||
//! let me = Person {
|
||||
//! id: 0,
|
||||
@ -37,14 +38,14 @@
|
||||
//! conn.execute(
|
||||
//! "INSERT INTO person (name, time_created, data)
|
||||
//! VALUES (?1, ?2, ?3)",
|
||||
//! &[&me.name, &me.time_created, &me.data],
|
||||
//! &[&me.name as &ToSql, &me.time_created, &me.data],
|
||||
//! ).unwrap();
|
||||
//!
|
||||
//! let mut stmt = conn
|
||||
//! .prepare("SELECT id, name, time_created, data FROM person")
|
||||
//! .unwrap();
|
||||
//! let person_iter = stmt
|
||||
//! .query_map(&[], |row| Person {
|
||||
//! .query_map(&[] as &[&ToSql], |row| Person {
|
||||
//! id: row.get(0),
|
||||
//! name: row.get(1),
|
||||
//! time_created: row.get(2),
|
||||
@ -306,7 +307,7 @@ impl Connection {
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Connection};
|
||||
/// fn update_rows(conn: &Connection) {
|
||||
/// match conn.execute("UPDATE foo SET bar = 'baz' WHERE qux = ?", &[&1i32]) {
|
||||
/// match conn.execute("UPDATE foo SET bar = 'baz' WHERE qux = ?", &[1i32]) {
|
||||
/// Ok(updated) => println!("{} rows were updated", updated),
|
||||
/// Err(err) => println!("update failed: {}", err),
|
||||
/// }
|
||||
@ -317,7 +318,11 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||
/// or if the underlying SQLite call fails.
|
||||
pub fn execute(&self, sql: &str, params: &[&ToSql]) -> Result<usize> {
|
||||
pub fn execute<P>(&self, sql: &str, params: P) -> Result<usize>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
{
|
||||
self.prepare(sql).and_then(|mut stmt| stmt.execute(params))
|
||||
}
|
||||
|
||||
@ -363,10 +368,11 @@ impl Connection {
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Result,Connection};
|
||||
/// # use rusqlite::types::ToSql;
|
||||
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
||||
/// conn.query_row(
|
||||
/// "SELECT value FROM preferences WHERE name='locale'",
|
||||
/// &[],
|
||||
/// &[] as &[&ToSql],
|
||||
/// |row| row.get(0),
|
||||
/// )
|
||||
/// }
|
||||
@ -379,8 +385,10 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||
/// or if the underlying SQLite call fails.
|
||||
pub fn query_row<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
|
||||
pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> Result<T>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
F: FnOnce(&Row) -> T,
|
||||
{
|
||||
let mut stmt = try!(self.prepare(sql));
|
||||
@ -416,10 +424,11 @@ impl Connection {
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Result,Connection};
|
||||
/// # use rusqlite::types::ToSql;
|
||||
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
||||
/// conn.query_row_and_then(
|
||||
/// "SELECT value FROM preferences WHERE name='locale'",
|
||||
/// &[],
|
||||
/// &[] as &[&ToSql],
|
||||
/// |row| row.get_checked(0),
|
||||
/// )
|
||||
/// }
|
||||
@ -432,13 +441,10 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||
/// or if the underlying SQLite call fails.
|
||||
pub fn query_row_and_then<T, E, F>(
|
||||
&self,
|
||||
sql: &str,
|
||||
params: &[&ToSql],
|
||||
f: F,
|
||||
) -> result::Result<T, E>
|
||||
pub fn query_row_and_then<T, E, P, F>(&self, sql: &str, params: P, f: F) -> result::Result<T, E>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
F: FnOnce(&Row) -> result::Result<T, E>,
|
||||
E: convert::From<Error>,
|
||||
{
|
||||
@ -448,37 +454,6 @@ impl Connection {
|
||||
rows.get_expected_row().map_err(E::from).and_then(|r| f(&r))
|
||||
}
|
||||
|
||||
/// Convenience method to execute a query that is expected to return a
|
||||
/// single row.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Result,Connection};
|
||||
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
||||
/// conn.query_row_safe(
|
||||
/// "SELECT value FROM preferences WHERE name='locale'",
|
||||
/// &[],
|
||||
/// |row| row.get(0),
|
||||
/// )
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// 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.
|
||||
#[deprecated(since = "0.1.0", note = "Use query_row instead")]
|
||||
pub fn query_row_safe<T, F>(&self, sql: &str, params: &[&ToSql], f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&Row) -> T,
|
||||
{
|
||||
self.query_row(sql, params, f)
|
||||
}
|
||||
|
||||
/// Prepare a SQL statement for execution.
|
||||
///
|
||||
/// ## Example
|
||||
@ -487,8 +462,8 @@ impl Connection {
|
||||
/// # use rusqlite::{Connection, Result};
|
||||
/// fn insert_new_people(conn: &Connection) -> Result<()> {
|
||||
/// let mut stmt = try!(conn.prepare("INSERT INTO People (name) VALUES (?)"));
|
||||
/// try!(stmt.execute(&[&"Joe Smith"]));
|
||||
/// try!(stmt.execute(&[&"Bob Jones"]));
|
||||
/// try!(stmt.execute(&["Joe Smith"]));
|
||||
/// try!(stmt.execute(&["Bob Jones"]));
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
@ -1089,14 +1064,14 @@ mod test {
|
||||
|
||||
// SELECT first makes sqlite lock with a shared lock
|
||||
let _ = tx1
|
||||
.query_row("SELECT x FROM foo LIMIT 1", &[], |_| ())
|
||||
.query_row("SELECT x FROM foo LIMIT 1", &[] as &[&ToSql], |_| ())
|
||||
.unwrap();
|
||||
let _ = tx2
|
||||
.query_row("SELECT x FROM foo LIMIT 1", &[], |_| ())
|
||||
.query_row("SELECT x FROM foo LIMIT 1", &[] as &[&ToSql], |_| ())
|
||||
.unwrap();
|
||||
|
||||
tx1.execute("INSERT INTO foo VALUES(?1)", &[&1]).unwrap();
|
||||
let _ = tx2.execute("INSERT INTO foo VALUES(?1)", &[&2]);
|
||||
tx1.execute("INSERT INTO foo VALUES(?1)", &[1]).unwrap();
|
||||
let _ = tx2.execute("INSERT INTO foo VALUES(?1)", &[2]);
|
||||
|
||||
let _ = tx1.commit();
|
||||
let _ = tx2.commit();
|
||||
@ -1126,7 +1101,8 @@ mod test {
|
||||
|
||||
let path_string = path.to_str().unwrap();
|
||||
let db = Connection::open(&path_string).unwrap();
|
||||
let the_answer: Result<i64> = db.query_row("SELECT x FROM foo", &[], |r| r.get(0));
|
||||
let the_answer: Result<i64> =
|
||||
db.query_row("SELECT x FROM foo", &[] as &[&ToSql], |r| r.get(0));
|
||||
|
||||
assert_eq!(42i64, the_answer.unwrap());
|
||||
}
|
||||
@ -1216,18 +1192,18 @@ mod test {
|
||||
|
||||
assert_eq!(
|
||||
1,
|
||||
db.execute("INSERT INTO foo(x) VALUES (?)", &[&1i32])
|
||||
db.execute("INSERT INTO foo(x) VALUES (?)", &[1i32])
|
||||
.unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
1,
|
||||
db.execute("INSERT INTO foo(x) VALUES (?)", &[&2i32])
|
||||
db.execute("INSERT INTO foo(x) VALUES (?)", &[2i32])
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
3i32,
|
||||
db.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
@ -1235,7 +1211,7 @@ mod test {
|
||||
#[test]
|
||||
fn test_execute_select() {
|
||||
let db = checked_memory_handle();
|
||||
let err = db.execute("SELECT 1 WHERE 1 < ?", &[&1i32]).unwrap_err();
|
||||
let err = db.execute("SELECT 1 WHERE 1 < ?", &[1i32]).unwrap_err();
|
||||
match err {
|
||||
Error::ExecuteReturnedResults => (),
|
||||
_ => panic!("Unexpected error: {}", err),
|
||||
@ -1262,18 +1238,18 @@ mod test {
|
||||
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
|
||||
|
||||
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)").unwrap();
|
||||
assert_eq!(insert_stmt.execute(&[&1i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[&2i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[&3i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[1i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[2i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[3i32]).unwrap(), 1);
|
||||
|
||||
assert_eq!(insert_stmt.execute(&[&"hello".to_string()]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[&"goodbye".to_string()]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[&types::Null]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&["hello".to_string()]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&["goodbye".to_string()]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[types::Null]).unwrap(), 1);
|
||||
|
||||
let mut update_stmt = db.prepare("UPDATE foo SET x=? WHERE x<?").unwrap();
|
||||
assert_eq!(update_stmt.execute(&[&3i32, &3i32]).unwrap(), 2);
|
||||
assert_eq!(update_stmt.execute(&[&3i32, &3i32]).unwrap(), 0);
|
||||
assert_eq!(update_stmt.execute(&[&8i32, &8i32]).unwrap(), 3);
|
||||
assert_eq!(update_stmt.execute(&[3i32, 3i32]).unwrap(), 2);
|
||||
assert_eq!(update_stmt.execute(&[3i32, 3i32]).unwrap(), 0);
|
||||
assert_eq!(update_stmt.execute(&[8i32, 8i32]).unwrap(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1282,15 +1258,15 @@ mod test {
|
||||
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
|
||||
|
||||
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)").unwrap();
|
||||
assert_eq!(insert_stmt.execute(&[&1i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[&2i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[&3i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[1i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[2i32]).unwrap(), 1);
|
||||
assert_eq!(insert_stmt.execute(&[3i32]).unwrap(), 1);
|
||||
|
||||
let mut query = db
|
||||
.prepare("SELECT x FROM foo WHERE x < ? ORDER BY x DESC")
|
||||
.unwrap();
|
||||
{
|
||||
let mut rows = query.query(&[&4i32]).unwrap();
|
||||
let mut rows = query.query(&[4i32]).unwrap();
|
||||
let mut v = Vec::<i32>::new();
|
||||
|
||||
while let Some(row) = rows.next() {
|
||||
@ -1301,7 +1277,7 @@ mod test {
|
||||
}
|
||||
|
||||
{
|
||||
let mut rows = query.query(&[&3i32]).unwrap();
|
||||
let mut rows = query.query(&[3i32]).unwrap();
|
||||
let mut v = Vec::<i32>::new();
|
||||
|
||||
while let Some(row) = rows.next() {
|
||||
@ -1325,8 +1301,10 @@ mod test {
|
||||
db.execute_batch(sql).unwrap();
|
||||
|
||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||
let results: Result<Vec<String>> =
|
||||
query.query_map(&[], |row| row.get(1)).unwrap().collect();
|
||||
let results: Result<Vec<String>> = query
|
||||
.query_map(&[] as &[&ToSql], |row| row.get(1))
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
assert_eq!(results.unwrap().concat(), "hello, world!");
|
||||
}
|
||||
@ -1345,17 +1323,21 @@ mod test {
|
||||
|
||||
assert_eq!(
|
||||
10i64,
|
||||
db.query_row::<i64, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
db.query_row::<i64, _, _>("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let result: Result<i64> = db.query_row("SELECT x FROM foo WHERE x > 5", &[], |r| r.get(0));
|
||||
let result: Result<i64> =
|
||||
db.query_row("SELECT x FROM foo WHERE x > 5", &[] as &[&ToSql], |r| {
|
||||
r.get(0)
|
||||
});
|
||||
match result.unwrap_err() {
|
||||
Error::QueryReturnedNoRows => (),
|
||||
err => panic!("Unexpected error {}", err),
|
||||
}
|
||||
|
||||
let bad_query_result = db.query_row("NOT A PROPER QUERY; test123", &[], |_| ());
|
||||
let bad_query_result =
|
||||
db.query_row("NOT A PROPER QUERY; test123", &[] as &[&ToSql], |_| ());
|
||||
|
||||
assert!(bad_query_result.is_err());
|
||||
}
|
||||
@ -1366,12 +1348,12 @@ mod test {
|
||||
|
||||
assert_eq!(
|
||||
"memory",
|
||||
db.query_row::<String, _>("PRAGMA journal_mode", &[], |r| r.get(0))
|
||||
db.query_row::<String, _, _>("PRAGMA journal_mode", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
"off",
|
||||
db.query_row::<String, _>("PRAGMA journal_mode=off", &[], |r| r.get(0))
|
||||
db.query_row::<String, _, _>("PRAGMA journal_mode=off", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
@ -1396,7 +1378,7 @@ mod test {
|
||||
|
||||
let mut stmt = db.prepare("INSERT INTO foo DEFAULT VALUES").unwrap();
|
||||
for _ in 0i32..9 {
|
||||
stmt.execute(&[]).unwrap();
|
||||
stmt.execute(&[] as &[&ToSql]).unwrap();
|
||||
}
|
||||
assert_eq!(db.last_insert_rowid(), 10);
|
||||
}
|
||||
@ -1418,7 +1400,7 @@ mod test {
|
||||
let mut stmt = db.prepare("PRAGMA schema_version").unwrap();
|
||||
assert!(!db.is_busy());
|
||||
{
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
let mut rows = stmt.query(&[] as &[&ToSql]).unwrap();
|
||||
assert!(!db.is_busy());
|
||||
let row = rows.next();
|
||||
assert!(db.is_busy());
|
||||
@ -1450,7 +1432,7 @@ mod test {
|
||||
let db = checked_memory_handle();
|
||||
db.execute_batch("CREATE TABLE foo(x NOT NULL)").unwrap();
|
||||
|
||||
let result = db.execute("INSERT INTO foo (x) VALUES (NULL)", &[]);
|
||||
let result = db.execute("INSERT INTO foo (x) VALUES (NULL)", &[] as &[&ToSql]);
|
||||
assert!(result.is_err());
|
||||
|
||||
match result.unwrap_err() {
|
||||
@ -1526,7 +1508,7 @@ mod test {
|
||||
|
||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||
let results: Result<Vec<String>> = query
|
||||
.query_and_then(&[], |row| row.get_checked(1))
|
||||
.query_and_then(&[] as &[&ToSql], |row| row.get_checked(1))
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
@ -1547,7 +1529,7 @@ mod test {
|
||||
|
||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||
let bad_type: Result<Vec<f64>> = query
|
||||
.query_and_then(&[], |row| row.get_checked(1))
|
||||
.query_and_then(&[] as &[&ToSql], |row| row.get_checked(1))
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
@ -1557,7 +1539,7 @@ mod test {
|
||||
}
|
||||
|
||||
let bad_idx: Result<Vec<String>> = query
|
||||
.query_and_then(&[], |row| row.get_checked(3))
|
||||
.query_and_then(&[] as &[&ToSql], |row| row.get_checked(3))
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
@ -1581,8 +1563,9 @@ mod test {
|
||||
|
||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||
let results: CustomResult<Vec<String>> = query
|
||||
.query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite))
|
||||
.unwrap()
|
||||
.query_and_then(&[] as &[&ToSql], |row| {
|
||||
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||
}).unwrap()
|
||||
.collect();
|
||||
|
||||
assert_eq!(results.unwrap().concat(), "hello, world!");
|
||||
@ -1602,8 +1585,9 @@ mod test {
|
||||
|
||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||
let bad_type: CustomResult<Vec<f64>> = query
|
||||
.query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite))
|
||||
.unwrap()
|
||||
.query_and_then(&[] as &[&ToSql], |row| {
|
||||
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||
}).unwrap()
|
||||
.collect();
|
||||
|
||||
match bad_type.unwrap_err() {
|
||||
@ -1612,8 +1596,9 @@ mod test {
|
||||
}
|
||||
|
||||
let bad_idx: CustomResult<Vec<String>> = query
|
||||
.query_and_then(&[], |row| row.get_checked(3).map_err(CustomError::Sqlite))
|
||||
.unwrap()
|
||||
.query_and_then(&[] as &[&ToSql], |row| {
|
||||
row.get_checked(3).map_err(CustomError::Sqlite)
|
||||
}).unwrap()
|
||||
.collect();
|
||||
|
||||
match bad_idx.unwrap_err() {
|
||||
@ -1622,7 +1607,7 @@ mod test {
|
||||
}
|
||||
|
||||
let non_sqlite_err: CustomResult<Vec<String>> = query
|
||||
.query_and_then(&[], |_| Err(CustomError::SomeError))
|
||||
.query_and_then(&[] as &[&ToSql], |_| Err(CustomError::SomeError))
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
@ -1642,7 +1627,8 @@ mod test {
|
||||
db.execute_batch(sql).unwrap();
|
||||
|
||||
let query = "SELECT x, y FROM foo ORDER BY x DESC";
|
||||
let results: CustomResult<String> = db.query_row_and_then(query, &[], |row| {
|
||||
let results: CustomResult<String> =
|
||||
db.query_row_and_then(query, &[] as &[&ToSql], |row| {
|
||||
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||
});
|
||||
|
||||
@ -1659,7 +1645,8 @@ mod test {
|
||||
db.execute_batch(sql).unwrap();
|
||||
|
||||
let query = "SELECT x, y FROM foo ORDER BY x DESC";
|
||||
let bad_type: CustomResult<f64> = db.query_row_and_then(query, &[], |row| {
|
||||
let bad_type: CustomResult<f64> =
|
||||
db.query_row_and_then(query, &[] as &[&ToSql], |row| {
|
||||
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||
});
|
||||
|
||||
@ -1668,7 +1655,8 @@ mod test {
|
||||
err => panic!("Unexpected error {}", err),
|
||||
}
|
||||
|
||||
let bad_idx: CustomResult<String> = db.query_row_and_then(query, &[], |row| {
|
||||
let bad_idx: CustomResult<String> =
|
||||
db.query_row_and_then(query, &[] as &[&ToSql], |row| {
|
||||
row.get_checked(3).map_err(CustomError::Sqlite)
|
||||
});
|
||||
|
||||
@ -1678,7 +1666,7 @@ mod test {
|
||||
}
|
||||
|
||||
let non_sqlite_err: CustomResult<String> =
|
||||
db.query_row_and_then(query, &[], |_| Err(CustomError::SomeError));
|
||||
db.query_row_and_then(query, &[] as &[&ToSql], |_| Err(CustomError::SomeError));
|
||||
|
||||
match non_sqlite_err.unwrap_err() {
|
||||
CustomError::SomeError => (),
|
||||
@ -1695,7 +1683,7 @@ mod test {
|
||||
END;";
|
||||
db.execute_batch(sql).unwrap();
|
||||
|
||||
db.query_row("SELECT * FROM foo", &[], |r| {
|
||||
db.query_row("SELECT * FROM foo", &[] as &[&ToSql], |r| {
|
||||
assert_eq!(2, r.column_count())
|
||||
}).unwrap();
|
||||
}
|
||||
|
117
src/statement.rs
117
src/statement.rs
@ -1,4 +1,5 @@
|
||||
use std::ffi::CStr;
|
||||
use std::iter::IntoIterator;
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
#[cfg(feature = "array")]
|
||||
use std::rc::Rc;
|
||||
@ -71,8 +72,8 @@ impl<'conn> Statement<'conn> {
|
||||
/// fn update_rows(conn: &Connection) -> Result<()> {
|
||||
/// let mut stmt = try!(conn.prepare("UPDATE foo SET bar = 'baz' WHERE qux = ?"));
|
||||
///
|
||||
/// try!(stmt.execute(&[&1i32]));
|
||||
/// try!(stmt.execute(&[&2i32]));
|
||||
/// try!(stmt.execute(&[1i32]));
|
||||
/// try!(stmt.execute(&[2i32]));
|
||||
///
|
||||
/// Ok(())
|
||||
/// }
|
||||
@ -83,7 +84,11 @@ impl<'conn> Statement<'conn> {
|
||||
/// Will return `Err` if binding parameters fails, the executed statement
|
||||
/// returns rows (in which case `query` should be used instead), or the
|
||||
/// underling SQLite call fails.
|
||||
pub fn execute(&mut self, params: &[&ToSql]) -> Result<usize> {
|
||||
pub fn execute<P>(&mut self, params: P) -> Result<usize>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
{
|
||||
try!(self.bind_parameters(params));
|
||||
self.execute_with_bound_parameters()
|
||||
}
|
||||
@ -130,7 +135,11 @@ impl<'conn> Statement<'conn> {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return `Err` if no row is inserted or many rows are inserted.
|
||||
pub fn insert(&mut self, params: &[&ToSql]) -> Result<i64> {
|
||||
pub fn insert<P>(&mut self, params: P) -> Result<i64>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
{
|
||||
let changes = try!(self.execute(params));
|
||||
match changes {
|
||||
1 => Ok(self.conn.last_insert_rowid()),
|
||||
@ -149,9 +158,10 @@ impl<'conn> Statement<'conn> {
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Connection, Result};
|
||||
/// # use rusqlite::types::ToSql;
|
||||
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
||||
/// let mut stmt = try!(conn.prepare("SELECT name FROM people"));
|
||||
/// let mut rows = try!(stmt.query(&[]));
|
||||
/// let mut rows = try!(stmt.query(&[] as &[&ToSql]));
|
||||
///
|
||||
/// let mut names = Vec::new();
|
||||
/// while let Some(result_row) = rows.next() {
|
||||
@ -166,7 +176,11 @@ impl<'conn> Statement<'conn> {
|
||||
/// ## Failure
|
||||
///
|
||||
/// Will return `Err` if binding parameters fails.
|
||||
pub fn query<'a>(&'a mut self, params: &[&ToSql]) -> Result<Rows<'a>> {
|
||||
pub fn query<'a, P>(&'a mut self, params: P) -> Result<Rows<'a>>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
{
|
||||
try!(self.check_readonly());
|
||||
try!(self.bind_parameters(params));
|
||||
Ok(Rows::new(self))
|
||||
@ -208,9 +222,10 @@ impl<'conn> Statement<'conn> {
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use rusqlite::{Connection, Result};
|
||||
/// # use rusqlite::types::ToSql;
|
||||
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
||||
/// let mut stmt = try!(conn.prepare("SELECT name FROM people"));
|
||||
/// let rows = try!(stmt.query_map(&[], |row| row.get(0)));
|
||||
/// let rows = try!(stmt.query_map(&[] as &[&ToSql], |row| row.get(0)));
|
||||
///
|
||||
/// let mut names = Vec::new();
|
||||
/// for name_result in rows {
|
||||
@ -224,8 +239,10 @@ impl<'conn> Statement<'conn> {
|
||||
/// ## Failure
|
||||
///
|
||||
/// Will return `Err` if binding parameters fails.
|
||||
pub fn query_map<'a, T, F>(&'a mut self, params: &[&ToSql], f: F) -> Result<MappedRows<'a, F>>
|
||||
pub fn query_map<'a, T, P, F>(&'a mut self, params: P, f: F) -> Result<MappedRows<'a, F>>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
F: FnMut(&Row) -> T,
|
||||
{
|
||||
let rows = self.query(params)?;
|
||||
@ -278,12 +295,14 @@ impl<'conn> Statement<'conn> {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return `Err` if binding parameters fails.
|
||||
pub fn query_and_then<'a, T, E, F>(
|
||||
pub fn query_and_then<'a, T, E, P, F>(
|
||||
&'a mut self,
|
||||
params: &[&ToSql],
|
||||
params: P,
|
||||
f: F,
|
||||
) -> Result<AndThenRows<'a, F>>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
E: convert::From<Error>,
|
||||
F: FnMut(&Row) -> result::Result<T, E>,
|
||||
{
|
||||
@ -344,7 +363,11 @@ impl<'conn> Statement<'conn> {
|
||||
|
||||
/// Return `true` if a query in the SQL statement it executes returns one
|
||||
/// or more rows and `false` if the SQL returns an empty set.
|
||||
pub fn exists(&mut self, params: &[&ToSql]) -> Result<bool> {
|
||||
pub fn exists<P>(&mut self, params: P) -> Result<bool>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
{
|
||||
let mut rows = try!(self.query(params));
|
||||
let exists = {
|
||||
match rows.next() {
|
||||
@ -364,8 +387,10 @@ impl<'conn> Statement<'conn> {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return `Err` if the underlying SQLite call fails.
|
||||
pub fn query_row<T, F>(&mut self, params: &[&ToSql], f: F) -> Result<T>
|
||||
pub fn query_row<T, P, F>(&mut self, params: P, f: F) -> Result<T>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
F: FnOnce(&Row) -> T,
|
||||
{
|
||||
let mut rows = try!(self.query(params));
|
||||
@ -396,18 +421,25 @@ impl<'conn> Statement<'conn> {
|
||||
Ok(self.stmt.bind_parameter_index(&c_name))
|
||||
}
|
||||
|
||||
fn bind_parameters(&mut self, params: &[&ToSql]) -> Result<()> {
|
||||
assert_eq!(
|
||||
params.len(),
|
||||
self.stmt.bind_parameter_count(),
|
||||
"incorrect number of parameters to query(): expected {}, got {}",
|
||||
self.stmt.bind_parameter_count(),
|
||||
params.len()
|
||||
);
|
||||
|
||||
for (i, p) in params.iter().enumerate() {
|
||||
try!(self.bind_parameter(*p, i + 1));
|
||||
fn bind_parameters<P>(&mut self, params: P) -> Result<()>
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
{
|
||||
let expected = self.stmt.bind_parameter_count();
|
||||
let mut index = 0;
|
||||
for p in params.into_iter() {
|
||||
index += 1; // The leftmost SQL parameter has an index of 1.
|
||||
if index > expected {
|
||||
break;
|
||||
}
|
||||
try!(self.bind_parameter(&p, index));
|
||||
}
|
||||
assert_eq!(
|
||||
index, expected,
|
||||
"incorrect number of parameters: expected {}, got {}",
|
||||
expected, index
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -643,6 +675,7 @@ impl<'conn> Statement<'conn> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use types::ToSql;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
#[test]
|
||||
@ -780,8 +813,11 @@ mod test {
|
||||
stmt.execute_named(&[(":x", &"one")]).unwrap();
|
||||
|
||||
let result: Option<String> = db
|
||||
.query_row("SELECT y FROM test WHERE x = 'one'", &[], |row| row.get(0))
|
||||
.unwrap();
|
||||
.query_row(
|
||||
"SELECT y FROM test WHERE x = 'one'",
|
||||
&[] as &[&ToSql],
|
||||
|row| row.get(0),
|
||||
).unwrap();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
@ -798,8 +834,11 @@ mod test {
|
||||
stmt.execute_named(&[(":y", &"two")]).unwrap();
|
||||
|
||||
let result: String = db
|
||||
.query_row("SELECT x FROM test WHERE y = 'two'", &[], |row| row.get(0))
|
||||
.unwrap();
|
||||
.query_row(
|
||||
"SELECT x FROM test WHERE y = 'two'",
|
||||
&[] as &[&ToSql],
|
||||
|row| row.get(0),
|
||||
).unwrap();
|
||||
assert_eq!(result, "one");
|
||||
}
|
||||
|
||||
@ -811,16 +850,16 @@ mod test {
|
||||
let mut stmt = db
|
||||
.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?)")
|
||||
.unwrap();
|
||||
assert_eq!(stmt.insert(&[&1i32]).unwrap(), 1);
|
||||
assert_eq!(stmt.insert(&[&2i32]).unwrap(), 2);
|
||||
match stmt.insert(&[&1i32]).unwrap_err() {
|
||||
assert_eq!(stmt.insert(&[1i32]).unwrap(), 1);
|
||||
assert_eq!(stmt.insert(&[2i32]).unwrap(), 2);
|
||||
match stmt.insert(&[1i32]).unwrap_err() {
|
||||
Error::StatementChangedRows(0) => (),
|
||||
err => panic!("Unexpected error {}", err),
|
||||
}
|
||||
let mut multi = db
|
||||
.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4")
|
||||
.unwrap();
|
||||
match multi.insert(&[]).unwrap_err() {
|
||||
match multi.insert(&[] as &[&ToSql]).unwrap_err() {
|
||||
Error::StatementChangedRows(2) => (),
|
||||
err => panic!("Unexpected error {}", err),
|
||||
}
|
||||
@ -840,14 +879,14 @@ mod test {
|
||||
assert_eq!(
|
||||
db.prepare("INSERT INTO foo VALUES (10)")
|
||||
.unwrap()
|
||||
.insert(&[])
|
||||
.insert(&[] as &[&ToSql])
|
||||
.unwrap(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
db.prepare("INSERT INTO bar VALUES (10)")
|
||||
.unwrap()
|
||||
.insert(&[])
|
||||
.insert(&[] as &[&ToSql])
|
||||
.unwrap(),
|
||||
1
|
||||
);
|
||||
@ -863,9 +902,9 @@ mod test {
|
||||
END;";
|
||||
db.execute_batch(sql).unwrap();
|
||||
let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?").unwrap();
|
||||
assert!(stmt.exists(&[&1i32]).unwrap());
|
||||
assert!(stmt.exists(&[&2i32]).unwrap());
|
||||
assert!(!stmt.exists(&[&0i32]).unwrap());
|
||||
assert!(stmt.exists(&[1i32]).unwrap());
|
||||
assert!(stmt.exists(&[2i32]).unwrap());
|
||||
assert!(!stmt.exists(&[0i32]).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -878,7 +917,7 @@ mod test {
|
||||
END;";
|
||||
db.execute_batch(sql).unwrap();
|
||||
let mut stmt = db.prepare("SELECT y FROM foo WHERE x = ?").unwrap();
|
||||
let y: Result<i64> = stmt.query_row(&[&1i32], |r| r.get(0));
|
||||
let y: Result<i64> = stmt.query_row(&[1i32], |r| r.get(0));
|
||||
assert_eq!(3i64, y.unwrap());
|
||||
}
|
||||
|
||||
@ -891,7 +930,7 @@ mod test {
|
||||
END;";
|
||||
db.execute_batch(sql).unwrap();
|
||||
let mut stmt = db.prepare("SELECT y FROM foo").unwrap();
|
||||
let y: Result<i64> = stmt.query_row(&[], |r| r.get("y"));
|
||||
let y: Result<i64> = stmt.query_row(&[] as &[&ToSql], |r| r.get("y"));
|
||||
assert_eq!(3i64, y.unwrap());
|
||||
}
|
||||
|
||||
@ -904,7 +943,7 @@ mod test {
|
||||
END;";
|
||||
db.execute_batch(sql).unwrap();
|
||||
let mut stmt = db.prepare("SELECT y as Y FROM foo").unwrap();
|
||||
let y: Result<i64> = stmt.query_row(&[], |r| r.get("y"));
|
||||
let y: Result<i64> = stmt.query_row(&[] as &[&ToSql], |r| r.get("y"));
|
||||
assert_eq!(3i64, y.unwrap());
|
||||
}
|
||||
|
||||
|
@ -139,13 +139,13 @@ mod test {
|
||||
let mut db = Connection::open_in_memory().unwrap();
|
||||
db.trace(Some(tracer));
|
||||
{
|
||||
let _ = db.query_row("SELECT ?", &[&1i32], |_| {});
|
||||
let _ = db.query_row("SELECT ?", &[&"hello"], |_| {});
|
||||
let _ = db.query_row("SELECT ?", &[1i32], |_| {});
|
||||
let _ = db.query_row("SELECT ?", &["hello"], |_| {});
|
||||
}
|
||||
db.trace(None);
|
||||
{
|
||||
let _ = db.query_row("SELECT ?", &[&2i32], |_| {});
|
||||
let _ = db.query_row("SELECT ?", &[&"goodbye"], |_| {});
|
||||
let _ = db.query_row("SELECT ?", &[2i32], |_| {});
|
||||
let _ = db.query_row("SELECT ?", &["goodbye"], |_| {});
|
||||
}
|
||||
|
||||
let traced_stmts = TRACED_STMTS.lock().unwrap();
|
||||
|
@ -419,6 +419,7 @@ impl Connection {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::DropBehavior;
|
||||
use types::ToSql;
|
||||
use Connection;
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
@ -444,7 +445,7 @@ mod test {
|
||||
let tx = db.transaction().unwrap();
|
||||
assert_eq!(
|
||||
2i32,
|
||||
tx.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
@ -473,7 +474,7 @@ mod test {
|
||||
let tx = db.transaction().unwrap();
|
||||
assert_eq!(
|
||||
6i32,
|
||||
tx.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
@ -565,12 +566,12 @@ mod test {
|
||||
}
|
||||
|
||||
fn insert(x: i32, conn: &Connection) {
|
||||
conn.execute("INSERT INTO foo VALUES(?)", &[&x]).unwrap();
|
||||
conn.execute("INSERT INTO foo VALUES(?)", &[x]).unwrap();
|
||||
}
|
||||
|
||||
fn assert_current_sum(x: i32, conn: &Connection) {
|
||||
let i = conn
|
||||
.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
||||
.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(x, i);
|
||||
}
|
||||
|
@ -132,6 +132,7 @@ mod test {
|
||||
use super::chrono::{
|
||||
DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc,
|
||||
};
|
||||
use types::ToSql;
|
||||
use Connection;
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
@ -149,11 +150,11 @@ mod test {
|
||||
.unwrap();
|
||||
|
||||
let s: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!("2016-02-23", s);
|
||||
let t: NaiveDate = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(date, t);
|
||||
}
|
||||
@ -166,11 +167,11 @@ mod test {
|
||||
.unwrap();
|
||||
|
||||
let s: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!("23:56:04", s);
|
||||
let v: NaiveTime = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(time, v);
|
||||
}
|
||||
@ -186,17 +187,18 @@ mod test {
|
||||
.unwrap();
|
||||
|
||||
let s: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!("2016-02-23T23:56:04", s);
|
||||
let v: NaiveDateTime = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(dt, v);
|
||||
|
||||
db.execute("UPDATE foo set b = datetime(t)", &[]).unwrap(); // "YYYY-MM-DD HH:MM:SS"
|
||||
db.execute("UPDATE foo set b = datetime(t)", &[] as &[&ToSql])
|
||||
.unwrap(); // "YYYY-MM-DD HH:MM:SS"
|
||||
let hms: NaiveDateTime = db
|
||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT b FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(dt, hms);
|
||||
}
|
||||
@ -213,28 +215,33 @@ mod test {
|
||||
.unwrap();
|
||||
|
||||
let s: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!("2016-02-23T23:56:04.789+00:00", s);
|
||||
|
||||
let v1: DateTime<Utc> = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(utc, v1);
|
||||
|
||||
let v2: DateTime<Utc> = db
|
||||
.query_row("SELECT '2016-02-23 23:56:04.789'", &[], |r| r.get(0))
|
||||
.unwrap();
|
||||
.query_row("SELECT '2016-02-23 23:56:04.789'", &[] as &[&ToSql], |r| {
|
||||
r.get(0)
|
||||
}).unwrap();
|
||||
assert_eq!(utc, v2);
|
||||
|
||||
let v3: DateTime<Utc> = db
|
||||
.query_row("SELECT '2016-02-23 23:56:04'", &[], |r| r.get(0))
|
||||
.unwrap();
|
||||
.query_row("SELECT '2016-02-23 23:56:04'", &[] as &[&ToSql], |r| {
|
||||
r.get(0)
|
||||
}).unwrap();
|
||||
assert_eq!(utc - Duration::milliseconds(789), v3);
|
||||
|
||||
let v4: DateTime<Utc> = db
|
||||
.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", &[], |r| r.get(0))
|
||||
.unwrap();
|
||||
.query_row(
|
||||
"SELECT '2016-02-23 23:56:04.789+00:00'",
|
||||
&[] as &[&ToSql],
|
||||
|r| r.get(0),
|
||||
).unwrap();
|
||||
assert_eq!(utc, v4);
|
||||
}
|
||||
|
||||
@ -251,12 +258,12 @@ mod test {
|
||||
|
||||
// Stored string should be in UTC
|
||||
let s: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert!(s.ends_with("+00:00"));
|
||||
|
||||
let v: DateTime<Local> = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(local, v);
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ mod value_ref;
|
||||
/// # use rusqlite::types::{Null};
|
||||
/// fn main() {}
|
||||
/// fn insert_null(conn: &Connection) -> Result<usize> {
|
||||
/// conn.execute("INSERT INTO people (name) VALUES (?)", &[&Null])
|
||||
/// conn.execute("INSERT INTO people (name) VALUES (?)", &[Null])
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Copy, Clone)]
|
||||
@ -115,6 +115,7 @@ mod test {
|
||||
use super::Value;
|
||||
use std::f64::EPSILON;
|
||||
use std::os::raw::{c_double, c_int};
|
||||
use types::ToSql;
|
||||
use Connection;
|
||||
use Error;
|
||||
|
||||
@ -134,7 +135,7 @@ mod test {
|
||||
.unwrap();
|
||||
|
||||
let v: Vec<u8> = db
|
||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT b FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(v, v1234);
|
||||
}
|
||||
@ -148,7 +149,7 @@ mod test {
|
||||
.unwrap();
|
||||
|
||||
let v: Vec<u8> = db
|
||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT b FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(v, empty);
|
||||
}
|
||||
@ -161,7 +162,7 @@ mod test {
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s]).unwrap();
|
||||
|
||||
let from: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(from, s);
|
||||
}
|
||||
@ -171,11 +172,11 @@ mod test {
|
||||
let db = checked_memory_handle();
|
||||
|
||||
let s = "hello, world!";
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s.to_owned()])
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[s.to_owned()])
|
||||
.unwrap();
|
||||
|
||||
let from: String = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(from, s);
|
||||
}
|
||||
@ -184,12 +185,12 @@ mod test {
|
||||
fn test_value() {
|
||||
let db = checked_memory_handle();
|
||||
|
||||
db.execute("INSERT INTO foo(i) VALUES (?)", &[&Value::Integer(10)])
|
||||
db.execute("INSERT INTO foo(i) VALUES (?)", &[Value::Integer(10)])
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
10i64,
|
||||
db.query_row::<i64, _>("SELECT i FROM foo", &[], |r| r.get(0))
|
||||
db.query_row::<i64, _, _>("SELECT i FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
@ -207,7 +208,7 @@ mod test {
|
||||
let mut stmt = db
|
||||
.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC")
|
||||
.unwrap();
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
let mut rows = stmt.query(&[] as &[&ToSql]).unwrap();
|
||||
|
||||
{
|
||||
let row1 = rows.next().unwrap().unwrap();
|
||||
@ -239,11 +240,11 @@ mod test {
|
||||
|
||||
db.execute(
|
||||
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
||||
&[],
|
||||
&[] as &[&ToSql],
|
||||
).unwrap();
|
||||
|
||||
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
let mut rows = stmt.query(&[] as &[&ToSql]).unwrap();
|
||||
|
||||
let row = rows.next().unwrap().unwrap();
|
||||
|
||||
@ -354,11 +355,11 @@ mod test {
|
||||
|
||||
db.execute(
|
||||
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
||||
&[],
|
||||
&[] as &[&ToSql],
|
||||
).unwrap();
|
||||
|
||||
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
|
||||
let mut rows = stmt.query(&[]).unwrap();
|
||||
let mut rows = stmt.query(&[] as &[&ToSql]).unwrap();
|
||||
|
||||
let row = rows.next().unwrap().unwrap();
|
||||
assert_eq!(
|
||||
|
@ -27,6 +27,7 @@ impl FromSql for Value {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::serde_json;
|
||||
use types::ToSql;
|
||||
use Connection;
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
@ -44,15 +45,15 @@ mod test {
|
||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||
db.execute(
|
||||
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
||||
&[&data, &json.as_bytes()],
|
||||
&[&data as &ToSql, &json.as_bytes()],
|
||||
).unwrap();
|
||||
|
||||
let t: serde_json::Value = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(data, t);
|
||||
let b: serde_json::Value = db
|
||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT b FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(data, b);
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ impl FromSql for time::Timespec {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::time;
|
||||
use types::ToSql;
|
||||
use Connection;
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
@ -58,10 +59,10 @@ mod test {
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
|
||||
|
||||
let from: time::Timespec = db
|
||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
||||
.query_row("SELECT t FROM foo", &[] as &[&ToSql], |r| r.get(0))
|
||||
.unwrap();
|
||||
|
||||
db.execute("DELETE FROM foo", &[]).unwrap();
|
||||
db.execute("DELETE FROM foo", &[] as &[&ToSql]).unwrap();
|
||||
|
||||
assert_eq!(from, ts);
|
||||
}
|
||||
|
@ -114,10 +114,10 @@ to_sql_self!(f64);
|
||||
|
||||
impl<'a, T: ?Sized> ToSql for &'a T
|
||||
where
|
||||
&'a T: Into<ToSqlOutput<'a>>,
|
||||
T: ToSql,
|
||||
{
|
||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||
Ok((*self).into())
|
||||
(*self).to_sql()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,6 +102,7 @@ mod test {
|
||||
use std::sync::mpsc::sync_channel;
|
||||
use std::thread;
|
||||
use std::time;
|
||||
use types::ToSql;
|
||||
use {Connection, OpenFlags, Result, Transaction, TransactionBehavior};
|
||||
|
||||
#[test]
|
||||
@ -121,7 +122,8 @@ mod test {
|
||||
tx2.commit().unwrap();
|
||||
});
|
||||
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: Result<i64> =
|
||||
db1.query_row("SELECT x FROM foo", &[] as &[&ToSql], |r| r.get(0));
|
||||
assert_eq!(42i64, the_answer.unwrap());
|
||||
child.join().unwrap();
|
||||
}
|
||||
|
@ -342,6 +342,7 @@ impl From<csv::Error> for Error {
|
||||
mod test {
|
||||
use vtab::csvtab;
|
||||
use {Connection, Result};
|
||||
use types::ToSql;
|
||||
|
||||
#[test]
|
||||
fn test_csv_module() {
|
||||
@ -358,7 +359,7 @@ mod test {
|
||||
}
|
||||
|
||||
let ids: Result<Vec<i32>> = s
|
||||
.query_map(&[], |row| row.get::<_, i32>(0))
|
||||
.query_map(&[] as &[&ToSql], |row| row.get::<_, i32>(0))
|
||||
.unwrap()
|
||||
.collect();
|
||||
let sum = ids.unwrap().iter().fold(0, |acc, &id| acc + id);
|
||||
@ -381,7 +382,7 @@ mod test {
|
||||
v1.rowid < v2.rowid",
|
||||
).unwrap();
|
||||
|
||||
let mut rows = s.query(&[]).unwrap();
|
||||
let mut rows = s.query(&[] as &[&ToSql]).unwrap();
|
||||
let row = rows.next().unwrap().unwrap();
|
||||
assert_eq!(row.get::<_, i32>(0), 2);
|
||||
}
|
||||
|
@ -266,6 +266,7 @@ mod test {
|
||||
use ffi;
|
||||
use vtab::series;
|
||||
use Connection;
|
||||
use types::ToSql;
|
||||
|
||||
#[test]
|
||||
fn test_series_module() {
|
||||
@ -279,7 +280,7 @@ mod test {
|
||||
|
||||
let mut s = db.prepare("SELECT * FROM generate_series(0,20,5)").unwrap();
|
||||
|
||||
let series = s.query_map(&[], |row| row.get::<_, i32>(0)).unwrap();
|
||||
let series = s.query_map(&[] as &[&ToSql], |row| row.get::<_, i32>(0)).unwrap();
|
||||
|
||||
let mut expected = 0;
|
||||
for value in series {
|
||||
|
@ -6,6 +6,7 @@ extern crate rusqlite;
|
||||
#[cfg(feature = "vtab")]
|
||||
#[test]
|
||||
fn test_dummy_module() {
|
||||
use rusqlite::types::ToSql;
|
||||
use rusqlite::vtab::{
|
||||
eponymous_only_module, sqlite3_vtab, sqlite3_vtab_cursor, Context, IndexInfo, VTab,
|
||||
VTabConnection, VTabCursor, Values,
|
||||
@ -96,6 +97,8 @@ fn test_dummy_module() {
|
||||
|
||||
let mut s = db.prepare("SELECT * FROM dummy()").unwrap();
|
||||
|
||||
let dummy = s.query_row(&[], |row| row.get::<_, i32>(0)).unwrap();
|
||||
let dummy = s
|
||||
.query_row(&[] as &[&ToSql], |row| row.get::<_, i32>(0))
|
||||
.unwrap();
|
||||
assert_eq!(1, dummy);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user