#[derive(Debug, thiserror::Error)]
pub enum SQLError {
#[error("parse error: {0}")]
Parse(String),
#[error("unsupported SQL feature: {0}")]
Unsupported(String),
#[error("unknown table: {0}")]
UnknownTable(String),
#[error("unknown column: {0}")]
UnknownColumn(String),
#[error("column reference \"{0}\" is ambiguous")]
AmbiguousColumn(String),
#[error("unknown function: {0}")]
UnknownFunction(String),
#[error("type mismatch: {0}")]
TypeMismatch(String),
#[error("invalid argument count for `{name}`: expected {expected}, got {actual}")]
BadArity {
name: String,
expected: String,
actual: usize,
},
#[error("No value supplied for parameter ${0}")]
MissingParam(usize),
#[error("vector dimension mismatch: expected {expected}, got {actual}")]
VectorDimMismatch { expected: usize, actual: usize },
#[error("{0}")]
Cancelled(#[from] uqa_core::QueryCancelled),
#[error("{message}")]
Routine { sqlstate: String, message: String },
#[error("internal error: {0}")]
Internal(String),
}
impl SQLError {
pub fn sqlstate(&self) -> Option<&str> {
match self {
SQLError::Cancelled(_) => Some(uqa_core::SQLSTATE_QUERY_CANCELED),
SQLError::Parse(_) => Some("42601"), SQLError::Unsupported(_) => Some("0A000"), SQLError::UnknownTable(_) => Some("42P01"), SQLError::UnknownColumn(_) => Some("42703"), SQLError::AmbiguousColumn(_) => Some("42702"), SQLError::UnknownFunction(_) => Some("42883"), SQLError::TypeMismatch(_) => Some("42804"), SQLError::BadArity { .. } => Some("42883"), SQLError::MissingParam(_) => Some("S1002"), SQLError::VectorDimMismatch { .. } => Some("22023"), SQLError::Routine { sqlstate, .. } => Some(sqlstate),
SQLError::Internal(_) => Some("XX000"), }
}
}
pub type Result<T> = std::result::Result<T, SQLError>;
impl From<pg_query::Error> for SQLError {
fn from(value: pg_query::Error) -> Self {
SQLError::Parse(value.to_string())
}
}