mirror of
https://github.com/isar/rusqlite.git
synced 2025-09-16 12:42:18 +08:00
Merge branch 'master' into gwenn-stmt-cache
This commit is contained in:
345
src/lib.rs
345
src/lib.rs
@@ -50,6 +50,9 @@
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
#![cfg_attr(feature="clippy", feature(plugin))]
|
||||
#![cfg_attr(feature="clippy", plugin(clippy))]
|
||||
|
||||
extern crate libc;
|
||||
extern crate libsqlite3_sys as ffi;
|
||||
#[macro_use]
|
||||
@@ -84,12 +87,13 @@ pub mod types;
|
||||
mod transaction;
|
||||
mod named_params;
|
||||
mod error;
|
||||
mod convenient;
|
||||
#[cfg(feature = "load_extension")]mod load_extension_guard;
|
||||
#[cfg(feature = "trace")]pub mod trace;
|
||||
#[cfg(feature = "backup")]pub mod backup;
|
||||
#[cfg(feature = "cache")] pub mod cache;
|
||||
#[cfg(feature = "functions")] pub mod functions;
|
||||
#[cfg(feature = "blob")] pub mod blob;
|
||||
#[cfg(feature = "cache")]pub mod cache;
|
||||
#[cfg(feature = "functions")]pub mod functions;
|
||||
#[cfg(feature = "blob")]pub mod blob;
|
||||
|
||||
/// Old name for `Result`. `SqliteResult` is deprecated.
|
||||
pub type SqliteResult<T> = Result<T>;
|
||||
@@ -99,8 +103,7 @@ pub type Result<T> = result::Result<T, Error>;
|
||||
|
||||
unsafe fn errmsg_to_string(errmsg: *const c_char) -> String {
|
||||
let c_slice = CStr::from_ptr(errmsg).to_bytes();
|
||||
let utf8_str = str::from_utf8(c_slice);
|
||||
utf8_str.unwrap_or("Invalid string encoding").to_string()
|
||||
String::from_utf8_lossy(c_slice).into_owned()
|
||||
}
|
||||
|
||||
fn str_to_cstring(s: &str) -> Result<CString> {
|
||||
@@ -128,9 +131,9 @@ pub enum DatabaseName<'a> {
|
||||
// impl to avoid dead code warnings.
|
||||
#[cfg(any(feature = "backup", feature = "blob"))]
|
||||
impl<'a> DatabaseName<'a> {
|
||||
fn to_cstring(self) -> Result<CString> {
|
||||
fn to_cstring(&self) -> Result<CString> {
|
||||
use self::DatabaseName::{Main, Temp, Attached};
|
||||
match self {
|
||||
match *self {
|
||||
Main => str_to_cstring("main"),
|
||||
Temp => str_to_cstring("temp"),
|
||||
Attached(s) => str_to_cstring(s),
|
||||
@@ -183,17 +186,15 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `path` cannot be converted to a C-compatible string or if the
|
||||
/// underlying SQLite open call fails.
|
||||
pub fn open_with_flags<P: AsRef<Path>>(path: P,
|
||||
flags: OpenFlags)
|
||||
-> Result<Connection> {
|
||||
let c_path = try!(path_to_cstring(path.as_ref()));
|
||||
InnerConnection::open_with_flags(&c_path, flags).map(|db| {
|
||||
Connection {
|
||||
db: RefCell::new(db),
|
||||
path: Some(path.as_ref().to_path_buf()),
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn open_with_flags<P: AsRef<Path>>(path: P, flags: OpenFlags) -> Result<Connection> {
|
||||
let c_path = try!(path_to_cstring(path.as_ref()));
|
||||
InnerConnection::open_with_flags(&c_path, flags).map(|db| {
|
||||
Connection {
|
||||
db: RefCell::new(db),
|
||||
path: Some(path.as_ref().to_path_buf()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Open a new connection to an in-memory SQLite database.
|
||||
///
|
||||
@@ -237,7 +238,7 @@ impl Connection {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return `Err` if the underlying SQLite call fails.
|
||||
pub fn transaction<'a>(&'a self) -> Result<Transaction<'a>> {
|
||||
pub fn transaction(&self) -> Result<Transaction> {
|
||||
Transaction::new(self, TransactionBehavior::Deferred)
|
||||
}
|
||||
|
||||
@@ -248,11 +249,9 @@ impl Connection {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return `Err` if the underlying SQLite call fails.
|
||||
pub fn transaction_with_behavior<'a>(&'a self,
|
||||
behavior: TransactionBehavior)
|
||||
-> Result<Transaction<'a>> {
|
||||
Transaction::new(self, behavior)
|
||||
}
|
||||
pub fn transaction_with_behavior(&self, behavior: TransactionBehavior) -> Result<Transaction> {
|
||||
Transaction::new(self, behavior)
|
||||
}
|
||||
|
||||
/// Convenience method to run multiple SQL statements (that cannot take any parameters).
|
||||
///
|
||||
@@ -360,7 +359,11 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string or if the
|
||||
/// underlying SQLite call fails.
|
||||
pub fn query_row_and_then<T, E, F>(&self, sql: &str, params: &[&ToSql], f: F) -> result::Result<T, E>
|
||||
pub fn query_row_and_then<T, E, F>(&self,
|
||||
sql: &str,
|
||||
params: &[&ToSql],
|
||||
f: F)
|
||||
-> result::Result<T, E>
|
||||
where F: FnOnce(Row) -> result::Result<T, E>,
|
||||
E: convert::From<Error>
|
||||
{
|
||||
@@ -391,9 +394,9 @@ impl Connection {
|
||||
/// does exactly the same thing.
|
||||
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)
|
||||
}
|
||||
{
|
||||
self.query_row(sql, params, f)
|
||||
}
|
||||
|
||||
/// Prepare a SQL statement for execution.
|
||||
///
|
||||
@@ -491,9 +494,9 @@ impl Connection {
|
||||
pub fn load_extension<P: AsRef<Path>>(&self,
|
||||
dylib_path: P,
|
||||
entry_point: Option<&str>)
|
||||
-> Result<()> {
|
||||
self.db.borrow_mut().load_extension(dylib_path.as_ref(), entry_point)
|
||||
}
|
||||
-> Result<()> {
|
||||
self.db.borrow_mut().load_extension(dylib_path.as_ref(), entry_point)
|
||||
}
|
||||
|
||||
/// Get access to the underlying SQLite database connection handle.
|
||||
///
|
||||
@@ -535,7 +538,7 @@ bitflags! {
|
||||
#[doc = "Flags for opening SQLite database connections."]
|
||||
#[doc = "See [sqlite3_open_v2](http://www.sqlite.org/c3ref/open.html) for details."]
|
||||
#[repr(C)]
|
||||
flags OpenFlags: c_int {
|
||||
pub flags OpenFlags: ::libc::c_int {
|
||||
const SQLITE_OPEN_READ_ONLY = 0x00000001,
|
||||
const SQLITE_OPEN_READ_WRITE = 0x00000002,
|
||||
const SQLITE_OPEN_CREATE = 0x00000004,
|
||||
@@ -555,60 +558,54 @@ impl Default for OpenFlags {
|
||||
}
|
||||
|
||||
impl InnerConnection {
|
||||
fn open_with_flags(c_path: &CString,
|
||||
flags: OpenFlags)
|
||||
-> Result<InnerConnection> {
|
||||
unsafe {
|
||||
// Before opening the database, we need to check that SQLite hasn't been
|
||||
// compiled or configured to be in single-threaded mode. If it has, we're
|
||||
// exposing a very unsafe API to Rust, so refuse to open connections at all.
|
||||
// Unfortunately, the check for this is quite gross. sqlite3_threadsafe() only
|
||||
// returns how SQLite was _compiled_; there is no public API to check whether
|
||||
// someone called sqlite3_config() to set single-threaded mode. We can cheat
|
||||
// by trying to allocate a mutex, though; in single-threaded mode due to
|
||||
// compilation settings, the magic value 8 is returned (see the definition of
|
||||
// sqlite3_mutex_alloc at https://github.com/mackyle/sqlite/blob/master/src/mutex.h);
|
||||
// in single-threaded mode due to sqlite3_config(), the magic value 8 is also
|
||||
// returned (see the definition of noopMutexAlloc at
|
||||
// https://github.com/mackyle/sqlite/blob/master/src/mutex_noop.c).
|
||||
const SQLITE_SINGLETHREADED_MUTEX_MAGIC: usize = 8;
|
||||
let mutex_ptr = ffi::sqlite3_mutex_alloc(0);
|
||||
let is_singlethreaded = if mutex_ptr as usize == SQLITE_SINGLETHREADED_MUTEX_MAGIC {
|
||||
true
|
||||
fn open_with_flags(c_path: &CString, flags: OpenFlags) -> Result<InnerConnection> {
|
||||
unsafe {
|
||||
// Before opening the database, we need to check that SQLite hasn't been
|
||||
// compiled or configured to be in single-threaded mode. If it has, we're
|
||||
// exposing a very unsafe API to Rust, so refuse to open connections at all.
|
||||
// Unfortunately, the check for this is quite gross. sqlite3_threadsafe() only
|
||||
// returns how SQLite was _compiled_; there is no public API to check whether
|
||||
// someone called sqlite3_config() to set single-threaded mode. We can cheat
|
||||
// by trying to allocate a mutex, though; in single-threaded mode due to
|
||||
// compilation settings, the magic value 8 is returned (see the definition of
|
||||
// sqlite3_mutex_alloc at https://github.com/mackyle/sqlite/blob/master/src/mutex.h);
|
||||
// in single-threaded mode due to sqlite3_config(), the magic value 8 is also
|
||||
// returned (see the definition of noopMutexAlloc at
|
||||
// https://github.com/mackyle/sqlite/blob/master/src/mutex_noop.c).
|
||||
const SQLITE_SINGLETHREADED_MUTEX_MAGIC: usize = 8;
|
||||
let mutex_ptr = ffi::sqlite3_mutex_alloc(0);
|
||||
let is_singlethreaded = mutex_ptr as usize == SQLITE_SINGLETHREADED_MUTEX_MAGIC;
|
||||
ffi::sqlite3_mutex_free(mutex_ptr);
|
||||
if is_singlethreaded {
|
||||
return Err(Error::SqliteSingleThreadedMode);
|
||||
}
|
||||
|
||||
let mut db: *mut ffi::sqlite3 = mem::uninitialized();
|
||||
let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null());
|
||||
if r != ffi::SQLITE_OK {
|
||||
let e = if db.is_null() {
|
||||
error_from_sqlite_code(r, None)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
ffi::sqlite3_mutex_free(mutex_ptr);
|
||||
if is_singlethreaded {
|
||||
return Err(Error::SqliteSingleThreadedMode);
|
||||
}
|
||||
|
||||
let mut db: *mut ffi::sqlite3 = mem::uninitialized();
|
||||
let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null());
|
||||
if r != ffi::SQLITE_OK {
|
||||
let e = if db.is_null() {
|
||||
error_from_sqlite_code(r, None)
|
||||
} else {
|
||||
let e = error_from_handle(db, r);
|
||||
ffi::sqlite3_close(db);
|
||||
e
|
||||
};
|
||||
|
||||
return Err(e);
|
||||
}
|
||||
let r = ffi::sqlite3_busy_timeout(db, 5000);
|
||||
if r != ffi::SQLITE_OK {
|
||||
let e = error_from_handle(db, r);
|
||||
ffi::sqlite3_close(db);
|
||||
return Err(e);
|
||||
}
|
||||
e
|
||||
};
|
||||
|
||||
// attempt to turn on extended results code; don't fail if we can't.
|
||||
ffi::sqlite3_extended_result_codes(db, 1);
|
||||
|
||||
Ok(InnerConnection { db: db })
|
||||
return Err(e);
|
||||
}
|
||||
let r = ffi::sqlite3_busy_timeout(db, 5000);
|
||||
if r != ffi::SQLITE_OK {
|
||||
let e = error_from_handle(db, r);
|
||||
ffi::sqlite3_close(db);
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
// attempt to turn on extended results code; don't fail if we can't.
|
||||
ffi::sqlite3_extended_result_codes(db, 1);
|
||||
|
||||
Ok(InnerConnection { db: db })
|
||||
}
|
||||
}
|
||||
|
||||
fn db(&self) -> *mut ffi::Struct_sqlite3 {
|
||||
self.db
|
||||
@@ -634,10 +631,10 @@ impl InnerConnection {
|
||||
let c_sql = try!(str_to_cstring(sql));
|
||||
unsafe {
|
||||
let r = ffi::sqlite3_exec(self.db(),
|
||||
c_sql.as_ptr(),
|
||||
None,
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut());
|
||||
c_sql.as_ptr(),
|
||||
None,
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut());
|
||||
self.decode_result(r)
|
||||
}
|
||||
}
|
||||
@@ -676,25 +673,22 @@ impl InnerConnection {
|
||||
unsafe { ffi::sqlite3_last_insert_rowid(self.db()) }
|
||||
}
|
||||
|
||||
fn prepare<'a>(&mut self,
|
||||
conn: &'a Connection,
|
||||
sql: &str)
|
||||
-> Result<Statement<'a>> {
|
||||
if sql.len() >= ::std::i32::MAX as usize {
|
||||
return Err(error_from_sqlite_code(ffi::SQLITE_TOOBIG, None));
|
||||
}
|
||||
let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() };
|
||||
let c_sql = try!(str_to_cstring(sql));
|
||||
let r = unsafe {
|
||||
let len_with_nul = (sql.len() + 1) as c_int;
|
||||
ffi::sqlite3_prepare_v2(self.db(),
|
||||
c_sql.as_ptr(),
|
||||
len_with_nul,
|
||||
&mut c_stmt,
|
||||
ptr::null_mut())
|
||||
};
|
||||
self.decode_result(r).map(|_| Statement::new(conn, c_stmt))
|
||||
fn prepare<'a>(&mut self, conn: &'a Connection, sql: &str) -> Result<Statement<'a>> {
|
||||
if sql.len() >= ::std::i32::MAX as usize {
|
||||
return Err(error_from_sqlite_code(ffi::SQLITE_TOOBIG, None));
|
||||
}
|
||||
let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() };
|
||||
let c_sql = try!(str_to_cstring(sql));
|
||||
let r = unsafe {
|
||||
let len_with_nul = (sql.len() + 1) as c_int;
|
||||
ffi::sqlite3_prepare_v2(self.db(),
|
||||
c_sql.as_ptr(),
|
||||
len_with_nul,
|
||||
&mut c_stmt,
|
||||
ptr::null_mut())
|
||||
};
|
||||
self.decode_result(r).map(|_| Statement::new(conn, c_stmt))
|
||||
}
|
||||
|
||||
fn changes(&mut self) -> c_int {
|
||||
unsafe { ffi::sqlite3_changes(self.db()) }
|
||||
@@ -715,7 +709,6 @@ pub type SqliteStatement<'conn> = Statement<'conn>;
|
||||
pub struct Statement<'conn> {
|
||||
conn: &'conn Connection,
|
||||
stmt: *mut ffi::sqlite3_stmt,
|
||||
needs_reset: bool,
|
||||
column_count: c_int,
|
||||
}
|
||||
|
||||
@@ -724,7 +717,6 @@ impl<'conn> Statement<'conn> {
|
||||
Statement {
|
||||
conn: conn,
|
||||
stmt: stmt,
|
||||
needs_reset: false,
|
||||
column_count: unsafe { ffi::sqlite3_column_count(stmt) },
|
||||
}
|
||||
}
|
||||
@@ -798,12 +790,12 @@ impl<'conn> Statement<'conn> {
|
||||
ffi::sqlite3_reset(self.stmt);
|
||||
match r {
|
||||
ffi::SQLITE_DONE => {
|
||||
if self.column_count != 0 {
|
||||
Err(Error::ExecuteReturnedResults)
|
||||
} else {
|
||||
if self.column_count == 0 {
|
||||
Ok(self.conn.changes())
|
||||
} else {
|
||||
Err(Error::ExecuteReturnedResults)
|
||||
}
|
||||
},
|
||||
}
|
||||
ffi::SQLITE_ROW => Err(Error::ExecuteReturnedResults),
|
||||
_ => Err(self.conn.decode_result(r).unwrap_err()),
|
||||
}
|
||||
@@ -833,13 +825,10 @@ impl<'conn> Statement<'conn> {
|
||||
///
|
||||
/// Will return `Err` if binding parameters fails.
|
||||
pub fn query<'a>(&'a mut self, params: &[&ToSql]) -> Result<Rows<'a>> {
|
||||
self.reset_if_needed();
|
||||
|
||||
unsafe {
|
||||
try!(self.bind_parameters(params));
|
||||
}
|
||||
|
||||
self.needs_reset = true;
|
||||
Ok(Rows::new(self))
|
||||
}
|
||||
|
||||
@@ -852,19 +841,16 @@ impl<'conn> Statement<'conn> {
|
||||
/// # Failure
|
||||
///
|
||||
/// Will return `Err` if binding parameters fails.
|
||||
pub fn query_map<'a, T, F>(&'a mut self,
|
||||
params: &[&ToSql],
|
||||
f: F)
|
||||
-> Result<MappedRows<'a, F>>
|
||||
pub fn query_map<'a, T, F>(&'a mut self, params: &[&ToSql], f: F) -> Result<MappedRows<'a, F>>
|
||||
where F: FnMut(&Row) -> T
|
||||
{
|
||||
let row_iter = try!(self.query(params));
|
||||
{
|
||||
let row_iter = try!(self.query(params));
|
||||
|
||||
Ok(MappedRows {
|
||||
rows: row_iter,
|
||||
map: f,
|
||||
})
|
||||
}
|
||||
Ok(MappedRows {
|
||||
rows: row_iter,
|
||||
map: f,
|
||||
})
|
||||
}
|
||||
|
||||
/// Executes the prepared statement and maps a function over the resulting
|
||||
/// rows, where the function returns a `Result` with `Error` type implementing
|
||||
@@ -879,17 +865,17 @@ impl<'conn> Statement<'conn> {
|
||||
pub fn query_and_then<'a, T, E, F>(&'a mut self,
|
||||
params: &[&ToSql],
|
||||
f: F)
|
||||
-> Result<AndThenRows<'a, F>>
|
||||
-> Result<AndThenRows<'a, F>>
|
||||
where E: convert::From<Error>,
|
||||
F: FnMut(&Row) -> result::Result<T, E>
|
||||
{
|
||||
let row_iter = try!(self.query(params));
|
||||
{
|
||||
let row_iter = try!(self.query(params));
|
||||
|
||||
Ok(AndThenRows {
|
||||
rows: row_iter,
|
||||
map: f,
|
||||
})
|
||||
}
|
||||
Ok(AndThenRows {
|
||||
rows: row_iter,
|
||||
map: f,
|
||||
})
|
||||
}
|
||||
|
||||
/// Consumes the statement.
|
||||
///
|
||||
@@ -905,9 +891,9 @@ impl<'conn> Statement<'conn> {
|
||||
|
||||
unsafe fn bind_parameters(&mut self, params: &[&ToSql]) -> Result<()> {
|
||||
assert!(params.len() as c_int == ffi::sqlite3_bind_parameter_count(self.stmt),
|
||||
"incorrect number of parameters to query(): expected {}, got {}",
|
||||
ffi::sqlite3_bind_parameter_count(self.stmt),
|
||||
params.len());
|
||||
"incorrect number of parameters to query(): expected {}, got {}",
|
||||
ffi::sqlite3_bind_parameter_count(self.stmt),
|
||||
params.len());
|
||||
|
||||
for (i, p) in params.iter().enumerate() {
|
||||
try!(self.conn.decode_result(p.bind_parameter(self.stmt, (i + 1) as c_int)));
|
||||
@@ -916,15 +902,6 @@ impl<'conn> Statement<'conn> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_if_needed(&mut self) {
|
||||
if self.needs_reset {
|
||||
unsafe {
|
||||
ffi::sqlite3_reset(self.stmt);
|
||||
};
|
||||
self.needs_reset = false;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
fn clear_bindings(&mut self) {
|
||||
unsafe {
|
||||
@@ -974,7 +951,8 @@ pub struct MappedRows<'stmt, F> {
|
||||
map: F,
|
||||
}
|
||||
|
||||
impl<'stmt, T, F> Iterator for MappedRows<'stmt, F> where F: FnMut(&Row) -> T
|
||||
impl<'stmt, T, F> Iterator for MappedRows<'stmt, F>
|
||||
where F: FnMut(&Row) -> T
|
||||
{
|
||||
type Item = Result<T>;
|
||||
|
||||
@@ -991,15 +969,15 @@ pub struct AndThenRows<'stmt, F> {
|
||||
}
|
||||
|
||||
impl<'stmt, T, E, F> Iterator for AndThenRows<'stmt, F>
|
||||
where E: convert::From<Error>,
|
||||
F: FnMut(&Row) -> result::Result<T, E>
|
||||
where E: convert::From<Error>,
|
||||
F: FnMut(&Row) -> result::Result<T, E>
|
||||
{
|
||||
type Item = result::Result<T, E>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.rows.next().map(|row_result| {
|
||||
row_result.map_err(E::from)
|
||||
.and_then(|row| (self.map)(&row))
|
||||
.and_then(|row| (self.map)(&row))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1040,17 +1018,15 @@ pub type SqliteRows<'stmt> = Rows<'stmt>;
|
||||
/// `min`/`max` (which could return a stale row unless the last row happened to be the min or max,
|
||||
/// respectively).
|
||||
pub struct Rows<'stmt> {
|
||||
stmt: &'stmt Statement<'stmt>,
|
||||
stmt: Option<&'stmt Statement<'stmt>>,
|
||||
current_row: Rc<Cell<c_int>>,
|
||||
failed: bool,
|
||||
}
|
||||
|
||||
impl<'stmt> Rows<'stmt> {
|
||||
fn new(stmt: &'stmt Statement<'stmt>) -> Rows<'stmt> {
|
||||
Rows {
|
||||
stmt: stmt,
|
||||
stmt: Some(stmt),
|
||||
current_row: Rc::new(Cell::new(0)),
|
||||
failed: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1060,31 +1036,47 @@ impl<'stmt> Rows<'stmt> {
|
||||
None => Err(Error::QueryReturnedNoRows),
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
if let Some(stmt) = self.stmt.take() {
|
||||
unsafe {
|
||||
ffi::sqlite3_reset(stmt.stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'stmt> Iterator for Rows<'stmt> {
|
||||
type Item = Result<Row<'stmt>>;
|
||||
|
||||
fn next(&mut self) -> Option<Result<Row<'stmt>>> {
|
||||
if self.failed {
|
||||
return None;
|
||||
}
|
||||
match unsafe { ffi::sqlite3_step(self.stmt.stmt) } {
|
||||
ffi::SQLITE_ROW => {
|
||||
let current_row = self.current_row.get() + 1;
|
||||
self.current_row.set(current_row);
|
||||
Some(Ok(Row {
|
||||
stmt: self.stmt,
|
||||
current_row: self.current_row.clone(),
|
||||
row_idx: current_row,
|
||||
}))
|
||||
self.stmt.and_then(|stmt| {
|
||||
match unsafe { ffi::sqlite3_step(stmt.stmt) } {
|
||||
ffi::SQLITE_ROW => {
|
||||
let current_row = self.current_row.get() + 1;
|
||||
self.current_row.set(current_row);
|
||||
Some(Ok(Row {
|
||||
stmt: stmt,
|
||||
current_row: self.current_row.clone(),
|
||||
row_idx: current_row,
|
||||
}))
|
||||
}
|
||||
ffi::SQLITE_DONE => {
|
||||
self.reset();
|
||||
None
|
||||
}
|
||||
code => {
|
||||
self.reset();
|
||||
Some(Err(stmt.conn.decode_result(code).unwrap_err()))
|
||||
}
|
||||
}
|
||||
ffi::SQLITE_DONE => None,
|
||||
code => {
|
||||
self.failed = true;
|
||||
Some(Err(self.stmt.conn.decode_result(code).unwrap_err()))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'stmt> Drop for Rows<'stmt> {
|
||||
fn drop(&mut self) {
|
||||
self.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1195,9 +1187,9 @@ impl<'a> RowIndex for &'a str {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate libsqlite3_sys as ffi;
|
||||
extern crate tempdir;
|
||||
pub use super::*;
|
||||
use ffi;
|
||||
use self::tempdir::TempDir;
|
||||
pub use std::error::Error as StdError;
|
||||
pub use std::fmt;
|
||||
@@ -1246,12 +1238,11 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_open_with_flags() {
|
||||
for bad_flags in [OpenFlags::empty(),
|
||||
SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_READ_WRITE,
|
||||
SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_CREATE]
|
||||
.iter() {
|
||||
assert!(Connection::open_in_memory_with_flags(*bad_flags).is_err());
|
||||
}
|
||||
for bad_flags in &[OpenFlags::empty(),
|
||||
SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_READ_WRITE,
|
||||
SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_CREATE] {
|
||||
assert!(Connection::open_in_memory_with_flags(*bad_flags).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1427,7 +1418,7 @@ mod test {
|
||||
|
||||
assert_eq!(2i32, second.get(0));
|
||||
|
||||
match first.get_checked::<i32,i32>(0).unwrap_err() {
|
||||
match first.get_checked::<i32, i32>(0).unwrap_err() {
|
||||
Error::GetFromStaleRow => (),
|
||||
err => panic!("Unexpected error {}", err),
|
||||
}
|
||||
@@ -1475,7 +1466,7 @@ mod test {
|
||||
if version >= 3007016 {
|
||||
assert_eq!(err.extended_code, ffi::SQLITE_CONSTRAINT_NOTNULL)
|
||||
}
|
||||
},
|
||||
}
|
||||
err => panic!("Unexpected error {}", err),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user