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