ignis_db/database/
traits.rs1use crate::migrate::Migration;
6use crate::schema::{CompositeType, Grant, Index, Role, Table, View};
7use anyhow::Result;
8use std::collections::HashMap;
9
10pub trait Database: Send + Sync {
15 fn database_type(&self) -> crate::database::DatabaseType;
17
18 fn name(&self) -> &str;
20
21 fn type_mapping(&self) -> &dyn TypeMapping;
23
24 fn sql_generator(&self) -> &dyn SqlGenerator;
26
27 fn introspector(&self) -> &dyn Introspector;
29
30 fn connection_manager(&self) -> &dyn ConnectionManager;
32
33 fn migration_manager(&self) -> &dyn MigrationManager;
35}
36
37pub trait TypeMapping: Send + Sync {
39 fn rust_to_sql_type(&self, rust_type: &str) -> String;
41
42 fn sql_to_rust_type(&self, sql_type: &str) -> String;
44
45 fn default_value(&self, sql_type: &str) -> Option<String>;
47
48 fn supports_auto_increment(&self, sql_type: &str) -> bool;
50
51 fn max_string_length(&self, sql_type: &str) -> Option<usize>;
53}
54
55pub trait SqlGenerator: Send + Sync {
57 fn create_table_sql(&self, table: &Table) -> String;
59
60 fn create_view_sql(&self, view: &View) -> String;
62
63 fn create_index_sql(&self, index: &Index) -> String;
65
66 fn create_type_sql(&self, composite_type: &CompositeType) -> String;
68
69 fn create_schema_sql(&self, schema_name: &str) -> String;
71
72 fn alter_table_sql(&self, table: &Table, changes: &[String]) -> String;
74
75 fn drop_table_sql(&self, table_name: &str) -> String;
77
78 fn drop_view_sql(&self, view_name: &str) -> String;
80
81 fn drop_index_sql(&self, index_name: &str) -> String;
83
84 fn insert_sql(&self, table_name: &str, columns: &[String], values: &[String]) -> String;
86
87 fn update_sql(&self, table_name: &str, set_clause: &str, where_clause: &str) -> String;
89
90 fn delete_sql(&self, table_name: &str, where_clause: &str) -> String;
92
93 fn select_sql(
95 &self,
96 table_name: &str,
97 columns: &[String],
98 where_clause: Option<&str>,
99 ) -> String;
100
101 fn quote_identifier(&self, identifier: &str) -> String;
103
104 fn quote_string(&self, value: &str) -> String;
106
107 fn current_timestamp_function(&self) -> &str;
109
110 fn auto_increment_keyword(&self) -> &str;
112
113 fn create_guc_sql(&self, guc: &crate::schema::GucSetting) -> String;
115
116 fn reset_guc_sql(&self, guc: &crate::schema::GucSetting) -> String;
118
119 fn get_guc_sql(&self, guc: &crate::schema::GucSetting) -> String;
121
122 fn list_guc_settings_sql(&self) -> String;
124
125 fn set_jwt_context_sql(
127 &self,
128 user_id: &str,
129 tenant_id: Option<&str>,
130 role: Option<&str>,
131 ) -> String;
132
133 fn clear_jwt_context_sql(&self) -> String;
135}
136
137pub trait Introspector: Send + Sync {
139 fn get_tables(&self) -> Result<Vec<Table>>;
141
142 fn get_table(&self, table_name: &str) -> Result<Option<Table>>;
144
145 fn get_views(&self) -> Result<Vec<View>>;
147
148 fn get_view(&self, view_name: &str) -> Result<Option<View>>;
150
151 fn get_indexes(&self) -> Result<Vec<Index>>;
153
154 fn get_table_indexes(&self, table_name: &str) -> Result<Vec<Index>>;
156
157 fn get_composite_types(&self) -> Result<Vec<CompositeType>>;
159
160 fn get_composite_type(&self, type_name: &str) -> Result<Option<CompositeType>>;
162
163 fn get_roles(&self) -> Result<Vec<Role>>;
165
166 fn get_grants(&self) -> Result<Vec<Grant>>;
168
169 fn get_schema_version(&self) -> Result<Option<String>>;
171
172 fn table_exists(&self, table_name: &str) -> Result<bool>;
174
175 fn view_exists(&self, view_name: &str) -> Result<bool>;
177
178 fn index_exists(&self, index_name: &str) -> Result<bool>;
180}
181
182pub trait ConnectionManager: Send + Sync {
184 fn create_pool(
186 &self,
187 config: &crate::database::DatabaseConfig,
188 ) -> Result<Box<dyn ConnectionPool>>;
189
190 fn test_connection(&self, config: &crate::database::DatabaseConfig) -> Result<()>;
192
193 fn connection_string(&self, config: &crate::database::DatabaseConfig) -> String;
195
196 fn connection_options(
198 &self,
199 config: &crate::database::DatabaseConfig,
200 ) -> Result<Box<dyn std::fmt::Debug>>;
201}
202
203pub trait ConnectionPool: Send + Sync {
205 fn execute_query(
207 &self,
208 sql: &str,
209 params: &[&dyn std::fmt::Display],
210 ) -> Result<Vec<HashMap<String, String>>>;
211
212 fn execute_command(&self, sql: &str, params: &[&dyn std::fmt::Display]) -> Result<u64>;
214
215 fn begin_transaction(&self) -> Result<Box<dyn Transaction>>;
217
218 fn active_connections(&self) -> u32;
220
221 fn max_connections(&self) -> u32;
223}
224
225pub trait Transaction: Send + Sync {
227 fn execute_query(
229 &self,
230 sql: &str,
231 params: &[&dyn std::fmt::Display],
232 ) -> Result<Vec<HashMap<String, String>>>;
233
234 fn execute_command(&self, sql: &str, params: &[&dyn std::fmt::Display]) -> Result<u64>;
236
237 fn commit(self: Box<Self>) -> Result<()>;
239
240 fn rollback(self: Box<Self>) -> Result<()>;
242}
243
244pub trait MigrationManager: Send + Sync {
246 fn get_applied_migrations(&self) -> Result<Vec<Migration>>;
248
249 fn apply_migration(&self, migration: &Migration) -> Result<()>;
251
252 fn rollback_migration(&self, migration: &Migration) -> Result<()>;
254
255 fn is_migration_applied(&self, migration_id: &str) -> Result<bool>;
257
258 fn create_migrations_table(&self) -> Result<()>;
260
261 fn get_current_version(&self) -> Result<Option<String>>;
263
264 fn set_schema_version(&self, version: &str) -> Result<()>;
266}