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
//! Definition for a `Program` to run code.
//!
//! It carries an [EnvDb] with the functions, idents, types.

use crate::errors::ErrorVm;
use crate::eval::{build_query, build_source_expr_query};
use crate::expr::{Code, CrudExpr, SourceSet};
use crate::rel_ops::RelOps;
use crate::relation::MemTable;
use spacetimedb_sats::ProductValue;

/// A trait to allow split the execution of `programs` to allow executing
/// `queries` that take in account each `program` state/enviroment.
///
/// To be specific, it allows you to run queries that run on the `SpacetimeDB` engine.
///
/// It could also permit run queries backed by different engines, like in `MySql`.
pub trait ProgramVm {
    /// Allows to execute the query with the state carried by the implementation of this
    /// trait
    fn eval_query<const N: usize>(&mut self, query: CrudExpr, sources: Sources<'_, N>) -> Result<Code, ErrorVm>;
}

pub type Sources<'a, const N: usize> = &'a mut SourceSet<Vec<ProductValue>, N>;

/// A default program that run in-memory without a database
pub struct Program;

impl ProgramVm for Program {
    fn eval_query<const N: usize>(&mut self, query: CrudExpr, sources: Sources<'_, N>) -> Result<Code, ErrorVm> {
        match query {
            CrudExpr::Query(query) => {
                let result = build_source_expr_query(sources, &query.source);
                let result = build_query(result, &query.query, sources)?;

                let head = result.head().clone();
                let rows: Vec<_> = result.collect_vec(|row| row.into_product_value())?;

                Ok(Code::Table(MemTable::new(head, query.source.table_access(), rows)))
            }
            CrudExpr::Insert { .. } => {
                todo!()
            }
            CrudExpr::Update { .. } => {
                todo!()
            }
            CrudExpr::Delete { .. } => {
                todo!()
            }
            CrudExpr::CreateTable { .. } => {
                todo!()
            }
            CrudExpr::Drop { .. } => {
                todo!()
            }
            CrudExpr::SetVar { .. } => {
                todo!()
            }
            CrudExpr::ReadVar { .. } => {
                todo!()
            }
        }
    }
}