dm-database-sqllog2db 0.2.2

高性能 CLI 工具:流式解析达梦数据库 SQL 日志并导出到 CSV/Parquet/JSONL/SQLite/DuckDB/PostgreSQL/DM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use crate::constants::LOG_LEVELS;
use crate::error::{ConfigError, Error, Result};
use serde::Deserialize;
use std::path::{Path, PathBuf};

/// 默认表名
#[cfg(any(
    feature = "sqlite",
    feature = "duckdb",
    feature = "postgres",
    feature = "dm"
))]
fn default_table_name() -> String {
    "sqllog_records".to_string()
}

/// 默认 true 值
#[cfg(any(
    feature = "sqlite",
    feature = "duckdb",
    feature = "postgres",
    feature = "dm"
))]
fn default_true() -> bool {
    true
}

/// `PostgreSQL` 默认主机
#[cfg(feature = "postgres")]
fn default_postgres_host() -> String {
    "localhost".to_string()
}

/// `PostgreSQL` 默认端口
#[cfg(feature = "postgres")]
fn default_postgres_port() -> u16 {
    5432
}

/// `PostgreSQL` 默认用户名
#[cfg(feature = "postgres")]
fn default_postgres_username() -> String {
    "postgres".to_string()
}

/// `PostgreSQL` 默认数据库
#[cfg(feature = "postgres")]
fn default_postgres_database() -> String {
    "sqllog".to_string()
}

/// `PostgreSQL` 默认 schema
#[cfg(feature = "postgres")]
fn default_postgres_schema() -> String {
    "public".to_string()
}

#[cfg_attr(feature = "csv", derive(Default))]
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
    /// 新增:SQL 日志输入相关配置
    #[serde(default)]
    pub sqllog: SqllogConfig,
    pub error: ErrorConfig,
    pub logging: LoggingConfig,
    pub features: FeaturesConfig,
    pub exporter: ExporterConfig,
}

impl Config {
    /// 从文件加载配置
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        let content = std::fs::read_to_string(path)
            .map_err(|_| Error::Config(ConfigError::NotFound(path.to_path_buf())))?;
        Self::from_str(&content, path.to_path_buf())
    }

    /// 从字符串解析配置
    pub fn from_str(content: &str, path: PathBuf) -> Result<Self> {
        let config: Config = toml::from_str(content).map_err(|e| {
            Error::Config(ConfigError::ParseFailed {
                path,
                reason: e.to_string(),
            })
        })?;

        // 验证配置
        config.validate()?;

        Ok(config)
    }

    /// 验证配置的有效性
    pub fn validate(&self) -> Result<()> {
        // 验证日志级别
        self.logging.validate()?;

        // 验证导出器配置
        self.exporter.validate()?;

        // 验证 sqllog 配置
        self.sqllog.validate()?;

        Ok(())
    }
}

/// SQL 日志输入配置
#[derive(Debug, Deserialize, Clone)]
pub struct SqllogConfig {
    /// SQL 日志输入目录(可包含多个日志文件)
    pub directory: String,
}

impl Default for SqllogConfig {
    fn default() -> Self {
        Self {
            directory: "sqllogs".to_string(),
        }
    }
}

impl SqllogConfig {
    /// 获取 SQL 日志输入目录
    #[must_use]
    pub fn directory(&self) -> &str {
        &self.directory
    }

    /// 验证配置
    pub fn validate(&self) -> Result<()> {
        if self.directory.trim().is_empty() {
            return Err(Error::Config(ConfigError::InvalidValue {
                field: "sqllog.directory".to_string(),
                value: self.directory.clone(),
                reason: "Input directory cannot be empty".to_string(),
            }));
        }
        Ok(())
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct ErrorConfig {
    /// 错误日志输出文件路径
    pub file: String,
}

impl ErrorConfig {
    /// 获取错误日志输出文件路径
    #[must_use]
    pub fn file(&self) -> &str {
        &self.file
    }
}

impl Default for ErrorConfig {
    fn default() -> Self {
        Self {
            file: "export/errors.log".to_string(),
        }
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct LoggingConfig {
    /// 应用日志输出文件路径
    pub file: String,
    pub level: String,
    #[serde(default = "default_retention_days")]
    pub retention_days: usize,
}

fn default_retention_days() -> usize {
    7
}

impl LoggingConfig {
    /// 获取日志输出文件路径
    #[must_use]
    pub fn file(&self) -> &str {
        &self.file
    }

    /// 获取日志级别
    #[must_use]
    pub fn level(&self) -> &str {
        &self.level
    }

    /// 获取日志保留天数
    #[must_use]
    pub fn retention_days(&self) -> usize {
        self.retention_days
    }

    /// 验证日志级别是否有效
    pub fn validate(&self) -> Result<()> {
        if !LOG_LEVELS
            .iter()
            .any(|&l| l.eq_ignore_ascii_case(self.level.as_str()))
        {
            return Err(Error::Config(ConfigError::InvalidLogLevel {
                level: self.level.clone(),
                valid_levels: LOG_LEVELS.iter().map(|s| (*s).to_string()).collect(),
            }));
        }

        // 验证保留天数(1-365天)
        if self.retention_days == 0 || self.retention_days > 365 {
            return Err(Error::Config(ConfigError::InvalidValue {
                field: "logging.retention_days".to_string(),
                value: self.retention_days.to_string(),
                reason: "Retention days must be between 1 and 365".to_string(),
            }));
        }

        Ok(())
    }
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            file: "logs/sqllog2db.log".to_string(),
            level: "info".to_string(),
            retention_days: 7,
        }
    }
}

/// 通用的 feature 开关
#[derive(Debug, Deserialize, Clone)]
pub struct ReplaceParametersFeature {
    pub enable: bool,
    pub symbols: Option<Vec<String>>,
}

#[derive(Debug, Deserialize, Clone, Default)]
pub struct FeaturesConfig {
    /// 对应配置文件中的 `[features.replace_parameters]`
    #[serde(default)]
    pub replace_parameters: Option<ReplaceParametersFeature>,
}

impl FeaturesConfig {
    /// 是否启用 SQL 参数替换
    #[must_use]
    pub fn should_replace_sql_parameters(&self) -> bool {
        self.replace_parameters.as_ref().is_some_and(|f| f.enable)
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct ExporterConfig {
    #[cfg(feature = "csv")]
    pub csv: Option<CsvExporter>,
    #[cfg(feature = "parquet")]
    pub parquet: Option<ParquetExporter>,
    #[cfg(feature = "jsonl")]
    pub jsonl: Option<JsonlExporter>,
    #[cfg(feature = "sqlite")]
    pub sqlite: Option<SqliteExporter>,
    #[cfg(feature = "duckdb")]
    pub duckdb: Option<DuckdbExporter>,
    #[cfg(feature = "postgres")]
    pub postgres: Option<PostgresExporter>,
    #[cfg(feature = "dm")]
    pub dm: Option<DmExporter>,
}

impl ExporterConfig {
    /// 获取 CSV 导出器配置
    #[cfg(feature = "csv")]
    #[must_use]
    pub fn csv(&self) -> Option<&CsvExporter> {
        self.csv.as_ref()
    }

    #[cfg(feature = "parquet")]
    /// 获取 Parquet 导出器配置
    #[must_use]
    pub fn parquet(&self) -> Option<&ParquetExporter> {
        self.parquet.as_ref()
    }

    #[cfg(feature = "jsonl")]
    /// 获取 JSONL 导出器配置
    #[must_use]
    pub fn jsonl(&self) -> Option<&JsonlExporter> {
        self.jsonl.as_ref()
    }

    #[cfg(feature = "sqlite")]
    /// 获取 `SQLite` 导出器配置
    #[must_use]
    pub fn sqlite(&self) -> Option<&SqliteExporter> {
        self.sqlite.as_ref()
    }

    #[cfg(feature = "duckdb")]
    /// 获取 `DuckDB` 导出器配置
    #[must_use]
    pub fn duckdb(&self) -> Option<&DuckdbExporter> {
        self.duckdb.as_ref()
    }

    #[cfg(feature = "postgres")]
    /// 获取 `PostgreSQL` 导出器配置
    #[must_use]
    pub fn postgres(&self) -> Option<&PostgresExporter> {
        self.postgres.as_ref()
    }

    #[cfg(feature = "dm")]
    /// 获取 DM 导出器配置
    #[must_use]
    pub fn dm(&self) -> Option<&DmExporter> {
        self.dm.as_ref()
    }

    /// 检查是否有任何导出器配置
    #[must_use]
    pub fn has_exporters(&self) -> bool {
        let mut found = false;
        #[cfg(feature = "csv")]
        {
            found = found || self.csv.is_some();
        }
        #[cfg(feature = "parquet")]
        {
            found = found || self.parquet.is_some();
        }
        #[cfg(feature = "jsonl")]
        {
            found = found || self.jsonl.is_some();
        }
        #[cfg(feature = "sqlite")]
        {
            found = found || self.sqlite.is_some();
        }
        #[cfg(feature = "duckdb")]
        {
            found = found || self.duckdb.is_some();
        }
        #[cfg(feature = "postgres")]
        {
            found = found || self.postgres.is_some();
        }
        #[cfg(feature = "dm")]
        {
            found = found || self.dm.is_some();
        }
        found
    }

    /// 统计配置的导出器总数
    #[must_use]
    pub fn total_exporters(&self) -> usize {
        let mut count = 0;
        #[cfg(feature = "csv")]
        {
            if self.csv.is_some() {
                count += 1;
            }
        }
        #[cfg(feature = "parquet")]
        {
            if self.parquet.is_some() {
                count += 1;
            }
        }
        #[cfg(feature = "jsonl")]
        {
            if self.jsonl.is_some() {
                count += 1;
            }
        }
        #[cfg(feature = "sqlite")]
        {
            if self.sqlite.is_some() {
                count += 1;
            }
        }
        #[cfg(feature = "duckdb")]
        {
            if self.duckdb.is_some() {
                count += 1;
            }
        }
        #[cfg(feature = "postgres")]
        {
            if self.postgres.is_some() {
                count += 1;
            }
        }
        #[cfg(feature = "dm")]
        {
            if self.dm.is_some() {
                count += 1;
            }
        }
        count
    }

    /// 验证导出器配置(只支持单个导出器)
    pub fn validate(&self) -> Result<()> {
        if !self.has_exporters() {
            return Err(Error::Config(ConfigError::NoExporters));
        }

        let total = self.total_exporters();
        if total > 1 {
            eprintln!("Warning: {total} exporters configured, but only one is supported.");
            eprintln!(
                "Will use the first exporter by priority: CSV > Parquet > JSONL > SQLite > DuckDB > PostgreSQL > DM"
            );
        }

        Ok(())
    }
}

impl Default for ExporterConfig {
    fn default() -> Self {
        Self {
            #[cfg(feature = "csv")]
            csv: Some(CsvExporter::default()),
            #[cfg(feature = "parquet")]
            parquet: Some(ParquetExporter::default()),
            #[cfg(feature = "jsonl")]
            jsonl: None,
            #[cfg(feature = "sqlite")]
            sqlite: None,
            #[cfg(feature = "duckdb")]
            duckdb: None,
            #[cfg(feature = "postgres")]
            postgres: None,
            #[cfg(feature = "dm")]
            dm: None,
        }
    }
}

#[cfg(feature = "parquet")]
#[derive(Debug, Deserialize, Clone)]
pub struct ParquetExporter {
    /// Parquet 输出文件路径
    pub file: String,
    /// 是否覆盖已存在的文件
    pub overwrite: bool,
    /// 每个 row group 的行数
    pub row_group_size: Option<usize>,
    /// 是否启用字典编码
    pub use_dictionary: Option<bool>,
}

#[cfg(feature = "parquet")]
impl Default for ParquetExporter {
    fn default() -> Self {
        Self {
            file: "export/sqllog2db.parquet".to_string(),
            overwrite: true,
            row_group_size: Some(100_000),
            use_dictionary: Some(true),
        }
    }
}

#[cfg(feature = "csv")]
#[derive(Debug, Deserialize, Clone)]
pub struct CsvExporter {
    /// CSV 输出文件路径
    pub file: String,
    /// 是否覆盖已存在的文件
    pub overwrite: bool,
    /// 是否追加模式(暂未实现)
    pub append: bool,
}

#[cfg(feature = "csv")]
impl Default for CsvExporter {
    fn default() -> Self {
        Self {
            file: "outputs/sqllog.csv".to_string(),
            overwrite: true,
            append: false,
        }
    }
}

#[cfg(feature = "jsonl")]
#[derive(Debug, Deserialize, Clone)]
pub struct JsonlExporter {
    /// JSONL 输出文件路径
    pub file: String,
    /// 是否覆盖已存在的文件
    pub overwrite: bool,
    /// 是否追加模式
    pub append: bool,
}

#[cfg(feature = "jsonl")]
impl Default for JsonlExporter {
    fn default() -> Self {
        Self {
            file: "export/sqllog2db.jsonl".to_string(),
            overwrite: true,
            append: false,
        }
    }
}

#[cfg(feature = "sqlite")]
#[derive(Debug, Deserialize, Clone)]
pub struct SqliteExporter {
    /// `SQLite` 数据库文件路径
    pub database_url: String,
    /// 表名
    #[serde(default = "default_table_name")]
    pub table_name: String,
    /// 是否覆盖已存在的表
    #[serde(default = "default_true")]
    pub overwrite: bool,
    /// 是否追加模式
    #[serde(default)]
    pub append: bool,
}

#[cfg(feature = "sqlite")]
impl Default for SqliteExporter {
    fn default() -> Self {
        Self {
            database_url: "export/sqllog2db.db".to_string(),
            table_name: "sqllog_records".to_string(),
            overwrite: true,
            append: false,
        }
    }
}

#[cfg(feature = "duckdb")]
#[derive(Debug, Deserialize, Clone)]
pub struct DuckdbExporter {
    /// `DuckDB` 数据库文件路径
    pub database_url: String,
    /// 表名
    #[serde(default = "default_table_name")]
    pub table_name: String,
    /// 是否覆盖已存在的表
    #[serde(default = "default_true")]
    pub overwrite: bool,
    /// 是否追加模式
    #[serde(default)]
    pub append: bool,
}

#[cfg(feature = "duckdb")]
impl Default for DuckdbExporter {
    fn default() -> Self {
        Self {
            database_url: "export/sqllog2db.duckdb".to_string(),
            table_name: "sqllog_records".to_string(),
            overwrite: true,
            append: false,
        }
    }
}

#[cfg(feature = "postgres")]
#[derive(Debug, Deserialize, Clone)]
pub struct PostgresExporter {
    /// `PostgreSQL` 主机地址
    #[serde(default = "default_postgres_host")]
    pub host: String,
    /// `PostgreSQL` 端口
    #[serde(default = "default_postgres_port")]
    pub port: u16,
    /// 用户名
    #[serde(default = "default_postgres_username")]
    pub username: String,
    /// 密码
    pub password: String,
    /// 数据库名
    #[serde(default = "default_postgres_database")]
    pub database: String,
    /// Schema 名称
    #[serde(default = "default_postgres_schema")]
    pub schema: String,
    /// 表名
    #[serde(default = "default_table_name")]
    pub table_name: String,
    /// 是否覆盖已存在的表
    #[serde(default = "default_true")]
    pub overwrite: bool,
    /// 是否追加模式
    #[serde(default)]
    pub append: bool,
}

#[cfg(feature = "postgres")]
impl Default for PostgresExporter {
    fn default() -> Self {
        Self {
            host: "localhost".to_string(),
            port: 5432,
            username: "postgres".to_string(),
            password: "postgres".to_string(),
            database: "sqllog".to_string(),
            schema: "public".to_string(),
            table_name: "sqllog_records".to_string(),
            overwrite: true,
            append: false,
        }
    }
}

#[cfg(feature = "postgres")]
impl PostgresExporter {
    /// 获取连接字符串
    #[must_use]
    pub fn connection_string(&self) -> String {
        if self.password.is_empty() {
            format!(
                "host={} port={} user={} dbname={}",
                self.host, self.port, self.username, self.database
            )
        } else {
            format!(
                "host={} port={} user={} password={} dbname={}",
                self.host, self.port, self.username, self.password, self.database
            )
        }
    }
}

#[cfg(feature = "dm")]
#[derive(Debug, Deserialize, Clone)]
pub struct DmExporter {
    /// DM 数据库连接字符串 (例如: SYSDBA/SYSDBA@localhost:5236)
    pub userid: String,
    /// 表名
    #[serde(default = "default_table_name")]
    pub table_name: String,
    /// 控制文件路径
    pub control_file: String,
    /// 日志目录
    pub log_dir: String,
}

#[cfg(feature = "dm")]
impl Default for DmExporter {
    fn default() -> Self {
        Self {
            userid: "SYSDBA/SYSDBA@localhost:5236".to_string(),
            table_name: "sqllog_records".to_string(),
            control_file: "export/sqllog.ctl".to_string(),
            log_dir: "export/log".to_string(),
        }
    }
}