mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 19:41:37 +08:00
Merge pull request #404 from gwenn/into-iterator
Take IntoIterator rather than `&[&ToSql]` (#312)
This commit is contained in:
commit
ef45312125
@ -309,7 +309,7 @@ impl<'a, 'b> Drop for Backup<'a, 'b> {
|
|||||||
mod test {
|
mod test {
|
||||||
use super::Backup;
|
use super::Backup;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use {Connection, DatabaseName};
|
use {Connection, DatabaseName, NO_PARAMS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_backup() {
|
fn test_backup() {
|
||||||
@ -328,7 +328,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst
|
let the_answer: i64 = dst
|
||||||
.query_row("SELECT x FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT x FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(42, the_answer);
|
assert_eq!(42, the_answer);
|
||||||
|
|
||||||
@ -342,7 +342,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst
|
let the_answer: i64 = dst
|
||||||
.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(42 + 43, the_answer);
|
assert_eq!(42 + 43, the_answer);
|
||||||
}
|
}
|
||||||
@ -366,7 +366,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst
|
let the_answer: i64 = dst
|
||||||
.query_row("SELECT x FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT x FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(42, the_answer);
|
assert_eq!(42, the_answer);
|
||||||
|
|
||||||
@ -382,7 +382,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst
|
let the_answer: i64 = dst
|
||||||
.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(42 + 43, the_answer);
|
assert_eq!(42 + 43, the_answer);
|
||||||
}
|
}
|
||||||
@ -410,7 +410,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst
|
let the_answer: i64 = dst
|
||||||
.query_row("SELECT x FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT x FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(42, the_answer);
|
assert_eq!(42, the_answer);
|
||||||
|
|
||||||
@ -429,7 +429,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let the_answer: i64 = dst
|
let the_answer: i64 = dst
|
||||||
.query_row("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(42 + 43, the_answer);
|
assert_eq!(42 + 43, the_answer);
|
||||||
}
|
}
|
||||||
|
10
src/blob.rs
10
src/blob.rs
@ -18,15 +18,17 @@
|
|||||||
//! extern crate rusqlite;
|
//! extern crate rusqlite;
|
||||||
//!
|
//!
|
||||||
//! use rusqlite::blob::ZeroBlob;
|
//! use rusqlite::blob::ZeroBlob;
|
||||||
//! use rusqlite::{Connection, DatabaseName};
|
//! use rusqlite::{Connection, DatabaseName, NO_PARAMS};
|
||||||
//! use std::io::{Read, Seek, SeekFrom, Write};
|
//! use std::io::{Read, Seek, SeekFrom, Write};
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! let db = Connection::open_in_memory().unwrap();
|
//! let db = Connection::open_in_memory().unwrap();
|
||||||
//! db.execute_batch("CREATE TABLE test (content BLOB);")
|
//! db.execute_batch("CREATE TABLE test (content BLOB);")
|
||||||
//! .unwrap();
|
//! .unwrap();
|
||||||
//! db.execute("INSERT INTO test (content) VALUES (ZEROBLOB(10))", &[])
|
//! db.execute(
|
||||||
//! .unwrap();
|
//! "INSERT INTO test (content) VALUES (ZEROBLOB(10))",
|
||||||
|
//! NO_PARAMS,
|
||||||
|
//! ).unwrap();
|
||||||
//!
|
//!
|
||||||
//! let rowid = db.last_insert_rowid();
|
//! let rowid = db.last_insert_rowid();
|
||||||
//! let mut blob = db
|
//! let mut blob = db
|
||||||
@ -44,7 +46,7 @@
|
|||||||
//! let bytes_read = blob.read(&mut buf[..]).unwrap();
|
//! let bytes_read = blob.read(&mut buf[..]).unwrap();
|
||||||
//! assert_eq!(bytes_read, 10); // note we read 10 bytes because the blob has size 10
|
//! 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();
|
//! .unwrap();
|
||||||
//!
|
//!
|
||||||
//! // given a new row ID, we can reopen the blob on that row
|
//! // given a new row ID, we can reopen the blob on that row
|
||||||
|
10
src/busy.rs
10
src/busy.rs
@ -14,7 +14,7 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Calling this routine with an argument equal to zero turns off all busy
|
/// Calling this routine with an argument equal to zero turns off all busy
|
||||||
/// handlers.
|
/// handlers.
|
||||||
//
|
///
|
||||||
/// There can only be a single busy handler for a particular database
|
/// There can only be a single busy handler for a particular database
|
||||||
/// connection at any given moment. If another busy handler was defined
|
/// connection at any given moment. If another busy handler was defined
|
||||||
/// (using `busy_handler`) prior to calling this routine, that other
|
/// (using `busy_handler`) prior to calling this routine, that other
|
||||||
@ -81,7 +81,7 @@ mod test {
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use {Connection, Error, ErrorCode, TransactionBehavior};
|
use {Connection, Error, ErrorCode, TransactionBehavior, NO_PARAMS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_busy() {
|
fn test_default_busy() {
|
||||||
@ -93,7 +93,7 @@ mod test {
|
|||||||
.transaction_with_behavior(TransactionBehavior::Exclusive)
|
.transaction_with_behavior(TransactionBehavior::Exclusive)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let db2 = Connection::open(&path).unwrap();
|
let db2 = Connection::open(&path).unwrap();
|
||||||
let r = db2.query_row("PRAGMA schema_version", &[], |_| unreachable!());
|
let r = db2.query_row("PRAGMA schema_version", NO_PARAMS, |_| unreachable!());
|
||||||
match r.unwrap_err() {
|
match r.unwrap_err() {
|
||||||
Error::SqliteFailure(err, _) => {
|
Error::SqliteFailure(err, _) => {
|
||||||
assert_eq!(err.code, ErrorCode::DatabaseBusy);
|
assert_eq!(err.code, ErrorCode::DatabaseBusy);
|
||||||
@ -125,7 +125,7 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(tx.recv().unwrap(), 1);
|
assert_eq!(tx.recv().unwrap(), 1);
|
||||||
let _ = db2
|
let _ = db2
|
||||||
.query_row("PRAGMA schema_version", &[], |row| {
|
.query_row("PRAGMA schema_version", NO_PARAMS, |row| {
|
||||||
row.get_checked::<_, i32>(0)
|
row.get_checked::<_, i32>(0)
|
||||||
}).expect("unexpected error");
|
}).expect("unexpected error");
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(tx.recv().unwrap(), 1);
|
assert_eq!(tx.recv().unwrap(), 1);
|
||||||
let _ = db2
|
let _ = db2
|
||||||
.query_row("PRAGMA schema_version", &[], |row| {
|
.query_row("PRAGMA schema_version", NO_PARAMS, |row| {
|
||||||
row.get_checked::<_, i32>(0)
|
row.get_checked::<_, i32>(0)
|
||||||
}).expect("unexpected error");
|
}).expect("unexpected error");
|
||||||
assert_eq!(CALLED.load(Ordering::Relaxed), true);
|
assert_eq!(CALLED.load(Ordering::Relaxed), true);
|
||||||
|
50
src/cache.rs
50
src/cache.rs
@ -17,13 +17,13 @@ impl Connection {
|
|||||||
/// fn insert_new_people(conn: &Connection) -> Result<()> {
|
/// fn insert_new_people(conn: &Connection) -> Result<()> {
|
||||||
/// {
|
/// {
|
||||||
/// let mut stmt = try!(conn.prepare_cached("INSERT INTO People (name) VALUES (?)"));
|
/// 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
|
/// // This will return the same underlying SQLite statement handle without
|
||||||
/// // having to prepare it again.
|
/// // having to prepare it again.
|
||||||
/// let mut stmt = try!(conn.prepare_cached("INSERT INTO People (name) VALUES (?)"));
|
/// let mut stmt = try!(conn.prepare_cached("INSERT INTO People (name) VALUES (?)"));
|
||||||
/// try!(stmt.execute(&[&"Bob Jones"]));
|
/// try!(stmt.execute(&["Bob Jones"]));
|
||||||
/// }
|
/// }
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
@ -152,7 +152,7 @@ impl StatementCache {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::StatementCache;
|
use super::StatementCache;
|
||||||
use Connection;
|
use {Connection, NO_PARAMS};
|
||||||
|
|
||||||
impl StatementCache {
|
impl StatementCache {
|
||||||
fn clear(&self) {
|
fn clear(&self) {
|
||||||
@ -180,14 +180,20 @@ mod test {
|
|||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(1, cache.len());
|
assert_eq!(1, cache.len());
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(1, cache.len());
|
assert_eq!(1, cache.len());
|
||||||
|
|
||||||
@ -205,7 +211,10 @@ mod test {
|
|||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(1, cache.len());
|
assert_eq!(1, cache.len());
|
||||||
|
|
||||||
@ -215,7 +224,10 @@ mod test {
|
|||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
|
|
||||||
@ -223,7 +235,10 @@ mod test {
|
|||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(1, cache.len());
|
assert_eq!(1, cache.len());
|
||||||
}
|
}
|
||||||
@ -237,7 +252,10 @@ mod test {
|
|||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
stmt.discard();
|
stmt.discard();
|
||||||
}
|
}
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
@ -259,7 +277,7 @@ mod test {
|
|||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
1i32,
|
1i32,
|
||||||
stmt.query_map::<i32, _>(&[], |r| r.get(0))
|
stmt.query_map::<i32, _, _>(NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -278,7 +296,7 @@ mod test {
|
|||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(1i32, 2i32),
|
(1i32, 2i32),
|
||||||
stmt.query_map(&[], |r| (r.get(0), r.get(1)))
|
stmt.query_map(NO_PARAMS, |r| (r.get(0), r.get(1)))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.next()
|
.next()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -306,14 +324,20 @@ mod test {
|
|||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(1, cache.len());
|
assert_eq!(1, cache.len());
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut stmt = db.prepare_cached(sql).unwrap();
|
let mut stmt = db.prepare_cached(sql).unwrap();
|
||||||
assert_eq!(0, cache.len());
|
assert_eq!(0, cache.len());
|
||||||
assert_eq!(0, stmt.query_row(&[], |r| r.get::<_, i64>(0)).unwrap());
|
assert_eq!(
|
||||||
|
0,
|
||||||
|
stmt.query_row(NO_PARAMS, |r| r.get::<_, i64>(0)).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
assert_eq!(1, cache.len());
|
assert_eq!(1, cache.len());
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
//! extern crate regex;
|
//! extern crate regex;
|
||||||
//!
|
//!
|
||||||
//! use regex::Regex;
|
//! use regex::Regex;
|
||||||
//! use rusqlite::{Connection, Error, Result};
|
//! use rusqlite::{Connection, Error, Result, NO_PARAMS};
|
||||||
//! use std::collections::HashMap;
|
//! use std::collections::HashMap;
|
||||||
//!
|
//!
|
||||||
//! fn add_regexp_function(db: &Connection) -> Result<()> {
|
//! fn add_regexp_function(db: &Connection) -> Result<()> {
|
||||||
@ -43,9 +43,11 @@
|
|||||||
//! add_regexp_function(&db).unwrap();
|
//! add_regexp_function(&db).unwrap();
|
||||||
//!
|
//!
|
||||||
//! let is_match: bool = db
|
//! let is_match: bool = db
|
||||||
//! .query_row("SELECT regexp('[aeiou]*', 'aaaaeeeiii')", &[], |row| {
|
//! .query_row(
|
||||||
//! row.get(0)
|
//! "SELECT regexp('[aeiou]*', 'aaaaeeeiii')",
|
||||||
//! }).unwrap();
|
//! NO_PARAMS,
|
||||||
|
//! |row| row.get(0),
|
||||||
|
//! ).unwrap();
|
||||||
//!
|
//!
|
||||||
//! assert!(is_match);
|
//! assert!(is_match);
|
||||||
//! }
|
//! }
|
||||||
@ -209,14 +211,14 @@ impl Connection {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use rusqlite::{Connection, Result};
|
/// # use rusqlite::{Connection, Result, NO_PARAMS};
|
||||||
/// fn scalar_function_example(db: Connection) -> Result<()> {
|
/// fn scalar_function_example(db: Connection) -> Result<()> {
|
||||||
/// try!(db.create_scalar_function("halve", 1, true, |ctx| {
|
/// try!(db.create_scalar_function("halve", 1, true, |ctx| {
|
||||||
/// let value = try!(ctx.get::<f64>(0));
|
/// let value = try!(ctx.get::<f64>(0));
|
||||||
/// Ok(value / 2f64)
|
/// 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)", NO_PARAMS, |r| r.get(0)));
|
||||||
/// assert_eq!(six_halved, 3f64);
|
/// assert_eq!(six_halved, 3f64);
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
@ -478,7 +480,7 @@ mod test {
|
|||||||
use std::os::raw::c_double;
|
use std::os::raw::c_double;
|
||||||
|
|
||||||
use functions::{Aggregate, Context};
|
use functions::{Aggregate, Context};
|
||||||
use {Connection, Error, Result};
|
use {Connection, Error, Result, NO_PARAMS};
|
||||||
|
|
||||||
fn half(ctx: &Context) -> Result<c_double> {
|
fn half(ctx: &Context) -> Result<c_double> {
|
||||||
assert!(ctx.len() == 1, "called with unexpected number of arguments");
|
assert!(ctx.len() == 1, "called with unexpected number of arguments");
|
||||||
@ -490,7 +492,7 @@ mod test {
|
|||||||
fn test_function_half() {
|
fn test_function_half() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
db.create_scalar_function("half", 1, true, half).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)", NO_PARAMS, |r| r.get(0));
|
||||||
|
|
||||||
assert!((3f64 - result.unwrap()).abs() < EPSILON);
|
assert!((3f64 - result.unwrap()).abs() < EPSILON);
|
||||||
}
|
}
|
||||||
@ -499,11 +501,11 @@ mod test {
|
|||||||
fn test_remove_function() {
|
fn test_remove_function() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
db.create_scalar_function("half", 1, true, half).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)", NO_PARAMS, |r| r.get(0));
|
||||||
assert!((3f64 - result.unwrap()).abs() < EPSILON);
|
assert!((3f64 - result.unwrap()).abs() < EPSILON);
|
||||||
|
|
||||||
db.remove_function("half", 1).unwrap();
|
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)", NO_PARAMS, |r| r.get(0));
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -554,13 +556,15 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let result: Result<bool> =
|
let result: Result<bool> =
|
||||||
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", &[], |r| r.get(0));
|
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", NO_PARAMS, |r| {
|
||||||
|
r.get(0)
|
||||||
|
});
|
||||||
|
|
||||||
assert_eq!(true, result.unwrap());
|
assert_eq!(true, result.unwrap());
|
||||||
|
|
||||||
let result: Result<i64> = db.query_row(
|
let result: Result<i64> = db.query_row(
|
||||||
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
||||||
&[],
|
NO_PARAMS,
|
||||||
|r| r.get(0),
|
|r| r.get(0),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -604,13 +608,15 @@ mod test {
|
|||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|
||||||
let result: Result<bool> =
|
let result: Result<bool> =
|
||||||
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", &[], |r| r.get(0));
|
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", NO_PARAMS, |r| {
|
||||||
|
r.get(0)
|
||||||
|
});
|
||||||
|
|
||||||
assert_eq!(true, result.unwrap());
|
assert_eq!(true, result.unwrap());
|
||||||
|
|
||||||
let result: Result<i64> = db.query_row(
|
let result: Result<i64> = db.query_row(
|
||||||
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
||||||
&[],
|
NO_PARAMS,
|
||||||
|r| r.get(0),
|
|r| r.get(0),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -636,7 +642,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)).unwrap();
|
let result: String = db.query_row(query, NO_PARAMS, |r| r.get(0)).unwrap();
|
||||||
assert_eq!(expected, result);
|
assert_eq!(expected, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -682,17 +688,17 @@ 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)).unwrap();
|
let result: Option<i64> = db.query_row(no_result, NO_PARAMS, |r| r.get(0)).unwrap();
|
||||||
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)).unwrap();
|
let result: i64 = db.query_row(single_sum, NO_PARAMS, |r| r.get(0)).unwrap();
|
||||||
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 \
|
||||||
2, 1)";
|
2, 1)";
|
||||||
let result: (i64, i64) = db
|
let result: (i64, i64) = db
|
||||||
.query_row(dual_sum, &[], |r| (r.get(0), r.get(1)))
|
.query_row(dual_sum, NO_PARAMS, |r| (r.get(0), r.get(1)))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!((4, 2), result);
|
assert_eq!((4, 2), result);
|
||||||
}
|
}
|
||||||
@ -705,11 +711,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)).unwrap();
|
let result: i64 = db.query_row(no_result, NO_PARAMS, |r| r.get(0)).unwrap();
|
||||||
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)).unwrap();
|
let result: i64 = db.query_row(single_sum, NO_PARAMS, |r| r.get(0)).unwrap();
|
||||||
assert_eq!(2, result);
|
assert_eq!(2, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
177
src/lib.rs
177
src/lib.rs
@ -5,7 +5,8 @@
|
|||||||
//! extern crate rusqlite;
|
//! extern crate rusqlite;
|
||||||
//! extern crate time;
|
//! extern crate time;
|
||||||
//!
|
//!
|
||||||
//! use rusqlite::Connection;
|
//! use rusqlite::types::ToSql;
|
||||||
|
//! use rusqlite::{Connection, NO_PARAMS};
|
||||||
//! use time::Timespec;
|
//! use time::Timespec;
|
||||||
//!
|
//!
|
||||||
//! #[derive(Debug)]
|
//! #[derive(Debug)]
|
||||||
@ -26,7 +27,7 @@
|
|||||||
//! time_created TEXT NOT NULL,
|
//! time_created TEXT NOT NULL,
|
||||||
//! data BLOB
|
//! data BLOB
|
||||||
//! )",
|
//! )",
|
||||||
//! &[],
|
//! NO_PARAMS,
|
||||||
//! ).unwrap();
|
//! ).unwrap();
|
||||||
//! let me = Person {
|
//! let me = Person {
|
||||||
//! id: 0,
|
//! id: 0,
|
||||||
@ -37,14 +38,14 @@
|
|||||||
//! conn.execute(
|
//! conn.execute(
|
||||||
//! "INSERT INTO person (name, time_created, data)
|
//! "INSERT INTO person (name, time_created, data)
|
||||||
//! VALUES (?1, ?2, ?3)",
|
//! VALUES (?1, ?2, ?3)",
|
||||||
//! &[&me.name, &me.time_created, &me.data],
|
//! &[&me.name as &ToSql, &me.time_created, &me.data],
|
||||||
//! ).unwrap();
|
//! ).unwrap();
|
||||||
//!
|
//!
|
||||||
//! let mut stmt = conn
|
//! let mut stmt = conn
|
||||||
//! .prepare("SELECT id, name, time_created, data FROM person")
|
//! .prepare("SELECT id, name, time_created, data FROM person")
|
||||||
//! .unwrap();
|
//! .unwrap();
|
||||||
//! let person_iter = stmt
|
//! let person_iter = stmt
|
||||||
//! .query_map(&[], |row| Person {
|
//! .query_map(NO_PARAMS, |row| Person {
|
||||||
//! id: row.get(0),
|
//! id: row.get(0),
|
||||||
//! name: row.get(1),
|
//! name: row.get(1),
|
||||||
//! time_created: row.get(2),
|
//! time_created: row.get(2),
|
||||||
@ -139,6 +140,8 @@ pub mod vtab;
|
|||||||
|
|
||||||
// Number of cached prepared statements we'll hold on to.
|
// Number of cached prepared statements we'll hold on to.
|
||||||
const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16;
|
const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16;
|
||||||
|
/// To be used when your statement has no [parameter](https://sqlite.org/lang_expr.html#varparam).
|
||||||
|
pub const NO_PARAMS: &'static [&'static ToSql] = &[];
|
||||||
|
|
||||||
/// Old name for `Result`. `SqliteResult` is deprecated.
|
/// Old name for `Result`. `SqliteResult` is deprecated.
|
||||||
#[deprecated(since = "0.6.0", note = "Use Result instead")]
|
#[deprecated(since = "0.6.0", note = "Use Result instead")]
|
||||||
@ -306,7 +309,7 @@ impl Connection {
|
|||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use rusqlite::{Connection};
|
/// # use rusqlite::{Connection};
|
||||||
/// fn update_rows(conn: &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),
|
/// Ok(updated) => println!("{} rows were updated", updated),
|
||||||
/// Err(err) => println!("update failed: {}", err),
|
/// Err(err) => println!("update failed: {}", err),
|
||||||
/// }
|
/// }
|
||||||
@ -317,7 +320,11 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||||
/// or if the underlying SQLite call fails.
|
/// 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))
|
self.prepare(sql).and_then(|mut stmt| stmt.execute(params))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,11 +369,11 @@ impl Connection {
|
|||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use rusqlite::{Result,Connection};
|
/// # use rusqlite::{Result,Connection, NO_PARAMS};
|
||||||
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
||||||
/// conn.query_row(
|
/// conn.query_row(
|
||||||
/// "SELECT value FROM preferences WHERE name='locale'",
|
/// "SELECT value FROM preferences WHERE name='locale'",
|
||||||
/// &[],
|
/// NO_PARAMS,
|
||||||
/// |row| row.get(0),
|
/// |row| row.get(0),
|
||||||
/// )
|
/// )
|
||||||
/// }
|
/// }
|
||||||
@ -379,8 +386,10 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||||
/// or if the underlying SQLite call fails.
|
/// 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
|
where
|
||||||
|
P: IntoIterator,
|
||||||
|
P::Item: ToSql,
|
||||||
F: FnOnce(&Row) -> T,
|
F: FnOnce(&Row) -> T,
|
||||||
{
|
{
|
||||||
let mut stmt = try!(self.prepare(sql));
|
let mut stmt = try!(self.prepare(sql));
|
||||||
@ -415,11 +424,11 @@ impl Connection {
|
|||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use rusqlite::{Result,Connection};
|
/// # use rusqlite::{Result,Connection, NO_PARAMS};
|
||||||
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
/// fn preferred_locale(conn: &Connection) -> Result<String> {
|
||||||
/// conn.query_row_and_then(
|
/// conn.query_row_and_then(
|
||||||
/// "SELECT value FROM preferences WHERE name='locale'",
|
/// "SELECT value FROM preferences WHERE name='locale'",
|
||||||
/// &[],
|
/// NO_PARAMS,
|
||||||
/// |row| row.get_checked(0),
|
/// |row| row.get_checked(0),
|
||||||
/// )
|
/// )
|
||||||
/// }
|
/// }
|
||||||
@ -432,13 +441,10 @@ impl Connection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||||
/// or if the underlying SQLite call fails.
|
/// or if the underlying SQLite call fails.
|
||||||
pub fn query_row_and_then<T, E, F>(
|
pub fn query_row_and_then<T, E, P, F>(&self, sql: &str, params: P, f: F) -> result::Result<T, E>
|
||||||
&self,
|
|
||||||
sql: &str,
|
|
||||||
params: &[&ToSql],
|
|
||||||
f: F,
|
|
||||||
) -> result::Result<T, E>
|
|
||||||
where
|
where
|
||||||
|
P: IntoIterator,
|
||||||
|
P::Item: ToSql,
|
||||||
F: FnOnce(&Row) -> result::Result<T, E>,
|
F: FnOnce(&Row) -> result::Result<T, E>,
|
||||||
E: convert::From<Error>,
|
E: convert::From<Error>,
|
||||||
{
|
{
|
||||||
@ -448,37 +454,6 @@ impl Connection {
|
|||||||
rows.get_expected_row().map_err(E::from).and_then(|r| f(&r))
|
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.
|
/// Prepare a SQL statement for execution.
|
||||||
///
|
///
|
||||||
/// ## Example
|
/// ## Example
|
||||||
@ -487,8 +462,8 @@ impl Connection {
|
|||||||
/// # use rusqlite::{Connection, Result};
|
/// # use rusqlite::{Connection, Result};
|
||||||
/// fn insert_new_people(conn: &Connection) -> Result<()> {
|
/// fn insert_new_people(conn: &Connection) -> Result<()> {
|
||||||
/// let mut stmt = try!(conn.prepare("INSERT INTO People (name) VALUES (?)"));
|
/// let mut stmt = try!(conn.prepare("INSERT INTO People (name) VALUES (?)"));
|
||||||
/// try!(stmt.execute(&[&"Joe Smith"]));
|
/// try!(stmt.execute(&["Joe Smith"]));
|
||||||
/// try!(stmt.execute(&[&"Bob Jones"]));
|
/// try!(stmt.execute(&["Bob Jones"]));
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -1089,14 +1064,14 @@ mod test {
|
|||||||
|
|
||||||
// SELECT first makes sqlite lock with a shared lock
|
// SELECT first makes sqlite lock with a shared lock
|
||||||
let _ = tx1
|
let _ = tx1
|
||||||
.query_row("SELECT x FROM foo LIMIT 1", &[], |_| ())
|
.query_row("SELECT x FROM foo LIMIT 1", NO_PARAMS, |_| ())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let _ = tx2
|
let _ = tx2
|
||||||
.query_row("SELECT x FROM foo LIMIT 1", &[], |_| ())
|
.query_row("SELECT x FROM foo LIMIT 1", NO_PARAMS, |_| ())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
tx1.execute("INSERT INTO foo VALUES(?1)", &[&1]).unwrap();
|
tx1.execute("INSERT INTO foo VALUES(?1)", &[1]).unwrap();
|
||||||
let _ = tx2.execute("INSERT INTO foo VALUES(?1)", &[&2]);
|
let _ = tx2.execute("INSERT INTO foo VALUES(?1)", &[2]);
|
||||||
|
|
||||||
let _ = tx1.commit();
|
let _ = tx1.commit();
|
||||||
let _ = tx2.commit();
|
let _ = tx2.commit();
|
||||||
@ -1126,7 +1101,7 @@ mod test {
|
|||||||
|
|
||||||
let path_string = path.to_str().unwrap();
|
let path_string = path.to_str().unwrap();
|
||||||
let db = Connection::open(&path_string).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", NO_PARAMS, |r| r.get(0));
|
||||||
|
|
||||||
assert_eq!(42i64, the_answer.unwrap());
|
assert_eq!(42i64, the_answer.unwrap());
|
||||||
}
|
}
|
||||||
@ -1216,18 +1191,18 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
1,
|
1,
|
||||||
db.execute("INSERT INTO foo(x) VALUES (?)", &[&1i32])
|
db.execute("INSERT INTO foo(x) VALUES (?)", &[1i32])
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
1,
|
1,
|
||||||
db.execute("INSERT INTO foo(x) VALUES (?)", &[&2i32])
|
db.execute("INSERT INTO foo(x) VALUES (?)", &[2i32])
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
3i32,
|
3i32,
|
||||||
db.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1235,7 +1210,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_execute_select() {
|
fn test_execute_select() {
|
||||||
let db = checked_memory_handle();
|
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 {
|
match err {
|
||||||
Error::ExecuteReturnedResults => (),
|
Error::ExecuteReturnedResults => (),
|
||||||
_ => panic!("Unexpected error: {}", err),
|
_ => panic!("Unexpected error: {}", err),
|
||||||
@ -1262,18 +1237,18 @@ mod test {
|
|||||||
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
|
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
|
||||||
|
|
||||||
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)").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(&[1i32]).unwrap(), 1);
|
||||||
assert_eq!(insert_stmt.execute(&[&2i32]).unwrap(), 1);
|
assert_eq!(insert_stmt.execute(&[2i32]).unwrap(), 1);
|
||||||
assert_eq!(insert_stmt.execute(&[&3i32]).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(&["hello".to_string()]).unwrap(), 1);
|
||||||
assert_eq!(insert_stmt.execute(&[&"goodbye".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(&[types::Null]).unwrap(), 1);
|
||||||
|
|
||||||
let mut update_stmt = db.prepare("UPDATE foo SET x=? WHERE x<?").unwrap();
|
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(), 2);
|
||||||
assert_eq!(update_stmt.execute(&[&3i32, &3i32]).unwrap(), 0);
|
assert_eq!(update_stmt.execute(&[3i32, 3i32]).unwrap(), 0);
|
||||||
assert_eq!(update_stmt.execute(&[&8i32, &8i32]).unwrap(), 3);
|
assert_eq!(update_stmt.execute(&[8i32, 8i32]).unwrap(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1282,15 +1257,15 @@ mod test {
|
|||||||
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
|
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
|
||||||
|
|
||||||
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)").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(&[1i32]).unwrap(), 1);
|
||||||
assert_eq!(insert_stmt.execute(&[&2i32]).unwrap(), 1);
|
assert_eq!(insert_stmt.execute(&[2i32]).unwrap(), 1);
|
||||||
assert_eq!(insert_stmt.execute(&[&3i32]).unwrap(), 1);
|
assert_eq!(insert_stmt.execute(&[3i32]).unwrap(), 1);
|
||||||
|
|
||||||
let mut query = db
|
let mut query = db
|
||||||
.prepare("SELECT x FROM foo WHERE x < ? ORDER BY x DESC")
|
.prepare("SELECT x FROM foo WHERE x < ? ORDER BY x DESC")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
{
|
{
|
||||||
let mut rows = query.query(&[&4i32]).unwrap();
|
let mut rows = query.query(&[4i32]).unwrap();
|
||||||
let mut v = Vec::<i32>::new();
|
let mut v = Vec::<i32>::new();
|
||||||
|
|
||||||
while let Some(row) = rows.next() {
|
while let Some(row) = rows.next() {
|
||||||
@ -1301,7 +1276,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut rows = query.query(&[&3i32]).unwrap();
|
let mut rows = query.query(&[3i32]).unwrap();
|
||||||
let mut v = Vec::<i32>::new();
|
let mut v = Vec::<i32>::new();
|
||||||
|
|
||||||
while let Some(row) = rows.next() {
|
while let Some(row) = rows.next() {
|
||||||
@ -1325,8 +1300,10 @@ mod test {
|
|||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
let results: Result<Vec<String>> =
|
let results: Result<Vec<String>> = query
|
||||||
query.query_map(&[], |row| row.get(1)).unwrap().collect();
|
.query_map(NO_PARAMS, |row| row.get(1))
|
||||||
|
.unwrap()
|
||||||
|
.collect();
|
||||||
|
|
||||||
assert_eq!(results.unwrap().concat(), "hello, world!");
|
assert_eq!(results.unwrap().concat(), "hello, world!");
|
||||||
}
|
}
|
||||||
@ -1345,17 +1322,18 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
10i64,
|
10i64,
|
||||||
db.query_row::<i64, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
db.query_row::<i64, _, _>("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.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", NO_PARAMS, |r| r.get(0));
|
||||||
match result.unwrap_err() {
|
match result.unwrap_err() {
|
||||||
Error::QueryReturnedNoRows => (),
|
Error::QueryReturnedNoRows => (),
|
||||||
err => panic!("Unexpected error {}", err),
|
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", NO_PARAMS, |_| ());
|
||||||
|
|
||||||
assert!(bad_query_result.is_err());
|
assert!(bad_query_result.is_err());
|
||||||
}
|
}
|
||||||
@ -1366,12 +1344,12 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"memory",
|
"memory",
|
||||||
db.query_row::<String, _>("PRAGMA journal_mode", &[], |r| r.get(0))
|
db.query_row::<String, _, _>("PRAGMA journal_mode", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"off",
|
"off",
|
||||||
db.query_row::<String, _>("PRAGMA journal_mode=off", &[], |r| r.get(0))
|
db.query_row::<String, _, _>("PRAGMA journal_mode=off", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1396,7 +1374,7 @@ mod test {
|
|||||||
|
|
||||||
let mut stmt = db.prepare("INSERT INTO foo DEFAULT VALUES").unwrap();
|
let mut stmt = db.prepare("INSERT INTO foo DEFAULT VALUES").unwrap();
|
||||||
for _ in 0i32..9 {
|
for _ in 0i32..9 {
|
||||||
stmt.execute(&[]).unwrap();
|
stmt.execute(NO_PARAMS).unwrap();
|
||||||
}
|
}
|
||||||
assert_eq!(db.last_insert_rowid(), 10);
|
assert_eq!(db.last_insert_rowid(), 10);
|
||||||
}
|
}
|
||||||
@ -1418,7 +1396,7 @@ mod test {
|
|||||||
let mut stmt = db.prepare("PRAGMA schema_version").unwrap();
|
let mut stmt = db.prepare("PRAGMA schema_version").unwrap();
|
||||||
assert!(!db.is_busy());
|
assert!(!db.is_busy());
|
||||||
{
|
{
|
||||||
let mut rows = stmt.query(&[]).unwrap();
|
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
||||||
assert!(!db.is_busy());
|
assert!(!db.is_busy());
|
||||||
let row = rows.next();
|
let row = rows.next();
|
||||||
assert!(db.is_busy());
|
assert!(db.is_busy());
|
||||||
@ -1450,7 +1428,7 @@ mod test {
|
|||||||
let db = checked_memory_handle();
|
let db = checked_memory_handle();
|
||||||
db.execute_batch("CREATE TABLE foo(x NOT NULL)").unwrap();
|
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)", NO_PARAMS);
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
|
|
||||||
match result.unwrap_err() {
|
match result.unwrap_err() {
|
||||||
@ -1526,7 +1504,7 @@ mod test {
|
|||||||
|
|
||||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
let results: Result<Vec<String>> = query
|
let results: Result<Vec<String>> = query
|
||||||
.query_and_then(&[], |row| row.get_checked(1))
|
.query_and_then(NO_PARAMS, |row| row.get_checked(1))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -1547,7 +1525,7 @@ mod test {
|
|||||||
|
|
||||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
let bad_type: Result<Vec<f64>> = query
|
let bad_type: Result<Vec<f64>> = query
|
||||||
.query_and_then(&[], |row| row.get_checked(1))
|
.query_and_then(NO_PARAMS, |row| row.get_checked(1))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -1557,7 +1535,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bad_idx: Result<Vec<String>> = query
|
let bad_idx: Result<Vec<String>> = query
|
||||||
.query_and_then(&[], |row| row.get_checked(3))
|
.query_and_then(NO_PARAMS, |row| row.get_checked(3))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -1581,8 +1559,9 @@ mod test {
|
|||||||
|
|
||||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
let results: CustomResult<Vec<String>> = query
|
let results: CustomResult<Vec<String>> = query
|
||||||
.query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite))
|
.query_and_then(NO_PARAMS, |row| {
|
||||||
.unwrap()
|
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||||
|
}).unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(results.unwrap().concat(), "hello, world!");
|
assert_eq!(results.unwrap().concat(), "hello, world!");
|
||||||
@ -1602,8 +1581,9 @@ mod test {
|
|||||||
|
|
||||||
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
let mut query = db.prepare("SELECT x, y FROM foo ORDER BY x DESC").unwrap();
|
||||||
let bad_type: CustomResult<Vec<f64>> = query
|
let bad_type: CustomResult<Vec<f64>> = query
|
||||||
.query_and_then(&[], |row| row.get_checked(1).map_err(CustomError::Sqlite))
|
.query_and_then(NO_PARAMS, |row| {
|
||||||
.unwrap()
|
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||||
|
}).unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
match bad_type.unwrap_err() {
|
match bad_type.unwrap_err() {
|
||||||
@ -1612,8 +1592,9 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bad_idx: CustomResult<Vec<String>> = query
|
let bad_idx: CustomResult<Vec<String>> = query
|
||||||
.query_and_then(&[], |row| row.get_checked(3).map_err(CustomError::Sqlite))
|
.query_and_then(NO_PARAMS, |row| {
|
||||||
.unwrap()
|
row.get_checked(3).map_err(CustomError::Sqlite)
|
||||||
|
}).unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
match bad_idx.unwrap_err() {
|
match bad_idx.unwrap_err() {
|
||||||
@ -1622,7 +1603,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let non_sqlite_err: CustomResult<Vec<String>> = query
|
let non_sqlite_err: CustomResult<Vec<String>> = query
|
||||||
.query_and_then(&[], |_| Err(CustomError::SomeError))
|
.query_and_then(NO_PARAMS, |_| Err(CustomError::SomeError))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -1642,7 +1623,7 @@ mod test {
|
|||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
let query = "SELECT x, y FROM foo ORDER BY x DESC";
|
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, NO_PARAMS, |row| {
|
||||||
row.get_checked(1).map_err(CustomError::Sqlite)
|
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1659,7 +1640,7 @@ mod test {
|
|||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
let query = "SELECT x, y FROM foo ORDER BY x DESC";
|
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, NO_PARAMS, |row| {
|
||||||
row.get_checked(1).map_err(CustomError::Sqlite)
|
row.get_checked(1).map_err(CustomError::Sqlite)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1668,7 +1649,7 @@ mod test {
|
|||||||
err => panic!("Unexpected error {}", err),
|
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, NO_PARAMS, |row| {
|
||||||
row.get_checked(3).map_err(CustomError::Sqlite)
|
row.get_checked(3).map_err(CustomError::Sqlite)
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1678,7 +1659,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let non_sqlite_err: CustomResult<String> =
|
let non_sqlite_err: CustomResult<String> =
|
||||||
db.query_row_and_then(query, &[], |_| Err(CustomError::SomeError));
|
db.query_row_and_then(query, NO_PARAMS, |_| Err(CustomError::SomeError));
|
||||||
|
|
||||||
match non_sqlite_err.unwrap_err() {
|
match non_sqlite_err.unwrap_err() {
|
||||||
CustomError::SomeError => (),
|
CustomError::SomeError => (),
|
||||||
@ -1695,7 +1676,7 @@ mod test {
|
|||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
db.query_row("SELECT * FROM foo", &[], |r| {
|
db.query_row("SELECT * FROM foo", NO_PARAMS, |r| {
|
||||||
assert_eq!(2, r.column_count())
|
assert_eq!(2, r.column_count())
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
}
|
}
|
||||||
|
116
src/statement.rs
116
src/statement.rs
@ -1,4 +1,5 @@
|
|||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
use std::iter::IntoIterator;
|
||||||
use std::os::raw::{c_char, c_int, c_void};
|
use std::os::raw::{c_char, c_int, c_void};
|
||||||
#[cfg(feature = "array")]
|
#[cfg(feature = "array")]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@ -71,8 +72,8 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// fn update_rows(conn: &Connection) -> Result<()> {
|
/// fn update_rows(conn: &Connection) -> Result<()> {
|
||||||
/// let mut stmt = try!(conn.prepare("UPDATE foo SET bar = 'baz' WHERE qux = ?"));
|
/// let mut stmt = try!(conn.prepare("UPDATE foo SET bar = 'baz' WHERE qux = ?"));
|
||||||
///
|
///
|
||||||
/// try!(stmt.execute(&[&1i32]));
|
/// try!(stmt.execute(&[1i32]));
|
||||||
/// try!(stmt.execute(&[&2i32]));
|
/// try!(stmt.execute(&[2i32]));
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
@ -83,7 +84,11 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// Will return `Err` if binding parameters fails, the executed statement
|
/// Will return `Err` if binding parameters fails, the executed statement
|
||||||
/// returns rows (in which case `query` should be used instead), or the
|
/// returns rows (in which case `query` should be used instead), or the
|
||||||
/// underling SQLite call fails.
|
/// 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));
|
try!(self.bind_parameters(params));
|
||||||
self.execute_with_bound_parameters()
|
self.execute_with_bound_parameters()
|
||||||
}
|
}
|
||||||
@ -130,7 +135,11 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if no row is inserted or many rows are inserted.
|
/// 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));
|
let changes = try!(self.execute(params));
|
||||||
match changes {
|
match changes {
|
||||||
1 => Ok(self.conn.last_insert_rowid()),
|
1 => Ok(self.conn.last_insert_rowid()),
|
||||||
@ -148,10 +157,10 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use rusqlite::{Connection, Result};
|
/// # use rusqlite::{Connection, Result, NO_PARAMS};
|
||||||
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
||||||
/// let mut stmt = try!(conn.prepare("SELECT name FROM people"));
|
/// let mut stmt = try!(conn.prepare("SELECT name FROM people"));
|
||||||
/// let mut rows = try!(stmt.query(&[]));
|
/// let mut rows = try!(stmt.query(NO_PARAMS));
|
||||||
///
|
///
|
||||||
/// let mut names = Vec::new();
|
/// let mut names = Vec::new();
|
||||||
/// while let Some(result_row) = rows.next() {
|
/// while let Some(result_row) = rows.next() {
|
||||||
@ -166,7 +175,11 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// ## Failure
|
/// ## Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if binding parameters fails.
|
/// 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.check_readonly());
|
||||||
try!(self.bind_parameters(params));
|
try!(self.bind_parameters(params));
|
||||||
Ok(Rows::new(self))
|
Ok(Rows::new(self))
|
||||||
@ -207,10 +220,10 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # use rusqlite::{Connection, Result};
|
/// # use rusqlite::{Connection, Result, NO_PARAMS};
|
||||||
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
/// fn get_names(conn: &Connection) -> Result<Vec<String>> {
|
||||||
/// let mut stmt = try!(conn.prepare("SELECT name FROM people"));
|
/// 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(NO_PARAMS, |row| row.get(0)));
|
||||||
///
|
///
|
||||||
/// let mut names = Vec::new();
|
/// let mut names = Vec::new();
|
||||||
/// for name_result in rows {
|
/// for name_result in rows {
|
||||||
@ -224,8 +237,10 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// ## Failure
|
/// ## Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if binding parameters fails.
|
/// 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
|
where
|
||||||
|
P: IntoIterator,
|
||||||
|
P::Item: ToSql,
|
||||||
F: FnMut(&Row) -> T,
|
F: FnMut(&Row) -> T,
|
||||||
{
|
{
|
||||||
let rows = self.query(params)?;
|
let rows = self.query(params)?;
|
||||||
@ -278,12 +293,14 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if binding parameters fails.
|
/// 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,
|
&'a mut self,
|
||||||
params: &[&ToSql],
|
params: P,
|
||||||
f: F,
|
f: F,
|
||||||
) -> Result<AndThenRows<'a, F>>
|
) -> Result<AndThenRows<'a, F>>
|
||||||
where
|
where
|
||||||
|
P: IntoIterator,
|
||||||
|
P::Item: ToSql,
|
||||||
E: convert::From<Error>,
|
E: convert::From<Error>,
|
||||||
F: FnMut(&Row) -> result::Result<T, E>,
|
F: FnMut(&Row) -> result::Result<T, E>,
|
||||||
{
|
{
|
||||||
@ -344,7 +361,11 @@ impl<'conn> Statement<'conn> {
|
|||||||
|
|
||||||
/// Return `true` if a query in the SQL statement it executes returns one
|
/// 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.
|
/// 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 mut rows = try!(self.query(params));
|
||||||
let exists = {
|
let exists = {
|
||||||
match rows.next() {
|
match rows.next() {
|
||||||
@ -364,8 +385,10 @@ impl<'conn> Statement<'conn> {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if the underlying SQLite call fails.
|
/// 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
|
where
|
||||||
|
P: IntoIterator,
|
||||||
|
P::Item: ToSql,
|
||||||
F: FnOnce(&Row) -> T,
|
F: FnOnce(&Row) -> T,
|
||||||
{
|
{
|
||||||
let mut rows = try!(self.query(params));
|
let mut rows = try!(self.query(params));
|
||||||
@ -396,18 +419,25 @@ impl<'conn> Statement<'conn> {
|
|||||||
Ok(self.stmt.bind_parameter_index(&c_name))
|
Ok(self.stmt.bind_parameter_index(&c_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bind_parameters(&mut self, params: &[&ToSql]) -> Result<()> {
|
fn bind_parameters<P>(&mut self, params: P) -> Result<()>
|
||||||
assert_eq!(
|
where
|
||||||
params.len(),
|
P: IntoIterator,
|
||||||
self.stmt.bind_parameter_count(),
|
P::Item: ToSql,
|
||||||
"incorrect number of parameters to query(): expected {}, got {}",
|
{
|
||||||
self.stmt.bind_parameter_count(),
|
let expected = self.stmt.bind_parameter_count();
|
||||||
params.len()
|
let mut index = 0;
|
||||||
);
|
for p in params.into_iter() {
|
||||||
|
index += 1; // The leftmost SQL parameter has an index of 1.
|
||||||
for (i, p) in params.iter().enumerate() {
|
if index > expected {
|
||||||
try!(self.bind_parameter(*p, i + 1));
|
break;
|
||||||
}
|
}
|
||||||
|
try!(self.bind_parameter(&p, index));
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
index, expected,
|
||||||
|
"incorrect number of parameters: expected {}, got {}",
|
||||||
|
expected, index
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -643,7 +673,7 @@ impl<'conn> Statement<'conn> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use {Connection, Error, Result};
|
use {Connection, Error, Result, NO_PARAMS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_execute_named() {
|
fn test_execute_named() {
|
||||||
@ -780,8 +810,9 @@ mod test {
|
|||||||
stmt.execute_named(&[(":x", &"one")]).unwrap();
|
stmt.execute_named(&[(":x", &"one")]).unwrap();
|
||||||
|
|
||||||
let result: Option<String> = db
|
let result: Option<String> = db
|
||||||
.query_row("SELECT y FROM test WHERE x = 'one'", &[], |row| row.get(0))
|
.query_row("SELECT y FROM test WHERE x = 'one'", NO_PARAMS, |row| {
|
||||||
.unwrap();
|
row.get(0)
|
||||||
|
}).unwrap();
|
||||||
assert!(result.is_none());
|
assert!(result.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -798,8 +829,9 @@ mod test {
|
|||||||
stmt.execute_named(&[(":y", &"two")]).unwrap();
|
stmt.execute_named(&[(":y", &"two")]).unwrap();
|
||||||
|
|
||||||
let result: String = db
|
let result: String = db
|
||||||
.query_row("SELECT x FROM test WHERE y = 'two'", &[], |row| row.get(0))
|
.query_row("SELECT x FROM test WHERE y = 'two'", NO_PARAMS, |row| {
|
||||||
.unwrap();
|
row.get(0)
|
||||||
|
}).unwrap();
|
||||||
assert_eq!(result, "one");
|
assert_eq!(result, "one");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -811,16 +843,16 @@ mod test {
|
|||||||
let mut stmt = db
|
let mut stmt = db
|
||||||
.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?)")
|
.prepare("INSERT OR IGNORE INTO foo (x) VALUES (?)")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(stmt.insert(&[&1i32]).unwrap(), 1);
|
assert_eq!(stmt.insert(&[1i32]).unwrap(), 1);
|
||||||
assert_eq!(stmt.insert(&[&2i32]).unwrap(), 2);
|
assert_eq!(stmt.insert(&[2i32]).unwrap(), 2);
|
||||||
match stmt.insert(&[&1i32]).unwrap_err() {
|
match stmt.insert(&[1i32]).unwrap_err() {
|
||||||
Error::StatementChangedRows(0) => (),
|
Error::StatementChangedRows(0) => (),
|
||||||
err => panic!("Unexpected error {}", err),
|
err => panic!("Unexpected error {}", err),
|
||||||
}
|
}
|
||||||
let mut multi = db
|
let mut multi = db
|
||||||
.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4")
|
.prepare("INSERT INTO foo (x) SELECT 3 UNION ALL SELECT 4")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
match multi.insert(&[]).unwrap_err() {
|
match multi.insert(NO_PARAMS).unwrap_err() {
|
||||||
Error::StatementChangedRows(2) => (),
|
Error::StatementChangedRows(2) => (),
|
||||||
err => panic!("Unexpected error {}", err),
|
err => panic!("Unexpected error {}", err),
|
||||||
}
|
}
|
||||||
@ -840,14 +872,14 @@ mod test {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.prepare("INSERT INTO foo VALUES (10)")
|
db.prepare("INSERT INTO foo VALUES (10)")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(&[])
|
.insert(NO_PARAMS)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.prepare("INSERT INTO bar VALUES (10)")
|
db.prepare("INSERT INTO bar VALUES (10)")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(&[])
|
.insert(NO_PARAMS)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
@ -863,9 +895,9 @@ mod test {
|
|||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?").unwrap();
|
let mut stmt = db.prepare("SELECT 1 FROM foo WHERE x = ?").unwrap();
|
||||||
assert!(stmt.exists(&[&1i32]).unwrap());
|
assert!(stmt.exists(&[1i32]).unwrap());
|
||||||
assert!(stmt.exists(&[&2i32]).unwrap());
|
assert!(stmt.exists(&[2i32]).unwrap());
|
||||||
assert!(!stmt.exists(&[&0i32]).unwrap());
|
assert!(!stmt.exists(&[0i32]).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -878,7 +910,7 @@ mod test {
|
|||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
let mut stmt = db.prepare("SELECT y FROM foo WHERE x = ?").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());
|
assert_eq!(3i64, y.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -891,7 +923,7 @@ mod test {
|
|||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
let mut stmt = db.prepare("SELECT y FROM foo").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(NO_PARAMS, |r| r.get("y"));
|
||||||
assert_eq!(3i64, y.unwrap());
|
assert_eq!(3i64, y.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -904,7 +936,7 @@ mod test {
|
|||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
let mut stmt = db.prepare("SELECT y as Y FROM foo").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(NO_PARAMS, |r| r.get("y"));
|
||||||
assert_eq!(3i64, y.unwrap());
|
assert_eq!(3i64, y.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,13 +139,13 @@ mod test {
|
|||||||
let mut db = Connection::open_in_memory().unwrap();
|
let mut db = Connection::open_in_memory().unwrap();
|
||||||
db.trace(Some(tracer));
|
db.trace(Some(tracer));
|
||||||
{
|
{
|
||||||
let _ = db.query_row("SELECT ?", &[&1i32], |_| {});
|
let _ = db.query_row("SELECT ?", &[1i32], |_| {});
|
||||||
let _ = db.query_row("SELECT ?", &[&"hello"], |_| {});
|
let _ = db.query_row("SELECT ?", &["hello"], |_| {});
|
||||||
}
|
}
|
||||||
db.trace(None);
|
db.trace(None);
|
||||||
{
|
{
|
||||||
let _ = db.query_row("SELECT ?", &[&2i32], |_| {});
|
let _ = db.query_row("SELECT ?", &[2i32], |_| {});
|
||||||
let _ = db.query_row("SELECT ?", &[&"goodbye"], |_| {});
|
let _ = db.query_row("SELECT ?", &["goodbye"], |_| {});
|
||||||
}
|
}
|
||||||
|
|
||||||
let traced_stmts = TRACED_STMTS.lock().unwrap();
|
let traced_stmts = TRACED_STMTS.lock().unwrap();
|
||||||
|
@ -419,7 +419,7 @@ impl Connection {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::DropBehavior;
|
use super::DropBehavior;
|
||||||
use Connection;
|
use {Connection, NO_PARAMS};
|
||||||
|
|
||||||
fn checked_memory_handle() -> Connection {
|
fn checked_memory_handle() -> Connection {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
@ -444,7 +444,7 @@ mod test {
|
|||||||
let tx = db.transaction().unwrap();
|
let tx = db.transaction().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
2i32,
|
2i32,
|
||||||
tx.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -473,7 +473,7 @@ mod test {
|
|||||||
let tx = db.transaction().unwrap();
|
let tx = db.transaction().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
6i32,
|
6i32,
|
||||||
tx.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -565,12 +565,12 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn insert(x: i32, conn: &Connection) {
|
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) {
|
fn assert_current_sum(x: i32, conn: &Connection) {
|
||||||
let i = conn
|
let i = conn
|
||||||
.query_row::<i32, _>("SELECT SUM(x) FROM foo", &[], |r| r.get(0))
|
.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(x, i);
|
assert_eq!(x, i);
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ mod test {
|
|||||||
use super::chrono::{
|
use super::chrono::{
|
||||||
DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc,
|
DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc,
|
||||||
};
|
};
|
||||||
use Connection;
|
use {Connection, NO_PARAMS};
|
||||||
|
|
||||||
fn checked_memory_handle() -> Connection {
|
fn checked_memory_handle() -> Connection {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
@ -149,11 +149,11 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let s: String = db
|
let s: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!("2016-02-23", s);
|
assert_eq!("2016-02-23", s);
|
||||||
let t: NaiveDate = db
|
let t: NaiveDate = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(date, t);
|
assert_eq!(date, t);
|
||||||
}
|
}
|
||||||
@ -166,11 +166,11 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let s: String = db
|
let s: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!("23:56:04", s);
|
assert_eq!("23:56:04", s);
|
||||||
let v: NaiveTime = db
|
let v: NaiveTime = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(time, v);
|
assert_eq!(time, v);
|
||||||
}
|
}
|
||||||
@ -186,17 +186,18 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let s: String = db
|
let s: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!("2016-02-23T23:56:04", s);
|
assert_eq!("2016-02-23T23:56:04", s);
|
||||||
let v: NaiveDateTime = db
|
let v: NaiveDateTime = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(dt, v);
|
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)", NO_PARAMS)
|
||||||
|
.unwrap(); // "YYYY-MM-DD HH:MM:SS"
|
||||||
let hms: NaiveDateTime = db
|
let hms: NaiveDateTime = db
|
||||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT b FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(dt, hms);
|
assert_eq!(dt, hms);
|
||||||
}
|
}
|
||||||
@ -213,28 +214,29 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let s: String = db
|
let s: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!("2016-02-23T23:56:04.789+00:00", s);
|
assert_eq!("2016-02-23T23:56:04.789+00:00", s);
|
||||||
|
|
||||||
let v1: DateTime<Utc> = db
|
let v1: DateTime<Utc> = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(utc, v1);
|
assert_eq!(utc, v1);
|
||||||
|
|
||||||
let v2: DateTime<Utc> = db
|
let v2: DateTime<Utc> = db
|
||||||
.query_row("SELECT '2016-02-23 23:56:04.789'", &[], |r| r.get(0))
|
.query_row("SELECT '2016-02-23 23:56:04.789'", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(utc, v2);
|
assert_eq!(utc, v2);
|
||||||
|
|
||||||
let v3: DateTime<Utc> = db
|
let v3: DateTime<Utc> = db
|
||||||
.query_row("SELECT '2016-02-23 23:56:04'", &[], |r| r.get(0))
|
.query_row("SELECT '2016-02-23 23:56:04'", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(utc - Duration::milliseconds(789), v3);
|
assert_eq!(utc - Duration::milliseconds(789), v3);
|
||||||
|
|
||||||
let v4: DateTime<Utc> = db
|
let v4: DateTime<Utc> = db
|
||||||
.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", &[], |r| r.get(0))
|
.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", NO_PARAMS, |r| {
|
||||||
.unwrap();
|
r.get(0)
|
||||||
|
}).unwrap();
|
||||||
assert_eq!(utc, v4);
|
assert_eq!(utc, v4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,12 +253,12 @@ mod test {
|
|||||||
|
|
||||||
// Stored string should be in UTC
|
// Stored string should be in UTC
|
||||||
let s: String = db
|
let s: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(s.ends_with("+00:00"));
|
assert!(s.ends_with("+00:00"));
|
||||||
|
|
||||||
let v: DateTime<Local> = db
|
let v: DateTime<Local> = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(local, v);
|
assert_eq!(local, v);
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ mod value_ref;
|
|||||||
/// # use rusqlite::types::{Null};
|
/// # use rusqlite::types::{Null};
|
||||||
/// fn main() {}
|
/// fn main() {}
|
||||||
/// fn insert_null(conn: &Connection) -> Result<usize> {
|
/// 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)]
|
#[derive(Copy, Clone)]
|
||||||
@ -115,8 +115,7 @@ mod test {
|
|||||||
use super::Value;
|
use super::Value;
|
||||||
use std::f64::EPSILON;
|
use std::f64::EPSILON;
|
||||||
use std::os::raw::{c_double, c_int};
|
use std::os::raw::{c_double, c_int};
|
||||||
use Connection;
|
use {Connection, Error, NO_PARAMS};
|
||||||
use Error;
|
|
||||||
|
|
||||||
fn checked_memory_handle() -> Connection {
|
fn checked_memory_handle() -> Connection {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
@ -134,7 +133,7 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let v: Vec<u8> = db
|
let v: Vec<u8> = db
|
||||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT b FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(v, v1234);
|
assert_eq!(v, v1234);
|
||||||
}
|
}
|
||||||
@ -148,7 +147,7 @@ mod test {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let v: Vec<u8> = db
|
let v: Vec<u8> = db
|
||||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT b FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(v, empty);
|
assert_eq!(v, empty);
|
||||||
}
|
}
|
||||||
@ -161,7 +160,7 @@ mod test {
|
|||||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s]).unwrap();
|
db.execute("INSERT INTO foo(t) VALUES (?)", &[&s]).unwrap();
|
||||||
|
|
||||||
let from: String = db
|
let from: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(from, s);
|
assert_eq!(from, s);
|
||||||
}
|
}
|
||||||
@ -171,11 +170,11 @@ mod test {
|
|||||||
let db = checked_memory_handle();
|
let db = checked_memory_handle();
|
||||||
|
|
||||||
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()])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let from: String = db
|
let from: String = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(from, s);
|
assert_eq!(from, s);
|
||||||
}
|
}
|
||||||
@ -184,12 +183,12 @@ mod test {
|
|||||||
fn test_value() {
|
fn test_value() {
|
||||||
let db = checked_memory_handle();
|
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();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
10i64,
|
10i64,
|
||||||
db.query_row::<i64, _>("SELECT i FROM foo", &[], |r| r.get(0))
|
db.query_row::<i64, _, _>("SELECT i FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -207,7 +206,7 @@ mod test {
|
|||||||
let mut stmt = db
|
let mut stmt = db
|
||||||
.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC")
|
.prepare("SELECT t, b FROM foo ORDER BY ROWID ASC")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let mut rows = stmt.query(&[]).unwrap();
|
let mut rows = stmt.query(NO_PARAMS).unwrap();
|
||||||
|
|
||||||
{
|
{
|
||||||
let row1 = rows.next().unwrap().unwrap();
|
let row1 = rows.next().unwrap().unwrap();
|
||||||
@ -239,11 +238,11 @@ mod test {
|
|||||||
|
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
||||||
&[],
|
NO_PARAMS,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").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(NO_PARAMS).unwrap();
|
||||||
|
|
||||||
let row = rows.next().unwrap().unwrap();
|
let row = rows.next().unwrap().unwrap();
|
||||||
|
|
||||||
@ -354,11 +353,11 @@ mod test {
|
|||||||
|
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
|
||||||
&[],
|
NO_PARAMS,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").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(NO_PARAMS).unwrap();
|
||||||
|
|
||||||
let row = rows.next().unwrap().unwrap();
|
let row = rows.next().unwrap().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -27,7 +27,8 @@ impl FromSql for Value {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::serde_json;
|
use super::serde_json;
|
||||||
use Connection;
|
use types::ToSql;
|
||||||
|
use {Connection, NO_PARAMS};
|
||||||
|
|
||||||
fn checked_memory_handle() -> Connection {
|
fn checked_memory_handle() -> Connection {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
@ -44,15 +45,15 @@ mod test {
|
|||||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
||||||
&[&data, &json.as_bytes()],
|
&[&data as &ToSql, &json.as_bytes()],
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let t: serde_json::Value = db
|
let t: serde_json::Value = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(data, t);
|
assert_eq!(data, t);
|
||||||
let b: serde_json::Value = db
|
let b: serde_json::Value = db
|
||||||
.query_row("SELECT b FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT b FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(data, b);
|
assert_eq!(data, b);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ impl FromSql for time::Timespec {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::time;
|
use super::time;
|
||||||
use Connection;
|
use {Connection, NO_PARAMS};
|
||||||
|
|
||||||
fn checked_memory_handle() -> Connection {
|
fn checked_memory_handle() -> Connection {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
@ -58,10 +58,10 @@ mod test {
|
|||||||
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
|
db.execute("INSERT INTO foo(t) VALUES (?)", &[&ts]).unwrap();
|
||||||
|
|
||||||
let from: time::Timespec = db
|
let from: time::Timespec = db
|
||||||
.query_row("SELECT t FROM foo", &[], |r| r.get(0))
|
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
db.execute("DELETE FROM foo", &[]).unwrap();
|
db.execute("DELETE FROM foo", NO_PARAMS).unwrap();
|
||||||
|
|
||||||
assert_eq!(from, ts);
|
assert_eq!(from, ts);
|
||||||
}
|
}
|
||||||
|
@ -114,10 +114,10 @@ to_sql_self!(f64);
|
|||||||
|
|
||||||
impl<'a, T: ?Sized> ToSql for &'a T
|
impl<'a, T: ?Sized> ToSql for &'a T
|
||||||
where
|
where
|
||||||
&'a T: Into<ToSqlOutput<'a>>,
|
T: ToSql,
|
||||||
{
|
{
|
||||||
fn to_sql(&self) -> Result<ToSqlOutput> {
|
fn to_sql(&self) -> Result<ToSqlOutput> {
|
||||||
Ok((*self).into())
|
(*self).to_sql()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ mod test {
|
|||||||
use std::sync::mpsc::sync_channel;
|
use std::sync::mpsc::sync_channel;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time;
|
use std::time;
|
||||||
use {Connection, OpenFlags, Result, Transaction, TransactionBehavior};
|
use {Connection, OpenFlags, Result, Transaction, TransactionBehavior, NO_PARAMS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unlock_notify() {
|
fn test_unlock_notify() {
|
||||||
@ -121,7 +121,7 @@ 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: Result<i64> = db1.query_row("SELECT x FROM foo", NO_PARAMS, |r| r.get(0));
|
||||||
assert_eq!(42i64, the_answer.unwrap());
|
assert_eq!(42i64, the_answer.unwrap());
|
||||||
child.join().unwrap();
|
child.join().unwrap();
|
||||||
}
|
}
|
||||||
|
@ -341,7 +341,7 @@ impl From<csv::Error> for Error {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use vtab::csvtab;
|
use vtab::csvtab;
|
||||||
use {Connection, Result};
|
use {Connection, Result, NO_PARAMS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_csv_module() {
|
fn test_csv_module() {
|
||||||
@ -358,7 +358,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ids: Result<Vec<i32>> = s
|
let ids: Result<Vec<i32>> = s
|
||||||
.query_map(&[], |row| row.get::<_, i32>(0))
|
.query_map(NO_PARAMS, |row| row.get::<_, i32>(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
let sum = ids.unwrap().iter().fold(0, |acc, &id| acc + id);
|
let sum = ids.unwrap().iter().fold(0, |acc, &id| acc + id);
|
||||||
@ -381,7 +381,7 @@ mod test {
|
|||||||
v1.rowid < v2.rowid",
|
v1.rowid < v2.rowid",
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let mut rows = s.query(&[]).unwrap();
|
let mut rows = s.query(NO_PARAMS).unwrap();
|
||||||
let row = rows.next().unwrap().unwrap();
|
let row = rows.next().unwrap().unwrap();
|
||||||
assert_eq!(row.get::<_, i32>(0), 2);
|
assert_eq!(row.get::<_, i32>(0), 2);
|
||||||
}
|
}
|
||||||
|
@ -265,7 +265,7 @@ impl VTabCursor for SeriesTabCursor {
|
|||||||
mod test {
|
mod test {
|
||||||
use ffi;
|
use ffi;
|
||||||
use vtab::series;
|
use vtab::series;
|
||||||
use Connection;
|
use {Connection, NO_PARAMS};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_series_module() {
|
fn test_series_module() {
|
||||||
@ -279,7 +279,7 @@ mod test {
|
|||||||
|
|
||||||
let mut s = db.prepare("SELECT * FROM generate_series(0,20,5)").unwrap();
|
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(NO_PARAMS, |row| row.get::<_, i32>(0)).unwrap();
|
||||||
|
|
||||||
let mut expected = 0;
|
let mut expected = 0;
|
||||||
for value in series {
|
for value in series {
|
||||||
|
@ -6,6 +6,7 @@ extern crate rusqlite;
|
|||||||
#[cfg(feature = "vtab")]
|
#[cfg(feature = "vtab")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dummy_module() {
|
fn test_dummy_module() {
|
||||||
|
use rusqlite::types::ToSql;
|
||||||
use rusqlite::vtab::{
|
use rusqlite::vtab::{
|
||||||
eponymous_only_module, sqlite3_vtab, sqlite3_vtab_cursor, Context, IndexInfo, VTab,
|
eponymous_only_module, sqlite3_vtab, sqlite3_vtab_cursor, Context, IndexInfo, VTab,
|
||||||
VTabConnection, VTabCursor, Values,
|
VTabConnection, VTabCursor, Values,
|
||||||
@ -96,6 +97,8 @@ fn test_dummy_module() {
|
|||||||
|
|
||||||
let mut s = db.prepare("SELECT * FROM dummy()").unwrap();
|
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);
|
assert_eq!(1, dummy);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user