Execute FOR statement: FOR [alias:] variable IN expression
(
&self,
for_stmt: &crate::ast::ForStatement,
context: &mut ExecutionContext,
)
| 7148 | |
| 7149 | /// Execute FOR statement: FOR [alias:] variable IN expression |
| 7150 | fn execute_for_statement( |
| 7151 | &self, |
| 7152 | for_stmt: &crate::ast::ForStatement, |
| 7153 | context: &mut ExecutionContext, |
| 7154 | ) -> Result<QueryResult, ExecutionError> { |
| 7155 | // FOR statements iterate over a collection |
| 7156 | // The expression should evaluate to a list or collection |
| 7157 | |
| 7158 | let mut result = QueryResult::new(); |
| 7159 | |
| 7160 | // Evaluate the expression to get the collection |
| 7161 | let collection_value = self.evaluate_expression(&for_stmt.expression, context)?; |
| 7162 | |
| 7163 | // Convert the value to a collection |
| 7164 | let collection = match collection_value { |
| 7165 | Value::List(items) => items, |
| 7166 | Value::String(s) => { |
| 7167 | // String can be treated as a collection of characters |
| 7168 | s.chars().map(|c| Value::String(c.to_string())).collect() |
| 7169 | } |
| 7170 | _ => { |
| 7171 | // Single value becomes a collection of one |
| 7172 | vec![collection_value] |
| 7173 | } |
| 7174 | }; |
| 7175 | |
| 7176 | // Set up the result variable name |
| 7177 | let variable_name = for_stmt.alias.as_ref().unwrap_or(&for_stmt.variable); |
| 7178 | result.variables.push(variable_name.clone()); |
| 7179 | |
| 7180 | // Create a row for each item in the collection |
| 7181 | for item in collection { |
| 7182 | let mut row = Row::new(); |
| 7183 | row.add_value(variable_name.clone(), item.clone()); |
| 7184 | row.positional_values.push(item); |
| 7185 | result.rows.push(row); |
| 7186 | } |
| 7187 | |
| 7188 | Ok(result) |
| 7189 | } |
| 7190 | |
| 7191 | /// Execute FILTER statement: FILTER [WHERE] expression |
| 7192 | fn execute_filter_statement( |
no test coverage detected