dbnexus 0.1.3

An enterprise-grade database abstraction layer for Rust with built-in permission control and connection pooling
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
// Copyright (c) 2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

//! 迁移差异计算和 SQL 生成
//!
//! 计算模式差异并生成迁移 SQL

use super::schema::*;
use super::types::*;
use crate::config::DatabaseType;
use regex::Regex;
use std::sync::LazyLock;

/// 验证 SQL 标识符(表名、列名等)
///
/// # Arguments
///
/// * `identifier` - 要验证的标识符
/// * `identifier_type` - 标识符类型描述(用于错误信息)
///
/// # Returns
///
/// 验证通过返回标识符,失败返回错误
fn validate_sql_identifier(identifier: &str, identifier_type: &str) -> Result<String, String> {
    if identifier.is_empty() {
        return Err(format!("{} 不能为空", identifier_type));
    }

    if identifier.len() > 64 {
        return Err(format!("{} 长度不能超过 64 个字符", identifier_type));
    }

    // 验证标识符格式:只允许字母、数字、下划线,且不能以数字开头
    static IDENTIFIER_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap());

    if !IDENTIFIER_REGEX.is_match(identifier) {
        return Err(format!(
            "{} '{}' 包含无效字符,只允许字母、数字和下划线,且不能以数字开头",
            identifier_type, identifier
        ));
    }

    // 检查保留关键字
    let reserved_keywords = [
        "select",
        "insert",
        "update",
        "delete",
        "drop",
        "create",
        "alter",
        "table",
        "index",
        "from",
        "where",
        "and",
        "or",
        "not",
        "null",
        "primary",
        "key",
        "foreign",
        "references",
        "constraint",
        "default",
        "unique",
        "check",
        "into",
        "values",
        "set",
        "join",
        "left",
        "right",
        "inner",
        "outer",
    ];

    if reserved_keywords.contains(&identifier.to_lowercase().as_str()) {
        return Err(format!(
            "{} '{}' 是 SQL 保留关键字,不允许使用",
            identifier_type, identifier
        ));
    }

    Ok(identifier.to_string())
}

/// 清理默认值,移除可能的 SQL 注入载荷
fn sanitize_default_value(default: &str) -> String {
    // 如果默认值包含可疑模式,返回安全的默认值
    let suspicious_patterns = [
        "select",
        "insert",
        "update",
        "delete",
        "drop",
        "create",
        "alter",
        "exec",
        "execute",
        "xp_",
        "sp_",
        "--",
        "/*",
        "*/",
        "chr(",
        "char(",
        "concat",
        "union",
        "benchmark",
        "sleep",
    ];

    let upper_default = default.to_uppercase();

    for pattern in &suspicious_patterns {
        if upper_default.contains(pattern) {
            tracing::warn!(
                "Suspicious pattern detected in default value: '{}', sanitizing",
                default
            );
            return "'***SANITIZED***'".to_string();
        }
    }

    // 确保默认值被引号包围
    let trimmed = default.trim();

    if (trimmed.starts_with('\'') && trimmed.ends_with('\''))
        || (trimmed.starts_with('(') && trimmed.ends_with(')'))
        || trimmed.parse::<i128>().is_ok()
        || trimmed.parse::<f64>().is_ok()
        || trimmed.to_uppercase() == "NULL"
        || trimmed.to_uppercase() == "CURRENT_TIMESTAMP"
        || trimmed.to_uppercase() == "NOW()"
    {
        // 已经有引号、是函数、数字、NULL 或时间戳,不需要额外引号
        trimmed.to_string()
    } else {
        // 其他情况添加单引号
        format!("'{}'", trimmed.replace('\'', "''"))
    }
}

/// 迁移计划
pub struct MigrationPlan {
    /// 待执行的迁移列表
    pub migrations: Vec<Migration>,
    /// 执行方向(向上或向下)
    pub direction: MigrationDirection,
}

/// 迁移方向
#[derive(Debug, Clone)]
pub enum MigrationDirection {
    /// 向上迁移(应用新版本)
    Up,
    /// 向下迁移(回滚版本)
    Down,
}

/// 迁移工具 CLI 命令
#[derive(Debug, Clone)]
pub enum MigrationCommand {
    /// 创建新的迁移文件
    Create {
        /// 迁移描述
        description: String,
        /// 目录路径
        directory: String,
    },
    /// 应用迁移
    Up {
        /// 目标版本号,None 表示应用所有迁移
        target_version: Option<u32>,
    },
    /// 回滚迁移
    Down {
        /// 目标版本号,None 表示回滚到初始状态
        target_version: Option<u32>,
    },
    /// 查看迁移状态
    Status,
    /// 生成迁移文件
    Generate {
        /// 从模式生成迁移
        from_schema: String,
        /// 到模式
        to_schema: String,
        /// 输出文件
        output_file: String,
    },
}

/// Schema 差异计算器
pub struct SchemaDiffer {
    /// 源 Schema
    old_schema: Schema,
    /// 目标 Schema
    new_schema: Schema,
}

impl SchemaDiffer {
    /// 创建新的 SchemaDiffer
    pub fn new(old_schema: Schema, new_schema: Schema) -> Self {
        Self { old_schema, new_schema }
    }

    /// 计算差异并生成 Migration
    pub fn diff(&self) -> Vec<Migration> {
        let mut migrations = Vec::new();
        let mut migration = Migration::new(1, "Schema changes".to_string());

        // 检测新增的表
        for new_table in &self.new_schema.tables {
            if !self.old_schema.has_table(&new_table.name) {
                migration.add_table_change(TableChange::CreateTable(new_table.clone()));
            }
        }

        // 检测删除的表
        for old_table in &self.old_schema.tables {
            if !self.new_schema.has_table(&old_table.name) {
                migration.add_table_change(TableChange::DropTable {
                    table_name: old_table.name.clone(),
                });
            }
        }

        // 检测修改的表
        for new_table in &self.new_schema.tables {
            if let Some(old_table) = self.old_schema.get_table(&new_table.name) {
                // 检测列变更
                let column_changes = self.detect_column_changes(old_table, new_table);
                let added_columns = self.detect_added_columns(old_table, new_table);
                let removed_columns = self.detect_removed_columns(old_table, new_table);
                let added_indexes = self.detect_added_indexes(old_table, new_table);
                let removed_indexes = self.detect_removed_indexes(old_table, new_table);
                let added_foreign_keys = self.detect_added_foreign_keys(old_table, new_table);
                let removed_foreign_keys = self.detect_removed_foreign_keys(old_table, new_table);

                if !column_changes.is_empty()
                    || !added_columns.is_empty()
                    || !removed_columns.is_empty()
                    || !added_indexes.is_empty()
                    || !removed_indexes.is_empty()
                    || !added_foreign_keys.is_empty()
                    || !removed_foreign_keys.is_empty()
                {
                    migration.add_table_change(TableChange::AlterTable {
                        table_name: new_table.name.clone(),
                        column_changes,
                        added_columns,
                        removed_columns,
                        added_indexes,
                        removed_indexes,
                        added_foreign_keys,
                        removed_foreign_keys,
                    });
                }
            }
        }

        if !migration.table_changes.is_empty() {
            migrations.push(migration);
        }

        migrations
    }

    /// 检测列变更
    fn detect_column_changes(&self, old_table: &Table, new_table: &Table) -> Vec<ColumnAlteration> {
        let mut changes = Vec::new();

        for new_column in &new_table.columns {
            if let Some(old_column) = old_table.columns.iter().find(|c| c.name == new_column.name) {
                // 检测类型变更
                if old_column.column_type != new_column.column_type {
                    changes.push(ColumnAlteration::TypeChanged {
                        column_name: new_column.name.clone(),
                        old_type: old_column.column_type.clone(),
                        new_type: new_column.column_type.clone(),
                    });
                }

                // 检测可空性变更
                if old_column.is_nullable != new_column.is_nullable {
                    changes.push(ColumnAlteration::NullabilityChanged {
                        column_name: new_column.name.clone(),
                        old_nullable: old_column.is_nullable,
                        new_nullable: new_column.is_nullable,
                    });
                }

                // 检测默认值变更
                if old_column.default_value != new_column.default_value {
                    changes.push(ColumnAlteration::DefaultChanged {
                        column_name: new_column.name.clone(),
                        old_default: old_column.default_value.clone(),
                        new_default: new_column.default_value.clone(),
                    });
                }
            }
        }

        changes
    }

    /// 检测新增的列
    fn detect_added_columns(&self, old_table: &Table, new_table: &Table) -> Vec<Column> {
        new_table
            .columns
            .iter()
            .filter(|c| !old_table.columns.iter().any(|oc| oc.name == c.name))
            .cloned()
            .collect()
    }

    /// 检测删除的列
    fn detect_removed_columns(&self, old_table: &Table, new_table: &Table) -> Vec<String> {
        old_table
            .columns
            .iter()
            .filter(|c| !new_table.columns.iter().any(|nc| nc.name == c.name))
            .map(|c| c.name.clone())
            .collect()
    }

    /// 检测新增的索引
    fn detect_added_indexes(&self, old_table: &Table, new_table: &Table) -> Vec<Index> {
        new_table
            .indexes
            .iter()
            .filter(|i| !old_table.indexes.iter().any(|oi| oi.name == i.name))
            .cloned()
            .collect()
    }

    /// 检测删除的索引
    fn detect_removed_indexes(&self, old_table: &Table, new_table: &Table) -> Vec<String> {
        old_table
            .indexes
            .iter()
            .filter(|i| !new_table.indexes.iter().any(|ni| ni.name == i.name))
            .map(|i| i.name.clone())
            .collect()
    }

    /// 检测新增的外键
    fn detect_added_foreign_keys(&self, old_table: &Table, new_table: &Table) -> Vec<ForeignKey> {
        new_table
            .foreign_keys
            .iter()
            .filter(|fk| !old_table.foreign_keys.iter().any(|ofk| ofk.name == fk.name))
            .cloned()
            .collect()
    }

    /// 检测删除的外键
    fn detect_removed_foreign_keys(&self, old_table: &Table, new_table: &Table) -> Vec<String> {
        old_table
            .foreign_keys
            .iter()
            .filter(|fk| !new_table.foreign_keys.iter().any(|nfk| nfk.name == fk.name))
            .map(|fk| fk.name.clone())
            .collect()
    }
}

/// SQL 生成器
#[derive(Debug, Clone)]
pub struct SqlGenerator {
    /// 数据库类型
    pub db_type: DatabaseType,
}

impl SqlGenerator {
    /// 创建新的 SQLGenerator
    pub fn new(db_type: DatabaseType) -> Self {
        Self { db_type }
    }

    /// 生成列定义的 SQL(仅类型部分,用于测试)
    pub fn generate_column_def(&self, column_type: &ColumnType) -> String {
        column_type.to_sql(self.db_type)
    }

    /// 生成创建表的 SQL
    pub fn generate_create_table_sql(&self, table: &Table) -> String {
        // 验证表名
        let table_name = match validate_sql_identifier(&table.name, "表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid table name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let mut sql = format!("CREATE TABLE {} (\n", table_name);

        let column_defs: Vec<String> = table
            .columns
            .iter()
            .map(|col| self.generate_column_definition(col, &table.primary_key_columns))
            .collect();

        sql.push_str(&column_defs.join(",\n"));

        // 添加主键约束(已验证的列名)
        if !table.primary_key_columns.is_empty() {
            sql.push_str(",\n");
            let pk_columns: Vec<String> = table
                .primary_key_columns
                .iter()
                .map(|col| match validate_sql_identifier(col, "主键列名") {
                    Ok(validated) => validated,
                    Err(e) => {
                        tracing::error!("Invalid primary key column: {}", e);
                        "***INVALID***".to_string()
                    }
                })
                .collect();
            sql.push_str(&format!("    PRIMARY KEY ({})", pk_columns.join(", ")));
        }

        sql.push_str("\n);");

        // 生成索引
        for index in &table.indexes {
            if !index.is_constraint {
                sql.push_str("\n\n");
                sql.push_str(&self.generate_create_index_sql(index));
            }
        }

        // 生成外键
        for fk in &table.foreign_keys {
            sql.push_str("\n\n");
            sql.push_str(&self.generate_add_foreign_key_sql(fk));
        }

        sql
    }

    /// 生成列定义
    fn generate_column_definition(&self, column: &Column, _pk_columns: &[String]) -> String {
        // 验证列名
        let column_name = match validate_sql_identifier(&column.name, "列名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid column name: {}", e);
                return format!("    -- 错误: {}\n", e);
            }
        };

        let mut def = format!("    {} {}", column_name, column.column_type.to_sql(self.db_type));

        // 自增列不需要指定
        if column.is_auto_increment && column.is_primary_key {
            match self.db_type {
                DatabaseType::MySql => def.push_str(" AUTO_INCREMENT"),
                DatabaseType::Sqlite => def.push_str(" PRIMARY KEY AUTOINCREMENT"),
                _ => {}
            }
        }

        if !column.is_nullable {
            def.push_str(" NOT NULL");
        }

        if let Some(default) = &column.default_value {
            let sanitized_default = sanitize_default_value(default);
            def.push_str(&format!(" DEFAULT {}", sanitized_default));
        }

        // 主键列如果有自增,不需要单独 PRIMARY KEY
        if column.is_primary_key && !column.is_auto_increment {
            // 主键已在表级别处理
        }

        def
    }

    /// 生成创建索引的 SQL
    pub fn generate_create_index_sql(&self, index: &Index) -> String {
        // 验证索引名
        let index_name = match validate_sql_identifier(&index.name, "索引名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid index name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        // 验证表名
        let table_name = match validate_sql_identifier(&index.table_name, "表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid table name in index: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        // 验证列名
        let validated_columns: Vec<String> = index
            .columns
            .iter()
            .map(|col| match validate_sql_identifier(col, "索引列名") {
                Ok(name) => name,
                Err(e) => {
                    tracing::error!("Invalid index column: {}", e);
                    "***INVALID***".to_string()
                }
            })
            .collect();

        let unique = if index.is_unique { "UNIQUE " } else { "" };
        format!(
            "CREATE {}INDEX {} ON {} ({})",
            unique,
            index_name,
            table_name,
            validated_columns.join(", ")
        )
    }

    /// 生成添加外键的 SQL
    fn generate_add_foreign_key_sql(&self, fk: &ForeignKey) -> String {
        // 验证所有标识符
        let table_name = match validate_sql_identifier(&fk.table_name, "外键表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid foreign key table name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let constraint_name = match validate_sql_identifier(&fk.name, "外键约束名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid foreign key constraint name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let column_name = match validate_sql_identifier(&fk.column_name, "外键列名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid foreign key column: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let referenced_table_name = match validate_sql_identifier(&fk.referenced_table_name, "外键引用表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid referenced table name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let referenced_column_name = match validate_sql_identifier(&fk.referenced_column_name, "外键引用列名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid referenced column: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let mut sql = format!(
            "ALTER TABLE {} ADD CONSTRAINT {} FOREIGN KEY ({}) REFERENCES {}({})",
            table_name, constraint_name, column_name, referenced_table_name, referenced_column_name
        );

        if let Some(on_delete) = &fk.on_delete {
            sql.push_str(&format!(" ON DELETE {}", on_delete));
        }

        if let Some(on_update) = &fk.on_update {
            sql.push_str(&format!(" ON UPDATE {}", on_update));
        }

        sql.push(';');
        sql
    }

    /// 生成删除表的 SQL
    pub fn generate_drop_table_sql(&self, table_name: &str) -> String {
        let validated_name = match validate_sql_identifier(table_name, "表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid table name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };
        format!("DROP TABLE {};", validated_name)
    }

    /// 生成添加列的 SQL
    pub fn generate_add_column_sql(&self, table_name: &str, column: &Column) -> String {
        // 验证表名
        let validated_table_name = match validate_sql_identifier(table_name, "表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid table name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let col_def = self.generate_column_definition(column, &Vec::new());
        format!(
            "ALTER TABLE {} ADD {};",
            validated_table_name,
            col_def.trim_start_matches("    ")
        )
    }

    /// 生成删除列的 SQL
    pub fn generate_drop_column_sql(&self, table_name: &str, column_name: &str) -> String {
        // 验证表名和列名
        let validated_table_name = match validate_sql_identifier(table_name, "表名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid table name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        let validated_column_name = match validate_sql_identifier(column_name, "列名") {
            Ok(name) => name,
            Err(e) => {
                tracing::error!("Invalid column name: {}", e);
                return format!("-- 错误: {}\n", e);
            }
        };

        match self.db_type {
            DatabaseType::MySql => {
                format!(
                    "ALTER TABLE {} DROP COLUMN {};",
                    validated_table_name, validated_column_name
                )
            }
            DatabaseType::Postgres => {
                format!(
                    "ALTER TABLE {} DROP COLUMN {};",
                    validated_table_name, validated_column_name
                )
            }
            DatabaseType::Sqlite => {
                // SQLite 不支持直接删除列,需要重建表
                format!(
                    "-- SQLite 不支持直接删除列,请手动重建表 {}
 ALTER TABLE {} DROP COLUMN {};",
                    validated_table_name, validated_table_name, validated_column_name
                )
            }
        }
    }

    /// 生成迁移的完整 SQL
    pub fn generate_migration_sql(&self, migration: &Migration) -> String {
        let mut sql = String::new();

        for change in &migration.table_changes {
            match change {
                TableChange::CreateTable(table) => {
                    sql.push_str(&format!("-- 创建表: {}\n", table.name));
                    sql.push_str(&self.generate_create_table_sql(table));
                    sql.push_str("\n\n");
                }
                TableChange::DropTable { table_name } => {
                    sql.push_str(&format!("-- 删除表: {}\n", table_name));
                    sql.push_str(&self.generate_drop_table_sql(table_name));
                    sql.push_str("\n\n");
                }
                TableChange::AlterTable {
                    table_name,
                    added_columns,
                    removed_columns,
                    added_indexes,
                    removed_indexes,
                    added_foreign_keys,
                    removed_foreign_keys,
                    ..
                } => {
                    sql.push_str(&format!("-- 修改表: {}\n", table_name));

                    for col in added_columns {
                        sql.push_str(&format!("-- 添加列: {}\n", col.name));
                        sql.push_str(&self.generate_add_column_sql(table_name, col));
                        sql.push('\n');
                    }

                    for col_name in removed_columns {
                        sql.push_str(&format!("-- 删除列: {}\n", col_name));
                        sql.push_str(&self.generate_drop_column_sql(table_name, col_name));
                        sql.push('\n');
                    }

                    for index in added_indexes {
                        sql.push_str(&format!("-- 添加索引: {}\n", index.name));
                        sql.push_str(&self.generate_create_index_sql(index));
                        sql.push('\n');
                    }

                    for index_name in removed_indexes {
                        sql.push_str(&format!("-- 删除索引: {}\n", index_name));
                        sql.push_str(&format!("DROP INDEX {};\n", index_name));
                    }

                    for fk in added_foreign_keys {
                        sql.push_str(&format!("-- 添加外键: {}\n", fk.name));
                        sql.push_str(&self.generate_add_foreign_key_sql(fk));
                        sql.push('\n');
                    }

                    for fk_name in removed_foreign_keys {
                        sql.push_str(&format!("-- 删除外键: {}\n", fk_name));
                        sql.push_str(&format!("ALTER TABLE {} DROP CONSTRAINT {};\n", table_name, fk_name));
                    }

                    sql.push('\n');
                }
            }
        }

        sql.trim_end().to_string()
    }
}

/// Rust 结构体解析器
///
/// 从 Rust 实体结构体定义中解析数据库表结构,
/// 支持从 `#[sea_orm(...)]` 属性中提取列信息
#[derive(Debug, Clone)]
/// Rust 实体解析器
///
/// 用于解析 Rust 实体定义源代码,提取数据库表结构信息
pub(crate) struct RustEntityParser;

impl RustEntityParser {
    /// 解析 Rust 实体定义
    ///
    /// 通过解析 Rust 源代码,提取实体结构体中的字段信息,
    /// 并转换为数据库表结构。
    ///
    /// # Arguments
    ///
    /// * `entity_code` - Rust 实体结构体源代码
    /// * `table_name` - 目标数据库表名
    ///
    /// # Returns
    ///
    /// 解析后的表结构定义
    pub fn parse_entity(entity_code: &str, table_name: &str) -> Result<Table, String> {
        // 简化实现:解析 sea-orm 属性
        // 实际实现需要完整的 Rust 解析器 (syn/quote)
        let columns = Self::extract_columns_from_code(entity_code)?;

        let primary_key_columns = columns
            .iter()
            .filter(|c| c.is_primary_key)
            .map(|c| c.name.clone())
            .collect();

        Ok(Table {
            name: table_name.to_string(),
            columns,
            primary_key_columns,
            indexes: Vec::new(),
            foreign_keys: Vec::new(),
            comment: None,
        })
    }

    /// 从代码中提取列信息
    fn extract_columns_from_code(entity_code: &str) -> Result<Vec<Column>, String> {
        let mut columns = Vec::new();

        // 解析属性和字段
        let lines: Vec<&str> = entity_code.lines().collect();

        let mut current_field_name: Option<String> = None;
        let mut current_field_type: Option<String> = None;
        let mut current_column_type: Option<ColumnType> = None;
        let mut field_column_type: Option<ColumnType> = None; // 当前字段开始前设置的 column_type
        let mut is_primary_key = false;
        let mut is_nullable = true;
        let mut is_auto_increment = false;

        for line in &lines {
            let line = line.trim();

            // 提取字段名和类型
            if let Some((field_name, field_type)) = Self::extract_field_and_type(line) {
                // 保存之前的字段(如果存在且有类型)
                if let Some(ref prev_field_name) = current_field_name {
                    let col_type = field_column_type
                        .take()
                        .or_else(|| Self::infer_column_type(&current_field_type));

                    if let Some(type_result) = col_type {
                        if !columns.iter().any(|c: &Column| c.name == *prev_field_name) {
                            columns.push(Column {
                                name: prev_field_name.clone(),
                                column_type: type_result,
                                is_primary_key,
                                is_nullable,
                                has_default: false,
                                default_value: None,
                                is_auto_increment,
                                comment: None,
                            });
                        }
                    }

                    // 保存完后重置属性,为新字段做准备
                    is_primary_key = false;
                    is_nullable = true;
                    is_auto_increment = false;
                }

                // 将当前属性行的 column_type 移到 field_column_type
                field_column_type = current_column_type.take();

                // 设置新字段
                current_field_name = Some(field_name);
                current_field_type = Some(field_type);
                continue;
            }

            // 提取列类型
            if line.contains("column_type") {
                current_column_type = Self::extract_column_type(line);
            }

            // 检测主键
            if line.contains("primary_key") {
                is_primary_key = true;
            }

            // 检测可空性
            if line.contains("NotNull") || line.contains("not_null") {
                is_nullable = false;
            }

            // 检测自增
            if line.contains("AutoIncrement") || line.contains("auto_increment") {
                is_auto_increment = true;
            }

            // 如果遇到新属性行,跳过
            if line.starts_with("#[") {
                continue;
            }
        }

        // 处理最后一个字段
        if let Some(ref field_name) = current_field_name {
            // 使用当前字段开始前设置的 column_type
            let col_type = field_column_type
                .take()
                .or_else(|| Self::infer_column_type(&current_field_type));

            if let Some(type_result) = col_type {
                columns.push(Column {
                    name: field_name.clone(),
                    column_type: type_result,
                    is_primary_key,
                    is_nullable,
                    has_default: false,
                    default_value: None,
                    is_auto_increment,
                    comment: None,
                });
            }
        }

        if columns.is_empty() {
            return Err("未能解析到任何列".to_string());
        }

        Ok(columns)
    }

    /// 从字段行提取字段名和类型
    fn extract_field_and_type(line: &str) -> Option<(String, String)> {
        let trimmed = line.trim();

        // 跳过属性行
        if trimmed.starts_with("#[") {
            return None;
        }

        // 跳过结构体定义行: pub struct Xxx {
        if trimmed.starts_with("pub struct ") || trimmed.starts_with("struct ") {
            return None;
        }

        // 匹配模式: pub name: String, 或 name: String,
        // 必须有冒号才是字段定义
        let colon_idx = trimmed.find(':')?;
        let before_colon = &trimmed[..colon_idx];
        let after_colon = &trimmed[colon_idx + 1..];

        // 清理字段名 - 去除 pub 前缀
        let mut field_name = before_colon.trim_end().trim_end_matches(',').trim().to_string();
        if field_name.starts_with("pub ") {
            field_name = field_name[4..].to_string();
        }

        // 跳过非字段行
        if field_name.starts_with("#[") || field_name.starts_with("fn ") || field_name.is_empty() {
            return None;
        }

        // 提取类型(直到逗号或右花括号)
        let mut type_str = after_colon.trim();
        let type_end = type_str
            .find(',')
            .or_else(|| type_str.find('}'))
            .unwrap_or(type_str.len());
        type_str = &type_str[..type_end];

        if field_name.is_empty() || type_str.is_empty() {
            return None;
        }

        Some((field_name.to_string(), type_str.to_string()))
    }

    /// 从属性行提取列类型
    fn extract_column_type(attr_line: &str) -> Option<ColumnType> {
        // 匹配: #[sea_orm(column_type = "String(255)")]
        // 或: #[sea_orm(column_type = "Text")]
        if let Some(start) = attr_line.find("column_type") {
            let after = &attr_line[start..];
            if let Some(eq_idx) = after.find('=') {
                let type_str = &after[eq_idx + 1..];
                // 提取引号内的内容
                if let Some(quote_start) = type_str.find('"') {
                    if let Some(quote_end) = type_str[quote_start + 1..].find('"') {
                        let type_content = &type_str[quote_start + 1..quote_start + 1 + quote_end];
                        return Some(Self::parse_column_type_str(type_content));
                    }
                }
            }
        }
        None
    }

    /// 解析列类型字符串
    fn parse_column_type_str(type_str: &str) -> ColumnType {
        match type_str {
            "Integer" | "Int" | "i32" => ColumnType::Integer,
            "BigInteger" | "BigInt" => ColumnType::BigInteger,
            "String" => ColumnType::String(Some(255)),
            s if s.starts_with("String(") => {
                if let Some(len_str) = s.strip_prefix("String(").and_then(|s| s.strip_suffix(')')) {
                    if let Ok(len) = len_str.parse() {
                        return ColumnType::String(Some(len));
                    }
                }
                ColumnType::String(Some(255))
            }
            "Text" => ColumnType::Text,
            "Boolean" | "Bool" | "bool" => ColumnType::Boolean,
            "Float" | "f32" => ColumnType::Float,
            "Double" | "f64" => ColumnType::Double,
            "Date" => ColumnType::Date,
            "Time" => ColumnType::Time,
            "DateTime" | "DateTimeUtc" => ColumnType::DateTime,
            "Timestamp" | "TimestampUtc" => ColumnType::Timestamp,
            "Json" | "JsonValue" => ColumnType::Json,
            "Binary" | "Vec<u8>" => ColumnType::Binary,
            _ => ColumnType::Custom(type_str.to_string()),
        }
    }

    /// 从 Rust 类型推断列类型
    fn infer_column_type(field_type: &Option<String>) -> Option<ColumnType> {
        let type_str = field_type.as_ref()?.to_lowercase();

        // 处理 Option<T>
        let inner_type = if type_str.starts_with("option<") {
            if let Some(end) = type_str.find('>') {
                &type_str[7..end]
            } else {
                &type_str
            }
        } else {
            &type_str
        };

        // 映射 Rust 类型到 ColumnType
        match inner_type {
            t if t.contains("i32") || t == "integer" || t == "int" => Some(ColumnType::Integer),
            t if t.contains("i64") || t == "biginteger" || t == "bigint" => Some(ColumnType::BigInteger),
            t if t.contains("string") || t.contains("&str") => {
                // 检查是否有长度指定
                if let Some(len_start) = t.find('<') {
                    if let Some(len_end) = t[len_start..].find('>') {
                        let len_str = &t[len_start + 1..len_start + len_end];
                        if let Ok(len) = len_str.parse() {
                            return Some(ColumnType::String(Some(len)));
                        }
                    }
                }
                Some(ColumnType::String(Some(255)))
            }
            t if t.contains("text") || t.contains("string") => Some(ColumnType::Text),
            t if t.contains("bool") => Some(ColumnType::Boolean),
            t if t.contains("f32") | t.contains("float") => Some(ColumnType::Float),
            t if t.contains("f64") | t.contains("double") => Some(ColumnType::Double),
            t if t.contains("date") && t.contains("time") => Some(ColumnType::DateTime),
            t if t.contains("date") => Some(ColumnType::Date),
            t if t.contains("time") => Some(ColumnType::Time),
            t if t.contains("timestamp") => Some(ColumnType::Timestamp),
            t if t.contains("json") => Some(ColumnType::Json),
            t if t.contains("vec<u8>") || t.contains("binary") => Some(ColumnType::Binary),
            _ => None,
        }
    }

    /// 生成从实体到表的迁移
    ///
    /// # Arguments
    ///
    /// * `entity_code` - Rust 实体结构体源代码
    /// * `table_name` - 目标数据库表名
    /// * `db_type` - 目标数据库类型
    ///
    /// # Returns
    ///
    /// 创建表的 SQL 语句
    pub fn generate_migration_sql(
        entity_code: &str,
        table_name: &str,
        db_type: DatabaseType,
    ) -> Result<String, String> {
        let table = Self::parse_entity(entity_code, table_name)?;
        let generator = SqlGenerator::new(db_type);
        Ok(generator.generate_create_table_sql(&table))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// TEST-U-020: ColumnType SQL 生成测试
    #[test]
    fn test_column_type_to_sql() {
        let pg = SqlGenerator::new(DatabaseType::Postgres);
        let mysql = SqlGenerator::new(DatabaseType::MySql);
        let sqlite = SqlGenerator::new(DatabaseType::Sqlite);

        // Integer
        assert_eq!(pg.generate_column_def(&ColumnType::Integer), "INTEGER");
        assert_eq!(mysql.generate_column_def(&ColumnType::Integer), "INTEGER");
        assert_eq!(sqlite.generate_column_def(&ColumnType::Integer), "INTEGER");

        // Boolean
        assert_eq!(pg.generate_column_def(&ColumnType::Boolean), "BOOLEAN");
        assert_eq!(mysql.generate_column_def(&ColumnType::Boolean), "BOOLEAN");
        assert_eq!(sqlite.generate_column_def(&ColumnType::Boolean), "INTEGER");
    }

    /// TEST-U-021: Schema 差异检测测试
    #[test]
    fn test_schema_diff_new_table() {
        let old_schema = Schema::new(DatabaseType::Postgres);
        let mut new_schema = Schema::new(DatabaseType::Postgres);

        // old: no tables
        // new: has users table
        let users_table = Table {
            name: "users".to_string(),
            columns: vec![Column {
                name: "id".to_string(),
                column_type: ColumnType::Integer,
                is_primary_key: true,
                is_nullable: false,
                has_default: false,
                default_value: None,
                is_auto_increment: true,
                comment: None,
            }],
            primary_key_columns: vec!["id".to_string()],
            indexes: vec![],
            foreign_keys: vec![],
            comment: None,
        };
        new_schema.add_table(users_table);

        let differ = SchemaDiffer::new(old_schema, new_schema);
        let migrations = differ.diff();

        assert_eq!(migrations.len(), 1);
        assert_eq!(migrations[0].table_changes.len(), 1);

        if let TableChange::CreateTable(table) = &migrations[0].table_changes[0] {
            assert_eq!(table.name, "users");
        } else {
            unreachable!("Expected CreateTable change");
        }
    }

    /// TEST-U-022: Schema 差异检测 - 删除表
    #[test]
    fn test_schema_diff_drop_table() {
        let mut old_schema = Schema::new(DatabaseType::Postgres);
        let new_schema = Schema::new(DatabaseType::Postgres);

        let users_table = Table {
            name: "users".to_string(),
            columns: vec![],
            primary_key_columns: vec![],
            indexes: vec![],
            foreign_keys: vec![],
            comment: None,
        };
        old_schema.add_table(users_table);
        // new_schema is empty

        let differ = SchemaDiffer::new(old_schema, new_schema);
        let migrations = differ.diff();

        assert_eq!(migrations.len(), 1);
        assert_eq!(migrations[0].table_changes.len(), 1);

        if let TableChange::DropTable { table_name } = &migrations[0].table_changes[0] {
            assert_eq!(table_name, "users");
        } else {
            unreachable!("Expected DropTable change");
        }
    }

    /// TEST-U-023: SQL 生成测试
    #[test]
    fn test_sql_generation() {
        let pg = SqlGenerator::new(DatabaseType::Postgres);

        let table = Table {
            name: "users".to_string(),
            columns: vec![
                Column {
                    name: "id".to_string(),
                    column_type: ColumnType::Integer,
                    is_primary_key: true,
                    is_nullable: false,
                    has_default: false,
                    default_value: None,
                    is_auto_increment: true,
                    comment: None,
                },
                Column {
                    name: "name".to_string(),
                    column_type: ColumnType::String(Some(255)),
                    is_primary_key: false,
                    is_nullable: false,
                    has_default: false,
                    default_value: None,
                    is_auto_increment: false,
                    comment: None,
                },
            ],
            primary_key_columns: vec!["id".to_string()],
            indexes: vec![],
            foreign_keys: vec![],
            comment: None,
        };

        let sql = pg.generate_create_table_sql(&table);

        assert!(sql.contains("CREATE TABLE users"));
        assert!(sql.contains("id INTEGER"));
        assert!(sql.contains("name VARCHAR(255)"));
        assert!(sql.contains("NOT NULL"));
        assert!(sql.contains("PRIMARY KEY (id)"));
    }

    /// TEST-U-024: Rust 实体解析测试 - 基础解析
    #[test]
    fn test_rust_entity_parser_basic() {
        let entity_code = r#"
#[sea_orm(table_name = "users")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(column_type = "String(255)")]
    pub name: String,
    #[sea_orm(column_type = "Text")]
    pub bio: Option<String>,
}
"#;

        let table = RustEntityParser::parse_entity(entity_code, "users").expect("Failed to parse entity code");

        assert_eq!(table.name, "users");
        assert_eq!(table.columns.len(), 3);
        assert_eq!(table.primary_key_columns, vec!["id"]);

        // 检查 id 列
        let id_col = table
            .columns
            .iter()
            .find(|c| c.name == "id")
            .expect("id column should exist");
        assert_eq!(id_col.column_type, ColumnType::Integer);
        assert!(id_col.is_primary_key);

        // 检查 name 列
        let name_col = table
            .columns
            .iter()
            .find(|c| c.name == "name")
            .expect("name column should exist");
        assert_eq!(name_col.column_type, ColumnType::String(Some(255)));
    }

    /// TEST-U-025: Rust 实体解析测试 - 生成迁移 SQL
    #[test]
    fn test_rust_entity_generate_migration() {
        let entity_code = r#"
#[sea_orm(table_name = "posts")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    #[sea_orm(column_type = "String(255)")]
    pub title: String,
    #[sea_orm(column_type = "Text")]
    pub content: String,
    #[sea_orm(column_type = "DateTime")]
    pub created_at: DateTimeUtc,
}
"#;

        let sql = RustEntityParser::generate_migration_sql(entity_code, "posts", DatabaseType::Postgres)
            .expect("Failed to generate migration SQL");

        assert!(sql.contains("CREATE TABLE posts"));
        assert!(sql.contains("id BIGINT"));
        assert!(sql.contains("title VARCHAR(255)"));
        assert!(sql.contains("content TEXT"));
        assert!(sql.contains("created_at TIMESTAMP"));
    }

    /// TEST-U-026: Rust 实体解析测试 - 列类型解析
    #[test]
    fn test_parse_column_type_string() {
        assert_eq!(RustEntityParser::parse_column_type_str("Integer"), ColumnType::Integer);
        assert_eq!(
            RustEntityParser::parse_column_type_str("String(100)"),
            ColumnType::String(Some(100))
        );
        assert_eq!(RustEntityParser::parse_column_type_str("Text"), ColumnType::Text);
        assert_eq!(RustEntityParser::parse_column_type_str("Boolean"), ColumnType::Boolean);
        assert_eq!(
            RustEntityParser::parse_column_type_str("DateTime"),
            ColumnType::DateTime
        );
        assert_eq!(RustEntityParser::parse_column_type_str("Json"), ColumnType::Json);
    }
}