1#[derive(Debug, thiserror::Error)]
10pub enum SQLError {
11 #[error("parse error: {0}")]
12 Parse(String),
13 #[error("unsupported SQL feature: {0}")]
14 Unsupported(String),
15 #[error("relation \"{0}\" does not exist")]
16 UnknownTable(String),
17 #[error("unknown column: {0}")]
18 UnknownColumn(String),
19 #[error("column reference \"{0}\" is ambiguous")]
20 AmbiguousColumn(String),
21 #[error("unknown function: {0}")]
22 UnknownFunction(String),
23 #[error("type mismatch: {0}")]
24 TypeMismatch(String),
25 #[error("invalid argument count for `{name}`: expected {expected}, got {actual}")]
26 BadArity {
27 name: String,
28 expected: String,
29 actual: usize,
30 },
31 #[error("No value supplied for parameter ${0}")]
32 MissingParam(usize),
33 #[error("vector dimension mismatch: expected {expected}, got {actual}")]
34 VectorDimMismatch { expected: usize, actual: usize },
35 #[error("{0}")]
36 Cancelled(#[from] uqa_core::QueryCancelled),
37 #[error("{message}")]
42 Routine { sqlstate: String, message: String },
43 #[error("internal error: {0}")]
44 Internal(String),
45}
46
47impl SQLError {
48 pub fn sqlstate(&self) -> Option<&str> {
52 match self {
53 SQLError::Cancelled(_) => Some(uqa_core::SQLSTATE_QUERY_CANCELED),
54 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),
65 SQLError::Internal(_) => Some("XX000"), }
67 }
68}
69
70pub type Result<T> = std::result::Result<T, SQLError>;
71
72impl From<pg_query::Error> for SQLError {
73 fn from(value: pg_query::Error) -> Self {
74 match value {
75 pg_query::Error::Parse(message)
76 if message == "WITH TIES cannot be specified without ORDER BY clause" =>
77 {
78 SQLError::Routine {
79 sqlstate: "42601".into(),
80 message,
81 }
82 }
83 pg_query::Error::Parse(message)
84 if message.contains("constraints cannot be altered to be NOT VALID") =>
85 {
86 SQLError::Routine {
87 sqlstate: "0A000".into(),
88 message,
89 }
90 }
91 other => SQLError::Parse(other.to_string()),
92 }
93 }
94}