citadel_sql/executor/
compile.rs1use 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 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 fn uses_scoped_params(&self) -> bool {
36 true
37 }
38}
39
40pub trait RowSourceIter {
42 fn next_row(&mut self) -> Result<Option<Vec<Value>>>;
43 fn columns(&self) -> &[String];
44}
45
46pub fn compile(schema: &SchemaManager, stmt: &Statement) -> Option<Arc<dyn CompiledPlan>> {
47 match stmt {
48 Statement::Select(sq) => super::select::CompiledSelect::try_compile(schema, sq)
49 .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
50 Statement::Insert(ins) => super::dml::CompiledInsert::try_compile(schema, ins)
51 .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
52 Statement::Update(upd) => super::write::CompiledUpdate::try_compile(schema, upd)
53 .ok()
54 .flatten()
55 .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
56 Statement::Delete(del) => super::dml::CompiledDelete::try_compile(schema, del)
57 .map(|c| Arc::new(c) as Arc<dyn CompiledPlan>),
58 _ => None,
59 }
60}