Make SQLite copy data when BLOB or TEXT

This commit is contained in:
Ivan Ukhov 2015-11-21 20:42:08 +01:00
parent 5691098657
commit edb26a9fa8

View File

@ -1,10 +1,12 @@
use ffi; use ffi;
use libc::{c_double, c_int, c_void}; use libc::{c_double, c_int};
use std::ffi::CString;
use std::marker::PhantomData; use std::marker::PhantomData;
use {Cursor, Result, Type, Value}; use {Cursor, Result, Type, Value};
// https://sqlite.org/c3ref/c_static.html
macro_rules! transient(() => (::std::mem::transmute(!0 as *const ::libc::c_void)));
/// A prepared statement. /// A prepared statement.
pub struct Statement<'l> { pub struct Statement<'l> {
raw: (*mut ffi::sqlite3_stmt, *mut ffi::sqlite3), raw: (*mut ffi::sqlite3_stmt, *mut ffi::sqlite3),
@ -126,7 +128,8 @@ impl<'l> Bindable for &'l [u8] {
unsafe { unsafe {
ok!(statement.raw.1, ffi::sqlite3_bind_blob(statement.raw.0, i as c_int, ok!(statement.raw.1, ffi::sqlite3_bind_blob(statement.raw.0, i as c_int,
self.as_ptr() as *const _, self.as_ptr() as *const _,
self.len() as c_int, None)); self.len() as c_int,
transient!()));
} }
Ok(()) Ok(())
} }
@ -162,8 +165,9 @@ impl<'l> Bindable for &'l str {
debug_assert!(i > 0, "the indexing starts from 1"); debug_assert!(i > 0, "the indexing starts from 1");
unsafe { unsafe {
ok!(statement.raw.1, ffi::sqlite3_bind_text(statement.raw.0, i as c_int, ok!(statement.raw.1, ffi::sqlite3_bind_text(statement.raw.0, i as c_int,
str_to_cstr!(self).into_raw(), -1, self.as_ptr() as *const _,
Some(drop_cstring))); self.len() as c_int,
transient!()));
} }
Ok(()) Ok(())
} }
@ -246,7 +250,3 @@ pub fn new<'l, T: AsRef<str>>(raw1: *mut ffi::sqlite3, statement: T) -> Result<S
} }
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData }) Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
} }
extern "C" fn drop_cstring(pointer: *mut c_void) {
let _ = unsafe { CString::from_raw(pointer as *mut _) };
}