use super::enums::{EnumTypeComparisonStates, EnumTypeLabels};
use super::Result;
use std::sync::Arc;
use uqa_core::{
memory::{Produced, ProductionControl},
EnumValue, Value,
};
mod records;
pub use records::SQLRecordFields;
mod record_view;
pub use record_view::SQLRecordView;
mod types;
pub(crate) use types::field_type_with_control;
pub(crate) use types::type_by_oid_with_control;
pub(crate) struct ValueCompositeCatalog<'a>(pub(crate) Option<&'a dyn SQLValueCatalog>);
impl super::composites::CompositeTypeCatalog for ValueCompositeCatalog<'_> {
fn composite_type_with_control(
&self,
oid: u32,
control: &ProductionControl<'_>,
) -> Result<Option<super::composites::OwnedCompositeTypeDescriptor>> {
control.check()?;
self.0
.ok_or_else(|| {
crate::SQLError::Internal("composite operator lookup has no catalog".into())
})?
.value_composite_type_with_control(oid, control)
}
fn composite_type(
&self,
oid: u32,
) -> Result<Option<Arc<super::composites::CompositeTypeDescriptor>>> {
self.0
.ok_or_else(|| {
crate::SQLError::Internal("composite operator lookup has no catalog".into())
})?
.value_composite_type(oid)
}
}
pub trait SQLValueCatalog {
fn value_range_type(
&self,
_oid: u32,
) -> Result<Option<Arc<super::range::RangeValueDescriptor>>> {
Ok(None)
}
fn compare_range_bounds(
&self,
_range_oid: u32,
_left: &Value,
_right: &Value,
_control: &ProductionControl<'_>,
) -> Result<std::cmp::Ordering> {
Err(super::SQLError::Internal(
"selected range comparison support is unavailable".into(),
))
}
fn range_bound_equality_key(
&self,
_range_oid: u32,
_value: &Value,
_control: &ProductionControl<'_>,
) -> Result<Produced<Vec<u8>>> {
Err(super::SQLError::Internal(
"selected range equality-key support is unavailable".into(),
))
}
fn value_type_by_oid(&self, _oid: u32) -> Result<Option<crate::ColumnType>> {
Ok(None)
}
fn value_type_by_oid_with_control(
&self,
oid: u32,
control: &ProductionControl<'_>,
) -> Result<Option<Produced<crate::ColumnType>>> {
control.check()?;
self.value_type_by_oid(oid)?
.map(|ty| ty.retain_external_with_control(control).map_err(Into::into))
.transpose()
}
fn value_type_exists(&self, oid: u32) -> Result<bool> {
Ok(self.value_type_by_oid(oid)?.is_some())
}
fn value_regtype_output(&self, _ty: &crate::ColumnType, _oid: i64) -> Result<Option<String>> {
Ok(None)
}
fn value_composite_type(
&self,
_oid: u32,
) -> Result<Option<Arc<super::composites::CompositeTypeDescriptor>>> {
Ok(None)
}
fn value_composite_type_with_control(
&self,
oid: u32,
control: &ProductionControl<'_>,
) -> Result<Option<super::composites::OwnedCompositeTypeDescriptor>> {
control.check()?;
self.value_composite_type(oid)?
.map(|descriptor| {
super::composites::OwnedCompositeTypeDescriptor::from_produced(
descriptor.clone_with_control(control)?,
control,
)
})
.transpose()
}
fn enum_type_comparison_states(&self) -> Option<&EnumTypeComparisonStates> {
None
}
fn enum_type_labels(&self, type_oid: u32) -> Result<Option<Arc<EnumTypeLabels>>>;
fn enum_value_by_oid(&self, _label_oid: u32) -> Result<Option<EnumValue>> {
Ok(None)
}
fn enum_label_position(&self, _label_oid: u32) -> Result<Option<(u32, usize)>> {
Ok(None)
}
fn enum_label_uncommitted(&self, label_oid: u32) -> bool;
fn enum_type_name(&self, type_oid: u32) -> Result<Option<String>>;
fn has_enum_types(&self) -> bool;
}
pub struct EngineValueCatalog<'a>(pub &'a dyn super::EngineHook);
impl SQLValueCatalog for EngineValueCatalog<'_> {
fn compare_range_bounds(
&self,
range_oid: u32,
left: &Value,
right: &Value,
control: &ProductionControl<'_>,
) -> Result<std::cmp::Ordering> {
self.0
.enum_labels()
.ok_or_else(|| {
super::SQLError::Internal("selected range comparison support is unavailable".into())
})?
.compare_range_bounds(range_oid, left, right, control)
}
fn range_bound_equality_key(
&self,
range_oid: u32,
value: &Value,
control: &ProductionControl<'_>,
) -> Result<Produced<Vec<u8>>> {
self.0
.enum_labels()
.ok_or_else(|| {
super::SQLError::Internal(
"selected range equality-key support is unavailable".into(),
)
})?
.range_bound_equality_key(range_oid, value, control)
}
fn value_range_type(
&self,
oid: u32,
) -> Result<Option<Arc<super::range::RangeValueDescriptor>>> {
self.0
.enum_labels()
.map(|catalog| catalog.value_range_type(oid))
.transpose()
.map(Option::flatten)
}
fn value_regtype_output(&self, ty: &crate::ColumnType, oid: i64) -> Result<Option<String>> {
self.0
.resolve_regtype_output(ty, oid)
.map_err(crate::SQLError::Internal)
}
fn value_type_by_oid(&self, oid: u32) -> Result<Option<crate::ColumnType>> {
self.0
.resolve_type_oid(oid)
.map_err(crate::SQLError::Internal)
}
fn value_type_by_oid_with_control(
&self,
oid: u32,
control: &ProductionControl<'_>,
) -> Result<Option<Produced<crate::ColumnType>>> {
self.0.resolve_type_oid_with_control(oid, control)
}
fn value_type_exists(&self, oid: u32) -> Result<bool> {
self.0
.type_oid_exists(oid)
.map_err(crate::SQLError::Internal)
}
fn value_composite_type(
&self,
oid: u32,
) -> Result<Option<Arc<super::composites::CompositeTypeDescriptor>>> {
self.0
.composite_types()
.map(|catalog| catalog.composite_type(oid))
.transpose()
.map(Option::flatten)
}
fn value_composite_type_with_control(
&self,
oid: u32,
control: &ProductionControl<'_>,
) -> Result<Option<super::composites::OwnedCompositeTypeDescriptor>> {
control.check()?;
self.0
.composite_types()
.map(|catalog| catalog.composite_type_with_control(oid, control))
.transpose()
.map(Option::flatten)
}
fn enum_type_comparison_states(&self) -> Option<&EnumTypeComparisonStates> {
self.0.enum_labels()?.enum_type_comparison_states()
}
fn enum_type_labels(&self, oid: u32) -> Result<Option<Arc<EnumTypeLabels>>> {
self.0
.enum_labels()
.map(|catalog| catalog.enum_type_labels(oid))
.transpose()
.map(Option::flatten)
}
fn enum_value_by_oid(&self, oid: u32) -> Result<Option<EnumValue>> {
self.0
.enum_labels()
.map(|catalog| catalog.enum_value_by_oid(oid))
.transpose()
.map(Option::flatten)
}
fn enum_label_position(&self, oid: u32) -> Result<Option<(u32, usize)>> {
self.0
.enum_labels()
.map(|catalog| catalog.enum_label_position(oid))
.transpose()
.map(Option::flatten)
}
fn enum_label_uncommitted(&self, oid: u32) -> bool {
self.0
.enum_labels()
.is_some_and(|catalog| catalog.enum_label_uncommitted(oid))
}
fn enum_type_name(&self, oid: u32) -> Result<Option<String>> {
self.0
.enum_labels()
.map(|catalog| catalog.enum_type_name(oid))
.transpose()
.map(Option::flatten)
}
fn has_enum_types(&self) -> bool {
self.0
.enum_labels()
.is_some_and(SQLValueCatalog::has_enum_types)
}
}