uqa_core/cancel/
cancelled.rs1use thiserror::Error;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub enum CancellationReason {
14 #[default]
16 UserRequest,
17 StatementTimeout,
19 LockTimeout,
21 IdleInTransactionSessionTimeout,
23 IdleSessionTimeout,
25 TransactionTimeout,
27}
28
29impl CancellationReason {
30 pub const fn message(self) -> &'static str {
32 match self {
33 Self::UserRequest => "canceling statement due to user request",
34 Self::StatementTimeout => "canceling statement due to statement timeout",
35 Self::LockTimeout => "canceling statement due to lock timeout",
36 Self::IdleInTransactionSessionTimeout => {
37 "terminating connection due to idle-in-transaction timeout"
38 }
39 Self::IdleSessionTimeout => "terminating connection due to idle-session timeout",
40 Self::TransactionTimeout => "terminating connection due to transaction timeout",
41 }
42 }
43
44 pub const fn terminates_session(self) -> bool {
46 matches!(
47 self,
48 Self::IdleInTransactionSessionTimeout
49 | Self::IdleSessionTimeout
50 | Self::TransactionTimeout
51 )
52 }
53
54 pub const fn sqlstate(self) -> &'static str {
56 match self {
57 Self::UserRequest | Self::StatementTimeout => super::SQLSTATE_QUERY_CANCELED,
58 Self::LockTimeout => "55P03",
59 Self::IdleInTransactionSessionTimeout => "25P03",
60 Self::IdleSessionTimeout => "57P05",
61 Self::TransactionTimeout => "25P04",
62 }
63 }
64
65 pub(super) const fn code(self) -> u8 {
66 match self {
67 Self::UserRequest => 1,
68 Self::StatementTimeout => 2,
69 Self::LockTimeout => 3,
70 Self::IdleInTransactionSessionTimeout => 4,
71 Self::IdleSessionTimeout => 5,
72 Self::TransactionTimeout => 6,
73 }
74 }
75
76 pub(super) const fn from_code(code: u8) -> Self {
77 match code {
78 2 => Self::StatementTimeout,
79 3 => Self::LockTimeout,
80 4 => Self::IdleInTransactionSessionTimeout,
81 5 => Self::IdleSessionTimeout,
82 6 => Self::TransactionTimeout,
83 _ => Self::UserRequest,
84 }
85 }
86}
87
88#[derive(Debug, Clone, Copy, Error, PartialEq, Eq, Default)]
90#[error("{}", .reason.message())]
91pub struct QueryCancelled {
92 pub reason: CancellationReason,
93}
94
95impl QueryCancelled {
96 pub const USER_REQUEST: Self = Self::new(CancellationReason::UserRequest);
98
99 pub const fn new(reason: CancellationReason) -> Self {
100 Self { reason }
101 }
102
103 pub const fn sqlstate(&self) -> &'static str {
104 self.reason.sqlstate()
105 }
106}