Skip to content

Commit

Permalink
Add show query action (shortcut - 'S') (#61)
Browse files Browse the repository at this point in the history
Fixes: #56
  • Loading branch information
azat authored Apr 17, 2024
2 parents 08807af + 3aa6084 commit 3fa6b89
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod utils;
pub use utils::edit_query;
#[cfg(not(target_family = "windows"))]
pub use utils::fuzzy_actions;
pub use utils::get_query;
pub use utils::highlight_sql;
pub use utils::open_graph_in_browser;

Expand Down
29 changes: 17 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,32 @@ pub fn highlight_sql(text: &String) -> Result<StyledString> {
.context("Cannot highlight query");
}

pub fn get_query(query: &String, settings: &HashMap<String, String>) -> String {
let mut ret = query.to_owned();
let settings_str = settings
.iter()
.map(|kv| format!("\t{}='{}'\n", kv.0, kv.1.replace('\'', "\\\'")))
.collect::<Vec<String>>()
.join(",");
if !query.contains("SETTINGS") {
ret.push_str("\nSETTINGS\n");
} else {
ret.push_str(",\n");
}
ret.push_str(&settings_str);
return ret;
}

pub fn edit_query(query: &String, settings: &HashMap<String, String>) -> Result<String> {
let mut tmp_file = Builder::new()
.prefix("chdig-query-")
.suffix(".sql")
.rand_bytes(5)
.tempfile()?;

let query = get_query(query, settings);
tmp_file.write_all(query.as_bytes())?;

let settings_str = settings
.iter()
.map(|kv| format!("\t{}='{}'\n", kv.0, kv.1.replace('\'', "\\\'")))
.collect::<Vec<String>>()
.join(",");
if query.contains("SETTINGS") {
tmp_file.write_all("\nSETTINGS\n".as_bytes())?;
} else {
tmp_file.write_all(",\n".as_bytes())?;
}
tmp_file.write_all(settings_str.as_bytes())?;

let editor = env::var_os("EDITOR").unwrap_or_else(|| "vim".into());
let tmp_file_path = tmp_file.path().to_str().unwrap();
let result = Command::new(&editor)
Expand Down
30 changes: 28 additions & 2 deletions src/view/processes_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::interpreter::{
};
use crate::view::{ExtTableView, ProcessView, QueryResultView, TableViewItem, TextLogView};
use crate::wrap_impl_no_move;
use chdig::edit_query;
use chdig::{edit_query, get_query};

// Analog of mapFromArrays() in ClickHouse
fn map_from_arrays<K, V>(keys: Vec<K>, values: Vec<V>) -> HashMap<K, V>
Expand Down Expand Up @@ -448,7 +448,7 @@ impl ProcessesView {

let is_system_processes = matches!(processes_type, Type::ProcessList);
let filter = Arc::new(Mutex::new(String::new()));
let limit = Arc::new(Mutex::new(if matches!(processes_type, Type::ProcessList) {
let limit = Arc::new(Mutex::new(if is_system_processes {
10000
} else {
100_u64
Expand Down Expand Up @@ -837,6 +837,32 @@ impl ProcessesView {
)))));
},
);
context.add_view_action(&mut event_view, "Show query", 'S', |v| {
let v = v.downcast_mut::<ProcessesView>().unwrap();
let selected_query = v.get_selected_query()?;
let query = selected_query.original_query.clone();
let database = selected_query.current_database.clone();
let settings = selected_query.settings.clone();

let query = get_query(&query, &settings);
let query = format!("USE {};\n{}", database, query);

v.context
.lock()
.unwrap()
.cb_sink
.send(Box::new(move |siv: &mut cursive::Cursive| {
siv.add_layer(views::Dialog::around(
views::LinearLayout::vertical()
.child(views::TextView::new("Query:").center())
.child(views::DummyView.fixed_height(1))
.child(views::TextView::new(query)),
));
}))
.unwrap();

return Ok(Some(EventResult::consumed()));
});
context.add_view_action(&mut event_view, "EXPLAIN SYNTAX", 's', |v| {
let v = v.downcast_mut::<ProcessesView>().unwrap();
let selected_query = v.get_selected_query()?;
Expand Down

0 comments on commit 3fa6b89

Please sign in to comment.