use super::{RetainedConstantValue, RetainedScalarLiteral};
use crate::SQLError;
use uqa_core::{
memory::{Produced, ProductionControl},
Value,
};
pub(crate) enum Observation<'a> {
Resident(&'a Value),
Decoded(Produced<Value>),
}
impl std::ops::Deref for Observation<'_> {
type Target = Value;
fn deref(&self) -> &Value {
match self {
Self::Resident(value) => value,
Self::Decoded(value) => value,
}
}
}
pub(crate) fn observe<'a>(
value: &'a RetainedConstantValue,
control: &ProductionControl<'_>,
) -> Result<Option<Observation<'a>>, SQLError> {
crate::expr::check_statement_cancellation()?;
control.check()?;
if let Some(value) = value.resident_value() {
return Ok(Some(Observation::Resident(value)));
}
match value.read_with_control(control) {
Ok(value) => Ok(Some(Observation::Decoded(value))),
Err(error) if matches!(error.sqlstate(), Some("53200" | "57014")) => Err(error),
Err(_) => Ok(None),
}
}
impl RetainedScalarLiteral {
pub fn has_same_representation_with_control(
&self,
other: &Self,
control: &ProductionControl<'_>,
) -> Result<bool, SQLError> {
control.check()?;
let value_matches = if self.value.shares_value_with(&other.value) {
true
} else {
let left = observe(&self.value, control)?;
let right = observe(&other.value, control)?;
match (left, right) {
(Some(left), Some(right)) => left.has_same_representation(&right),
_ => false,
}
};
Ok(value_matches
&& self.ty == other.ty
&& self.declaration == other.declaration
&& self.collation_oid == other.collation_oid)
}
}