dm_database_parser_sqllog/parser/record.rs
1//! Record 结构定义和相关方法
2//!
3//! Record 表示一条原始的日志记录,可能包含多行(起始行 + 继续行)。
4
5use crate::error::ParseError;
6use crate::sqllog::Sqllog;
7
8/// 表示一条完整的日志记录(可能包含多行)
9///
10/// 日志记录由一个起始行和零个或多个继续行组成。起始行包含时间戳和元数据,
11/// 继续行包含多行 SQL 语句的后续部分。
12///
13
14#[derive(Debug, Clone, PartialEq)]
15pub struct Record {
16 /// 记录的所有行(第一行是起始行,后续行是继续行)
17 pub lines: Vec<String>,
18}
19
20impl Record {
21 /// 创建新的记录
22 ///
23 /// # 参数
24 ///
25 /// * `start_line` - 记录的起始行
26 pub fn new(start_line: String) -> Self {
27 Self {
28 lines: vec![start_line],
29 }
30 }
31
32 /// 添加继续行
33 ///
34 /// # 参数
35 ///
36 /// * `line` - 要添加的继续行
37 pub fn add_line(&mut self, line: String) {
38 self.lines.push(line);
39 }
40
41 /// 获取起始行
42 ///
43 /// # 返回
44 ///
45 /// 返回记录的第一行(起始行)
46 pub fn start_line(&self) -> &str {
47 &self.lines[0]
48 }
49
50 /// 获取所有行
51 ///
52 /// # 返回
53 ///
54 /// 返回包含所有行的切片
55 pub fn all_lines(&self) -> &[String] {
56 &self.lines
57 }
58
59 /// 获取完整的记录内容(所有行拼接)
60 ///
61 /// # 返回
62 ///
63 /// 返回所有行用换行符拼接后的字符串
64 pub fn full_content(&self) -> String {
65 self.lines.join("\n")
66 }
67
68 /// 判断是否有继续行
69 ///
70 /// # 返回
71 ///
72 /// 如果记录包含多行(有继续行)返回 `true`,否则返回 `false`
73 pub fn has_continuation_lines(&self) -> bool {
74 self.lines.len() > 1
75 }
76
77 /// 将 Record 解析为 Sqllog
78 ///
79 /// # 返回
80 ///
81 /// * `Ok(Sqllog)` - 解析成功
82 /// * `Err(ParseError)` - 解析失败
83 pub fn parse_to_sqllog(&self) -> Result<Sqllog, ParseError> {
84 let lines: Vec<&str> = self.lines.iter().map(|s| s.as_str()).collect();
85 super::parse_functions::parse_record(&lines)
86 }
87}