Execute DECLARE statement to define local variables with type specifications Internal method for declare statements
(
&self,
declare_stmt: &DeclareStatement,
context: &ExecutionContext,
)
| 8538 | /// Execute DECLARE statement to define local variables with type specifications |
| 8539 | /// Internal method for declare statements |
| 8540 | fn execute_declare_statement( |
| 8541 | &self, |
| 8542 | declare_stmt: &DeclareStatement, |
| 8543 | context: &ExecutionContext, |
| 8544 | ) -> Result<QueryResult, ExecutionError> { |
| 8545 | // For now, simulate variable declaration by storing in session state |
| 8546 | let mut declared_vars = Vec::new(); |
| 8547 | |
| 8548 | for var_decl in &declare_stmt.variable_declarations { |
| 8549 | let initial_val = match &var_decl.initial_value { |
| 8550 | Some(expr) => { |
| 8551 | // Evaluate the expression using the provided context so it can reference existing variables |
| 8552 | self.evaluate_expression(expr, context)? |
| 8553 | } |
| 8554 | None => { |
| 8555 | // Use type-appropriate default value |
| 8556 | match var_decl.type_spec { |
| 8557 | TypeSpec::Integer => Value::Number(0.0), |
| 8558 | TypeSpec::String { .. } => Value::String("".to_string()), |
| 8559 | TypeSpec::Boolean => Value::Boolean(false), |
| 8560 | _ => Value::Null, |
| 8561 | } |
| 8562 | } |
| 8563 | }; |
| 8564 | |
| 8565 | // Store variable in session context |
| 8566 | self.set_session_parameter(&var_decl.variable_name, initial_val)?; |
| 8567 | |
| 8568 | declared_vars.push(format!( |
| 8569 | "{}: {} = {:?}", |
| 8570 | var_decl.variable_name, |
| 8571 | var_decl.type_spec, |
| 8572 | var_decl |
| 8573 | .initial_value |
| 8574 | .as_ref() |
| 8575 | .map(|_| "initialized") |
| 8576 | .unwrap_or("default") |
| 8577 | )); |
| 8578 | } |
| 8579 | |
| 8580 | // Return success result |
| 8581 | let rows_count = declared_vars.len(); |
| 8582 | Ok(QueryResult { |
| 8583 | variables: vec!["variable_declaration".to_string()], |
| 8584 | rows: declared_vars |
| 8585 | .into_iter() |
| 8586 | .map(|var| { |
| 8587 | let mut values = std::collections::HashMap::new(); |
| 8588 | values.insert("variable_declaration".to_string(), Value::String(var)); |
| 8589 | Row::from_values(values) |
| 8590 | }) |
| 8591 | .collect(), |
| 8592 | rows_affected: rows_count, |
| 8593 | session_result: None, |
| 8594 | warnings: Vec::new(), |
| 8595 | |
| 8596 | execution_time_ms: 0, |
| 8597 | }) |
no test coverage detected