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
1e3dc542a4
@ -37,6 +37,7 @@ script:
|
|||||||
- cargo test --features serde_json
|
- cargo test --features serde_json
|
||||||
- cargo test --features bundled
|
- cargo test --features bundled
|
||||||
- cargo test --features sqlcipher
|
- cargo test --features sqlcipher
|
||||||
|
- cargo test --features "unlock_notify bundled"
|
||||||
- cargo test --features "csvtab functions vtab"
|
- cargo test --features "csvtab functions vtab"
|
||||||
- cargo test --features "backup blob chrono csvtab functions hooks limits load_extension serde_json trace vtab"
|
- cargo test --features "backup blob chrono csvtab functions hooks limits load_extension serde_json trace vtab"
|
||||||
- cargo test --features "backup blob chrono csvtab functions hooks limits load_extension serde_json trace vtab buildtime_bindgen"
|
- cargo test --features "backup blob chrono csvtab functions hooks limits load_extension serde_json trace vtab buildtime_bindgen"
|
||||||
|
@ -28,6 +28,7 @@ buildtime_bindgen = ["libsqlite3-sys/buildtime_bindgen"]
|
|||||||
limits = []
|
limits = []
|
||||||
hooks = []
|
hooks = []
|
||||||
sqlcipher = ["libsqlite3-sys/sqlcipher"]
|
sqlcipher = ["libsqlite3-sys/sqlcipher"]
|
||||||
|
unlock_notify = ["libsqlite3-sys/unlock_notify"]
|
||||||
vtab = ["functions", "libsqlite3-sys/min_sqlite_version_3_7_7"]
|
vtab = ["functions", "libsqlite3-sys/min_sqlite_version_3_7_7"]
|
||||||
csvtab = ["csv", "vtab"]
|
csvtab = ["csv", "vtab"]
|
||||||
|
|
||||||
@ -42,7 +43,7 @@ csv = { version = "0.15", optional = true }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempdir = "0.3"
|
tempdir = "0.3"
|
||||||
lazy_static = "1.0"
|
lazy_static = "1.0"
|
||||||
regex = "0.2"
|
regex = "1.0"
|
||||||
|
|
||||||
[dependencies.libsqlite3-sys]
|
[dependencies.libsqlite3-sys]
|
||||||
path = "libsqlite3-sys"
|
path = "libsqlite3-sys"
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
environment:
|
environment:
|
||||||
matrix:
|
matrix:
|
||||||
- TARGET: 1.24.1-x86_64-pc-windows-gnu
|
- TARGET: 1.25.0-x86_64-pc-windows-gnu
|
||||||
MSYS2_BITS: 64
|
MSYS2_BITS: 64
|
||||||
- TARGET: 1.24.1-x86_64-pc-windows-msvc
|
- TARGET: 1.25.0-x86_64-pc-windows-msvc
|
||||||
VCPKG_DEFAULT_TRIPLET: x64-windows
|
VCPKG_DEFAULT_TRIPLET: x64-windows
|
||||||
VCPKGRS_DYNAMIC: 1
|
VCPKGRS_DYNAMIC: 1
|
||||||
- TARGET: nightly-x86_64-pc-windows-msvc
|
- TARGET: nightly-x86_64-pc-windows-msvc
|
||||||
|
@ -22,6 +22,8 @@ min_sqlite_version_3_7_3 = ["pkg-config", "vcpkg"]
|
|||||||
min_sqlite_version_3_7_4 = ["pkg-config", "vcpkg"]
|
min_sqlite_version_3_7_4 = ["pkg-config", "vcpkg"]
|
||||||
min_sqlite_version_3_7_7 = ["pkg-config", "vcpkg"]
|
min_sqlite_version_3_7_7 = ["pkg-config", "vcpkg"]
|
||||||
min_sqlite_version_3_7_16 = ["pkg-config", "vcpkg"]
|
min_sqlite_version_3_7_16 = ["pkg-config", "vcpkg"]
|
||||||
|
# sqlite3_unlock_notify >= 3.6.12
|
||||||
|
unlock_notify = []
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
bindgen = { version = "0.36", optional = true }
|
bindgen = { version = "0.36", optional = true }
|
||||||
|
@ -18,8 +18,8 @@ mod build {
|
|||||||
fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
|
fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
|
||||||
.expect("Could not copy bindings to output directory");
|
.expect("Could not copy bindings to output directory");
|
||||||
|
|
||||||
cc::Build::new()
|
let mut cfg = cc::Build::new();
|
||||||
.file("sqlite3/sqlite3.c")
|
cfg.file("sqlite3/sqlite3.c")
|
||||||
.flag("-DSQLITE_CORE")
|
.flag("-DSQLITE_CORE")
|
||||||
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
|
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
|
||||||
.flag("-DSQLITE_ENABLE_API_ARMOR")
|
.flag("-DSQLITE_ENABLE_API_ARMOR")
|
||||||
@ -38,8 +38,11 @@ mod build {
|
|||||||
.flag("-DSQLITE_SOUNDEX")
|
.flag("-DSQLITE_SOUNDEX")
|
||||||
.flag("-DSQLITE_THREADSAFE=1")
|
.flag("-DSQLITE_THREADSAFE=1")
|
||||||
.flag("-DSQLITE_USE_URI")
|
.flag("-DSQLITE_USE_URI")
|
||||||
.flag("-DHAVE_USLEEP=1")
|
.flag("-DHAVE_USLEEP=1");
|
||||||
.compile("libsqlite3.a");
|
if cfg!(feature = "unlock_notify") {
|
||||||
|
cfg.flag("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
|
||||||
|
}
|
||||||
|
cfg.compile("libsqlite3.a");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ impl Error {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Error {
|
Error {
|
||||||
code: code,
|
code,
|
||||||
extended_code: result_code,
|
extended_code: result_code,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,7 +208,7 @@ impl<'a, 'b> Backup<'a, 'b> {
|
|||||||
Ok(Backup {
|
Ok(Backup {
|
||||||
phantom_from: PhantomData,
|
phantom_from: PhantomData,
|
||||||
phantom_to: PhantomData,
|
phantom_to: PhantomData,
|
||||||
b: b,
|
b,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ impl Connection {
|
|||||||
.map(|_| {
|
.map(|_| {
|
||||||
Blob {
|
Blob {
|
||||||
conn: self,
|
conn: self,
|
||||||
blob: blob,
|
blob,
|
||||||
pos: 0,
|
pos: 0,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -205,14 +205,14 @@ impl<'conn> io::Seek for Blob<'conn> {
|
|||||||
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
|
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
|
||||||
let pos = match pos {
|
let pos = match pos {
|
||||||
io::SeekFrom::Start(offset) => offset as i64,
|
io::SeekFrom::Start(offset) => offset as i64,
|
||||||
io::SeekFrom::Current(offset) => self.pos as i64 + offset,
|
io::SeekFrom::Current(offset) => i64::from(self.pos) + offset,
|
||||||
io::SeekFrom::End(offset) => self.size() as i64 + offset,
|
io::SeekFrom::End(offset) => i64::from(self.size()) + offset,
|
||||||
};
|
};
|
||||||
|
|
||||||
if pos < 0 {
|
if pos < 0 {
|
||||||
Err(io::Error::new(io::ErrorKind::InvalidInput,
|
Err(io::Error::new(io::ErrorKind::InvalidInput,
|
||||||
"invalid seek to negative position"))
|
"invalid seek to negative position"))
|
||||||
} else if pos > self.size() as i64 {
|
} else if pos > i64::from(self.size()) {
|
||||||
Err(io::Error::new(io::ErrorKind::InvalidInput,
|
Err(io::Error::new(io::ErrorKind::InvalidInput,
|
||||||
"invalid seek to position past end of blob"))
|
"invalid seek to position past end of blob"))
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,7 +92,7 @@ impl<'conn> CachedStatement<'conn> {
|
|||||||
fn new(stmt: Statement<'conn>, cache: &'conn StatementCache) -> CachedStatement<'conn> {
|
fn new(stmt: Statement<'conn>, cache: &'conn StatementCache) -> CachedStatement<'conn> {
|
||||||
CachedStatement {
|
CachedStatement {
|
||||||
stmt: Some(stmt),
|
stmt: Some(stmt),
|
||||||
cache: cache,
|
cache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +51,6 @@
|
|||||||
//! ```
|
//! ```
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::mem;
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::os::raw::{c_int, c_char, c_void};
|
use std::os::raw::{c_int, c_char, c_void};
|
||||||
@ -183,7 +182,7 @@ impl<'a> ValueRef<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn free_boxed_value<T>(p: *mut c_void) {
|
unsafe extern "C" fn free_boxed_value<T>(p: *mut c_void) {
|
||||||
let _: Box<T> = Box::from_raw(mem::transmute(p));
|
let _: Box<T> = Box::from_raw(p as *mut T);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Context is a wrapper for the SQLite function evaluation context.
|
/// Context is a wrapper for the SQLite function evaluation context.
|
||||||
@ -234,7 +233,7 @@ impl<'a> Context<'a> {
|
|||||||
unsafe {
|
unsafe {
|
||||||
ffi::sqlite3_set_auxdata(self.ctx,
|
ffi::sqlite3_set_auxdata(self.ctx,
|
||||||
arg,
|
arg,
|
||||||
mem::transmute(boxed),
|
boxed as *mut c_void,
|
||||||
Some(free_boxed_value::<T>))
|
Some(free_boxed_value::<T>))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -370,10 +369,10 @@ impl InnerConnection {
|
|||||||
T: ToSql
|
T: ToSql
|
||||||
{
|
{
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
ctx: ctx,
|
ctx,
|
||||||
args: slice::from_raw_parts(argv, argc as usize),
|
args: slice::from_raw_parts(argv, argc as usize),
|
||||||
};
|
};
|
||||||
let boxed_f: *mut F = mem::transmute(ffi::sqlite3_user_data(ctx.ctx));
|
let boxed_f: *mut F = ffi::sqlite3_user_data(ctx.ctx) as *mut F;
|
||||||
assert!(!boxed_f.is_null(), "Internal error - null function pointer");
|
assert!(!boxed_f.is_null(), "Internal error - null function pointer");
|
||||||
|
|
||||||
let t = (*boxed_f)(&ctx);
|
let t = (*boxed_f)(&ctx);
|
||||||
@ -397,7 +396,7 @@ impl InnerConnection {
|
|||||||
c_name.as_ptr(),
|
c_name.as_ptr(),
|
||||||
n_arg,
|
n_arg,
|
||||||
flags,
|
flags,
|
||||||
mem::transmute(boxed_f),
|
boxed_f as *mut c_void,
|
||||||
Some(call_boxed_closure::<F, T>),
|
Some(call_boxed_closure::<F, T>),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
@ -431,7 +430,7 @@ impl InnerConnection {
|
|||||||
where D: Aggregate<A, T>,
|
where D: Aggregate<A, T>,
|
||||||
T: ToSql
|
T: ToSql
|
||||||
{
|
{
|
||||||
let boxed_aggr: *mut D = mem::transmute(ffi::sqlite3_user_data(ctx));
|
let boxed_aggr: *mut D = ffi::sqlite3_user_data(ctx) as *mut D;
|
||||||
assert!(!boxed_aggr.is_null(),
|
assert!(!boxed_aggr.is_null(),
|
||||||
"Internal error - null aggregate pointer");
|
"Internal error - null aggregate pointer");
|
||||||
|
|
||||||
@ -448,7 +447,7 @@ impl InnerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut ctx = Context {
|
let mut ctx = Context {
|
||||||
ctx: ctx,
|
ctx,
|
||||||
args: slice::from_raw_parts(argv, argc as usize),
|
args: slice::from_raw_parts(argv, argc as usize),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -462,7 +461,7 @@ impl InnerConnection {
|
|||||||
where D: Aggregate<A, T>,
|
where D: Aggregate<A, T>,
|
||||||
T: ToSql
|
T: ToSql
|
||||||
{
|
{
|
||||||
let boxed_aggr: *mut D = mem::transmute(ffi::sqlite3_user_data(ctx));
|
let boxed_aggr: *mut D = ffi::sqlite3_user_data(ctx) as *mut D;
|
||||||
assert!(!boxed_aggr.is_null(),
|
assert!(!boxed_aggr.is_null(),
|
||||||
"Internal error - null aggregate pointer");
|
"Internal error - null aggregate pointer");
|
||||||
|
|
||||||
@ -500,7 +499,7 @@ impl InnerConnection {
|
|||||||
c_name.as_ptr(),
|
c_name.as_ptr(),
|
||||||
n_arg,
|
n_arg,
|
||||||
flags,
|
flags,
|
||||||
mem::transmute(boxed_aggr),
|
boxed_aggr as *mut c_void,
|
||||||
None,
|
None,
|
||||||
Some(call_boxed_step::<A, D, T>),
|
Some(call_boxed_step::<A, D, T>),
|
||||||
Some(call_boxed_final::<A, D, T>),
|
Some(call_boxed_final::<A, D, T>),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
//! Commit, Data Change and Rollback Notification Callbacks
|
//! Commit, Data Change and Rollback Notification Callbacks
|
||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
|
|
||||||
use std::mem;
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::os::raw::{c_int, c_char, c_void};
|
use std::os::raw::{c_int, c_char, c_void};
|
||||||
|
|
||||||
@ -154,7 +153,7 @@ impl InnerConnection {
|
|||||||
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int
|
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int
|
||||||
where F: FnMut() -> bool
|
where F: FnMut() -> bool
|
||||||
{
|
{
|
||||||
let boxed_hook: *mut F = mem::transmute(p_arg);
|
let boxed_hook: *mut F = p_arg as *mut F;
|
||||||
assert!(!boxed_hook.is_null(),
|
assert!(!boxed_hook.is_null(),
|
||||||
"Internal error - null function pointer");
|
"Internal error - null function pointer");
|
||||||
|
|
||||||
@ -178,7 +177,7 @@ impl InnerConnection {
|
|||||||
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void)
|
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void)
|
||||||
where F: FnMut()
|
where F: FnMut()
|
||||||
{
|
{
|
||||||
let boxed_hook: *mut F = mem::transmute(p_arg);
|
let boxed_hook: *mut F = p_arg as *mut F;
|
||||||
assert!(!boxed_hook.is_null(),
|
assert!(!boxed_hook.is_null(),
|
||||||
"Internal error - null function pointer");
|
"Internal error - null function pointer");
|
||||||
|
|
||||||
@ -209,7 +208,7 @@ impl InnerConnection {
|
|||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
let boxed_hook: *mut F = mem::transmute(p_arg);
|
let boxed_hook: *mut F = p_arg as *mut F;
|
||||||
assert!(!boxed_hook.is_null(),
|
assert!(!boxed_hook.is_null(),
|
||||||
"Internal error - null function pointer");
|
"Internal error - null function pointer");
|
||||||
|
|
||||||
|
37
src/lib.rs
37
src/lib.rs
@ -125,6 +125,7 @@ pub mod limits;
|
|||||||
mod hooks;
|
mod hooks;
|
||||||
#[cfg(feature = "hooks")]
|
#[cfg(feature = "hooks")]
|
||||||
pub use hooks::*;
|
pub use hooks::*;
|
||||||
|
mod unlock_notify;
|
||||||
#[cfg(feature = "vtab")]
|
#[cfg(feature = "vtab")]
|
||||||
pub mod vtab;
|
pub mod vtab;
|
||||||
|
|
||||||
@ -781,7 +782,7 @@ impl InnerConnection {
|
|||||||
// attempt to turn on extended results code; don't fail if we can't.
|
// attempt to turn on extended results code; don't fail if we can't.
|
||||||
ffi::sqlite3_extended_result_codes(db, 1);
|
ffi::sqlite3_extended_result_codes(db, 1);
|
||||||
|
|
||||||
Ok(InnerConnection { db: db })
|
Ok(InnerConnection { db })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -864,16 +865,40 @@ impl InnerConnection {
|
|||||||
}
|
}
|
||||||
let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() };
|
let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() };
|
||||||
let c_sql = try!(str_to_cstring(sql));
|
let c_sql = try!(str_to_cstring(sql));
|
||||||
let r = unsafe {
|
|
||||||
let len_with_nul = (sql.len() + 1) as c_int;
|
let len_with_nul = (sql.len() + 1) as c_int;
|
||||||
ffi::sqlite3_prepare_v2(self.db(),
|
let r = unsafe {
|
||||||
|
if cfg!(feature = "unlock_notify") {
|
||||||
|
let mut rc;
|
||||||
|
loop {
|
||||||
|
rc = ffi::sqlite3_prepare_v2(
|
||||||
|
self.db(),
|
||||||
c_sql.as_ptr(),
|
c_sql.as_ptr(),
|
||||||
len_with_nul,
|
len_with_nul,
|
||||||
&mut c_stmt,
|
&mut c_stmt,
|
||||||
ptr::null_mut())
|
ptr::null_mut(),
|
||||||
|
);
|
||||||
|
if !unlock_notify::is_locked(self.db, rc) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rc = unlock_notify::wait_for_unlock_notify(self.db);
|
||||||
|
if rc != ffi::SQLITE_OK {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rc
|
||||||
|
} else {
|
||||||
|
ffi::sqlite3_prepare_v2(
|
||||||
|
self.db(),
|
||||||
|
c_sql.as_ptr(),
|
||||||
|
len_with_nul,
|
||||||
|
&mut c_stmt,
|
||||||
|
ptr::null_mut(),
|
||||||
|
)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
self.decode_result(r)
|
self.decode_result(r).map(|_| {
|
||||||
.map(|_| Statement::new(conn, RawStatement::new(c_stmt)))
|
Statement::new(conn, RawStatement::new(c_stmt))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn changes(&mut self) -> c_int {
|
fn changes(&mut self) -> c_int {
|
||||||
|
@ -26,7 +26,7 @@ impl<'conn> LoadExtensionGuard<'conn> {
|
|||||||
/// guard goes out of scope. Cannot be meaningfully nested.
|
/// guard goes out of scope. Cannot be meaningfully nested.
|
||||||
pub fn new(conn: &Connection) -> Result<LoadExtensionGuard> {
|
pub fn new(conn: &Connection) -> Result<LoadExtensionGuard> {
|
||||||
conn.load_extension_enable()
|
conn.load_extension_enable()
|
||||||
.map(|_| LoadExtensionGuard { conn: conn })
|
.map(|_| LoadExtensionGuard { conn })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ use std::ffi::CStr;
|
|||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::os::raw::c_int;
|
use std::os::raw::c_int;
|
||||||
use super::ffi;
|
use super::ffi;
|
||||||
|
use super::unlock_notify;
|
||||||
|
|
||||||
// Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
|
// Private newtype for raw sqlite3_stmts that finalize themselves when dropped.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -29,8 +30,25 @@ impl RawStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&self) -> c_int {
|
pub fn step(&self) -> c_int {
|
||||||
|
if cfg!(feature = "unlock_notify") {
|
||||||
|
let db = unsafe { ffi::sqlite3_db_handle(self.0) };
|
||||||
|
let mut rc;
|
||||||
|
loop {
|
||||||
|
rc = unsafe { ffi::sqlite3_step(self.0) };
|
||||||
|
if !unlock_notify::is_locked(db, rc) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rc = unlock_notify::wait_for_unlock_notify(db);
|
||||||
|
if rc != ffi::SQLITE_OK {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
self.reset();
|
||||||
|
}
|
||||||
|
rc
|
||||||
|
} else {
|
||||||
unsafe { ffi::sqlite3_step(self.0) }
|
unsafe { ffi::sqlite3_step(self.0) }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reset(&self) -> c_int {
|
pub fn reset(&self) -> c_int {
|
||||||
unsafe { ffi::sqlite3_reset(self.0) }
|
unsafe { ffi::sqlite3_reset(self.0) }
|
||||||
|
@ -32,7 +32,7 @@ impl<'stmt> Rows<'stmt> {
|
|||||||
.and_then(|stmt| match stmt.step() {
|
.and_then(|stmt| match stmt.step() {
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
Some(Ok(Row {
|
Some(Ok(Row {
|
||||||
stmt: stmt,
|
stmt,
|
||||||
phantom: PhantomData,
|
phantom: PhantomData,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -90,7 +90,7 @@ impl<'stmt, T, F> MappedRowsCrateImpl<'stmt, T, F> for MappedRows<'stmt, F>
|
|||||||
where F: FnMut(&Row) -> T
|
where F: FnMut(&Row) -> T
|
||||||
{
|
{
|
||||||
fn new(rows: Rows<'stmt>, f: F) -> MappedRows<'stmt, F> {
|
fn new(rows: Rows<'stmt>, f: F) -> MappedRows<'stmt, F> {
|
||||||
MappedRows { rows: rows, map: f }
|
MappedRows { rows, map: f }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ impl<'stmt, T, E, F> AndThenRowsCrateImpl<'stmt, T, E, F> for AndThenRows<'stmt,
|
|||||||
where F: FnMut(&Row) -> result::Result<T, E>
|
where F: FnMut(&Row) -> result::Result<T, E>
|
||||||
{
|
{
|
||||||
fn new(rows: Rows<'stmt>, f: F) -> AndThenRows<'stmt, F> {
|
fn new(rows: Rows<'stmt>, f: F) -> AndThenRows<'stmt, F> {
|
||||||
AndThenRows { rows: rows, map: f }
|
AndThenRows { rows, map: f }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,8 +506,8 @@ pub trait StatementCrateImpl<'conn> {
|
|||||||
impl<'conn> StatementCrateImpl<'conn> for Statement<'conn> {
|
impl<'conn> StatementCrateImpl<'conn> for Statement<'conn> {
|
||||||
fn new(conn: &Connection, stmt: RawStatement) -> Statement {
|
fn new(conn: &Connection, stmt: RawStatement) -> Statement {
|
||||||
Statement {
|
Statement {
|
||||||
conn: conn,
|
conn,
|
||||||
stmt: stmt,
|
stmt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ impl<'conn> Transaction<'conn> {
|
|||||||
conn.execute_batch(query)
|
conn.execute_batch(query)
|
||||||
.map(move |_| {
|
.map(move |_| {
|
||||||
Transaction {
|
Transaction {
|
||||||
conn: conn,
|
conn,
|
||||||
drop_behavior: DropBehavior::Rollback,
|
drop_behavior: DropBehavior::Rollback,
|
||||||
committed: false,
|
committed: false,
|
||||||
}
|
}
|
||||||
@ -227,9 +227,9 @@ impl<'conn> Savepoint<'conn> {
|
|||||||
conn.execute_batch(&format!("SAVEPOINT {}", name))
|
conn.execute_batch(&format!("SAVEPOINT {}", name))
|
||||||
.map(|_| {
|
.map(|_| {
|
||||||
Savepoint {
|
Savepoint {
|
||||||
conn: conn,
|
conn,
|
||||||
name: name,
|
name,
|
||||||
depth: depth,
|
depth,
|
||||||
drop_behavior: DropBehavior::Rollback,
|
drop_behavior: DropBehavior::Rollback,
|
||||||
committed: false,
|
committed: false,
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ impl FromSql for time::Timespec {
|
|||||||
time::strptime(s, SQLITE_DATETIME_FMT)
|
time::strptime(s, SQLITE_DATETIME_FMT)
|
||||||
.or_else(|err| {
|
.or_else(|err| {
|
||||||
time::strptime(s, SQLITE_DATETIME_FMT_LEGACY)
|
time::strptime(s, SQLITE_DATETIME_FMT_LEGACY)
|
||||||
.or(Err(FromSqlError::Other(Box::new(err))))})})
|
.or_else(|_| Err(FromSqlError::Other(Box::new(err))))})})
|
||||||
.map(|tm| tm.to_timespec())
|
.map(|tm| tm.to_timespec())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
129
src/unlock_notify.rs
Normal file
129
src/unlock_notify.rs
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
//! [Unlock Notification](http://sqlite.org/unlock_notify.html)
|
||||||
|
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
use std::sync::{Condvar, Mutex};
|
||||||
|
use std::os::raw::c_int;
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
use std::os::raw::c_void;
|
||||||
|
|
||||||
|
use ffi;
|
||||||
|
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
struct UnlockNotification {
|
||||||
|
cond: Condvar, // Condition variable to wait on
|
||||||
|
mutex: Mutex<bool>, // Mutex to protect structure
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
impl UnlockNotification {
|
||||||
|
fn new() -> UnlockNotification {
|
||||||
|
UnlockNotification {
|
||||||
|
cond: Condvar::new(),
|
||||||
|
mutex: Mutex::new(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fired(&mut self) {
|
||||||
|
*self.mutex.lock().unwrap() = true;
|
||||||
|
self.cond.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait(&mut self) {
|
||||||
|
let mut fired = self.mutex.lock().unwrap();
|
||||||
|
while !*fired {
|
||||||
|
fired = self.cond.wait(fired).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This function is an unlock-notify callback
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
unsafe extern "C" fn unlock_notify_cb(ap_arg: *mut *mut c_void, n_arg: c_int) {
|
||||||
|
use std::slice::from_raw_parts;
|
||||||
|
let args = from_raw_parts(ap_arg, n_arg as usize);
|
||||||
|
for arg in args {
|
||||||
|
let un: &mut UnlockNotification = &mut *(*arg as *mut UnlockNotification);
|
||||||
|
un.fired();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
pub fn is_locked(db: *mut ffi::sqlite3, rc: c_int) -> bool {
|
||||||
|
rc == ffi::SQLITE_LOCKED_SHAREDCACHE || (rc & 0xFF) == ffi::SQLITE_LOCKED && unsafe {
|
||||||
|
ffi::sqlite3_extended_errcode(db)
|
||||||
|
}
|
||||||
|
== ffi::SQLITE_LOCKED_SHAREDCACHE
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This function assumes that an SQLite API call (either `sqlite3_prepare_v2()`
|
||||||
|
/// or `sqlite3_step()`) has just returned `SQLITE_LOCKED`. The argument is the
|
||||||
|
/// associated database connection.
|
||||||
|
///
|
||||||
|
/// This function calls `sqlite3_unlock_notify()` to register for an
|
||||||
|
/// unlock-notify callback, then blocks until that callback is delivered
|
||||||
|
/// and returns `SQLITE_OK`. The caller should then retry the failed operation.
|
||||||
|
///
|
||||||
|
/// Or, if `sqlite3_unlock_notify()` indicates that to block would deadlock
|
||||||
|
/// the system, then this function returns `SQLITE_LOCKED` immediately. In
|
||||||
|
/// this case the caller should not retry the operation and should roll
|
||||||
|
/// back the current transaction (if any).
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
pub fn wait_for_unlock_notify(db: *mut ffi::sqlite3) -> c_int {
|
||||||
|
let mut un = UnlockNotification::new();
|
||||||
|
/* Register for an unlock-notify callback. */
|
||||||
|
let rc = unsafe {
|
||||||
|
ffi::sqlite3_unlock_notify(
|
||||||
|
db,
|
||||||
|
Some(unlock_notify_cb),
|
||||||
|
&mut un as *mut UnlockNotification as *mut c_void,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
debug_assert!(
|
||||||
|
rc == ffi::SQLITE_LOCKED || rc == ffi::SQLITE_LOCKED_SHAREDCACHE || rc == ffi::SQLITE_OK
|
||||||
|
);
|
||||||
|
if rc == ffi::SQLITE_OK {
|
||||||
|
un.wait();
|
||||||
|
}
|
||||||
|
rc
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "unlock_notify"))]
|
||||||
|
pub fn is_locked(_db: *mut ffi::sqlite3, _rc: c_int) -> bool {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "unlock_notify"))]
|
||||||
|
pub fn wait_for_unlock_notify(_db: *mut ffi::sqlite3) -> c_int {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "unlock_notify")]
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use std::sync::mpsc::sync_channel;
|
||||||
|
use std::thread;
|
||||||
|
use std::time;
|
||||||
|
use {Connection, OpenFlags, Result, Transaction, TransactionBehavior};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_unlock_notify() {
|
||||||
|
let url = "file::memory:?cache=shared";
|
||||||
|
let flags = OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_URI;
|
||||||
|
let db1 = Connection::open_with_flags(url, flags).unwrap();
|
||||||
|
db1.execute_batch("CREATE TABLE foo (x)").unwrap();
|
||||||
|
let (rx, tx) = sync_channel(0);
|
||||||
|
let child = thread::spawn(move || {
|
||||||
|
let mut db2 = Connection::open_with_flags(url, flags).unwrap();
|
||||||
|
let tx2 = Transaction::new(&mut db2, TransactionBehavior::Immediate).unwrap();
|
||||||
|
tx2.execute_batch("INSERT INTO foo VALUES (42)").unwrap();
|
||||||
|
rx.send(1).unwrap();
|
||||||
|
let ten_millis = time::Duration::from_millis(10);
|
||||||
|
thread::sleep(ten_millis);
|
||||||
|
tx2.commit().unwrap();
|
||||||
|
});
|
||||||
|
assert_eq!(tx.recv().unwrap(), 1);
|
||||||
|
let the_answer: Result<i64> = db1.query_row("SELECT x FROM foo", &[], |r| r.get(0));
|
||||||
|
assert_eq!(42i64, the_answer.unwrap());
|
||||||
|
child.join().unwrap();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user