vibesql_storage/database/
debug.rs

1// ============================================================================
2// Database Debug and Utility Functions
3// ============================================================================
4
5use super::core::Database;
6use crate::StorageError;
7
8impl Database {
9    // ============================================================================
10    // Debug and Utilities
11    // ============================================================================
12
13    /// Get debug information about database state
14    pub fn debug_info(&self) -> String {
15        let mut output = String::new();
16        output.push_str("=== Database Debug Info ===\n");
17        output.push_str(&format!("Tables: {}\n", self.list_tables().len()));
18        for table_name in self.list_tables() {
19            if let Some(table) = self.get_table(&table_name) {
20                output.push_str(&format!(
21                    "  - {} ({} rows, {} columns)\n",
22                    table_name,
23                    table.row_count(),
24                    table.schema.column_count()
25                ));
26            }
27        }
28        output
29    }
30
31    /// Dump all table contents in readable format
32    pub fn dump_tables(&self) -> String {
33        let mut output = String::new();
34        for table_name in self.list_tables() {
35            if let Ok(dump) = self.dump_table(&table_name) {
36                output.push_str(&dump);
37                output.push('\n');
38            }
39        }
40        output
41    }
42
43    /// Dump a specific table's contents
44    pub fn dump_table(&self, name: &str) -> Result<String, StorageError> {
45        let table =
46            self.get_table(name).ok_or_else(|| StorageError::TableNotFound(name.to_string()))?;
47
48        let mut output = String::new();
49        output.push_str(&format!("=== Table: {} ===\n", name));
50
51        let col_names: Vec<String> = table.schema.columns.iter().map(|c| c.name.clone()).collect();
52        output.push_str(&format!("{}\n", col_names.join(" | ")));
53        output.push_str(&format!("{}\n", "-".repeat(col_names.join(" | ").len())));
54
55        for row in table.scan() {
56            let values: Vec<String> = row.values.iter().map(|v| format!("{}", v)).collect();
57            output.push_str(&format!("{}\n", values.join(" | ")));
58        }
59
60        output.push_str(&format!("({} rows)\n", table.row_count()));
61        Ok(output)
62    }
63}