uqa_sql/expr/
value_catalog.rs1use super::enums::{EnumTypeComparisonStates, EnumTypeLabels};
10use super::Result;
11use std::sync::Arc;
12use uqa_core::{
13 memory::{Produced, ProductionControl},
14 EnumValue, Value,
15};
16
17mod records;
18pub use records::SQLRecordFields;
19mod record_view;
20pub use record_view::SQLRecordView;
21mod types;
22pub(crate) use types::field_type_with_control;
23pub(crate) use types::type_by_oid_with_control;
24
25pub(crate) struct ValueCompositeCatalog<'a>(pub(crate) Option<&'a dyn SQLValueCatalog>);
26
27impl super::composites::CompositeTypeCatalog for ValueCompositeCatalog<'_> {
28 fn composite_type(
29 &self,
30 oid: u32,
31 ) -> Result<Option<Arc<super::composites::CompositeTypeDescriptor>>> {
32 self.0
33 .ok_or_else(|| {
34 crate::SQLError::Internal("composite operator lookup has no catalog".into())
35 })?
36 .value_composite_type(oid)
37 }
38}
39
40pub trait SQLValueCatalog {
42 fn value_range_type(
44 &self,
45 _oid: u32,
46 ) -> Result<Option<Arc<super::range::RangeValueDescriptor>>> {
47 Ok(None)
48 }
49
50 fn compare_range_bounds(
52 &self,
53 _range_oid: u32,
54 _left: &Value,
55 _right: &Value,
56 _control: &ProductionControl<'_>,
57 ) -> Result<std::cmp::Ordering> {
58 Err(super::SQLError::Internal(
59 "selected range comparison support is unavailable".into(),
60 ))
61 }
62
63 fn range_bound_equality_key(
65 &self,
66 _range_oid: u32,
67 _value: &Value,
68 _control: &ProductionControl<'_>,
69 ) -> Result<Produced<Vec<u8>>> {
70 Err(super::SQLError::Internal(
71 "selected range equality-key support is unavailable".into(),
72 ))
73 }
74
75 fn value_type_by_oid(&self, _oid: u32) -> Result<Option<crate::ColumnType>> {
77 Ok(None)
78 }
79
80 fn value_type_exists(&self, oid: u32) -> Result<bool> {
82 Ok(self.value_type_by_oid(oid)?.is_some())
83 }
84
85 fn value_regtype_output(&self, _ty: &crate::ColumnType, _oid: i64) -> Result<Option<String>> {
87 Ok(None)
88 }
89
90 fn value_composite_type(
92 &self,
93 _oid: u32,
94 ) -> Result<Option<Arc<super::composites::CompositeTypeDescriptor>>> {
95 Ok(None)
96 }
97
98 fn enum_type_comparison_states(&self) -> Option<&EnumTypeComparisonStates> {
100 None
101 }
102
103 fn enum_type_labels(&self, type_oid: u32) -> Result<Option<Arc<EnumTypeLabels>>>;
105
106 fn enum_value_by_oid(&self, _label_oid: u32) -> Result<Option<EnumValue>> {
108 Ok(None)
109 }
110
111 fn enum_label_position(&self, _label_oid: u32) -> Result<Option<(u32, usize)>> {
113 Ok(None)
114 }
115
116 fn enum_label_uncommitted(&self, label_oid: u32) -> bool;
118
119 fn enum_type_name(&self, type_oid: u32) -> Result<Option<String>>;
121
122 fn has_enum_types(&self) -> bool;
124}
125
126pub struct EngineValueCatalog<'a>(pub &'a dyn super::EngineHook);
128
129impl SQLValueCatalog for EngineValueCatalog<'_> {
130 fn compare_range_bounds(
131 &self,
132 range_oid: u32,
133 left: &Value,
134 right: &Value,
135 control: &ProductionControl<'_>,
136 ) -> Result<std::cmp::Ordering> {
137 self.0
138 .enum_labels()
139 .ok_or_else(|| {
140 super::SQLError::Internal("selected range comparison support is unavailable".into())
141 })?
142 .compare_range_bounds(range_oid, left, right, control)
143 }
144
145 fn range_bound_equality_key(
146 &self,
147 range_oid: u32,
148 value: &Value,
149 control: &ProductionControl<'_>,
150 ) -> Result<Produced<Vec<u8>>> {
151 self.0
152 .enum_labels()
153 .ok_or_else(|| {
154 super::SQLError::Internal(
155 "selected range equality-key support is unavailable".into(),
156 )
157 })?
158 .range_bound_equality_key(range_oid, value, control)
159 }
160
161 fn value_range_type(
162 &self,
163 oid: u32,
164 ) -> Result<Option<Arc<super::range::RangeValueDescriptor>>> {
165 self.0
166 .enum_labels()
167 .map(|catalog| catalog.value_range_type(oid))
168 .transpose()
169 .map(Option::flatten)
170 }
171
172 fn value_regtype_output(&self, ty: &crate::ColumnType, oid: i64) -> Result<Option<String>> {
173 self.0
174 .resolve_regtype_output(ty, oid)
175 .map_err(crate::SQLError::Internal)
176 }
177
178 fn value_type_by_oid(&self, oid: u32) -> Result<Option<crate::ColumnType>> {
179 self.0
180 .resolve_type_oid(oid)
181 .map_err(crate::SQLError::Internal)
182 }
183
184 fn value_type_exists(&self, oid: u32) -> Result<bool> {
185 self.0
186 .type_oid_exists(oid)
187 .map_err(crate::SQLError::Internal)
188 }
189
190 fn value_composite_type(
191 &self,
192 oid: u32,
193 ) -> Result<Option<Arc<super::composites::CompositeTypeDescriptor>>> {
194 self.0
195 .composite_types()
196 .map(|catalog| catalog.composite_type(oid))
197 .transpose()
198 .map(Option::flatten)
199 }
200
201 fn enum_type_comparison_states(&self) -> Option<&EnumTypeComparisonStates> {
202 self.0.enum_labels()?.enum_type_comparison_states()
203 }
204
205 fn enum_type_labels(&self, oid: u32) -> Result<Option<Arc<EnumTypeLabels>>> {
206 self.0
207 .enum_labels()
208 .map(|catalog| catalog.enum_type_labels(oid))
209 .transpose()
210 .map(Option::flatten)
211 }
212
213 fn enum_value_by_oid(&self, oid: u32) -> Result<Option<EnumValue>> {
214 self.0
215 .enum_labels()
216 .map(|catalog| catalog.enum_value_by_oid(oid))
217 .transpose()
218 .map(Option::flatten)
219 }
220
221 fn enum_label_position(&self, oid: u32) -> Result<Option<(u32, usize)>> {
222 self.0
223 .enum_labels()
224 .map(|catalog| catalog.enum_label_position(oid))
225 .transpose()
226 .map(Option::flatten)
227 }
228
229 fn enum_label_uncommitted(&self, oid: u32) -> bool {
230 self.0
231 .enum_labels()
232 .is_some_and(|catalog| catalog.enum_label_uncommitted(oid))
233 }
234
235 fn enum_type_name(&self, oid: u32) -> Result<Option<String>> {
236 self.0
237 .enum_labels()
238 .map(|catalog| catalog.enum_type_name(oid))
239 .transpose()
240 .map(Option::flatten)
241 }
242
243 fn has_enum_types(&self) -> bool {
244 self.0
245 .enum_labels()
246 .is_some_and(SQLValueCatalog::has_enum_types)
247 }
248}