drizzle_sqlite/values/owned/
mod.rs1mod conversions;
4mod drivers;
5
6use super::SQLiteValue;
7use crate::prelude::*;
8use crate::traits::FromSQLiteValue;
9use drizzle_core::{error::DrizzleError, sql::SQL, traits::SQLParam};
10
11#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
13pub enum OwnedSQLiteValue {
14 Integer(i64),
16 Real(f64),
18 Text(String),
20 Blob(Box<[u8]>),
22 #[default]
24 Null,
25}
26
27impl OwnedSQLiteValue {
28 #[inline]
30 #[must_use]
31 pub const fn is_null(&self) -> bool {
32 matches!(self, Self::Null)
33 }
34
35 #[inline]
37 #[must_use]
38 pub const fn as_i64(&self) -> Option<i64> {
39 match self {
40 Self::Integer(value) => Some(*value),
41 _ => None,
42 }
43 }
44
45 #[inline]
47 #[must_use]
48 pub const fn as_f64(&self) -> Option<f64> {
49 match self {
50 Self::Real(value) => Some(*value),
51 _ => None,
52 }
53 }
54
55 #[inline]
57 #[must_use]
58 pub const fn as_str(&self) -> Option<&str> {
59 match self {
60 Self::Text(value) => Some(value.as_str()),
61 _ => None,
62 }
63 }
64
65 #[inline]
67 #[must_use]
68 pub fn as_bytes(&self) -> Option<&[u8]> {
69 match self {
70 Self::Blob(value) => Some(value.as_ref()),
71 _ => None,
72 }
73 }
74
75 #[inline]
77 #[must_use]
78 pub fn as_value(&self) -> SQLiteValue<'_> {
79 match self {
80 Self::Integer(value) => SQLiteValue::Integer(*value),
81 Self::Real(value) => SQLiteValue::Real(*value),
82 Self::Text(value) => SQLiteValue::Text(Cow::Borrowed(value)),
83 Self::Blob(value) => SQLiteValue::Blob(Cow::Borrowed(value)),
84 Self::Null => SQLiteValue::Null,
85 }
86 }
87
88 pub fn convert<T: FromSQLiteValue>(self) -> Result<T, DrizzleError> {
95 match self {
96 Self::Integer(i) => T::from_sqlite_integer(i),
97 Self::Text(s) => T::from_sqlite_text(&s),
98 Self::Real(r) => T::from_sqlite_real(r),
99 Self::Blob(b) => T::from_sqlite_blob(&b),
100 Self::Null => T::from_sqlite_null(),
101 }
102 }
103
104 pub fn convert_ref<T: FromSQLiteValue>(&self) -> Result<T, DrizzleError> {
111 match self {
112 Self::Integer(i) => T::from_sqlite_integer(*i),
113 Self::Text(s) => T::from_sqlite_text(s),
114 Self::Real(r) => T::from_sqlite_real(*r),
115 Self::Blob(b) => T::from_sqlite_blob(b),
116 Self::Null => T::from_sqlite_null(),
117 }
118 }
119}
120
121impl core::fmt::Display for OwnedSQLiteValue {
122 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
123 let value = match self {
124 Self::Integer(i) => i.to_string(),
125 Self::Real(r) => r.to_string(),
126 Self::Text(s) => s.clone(),
127 Self::Blob(b) => String::from_utf8_lossy(b).to_string(),
128 Self::Null => String::new(),
129 };
130 write!(f, "{value}")
131 }
132}
133
134impl<'a> From<SQLiteValue<'a>> for OwnedSQLiteValue {
139 fn from(value: SQLiteValue<'a>) -> Self {
140 match value {
141 SQLiteValue::Integer(i) => Self::Integer(i),
142 SQLiteValue::Real(r) => Self::Real(r),
143 SQLiteValue::Text(cow) => Self::Text(cow.into_owned()),
144 SQLiteValue::Blob(cow) => Self::Blob(cow.into_owned().into_boxed_slice()),
145 SQLiteValue::Null => Self::Null,
146 }
147 }
148}
149
150impl<'a> From<&SQLiteValue<'a>> for OwnedSQLiteValue {
151 fn from(value: &SQLiteValue<'a>) -> Self {
152 match value {
153 SQLiteValue::Integer(i) => Self::Integer(*i),
154 SQLiteValue::Real(r) => Self::Real(*r),
155 SQLiteValue::Text(cow) => Self::Text(cow.clone().into_owned()),
156 SQLiteValue::Blob(cow) => Self::Blob(cow.clone().into_owned().into_boxed_slice()),
157 SQLiteValue::Null => Self::Null,
158 }
159 }
160}
161
162impl SQLParam for OwnedSQLiteValue {
167 const DIALECT: drizzle_core::Dialect = drizzle_core::Dialect::SQLite;
168 type DialectMarker = drizzle_core::dialect::SQLiteDialect;
169}
170
171impl From<OwnedSQLiteValue> for SQL<'_, OwnedSQLiteValue> {
172 fn from(value: OwnedSQLiteValue) -> Self {
173 SQL::param(value)
174 }
175}
176
177impl From<OwnedSQLiteValue> for Cow<'_, OwnedSQLiteValue> {
182 fn from(value: OwnedSQLiteValue) -> Self {
183 Cow::Owned(value)
184 }
185}
186
187impl<'a> From<&'a OwnedSQLiteValue> for Cow<'a, OwnedSQLiteValue> {
188 fn from(value: &'a OwnedSQLiteValue) -> Self {
189 Cow::Borrowed(value)
190 }
191}