1use crate::models::{ASTNode, ExecutionContext, ExecutionResult, ParsedCode};
2use crate::services::database::DatabaseService;
3use anyhow::Result;
4use serde_json::Value;
5use tracing::{debug, info};
6
7pub struct Executor {
8 database: DatabaseService,
9}
10
11impl Executor {
12 pub fn new() -> Self {
13 Self {
14 database: DatabaseService::new(),
15 }
16 }
17
18 pub async fn execute(&self, parsed: &ParsedCode, context: &ExecutionContext) -> Result<ExecutionResult> {
19 info!("Executing parsed code with {} nodes", parsed.nodes.len());
20
21 let mut results = Vec::new();
22
23 for node in &parsed.nodes {
24 let result = self.execute_node(node, context).await?;
25 results.push(result);
26 }
27
28 Ok(ExecutionResult {
29 data: results,
30 metadata: serde_json::json!({
31 "execution_time": 0,
32 "nodes_processed": parsed.nodes.len(),
33 "success": true
34 }),
35 })
36 }
37
38 pub fn compile(&self, parsed: &ParsedCode) -> Result<String> {
39 debug!("Compiling parsed code to SQL");
40
41 let mut sql_statements = Vec::new();
42
43 for node in &parsed.nodes {
44 let sql = self.compile_node(node)?;
45 sql_statements.push(sql);
46 }
47
48 Ok(sql_statements.join(";\n") + ";")
49 }
50
51 async fn execute_node(&self, node: &ASTNode, context: &ExecutionContext) -> Result<Value> {
52 match node {
53 ASTNode::Create { entity, fields, line } => {
54 info!("Executing CREATE for entity: {}", entity);
55 let result = self.database.create(entity, fields, context).await?;
56 Ok(serde_json::json!({
57 "operation": "CREATE",
58 "entity": entity,
59 "result": result,
60 "line": line
61 }))
62 }
63
64 ASTNode::Read { entity, conditions, line } => {
65 info!("Executing READ for entity: {} with conditions: {:?}", entity, conditions);
66 let result = self.database.read(entity, conditions, context).await?;
67 Ok(serde_json::json!({
68 "operation": "READ",
69 "entity": entity,
70 "result": result,
71 "line": line
72 }))
73 }
74
75 ASTNode::Update { entity, fields, conditions, line } => {
76 info!("Executing UPDATE for entity: {} with conditions: {:?}", entity, conditions);
77 let result = self.database.update(entity, fields, conditions, context).await?;
78 Ok(serde_json::json!({
79 "operation": "UPDATE",
80 "entity": entity,
81 "result": result,
82 "line": line
83 }))
84 }
85
86 ASTNode::Delete { entity, conditions, line } => {
87 info!("Executing DELETE for entity: {} with conditions: {:?}", entity, conditions);
88 let result = self.database.delete(entity, conditions, context).await?;
89 Ok(serde_json::json!({
90 "operation": "DELETE",
91 "entity": entity,
92 "result": result,
93 "line": line
94 }))
95 }
96
97 ASTNode::Function { name, parameters, body, line } => {
98 info!("Executing FUNCTION: {} with parameters: {:?}", name, parameters);
99 let result = self.execute_function(name, parameters, body, context).await?;
100 Ok(serde_json::json!({
101 "operation": "FUNCTION",
102 "name": name,
103 "result": result,
104 "line": line
105 }))
106 }
107 }
108 }
109
110 fn compile_node(&self, node: &ASTNode) -> Result<String> {
111 match node {
112 ASTNode::Create { entity, fields, line: _ } => {
113 let field_list = fields.join(", ");
114 Ok(format!("INSERT INTO {} ({}) VALUES (?)", entity, field_list))
115 }
116
117 ASTNode::Read { entity, conditions, line: _ } => {
118 let where_clause = if conditions.is_empty() {
119 String::new()
120 } else {
121 format!(" WHERE {}", conditions.join(" AND "))
122 };
123 Ok(format!("SELECT * FROM {}{}", entity, where_clause))
124 }
125
126 ASTNode::Update { entity, fields, conditions, line: _ } => {
127 let set_clause = fields.join(", ");
128 let where_clause = if conditions.is_empty() {
129 String::new()
130 } else {
131 format!(" WHERE {}", conditions.join(" AND "))
132 };
133 Ok(format!("UPDATE {} SET {}{}", entity, set_clause, where_clause))
134 }
135
136 ASTNode::Delete { entity, conditions, line: _ } => {
137 let where_clause = if conditions.is_empty() {
138 String::new()
139 } else {
140 format!(" WHERE {}", conditions.join(" AND "))
141 };
142 Ok(format!("DELETE FROM {}{}", entity, where_clause))
143 }
144
145 ASTNode::Function { name, parameters, body: _, line: _ } => {
146 Ok(format!("-- Function: {} with parameters: {}", name, parameters.join(", ")))
147 }
148 }
149 }
150
151 async fn execute_function(&self, name: &str, parameters: &[String], body: &str, _context: &ExecutionContext) -> Result<Value> {
152 debug!("Executing function: {} with body: {}", name, body);
153
154 Ok(serde_json::json!({
156 "function_name": name,
157 "parameters": parameters,
158 "body": body,
159 "result": "executed"
160 }))
161 }
162}