mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 11:31:37 +08:00
Merge remote-tracking branch 'jgallagher/master' into vtab
This commit is contained in:
commit
29373e7d0d
@ -23,7 +23,7 @@ csvtab = ["csv"]
|
||||
|
||||
[dependencies]
|
||||
time = "~0.1.0"
|
||||
bitflags = "~0.1"
|
||||
bitflags = "0.7"
|
||||
libc = "~0.2"
|
||||
clippy = {version = "~0.0.58", optional = true}
|
||||
chrono = { version = "~0.2", optional = true }
|
||||
|
@ -1,5 +1,5 @@
|
||||
environment:
|
||||
TARGET: 1.6.0-x86_64-pc-windows-gnu
|
||||
TARGET: 1.8.0-x86_64-pc-windows-gnu
|
||||
MSYS2_BITS: 64
|
||||
install:
|
||||
- ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:TARGET}.exe"
|
||||
|
@ -159,9 +159,8 @@ impl<'conn> io::Read for Blob<'conn> {
|
||||
if n <= 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let rc = unsafe {
|
||||
ffi::sqlite3_blob_read(self.blob, mem::transmute(buf.as_ptr()), n, self.pos)
|
||||
};
|
||||
let rc =
|
||||
unsafe { ffi::sqlite3_blob_read(self.blob, mem::transmute(buf.as_ptr()), n, self.pos) };
|
||||
self.conn
|
||||
.decode_result(rc)
|
||||
.map(|_| {
|
||||
|
@ -23,12 +23,14 @@ impl<'conn> Statement<'conn> {
|
||||
/// Return `true` if a query in the SQL statement it executes returns one or more rows
|
||||
/// and `false` if the SQL returns an empty set.
|
||||
pub fn exists(&mut self, params: &[&ToSql]) -> Result<bool> {
|
||||
self.reset_if_needed();
|
||||
let mut rows = try!(self.query(params));
|
||||
let exists = {
|
||||
match rows.next() {
|
||||
Some(_) => Ok(true),
|
||||
None => Ok(false),
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
}
|
||||
};
|
||||
Ok(exists)
|
||||
}
|
||||
}
|
||||
|
||||
|
54
src/lib.rs
54
src/lib.rs
@ -538,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,
|
||||
@ -709,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,
|
||||
}
|
||||
|
||||
@ -718,7 +717,6 @@ impl<'conn> Statement<'conn> {
|
||||
Statement {
|
||||
conn: conn,
|
||||
stmt: stmt,
|
||||
needs_reset: false,
|
||||
column_count: unsafe { ffi::sqlite3_column_count(stmt) },
|
||||
}
|
||||
}
|
||||
@ -827,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))
|
||||
}
|
||||
|
||||
@ -907,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;
|
||||
}
|
||||
}
|
||||
|
||||
fn finalize_(&mut self) -> Result<()> {
|
||||
let r = unsafe { ffi::sqlite3_finalize(self.stmt) };
|
||||
self.stmt = ptr::null_mut();
|
||||
@ -1017,17 +1003,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,
|
||||
}
|
||||
}
|
||||
|
||||
@ -1037,31 +1021,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) } {
|
||||
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: self.stmt,
|
||||
stmt: stmt,
|
||||
current_row: self.current_row.clone(),
|
||||
row_idx: current_row,
|
||||
}))
|
||||
}
|
||||
ffi::SQLITE_DONE => None,
|
||||
ffi::SQLITE_DONE => {
|
||||
self.reset();
|
||||
None
|
||||
}
|
||||
code => {
|
||||
self.failed = true;
|
||||
Some(Err(self.stmt.conn.decode_result(code).unwrap_err()))
|
||||
self.reset();
|
||||
Some(Err(stmt.conn.decode_result(code).unwrap_err()))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'stmt> Drop for Rows<'stmt> {
|
||||
fn drop(&mut self) {
|
||||
self.reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,10 +113,7 @@ impl<'conn> Statement<'conn> {
|
||||
///
|
||||
/// Will return `Err` if binding parameters fails.
|
||||
pub fn query_named<'a>(&'a mut self, params: &[(&str, &ToSql)]) -> Result<Rows<'a>> {
|
||||
self.reset_if_needed();
|
||||
try!(self.bind_parameters_named(params));
|
||||
|
||||
self.needs_reset = true;
|
||||
Ok(Rows::new(self))
|
||||
}
|
||||
|
||||
@ -190,9 +187,8 @@ mod test {
|
||||
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)").unwrap();
|
||||
stmt.execute_named(&[(":x", &"one")]).unwrap();
|
||||
|
||||
let result: Option<String> = db.query_row("SELECT y FROM test WHERE x = 'one'",
|
||||
&[],
|
||||
|row| row.get(0))
|
||||
let result: Option<String> =
|
||||
db.query_row("SELECT y FROM test WHERE x = 'one'", &[], |row| row.get(0))
|
||||
.unwrap();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
@ -207,9 +203,8 @@ mod test {
|
||||
stmt.execute_named(&[(":x", &"one")]).unwrap();
|
||||
stmt.execute_named(&[(":y", &"two")]).unwrap();
|
||||
|
||||
let result: String = db.query_row("SELECT x FROM test WHERE y = 'two'",
|
||||
&[],
|
||||
|row| row.get(0))
|
||||
let result: String =
|
||||
db.query_row("SELECT x FROM test WHERE y = 'two'", &[], |row| row.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(result, "one");
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ use libc::{c_char, c_int, c_void};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
use std::str;
|
||||
use std::time::Duration;
|
||||
|
||||
use super::ffi;
|
||||
|
Loading…
Reference in New Issue
Block a user