diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 80c40ab..fc4936a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ use eframe::egui; +use egui_extras::{Column, TableBuilder}; use postgres::{Client, NoTls}; struct MyApp { client: Option<Client>, tables: Vec<String>, selected_table: Option<String>, + loaded_table: Option<String>, + columns: Vec<String>, + rows: Vec<Vec<String>>, error: Option<String>, } @@ -14,6 +18,9 @@ impl Default for MyApp { client: None, tables: Vec::new(), selected_table: None, + loaded_table: None, + columns: Vec::new(), + rows: Vec::new(), error: None, }; app.connect_and_load_tables(); @@ -45,6 +52,24 @@ impl MyApp { Err(e) => self.error = Some(format!("Verbindungsfehler: {e}")), } } + fn load_table_data(&mut self, table: &str) { + if let Some(client) = &mut self.client { + let query = format!("SELECT * FROM \"{table}\" LIMIT 100"); + match client.query(&query, &[]) { + Ok(rows) => { + self.columns = rows + .first() + .map(|r| r.columns().iter().map(|c| c.name().to_string()).collect()) + .unwrap_or_default(); + self.rows = rows + .iter() + .map(|r| (0..r.len()).map(|i| value_to_string(r, i)).collect()) + .collect(); + } + Err(e) => self.error = Some(format!("Ladefehler: {e}")), + } + } + } } impl eframe::App for MyApp { @@ -64,10 +89,58 @@ impl eframe::App for MyApp { ui.selectable_value(&mut self.selected_table, Some(table.clone()), table); } }); + + if self.selected_table != self.loaded_table { + if let Some(table) = self.selected_table.clone() { + self.load_table_data(&table); + self.loaded_table = Some(table); + } + } + + ui.separator(); + + TableBuilder::new(ui) + .columns(Column::auto(), self.columns.len()) + .header(20.0, |mut header| { + for col in &self.columns { + header.col(|ui| { + ui.strong(col); + }); + } + }) + .body(|mut body| { + for row in &self.rows { + body.row(18.0, |mut table_row| { + for cell in row { + table_row.col(|ui| { + ui.label(cell); + }); + } + }); + } + }); }); } } +use postgres::Row; + +fn value_to_string(row: &Row, idx: usize) -> String { + if let Ok(v) = row.try_get::<_, Option<String>>(idx) { + return v.unwrap_or_else(|| "NULL".into()); + } + if let Ok(v) = row.try_get::<_, Option<i32>>(idx) { + return v.map(|x| x.to_string()).unwrap_or_else(|| "NULL".into()); + } + if let Ok(v) = row.try_get::<_, Option<i64>>(idx) { + return v.map(|x| x.to_string()).unwrap_or_else(|| "NULL".into()); + } + if let Ok(v) = row.try_get::<_, Option<bool>>(idx) { + return v.map(|x| x.to_string()).unwrap_or_else(|| "NULL".into()); + } + "?".to_string() +} + fn main() -> eframe::Result<()> { eframe::run_native( "Postgres Browser", |
