mirror of
https://github.com/isar/rusqlite.git
synced 2025-10-30 21:38:57 +08:00
@@ -22,6 +22,18 @@ pub enum FromSqlError {
|
||||
Other(Box<dyn Error + Send + Sync>),
|
||||
}
|
||||
|
||||
impl PartialEq for FromSqlError {
|
||||
fn eq(&self, other: &FromSqlError) -> bool {
|
||||
match (self, other) {
|
||||
(FromSqlError::InvalidType, FromSqlError::InvalidType) => true,
|
||||
(FromSqlError::OutOfRange(n1), FromSqlError::OutOfRange(n2)) => n1 == n2,
|
||||
#[cfg(feature = "i128_blob")]
|
||||
(FromSqlError::InvalidI128Size(s1), FromSqlError::InvalidI128Size(s2)) => s1 == s2,
|
||||
(_, _) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FromSqlError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
|
||||
@@ -104,3 +104,56 @@ impl<'a> From<&'a Value> for ValueRef<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "functions", feature = "session", feature = "vtab"))]
|
||||
impl<'a> ValueRef<'a> {
|
||||
pub(crate) unsafe fn from_value(value: *mut crate::ffi::sqlite3_value) -> ValueRef<'a> {
|
||||
use crate::ffi;
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
use std::slice::from_raw_parts;
|
||||
|
||||
match ffi::sqlite3_value_type(value) {
|
||||
ffi::SQLITE_NULL => ValueRef::Null,
|
||||
ffi::SQLITE_INTEGER => ValueRef::Integer(ffi::sqlite3_value_int64(value)),
|
||||
ffi::SQLITE_FLOAT => ValueRef::Real(ffi::sqlite3_value_double(value)),
|
||||
ffi::SQLITE_TEXT => {
|
||||
let text = ffi::sqlite3_value_text(value);
|
||||
assert!(
|
||||
!text.is_null(),
|
||||
"unexpected SQLITE_TEXT value type with NULL data"
|
||||
);
|
||||
let s = CStr::from_ptr(text as *const c_char);
|
||||
|
||||
// sqlite3_value_text returns UTF8 data, so our unwrap here should be fine.
|
||||
let s = s
|
||||
.to_str()
|
||||
.expect("sqlite3_value_text returned invalid UTF-8");
|
||||
ValueRef::Text(s)
|
||||
}
|
||||
ffi::SQLITE_BLOB => {
|
||||
let (blob, len) = (
|
||||
ffi::sqlite3_value_blob(value),
|
||||
ffi::sqlite3_value_bytes(value),
|
||||
);
|
||||
|
||||
assert!(
|
||||
len >= 0,
|
||||
"unexpected negative return from sqlite3_value_bytes"
|
||||
);
|
||||
if len > 0 {
|
||||
assert!(
|
||||
!blob.is_null(),
|
||||
"unexpected SQLITE_BLOB value type with NULL data"
|
||||
);
|
||||
ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize))
|
||||
} else {
|
||||
// The return value from sqlite3_value_blob() for a zero-length BLOB
|
||||
// is a NULL pointer.
|
||||
ValueRef::Blob(&[])
|
||||
}
|
||||
}
|
||||
_ => unreachable!("sqlite3_value_type returned invalid value"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user