uqa-sql 0.5.2

PostgreSQL-compatible SQL compiler built on libpg_query
Documentation
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! The levels of notices.

/// The level of a notice, as `PostgreSQL` names its message levels below `ERROR`, in increasing order of severity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NoticeLevel {
    /// `DEBUG`, which `PL/pgSQL`'s `RAISE DEBUG` reports as `DEBUG1`.
    Debug,
    Log,
    Info,
    Notice,
    Warning,
}

impl NoticeLevel {
    /// The level a name spells, as `RAISE` and `elog` accept it; `DEBUG1` through `DEBUG5` are `DEBUG`.
    pub fn parse(name: &str) -> Option<Self> {
        let name = name.to_ascii_uppercase();
        match name.as_str() {
            "LOG" => Some(NoticeLevel::Log),
            "INFO" => Some(NoticeLevel::Info),
            "NOTICE" => Some(NoticeLevel::Notice),
            "WARNING" => Some(NoticeLevel::Warning),
            _ if name.starts_with("DEBUG") => Some(NoticeLevel::Debug),
            _ => None,
        }
    }

    /// The level's name, as clients show it.
    pub const fn as_str(self) -> &'static str {
        match self {
            NoticeLevel::Debug => "DEBUG",
            NoticeLevel::Log => "LOG",
            NoticeLevel::Info => "INFO",
            NoticeLevel::Notice => "NOTICE",
            NoticeLevel::Warning => "WARNING",
        }
    }

    /// The message level of `PostgreSQL`'s `elog.h` that the notice is reported at, which `client_min_messages` compares against.
    pub const fn message_level(self) -> u8 {
        use crate::semantics::parameters::catalog::message_levels;
        match self {
            NoticeLevel::Debug => message_levels::DEBUG1,
            NoticeLevel::Log => message_levels::LOG,
            NoticeLevel::Info => message_levels::INFO,
            NoticeLevel::Notice => message_levels::NOTICE,
            NoticeLevel::Warning => message_levels::WARNING,
        }
    }

    /// Whether a notice of this level reaches a client whose `client_min_messages` is `minimum`: `INFO` always does, and every other level at or above the minimum (`errstart`'s `output_to_client`).
    pub const fn reaches_client(self, minimum: u8) -> bool {
        matches!(self, NoticeLevel::Info) || self.message_level() >= minimum
    }

    /// The SQLSTATE of a notice whose report names none, as `errstart` assigns it: `01000` (`warning`) for a warning and `00000` (`successful_completion`) for every lower level.
    pub const fn default_sqlstate(self) -> &'static str {
        match self {
            NoticeLevel::Warning => "01000",
            NoticeLevel::Debug | NoticeLevel::Log | NoticeLevel::Info | NoticeLevel::Notice => {
                "00000"
            }
        }
    }
}