mirror of
https://github.com/isar/rusqlite.git
synced 2025-01-20 07:00:51 +08:00
Run rustfmt on named_params.rs
This commit is contained in:
parent
53979407c5
commit
ab6ab3b2e9
@ -1,4 +1,4 @@
|
|||||||
//use std::collections::HashMap;
|
// use std::collections::HashMap;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
|
|
||||||
@ -35,34 +35,41 @@ impl SqliteConnection {
|
|||||||
///
|
///
|
||||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
||||||
/// underlying SQLite call fails.
|
/// underlying SQLite call fails.
|
||||||
pub fn query_named_row<T, F>(&self, sql: &str, params: &[(&str, &ToSql)], f: F) -> SqliteResult<T>
|
pub fn query_named_row<T, F>(&self,
|
||||||
where F: FnOnce(SqliteRow) -> T {
|
sql: &str,
|
||||||
|
params: &[(&str, &ToSql)],
|
||||||
|
f: F)
|
||||||
|
-> SqliteResult<T>
|
||||||
|
where F: FnOnce(SqliteRow) -> T
|
||||||
|
{
|
||||||
let mut stmt = try!(self.prepare(sql));
|
let mut stmt = try!(self.prepare(sql));
|
||||||
let mut rows = try!(stmt.query_named(params));
|
let mut rows = try!(stmt.query_named(params));
|
||||||
|
|
||||||
match rows.next() {
|
match rows.next() {
|
||||||
Some(row) => row.map(f),
|
Some(row) => row.map(f),
|
||||||
None => Err(SqliteError{
|
None => {
|
||||||
code: ffi::SQLITE_NOTICE,
|
Err(SqliteError {
|
||||||
message: "Query did not return a row".to_string(),
|
code: ffi::SQLITE_NOTICE,
|
||||||
})
|
message: "Query did not return a row".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'conn> SqliteStatement<'conn> {
|
impl<'conn> SqliteStatement<'conn> {
|
||||||
/*pub fn parameter_names(&self) -> HashMap<String, i32> {
|
// pub fn parameter_names(&self) -> HashMap<String, i32> {
|
||||||
let n = unsafe { ffi::sqlite3_bind_parameter_count(self.stmt) };
|
// let n = unsafe { ffi::sqlite3_bind_parameter_count(self.stmt) };
|
||||||
let mut index_by_name = HashMap::with_capacity(n as usize);
|
// let mut index_by_name = HashMap::with_capacity(n as usize);
|
||||||
for i in 1..n+1 {
|
// for i in 1..n+1 {
|
||||||
let c_name = unsafe { ffi::sqlite3_bind_parameter_name(self.stmt, i) };
|
// let c_name = unsafe { ffi::sqlite3_bind_parameter_name(self.stmt, i) };
|
||||||
if !c_name.is_null() {
|
// if !c_name.is_null() {
|
||||||
let c_slice = unsafe { CStr::from_ptr(c_name).to_bytes() };
|
// let c_slice = unsafe { CStr::from_ptr(c_name).to_bytes() };
|
||||||
index_by_name.insert(str::from_utf8(c_slice).unwrap().to_string(), n);
|
// index_by_name.insert(str::from_utf8(c_slice).unwrap().to_string(), n);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
index_by_name
|
// index_by_name
|
||||||
}*/
|
// }
|
||||||
|
|
||||||
/// Return the index of an SQL parameter given its name.
|
/// Return the index of an SQL parameter given its name.
|
||||||
///
|
///
|
||||||
@ -71,12 +78,12 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
/// Return None if `name` is invalid (NulError) or if no matching parameter is found.
|
/// Return None if `name` is invalid (NulError) or if no matching parameter is found.
|
||||||
pub fn parameter_index(&self, name: &str) -> Option<i32> {
|
pub fn parameter_index(&self, name: &str) -> Option<i32> {
|
||||||
unsafe {
|
unsafe {
|
||||||
CString::new(name).ok().and_then(|c_name|
|
CString::new(name).ok().and_then(|c_name| {
|
||||||
match ffi::sqlite3_bind_parameter_index(self.stmt, c_name.as_ptr()) {
|
match ffi::sqlite3_bind_parameter_index(self.stmt, c_name.as_ptr()) {
|
||||||
0 => None, // A zero is returned if no matching parameter is found.
|
0 => None, // A zero is returned if no matching parameter is found.
|
||||||
n => Some(n)
|
n => Some(n),
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,7 +133,9 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if binding parameters fails.
|
/// Will return `Err` if binding parameters fails.
|
||||||
pub fn query_named<'a>(&'a mut self, params: &[(&str, &ToSql)]) -> SqliteResult<SqliteRows<'a>> {
|
pub fn query_named<'a>(&'a mut self,
|
||||||
|
params: &[(&str, &ToSql)])
|
||||||
|
-> SqliteResult<SqliteRows<'a>> {
|
||||||
self.reset_if_needed();
|
self.reset_if_needed();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -139,9 +148,9 @@ impl<'conn> SqliteStatement<'conn> {
|
|||||||
|
|
||||||
unsafe fn bind_named_parameters(&mut self, params: &[(&str, &ToSql)]) -> SqliteResult<()> {
|
unsafe fn bind_named_parameters(&mut self, params: &[(&str, &ToSql)]) -> SqliteResult<()> {
|
||||||
for &(name, value) in params {
|
for &(name, value) in params {
|
||||||
let i = try!(self.parameter_index(name).ok_or(SqliteError{
|
let i = try!(self.parameter_index(name).ok_or(SqliteError {
|
||||||
code: ffi::SQLITE_MISUSE,
|
code: ffi::SQLITE_MISUSE,
|
||||||
message: format!("Invalid parameter name: {}", name)
|
message: format!("Invalid parameter name: {}", name),
|
||||||
}));
|
}));
|
||||||
try!(self.conn.decode_result(value.bind_parameter(self.stmt, i)));
|
try!(self.conn.decode_result(value.bind_parameter(self.stmt, i)));
|
||||||
}
|
}
|
||||||
@ -158,26 +167,35 @@ mod test {
|
|||||||
let db = SqliteConnection::open_in_memory().unwrap();
|
let db = SqliteConnection::open_in_memory().unwrap();
|
||||||
db.execute_batch("CREATE TABLE foo(x INTEGER)").unwrap();
|
db.execute_batch("CREATE TABLE foo(x INTEGER)").unwrap();
|
||||||
|
|
||||||
assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &1i32)]).unwrap(), 1);
|
assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &1i32)]).unwrap(),
|
||||||
assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &2i32)]).unwrap(), 1);
|
1);
|
||||||
|
assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &2i32)]).unwrap(),
|
||||||
|
1);
|
||||||
|
|
||||||
assert_eq!(3i32, db.query_named_row("SELECT SUM(x) FROM foo WHERE x > :x", &[(":x", &0i32)], |r| r.get(0)).unwrap());
|
assert_eq!(3i32,
|
||||||
|
db.query_named_row("SELECT SUM(x) FROM foo WHERE x > :x",
|
||||||
|
&[(":x", &0i32)],
|
||||||
|
|r| r.get(0))
|
||||||
|
.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_stmt_execute_named() {
|
fn test_stmt_execute_named() {
|
||||||
let db = SqliteConnection::open_in_memory().unwrap();
|
let db = SqliteConnection::open_in_memory().unwrap();
|
||||||
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)";
|
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag \
|
||||||
|
INTEGER)";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
let mut stmt = db.prepare("INSERT INTO test (id, name, flag) VALUES (:id, :name, :flag)").unwrap();
|
let mut stmt = db.prepare("INSERT INTO test (id, name, flag) VALUES (:id, :name, :flag)")
|
||||||
|
.unwrap();
|
||||||
stmt.execute_named(&[(":name", &"one")]).unwrap();
|
stmt.execute_named(&[(":name", &"one")]).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_named() {
|
fn test_query_named() {
|
||||||
let db = SqliteConnection::open_in_memory().unwrap();
|
let db = SqliteConnection::open_in_memory().unwrap();
|
||||||
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag INTEGER)";
|
let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag \
|
||||||
|
INTEGER)";
|
||||||
db.execute_batch(sql).unwrap();
|
db.execute_batch(sql).unwrap();
|
||||||
|
|
||||||
let mut stmt = db.prepare("SELECT * FROM test where name = :name").unwrap();
|
let mut stmt = db.prepare("SELECT * FROM test where name = :name").unwrap();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user