use uqa_core::{ArrayValue, Value};
use uqa_sql::{ResultRow, SQLError};
pub fn catalog_name() -> Value {
Value::Str("uqa".into())
}
pub fn catalog_usize(value: usize, label: &str) -> Result<i64, SQLError> {
i64::try_from(value).map_err(|_| {
SQLError::Internal(format!(
"{label} exceeds the SQL catalog BIGINT representation"
))
})
}
pub fn catalog_ordinal(index: usize, label: &str) -> Result<i64, SQLError> {
let ordinal = index
.checked_add(1)
.ok_or_else(|| SQLError::Internal(format!("{label} ordinal overflow")))?;
catalog_usize(ordinal, label)
}
pub fn str_value(value: impl Into<String>) -> Value {
Value::Str(value.into())
}
pub fn int_value(value: i64) -> Value {
Value::Int(value)
}
pub fn bool_value(value: bool) -> Value {
Value::Bool(value)
}
pub fn catalog_int2vector(values: Vec<Value>, label: &str) -> Result<Value, SQLError> {
catalog_vector(uqa_core::LegacyVectorKind::SmallInteger, values, label)
}
pub fn catalog_oidvector(values: Vec<Value>, label: &str) -> Result<Value, SQLError> {
catalog_vector(uqa_core::LegacyVectorKind::Oid, values, label)
}
fn catalog_vector(
kind: uqa_core::LegacyVectorKind,
values: Vec<Value>,
label: &str,
) -> Result<Value, SQLError> {
uqa_core::LegacyVectorValue::try_new(kind, values)
.map(Value::LegacyVector)
.ok_or_else(|| {
SQLError::Internal(format!("{label} has invalid {} elements", kind.type_name()))
})
}
pub fn catalog_array(values: Vec<Value>, label: &str) -> Result<Value, SQLError> {
ArrayValue::try_new(values)
.map(Value::Array)
.ok_or_else(|| SQLError::Internal(format!("{label} has non-rectangular dimensions")))
}
pub fn row(entries: impl IntoIterator<Item = (&'static str, Value)>) -> ResultRow {
let mut out = ResultRow::new();
for (key, value) in entries {
out.insert(key.to_string(), value);
}
out
}