Skip to main content

uqa_sql/plpgsql/
diagnostics.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Original procedural statement locations and `PostgreSQL` statement-kind names.
8
9use super::{PLpgSQLParseMode, PLpgSQLSource, PLpgSQLStmt};
10
11impl PLpgSQLSource {
12    /// SPI identifies the original embedded input unless a parser position already identifies it.
13    pub fn error_context(&self, error: crate::SQLError) -> crate::SQLError {
14        if error.position().is_some_and(|position| position > 0) {
15            return error;
16        }
17        let kind = match self.mode {
18            PLpgSQLParseMode::Expression => "PL/pgSQL expression",
19            PLpgSQLParseMode::Assignment1
20            | PLpgSQLParseMode::Assignment2
21            | PLpgSQLParseMode::Assignment3 => "PL/pgSQL assignment",
22            PLpgSQLParseMode::Statement => "SQL statement",
23        };
24        error.with_context(format!("{kind} \"{}\"", self.query))
25    }
26}
27
28impl PLpgSQLStmt {
29    pub fn unlocated(&self) -> &Self {
30        match self {
31            Self::Located { statement, .. } => statement.unlocated(),
32            _ => self,
33        }
34    }
35
36    pub fn diagnostic_kind(&self) -> &'static str {
37        match self.unlocated() {
38            Self::Located { .. } => unreachable!("statement locations were removed"),
39            Self::Block(_) => "statement block",
40            Self::Assign { .. } => "assignment",
41            Self::If { .. } => "IF",
42            Self::Case { .. } => "CASE",
43            Self::Loop { .. } => "LOOP",
44            Self::While { .. } => "WHILE",
45            Self::ForI { .. } => "FOR with integer loop variable",
46            Self::ForQuery { .. } => "FOR over SELECT rows",
47            Self::ForDynamic { .. } => "FOR over EXECUTE statement",
48            Self::ForCursor { .. } => "FOR over cursor",
49            Self::ForeachArray { .. } => "FOREACH over array",
50            Self::Exit { is_exit: true, .. } => "EXIT",
51            Self::Exit { is_exit: false, .. } => "CONTINUE",
52            Self::Return { .. } => "RETURN",
53            Self::ReturnNext { .. } => "RETURN NEXT",
54            Self::ReturnQuery { .. } | Self::ReturnQueryExecute { .. } => "RETURN QUERY",
55            Self::Raise { .. } => "RAISE",
56            Self::Assert { .. } => "ASSERT",
57            Self::ExecSQL { .. } => "SQL statement",
58            Self::DynExecute { .. } => "EXECUTE",
59            Self::Perform { .. } => "PERFORM",
60            Self::OpenCursor { .. } => "OPEN",
61            Self::FetchCursor { .. } => "FETCH",
62            Self::MoveCursor { .. } => "MOVE",
63            Self::CloseCursor { .. } => "CLOSE",
64            Self::Commit { .. } => "COMMIT",
65            Self::Rollback { .. } => "ROLLBACK",
66            Self::GetDiagnostics { .. } => "GET DIAGNOSTICS",
67        }
68    }
69}