vlorql_core/execute/mod.rs
1//! Query execution trait and result types.
2//!
3//! This module defines [`DatabaseExecutor`], the core abstraction for
4//! executing compiled SQL queries against a database backend, and
5//! [`QueryResult`], the structured result returned after execution.
6
7use crate::compile::CompiledQuery;
8use crate::errors::VlorQLError;
9use async_trait::async_trait;
10use serde::{Deserialize, Serialize};
11
12/// The result of executing a compiled SQL query.
13///
14/// Contains the column names, the returned rows, and the count of
15/// rows affected (useful for DML statements such as `INSERT`,
16/// `UPDATE`, or `DELETE`).
17///
18/// # Examples
19///
20/// ```
21/// use vlorql_core::execute::QueryResult;
22///
23/// let result = QueryResult {
24/// columns: vec!["id".to_string(), "name".to_string()],
25/// rows: vec![
26/// vec![serde_json::json!(1), serde_json::json!("Alice")],
27/// vec![serde_json::json!(2), serde_json::json!("Bob")],
28/// ],
29/// rows_affected: 2,
30/// };
31/// assert_eq!(result.columns.len(), 2);
32/// assert_eq!(result.rows.len(), 2);
33/// ```
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
35pub struct QueryResult {
36 /// The column names in the order they appear in the result set.
37 pub columns: Vec<String>,
38
39 /// The data rows, each element positionally matching `columns`.
40 pub rows: Vec<Vec<serde_json::Value>>,
41
42 /// The number of rows affected by the query.
43 ///
44 /// For `SELECT` queries this is typically the row count of the
45 /// result set; for DML statements it reflects the number of
46 /// modified rows.
47 pub rows_affected: u64,
48}
49
50/// Trait for executing compiled SQL queries against a database.
51///
52/// Implementors are responsible for connecting to a database backend,
53/// sending the compiled SQL with its bound parameters, and returning
54/// the result as a [`QueryResult`].
55///
56/// # Example (mock executor)
57///
58/// ```
59/// use vlorql_core::execute::{DatabaseExecutor, QueryResult};
60/// use vlorql_core::compile::CompiledQuery;
61/// use vlorql_core::errors::VlorQLError;
62/// use vlorql_core::schema::SqlDialect;
63///
64/// struct MockExecutor;
65///
66/// #[async_trait::async_trait]
67/// impl DatabaseExecutor for MockExecutor {
68/// async fn execute(&self, _query: &CompiledQuery) -> Result<QueryResult, VlorQLError> {
69/// Ok(QueryResult {
70/// columns: vec!["id".to_string(), "name".to_string()],
71/// rows: vec![
72/// vec![serde_json::json!(1), serde_json::json!("Alice")],
73/// ],
74/// rows_affected: 1,
75/// })
76/// }
77/// }
78///
79/// # #[tokio::main]
80/// # async fn main() {
81/// let executor = MockExecutor;
82/// let query = CompiledQuery {
83/// sql: "SELECT id, name FROM users".to_owned(),
84/// parameters: vec![],
85/// dialect: SqlDialect::Postgres,
86/// };
87/// let result = executor.execute(&query).await.unwrap();
88/// assert_eq!(result.columns, vec!["id", "name"]);
89/// assert_eq!(result.rows[0][0], serde_json::json!(1));
90/// # }
91/// ```
92#[async_trait]
93pub trait DatabaseExecutor: Send + Sync {
94 /// Execute a compiled query and return the result.
95 ///
96 /// # Arguments
97 ///
98 /// * `query` - The [`CompiledQuery`] containing SQL, bind
99 /// parameters, and dialect information.
100 ///
101 /// # Returns
102 ///
103 /// * `Ok(QueryResult)` on success.
104 /// * `Err(VlorQLError)` if execution fails.
105 async fn execute(&self, query: &CompiledQuery) -> Result<QueryResult, VlorQLError>;
106}