add useful functions

pull/1/head
Takayuki Maeda 3 years ago
parent cf23d24ed6
commit 82b228148e

@ -216,4 +216,24 @@ impl<'a> App<'a> {
self.selected_connection.select(Some(i));
}
}
pub fn selected_database(&self) -> Option<&mut Database> {
match self.selected_database.selected() {
Some(i) => match self.databases.get(i) {
Some(db) => Some(&mut db),
None => None,
},
None => None,
}
}
pub fn selected_table(&self) -> Option<&Table> {
match self.selected_database() {
Some(db) => match db.selected_table.selected() {
Some(i) => db.tables.get(i),
None => None,
},
None => None,
}
}
}

@ -4,16 +4,12 @@ use crate::utils::get_records;
use sqlx::mysql::MySqlPool;
pub async fn handler<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
let selected_database = app
.databases
.get(app.selected_database.selected().unwrap())
.unwrap();
let selected_table = selected_database
.tables
.get(selected_database.selected_table.selected().unwrap())
.unwrap();
let (headers, records) = get_records(selected_database, selected_table, pool).await?;
app.record_table.headers = headers;
app.record_table.rows = records;
if let Some(database) = app.selected_database() {
if let Some(table) = app.selected_table() {
let (headers, records) = get_records(database, table, pool).await?;
app.record_table.headers = headers;
app.record_table.rows = records;
}
}
Ok(())
}

@ -5,7 +5,7 @@ mod ui;
mod user_config;
mod utils;
use crate::app::{Database, FocusType, InputMode, Table};
use crate::app::{FocusType, InputMode, Table};
use crate::event::{Event, Key};
use crate::handlers::handle_app;
use crossterm::{

Loading…
Cancel
Save