mirror of
https://github.com/isar/rusqlite.git
synced 2025-09-16 12:42:18 +08:00
Rework ToSql to be implementable without unsafe
.
This commit is contained in:
63
src/lib.rs
63
src/lib.rs
@@ -73,9 +73,9 @@ use std::cell::RefCell;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::result;
|
||||
use std::str;
|
||||
use libc::{c_int, c_char};
|
||||
use libc::{c_int, c_char, c_void};
|
||||
|
||||
use types::{ToSql, FromSql, ValueRef};
|
||||
use types::{ToSql, ToSqlOutput, FromSql, ValueRef};
|
||||
use error::{error_from_sqlite_code, error_from_handle};
|
||||
use raw_statement::RawStatement;
|
||||
use cache::StatementCache;
|
||||
@@ -874,6 +874,54 @@ impl<'conn> Statement<'conn> {
|
||||
self.finalize_()
|
||||
}
|
||||
|
||||
fn bind_parameter(&self, param: &ToSql, col: c_int) -> Result<()> {
|
||||
// This should be
|
||||
// let value = try!(param.to_sql());
|
||||
// but that hits a bug in the Rust compiler around re-exported
|
||||
// trait visibility. It's fixed in 1.9.
|
||||
let value = try!(ToSql::to_sql(param));
|
||||
|
||||
let ptr = unsafe { self.stmt.ptr() };
|
||||
let value = match value {
|
||||
ToSqlOutput::Borrowed(v) => v,
|
||||
ToSqlOutput::Owned(ref v) => ValueRef::from(v),
|
||||
|
||||
#[cfg(feature = "blob")]
|
||||
ToSqlOutput::ZeroBlob(len) => {
|
||||
return self.conn.decode_result(unsafe { ffi::sqlite3_bind_zeroblob(ptr, col, len) });
|
||||
}
|
||||
};
|
||||
self.conn.decode_result(match value {
|
||||
ValueRef::Null => unsafe { ffi::sqlite3_bind_null(ptr, col) },
|
||||
ValueRef::Integer(i) => unsafe { ffi::sqlite3_bind_int64(ptr, col, i) },
|
||||
ValueRef::Real(r) => unsafe { ffi::sqlite3_bind_double(ptr, col, r) },
|
||||
ValueRef::Text(ref s) => unsafe {
|
||||
let length = s.len();
|
||||
if length > ::std::i32::MAX as usize {
|
||||
ffi::SQLITE_TOOBIG
|
||||
} else {
|
||||
let c_str = try!(str_to_cstring(s));
|
||||
let destructor = if length > 0 {
|
||||
ffi::SQLITE_TRANSIENT()
|
||||
} else {
|
||||
ffi::SQLITE_STATIC()
|
||||
};
|
||||
ffi::sqlite3_bind_text(ptr, col, c_str.as_ptr(), length as c_int, destructor)
|
||||
}
|
||||
},
|
||||
ValueRef::Blob(ref b) => unsafe {
|
||||
let length = b.len();
|
||||
if length > ::std::i32::MAX as usize {
|
||||
ffi::SQLITE_TOOBIG
|
||||
} else if length == 0 {
|
||||
ffi::sqlite3_bind_zeroblob(ptr, col, 0)
|
||||
} else {
|
||||
ffi::sqlite3_bind_blob(ptr, col, b.as_ptr() as *const c_void, length as c_int, ffi::SQLITE_TRANSIENT())
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn bind_parameters(&mut self, params: &[&ToSql]) -> Result<()> {
|
||||
assert!(params.len() as c_int == self.stmt.bind_parameter_count(),
|
||||
"incorrect number of parameters to query(): expected {}, got {}",
|
||||
@@ -881,14 +929,7 @@ impl<'conn> Statement<'conn> {
|
||||
params.len());
|
||||
|
||||
for (i, p) in params.iter().enumerate() {
|
||||
try!(unsafe {
|
||||
self.conn.decode_result(
|
||||
// This should be
|
||||
// `p.bind_parameter(self.stmt.ptr(), (i + 1) as c_int)`
|
||||
// but that doesn't compile until Rust 1.9 due to a compiler bug.
|
||||
ToSql::bind_parameter(*p, self.stmt.ptr(), (i + 1) as c_int)
|
||||
)
|
||||
});
|
||||
try!(self.bind_parameter(*p, (i + 1) as c_int));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -1128,7 +1169,7 @@ impl<'a> ValueRef<'a> {
|
||||
|
||||
ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize))
|
||||
}
|
||||
_ => unreachable!("sqlite3_column_type returned invalid value")
|
||||
_ => unreachable!("sqlite3_column_type returned invalid value"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user