Skip to main content

sqltool/databases/
compatibility.rs

1use crate::models::Field;
2use anyhow::Result;
3
4/// 数据库版本信息
5#[derive(Debug, Clone)]
6pub struct DatabaseVersion {
7    pub major: u32,
8    pub minor: u32,
9    pub patch: u32,
10}
11
12impl DatabaseVersion {
13    pub fn new(major: u32, minor: u32, patch: u32) -> Self {
14        Self { major, minor, patch }
15    }
16
17    /// 解析版本字符串
18    pub fn parse(version_str: &str) -> Result<Self> {
19        let parts: Vec<&str> = version_str.split('.').collect();
20        if parts.len() < 1 {
21            return Err(anyhow::anyhow!("Invalid version string: {}", version_str));
22        }
23
24        let major = parts[0].parse()?;
25        let minor = if parts.len() > 1 { parts[1].parse()? } else { 0 };
26        let patch = if parts.len() > 2 { parts[2].parse()? } else { 0 };
27
28        Ok(Self::new(major, minor, patch))
29    }
30
31    /// 比较版本
32    pub fn compare(&self, other: &Self) -> std::cmp::Ordering {
33        if self.major != other.major {
34            self.major.cmp(&other.major)
35        } else if self.minor != other.minor {
36            self.minor.cmp(&other.minor)
37        } else {
38            self.patch.cmp(&other.patch)
39        }
40    }
41
42    /// 是否大于等于另一个版本
43    pub fn is_gte(&self, other: &Self) -> bool {
44        self.compare(other) != std::cmp::Ordering::Less
45    }
46
47    /// 是否小于另一个版本
48    pub fn is_lt(&self, other: &Self) -> bool {
49        self.compare(other) == std::cmp::Ordering::Less
50    }
51}
52
53/// 数据库兼容性处理
54pub trait DatabaseCompatibility {
55    /// 获取数据库版本
56    fn get_version(&self) -> Result<DatabaseVersion>;
57
58    /// 转换字段类型以兼容目标数据库版本
59    fn convert_field_type(&self, field: &Field, target_version: &DatabaseVersion) -> Result<Field>;
60
61    /// 生成兼容的SQL语句
62    fn generate_compatible_sql(&self, sql: &str, target_version: &DatabaseVersion) -> Result<String>;
63}
64
65/// MySQL兼容性处理
66pub struct MySqlCompatibility;
67
68impl DatabaseCompatibility for MySqlCompatibility {
69    fn get_version(&self) -> Result<DatabaseVersion> {
70        // 实现获取MySQL版本的逻辑
71        todo!()
72    }
73
74    fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
75        // 处理MySQL版本之间的字段类型差异
76        // 例如:在MySQL 8.0中,某些字段类型可能有变化
77        Ok(field.clone())
78    }
79
80    fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
81        // 处理MySQL版本之间的SQL语法差异
82        Ok(sql.to_string())
83    }
84}
85
86/// PostgreSQL兼容性处理
87pub struct PostgresCompatibility;
88
89impl DatabaseCompatibility for PostgresCompatibility {
90    fn get_version(&self) -> Result<DatabaseVersion> {
91        // 实现获取PostgreSQL版本的逻辑
92        todo!()
93    }
94
95    fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
96        // 处理PostgreSQL版本之间的字段类型差异
97        Ok(field.clone())
98    }
99
100    fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
101        // 处理PostgreSQL版本之间的SQL语法差异
102        Ok(sql.to_string())
103    }
104}
105
106/// SQLite兼容性处理
107pub struct SqliteCompatibility;
108
109impl DatabaseCompatibility for SqliteCompatibility {
110    fn get_version(&self) -> Result<DatabaseVersion> {
111        // 实现获取SQLite版本的逻辑
112        todo!()
113    }
114
115    fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
116        // 处理SQLite版本之间的字段类型差异
117        Ok(field.clone())
118    }
119
120    fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
121        // 处理SQLite版本之间的SQL语法差异
122        Ok(sql.to_string())
123    }
124}