Skip to main content

Executor

Trait Executor 

Source
pub trait Executor {
    // Required methods
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        query: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<MySqlQueryResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn fetch_all<'q, 'life0, 'async_trait, T>(
        &'life0 mut self,
        query: &'q str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>
       where T: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + 'async_trait,
             Self: 'async_trait,
             'q: 'async_trait,
             'life0: 'async_trait;
    fn fetch_one<'q, 'life0, 'async_trait, T>(
        &'life0 mut self,
        query: &'q str,
    ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
       where T: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + 'async_trait,
             Self: 'async_trait,
             'q: 'async_trait,
             'life0: 'async_trait;
    fn fetch_optional<'q, 'life0, 'async_trait, T>(
        &'life0 mut self,
        query: &'q str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>
       where T: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + 'async_trait,
             Self: 'async_trait,
             'q: 'async_trait,
             'life0: 'async_trait;
}
Expand description

数据库执行器特征

提供了基本的数据库操作接口,包括执行查询、获取结果等。

§示例

use baichun_framework_db::{Executor, DbPool};

async fn example(pool: &mut DbPool) -> Result<()> {
    // 执行插入操作
    pool.execute("INSERT INTO users (name) VALUES ('Alice')").await?;

    // 查询单条记录
    let user: User = pool.fetch_one("SELECT * FROM users WHERE id = 1").await?;

    // 查询多条记录
    let users: Vec<User> = pool.fetch_all("SELECT * FROM users").await?;

    Ok(())
}

Required Methods§

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 mut self, query: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<MySqlQueryResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

执行不返回行的查询

适用于 INSERT、UPDATE、DELETE 等操作。

Source

fn fetch_all<'q, 'life0, 'async_trait, T>( &'life0 mut self, query: &'q str, ) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>
where T: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + 'async_trait, Self: 'async_trait, 'q: 'async_trait, 'life0: 'async_trait,

执行返回多行的查询

将结果转换为指定类型的向量。

Source

fn fetch_one<'q, 'life0, 'async_trait, T>( &'life0 mut self, query: &'q str, ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
where T: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + 'async_trait, Self: 'async_trait, 'q: 'async_trait, 'life0: 'async_trait,

执行返回单行的查询

如果查询没有返回行,将返回错误。

Source

fn fetch_optional<'q, 'life0, 'async_trait, T>( &'life0 mut self, query: &'q str, ) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>
where T: Send + Unpin + for<'r> FromRow<'r, MySqlRow> + 'async_trait, Self: 'async_trait, 'q: 'async_trait, 'life0: 'async_trait,

执行返回可选单行的查询

如果查询没有返回行,将返回 None。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<'c, T> Executor for T
where T: DbExecutor<'c> + Send + Sync,