Skip to content

Commit

Permalink
feat: add sql query parseing
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Dec 29, 2023
1 parent 99e2f20 commit f4413f7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/query_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use monaco::{
api::{CodeEditor, CodeEditorOptions},
sys::editor::{IDimension, IEditorMinimapOptions},
};
use wasm_bindgen::JsCast;
use wasm_bindgen::{closure::Closure, JsCast};

use crate::store::{editor::EditorState, query::QueryState};

Expand Down Expand Up @@ -42,6 +42,12 @@ pub fn query_editor() -> impl IntoView {
options.set_minimap(Some(&minimap_settings));

let e = CodeEditor::create(html_element, Some(options));
let keycode = monaco::sys::KeyMod::win_ctrl() as u32 | monaco::sys::KeyCode::Enter.to_value();
e.as_ref().add_command(
keycode.into(),
Closure::<dyn Fn()>::new(|| ()).as_ref().unchecked_ref(),
None,
);
editor.update(|prev| {
prev.replace(Some(e));
});
Expand Down
55 changes: 54 additions & 1 deletion src/store/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ impl QueryState {
*prev = true;
});
let editor_state = use_context::<EditorState>().unwrap();
let position: monaco::sys::Position = editor_state
.editor
.get_untracked()
.borrow()
.as_ref()
.unwrap()
.as_ref()
.get_position()
.unwrap();
let sql = editor_state.get_value();
let args = serde_wasm_bindgen::to_value(&InvokeQueryArgs { sql }).unwrap();
let sql = match self.find_query_for_line(&sql, position.line_number()) {
Some(query) => Some(query),
None => None,
};
let args = serde_wasm_bindgen::to_value(&InvokeQueryArgs {
sql: sql.unwrap().query,
})
.unwrap();
let data = invoke(&Invoke::select_sql_result.to_string(), args).await;
let data = serde_wasm_bindgen::from_value::<(Vec<String>, Vec<Vec<String>>)>(data).unwrap();
self.sql_result.set(Some(data));
Expand Down Expand Up @@ -86,4 +102,41 @@ impl QueryState {
let editor_state = use_context::<EditorState>().unwrap();
editor_state.set_value(&query);
}

fn find_query_for_line(&self, queries: &str, line_number: f64) -> Option<QueryInfo> {
let mut start_line = 1f64;
let mut end_line = 1f64;
let mut current_query = String::new();

for line in queries.lines() {
if !current_query.is_empty() {
current_query.push('\n');
}
current_query.push_str(line);
end_line += 1f64;

if line.ends_with(';') {
if line_number >= start_line && line_number < end_line {
return Some(QueryInfo {
query: current_query.clone(),
start_line,
end_line: end_line - 1f64,
});
}
start_line = end_line;
current_query.clear();
}
}

None
}
}

#[derive(Clone, Debug)]
struct QueryInfo {
query: String,
#[allow(dead_code)]
start_line: f64,
#[allow(dead_code)]
end_line: f64,
}

0 comments on commit f4413f7

Please sign in to comment.