Skip to main content

uqa_sql/
notice.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Notices that a statement reports without failing.
8
9pub mod level;
10
11pub use level::NoticeLevel;
12
13/// A notice that a statement reports without failing, as `PostgreSQL`'s `ereport` sends one below `ERROR`: its level, SQLSTATE and primary message, and the detail and hint that clients receive as fields of their own.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct SQLNotice {
16    pub level: NoticeLevel,
17    pub sqlstate: String,
18    pub message: String,
19    pub detail: Option<String>,
20    pub hint: Option<String>,
21}
22
23impl SQLNotice {
24    /// A notice of `level` that carries the SQLSTATE `PostgreSQL` assigns a report that names none (see [`NoticeLevel::default_sqlstate`]).
25    pub fn new(level: NoticeLevel, message: impl Into<String>) -> Self {
26        Self {
27            level,
28            sqlstate: level.default_sqlstate().to_string(),
29            message: message.into(),
30            detail: None,
31            hint: None,
32        }
33    }
34
35    /// A `NOTICE` with SQLSTATE `00000`.
36    pub fn notice(message: impl Into<String>) -> Self {
37        Self::new(NoticeLevel::Notice, message)
38    }
39
40    /// A `WARNING` with SQLSTATE `01000`.
41    pub fn warning(message: impl Into<String>) -> Self {
42        Self::new(NoticeLevel::Warning, message)
43    }
44
45    /// The notice with the SQLSTATE its report names.
46    #[must_use]
47    pub fn with_sqlstate(mut self, sqlstate: impl Into<String>) -> Self {
48        self.sqlstate = sqlstate.into();
49        self
50    }
51
52    /// The notice with a detail.
53    #[must_use]
54    pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
55        self.detail = Some(detail.into());
56        self
57    }
58
59    /// The notice with a hint.
60    #[must_use]
61    pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
62        self.hint = Some(hint.into());
63        self
64    }
65}