Skip to main content

rustdb/
run.rs

1use crate::*;
2
3/// Instruction.
4#[non_exhaustive]
5pub enum Instruction {
6    /// Push constant.
7    PushConst(Value),
8    /// Push expression.
9    PushValue(CExpPtr<Value>),
10    /// Push local variable.
11    PushLocal(usize),
12    /// Assign local variable.
13    PopToLocal(usize),
14    /// Jump.
15    Jump(usize),
16    /// Jump if false.
17    JumpIfFalse(usize, CExpPtr<bool>),
18    /// Call
19    Call(LRc<Function>),
20    /// Return from function.
21    Return,
22    /// Throw error.
23    Throw,
24    /// Execute string.
25    Execute,
26    /// Initialise FOR statement.
27    ForInit(usize, LBox<CTableExpression>),
28    /// Next iteration of FOR statement.
29    ForNext(usize, LBox<ForNextInfo>),
30    /// Initialise FOR statement ( sorted case ).
31    ForSortInit(usize, LBox<CFromExpression>),
32    /// Next iteration of FOR statement ( sorted case ).
33    ForSortNext(usize, LBox<(usize, usize, Assigns)>),
34    /// Data operation.
35    DataOp(LBox<DO>),
36    /// SELECT expression.
37    Select(LBox<CFromExpression>),
38    /// Set local variables from table.
39    Set(LBox<CFromExpression>),
40    // Special push instructions ( optimisations )
41    /// Push Integer expression.
42    PushInt(CExpPtr<i64>),
43    /// Push Float expression.
44    PushFloat(CExpPtr<f64>),
45    /// Push bool expression.
46    PushBool(CExpPtr<bool>),
47    // More optimisations.
48    /// Assign a local variable.
49    AssignLocal(usize, CExpPtr<Value>),
50    /// Append to a local variable.
51    AppendLocal(usize, CExpPtr<Value>),
52    /// Increment (+=) a local variable.
53    IncLocal(usize, CExpPtr<Value>),
54    /// Decrement (-=) a local variable.
55    DecLocal(usize, CExpPtr<Value>),
56}
57
58/// Compiled Function.
59#[non_exhaustive]
60pub struct Function {
61    /// Number of parameters.
62    pub param_count: usize,
63    /// Function return type.
64    pub return_type: DataType,
65    /// Types of local parameters/variables.
66    pub local_typ: LVec<DataType>,
67    /// Source SQL.
68    pub source: LRc<LString>,
69    /// List of instructions.
70    pub ilist: RefCell<LVec<Instruction>>, // Valid when compiled is true.
71    /// Has function been compiled.
72    pub compiled: Cell<bool>,
73}
74
75/// Compiled expression which yields type T when evaluated.
76pub trait CExp<T> {
77    /// Evaluate the compiled expression.
78    fn eval(&self, ee: &mut EvalEnv, data: &[u8]) -> T;
79}
80
81/// Pointer to [CExp].
82pub type CExpPtr<T> = LBox<dyn CExp<T>>;
83
84/// Function that compiles a builtin function call.
85#[derive(Clone, Copy)]
86#[non_exhaustive]
87pub enum CompileFunc {
88    /// Value result.
89    Value(fn(&Block, &mut [Expr]) -> CExpPtr<Value>),
90    /// Int result.
91    Int(fn(&Block, &mut [Expr]) -> CExpPtr<i64>),
92    /// Float result.
93    Float(fn(&Block, &mut [Expr]) -> CExpPtr<f64>),
94}
95
96/// Iterator that yields references to page data.
97pub type DataSource = LBox<dyn Iterator<Item = (PagePtr, usize)>>;
98
99/// State for FOR loop (non-sorted case).
100#[non_exhaustive]
101pub struct ForState {
102    /// Data source.
103    pub data_source: DataSource,
104}
105impl std::fmt::Debug for ForState {
106    fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
107        Ok(())
108    }
109}
110
111/// State for FOR loop (sorted case).
112#[non_exhaustive]
113pub struct ForSortState {
114    /// Currrent index into rows.
115    pub ix: usize,
116    /// Rows.
117    pub rows: LVec<LVec<Value>>,
118}
119impl std::fmt::Debug for ForSortState {
120    fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
121        Ok(())
122    }
123}
124
125/// Info for ForNext Inst.
126#[non_exhaustive]
127pub struct ForNextInfo {
128    /// FOR id.
129    pub for_id: usize,
130    /// Assigns.
131    pub assigns: Assigns,
132    /// Expressions.
133    pub exps: LVec<CExpPtr<Value>>,
134    /// WHERE expression.
135    pub wher: Option<CExpPtr<bool>>,
136}
137
138/// Compiled Table Expression.
139#[non_exhaustive]
140pub enum CTableExpression {
141    /// Base table.
142    Base(LRc<Table>),
143    /// Row identified by Id.
144    IdGet(LRc<Table>, CExpPtr<i64>),
145    /// Indexed rows.
146    IxGet(LRc<Table>, LVec<CExpPtr<Value>>, usize),
147    /// VALUE expressions.
148    Values(LVec<LVec<CExpPtr<Value>>>),
149}
150
151impl CTableExpression {
152    /// Get underlying table.
153    pub fn table(&self) -> LRc<Table> {
154        match self {
155            CTableExpression::Base(t) => t.clone(),
156            CTableExpression::IdGet(t, _) => t.clone(),
157            CTableExpression::IxGet(t, _, _) => t.clone(),
158            _ => panic!(),
159        }
160    }
161}
162
163/// Compiled From Expression.
164#[non_exhaustive]
165pub struct CFromExpression {
166    /// Column names.
167    pub colnames: LVec<LBox<str>>,
168    /// Assignments ( left hand side ).
169    pub assigns: Assigns,
170    /// Expressions.
171    pub exps: LVec<CExpPtr<Value>>,
172    /// FROM expression.
173    pub from: Option<CTableExpression>,
174    /// WHERE expression.
175    pub wher: Option<CExpPtr<bool>>,
176    /// ORDER BY expressions.
177    pub orderby: LVec<CExpPtr<Value>>,
178    /// DESC bits.
179    pub desc: LVec<bool>,
180}
181
182/// Database Operation
183#[non_exhaustive]
184pub enum DO {
185    /// Create Schema.
186    CreateSchema(LBox<str>),
187    /// Create Table.
188    CreateTable(ColInfo),
189    /// Create Index.
190    CreateIndex(IndexInfo),
191    /// Create Function.
192    CreateFunction(ObjRef, LRc<LString>, bool),
193    /// Alter Table.
194    AlterTable(ObjRef, LVec<AlterCol>),
195    /// Drop Schema.
196    DropSchema(LBox<str>),
197    /// Drop Table.
198    DropTable(ObjRef),
199    /// Drop Index.
200    DropIndex(ObjRef, LBox<str>),
201    /// Drop Function.
202    DropFunction(ObjRef),
203    /// Insert into Table.
204    Insert(LRc<Table>, LVec<usize>, CTableExpression),
205    /// Update Table rows.
206    Update(
207        LVec<(usize, CExpPtr<Value>)>,
208        CTableExpression,
209        Option<CExpPtr<bool>>,
210    ),
211    /// Delete Table rows.
212    Delete(CTableExpression, Option<CExpPtr<bool>>),
213}
214
215/// Actions for altering columns of a table.
216#[non_exhaustive]
217pub enum AlterCol {
218    /// Add column.
219    Add(LBox<str>, DataType),
220    /// Drop column.
221    Drop(LBox<str>),
222    /// Modify column.
223    Modify(LBox<str>, DataType),
224}