Merge pull request #465 from gwenn/params

Test parameters binding
This commit is contained in:
gwenn 2019-01-26 08:47:11 +01:00 committed by GitHub
commit 2ae8044c20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -809,7 +809,9 @@ mod test {
assert!(!called.load(Ordering::Relaxed)); assert!(!called.load(Ordering::Relaxed));
let check = db let check = db
.query_row("SELECT 1 FROM foo WHERE t = ?", &["bar"], |row| row.get::<_, i32>(0)) .query_row("SELECT 1 FROM foo WHERE t = ?", &["bar"], |row| {
row.get::<_, i32>(0)
})
.unwrap(); .unwrap();
assert_eq!(1, check); assert_eq!(1, check);
@ -845,7 +847,9 @@ mod test {
.unwrap(); .unwrap();
let check = db let check = db
.query_row("SELECT 1 FROM foo WHERE t = ?", &["bar"], |row| row.get::<_, i32>(0)) .query_row("SELECT 1 FROM foo WHERE t = ?", &["bar"], |row| {
row.get::<_, i32>(0)
})
.unwrap(); .unwrap();
assert_eq!(1, check); assert_eq!(1, check);
} }

View File

@ -709,6 +709,7 @@ pub enum StatementStatus {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::types::ToSql;
use crate::{Connection, Error, Result, NO_PARAMS}; use crate::{Connection, Error, Result, NO_PARAMS};
#[test] #[test]
@ -991,4 +992,40 @@ mod test {
stmt.bind_parameter(&1, 1).unwrap(); stmt.bind_parameter(&1, 1).unwrap();
assert_eq!(Some("SELECT 1"), stmt.expanded_sql()); assert_eq!(Some("SELECT 1"), stmt.expanded_sql());
} }
#[test]
fn test_bind_parameters() {
let db = Connection::open_in_memory().unwrap();
// dynamic slice:
db.query_row(
"SELECT ?1, ?2, ?3",
&[&1u8 as &ToSql, &"one", &Some("one")],
|row| row.get::<_, u8>(0),
)
.unwrap();
// existing collection:
let data = vec![1, 2, 3];
db.query_row("SELECT ?1, ?2, ?3", &data, |row| row.get::<_, u8>(0))
.unwrap();
db.query_row("SELECT ?1, ?2, ?3", data.as_slice(), |row| {
row.get::<_, u8>(0)
})
.unwrap();
db.query_row("SELECT ?1, ?2, ?3", data, |row| row.get::<_, u8>(0))
.unwrap();
use std::collections::BTreeSet;
let data: BTreeSet<String> = ["one", "two", "three"]
.into_iter()
.map(|s| s.to_string())
.collect();
db.query_row("SELECT ?1, ?2, ?3", &data, |row| row.get::<_, String>(0))
.unwrap();
let data = [0; 3];
db.query_row("SELECT ?1, ?2, ?3", &data, |row| row.get::<_, u8>(0))
.unwrap();
db.query_row("SELECT ?1, ?2, ?3", data.iter(), |row| row.get::<_, u8>(0))
.unwrap();
}
} }