sqltool/databases/
compatibility.rs1use crate::models::Field;
2use anyhow::Result;
3
4#[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 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 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 pub fn is_gte(&self, other: &Self) -> bool {
44 self.compare(other) != std::cmp::Ordering::Less
45 }
46
47 pub fn is_lt(&self, other: &Self) -> bool {
49 self.compare(other) == std::cmp::Ordering::Less
50 }
51}
52
53pub trait DatabaseCompatibility {
55 fn get_version(&self) -> Result<DatabaseVersion>;
57
58 fn convert_field_type(&self, field: &Field, target_version: &DatabaseVersion) -> Result<Field>;
60
61 fn generate_compatible_sql(&self, sql: &str, target_version: &DatabaseVersion) -> Result<String>;
63}
64
65pub struct MySqlCompatibility;
67
68impl DatabaseCompatibility for MySqlCompatibility {
69 fn get_version(&self) -> Result<DatabaseVersion> {
70 todo!()
72 }
73
74 fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
75 Ok(field.clone())
78 }
79
80 fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
81 Ok(sql.to_string())
83 }
84}
85
86pub struct PostgresCompatibility;
88
89impl DatabaseCompatibility for PostgresCompatibility {
90 fn get_version(&self) -> Result<DatabaseVersion> {
91 todo!()
93 }
94
95 fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
96 Ok(field.clone())
98 }
99
100 fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
101 Ok(sql.to_string())
103 }
104}
105
106pub struct SqliteCompatibility;
108
109impl DatabaseCompatibility for SqliteCompatibility {
110 fn get_version(&self) -> Result<DatabaseVersion> {
111 todo!()
113 }
114
115 fn convert_field_type(&self, field: &Field, _target_version: &DatabaseVersion) -> Result<Field> {
116 Ok(field.clone())
118 }
119
120 fn generate_compatible_sql(&self, sql: &str, _target_version: &DatabaseVersion) -> Result<String> {
121 Ok(sql.to_string())
123 }
124}