Skip to main content

uqa_execution/query/
notice_queue.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The notices a session queues for its client.
8
9use parking_lot::Mutex;
10use std::sync::atomic::{AtomicU8, Ordering};
11use std::sync::Arc;
12use uqa_sql::semantics::parameters::catalog::message_levels;
13use uqa_sql::SQLNotice;
14
15/// The notices a session queues for its client, in the order they arrive. A notice below the session's `client_min_messages` is dropped as it arrives, as `PostgreSQL` decides whether a report reaches the client when it raises it (`errstart`), so a level that a function sets for its own body silences only the notices raised there.
16pub struct NoticeQueue {
17    notices: Mutex<Vec<SQLNotice>>,
18    /// The session's `client_min_messages`, as a message level, which the session keeps current.
19    client_level: Arc<AtomicU8>,
20}
21
22impl Default for NoticeQueue {
23    fn default() -> Self {
24        Self::new(Arc::new(AtomicU8::new(message_levels::NOTICE)))
25    }
26}
27
28impl NoticeQueue {
29    /// A queue that reads the session's `client_min_messages` from `client_level`.
30    pub fn new(client_level: Arc<AtomicU8>) -> Self {
31        Self {
32            notices: Mutex::new(Vec::new()),
33            client_level,
34        }
35    }
36
37    /// Queue `notice` after the notices queued before it, unless its level does not reach the client.
38    pub fn push(&self, notice: SQLNotice) {
39        if notice
40            .level
41            .reaches_client(self.client_level.load(Ordering::Acquire))
42        {
43            self.notices.lock().push(notice);
44        }
45    }
46
47    /// Take every queued notice in the order they arrived.
48    pub fn take(&self) -> Vec<SQLNotice> {
49        std::mem::take(&mut *self.notices.lock())
50    }
51
52    /// The queued notices, left queued.
53    pub fn snapshot(&self) -> Vec<SQLNotice> {
54        self.notices.lock().clone()
55    }
56
57    pub fn len(&self) -> usize {
58        self.notices.lock().len()
59    }
60
61    pub fn is_empty(&self) -> bool {
62        self.notices.lock().is_empty()
63    }
64}