implement some handlers

pull/1/head
Takayuki Maeda 3 years ago
parent d88a79f0a0
commit a061efc3bd

2
.gitignore vendored

@ -1 +1,3 @@
/target
gobang
gobang.yml

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 MiB

@ -8,3 +8,4 @@ port = 3306
user = "root"
host = "localhost"
port = 3306
database = "world"

@ -1,4 +1,7 @@
use crate::{user_config::UserConfig, utils::get_tables};
use crate::{
user_config::{Connection, UserConfig},
utils::get_tables,
};
use sqlx::mysql::MySqlPool;
use tui::widgets::{ListState, TableState};
@ -124,7 +127,7 @@ impl Database {
}
}
pub struct App<'a> {
pub struct App {
pub input: String,
pub input_mode: InputMode,
pub messages: Vec<Vec<String>>,
@ -134,11 +137,11 @@ pub struct App<'a> {
pub focus_type: FocusType,
pub user_config: Option<UserConfig>,
pub selected_connection: ListState,
pub pool: Option<&'a MySqlPool>,
pub pool: Option<MySqlPool>,
}
impl<'a> Default for App<'a> {
fn default() -> App<'a> {
impl Default for App {
fn default() -> App {
App {
input: String::new(),
input_mode: InputMode::Normal,
@ -154,7 +157,7 @@ impl<'a> Default for App<'a> {
}
}
impl<'a> App<'a> {
impl App {
pub fn next_database(&mut self) {
let i = match self.selected_database.selected() {
Some(i) => {
@ -234,4 +237,14 @@ impl<'a> App<'a> {
None => None,
}
}
pub fn selected_connection(&self) -> Option<&Connection> {
match &self.user_config {
Some(config) => match self.selected_connection.selected() {
Some(i) => config.conn.get(i),
None => None,
},
None => None,
}
}
}

@ -0,0 +1,13 @@
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,11 +1,16 @@
use crate::app::App;
use crate::app::{App, Database};
use crate::event::Key;
use crate::utils::get_databases;
use sqlx::mysql::MySqlPool;
pub async fn handler<'a>(_key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
for db in get_databases(pool).await? {
app.databases.push(db)
}
pub async fn handler(_key: Key, app: &mut App) -> anyhow::Result<()> {
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,11 +1,11 @@
pub mod create_connection;
pub mod database_list;
pub mod record_table;
use crate::app::{App, FocusType, InputMode};
use crate::event::Key;
use sqlx::mysql::MySqlPool;
pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
pub async fn handle_app(key: Key, app: &mut App) -> anyhow::Result<()> {
match app.input_mode {
InputMode::Normal => match key {
Key::Char('e') => {
@ -42,6 +42,7 @@ pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> an
Some(index) => {
app.record_table.column_index = 0;
app.databases[index].previous();
record_table::handler(key, app).await?;
}
None => (),
},
@ -55,14 +56,17 @@ pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> an
Some(index) => {
app.record_table.column_index = 0;
app.databases[index].next();
record_table::handler(key, app, pool).await?
record_table::handler(key, app).await?
}
None => (),
},
_ => (),
},
Key::Enter => match &app.focus_type {
FocusType::Connections => app.focus_type = FocusType::Dabatases(true),
Key::Enter => match app.focus_type {
FocusType::Connections => {
create_connection::handler(key, app).await?;
database_list::handler(key, app).await?;
}
FocusType::Records(false) => app.focus_type = FocusType::Records(true),
FocusType::Dabatases(false) => app.focus_type = FocusType::Dabatases(true),
FocusType::Tables(false) => app.focus_type = FocusType::Tables(true),

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

@ -43,12 +43,13 @@ async fn main() -> anyhow::Result<()> {
.as_str(),
)
.await?;
app.pool = Some(pool);
app.databases = utils::get_databases(&pool).await?;
app.databases = utils::get_databases(app.pool.as_ref().unwrap()).await?;
let (headers, records) = utils::get_records(
app.databases.first().unwrap(),
app.databases.first().unwrap().tables.first().unwrap(),
&pool,
app.pool.as_ref().unwrap(),
)
.await?;
app.record_table.rows = records;
@ -65,7 +66,7 @@ async fn main() -> anyhow::Result<()> {
if key == Key::Char('q') {
break;
};
handle_app(key, app, &pool).await?
handle_app(key, app).await?
}
Event::Tick => (),
}

@ -14,6 +14,7 @@ pub struct Connection {
pub user: String,
pub host: String,
pub port: u64,
pub database: Option<String>,
}
impl UserConfig {
@ -33,11 +34,20 @@ impl UserConfig {
impl Connection {
pub fn database_url(&self) -> String {
format!(
"mysql://{user}:@{host}:{port}",
user = self.user,
host = self.host,
port = self.port
)
match &self.database {
Some(database) => format!(
"mysql://{user}:@{host}:{port}/{database}",
user = self.user,
host = self.host,
port = self.port,
database = database
),
None => format!(
"mysql://{user}:@{host}:{port}",
user = self.user,
host = self.host,
port = self.port,
),
}
}
}

@ -49,7 +49,7 @@ pub async fn get_records(
for col in row.columns() {
let col_name = col.name();
match col.type_info().clone().name() {
"INT" => match row.try_get(col_name) {
"INT" | "DECIMAL" | "SMALLINT" => match row.try_get(col_name) {
Ok(value) => {
let value: i64 = value;
row_vec.push(value.to_string())
@ -63,7 +63,7 @@ pub async fn get_records(
}
Err(_) => row_vec.push("".to_string()),
},
"VARCHAR" => {
"VARCHAR" | "CHAR" => {
let value: String = row.try_get(col_name).unwrap_or("".to_string());
row_vec.push(value);
}

Loading…
Cancel
Save