Skip to main content

uqa_sql/catalog/sequence_functions/
value_error.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Stable diagnostics and SQLSTATEs for sequence value functions.
8use crate::SQLError;
9#[derive(Debug, thiserror::Error)]
10pub enum SequenceValueError {
11    #[error("relation \"{0}\" does not exist")]
12    Undefined(String),
13    #[error("cannot open relation \"{name}\": this operation is not supported for {kind}s")]
14    WrongKind { name: String, kind: &'static str },
15    #[error("currval of sequence \"{0}\" is not yet defined in this session")]
16    CurrvalUndefined(String),
17    #[error("lastval is not yet defined in this session")]
18    LastvalUndefined,
19    #[error("setval: value {value} is out of bounds for sequence \"{name}\" ({min}..{max})")]
20    SetvalOutOfBounds {
21        name: String,
22        value: i64,
23        min: i64,
24        max: i64,
25    },
26    #[error("nextval: reached {bound} value of sequence \"{name}\" ({value})")]
27    Exhausted {
28        name: String,
29        bound: &'static str,
30        value: i64,
31    },
32    #[error("cannot execute {0}() in a read-only transaction")]
33    ReadOnly(&'static str),
34    #[error(transparent)]
35    Security(#[from] SQLError),
36    #[error("{0}")]
37    Internal(String),
38}
39
40impl SequenceValueError {
41    pub fn into_sql_error(self) -> SQLError {
42        let sqlstate = match self {
43            Self::Undefined(_) => "42P01",
44            Self::WrongKind { .. } => "42809",
45            Self::CurrvalUndefined(_) | Self::LastvalUndefined => "55000",
46            Self::SetvalOutOfBounds { .. } => "22003",
47            Self::Exhausted { .. } => "2200H",
48            Self::ReadOnly(_) => "25006",
49            Self::Security(error) => return error,
50            Self::Internal(message) => return SQLError::Internal(message),
51        };
52        SQLError::Routine {
53            sqlstate: sqlstate.into(),
54            message: self.to_string(),
55        }
56    }
57}
58
59#[cfg(test)]
60mod tests;