1use std::borrow::Cow;
4
5use sqlx_core::error::BoxDynError;
6use sqlx_core::value::{Value, ValueRef};
7
8use spg_embedded::Value as EngineValue;
9
10use crate::database::Spg;
11use crate::type_info::{Kind, SpgTypeInfo};
12
13#[derive(Debug, Clone)]
18pub struct SpgValue {
19 inner: EngineValue,
20 type_info: SpgTypeInfo,
21}
22
23impl SpgValue {
24 #[must_use]
26 pub fn new(inner: EngineValue, type_info: SpgTypeInfo) -> Self {
27 Self { inner, type_info }
28 }
29
30 #[must_use]
32 pub const fn engine(&self) -> &EngineValue {
33 &self.inner
34 }
35}
36
37impl Value for SpgValue {
38 type Database = Spg;
39
40 fn as_ref(&self) -> SpgValueRef<'_> {
41 SpgValueRef::new(&self.inner, self.type_info.clone())
42 }
43
44 fn type_info(&self) -> Cow<'_, SpgTypeInfo> {
45 Cow::Borrowed(&self.type_info)
46 }
47
48 fn is_null(&self) -> bool {
49 matches!(self.inner, EngineValue::Null)
50 }
51}
52
53#[derive(Debug, Clone)]
57pub struct SpgValueRef<'r> {
58 inner: &'r EngineValue,
59 type_info: SpgTypeInfo,
60}
61
62impl<'r> SpgValueRef<'r> {
63 #[must_use]
65 pub fn new(inner: &'r EngineValue, type_info: SpgTypeInfo) -> Self {
66 Self { inner, type_info }
67 }
68
69 #[must_use]
71 pub const fn engine(&self) -> &'r EngineValue {
72 self.inner
73 }
74
75 #[must_use]
77 pub const fn kind(&self) -> Kind {
78 self.type_info.kind()
79 }
80}
81
82impl<'r> ValueRef<'r> for SpgValueRef<'r> {
83 type Database = Spg;
84
85 fn to_owned(&self) -> SpgValue {
86 SpgValue {
87 inner: self.inner.clone(),
88 type_info: self.type_info.clone(),
89 }
90 }
91
92 fn type_info(&self) -> Cow<'_, SpgTypeInfo> {
93 Cow::Borrowed(&self.type_info)
94 }
95
96 fn is_null(&self) -> bool {
97 matches!(self.inner, EngineValue::Null)
98 }
99}
100
101#[must_use]
105#[allow(dead_code)]
106pub fn engine_value_kind(v: &EngineValue) -> Kind {
107 match v {
108 EngineValue::Null => Kind::Null,
109 EngineValue::SmallInt(_) => Kind::SmallInt,
110 EngineValue::Int(_) => Kind::Int,
111 EngineValue::BigInt(_) => Kind::BigInt,
112 EngineValue::Bool(_) => Kind::Bool,
113 EngineValue::Text(_) => Kind::Text,
114 EngineValue::Bytes(_) => Kind::Bytes,
115 EngineValue::Float(_) => Kind::Float,
116 EngineValue::Date(_) => Kind::Date,
117 EngineValue::Timestamp(_) => Kind::Timestamp,
118 EngineValue::Json(_) => Kind::Json,
119 EngineValue::Uuid(_) => Kind::Uuid,
122 EngineValue::Numeric { .. } => Kind::Numeric,
125 EngineValue::Vector(_) | EngineValue::Sq8Vector(_) | EngineValue::HalfVector(_) => {
130 Kind::Vector
131 }
132 EngineValue::TsVector(_) => Kind::TsVector,
133 _ => Kind::Null,
137 }
138}
139
140#[allow(dead_code)]
144fn _box_dyn_marker() -> Option<BoxDynError> {
145 None
146}