vespertide_query/sql/
types.rs

1/// Database backend for SQL generation
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum DatabaseBackend {
4    Postgres,
5    MySql,
6    Sqlite,
7}
8
9/// Represents a built query that can be converted to SQL for any database backend
10#[derive(Debug, Clone)]
11pub enum BuiltQuery {
12    CreateTable(Box<sea_query::TableCreateStatement>),
13    DropTable(Box<sea_query::TableDropStatement>),
14    AlterTable(Box<sea_query::TableAlterStatement>),
15    CreateIndex(Box<sea_query::IndexCreateStatement>),
16    DropIndex(Box<sea_query::IndexDropStatement>),
17    RenameTable(Box<sea_query::TableRenameStatement>),
18    CreateForeignKey(Box<sea_query::ForeignKeyCreateStatement>),
19    DropForeignKey(Box<sea_query::ForeignKeyDropStatement>),
20    Insert(Box<sea_query::InsertStatement>),
21    Update(Box<sea_query::UpdateStatement>),
22    Raw(RawSql),
23}
24
25/// Raw SQL that may have backend-specific variants
26#[derive(Debug, Clone)]
27pub struct RawSql {
28    pub postgres: String,
29    pub mysql: String,
30    pub sqlite: String,
31}
32
33impl RawSql {
34    /// Create a RawSql with the same SQL for all backends
35    pub fn uniform(sql: String) -> Self {
36        Self {
37            postgres: sql.clone(),
38            mysql: sql.clone(),
39            sqlite: sql,
40        }
41    }
42
43    /// Create a RawSql with different SQL for each backend
44    pub fn per_backend(postgres: String, mysql: String, sqlite: String) -> Self {
45        Self {
46            postgres,
47            mysql,
48            sqlite,
49        }
50    }
51}
52
53impl BuiltQuery {
54    /// Build SQL string for the specified database backend
55    pub fn build(&self, backend: DatabaseBackend) -> String {
56        match self {
57            BuiltQuery::CreateTable(stmt) => {
58                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
59            }
60            BuiltQuery::DropTable(stmt) => {
61                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
62            }
63            BuiltQuery::AlterTable(stmt) => {
64                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
65            }
66            BuiltQuery::CreateIndex(stmt) => {
67                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
68            }
69            BuiltQuery::DropIndex(stmt) => {
70                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
71            }
72            BuiltQuery::RenameTable(stmt) => {
73                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
74            }
75            BuiltQuery::CreateForeignKey(stmt) => {
76                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
77            }
78            BuiltQuery::DropForeignKey(stmt) => {
79                crate::sql::helpers::build_schema_statement(stmt.as_ref(), backend)
80            }
81            BuiltQuery::Insert(stmt) => {
82                crate::sql::helpers::build_query_statement(stmt.as_ref(), backend)
83            }
84            BuiltQuery::Update(stmt) => {
85                crate::sql::helpers::build_query_statement(stmt.as_ref(), backend)
86            }
87            BuiltQuery::Raw(raw) => match backend {
88                DatabaseBackend::Postgres => raw.postgres.clone(),
89                DatabaseBackend::MySql => raw.mysql.clone(),
90                DatabaseBackend::Sqlite => raw.sqlite.clone(),
91            },
92        }
93    }
94}