Skip to main content

wae_database/connection/
statement.rs

1//! 预处理语句模块
2
3use crate::connection::{config::DatabaseResult, row::DatabaseRows};
4use wae_types::WaeError;
5
6/// 预处理语句
7pub enum DatabaseStatement {
8    /// Turso 预处理语句
9    #[cfg(feature = "turso")]
10    Turso(turso::Statement),
11    /// PostgreSQL 预处理语句
12    #[cfg(feature = "postgres")]
13    Postgres(String),
14    /// MySQL 预处理语句
15    #[cfg(feature = "mysql")]
16    MySql(String),
17}
18
19impl DatabaseStatement {
20    #[cfg(feature = "turso")]
21    pub(crate) fn new_turso(stmt: turso::Statement) -> Self {
22        Self::Turso(stmt)
23    }
24
25    #[cfg(feature = "postgres")]
26    pub(crate) fn new_postgres(sql: String) -> Self {
27        Self::Postgres(sql)
28    }
29
30    #[cfg(feature = "mysql")]
31    pub(crate) fn new_mysql(sql: String) -> Self {
32        Self::MySql(sql)
33    }
34
35    /// 执行查询 (使用 wae_types::Value)
36    pub async fn query(&mut self, _params: Vec<wae_types::Value>) -> DatabaseResult<DatabaseRows> {
37        match self {
38            #[cfg(feature = "turso")]
39            Self::Turso(stmt) => {
40                let turso_params = crate::types::from_wae_values(_params);
41                stmt.query(turso_params)
42                    .await
43                    .map_err(|e| WaeError::internal(format!("Query failed: {}", e)))
44                    .map(DatabaseRows::new_turso)
45            }
46            #[cfg(feature = "postgres")]
47            Self::Postgres(_) => Err(WaeError::internal("Prepared statements not supported for Postgres yet")),
48            #[cfg(feature = "mysql")]
49            Self::MySql(_) => Err(WaeError::internal("Prepared statements not supported for MySQL yet")),
50        }
51    }
52}