1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Query execution trait and result types.
//!
//! This module defines [`DatabaseExecutor`], the core abstraction for
//! executing compiled SQL queries against a database backend, and
//! [`QueryResult`], the structured result returned after execution.
use crateCompiledQuery;
use crateVlorQLError;
use async_trait;
use ;
/// The result of executing a compiled SQL query.
///
/// Contains the column names, the returned rows, and the count of
/// rows affected (useful for DML statements such as `INSERT`,
/// `UPDATE`, or `DELETE`).
///
/// # Examples
///
/// ```
/// use vlorql_core::execute::QueryResult;
///
/// let result = QueryResult {
/// columns: vec!["id".to_string(), "name".to_string()],
/// rows: vec![
/// vec![serde_json::json!(1), serde_json::json!("Alice")],
/// vec![serde_json::json!(2), serde_json::json!("Bob")],
/// ],
/// rows_affected: 2,
/// };
/// assert_eq!(result.columns.len(), 2);
/// assert_eq!(result.rows.len(), 2);
/// ```
/// 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,
/// })
/// }
/// }
///
/// # #[tokio::main]
/// # async fn main() {
/// 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));
/// # }
/// ```