except_plugin/core/
sql.rs

1//! # SQL Exception
2//! ```txt
3//! @author:syf20020816@Outlook.com
4//! @date:2023/8/15
5//! @version:0.0.1
6//! @description:
7//! ```
8
9use std::collections::HashMap;
10use std::error::Error;
11use std::path::{PathBuf};
12use std::fmt::{Debug, Display, Formatter};
13use std::time::{Duration, SystemTime, UNIX_EPOCH};
14use super::{SQL_MSG, SQLExceptionBuilder, ReasonParamImpl, SuperBuilderImpl, SQLParamImpl, ExceptionLevel, NewFrom, FromBuilder, CommonParamImpl, Exception, Exceptions, ExceptionCode, DerefException};
15use crate::{display_err_impl, exception_impl, common_param_impl, reason_param_impl, sql_param_impl, Reasons, SQLReasons};
16
17/// # SQLException
18/// - code: exception code
19/// - msg: exception msg
20/// - line: error line
21/// - path: error file path
22/// - level: exception level
23/// - reason: reason for error
24/// - stmt: sql statement (error)
25/// - tips: how to recover
26#[derive(Debug, Clone, PartialEq)]
27pub struct SQLException {
28    code: u32,
29    msg: String,
30    line: u32,
31    path: PathBuf,
32    level: ExceptionLevel,
33    reason: Reasons,
34    stmt: Option<String>,
35    tips: HashMap<String, String>,
36    timestamp: Duration,
37}
38
39impl Default for SQLException {
40    fn default() -> Self {
41        SQLException {
42            code: ExceptionCode::SQL,
43            msg: String::from(SQL_MSG),
44            line: 0,
45            path: PathBuf::new(),
46            level: ExceptionLevel::Info,
47            reason: Reasons::SQL(SQLReasons::Query),
48            stmt: None,
49            tips: HashMap::new(),
50            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
51        }
52    }
53}
54
55impl NewFrom for SQLException {
56    type Builder = SQLExceptionBuilder;
57    fn new() -> Self::Builder {
58        SQLExceptionBuilder::new()
59    }
60    fn from_super(e: Box<dyn Exception>) -> Self where Self: Sized {
61        SQLException {
62            code: e.code(),
63            msg: String::from(e.msg()),
64            level: e.level(),
65            reason: Reasons::SQL(SQLReasons::Other),
66            stmt: None,
67            path: PathBuf::new(),
68            line: 0,
69            timestamp: e.timestamp(),
70            tips: HashMap::new()
71        }
72    }
73}
74
75impl FromBuilder for SQLException {
76    type Input = SQLExceptionBuilder;
77    type Output = SQLException;
78    fn from_builder(builder: &Self::Input) -> Self::Output {
79        Self::Output {
80            code: builder.code(),
81            msg: String::from(builder.msg()),
82            level: builder.level(),
83            reason: builder.reason(),
84            stmt: Some(String::from(builder.stmt())),
85            line: builder.line(),
86            path: builder.path(),
87            timestamp: builder.timestamp(),
88            tips: builder.tips().clone()
89        }
90    }
91}
92
93display_err_impl!(SQLException);
94
95exception_impl!(SQLException,Exceptions::SQL);
96
97common_param_impl!(SQLException);
98
99reason_param_impl!(SQLException);
100
101sql_param_impl!(SQLException);
102
103impl DerefException for SQLException {
104    fn deref_mut_exception(&mut self) -> Self {
105        SQLException {
106            code: self.code(),
107            msg: String::from(self.msg()),
108            line: self.line(),
109            path: self.path(),
110            level: self.level(),
111            reason: self.reason(),
112            stmt: Some(String::from(self.stmt())),
113            tips: self.tips().clone(),
114            timestamp: self.timestamp(),
115        }
116    }
117}
118
119