Skip to main content

DatabaseExecutor

Trait DatabaseExecutor 

Source
pub trait DatabaseExecutor: Send + Sync {
    // Required method
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        query: &'life1 CompiledQuery,
    ) -> Pin<Box<dyn Future<Output = Result<QueryResult, VlorQLError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Trait for executing compiled SQL queries against a database.

Implementors are responsible for connecting to a database backend, sending the compiled SQL with its bound parameters, and returning the result as a QueryResult.

§Example (mock executor)

use vlorql_core::execute::{DatabaseExecutor, QueryResult};
use vlorql_core::compile::CompiledQuery;
use vlorql_core::errors::VlorQLError;
use vlorql_core::schema::SqlDialect;

struct MockExecutor;

#[async_trait::async_trait]
impl DatabaseExecutor for MockExecutor {
    async fn execute(&self, _query: &CompiledQuery) -> Result<QueryResult, VlorQLError> {
        Ok(QueryResult {
            columns: vec!["id".to_string(), "name".to_string()],
            rows: vec![
                vec![serde_json::json!(1), serde_json::json!("Alice")],
            ],
            rows_affected: 1,
        })
    }
}

let executor = MockExecutor;
let query = CompiledQuery {
    sql: "SELECT id, name FROM users".to_owned(),
    parameters: vec![],
    dialect: SqlDialect::Postgres,
};
let result = executor.execute(&query).await.unwrap();
assert_eq!(result.columns, vec!["id", "name"]);
assert_eq!(result.rows[0][0], serde_json::json!(1));

Required Methods§

Source

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

Execute a compiled query and return the result.

§Arguments
  • query - The CompiledQuery containing SQL, bind parameters, and dialect information.
§Returns
  • Ok(QueryResult) on success.
  • Err(VlorQLError) if execution fails.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§