1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
use crate::rqlite;
use std::borrow::Cow;
//use std::ptr::NonNull;
//use std::slice::from_raw_parts;
//use std::str::from_utf8;
use std::sync::Arc;
//use rqlite::types::FromJsonValue;
use crate::Rqlite;
use rqlite::types::FromJsonValueRef;
use sqlx_core::type_info::TypeInfo;
/*
use libsqlite3_sys::{
sqlite3_value, sqlite3_value_blob, sqlite3_value_bytes, sqlite3_value_double,
sqlite3_value_dup, sqlite3_value_free, sqlite3_value_int, sqlite3_value_int64,
sqlite3_value_type, SQLITE_NULL,
};
*/
pub(crate) use sqlx_core::value::{Value, ValueRef};
use crate::error::BoxDynError;
//use crate::type_info::DataType;
use crate::{/*Sqlite,*/ RqliteTypeInfo};
enum RqliteValueData<'r> {
Value(&'r RqliteValue),
}
pub struct RqliteValueRef<'r>(RqliteValueData<'r>);
impl<'r> RqliteValueRef<'r> {
pub(crate) fn value(value: &'r RqliteValue) -> Self {
Self(RqliteValueData::Value(value))
}
pub(super) fn int(&self) -> Result<i32, BoxDynError> {
match self.0 {
RqliteValueData::Value(v) => i32::from_json_value_ref(&v.handle),
}
}
pub(super) fn int64(&self) -> Result<i64, BoxDynError> {
match self.0 {
RqliteValueData::Value(v) => i64::from_json_value_ref(&v.handle),
}
}
pub(super) fn double(&self) -> Result<f64, BoxDynError> {
match self.0 {
RqliteValueData::Value(v) => f64::from_json_value_ref(&v.handle),
}
}
/*
pub(super) fn blob(&self) -> Result<&'r [u8],BoxDynError> {
Err(std::io::Error::new(std::io::ErrorKind::Other , "blob not supported yet").into())
}
*/
pub(super) fn text(&self) -> Result<String, BoxDynError> {
match self.0 {
RqliteValueData::Value(v) => String::from_json_value_ref(&v.handle),
}
}
}
impl<'r> ValueRef<'r> for RqliteValueRef<'r> {
type Database = Rqlite;
fn to_owned(&self) -> RqliteValue {
match self.0 {
RqliteValueData::Value(v) => v.clone(),
}
}
fn type_info(&self) -> Cow<'_, RqliteTypeInfo> {
match self.0 {
RqliteValueData::Value(v) => v.type_info(),
}
}
fn is_null(&self) -> bool {
match self.0 {
RqliteValueData::Value(v) => v.is_null(),
}
}
}
#[derive(Clone)]
pub struct RqliteValue {
pub(crate) handle: Arc<rqlite::Value>,
pub(crate) type_info: RqliteTypeInfo,
}
//pub(crate) struct ValueHandle(NonNull<sqlite3_value>);
// SAFE: only protected value objects are stored in RqliteValue
/*
unsafe impl Send for ValueHandle {}
unsafe impl Sync for ValueHandle {}
*/
impl RqliteValue {
/*
pub(crate) unsafe fn new(value: *mut sqlite3_value, type_info: RqliteTypeInfo) -> Self {
debug_assert!(!value.is_null());
Self {
type_info,
handle: Arc::new(ValueHandle(NonNull::new_unchecked(sqlite3_value_dup(
value,
)))),
}
}
*/
pub(crate) fn new(value: rqlite::Value, type_info: RqliteTypeInfo) -> Self {
Self {
type_info,
handle: Arc::new(value),
}
}
fn type_info_opt(&self) -> Option<RqliteTypeInfo> {
/*
let dt = DataType::from_code(unsafe { sqlite3_value_type(self.handle.0.as_ptr()) });
if let DataType::Null = dt {
None
} else {
Some(RqliteTypeInfo(dt))
}
*/
Some(self.type_info.clone())
}
/*
fn int(&self) -> i32 {
//unsafe { sqlite3_value_int(self.handle.0.as_ptr()) }
}
fn int64(&self) -> i64 {
//unsafe { sqlite3_value_int64(self.handle.0.as_ptr()) }
}
fn double(&self) -> f64 {
unsafe { sqlite3_value_double(self.handle.0.as_ptr()) }
}
fn blob(&self) -> &[u8] {
let len = unsafe { sqlite3_value_bytes(self.handle.0.as_ptr()) } as usize;
if len == 0 {
// empty blobs are NULL so just return an empty slice
return &[];
}
let ptr = unsafe { sqlite3_value_blob(self.handle.0.as_ptr()) } as *const u8;
debug_assert!(!ptr.is_null());
unsafe { from_raw_parts(ptr, len) }
}
fn text(&self) -> Result<&str, BoxDynError> {
Ok(from_utf8(self.blob())?)
}
*/
}
impl Value for RqliteValue {
type Database = Rqlite;
fn as_ref(&self) -> RqliteValueRef<'_> {
RqliteValueRef::value(self)
}
fn type_info(&self) -> Cow<'_, RqliteTypeInfo> {
self.type_info_opt()
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(&self.type_info))
}
fn is_null(&self) -> bool {
self.type_info.is_null()
}
}
/*
impl Drop for ValueHandle {
fn drop(&mut self) {
/*
unsafe {
sqlite3_value_free(self.0.as_ptr());
}
*/
}
}
*/
// #[cfg(feature = "any")]
// impl<'r> From<RqliteValueRef<'r>> for crate::any::AnyValueRef<'r> {
// #[inline]
// fn from(value: RqliteValueRef<'r>) -> Self {
// crate::any::AnyValueRef {
// type_info: value.type_info().clone().into_owned().into(),
// kind: crate::any::value::AnyValueRefKind::Rqlite(value),
// }
// }
// }
//
// #[cfg(feature = "any")]
// impl From<RqliteValue> for crate::any::AnyValue {
// #[inline]
// fn from(value: RqliteValue) -> Self {
// crate::any::AnyValue {
// type_info: value.type_info().clone().into_owned().into(),
// kind: crate::any::value::AnyValueKind::Rqlite(value),
// }
// }
// }