mirror of https://github.com/TaKO8Ki/gobang
implement query widget (#8)
parent
4b26b71a38
commit
b8c36b5f94
@ -1,32 +1,74 @@
|
|||||||
use crate::app::App;
|
use crate::app::{App, FocusBlock};
|
||||||
use crate::event::Key;
|
use crate::event::Key;
|
||||||
use crate::utils::convert_column_value_to_string;
|
use crate::utils::convert_column_value_to_string;
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
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.query = app.input.drain(..).collect();
|
if focused {
|
||||||
let re = Regex::new(r"select .+ from (.+)").unwrap();
|
match key {
|
||||||
if let Some(caps) = re.captures(app.query.as_str()) {
|
Key::Enter => {
|
||||||
let mut rows = sqlx::query(app.query.as_str()).fetch(app.pool.as_ref().unwrap());
|
app.query = app.input.drain(..).collect();
|
||||||
let headers = sqlx::query(format!("desc `{}`", caps.get(1).unwrap().as_str()).as_str())
|
let re = Regex::new(r"select .+ from ([^ ]+).*").unwrap();
|
||||||
.fetch_all(app.pool.as_ref().unwrap())
|
match re.captures(app.query.as_str()) {
|
||||||
.await?
|
Some(caps) => {
|
||||||
.iter()
|
let mut rows =
|
||||||
.map(|table| table.get(0))
|
sqlx::query(app.query.as_str()).fetch(app.pool.as_ref().unwrap());
|
||||||
.collect::<Vec<String>>();
|
let headers = sqlx::query(
|
||||||
let mut records = vec![];
|
format!("desc `{}`", caps.get(1).unwrap().as_str()).as_str(),
|
||||||
while let Some(row) = rows.try_next().await? {
|
)
|
||||||
records.push(
|
.fetch_all(app.pool.as_ref().unwrap())
|
||||||
row.columns()
|
.await?
|
||||||
.iter()
|
.iter()
|
||||||
.map(|col| convert_column_value_to_string(&row, col))
|
.map(|table| table.get(0))
|
||||||
.collect::<Vec<String>>(),
|
.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;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
sqlx::query(app.query.as_str())
|
||||||
|
.execute(app.pool.as_ref().unwrap())
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Char(c) => app.input.push(c),
|
||||||
|
Key::Delete | Key::Backspace => {
|
||||||
|
if app.input.width() > 0 {
|
||||||
|
if app.input_cursor_x == 0 {
|
||||||
|
app.input.pop();
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
if app.input.width() - app.input_cursor_x as usize > 0 {
|
||||||
|
app.input
|
||||||
|
.remove(app.input.width() - app.input_cursor_x as usize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Left => app.decrement_input_cursor_x(),
|
||||||
|
Key::Right => app.increment_input_cursor_x(),
|
||||||
|
Key::Esc => app.focus_block = FocusBlock::Query(false),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match key {
|
||||||
|
Key::Char('h') => app.focus_block = FocusBlock::DabataseList(false),
|
||||||
|
Key::Char('j') => app.focus_block = FocusBlock::RecordTable(false),
|
||||||
|
Key::Char('c') => app.focus_block = FocusBlock::ConnectionList,
|
||||||
|
Key::Enter => app.focus_block = FocusBlock::Query(true),
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
app.record_table.headers = headers;
|
|
||||||
app.record_table.rows = records;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue