Skip to main content

wae_database/connection/
trait_impl.rs

1//! 数据库连接抽象 trait
2
3use crate::connection::{config::DatabaseResult, row::DatabaseRows, statement::DatabaseStatement};
4use async_trait::async_trait;
5
6/// 数据库后端类型
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum DatabaseBackend {
9    /// Turso (SQLite) backend
10    Turso,
11    /// PostgreSQL backend
12    Postgres,
13    /// MySQL backend
14    MySql,
15}
16
17/// 数据库连接抽象
18#[async_trait]
19pub trait DatabaseConnection: Send + Sync {
20    /// 获取数据库后端类型
21    fn backend(&self) -> DatabaseBackend;
22
23    /// 执行查询,返回结果集
24    async fn query(&self, sql: &str) -> DatabaseResult<DatabaseRows>;
25
26    /// 执行带参数的查询 (使用 wae_types::Value)
27    async fn query_with(&self, sql: &str, params: Vec<wae_types::Value>) -> DatabaseResult<DatabaseRows>;
28
29    /// 执行语句,返回影响的行数
30    async fn execute(&self, sql: &str) -> DatabaseResult<u64>;
31
32    /// 执行带参数的语句 (使用 wae_types::Value)
33    async fn execute_with(&self, sql: &str, params: Vec<wae_types::Value>) -> DatabaseResult<u64>;
34
35    /// 准备语句
36    async fn prepare(&self, sql: &str) -> DatabaseResult<DatabaseStatement>;
37
38    /// 开始一个新事务
39    async fn begin_transaction(&self) -> DatabaseResult<()>;
40
41    /// 提交当前事务
42    async fn commit(&self) -> DatabaseResult<()>;
43
44    /// 回滚当前事务
45    async fn rollback(&self) -> DatabaseResult<()>;
46
47    #[cfg(feature = "turso")]
48    /// 执行带参数的查询 (内部使用 turso::Value)
49    async fn query_with_turso(&self, _sql: &str, _params: Vec<turso::Value>) -> DatabaseResult<DatabaseRows> {
50        unimplemented!("query_with_turso is only implemented for Turso connections")
51    }
52
53    #[cfg(feature = "turso")]
54    /// 执行带参数的语句 (内部使用 turso::Value)
55    async fn execute_with_turso(&self, _sql: &str, _params: Vec<turso::Value>) -> DatabaseResult<u64> {
56        unimplemented!("execute_with_turso is only implemented for Turso connections")
57    }
58}