use super::{EnumKeyOrder, Ordering, ProductionControl, Result, Value};
use crate::{
catalog::collations::{builtin_collation_order, BuiltinCollationOrder},
expr::SQLValueCatalog,
SQLError,
};
pub(super) const DEFAULT: u32 = crate::catalog::collations::DEFAULT_COLLATION_OID as u32;
pub fn compare_typed_values_with_catalog_and_collation(
left: &Value,
right: &Value,
collation: u32,
control: &ProductionControl<'_>,
catalog: Option<&dyn SQLValueCatalog>,
) -> Result<Ordering> {
super::compare_typed_values_for_purpose(
left,
right,
control,
catalog,
EnumKeyOrder::Declaration,
collation,
)
}
pub fn equal_typed_values_with_catalog_and_collation(
left: &Value,
right: &Value,
collation: u32,
control: &ProductionControl<'_>,
catalog: Option<&dyn SQLValueCatalog>,
) -> Result<bool> {
super::equal_values(left, right, control, catalog, true, collation)
}
pub(super) fn compare_text(
left: &str,
right: &str,
collation: u32,
purpose: EnumKeyOrder,
control: &ProductionControl<'_>,
) -> Result<Ordering> {
control.check()?;
if collation == 0 {
return Err(SQLError::Diagnostic {
sqlstate: "42P22".into(),
message: "could not determine which collation to use for string comparison".into(),
detail: None,
hint: Some("Use the COLLATE clause to set the collation explicitly.".into()),
});
}
let selected = builtin_collation_order(collation).ok_or_else(|| {
SQLError::Internal(format!("cache lookup failed for collation {collation}"))
})?;
match (selected, purpose) {
(BuiltinCollationOrder::Unicode, EnumKeyOrder::Declaration) => {
crate::expr::compare_unicode_with_control(left, right, control)
}
_ => uqa_core::compare_bytes_with_control(left.as_bytes(), right.as_bytes(), control)
.map_err(Into::into),
}
}