Skip to main content

citadel_sql/executor/
compile.rs

1use std::sync::Arc;
2
3use citadel::Database;
4use citadel_txn::write_txn::WriteTxn;
5
6use crate::error::Result;
7use crate::parser::Statement;
8use crate::schema::SchemaManager;
9use crate::types::{ExecutionResult, Value};
10
11pub trait CompiledPlan: Send + Sync {
12    fn execute(
13        &self,
14        db: &Database,
15        schema: &SchemaManager,
16        stmt: &Statement,
17        params: &[Value],
18        wtx: Option<&mut WriteTxn<'_>>,
19    ) -> Result<ExecutionResult>;
20
21    /// Attempt to produce a streaming row source. Returns `None` if this plan
22    /// cannot stream the given statement — caller falls back to `execute`.
23    fn try_stream<'db>(
24        &self,
25        _db: &'db Database,
26        _schema: &SchemaManager,
27        _stmt: &Statement,
28        _params: &[Value],
29    ) -> Option<Box<dyn RowSourceIter + 'db>> {
30        None
31    }
32}
33
34/// Internal trait: object-safe streaming source over decoded rows.
35pub trait RowSourceIter {
36    fn next_row(&mut self) -> Result<Option<Vec<Value>>>;
37    fn columns(&self) -> &[String];
38}
39
40pub fn compile(schema: &SchemaManager, stmt: &Statement) -> Option<Arc<dyn CompiledPlan>> {
41    match stmt {
42        Statement::Select(sq) => super::select::CompiledSelect::try_compile(schema, sq)
43            .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
44        Statement::Insert(ins) => super::dml::CompiledInsert::try_compile(schema, ins)
45            .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
46        Statement::Update(upd) => super::write::CompiledUpdate::try_compile(schema, upd)
47            .ok()
48            .flatten()
49            .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
50        Statement::Delete(del) => super::dml::CompiledDelete::try_compile(schema, del)
51            .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
52        _ => None,
53    }
54}