Skip to main content

cool_core/service/
mysql.rs

1//! MySQL 专用 Service 包装
2//!
3//! 对齐 TS 版本的 `service/mysql.ts`,主要提供:
4//! - 便捷的构造函数
5//! - 后端校验(确保当前连接是 MySQL)
6//! - 复用 `SimpleService` 的所有 CRUD 能力
7
8use super::base::{BaseService, SimpleService};
9use crate::error::CoolError;
10use sea_orm::{ConnectionTrait, DatabaseBackend, DatabaseConnection};
11use std::sync::Arc;
12
13/// MySQL Service
14pub struct MySqlService {
15    inner: SimpleService,
16}
17
18impl MySqlService {
19    /// 创建 MySQL Service,校验后端类型
20    pub fn new(db: Arc<DatabaseConnection>, table: impl Into<String>) -> Result<Self, CoolError> {
21        if db.get_database_backend() != DatabaseBackend::MySql {
22            return Err(CoolError::comm("当前连接不是 MySQL 后端"));
23        }
24        Ok(Self {
25            inner: SimpleService::new(db, table),
26        })
27    }
28}
29
30#[async_trait::async_trait]
31impl BaseService for MySqlService {
32    fn db(&self) -> &DatabaseConnection {
33        self.inner.db()
34    }
35
36    fn table_name(&self) -> &str {
37        self.inner.table_name()
38    }
39}