fix user config

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

69
Cargo.lock generated

@ -122,12 +122,6 @@ version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "bytes"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040"
[[package]] [[package]]
name = "cargo-platform" name = "cargo-platform"
version = "0.1.1" version = "0.1.1"
@ -306,6 +300,12 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.0.1" version = "1.0.1"
@ -475,7 +475,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",
"tokio 1.7.1", "tokio",
"toml", "toml",
"tui", "tui",
"unicode-width", "unicode-width",
@ -679,6 +679,18 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "mio-named-pipes"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656"
dependencies = [
"log",
"mio 0.6.23",
"miow 0.3.7",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "mio-uds" name = "mio-uds"
version = "0.6.8" version = "0.6.8"
@ -1246,7 +1258,7 @@ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"bitflags", "bitflags",
"byteorder", "byteorder",
"bytes 0.5.6", "bytes",
"chrono", "chrono",
"crc", "crc",
"crossbeam-channel", "crossbeam-channel",
@ -1312,7 +1324,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63fc5454c9dd7aaea3a0eeeb65ca40d06d0d8e7413a8184f7c3a3ffa5056190b" checksum = "63fc5454c9dd7aaea3a0eeeb65ca40d06d0d8e7413a8184f7c3a3ffa5056190b"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"tokio 0.2.25", "tokio",
"tokio-rustls", "tokio-rustls",
] ]
@ -1418,37 +1430,21 @@ version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092"
dependencies = [ dependencies = [
"bytes 0.5.6", "bytes",
"fnv",
"futures-core", "futures-core",
"iovec", "iovec",
"lazy_static", "lazy_static",
"libc", "libc",
"memchr", "memchr",
"mio 0.6.23", "mio 0.6.23",
"mio-named-pipes",
"mio-uds", "mio-uds",
"num_cpus", "num_cpus",
"pin-project-lite 0.1.12", "pin-project-lite 0.1.12",
"slab",
"tokio-macros 0.2.6",
]
[[package]]
name = "tokio"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fb2ed024293bb19f7a5dc54fe83bf86532a44c12a2bb8ba40d64a4509395ca2"
dependencies = [
"autocfg 1.0.1",
"bytes 1.0.1",
"libc",
"memchr",
"mio 0.7.11",
"num_cpus",
"once_cell",
"parking_lot",
"pin-project-lite 0.2.6",
"signal-hook-registry", "signal-hook-registry",
"tokio-macros 1.2.0", "slab",
"tokio-macros",
"winapi 0.3.9", "winapi 0.3.9",
] ]
@ -1463,17 +1459,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tokio-macros"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.14.1" version = "0.14.1"
@ -1482,7 +1467,7 @@ checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"rustls", "rustls",
"tokio 0.2.25", "tokio",
"webpki", "webpki",
] ]

@ -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

@ -189,7 +189,7 @@ impl<'a> App<'a> {
if let Some(config) = &self.user_config { if let Some(config) = &self.user_config {
let i = match self.selected_connection.selected() { let i = match self.selected_connection.selected() {
Some(i) => { Some(i) => {
if i >= config.connections.len() - 1 { if i >= config.conn.len() - 1 {
0 0
} else { } else {
i + 1 i + 1
@ -203,10 +203,10 @@ impl<'a> App<'a> {
pub fn previous_connection(&mut self) { pub fn previous_connection(&mut self) {
if let Some(config) = &self.user_config { if let Some(config) = &self.user_config {
let i = match self.selected_database.selected() { let i = match self.selected_connection.selected() {
Some(i) => { Some(i) => {
if i == 0 { if i == 0 {
config.connections.len() - 1 config.conn.len() - 1
} else { } else {
i - 1 i - 1
} }

@ -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,12 +1,9 @@
pub mod database_list; pub mod database_list;
pub mod record_table; pub mod record_table;
pub mod table_list;
use crate::app::{App, FocusType, InputMode}; use crate::app::{App, FocusType, InputMode};
use crate::event::Key; use crate::event::Key;
use futures::TryStreamExt;
use sqlx::mysql::MySqlPool; use sqlx::mysql::MySqlPool;
use sqlx::{Column, Executor, Row, TypeInfo};
pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> { pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> anyhow::Result<()> {
match app.input_mode { match app.input_mode {
@ -45,15 +42,6 @@ pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> an
Some(index) => { Some(index) => {
app.record_table.column_index = 0; app.record_table.column_index = 0;
app.databases[index].previous(); app.databases[index].previous();
let db = &app.databases[app.selected_database.selected().unwrap_or(0)];
let (headers, records) = crate::utils::get_records(
db,
&db.tables[db.selected_table.selected().unwrap()],
&pool,
)
.await?;
app.record_table.rows = records;
app.record_table.headers = headers;
} }
None => (), None => (),
}, },
@ -67,15 +55,7 @@ pub async fn handle_app<'a>(key: Key, app: &mut App<'a>, pool: &MySqlPool) -> an
Some(index) => { Some(index) => {
app.record_table.column_index = 0; app.record_table.column_index = 0;
&app.databases[index].next(); &app.databases[index].next();
let db = &app.databases[app.selected_database.selected().unwrap_or(0)]; record_table::handler(key, app, pool).await?
let (headers, records) = crate::utils::get_records(
db,
&db.tables[db.selected_table.selected().unwrap()],
&pool,
)
.await?;
app.record_table.rows = records;
app.record_table.headers = headers;
} }
None => (), None => (),
}, },

@ -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(())
}

@ -45,13 +45,7 @@ async fn main() -> anyhow::Result<()> {
let mut app = &mut app::App::default(); let mut app = &mut app::App::default();
app.user_config = Some(config); app.user_config = Some(config);
let conn = &app let conn = &app.user_config.as_ref().unwrap().conn.get(0).unwrap();
.user_config
.as_ref()
.unwrap()
.connections
.get(0)
.unwrap();
let pool = MySqlPool::connect( let pool = MySqlPool::connect(
format!( format!(
"mysql://{user}:@{host}:{port}", "mysql://{user}:@{host}:{port}",

@ -19,11 +19,11 @@ pub fn draw<B: Backend>(f: &mut Frame<'_, B>, app: &mut App) -> anyhow::Result<(
if let FocusType::Connections = app.focus_type { if let FocusType::Connections = app.focus_type {
let percent_x = 60; let percent_x = 60;
let percent_y = 50; let percent_y = 50;
let conns = &app.user_config.as_ref().unwrap().connections; let conns = &app.user_config.as_ref().unwrap().conn;
let connections: Vec<ListItem> = conns let connections: Vec<ListItem> = conns
.iter() .iter()
.map(|i| { .map(|i| {
ListItem::new(vec![Spans::from(Span::raw(&i.name))]) ListItem::new(vec![Spans::from(Span::raw(i.database_url().to_string()))])
.style(Style::default().fg(Color::White)) .style(Style::default().fg(Color::White))
}) })
.collect(); .collect();

@ -3,14 +3,14 @@ use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
#[derive(Debug)] #[derive(Debug, Deserialize)]
pub struct UserConfig { pub struct UserConfig {
pub connections: Vec<Connection>, pub conn: Vec<Connection>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Connection { pub struct Connection {
pub name: String, pub name: Option<String>,
pub user: String, pub user: String,
pub host: String, pub host: String,
pub port: u64, pub port: u64,
@ -18,31 +18,26 @@ pub struct Connection {
impl UserConfig { impl UserConfig {
pub fn new(path: &str) -> anyhow::Result<Self> { pub fn new(path: &str) -> anyhow::Result<Self> {
#[derive(Debug, Deserialize)]
pub struct ConfigFormat {
pub conn: HashMap<String, Connection>,
}
let file = File::open(path)?; let file = File::open(path)?;
let mut buf_reader = BufReader::new(file); let mut buf_reader = BufReader::new(file);
let mut contents = String::new(); let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?; buf_reader.read_to_string(&mut contents)?;
let config: Result<ConfigFormat, toml::de::Error> = toml::from_str(&contents); let config: Result<UserConfig, toml::de::Error> = toml::from_str(&contents);
match config { match config {
Ok(config) => Ok(UserConfig { Ok(config) => Ok(config),
connections: config
.conn
.iter()
.map(|(name, conn)| Connection {
name: name.to_string(),
user: conn.user.to_string(),
host: conn.host.to_string(),
port: conn.port,
})
.collect::<Vec<Connection>>(),
}),
Err(e) => panic!("fail to parse config file: {}", e), Err(e) => panic!("fail to parse config file: {}", e),
} }
} }
} }
impl Connection {
pub fn database_url(&self) -> String {
format!(
"mysql://{user}:@{host}:{port}",
user = self.user,
host = self.host,
port = self.port
)
}
}

Loading…
Cancel
Save