mirror of https://github.com/TaKO8Ki/gobang
Refactor handlers (#3)
* fix workflows * remove a unused function * initialize record table state every time specifying table * add a crates.io badge * close pool if exists * remove unneeded lines * change gobang.gif * fix layout * update gobang.gif * refactor `utils::get_records` * refactor handlers * change gobang.gif * fix clippy warningspull/8/head
parent
63e96b6aed
commit
4b26b71a38
Binary file not shown.
Before Width: | Height: | Size: 8.5 MiB After Width: | Height: | Size: 5.4 MiB |
@ -0,0 +1,34 @@
|
|||||||
|
use crate::app::{App, Database, FocusBlock};
|
||||||
|
use crate::event::Key;
|
||||||
|
use crate::utils::get_databases;
|
||||||
|
use sqlx::mysql::MySqlPool;
|
||||||
|
|
||||||
|
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||||
|
match key {
|
||||||
|
Key::Char('j') => app.next_connection(),
|
||||||
|
Key::Char('k') => app.previous_connection(),
|
||||||
|
Key::Enter => {
|
||||||
|
app.selected_database.select(Some(0));
|
||||||
|
app.selected_table.select(Some(0));
|
||||||
|
if let Some(conn) = app.selected_connection() {
|
||||||
|
if let Some(pool) = app.pool.as_ref() {
|
||||||
|
pool.close().await;
|
||||||
|
}
|
||||||
|
let pool = MySqlPool::connect(conn.database_url().as_str()).await?;
|
||||||
|
app.pool = Some(pool);
|
||||||
|
app.focus_type = FocusBlock::DabataseList(false);
|
||||||
|
}
|
||||||
|
app.databases = match app.selected_connection() {
|
||||||
|
Some(conn) => match &conn.database {
|
||||||
|
Some(database) => {
|
||||||
|
vec![Database::new(database.clone(), app.pool.as_ref().unwrap()).await?]
|
||||||
|
}
|
||||||
|
None => get_databases(app.pool.as_ref().unwrap()).await?,
|
||||||
|
},
|
||||||
|
None => vec![],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
use crate::app::{App, FocusType};
|
|
||||||
use crate::event::Key;
|
|
||||||
use sqlx::mysql::MySqlPool;
|
|
||||||
|
|
||||||
pub async fn handler(_key: Key, app: &mut App) -> anyhow::Result<()> {
|
|
||||||
if let Some(conn) = app.selected_connection() {
|
|
||||||
app.pool.as_ref().unwrap().close().await;
|
|
||||||
let pool = MySqlPool::connect(conn.database_url().as_str()).await?;
|
|
||||||
app.pool = Some(pool);
|
|
||||||
app.focus_type = FocusType::Dabatases(true);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -1,16 +1,22 @@
|
|||||||
use crate::app::{App, Database};
|
use crate::app::{App, FocusBlock};
|
||||||
use crate::event::Key;
|
use crate::event::Key;
|
||||||
use crate::utils::get_databases;
|
|
||||||
|
|
||||||
pub async fn handler(_key: Key, app: &mut App) -> anyhow::Result<()> {
|
pub async fn handler(key: Key, app: &mut App, focused: bool) -> anyhow::Result<()> {
|
||||||
app.databases = match app.selected_connection() {
|
if focused {
|
||||||
Some(conn) => match &conn.database {
|
match key {
|
||||||
Some(database) => {
|
Key::Char('j') => app.next_database(),
|
||||||
vec![Database::new(database.clone(), app.pool.as_ref().unwrap()).await?]
|
Key::Char('k') => app.previous_database(),
|
||||||
}
|
Key::Esc => app.focus_type = FocusBlock::DabataseList(false),
|
||||||
None => get_databases(app.pool.as_ref().unwrap()).await?,
|
_ => (),
|
||||||
},
|
}
|
||||||
None => vec![],
|
} else {
|
||||||
};
|
match key {
|
||||||
|
Key::Char('j') => app.focus_type = FocusBlock::TableList(false),
|
||||||
|
Key::Char('l') => app.focus_type = FocusBlock::RecordTable(false),
|
||||||
|
Key::Char('c') => app.focus_type = FocusBlock::ConnectionList,
|
||||||
|
Key::Enter => app.focus_type = FocusBlock::DabataseList(true),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
use crate::app::App;
|
||||||
|
use crate::event::Key;
|
||||||
|
use crate::utils::convert_column_value_to_string;
|
||||||
|
use futures::TryStreamExt;
|
||||||
|
use regex::Regex;
|
||||||
|
use sqlx::Row;
|
||||||
|
|
||||||
|
pub async fn handler(_key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||||
|
app.query = app.input.drain(..).collect();
|
||||||
|
let re = Regex::new(r"select .+ from (.+)").unwrap();
|
||||||
|
if let Some(caps) = re.captures(app.query.as_str()) {
|
||||||
|
let mut rows = sqlx::query(app.query.as_str()).fetch(app.pool.as_ref().unwrap());
|
||||||
|
let headers = sqlx::query(format!("desc `{}`", caps.get(1).unwrap().as_str()).as_str())
|
||||||
|
.fetch_all(app.pool.as_ref().unwrap())
|
||||||
|
.await?
|
||||||
|
.iter()
|
||||||
|
.map(|table| table.get(0))
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
let mut records = vec![];
|
||||||
|
while let Some(row) = rows.try_next().await? {
|
||||||
|
records.push(
|
||||||
|
row.columns()
|
||||||
|
.iter()
|
||||||
|
.map(|col| convert_column_value_to_string(&row, col))
|
||||||
|
.collect::<Vec<String>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
app.record_table.headers = headers;
|
||||||
|
app.record_table.rows = records;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
use crate::app::{App, FocusBlock};
|
||||||
|
use crate::event::Key;
|
||||||
|
use crate::utils::get_records;
|
||||||
|
|
||||||
|
pub async fn handler(key: Key, app: &mut App, focused: bool) -> anyhow::Result<()> {
|
||||||
|
if focused {
|
||||||
|
match key {
|
||||||
|
Key::Char('j') => {
|
||||||
|
if app.selected_database.selected().is_some() {
|
||||||
|
app.record_table.column_index = 0;
|
||||||
|
app.next_table();
|
||||||
|
if let Some(database) = app.selected_database() {
|
||||||
|
if let Some(table) = app.selected_table() {
|
||||||
|
let (headers, records) =
|
||||||
|
get_records(database, table, app.pool.as_ref().unwrap()).await?;
|
||||||
|
app.record_table.state.select(Some(0));
|
||||||
|
app.record_table.headers = headers;
|
||||||
|
app.record_table.rows = records;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Char('k') => {
|
||||||
|
if app.selected_database.selected().is_some() {
|
||||||
|
app.record_table.column_index = 0;
|
||||||
|
app.previous_table();
|
||||||
|
if let Some(database) = app.selected_database() {
|
||||||
|
if let Some(table) = app.selected_table() {
|
||||||
|
let (headers, records) =
|
||||||
|
get_records(database, table, app.pool.as_ref().unwrap()).await?;
|
||||||
|
app.record_table.state.select(Some(0));
|
||||||
|
app.record_table.headers = headers;
|
||||||
|
app.record_table.rows = records;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Esc => app.focus_type = FocusBlock::TableList(false),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match key {
|
||||||
|
Key::Char('k') => app.focus_type = FocusBlock::DabataseList(false),
|
||||||
|
Key::Char('l') => app.focus_type = FocusBlock::RecordTable(false),
|
||||||
|
Key::Char('c') => app.focus_type = FocusBlock::ConnectionList,
|
||||||
|
Key::Enter => app.focus_type = FocusBlock::TableList(true),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue