mirror of https://github.com/TaKO8Ki/gobang
fix user config
parent
f053dd1f31
commit
cf23d24ed6
@ -1,5 +1,10 @@
|
|||||||
[conn.sample]
|
[[conn]]
|
||||||
name = "sample"
|
name = "sample"
|
||||||
user = "root"
|
user = "root"
|
||||||
host = "localhost"
|
host = "localhost"
|
||||||
port = 3306
|
port = 3306
|
||||||
|
|
||||||
|
[[conn]]
|
||||||
|
user = "root"
|
||||||
|
host = "localhost"
|
||||||
|
port = 3306
|
||||||
|
@ -1,17 +1,12 @@
|
|||||||
use crate::app::{App, Database};
|
use crate::app::{App, Database};
|
||||||
use crate::event::Key;
|
use crate::event::Key;
|
||||||
|
use crate::utils::get_databases;
|
||||||
use sqlx::mysql::MySqlPool;
|
use sqlx::mysql::MySqlPool;
|
||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
|
|
||||||
pub async fn handler<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
|
pub async fn handler<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
|
||||||
let databases = sqlx::query("show databases")
|
for db in get_databases(pool).await? {
|
||||||
.fetch_all(pool)
|
app.databases.push(db)
|
||||||
.await?
|
|
||||||
.iter()
|
|
||||||
.map(|table| table.get(0))
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
for db in databases {
|
|
||||||
app.databases.push(Database::new(db, pool).await?)
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,19 @@
|
|||||||
use crate::app::App;
|
use crate::app::App;
|
||||||
use crate::event::Key;
|
use crate::event::Key;
|
||||||
|
use crate::utils::get_records;
|
||||||
use sqlx::mysql::MySqlPool;
|
use sqlx::mysql::MySqlPool;
|
||||||
|
|
||||||
pub async fn handler<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
|
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;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
use crate::app::App;
|
|
||||||
use crate::event::Key;
|
|
||||||
use futures::TryStreamExt;
|
|
||||||
use sqlx::mysql::MySqlPool;
|
|
||||||
use sqlx::{Column, Executor, Row, TypeInfo};
|
|
||||||
|
|
||||||
pub async fn handler<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
|
|
||||||
match app.selected_database.selected() {
|
|
||||||
Some(index) => {
|
|
||||||
&app.databases[index].next();
|
|
||||||
let db = &app.databases[app.selected_database.selected().unwrap_or(0)];
|
|
||||||
&pool.execute(format!("use {}", db.name).as_str()).await?;
|
|
||||||
let table_name = format!(
|
|
||||||
"SELECT * FROM {}",
|
|
||||||
&db.tables[db.selected_table.selected().unwrap_or(0)].name
|
|
||||||
);
|
|
||||||
let mut rows = sqlx::query(table_name.as_str()).fetch(pool);
|
|
||||||
let headers = sqlx::query(
|
|
||||||
format!(
|
|
||||||
"desc {}",
|
|
||||||
&db.tables[db.selected_table.selected().unwrap_or(0)].name
|
|
||||||
)
|
|
||||||
.as_str(),
|
|
||||||
)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
.iter()
|
|
||||||
.map(|table| table.get(0))
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
let mut records = vec![];
|
|
||||||
|
|
||||||
while let Some(row) = rows.try_next().await? {
|
|
||||||
let mut row_vec = vec![];
|
|
||||||
for col in row.columns() {
|
|
||||||
let col_name = col.name();
|
|
||||||
match col.type_info().clone().name() {
|
|
||||||
"INT" => {
|
|
||||||
let value: i32 = row.try_get(col_name).unwrap_or(0);
|
|
||||||
row_vec.push(value.to_string());
|
|
||||||
}
|
|
||||||
"VARCHAR" => {
|
|
||||||
let value: String = row.try_get(col_name).unwrap_or("".to_string());
|
|
||||||
row_vec.push(value);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
records.push(row_vec)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.record_table.rows = records;
|
|
||||||
app.record_table.headers = headers;
|
|
||||||
}
|
|
||||||
None => (),
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
Loading…
Reference in New Issue