dm_database_parser_sqllog/
sqllog.rs1#[derive(Debug, Clone, PartialEq, Default)]
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)]
33pub struct MetaParts {
34 pub ep: u8,
36
37 pub sess_id: String,
39
40 pub thrd_id: String,
42
43 pub username: String,
45
46 pub trxid: String,
48
49 pub statement: String,
51
52 pub appname: String,
54
55 pub client_ip: String,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Default)]
65pub struct IndicatorsParts {
66 pub execute_time: f32,
68
69 pub row_count: u32,
71
72 pub execute_id: i64,
74}
75
76impl Sqllog {
77 #[inline]
83 pub fn has_indicators(&self) -> bool {
84 self.indicators.is_some()
85 }
86
87 #[inline]
93 pub fn execute_time(&self) -> Option<f32> {
94 self.indicators.map(|i| i.execute_time)
95 }
96
97 #[inline]
103 pub fn row_count(&self) -> Option<u32> {
104 self.indicators.map(|i| i.row_count)
105 }
106
107 #[inline]
113 pub fn execute_id(&self) -> Option<i64> {
114 self.indicators.map(|i| i.execute_id)
115 }
116}
117
118impl MetaParts {
119 #[inline]
125 pub fn has_client_ip(&self) -> bool {
126 !self.client_ip.is_empty()
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn test_sqllog_has_indicators() {
136 let sqllog = Sqllog {
138 ts: "2025-08-12 10:57:09.548".to_string(),
139 meta: MetaParts::default(),
140 body: "SELECT 1".to_string(),
141 indicators: Some(IndicatorsParts {
142 execute_time: 10.5,
143 row_count: 100,
144 execute_id: 12345,
145 }),
146 };
147 assert!(sqllog.has_indicators());
148
149 let sqllog_no_indicators = Sqllog {
151 ts: "2025-08-12 10:57:09.548".to_string(),
152 meta: MetaParts::default(),
153 body: "SELECT 1".to_string(),
154 indicators: None,
155 };
156 assert!(!sqllog_no_indicators.has_indicators());
157 }
158
159 #[test]
160 fn test_sqllog_execute_time() {
161 let sqllog = Sqllog {
163 ts: "2025-08-12 10:57:09.548".to_string(),
164 meta: MetaParts::default(),
165 body: "SELECT 1".to_string(),
166 indicators: Some(IndicatorsParts {
167 execute_time: 10.5,
168 row_count: 100,
169 execute_id: 12345,
170 }),
171 };
172 assert_eq!(sqllog.execute_time(), Some(10.5));
173
174 let sqllog_no_indicators = Sqllog {
176 ts: "2025-08-12 10:57:09.548".to_string(),
177 meta: MetaParts::default(),
178 body: "SELECT 1".to_string(),
179 indicators: None,
180 };
181 assert_eq!(sqllog_no_indicators.execute_time(), None);
182 }
183
184 #[test]
185 fn test_sqllog_row_count() {
186 let sqllog = Sqllog {
188 ts: "2025-08-12 10:57:09.548".to_string(),
189 meta: MetaParts::default(),
190 body: "SELECT 1".to_string(),
191 indicators: Some(IndicatorsParts {
192 execute_time: 10.5,
193 row_count: 100,
194 execute_id: 12345,
195 }),
196 };
197 assert_eq!(sqllog.row_count(), Some(100));
198
199 let sqllog_no_indicators = Sqllog {
201 ts: "2025-08-12 10:57:09.548".to_string(),
202 meta: MetaParts::default(),
203 body: "SELECT 1".to_string(),
204 indicators: None,
205 };
206 assert_eq!(sqllog_no_indicators.row_count(), None);
207 }
208
209 #[test]
210 fn test_sqllog_execute_id() {
211 let sqllog = Sqllog {
213 ts: "2025-08-12 10:57:09.548".to_string(),
214 meta: MetaParts::default(),
215 body: "SELECT 1".to_string(),
216 indicators: Some(IndicatorsParts {
217 execute_time: 10.5,
218 row_count: 100,
219 execute_id: 12345,
220 }),
221 };
222 assert_eq!(sqllog.execute_id(), Some(12345));
223
224 let sqllog_no_indicators = Sqllog {
226 ts: "2025-08-12 10:57:09.548".to_string(),
227 meta: MetaParts::default(),
228 body: "SELECT 1".to_string(),
229 indicators: None,
230 };
231 assert_eq!(sqllog_no_indicators.execute_id(), None);
232 }
233
234 #[test]
235 fn test_meta_has_client_ip() {
236 let meta_with_ip = MetaParts {
238 ep: 0,
239 sess_id: "123".to_string(),
240 thrd_id: "456".to_string(),
241 username: "alice".to_string(),
242 trxid: "789".to_string(),
243 statement: "999".to_string(),
244 appname: "app".to_string(),
245 client_ip: "192.168.1.1".to_string(),
246 };
247 assert!(meta_with_ip.has_client_ip());
248
249 let meta_no_ip = MetaParts {
251 ep: 0,
252 sess_id: "123".to_string(),
253 thrd_id: "456".to_string(),
254 username: "alice".to_string(),
255 trxid: "789".to_string(),
256 statement: "999".to_string(),
257 appname: "app".to_string(),
258 client_ip: "".to_string(),
259 };
260 assert!(!meta_no_ip.has_client_ip());
261 }
262
263 #[test]
264 fn test_indicators_copy_trait() {
265 let indicators = IndicatorsParts {
266 execute_time: 10.5,
267 row_count: 100,
268 execute_id: 12345,
269 };
270 let copied = indicators;
271 assert_eq!(indicators.execute_time, copied.execute_time);
272 assert_eq!(indicators.row_count, copied.row_count);
273 assert_eq!(indicators.execute_id, copied.execute_id);
274 }
275
276 #[test]
277 fn test_sqllog_default() {
278 let sqllog = Sqllog::default();
279 assert_eq!(sqllog.ts, "");
280 assert_eq!(sqllog.body, "");
281 assert!(sqllog.indicators.is_none());
282 }
283
284 #[test]
285 fn test_meta_default() {
286 let meta = MetaParts::default();
287 assert_eq!(meta.ep, 0);
288 assert_eq!(meta.sess_id, "");
289 assert_eq!(meta.username, "");
290 assert!(!meta.has_client_ip());
291 }
292
293 #[test]
294 fn test_indicators_default() {
295 let indicators = IndicatorsParts::default();
296 assert_eq!(indicators.execute_time, 0.0);
297 assert_eq!(indicators.row_count, 0);
298 assert_eq!(indicators.execute_id, 0);
299 }
300}