Skip to main content

systemprompt_database/services/
display.rs

1#![allow(clippy::print_stdout)]
2
3use crate::models::{ColumnInfo, DatabaseInfo, QueryResult, TableInfo};
4
5pub trait DatabaseCliDisplay {
6    fn display_with_cli(&self);
7}
8
9impl DatabaseCliDisplay for Vec<TableInfo> {
10    fn display_with_cli(&self) {
11        if self.is_empty() {
12            println!("No tables found");
13        } else {
14            println!("Tables:");
15            for table in self {
16                println!("  {} (rows: {})", table.name, table.row_count);
17            }
18        }
19    }
20}
21
22impl DatabaseCliDisplay for (Vec<ColumnInfo>, i64) {
23    fn display_with_cli(&self) {
24        let (columns, _) = self;
25        println!("Columns:");
26        for col in columns {
27            let default_display = col
28                .default
29                .as_deref()
30                .map_or_else(String::new, |d| format!("DEFAULT {d}"));
31
32            println!(
33                "  {} {} {} {} {}",
34                col.name,
35                col.data_type,
36                if col.nullable { "NULL" } else { "NOT NULL" },
37                if col.primary_key { "PK" } else { "" },
38                default_display
39            );
40        }
41    }
42}
43
44impl DatabaseCliDisplay for DatabaseInfo {
45    fn display_with_cli(&self) {
46        println!("Database Info:");
47        println!("  Path: {}", self.path);
48        println!("  Version: {}", self.version);
49        println!("  Tables: {}", self.tables.len());
50    }
51}
52
53impl DatabaseCliDisplay for QueryResult {
54    fn display_with_cli(&self) {
55        if self.columns.is_empty() {
56            println!("No data returned");
57            return;
58        }
59
60        println!("{}", self.columns.join(" | "));
61        println!("{}", "-".repeat(80));
62
63        for row in &self.rows {
64            let values: Vec<String> = self
65                .columns
66                .iter()
67                .map(|col| {
68                    row.get(col).map_or_else(
69                        || "NULL".to_string(),
70                        |v| match v {
71                            serde_json::Value::String(s) => s.clone(),
72                            serde_json::Value::Null => "NULL".to_string(),
73                            serde_json::Value::Bool(_)
74                            | serde_json::Value::Number(_)
75                            | serde_json::Value::Array(_)
76                            | serde_json::Value::Object(_) => v.to_string(),
77                        },
78                    )
79                })
80                .collect();
81            println!("{}", values.join(" | "));
82        }
83
84        println!(
85            "\n{} rows returned in {}ms",
86            self.row_count, self.execution_time_ms
87        );
88    }
89}