mirror of
https://github.com/fluencelabs/sqlite-wasm-connector
synced 2025-03-15 06:20:50 +00:00
Fix the handling of C strings
This commit is contained in:
parent
4fac86f604
commit
ebbf073c82
@ -17,7 +17,7 @@ impl<'l> Connection<'l> {
|
||||
pub fn open<T: AsRef<Path>>(path: T) -> Result<Connection<'l>> {
|
||||
let mut raw = 0 as *mut _;
|
||||
unsafe {
|
||||
ok!(ffi::sqlite3_open_v2(path_to_c_str!(path.as_ref()), &mut raw,
|
||||
ok!(ffi::sqlite3_open_v2(path_to_cstr!(path.as_ref()).as_ptr(), &mut raw,
|
||||
ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE,
|
||||
0 as *const _));
|
||||
}
|
||||
@ -32,8 +32,8 @@ impl<'l> Connection<'l> {
|
||||
#[inline]
|
||||
pub fn execute(&self, sql: &str) -> Result<()> {
|
||||
unsafe {
|
||||
ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
|
||||
0 as *mut _));
|
||||
ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_cstr!(sql).as_ptr(), None,
|
||||
0 as *mut _, 0 as *mut _));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -49,7 +49,7 @@ impl<'l> Connection<'l> {
|
||||
{
|
||||
unsafe {
|
||||
let callback = Box::new(callback);
|
||||
ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql),
|
||||
ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_cstr!(sql).as_ptr(),
|
||||
Some(process_callback::<F>),
|
||||
&*callback as *const F as *mut F as *mut _,
|
||||
0 as *mut _));
|
||||
|
62
src/lib.rs
62
src/lib.rs
@ -37,38 +37,20 @@ macro_rules! raise(
|
||||
);
|
||||
|
||||
macro_rules! error(
|
||||
($connection:expr, $code:expr) => (
|
||||
match ::error::last($connection) {
|
||||
Some(error) => return Err(error),
|
||||
None => return Err(::Error::from(::ErrorKind::from($code as isize))),
|
||||
}
|
||||
);
|
||||
($connection:expr, $code:expr) => (match ::error::last($connection) {
|
||||
Some(error) => return Err(error),
|
||||
None => return Err(::Error::from(::ErrorKind::from($code as isize))),
|
||||
});
|
||||
);
|
||||
|
||||
macro_rules! ok(
|
||||
($connection:expr, $result:expr) => (
|
||||
match $result {
|
||||
::ffi::SQLITE_OK => {},
|
||||
code => error!($connection, code),
|
||||
}
|
||||
);
|
||||
($result:expr) => (
|
||||
match $result {
|
||||
::ffi::SQLITE_OK => {},
|
||||
code => return Err(::Error::from(::ErrorKind::from(code as isize))),
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
macro_rules! path_to_c_str(
|
||||
($path:expr) => ({
|
||||
match $path.to_str() {
|
||||
Some(path) => match ::std::ffi::CString::new(path) {
|
||||
Ok(string) => string.as_ptr(),
|
||||
Err(_) => raise!("failed to process a path"),
|
||||
},
|
||||
None => raise!("failed to process a path"),
|
||||
}
|
||||
($connection:expr, $result:expr) => (match $result {
|
||||
::ffi::SQLITE_OK => {},
|
||||
code => error!($connection, code),
|
||||
});
|
||||
($result:expr) => (match $result {
|
||||
::ffi::SQLITE_OK => {},
|
||||
code => return Err(::Error::from(::ErrorKind::from(code as isize))),
|
||||
});
|
||||
);
|
||||
|
||||
@ -83,13 +65,21 @@ macro_rules! c_str_to_string(
|
||||
);
|
||||
);
|
||||
|
||||
macro_rules! str_to_c_str(
|
||||
($string:expr) => (
|
||||
match ::std::ffi::CString::new($string) {
|
||||
Ok(string) => string.as_ptr(),
|
||||
Err(_) => raise!("failed to process a string"),
|
||||
}
|
||||
);
|
||||
macro_rules! path_to_cstr(
|
||||
($path:expr) => (match $path.to_str() {
|
||||
Some(path) => match ::std::ffi::CString::new(path) {
|
||||
Ok(string) => string,
|
||||
_ => raise!("failed to process a path"),
|
||||
},
|
||||
_ => raise!("failed to process a path"),
|
||||
});
|
||||
);
|
||||
|
||||
macro_rules! str_to_cstr(
|
||||
($string:expr) => (match ::std::ffi::CString::new($string) {
|
||||
Ok(string) => string,
|
||||
_ => raise!("failed to process a string"),
|
||||
});
|
||||
);
|
||||
|
||||
mod connection;
|
||||
|
@ -109,7 +109,7 @@ impl<'l> Parameter for &'l str {
|
||||
debug_assert!(i > 0, "the indexing starts from 1");
|
||||
unsafe {
|
||||
ok!(statement.raw.1, ffi::sqlite3_bind_text(statement.raw.0, i as c_int,
|
||||
str_to_c_str!(*self), -1, None));
|
||||
str_to_cstr!(*self).as_ptr(), -1, None));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -146,7 +146,8 @@ impl Value for String {
|
||||
pub fn new<'l>(raw1: *mut ffi::sqlite3, sql: &str) -> Result<Statement<'l>> {
|
||||
let mut raw0 = 0 as *mut _;
|
||||
unsafe {
|
||||
ok!(raw1, ffi::sqlite3_prepare_v2(raw1, str_to_c_str!(sql), -1, &mut raw0, 0 as *mut _));
|
||||
ok!(raw1, ffi::sqlite3_prepare_v2(raw1, str_to_cstr!(sql).as_ptr(), -1, &mut raw0,
|
||||
0 as *mut _));
|
||||
}
|
||||
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user