Skip to content

Commit

Permalink
feat: pass dynamic dialect from config to sql parser
Browse files Browse the repository at this point in the history
  • Loading branch information
invm committed Oct 9, 2023
1 parent 8566f6d commit 5f5b4c8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/database/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum Dialect {
}

impl Dialect {
fn as_str(&self) -> &'static str {
pub fn as_str(&self) -> &'static str {
match self {
Dialect::Mysql => "mysql",
Dialect::Postgres => "postgres",
Expand Down
17 changes: 9 additions & 8 deletions src-tauri/src/handlers/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlparser::dialect::GenericDialect;
use sqlparser::{dialect::dialect_from_str, parser::Parser};
use tauri::{command, AppHandle, State};
use tracing::info;

Expand All @@ -19,20 +19,21 @@ pub async fn enqueue_query(
_auto_limit: bool,
) -> CommandResult<QueryTaskEnqueueResult> {
info!(sql, conn_id, tab_id, "enqueue_query");
let binding = async_state.connections.lock().await;
let conn = binding.get(&conn_id);
let dialect = conn.unwrap().config.dialect.as_str();
let statements: Result<Vec<String>, Error> =
match sqlparser::parser::Parser::parse_sql(&GenericDialect {}, sql) {
match Parser::parse_sql(dialect_from_str(dialect).unwrap().as_ref(), sql) {
Ok(statements) => Ok(statements.into_iter().map(|s| s.to_string()).collect()),
Err(e) => Err(Error::from(e)),
};
match statements {
Ok(statements) => {
let binding = async_state.connections.lock().await;
let conn = binding.get(&conn_id);
let async_proc_input_tx = async_state.tasks.lock().await;
if let Some(conn) = conn {
for statement in statements {
println!("Got statement {:?}", statement.to_string());
let task = QueryTask::new(conn.clone(), &tab_id, &statement);
let async_proc_input_tx = async_state.tasks.lock().await;
for (idx, statement) in statements.iter().enumerate() {
info!("Got statement {:?}", statement.to_string());
let task = QueryTask::new(conn.clone(), &tab_id, &statement, idx);
let res = async_proc_input_tx.send(task.clone()).await;
if let Err(e) = res {
return Err(Error::from(e));
Expand Down
52 changes: 27 additions & 25 deletions src-tauri/src/queues/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ impl Default for QueryTaskStatus {
pub struct QueryTask {
pub conn: ConnectedConnection,
pub query: String,
pub tab_number: String,
pub tab_id: String,
pub id: String,
pub status: QueryTaskStatus,
pub query_idx: usize,
}

impl QueryTask {
pub fn new(conn: ConnectedConnection, tab_number: &str, query: &str) -> Self {
pub fn new(conn: ConnectedConnection, tab_id: &str, query: &str, query_idx: usize) -> Self {
let id = Uuid::new_v4();
QueryTask {
conn,
tab_number: tab_number.to_string(),
tab_id: tab_id.to_string(),
query: query.to_string(),
id: id.to_string(),
status: QueryTaskStatus::Queued,
query_idx,
}
}
}
Expand All @@ -64,28 +66,28 @@ pub async fn async_process_model(
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
while let Some(input) = input_rx.recv().await {
let task = input;
// match task.conn.execute_query(task.query).await {
// Ok(_res) => {
// output_tx
// .send(QueryTaskResult {
// success: false,
// id: "asd".to_string(),
// path: "asd".to_string(),
// error: None,
// })
// .await?;
// }
// Err(e) => {
// output_tx
// .send(QueryTaskResult {
// success: false,
// id: "asd".to_string(),
// path: "asd".to_string(),
// error: Some(e.to_string()),
// })
// .await?;
// }
// }
match task.conn.execute_query(task.query).await {
Ok(_res) => {
output_tx
.send(QueryTaskResult {
success: false,
id: "asd".to_string(),
path: "asd".to_string(),
error: None,
})
.await?;
}
Err(e) => {
output_tx
.send(QueryTaskResult {
success: false,
id: "asd".to_string(),
path: "asd".to_string(),
error: Some(e.to_string()),
})
.await?;
}
}
}
Ok(())
}
Expand Down

0 comments on commit 5f5b4c8

Please sign in to comment.