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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright 2025 Oxibase Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::ast::{BlockStatement, PlSqlStatement};
use super::env::Environment;
use crate::core::{Error, Result, Value};
use crate::functions::FunctionRegistry;
use std::sync::Arc;
pub struct PlSqlInterpreter<'a> {
pub(crate) _function_registry: Arc<FunctionRegistry>,
runner: Option<&'a dyn crate::functions::backends::SqlRunner>,
}
impl<'a> PlSqlInterpreter<'a> {
pub fn new(
function_registry: Arc<FunctionRegistry>,
runner: Option<&'a dyn crate::functions::backends::SqlRunner>,
) -> Self {
Self {
_function_registry: function_registry,
runner,
}
}
pub fn execute(&self, block: &BlockStatement, env: &mut Environment) -> Result<()> {
for stmt in &block.statements {
match self.execute_statement(stmt, env) {
Ok(true) => return Ok(()), // RETURN statement executed
Ok(false) => continue,
Err(e) => return Err(e),
}
}
Ok(())
}
fn eval_expr(&self, expr: &crate::parser::ast::Expression, env: &Environment) -> Result<Value> {
// For the minimal MVP to make the tests pass:
// We will do simple matching on standard AST nodes
use crate::parser::ast::Expression;
match expr {
Expression::BooleanLiteral(b) => Ok(Value::Boolean(b.value)),
Expression::IntegerLiteral(i) => Ok(Value::Integer(i.value)),
Expression::StringLiteral(s) => Ok(Value::Text(std::sync::Arc::from(s.value.clone()))),
Expression::Identifier(id) => {
if let Some(val) = env.get(&id.value) {
Ok(val.clone())
} else {
Err(Error::internal(format!("Variable not found: {}", id.value)))
}
}
Expression::Infix(comp) => {
let left = self.eval_expr(&comp.left, env)?;
let right = self.eval_expr(&comp.right, env)?;
match comp.operator.as_str() {
"<" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Boolean(l < r))
} else {
Ok(Value::Boolean(false))
}
}
"<=" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Boolean(l <= r))
} else {
Ok(Value::Boolean(false))
}
}
">" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Boolean(l > r))
} else {
Ok(Value::Boolean(false))
}
}
">=" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Boolean(l >= r))
} else {
Ok(Value::Boolean(false))
}
}
"=" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Boolean(l == r))
} else {
Ok(Value::Boolean(false))
}
}
"!=" | "<>" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Boolean(l != r))
} else {
Ok(Value::Boolean(false))
}
}
"+" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Integer(l + r))
} else {
Err(Error::internal(
"Addition supported only for integers in MVP",
))
}
}
"-" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Integer(l - r))
} else {
Err(Error::internal(
"Subtraction supported only for integers in MVP",
))
}
}
"*" => {
if let (Value::Integer(l), Value::Integer(r)) = (&left, &right) {
Ok(Value::Integer(l * r))
} else {
Err(Error::internal(
"Multiplication supported only for integers in MVP",
))
}
}
_ => Err(Error::internal(
"Operator not implemented in PL/SQL interpreter yet",
)),
}
}
_ => Err(Error::internal(format!(
"Expression type not fully supported in simple PL/SQL interpreter: {:?}",
expr
))),
}
}
fn substitute_variables_in_statement(
&self,
stmt: &mut crate::parser::ast::Statement,
env: &Environment,
) {
match stmt {
crate::parser::ast::Statement::Insert(insert) => {
if let Some(select) = &mut insert.select {
self.substitute_variables_in_select(select, env);
} else {
for row in &mut insert.values {
for expr in row {
self.substitute_variables_in_expr(expr, env);
}
}
}
}
crate::parser::ast::Statement::Update(update) => {
for expr in update.updates.values_mut() {
self.substitute_variables_in_expr(expr, env);
}
if let Some(where_expr) = &mut update.where_clause {
self.substitute_variables_in_expr(where_expr, env);
}
}
crate::parser::ast::Statement::Delete(delete) => {
if let Some(where_expr) = &mut delete.where_clause {
self.substitute_variables_in_expr(where_expr, env);
}
}
crate::parser::ast::Statement::Select(select) => {
self.substitute_variables_in_select(select, env);
}
_ => {} // Other statements (DDL) typically don't have expressions with variables
}
}
fn substitute_variables_in_select(
&self,
select: &mut crate::parser::ast::SelectStatement,
env: &Environment,
) {
// Substitute in SELECT expressions
for col in &mut select.columns {
self.substitute_variables_in_expr(col, env);
}
// Substitute in WHERE clause
if let Some(where_clause) = &mut select.where_clause {
self.substitute_variables_in_expr(where_clause, env);
}
// Substitute in HAVING clause
if let Some(having_clause) = &mut select.having {
self.substitute_variables_in_expr(having_clause, env);
}
// LIMIT and OFFSET
if let Some(limit) = &mut select.limit {
self.substitute_variables_in_expr(limit, env);
}
if let Some(offset) = &mut select.offset {
self.substitute_variables_in_expr(offset, env);
}
}
fn substitute_variables_in_expr(
&self,
expr: &mut crate::parser::ast::Expression,
env: &Environment,
) {
use crate::parser::ast::Expression;
// Recursively substitute
match expr {
Expression::Infix(infix) => {
self.substitute_variables_in_expr(&mut infix.left, env);
self.substitute_variables_in_expr(&mut infix.right, env);
}
Expression::Prefix(prefix) => {
self.substitute_variables_in_expr(&mut prefix.right, env);
}
Expression::FunctionCall(fc) => {
for arg in &mut fc.arguments {
self.substitute_variables_in_expr(arg, env);
}
}
Expression::Identifier(id) => {
if let Some(val) = env.get(&id.value) {
// Found a match! We need to replace the identifier with a literal
match val {
Value::Integer(i) => {
*expr =
Expression::IntegerLiteral(crate::parser::ast::IntegerLiteral {
token: crate::parser::token::Token::new(
crate::parser::token::TokenType::Integer,
i.to_string(),
crate::parser::token::Position::default(),
),
value: *i,
});
}
Value::Text(s) => {
*expr = Expression::StringLiteral(crate::parser::ast::StringLiteral {
token: crate::parser::token::Token::new(
crate::parser::token::TokenType::String,
format!("'{}'", s),
crate::parser::token::Position::default(),
),
value: s.to_string(),
type_hint: None,
});
}
Value::Boolean(b) => {
*expr =
Expression::BooleanLiteral(crate::parser::ast::BooleanLiteral {
token: crate::parser::token::Token::new(
crate::parser::token::TokenType::Keyword,
if *b {
"TRUE".to_string()
} else {
"FALSE".to_string()
},
crate::parser::token::Position::default(),
),
value: *b,
});
}
_ => {}
}
}
}
// Add other nested expressions as needed
_ => {}
}
}
fn execute_statement(&self, stmt: &PlSqlStatement, env: &mut Environment) -> Result<bool> {
match stmt {
PlSqlStatement::Declare(decl) => {
for v in &decl.declarations {
let mut initial_val = Value::Null(crate::core::DataType::Null);
if let Some(expr) = &v.default_value {
initial_val = self.eval_expr(expr, env)?;
println!("Declaring {} = {:?}", v.name, initial_val);
} else {
// Very basic default initialization based on type name
let ty = v.data_type.to_uppercase();
if ty.contains("INT") {
initial_val = Value::Integer(0);
} else if ty.contains("BOOL") {
initial_val = Value::Boolean(false);
} else if ty.contains("TEXT")
|| ty.contains("VARCHAR")
|| ty.contains("CHAR")
{
initial_val = Value::Text(std::sync::Arc::from(String::new()));
}
}
if env.assign(&v.name, initial_val.clone()).is_err() {
env.define_global(&v.name, initial_val);
}
}
Ok(false)
}
PlSqlStatement::Block(block) => {
// If it is an explicit inner block, push frame. If root block of procedure, we should probably not,
// but since assign updates outer frames, it is fine.
env.push_frame("block");
let res = self.execute(block, env);
env.pop_frame();
res?;
Ok(false)
}
PlSqlStatement::Assignment(assign) => {
let val = self.eval_expr(&assign.expression, env)?;
println!("Evaluating assign: {} = {:?}", assign.variable, val);
// Variables bound from CALL are actually defined globally in the env by backend.
// Assignment updates them correctly. If not found, fall back to current frame.
if env.assign(&assign.variable, val.clone()).is_err() {
env.define(&assign.variable, val);
}
Ok(false)
}
PlSqlStatement::If(if_stmt) => {
let condition_val = self.eval_expr(&if_stmt.condition, env)?;
let is_true = match condition_val {
Value::Boolean(b) => b,
_ => false, // Simplification
};
if is_true {
env.push_frame("if_then");
for stmt in &if_stmt.then_block {
if self.execute_statement(stmt, env)? {
env.pop_frame();
return Ok(true);
}
}
env.pop_frame();
} else if let Some(else_block) = &if_stmt.else_block {
env.push_frame("if_else");
for stmt in else_block {
if self.execute_statement(stmt, env)? {
env.pop_frame();
return Ok(true);
}
}
env.pop_frame();
}
Ok(false)
}
PlSqlStatement::While(while_stmt) => {
env.push_frame("while");
loop {
let condition_val = self.eval_expr(&while_stmt.condition, env)?;
let is_true = match condition_val {
Value::Boolean(b) => b,
_ => false,
};
if !is_true {
break;
}
for stmt in &while_stmt.block {
if self.execute_statement(stmt, env)? {
env.pop_frame();
return Ok(true);
}
}
}
env.pop_frame();
Ok(false)
}
PlSqlStatement::Sql(box_stmt) => {
println!("Executing SQL statement in plsql: {:?}", box_stmt);
if let Some(runner) = self.runner {
let mut modified_stmt = *box_stmt.clone();
self.substitute_variables_in_statement(&mut modified_stmt, env);
// Note: for queries that modify data, we should also track ROW_COUNT,
// but for now we'll just execute it.
runner.execute_ast(&modified_stmt)?;
Ok(false)
} else {
Err(Error::internal(
"Cannot execute SQL statement: No SqlRunner bridge provided",
))
}
}
PlSqlStatement::Return(_) => Ok(true),
PlSqlStatement::Commit(_) => {
if let Some(runner) = self.runner {
runner.commit()?;
Ok(false)
} else {
Err(Error::internal(
"Cannot execute COMMIT: No SqlRunner bridge provided",
))
}
}
PlSqlStatement::Rollback(_) => {
if let Some(runner) = self.runner {
runner.rollback()?;
Ok(false)
} else {
Err(Error::internal(
"Cannot execute ROLLBACK: No SqlRunner bridge provided",
))
}
}
PlSqlStatement::BeginTransaction(_) => {
if let Some(runner) = self.runner {
runner.begin()?;
Ok(false)
} else {
Err(Error::internal(
"Cannot execute BEGIN: No SqlRunner bridge provided",
))
}
}
}
}
}