wae-database 0.0.2

WAE Database - 数据库服务抽象层,支持 Turso/PostgreSQL/MySQL
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//! Schema 定义模块
//!
//! 提供数据库表结构的元信息定义,用于自动迁移。

use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, path::Path, sync::Mutex};

/// 数据库类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DatabaseType {
    /// Turso/SQLite
    Turso,
    /// PostgreSQL
    Postgres,
    /// MySQL
    MySql,
}

impl DatabaseType {
    /// 获取当前数据库类型(根据 feature flag)
    pub fn current() -> Self {
        #[cfg(feature = "postgres")]
        {
            return DatabaseType::Postgres;
        }
        #[cfg(feature = "mysql")]
        {
            return DatabaseType::MySql;
        }
        #[cfg(all(not(feature = "postgres"), not(feature = "mysql")))]
        {
            return DatabaseType::Turso;
        }
    }
}

/// 列类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ColumnType {
    /// 整数类型
    Integer,
    /// 浮点类型
    Real,
    /// 文本类型
    Text,
    /// 二进制类型
    Blob,
}

impl ColumnType {
    /// 转换为 SQL 类型字符串
    pub fn to_sql(&self) -> &'static str {
        self.to_sql_for(DatabaseType::Turso)
    }

    /// 转换为指定数据库的 SQL 类型字符串
    pub fn to_sql_for(&self, db_type: DatabaseType) -> &'static str {
        match db_type {
            DatabaseType::Turso => match self {
                ColumnType::Integer => "INTEGER",
                ColumnType::Real => "REAL",
                ColumnType::Text => "TEXT",
                ColumnType::Blob => "BLOB",
            },
            DatabaseType::Postgres => match self {
                ColumnType::Integer => "BIGINT",
                ColumnType::Real => "DOUBLE PRECISION",
                ColumnType::Text => "TEXT",
                ColumnType::Blob => "BYTEA",
            },
            DatabaseType::MySql => match self {
                ColumnType::Integer => "BIGINT",
                ColumnType::Real => "DOUBLE",
                ColumnType::Text => "VARCHAR(255)",
                ColumnType::Blob => "BLOB",
            },
        }
    }

    /// 转换为 MySQL SQL 类型字符串
    #[cfg(feature = "mysql")]
    pub fn to_mysql_sql(&self) -> &'static str {
        self.to_sql_for(DatabaseType::MySql)
    }

    /// 从 SQL 类型字符串解析
    pub fn from_sql(sql: &str) -> Self {
        match sql.to_uppercase().as_str() {
            "INTEGER" | "INT" | "BIGINT" | "SMALLINT" | "TINYINT" | "SERIAL" | "BIGSERIAL" => ColumnType::Integer,
            "REAL" | "FLOAT" | "DOUBLE" | "NUMERIC" | "DECIMAL" | "DOUBLE PRECISION" => ColumnType::Real,
            "TEXT" | "VARCHAR" | "CHAR" | "STRING" | "VARCHAR(255)" | "TEXT[]" => ColumnType::Text,
            "BLOB" | "BINARY" | "BYTEA" | "LONGBLOB" => ColumnType::Blob,
            _ => ColumnType::Text,
        }
    }
}

/// 列定义
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnDef {
    /// 列名
    pub name: String,
    /// 列类型
    pub col_type: ColumnType,
    /// 是否可空
    pub nullable: bool,
    /// 是否主键
    pub primary_key: bool,
    /// 是否自增
    pub auto_increment: bool,
    /// 默认值
    pub default_value: Option<String>,
    /// 是否唯一
    pub unique: bool,
}

impl ColumnDef {
    /// 创建新的列定义
    pub fn new(name: impl Into<String>, col_type: ColumnType) -> Self {
        Self {
            name: name.into(),
            col_type,
            nullable: true,
            primary_key: false,
            auto_increment: false,
            default_value: None,
            unique: false,
        }
    }

    /// 设置为不可空
    pub fn not_null(mut self) -> Self {
        self.nullable = false;
        self
    }

    /// 设置为主键
    pub fn primary_key(mut self) -> Self {
        self.primary_key = true;
        self.nullable = false;
        self
    }

    /// 设置为自增
    pub fn auto_increment(mut self) -> Self {
        self.auto_increment = true;
        self
    }

    /// 设置默认值
    pub fn default(mut self, value: impl Into<String>) -> Self {
        self.default_value = Some(value.into());
        self
    }

    /// 设置为唯一
    pub fn unique(mut self) -> Self {
        self.unique = true;
        self
    }

    /// 生成建表 SQL 片段(使用当前数据库类型)
    pub fn to_sql(&self) -> String {
        self.to_sql_for(DatabaseType::current())
    }

    /// 生成指定数据库类型的建表 SQL 片段
    pub fn to_sql_for(&self, db_type: DatabaseType) -> String {
        let mut sql = format!("{} {}", self.name, self.col_type.to_sql_for(db_type));

        if self.primary_key {
            sql.push_str(" PRIMARY KEY");
        }

        if self.auto_increment {
            match db_type {
                DatabaseType::Turso => sql.push_str(" AUTOINCREMENT"),
                DatabaseType::Postgres => {
                    if self.primary_key && self.col_type == ColumnType::Integer {
                        sql = format!("{} SERIAL", self.name);
                        sql.push_str(" PRIMARY KEY");
                    }
                }
                DatabaseType::MySql => sql.push_str(" AUTO_INCREMENT"),
            }
        }

        if !self.nullable && !self.primary_key {
            sql.push_str(" NOT NULL");
        }

        if let Some(ref default) = self.default_value {
            sql.push_str(&format!(" DEFAULT {}", default));
        }

        if self.unique && !self.primary_key {
            sql.push_str(" UNIQUE");
        }

        sql
    }

    /// 生成 MySQL 建表 SQL 片段
    #[cfg(feature = "mysql")]
    pub fn to_mysql_sql(&self) -> String {
        self.to_sql_for(DatabaseType::MySql)
    }
}

/// 索引定义
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexDef {
    /// 索引名称
    pub name: String,
    /// 表名
    pub table_name: String,
    /// 列名列表
    pub columns: Vec<String>,
    /// 是否唯一索引
    pub unique: bool,
}

impl IndexDef {
    /// 创建新的索引定义
    pub fn new(name: impl Into<String>, table_name: impl Into<String>, columns: Vec<String>) -> Self {
        Self { name: name.into(), table_name: table_name.into(), columns, unique: false }
    }

    /// 设置为唯一索引
    pub fn unique(mut self) -> Self {
        self.unique = true;
        self
    }

    /// 生成创建索引 SQL(使用当前数据库类型)
    pub fn to_create_sql(&self) -> String {
        self.to_create_sql_for(DatabaseType::current())
    }

    /// 生成创建索引 SQL(指定数据库类型)
    pub fn to_create_sql_for(&self, db_type: DatabaseType) -> String {
        let unique_str = if self.unique { "UNIQUE " } else { "" };
        let columns = self.columns.join(", ");
        match db_type {
            DatabaseType::Turso | DatabaseType::Postgres => {
                format!("CREATE {}INDEX {} ON {} ({})", unique_str, self.name, self.table_name, columns)
            }
            DatabaseType::MySql => {
                format!("CREATE {}INDEX {} ON {} ({})", unique_str, self.name, self.table_name, columns)
            }
        }
    }

    /// 生成删除索引 SQL
    pub fn to_drop_sql(&self) -> String {
        format!("DROP INDEX IF EXISTS {}", self.name)
    }
}

/// 数据库 schema 定义
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseSchema {
    /// 数据库名称
    pub name: String,
    /// 数据库类型
    pub r#type: DatabaseType,
    /// 数据库连接字符串(可以直接包含密码)
    pub url: Option<String>,
    /// 环境变量名称(用于从环境变量获取连接字符串)
    pub env: Option<String>,
    /// 该数据库下的表结构列表
    pub schemas: Vec<TableSchema>,
}

impl DatabaseSchema {
    /// 创建新的数据库 schema
    pub fn new(name: impl Into<String>, db_type: DatabaseType) -> Self {
        Self { name: name.into(), r#type: db_type, url: None, env: None, schemas: Vec::new() }
    }

    /// 设置数据库连接 URL
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.url = Some(url.into());
        self
    }

    /// 设置环境变量名称
    pub fn env(mut self, env: impl Into<String>) -> Self {
        self.env = Some(env.into());
        self
    }

    /// 添加表结构
    pub fn schema(mut self, schema: TableSchema) -> Self {
        self.schemas.push(schema);
        self
    }

    /// 获取数据库连接字符串
    pub fn get_url(&self) -> Result<String, Box<dyn std::error::Error>> {
        if let Some(url) = &self.url {
            Ok(url.clone())
        }
        else if let Some(env_name) = &self.env {
            std::env::var(env_name).map_err(|e| format!("Failed to read environment variable {}: {}", env_name, e).into())
        }
        else {
            Err("No database URL or environment variable specified".into())
        }
    }
}

/// 表结构定义
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableSchema {
    /// 表名
    pub name: String,
    /// 列定义列表
    pub columns: Vec<ColumnDef>,
    /// 索引定义列表
    pub indexes: Vec<IndexDef>,
    /// 外键约束列表
    pub foreign_keys: Vec<ForeignKeyDef>,
}

impl TableSchema {
    /// 创建新的表结构
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into(), columns: Vec::new(), indexes: Vec::new(), foreign_keys: Vec::new() }
    }

    /// 添加列
    pub fn column(mut self, col: ColumnDef) -> Self {
        self.columns.push(col);
        self
    }

    /// 添加索引
    pub fn index(mut self, idx: IndexDef) -> Self {
        self.indexes.push(idx);
        self
    }

    /// 添加外键约束
    pub fn foreign_key(mut self, fk: ForeignKeyDef) -> Self {
        self.foreign_keys.push(fk);
        self
    }

    /// 生成删表 SQL
    pub fn to_drop_sql(&self) -> String {
        format!("DROP TABLE IF EXISTS {}", self.name)
    }

    /// 获取主键列
    pub fn primary_key(&self) -> Option<&ColumnDef> {
        self.columns.iter().find(|c| c.primary_key)
    }

    /// 根据名称获取列
    pub fn get_column(&self, name: &str) -> Option<&ColumnDef> {
        self.columns.iter().find(|c| c.name == name)
    }
}

/// 外键定义
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForeignKeyDef {
    /// 外键名称
    pub name: String,
    /// 本表列名
    pub column: String,
    /// 引用表名
    pub ref_table: String,
    /// 引用列名
    pub ref_column: String,
    /// 更新行为
    pub on_update: ReferentialAction,
    /// 删除行为
    pub on_delete: ReferentialAction,
}

impl ForeignKeyDef {
    /// 创建新的外键定义
    pub fn new(
        name: impl Into<String>,
        column: impl Into<String>,
        ref_table: impl Into<String>,
        ref_column: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            column: column.into(),
            ref_table: ref_table.into(),
            ref_column: ref_column.into(),
            on_update: ReferentialAction::NoAction,
            on_delete: ReferentialAction::NoAction,
        }
    }

    /// 设置更新时的引用行为
    pub fn on_update(mut self, action: ReferentialAction) -> Self {
        self.on_update = action;
        self
    }

    /// 设置删除时的引用行为
    pub fn on_delete(mut self, action: ReferentialAction) -> Self {
        self.on_delete = action;
        self
    }

    /// 生成约束 SQL 片段
    pub fn to_constraint_sql(&self) -> String {
        format!(
            "CONSTRAINT {} FOREIGN KEY ({}) REFERENCES {} ({}) ON UPDATE {} ON DELETE {}",
            self.name,
            self.column,
            self.ref_table,
            self.ref_column,
            self.on_update.to_sql(),
            self.on_delete.to_sql()
        )
    }

    /// 生成添加外键约束 SQL
    pub fn to_add_sql(&self, table_name: &str) -> String {
        format!("ALTER TABLE {} ADD {}", table_name, self.to_constraint_sql())
    }

    /// 生成删除外键约束 SQL
    pub fn to_drop_sql(&self, table_name: &str) -> String {
        format!("ALTER TABLE {} DROP CONSTRAINT {}", table_name, self.name)
    }
}

/// 外键引用行为
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReferentialAction {
    /// 无操作
    NoAction,
    /// 限制
    Restrict,
    /// 级联
    Cascade,
    /// 设为空
    SetNull,
    /// 设为默认值
    SetDefault,
}

impl ReferentialAction {
    /// 转换为 SQL
    pub fn to_sql(&self) -> &'static str {
        match self {
            ReferentialAction::NoAction => "NO ACTION",
            ReferentialAction::Restrict => "RESTRICT",
            ReferentialAction::Cascade => "CASCADE",
            ReferentialAction::SetNull => "SET NULL",
            ReferentialAction::SetDefault => "SET DEFAULT",
        }
    }
}

/// 完整的 schema 配置(包含多个数据库)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaConfig {
    /// 数据库列表
    pub databases: Vec<DatabaseSchema>,
    /// 默认数据库名称(可选,不指定则使用第一个数据库)
    pub default_database: Option<String>,
}

impl SchemaConfig {
    /// 创建新的 schema 配置
    pub fn new() -> Self {
        Self { databases: Vec::new(), default_database: None }
    }

    /// 添加数据库
    pub fn database(mut self, database: DatabaseSchema) -> Self {
        self.databases.push(database);
        self
    }

    /// 设置默认数据库
    pub fn default_database(mut self, name: impl Into<String>) -> Self {
        self.default_database = Some(name.into());
        self
    }

    /// 获取默认数据库
    pub fn get_default_database(&self) -> Option<&DatabaseSchema> {
        if let Some(default_name) = &self.default_database {
            self.databases.iter().find(|db| db.name == *default_name)
        }
        else {
            self.databases.first()
        }
    }

    /// 根据名称获取数据库
    pub fn get_database(&self, name: &str) -> Option<&DatabaseSchema> {
        self.databases.iter().find(|db| db.name == name)
    }
}

/// 数据库链接配置(保留向后兼容)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseLinkConfig {
    /// 数据库类型
    pub r#type: DatabaseType,
    /// 数据库连接字符串(可以直接包含密码)
    pub url: Option<String>,
    /// 环境变量名称(用于从环境变量获取连接字符串)
    pub env: Option<String>,
}

impl DatabaseLinkConfig {
    /// 获取数据库连接字符串
    pub fn get_url(&self) -> Result<String, Box<dyn std::error::Error>> {
        if let Some(url) = &self.url {
            Ok(url.clone())
        }
        else if let Some(env_name) = &self.env {
            std::env::var(env_name).map_err(|e| format!("Failed to read environment variable {}: {}", env_name, e).into())
        }
        else {
            Err("No database URL or environment variable specified".into())
        }
    }
}

/// 列类型快捷构造函数
pub mod col {
    use super::{ColumnDef, ColumnType};

    /// 创建整数列
    pub fn integer(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Integer)
    }

    /// 创建浮点列
    pub fn real(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Real)
    }

    /// 创建文本列
    pub fn text(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Text)
    }

    /// 创建二进制列
    pub fn blob(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Blob)
    }

    /// 创建主键列 (自增整数)
    pub fn id(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Integer).primary_key().auto_increment()
    }

    /// 创建布尔列
    pub fn boolean(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Integer).default("0")
    }

    /// 创建时间戳列
    pub fn timestamp(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Text)
    }

    /// 创建 JSON 列
    pub fn json(name: &str) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Text)
    }
}

/// 全局 Schema 注册表
static SCHEMA_REGISTRY: Lazy<Mutex<HashMap<String, TableSchema>>> = Lazy::new(|| Mutex::new(HashMap::new()));

/// 注册 TableSchema 到全局注册表
pub fn register_schema(schema: TableSchema) {
    let mut registry = SCHEMA_REGISTRY.lock().unwrap();
    registry.insert(schema.name.clone(), schema);
}

/// 批量注册 TableSchema 到全局注册表
pub fn register_schemas(schemas: Vec<TableSchema>) {
    let mut registry = SCHEMA_REGISTRY.lock().unwrap();
    for schema in schemas {
        registry.insert(schema.name.clone(), schema);
    }
}

/// 获取所有已注册的 TableSchema
pub fn get_registered_schemas() -> Vec<TableSchema> {
    let registry = SCHEMA_REGISTRY.lock().unwrap();
    registry.values().cloned().collect()
}

/// 获取指定名称的 TableSchema
pub fn get_schema(name: &str) -> Option<TableSchema> {
    let registry = SCHEMA_REGISTRY.lock().unwrap();
    registry.get(name).cloned()
}

/// 清空注册表
pub fn clear_schemas() {
    let mut registry = SCHEMA_REGISTRY.lock().unwrap();
    registry.clear();
}

/// 将所有已注册的 TableSchema 导出为 YAML 字符串
pub fn export_schemas_to_yaml() -> String {
    let schemas = get_registered_schemas();
    serde_yaml::to_string(&schemas).unwrap_or_else(|e| format!("# Error: {}", e))
}

/// 将所有已注册的 TableSchema 导出到 YAML 文件
#[cfg(debug_assertions)]
pub fn export_schemas_to_yaml_file(path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
    use std::{fs::File, io::Write};

    let yaml = export_schemas_to_yaml();
    let mut file = File::create(path)?;
    file.write_all(yaml.as_bytes())?;
    Ok(())
}

/// 在 debug 模式下自动导出 schema 到默认路径
#[cfg(debug_assertions)]
pub fn auto_export_schemas() {
    let path = std::path::Path::new("schemas.yaml");
    if let Err(e) = export_schemas_to_yaml_file(path) {
        eprintln!("Warning: Failed to export schemas: {}", e);
    }
    else {
        println!("Schemas exported to: {}", path.display());
    }
}

impl TableSchema {
    /// 注册当前 TableSchema 到全局注册表
    pub fn register(self) -> Self {
        let name = self.name.clone();
        register_schema(self);
        get_schema(&name).unwrap()
    }

    /// 将当前 TableSchema 导出为 YAML 字符串
    pub fn to_yaml(&self) -> String {
        serde_yaml::to_string(self).unwrap_or_else(|e| format!("# Error: {}", e))
    }

    /// 生成建表 SQL(使用当前数据库类型)
    pub fn to_create_sql(&self) -> String {
        self.to_create_sql_for(DatabaseType::current())
    }

    /// 生成指定数据库类型的建表 SQL
    pub fn to_create_sql_for(&self, db_type: DatabaseType) -> String {
        let mut parts: Vec<String> = self.columns.iter().map(|c| c.to_sql_for(db_type)).collect();
        for fk in &self.foreign_keys {
            parts.push(fk.to_constraint_sql());
        }
        match db_type {
            DatabaseType::Turso | DatabaseType::Postgres => {
                format!("CREATE TABLE {} ({})", self.name, parts.join(", "))
            }
            DatabaseType::MySql => {
                format!("CREATE TABLE {} ({}) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", self.name, parts.join(", "))
            }
        }
    }

    /// 生成 MySQL 建表 SQL
    #[cfg(feature = "mysql")]
    pub fn to_mysql_create_sql(&self) -> String {
        self.to_create_sql_for(DatabaseType::MySql)
    }

    /// 生成所有创建索引的 SQL(使用当前数据库类型)
    pub fn to_create_indexes_sql(&self) -> Vec<String> {
        self.to_create_indexes_sql_for(DatabaseType::current())
    }

    /// 生成所有创建索引的 SQL(指定数据库类型)
    pub fn to_create_indexes_sql_for(&self, db_type: DatabaseType) -> Vec<String> {
        self.indexes.iter().map(|idx| idx.to_create_sql_for(db_type)).collect()
    }

    /// 生成完整的建表和索引 SQL(使用当前数据库类型)
    pub fn to_full_create_sql(&self) -> Vec<String> {
        self.to_full_create_sql_for(DatabaseType::current())
    }

    /// 生成完整的建表和索引 SQL(指定数据库类型)
    pub fn to_full_create_sql_for(&self, db_type: DatabaseType) -> Vec<String> {
        let mut sqls = vec![self.to_create_sql_for(db_type)];
        sqls.extend(self.to_create_indexes_sql_for(db_type));
        sqls
    }
}

/// 从 YAML 字符串解析 SchemaConfig
pub fn load_schema_config_from_yaml(yaml_str: &str) -> Result<SchemaConfig, serde_yaml::Error> {
    serde_yaml::from_str(yaml_str)
}

/// 从 YAML 文件加载 SchemaConfig
pub fn load_schema_config_from_yaml_file(path: impl AsRef<Path>) -> Result<SchemaConfig, Box<dyn std::error::Error>> {
    let content = fs::read_to_string(path)?;
    let config = load_schema_config_from_yaml(&content)?;
    Ok(config)
}

/// 从 YAML 字符串解析 TableSchema 列表(兼容旧格式)
pub fn load_schemas_from_yaml(yaml_str: &str) -> Result<Vec<TableSchema>, serde_yaml::Error> {
    serde_yaml::from_str(yaml_str)
}

/// 从 YAML 文件加载 TableSchema 列表(兼容旧格式)
pub fn load_schemas_from_yaml_file(path: impl AsRef<Path>) -> Result<Vec<TableSchema>, Box<dyn std::error::Error>> {
    let content = fs::read_to_string(path)?;
    let schemas = load_schemas_from_yaml(&content)?;
    Ok(schemas)
}

/// 从 YAML 文件加载并注册所有 TableSchema(兼容旧格式)
pub fn load_and_register_schemas_from_yaml_file(path: impl AsRef<Path>) -> Result<(), Box<dyn std::error::Error>> {
    let schemas = load_schemas_from_yaml_file(path)?;
    register_schemas(schemas);
    Ok(())
}

/// 将 SchemaConfig 导出为 YAML 字符串
pub fn export_schema_config_to_yaml(config: &SchemaConfig) -> String {
    serde_yaml::to_string(config).unwrap_or_else(|e| format!("# Error: {}", e))
}

/// 将 SchemaConfig 导出到 YAML 文件
pub fn export_schema_config_to_yaml_file(config: &SchemaConfig, path: impl AsRef<Path>) -> std::io::Result<()> {
    use std::{fs::File, io::Write};

    let yaml = export_schema_config_to_yaml(config);
    let mut file = File::create(path)?;
    file.write_all(yaml.as_bytes())?;
    Ok(())
}

/// 从已注册的 schemas 创建 SchemaConfig(将所有表放在默认数据库中)
pub fn create_schema_config_from_registered(default_db: DatabaseSchema, default_database: Option<String>) -> SchemaConfig {
    let schemas = get_registered_schemas();
    let mut default_db = default_db;
    default_db.schemas = schemas;
    SchemaConfig { databases: vec![default_db], default_database }
}

/// 为所有已注册的 schema 生成完整的 SQL(使用当前数据库类型)
pub fn generate_full_sql_for_registered_schemas() -> Vec<String> {
    generate_full_sql_for_registered_schemas_for(DatabaseType::current())
}

/// 为所有已注册的 schema 生成完整的 SQL(指定数据库类型)
pub fn generate_full_sql_for_registered_schemas_for(db_type: DatabaseType) -> Vec<String> {
    let schemas = get_registered_schemas();
    let mut all_sql = Vec::new();
    for schema in schemas {
        all_sql.extend(schema.to_full_create_sql_for(db_type));
    }
    all_sql
}

/// 导出所有数据库类型的 SQL 到文件
#[cfg(debug_assertions)]
pub fn export_sql_for_all_databases(output_dir: impl AsRef<Path>) -> Result<(), Box<dyn std::error::Error>> {
    let output_dir = output_dir.as_ref();
    fs::create_dir_all(output_dir)?;

    let db_types = [DatabaseType::Turso, DatabaseType::Postgres, DatabaseType::MySql];

    for &db_type in &db_types {
        let sqls = generate_full_sql_for_registered_schemas_for(db_type);
        let db_name = match db_type {
            DatabaseType::Turso => "turso",
            DatabaseType::Postgres => "postgres",
            DatabaseType::MySql => "mysql",
        };
        let file_path = output_dir.join(format!("schema_{}.sql", db_name));
        let content = sqls.join(";\n\n") + ";\n";
        fs::write(&file_path, content)?;
        println!("Exported SQL for {} to: {}", db_name, file_path.display());
    }

    Ok(())
}