dm_database_parser_sqllog/
sqllog.rs1#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Default)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15pub struct Sqllog {
16 pub ts: String,
18
19 pub meta: MetaParts,
21
22 pub body: String,
24
25 pub indicators: Option<IndicatorsParts>,
27}
28
29#[derive(Debug, Clone, PartialEq, Default)]
33#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
34pub struct MetaParts {
35 pub ep: u8,
37
38 pub sess_id: String,
40
41 pub thrd_id: String,
43
44 pub username: String,
46
47 pub trxid: String,
49
50 pub statement: String,
52
53 pub appname: String,
55
56 pub client_ip: String,
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Default)]
66#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
67pub struct IndicatorsParts {
68 pub execute_time: f32,
70
71 pub row_count: u32,
73
74 pub execute_id: i64,
76}
77
78impl Sqllog {
79 #[inline]
85 pub fn has_indicators(&self) -> bool {
86 self.indicators.is_some()
87 }
88
89 #[inline]
95 pub fn execute_time(&self) -> Option<f32> {
96 self.indicators.map(|i| i.execute_time)
97 }
98
99 #[inline]
105 pub fn row_count(&self) -> Option<u32> {
106 self.indicators.map(|i| i.row_count)
107 }
108
109 #[inline]
115 pub fn execute_id(&self) -> Option<i64> {
116 self.indicators.map(|i| i.execute_id)
117 }
118}
119
120impl MetaParts {
121 #[inline]
127 pub fn has_client_ip(&self) -> bool {
128 !self.client_ip.is_empty()
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use super::*;
135
136 #[test]
137 fn test_sqllog_has_indicators() {
138 let sqllog = Sqllog {
140 ts: "2025-08-12 10:57:09.548".to_string(),
141 meta: MetaParts::default(),
142 body: "SELECT 1".to_string(),
143 indicators: Some(IndicatorsParts {
144 execute_time: 10.5,
145 row_count: 100,
146 execute_id: 12345,
147 }),
148 };
149 assert!(sqllog.has_indicators());
150
151 let sqllog_no_indicators = Sqllog {
153 ts: "2025-08-12 10:57:09.548".to_string(),
154 meta: MetaParts::default(),
155 body: "SELECT 1".to_string(),
156 indicators: None,
157 };
158 assert!(!sqllog_no_indicators.has_indicators());
159 }
160
161 #[test]
162 fn test_sqllog_execute_time() {
163 let sqllog = Sqllog {
165 ts: "2025-08-12 10:57:09.548".to_string(),
166 meta: MetaParts::default(),
167 body: "SELECT 1".to_string(),
168 indicators: Some(IndicatorsParts {
169 execute_time: 10.5,
170 row_count: 100,
171 execute_id: 12345,
172 }),
173 };
174 assert_eq!(sqllog.execute_time(), Some(10.5));
175
176 let sqllog_no_indicators = Sqllog {
178 ts: "2025-08-12 10:57:09.548".to_string(),
179 meta: MetaParts::default(),
180 body: "SELECT 1".to_string(),
181 indicators: None,
182 };
183 assert_eq!(sqllog_no_indicators.execute_time(), None);
184 }
185
186 #[test]
187 fn test_sqllog_row_count() {
188 let sqllog = Sqllog {
190 ts: "2025-08-12 10:57:09.548".to_string(),
191 meta: MetaParts::default(),
192 body: "SELECT 1".to_string(),
193 indicators: Some(IndicatorsParts {
194 execute_time: 10.5,
195 row_count: 100,
196 execute_id: 12345,
197 }),
198 };
199 assert_eq!(sqllog.row_count(), Some(100));
200
201 let sqllog_no_indicators = Sqllog {
203 ts: "2025-08-12 10:57:09.548".to_string(),
204 meta: MetaParts::default(),
205 body: "SELECT 1".to_string(),
206 indicators: None,
207 };
208 assert_eq!(sqllog_no_indicators.row_count(), None);
209 }
210
211 #[test]
212 fn test_sqllog_execute_id() {
213 let sqllog = Sqllog {
215 ts: "2025-08-12 10:57:09.548".to_string(),
216 meta: MetaParts::default(),
217 body: "SELECT 1".to_string(),
218 indicators: Some(IndicatorsParts {
219 execute_time: 10.5,
220 row_count: 100,
221 execute_id: 12345,
222 }),
223 };
224 assert_eq!(sqllog.execute_id(), Some(12345));
225
226 let sqllog_no_indicators = Sqllog {
228 ts: "2025-08-12 10:57:09.548".to_string(),
229 meta: MetaParts::default(),
230 body: "SELECT 1".to_string(),
231 indicators: None,
232 };
233 assert_eq!(sqllog_no_indicators.execute_id(), None);
234 }
235
236 #[test]
237 fn test_meta_has_client_ip() {
238 let meta_with_ip = MetaParts {
240 ep: 0,
241 sess_id: "123".to_string(),
242 thrd_id: "456".to_string(),
243 username: "alice".to_string(),
244 trxid: "789".to_string(),
245 statement: "999".to_string(),
246 appname: "app".to_string(),
247 client_ip: "192.168.1.1".to_string(),
248 };
249 assert!(meta_with_ip.has_client_ip());
250
251 let meta_no_ip = MetaParts {
253 ep: 0,
254 sess_id: "123".to_string(),
255 thrd_id: "456".to_string(),
256 username: "alice".to_string(),
257 trxid: "789".to_string(),
258 statement: "999".to_string(),
259 appname: "app".to_string(),
260 client_ip: "".to_string(),
261 };
262 assert!(!meta_no_ip.has_client_ip());
263 }
264
265 #[test]
266 fn test_indicators_copy_trait() {
267 let indicators = IndicatorsParts {
268 execute_time: 10.5,
269 row_count: 100,
270 execute_id: 12345,
271 };
272 let copied = indicators;
273 assert_eq!(indicators.execute_time, copied.execute_time);
274 assert_eq!(indicators.row_count, copied.row_count);
275 assert_eq!(indicators.execute_id, copied.execute_id);
276 }
277
278 #[test]
279 fn test_sqllog_default() {
280 let sqllog = Sqllog::default();
281 assert_eq!(sqllog.ts, "");
282 assert_eq!(sqllog.body, "");
283 assert!(sqllog.indicators.is_none());
284 }
285
286 #[test]
287 fn test_meta_default() {
288 let meta = MetaParts::default();
289 assert_eq!(meta.ep, 0);
290 assert_eq!(meta.sess_id, "");
291 assert_eq!(meta.username, "");
292 assert!(!meta.has_client_ip());
293 }
294
295 #[test]
296 fn test_indicators_default() {
297 let indicators = IndicatorsParts::default();
298 assert_eq!(indicators.execute_time, 0.0);
299 assert_eq!(indicators.row_count, 0);
300 assert_eq!(indicators.execute_id, 0);
301 }
302}