Format results as JSON
(result: &QueryResult)
| 85 | |
| 86 | /// Format results as JSON |
| 87 | fn format_json(result: &QueryResult) -> String { |
| 88 | // Create a JSON-friendly representation |
| 89 | let mut json_obj = serde_json::json!({ |
| 90 | "status": "success", |
| 91 | "columns": result.variables, |
| 92 | "rows": result.rows.iter().map(|row| { |
| 93 | let mut row_map = serde_json::Map::new(); |
| 94 | for col in &result.variables { |
| 95 | let value = row.get_value(col) |
| 96 | .map(Self::value_to_json) |
| 97 | .unwrap_or(serde_json::Value::Null); |
| 98 | row_map.insert(col.clone(), value); |
| 99 | } |
| 100 | serde_json::Value::Object(row_map) |
| 101 | }).collect::<Vec<_>>(), |
| 102 | "rows_affected": result.rows.len(), |
| 103 | "execution_time_ms": result.execution_time_ms, |
| 104 | }); |
| 105 | |
| 106 | // Add warnings if any |
| 107 | if !result.warnings.is_empty() { |
| 108 | if let serde_json::Value::Object(ref mut map) = json_obj { |
| 109 | map.insert("warnings".to_string(), serde_json::json!(result.warnings)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | let json_result = json_obj; |
| 114 | |
| 115 | serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| { |
| 116 | "{\"status\": \"error\", \"error\": \"Could not serialize results to JSON\"}" |
| 117 | .to_string() |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | /// Format results as CSV |
| 122 | fn format_csv(result: &QueryResult) -> String { |