Format results as a table using comfy-table
(result: &QueryResult)
| 22 | |
| 23 | /// Format results as a table using comfy-table |
| 24 | fn format_table(result: &QueryResult) -> String { |
| 25 | // Check if this is a session command |
| 26 | if result.is_session_command() { |
| 27 | if let Some(msg) = result.get_session_message() { |
| 28 | return format!("{}\n", msg.to_string().green()); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if result.rows.is_empty() { |
| 33 | return format!("{}\n", "No results found".yellow()); |
| 34 | } |
| 35 | |
| 36 | let mut output = String::new(); |
| 37 | |
| 38 | // Header |
| 39 | output.push_str(&format!("{}\n", "Query Results".bold().green())); |
| 40 | output.push_str(&format!( |
| 41 | "Execution time: {} ms\n", |
| 42 | result.execution_time_ms |
| 43 | )); |
| 44 | output.push_str(&format!("Rows returned: {}\n\n", result.rows.len())); |
| 45 | |
| 46 | // Create table |
| 47 | let mut table = Table::new(); |
| 48 | table.load_preset(UTF8_FULL); |
| 49 | |
| 50 | // Table header |
| 51 | let header_cells: Vec<Cell> = result |
| 52 | .variables |
| 53 | .iter() |
| 54 | .map(|col| Cell::new(col).fg(Color::Green)) |
| 55 | .collect(); |
| 56 | table.set_header(header_cells); |
| 57 | |
| 58 | // Table rows |
| 59 | for row in &result.rows { |
| 60 | let row_values: Vec<String> = result |
| 61 | .variables |
| 62 | .iter() |
| 63 | .map(|col| { |
| 64 | row.get_value(col) |
| 65 | .map(Self::value_to_string) |
| 66 | .unwrap_or_else(|| "NULL".to_string()) |
| 67 | }) |
| 68 | .collect(); |
| 69 | table.add_row(row_values); |
| 70 | } |
| 71 | |
| 72 | output.push_str(&table.to_string()); |
| 73 | output.push('\n'); |
| 74 | |
| 75 | // Display warnings if any |
| 76 | if !result.warnings.is_empty() { |
| 77 | output.push_str(&format!("\n{}\n", "Warnings:".bold().yellow())); |
| 78 | for (i, warning) in result.warnings.iter().enumerate() { |
| 79 | output.push_str(&format!(" {}. {}\n", i + 1, warning.yellow())); |
| 80 | } |
| 81 | } |
nothing calls this directly
no test coverage detected