1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum BackendKind {
7 MySql,
9 PostgreSql,
11 Redis,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum PlaceholderStyle {
19 QuestionMark,
21 NumberedDollar,
23 NotApplicable,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29#[non_exhaustive]
30pub enum BackendCapability {
31 CheckedIdentifiers,
33 Select,
35 Aggregate,
37 Join,
39 Insert,
41 BatchInsert,
43 Update,
45 BatchUpdate,
47 Delete,
49 Upsert,
51 BatchUpsert,
54 Returning,
56 ExplicitConflictTarget,
58 Savepoint,
60 SkipLocked,
62 Copy,
64 ListenNotify,
66 Cte,
68 Window,
70 Arrays,
72 Range,
74 Multirange,
76 JsonPath,
78 FullText,
80 Merge,
82 Diagnostics,
84 TwoPhaseCommit,
86 TableLock,
88 TransactionLocalConfig,
90 TransactionCrud,
92 RedisPipeline,
94 RedisOptimisticTransaction,
96 RedisLua,
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102#[non_exhaustive]
103pub enum SafetyConstraint {
104 ParameterizedValues,
106 CheckedIdentifiers,
108 ExplicitTrustedExpressions,
110 WriteRequiresWhere,
112 StructuredCommandArguments,
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
118#[non_exhaustive]
119pub struct BackendCapabilities {
120 pub backend: BackendKind,
122 pub relational: bool,
124 pub placeholder_style: PlaceholderStyle,
126 pub operations: &'static [BackendCapability],
128 pub transaction: &'static [BackendCapability],
130 pub safety: &'static [SafetyConstraint],
132}
133
134impl BackendCapabilities {
135 pub fn supports(&self, capability: BackendCapability) -> bool {
137 self.operations.contains(&capability) || self.transaction.contains(&capability)
138 }
139
140 pub fn enforces(&self, constraint: SafetyConstraint) -> bool {
142 self.safety.contains(&constraint)
143 }
144}
145
146const MYSQL_OPERATIONS: &[BackendCapability] = &[
147 BackendCapability::CheckedIdentifiers,
148 BackendCapability::Select,
149 BackendCapability::Aggregate,
150 BackendCapability::Join,
151 BackendCapability::Insert,
152 BackendCapability::BatchInsert,
153 BackendCapability::Update,
154 BackendCapability::BatchUpdate,
155 BackendCapability::Delete,
156 BackendCapability::Upsert,
157 BackendCapability::BatchUpsert,
158];
159
160const POSTGRES_OPERATIONS: &[BackendCapability] = &[
167 BackendCapability::CheckedIdentifiers,
168 BackendCapability::Select,
169 BackendCapability::Aggregate,
170 BackendCapability::Join,
171 BackendCapability::Insert,
172 BackendCapability::BatchInsert,
173 BackendCapability::Update,
174 BackendCapability::BatchUpdate,
175 BackendCapability::Delete,
176 BackendCapability::Upsert,
177 BackendCapability::BatchUpsert,
178 BackendCapability::Returning,
179 BackendCapability::ExplicitConflictTarget,
180 BackendCapability::SkipLocked,
181 BackendCapability::Copy,
182 BackendCapability::ListenNotify,
183 BackendCapability::Cte,
184 BackendCapability::Window,
185 BackendCapability::Arrays,
186 BackendCapability::Range,
187 BackendCapability::Multirange,
188 BackendCapability::JsonPath,
189 BackendCapability::FullText,
190 BackendCapability::Merge,
191 BackendCapability::Diagnostics,
192];
193
194const SQL_SAFETY: &[SafetyConstraint] = &[
195 SafetyConstraint::ParameterizedValues,
196 SafetyConstraint::CheckedIdentifiers,
197 SafetyConstraint::ExplicitTrustedExpressions,
198 SafetyConstraint::WriteRequiresWhere,
199];
200
201pub const MYSQL_CAPABILITIES: BackendCapabilities = BackendCapabilities {
203 backend: BackendKind::MySql,
204 relational: true,
205 placeholder_style: PlaceholderStyle::QuestionMark,
206 operations: MYSQL_OPERATIONS,
207 transaction: &[BackendCapability::TransactionCrud],
208 safety: SQL_SAFETY,
209};
210
211pub const POSTGRES_CAPABILITIES: BackendCapabilities = BackendCapabilities {
213 backend: BackendKind::PostgreSql,
214 relational: true,
215 placeholder_style: PlaceholderStyle::NumberedDollar,
216 operations: POSTGRES_OPERATIONS,
217 transaction: &[
218 BackendCapability::TransactionCrud,
219 BackendCapability::Returning,
220 BackendCapability::Savepoint,
221 BackendCapability::TwoPhaseCommit,
222 BackendCapability::TableLock,
223 BackendCapability::TransactionLocalConfig,
224 ],
225 safety: SQL_SAFETY,
226};
227
228pub const REDIS_CAPABILITIES: BackendCapabilities = BackendCapabilities {
230 backend: BackendKind::Redis,
231 relational: false,
232 placeholder_style: PlaceholderStyle::NotApplicable,
233 operations: &[
234 BackendCapability::RedisPipeline,
235 BackendCapability::RedisLua,
236 ],
237 transaction: &[BackendCapability::RedisOptimisticTransaction],
238 safety: &[SafetyConstraint::StructuredCommandArguments],
239};
240
241#[cfg(test)]
242mod tests {
243 use super::*;
244 #[cfg(any(feature = "mysql", feature = "postgres", feature = "redis"))]
245 use std::future::Future;
246
247 #[cfg(any(feature = "mysql", feature = "postgres", feature = "redis"))]
248 fn assert_future<T>(_future: impl Future<Output = T>) {}
249
250 #[cfg(feature = "mysql")]
251 fn mysql_management_contract(db: &crate::mysql::Database) {
252 let _: &'static BackendCapabilities = crate::mysql::Database::capabilities();
253 let _: crate::PoolStatus = db.pool_status();
254 let _: bool = db.is_closed();
255 assert_future::<Result<bool, crate::DbError>>(db.health_check());
256 assert_future::<()>(db.close());
257 }
258
259 #[cfg(feature = "postgres")]
260 fn postgres_management_contract(db: &crate::postgres::Database) {
261 let _: &'static BackendCapabilities = crate::postgres::Database::capabilities();
262 let _: crate::PoolStatus = db.pool_status();
263 let _: bool = db.is_closed();
264 assert_future::<Result<bool, crate::DbError>>(db.health_check());
265 assert_future::<()>(db.close());
266 }
267
268 #[cfg(feature = "redis")]
269 fn redis_management_contract(client: &crate::redis::RedisClient) {
270 let _: &'static BackendCapabilities = crate::redis::RedisClient::capabilities();
271 let _: crate::PoolStatus = client.pool_status();
272 let _: bool = client.is_closed();
273 assert_future::<Result<bool, crate::DbError>>(client.health_check());
274 assert_future::<()>(client.close());
275 }
276
277 #[test]
278 fn management_contract_signatures_compile() {
279 #[cfg(feature = "mysql")]
280 let _ = mysql_management_contract;
281 #[cfg(feature = "postgres")]
282 let _ = postgres_management_contract;
283 #[cfg(feature = "redis")]
284 let _ = redis_management_contract;
285 }
286
287 #[test]
288 fn sql_backends_share_common_contract_and_expose_dialect_differences() {
289 for capability in [
290 BackendCapability::CheckedIdentifiers,
291 BackendCapability::Select,
292 BackendCapability::Aggregate,
293 BackendCapability::Join,
294 BackendCapability::Insert,
295 BackendCapability::BatchInsert,
296 BackendCapability::Update,
297 BackendCapability::BatchUpdate,
298 BackendCapability::Delete,
299 BackendCapability::Upsert,
300 BackendCapability::TransactionCrud,
301 ] {
302 assert!(MYSQL_CAPABILITIES.supports(capability));
303 assert!(POSTGRES_CAPABILITIES.supports(capability));
304 }
305
306 assert!(!MYSQL_CAPABILITIES.supports(BackendCapability::Returning));
307 assert!(POSTGRES_CAPABILITIES.supports(BackendCapability::Returning));
308 assert!(!MYSQL_CAPABILITIES.supports(BackendCapability::ExplicitConflictTarget));
309 assert!(POSTGRES_CAPABILITIES.supports(BackendCapability::ExplicitConflictTarget));
310 }
311
312 #[test]
313 fn both_backends_declare_batch_upsert() {
314 assert!(MYSQL_CAPABILITIES.supports(BackendCapability::BatchUpsert));
316 assert!(POSTGRES_CAPABILITIES.supports(BackendCapability::BatchUpsert));
317 }
318
319 #[test]
320 fn postgres_capabilities_match_implemented_features() {
321 for capability in [
323 BackendCapability::Savepoint,
324 BackendCapability::Cte,
325 BackendCapability::Window,
326 BackendCapability::SkipLocked,
327 BackendCapability::Returning,
328 BackendCapability::ExplicitConflictTarget,
329 BackendCapability::Copy,
330 BackendCapability::ListenNotify,
331 BackendCapability::BatchUpsert,
332 BackendCapability::Arrays,
333 BackendCapability::Range,
334 BackendCapability::Multirange,
335 BackendCapability::JsonPath,
336 BackendCapability::FullText,
337 BackendCapability::Merge,
338 BackendCapability::Diagnostics,
339 BackendCapability::TwoPhaseCommit,
340 BackendCapability::TableLock,
341 BackendCapability::TransactionLocalConfig,
342 ] {
343 assert!(
344 POSTGRES_CAPABILITIES.supports(capability),
345 "PostgreSQL 已实现 {capability:?},必须声明"
346 );
347 }
348
349 assert!(
351 POSTGRES_CAPABILITIES.enforces(SafetyConstraint::ExplicitTrustedExpressions),
352 "PostgreSQL 必须声明 ExplicitTrustedExpressions 安全约束"
353 );
354
355 for capability in [
357 BackendCapability::Copy,
358 BackendCapability::ListenNotify,
359 BackendCapability::Arrays,
360 BackendCapability::Range,
361 BackendCapability::Multirange,
362 BackendCapability::JsonPath,
363 BackendCapability::FullText,
364 BackendCapability::Merge,
365 BackendCapability::Diagnostics,
366 BackendCapability::TwoPhaseCommit,
367 BackendCapability::TableLock,
368 BackendCapability::TransactionLocalConfig,
369 ] {
370 assert!(
371 !MYSQL_CAPABILITIES.supports(capability),
372 "MySQL 未实现 {capability:?},不得声明"
373 );
374 assert!(
375 !REDIS_CAPABILITIES.supports(capability),
376 "Redis 未实现 {capability:?},不得声明"
377 );
378 }
379 }
380
381 #[test]
382 fn non_postgres_backends_do_not_claim_postgres_only_capabilities() {
383 for capability in [
385 BackendCapability::Returning,
386 BackendCapability::ExplicitConflictTarget,
387 BackendCapability::SkipLocked,
388 BackendCapability::Savepoint,
389 BackendCapability::Cte,
390 BackendCapability::Window,
391 BackendCapability::Arrays,
392 BackendCapability::Range,
393 BackendCapability::Multirange,
394 BackendCapability::JsonPath,
395 BackendCapability::FullText,
396 BackendCapability::Merge,
397 BackendCapability::Diagnostics,
398 BackendCapability::TwoPhaseCommit,
399 BackendCapability::TableLock,
400 BackendCapability::TransactionLocalConfig,
401 ] {
402 assert!(!MYSQL_CAPABILITIES.supports(capability), "{capability:?}");
403 assert!(!REDIS_CAPABILITIES.supports(capability), "{capability:?}");
404 }
405 }
406
407 #[test]
408 fn redis_is_explicitly_non_relational() {
409 let redis = std::hint::black_box(REDIS_CAPABILITIES);
410
411 assert!(!redis.relational);
412 assert_eq!(redis.placeholder_style, PlaceholderStyle::NotApplicable);
413 assert!(!redis.supports(BackendCapability::Select));
414 assert!(redis.supports(BackendCapability::RedisPipeline));
415 assert!(redis.supports(BackendCapability::RedisOptimisticTransaction));
416 assert!(redis.supports(BackendCapability::RedisLua));
417 }
418
419 #[test]
420 fn sql_safety_contract_is_symmetric() {
421 for constraint in SQL_SAFETY {
422 assert!(MYSQL_CAPABILITIES.enforces(*constraint));
423 assert!(POSTGRES_CAPABILITIES.enforces(*constraint));
424 }
425 assert!(REDIS_CAPABILITIES.enforces(SafetyConstraint::StructuredCommandArguments));
426 assert!(!REDIS_CAPABILITIES.enforces(SafetyConstraint::WriteRequiresWhere));
427 }
428}