Skip to content

Commit 6baab08

Browse files
refactor for bypassing
1 parent 3ae3f90 commit 6baab08

File tree

8 files changed

+62
-38
lines changed

8 files changed

+62
-38
lines changed

src/app.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ impl App {
199199
}
200200
match database.get_query_results().await? {
201201
DbTaskResult::Finished(results) => {
202-
self.components.data.set_data_state(Some(results.results), Some(results.statement_type));
202+
self.components.data.set_data_state(Some(results.results), results.statement_type);
203203
self.state.last_query_end = Some(chrono::Utc::now());
204204
self.state.query_task_running = false;
205205
},
206206
DbTaskResult::ConfirmTx(rows_affected, statement) => {
207207
self.state.last_query_end = Some(chrono::Utc::now());
208-
self.set_popup(Box::new(ConfirmTx::new(rows_affected, statement.clone())));
208+
self.set_popup(Box::new(ConfirmTx::new(rows_affected, statement)));
209209
self.state.query_task_running = true;
210210
},
211211
DbTaskResult::Pending => {
@@ -265,7 +265,7 @@ impl App {
265265
let response = database.commit_tx().await?;
266266
self.state.last_query_end = Some(chrono::Utc::now());
267267
if let Some(results) = response {
268-
self.components.data.set_data_state(Some(results.results), Some(results.statement_type));
268+
self.components.data.set_data_state(Some(results.results), results.statement_type);
269269
self.set_focus(Focus::Editor);
270270
}
271271
},
@@ -401,7 +401,7 @@ impl App {
401401
},
402402
Ok((ExecutionType::Normal, _)) => {
403403
self.components.data.set_loading();
404-
database.start_query(query_string).await?;
404+
database.start_query(query_string, *bypass).await?;
405405
self.state.last_query_start = Some(chrono::Utc::now());
406406
self.state.last_query_end = None;
407407
},

src/components/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl Component for Data<'_> {
443443
},
444444
DataState::StatementCompleted(statement) => {
445445
f.render_widget(
446-
Paragraph::new(format!("{} statement completed", statement_type_string(statement)))
446+
Paragraph::new(format!("{} statement completed", statement_type_string(Some(statement.clone()))))
447447
.wrap(Wrap { trim: false })
448448
.block(block),
449449
area,

src/database/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub struct Rows {
4141
#[derive(Debug)]
4242
pub struct QueryResultsWithMetadata {
4343
pub results: Result<Rows>,
44-
pub statement_type: Statement,
44+
pub statement_type: Option<Statement>,
4545
}
4646

4747
#[derive(Debug, Clone, PartialEq)]
@@ -68,7 +68,7 @@ pub type QueryTask = JoinHandle<QueryResultsWithMetadata>;
6868

6969
pub enum DbTaskResult {
7070
Finished(QueryResultsWithMetadata),
71-
ConfirmTx(Option<u64>, Statement),
71+
ConfirmTx(Option<u64>, Option<Statement>),
7272
Pending,
7373
NoTask,
7474
}
@@ -83,7 +83,7 @@ pub trait Database {
8383

8484
/// Spawns a tokio task that runs the query. The task should
8585
/// expect to be polled via the `get_query_results()` method.
86-
async fn start_query(&mut self, query: String) -> Result<()>;
86+
async fn start_query(&mut self, query: String, bypass_parser: bool) -> Result<()>;
8787

8888
/// Aborts the tokio task running the active query or transaction.
8989
/// Some drivers also kill the process that was running the query,
@@ -193,12 +193,15 @@ fn get_default_execution_type(statement: Statement, confirmed: bool) -> Executio
193193
}
194194
}
195195

196-
pub fn statement_type_string(statement: &Statement) -> String {
197-
format!("{statement:?}").split('(').collect::<Vec<&str>>()[0].split('{').collect::<Vec<&str>>()[0]
198-
.split('[')
199-
.collect::<Vec<&str>>()[0]
200-
.trim()
201-
.to_string()
196+
pub fn statement_type_string(statement: Option<Statement>) -> String {
197+
match statement {
198+
Some(stmt) => format!("{stmt:?}").split('(').collect::<Vec<&str>>()[0].split('{').collect::<Vec<&str>>()[0]
199+
.split('[')
200+
.collect::<Vec<&str>>()[0]
201+
.trim()
202+
.to_string(),
203+
None => "UNKNOWN".to_string(),
204+
}
202205
}
203206

204207
pub fn vec_to_string<T: std::string::ToString>(vec: Vec<T>) -> String {

src/database/mysql.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ impl Database for MySqlDriver<'_> {
4646

4747
// since it's possible for raw_sql to execute multiple queries in a single string,
4848
// we only execute the first one and then drop the rest.
49-
async fn start_query(&mut self, query: String) -> Result<()> {
50-
let (first_query, statement_type) = super::get_first_query(query, Driver::MySql)?;
49+
async fn start_query(&mut self, query: String, bypass_parser: bool) -> Result<()> {
50+
let (first_query, statement_type) = match bypass_parser {
51+
true => (query, None),
52+
false => {
53+
let (first, stmt) = super::get_first_query(query, Driver::MySql)?;
54+
(first, Some(stmt))
55+
},
56+
};
5157
let pool = self.pool.clone().unwrap();
5258
self.querying_conn = Some(Arc::new(Mutex::new(pool.acquire().await?)));
5359
let conn = self.querying_conn.clone().unwrap();
@@ -154,18 +160,18 @@ impl Database for MySqlDriver<'_> {
154160
(
155161
QueryResultsWithMetadata {
156162
results: Ok(Rows { headers: vec![], rows: vec![], rows_affected: Some(rows_affected) }),
157-
statement_type,
163+
statement_type: Some(statement_type),
158164
},
159165
tx,
160166
)
161167
},
162168
Ok(Either::Right(rows)) => {
163169
log::info!("{:?} rows affected", rows.rows_affected);
164-
(QueryResultsWithMetadata { results: Ok(rows), statement_type }, tx)
170+
(QueryResultsWithMetadata { results: Ok(rows), statement_type: Some(statement_type) }, tx)
165171
},
166172
Err(e) => {
167173
log::error!("{e:?}");
168-
(QueryResultsWithMetadata { results: Err(e), statement_type }, tx)
174+
(QueryResultsWithMetadata { results: Err(e), statement_type: Some(statement_type) }, tx)
169175
},
170176
}
171177
})));

src/database/postgresql.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ impl Database for PostgresDriver<'_> {
5050

5151
// since it's possible for raw_sql to execute multiple queries in a single string,
5252
// we only execute the first one and then drop the rest.
53-
async fn start_query(&mut self, query: String) -> Result<()> {
54-
let (first_query, statement_type) = super::get_first_query(query, Driver::Postgres)?;
53+
async fn start_query(&mut self, query: String, bypass_parser: bool) -> Result<()> {
54+
let (first_query, statement_type) = match bypass_parser {
55+
true => (query, None),
56+
false => {
57+
let (first, stmt) = super::get_first_query(query, Driver::Postgres)?;
58+
(first, Some(stmt))
59+
},
60+
};
5561
let pool = self.pool.clone().unwrap();
5662
self.querying_conn = Some(Arc::new(Mutex::new(pool.acquire().await?)));
5763
let conn = self.querying_conn.clone().unwrap();
@@ -170,18 +176,18 @@ impl Database for PostgresDriver<'_> {
170176
(
171177
QueryResultsWithMetadata {
172178
results: Ok(Rows { headers: vec![], rows: vec![], rows_affected: Some(rows_affected) }),
173-
statement_type,
179+
statement_type: Some(statement_type),
174180
},
175181
tx,
176182
)
177183
},
178184
Ok(Either::Right(rows)) => {
179185
log::info!("{:?} rows affected", rows.rows_affected);
180-
(QueryResultsWithMetadata { results: Ok(rows), statement_type }, tx)
186+
(QueryResultsWithMetadata { results: Ok(rows), statement_type: Some(statement_type) }, tx)
181187
},
182188
Err(e) => {
183189
log::error!("{e:?}");
184-
(QueryResultsWithMetadata { results: Err(e), statement_type }, tx)
190+
(QueryResultsWithMetadata { results: Err(e), statement_type: Some(statement_type) }, tx)
185191
},
186192
}
187193
})));

src/database/sqlite.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ impl Database for SqliteDriver<'_> {
4343

4444
// since it's possible for raw_sql to execute multiple queries in a single string,
4545
// we only execute the first one and then drop the rest.
46-
async fn start_query(&mut self, query: String) -> Result<()> {
47-
let (first_query, statement_type) = super::get_first_query(query, Driver::Sqlite)?;
46+
async fn start_query(&mut self, query: String, bypass_parser: bool) -> Result<()> {
47+
let (first_query, statement_type) = match bypass_parser {
48+
true => (query, None),
49+
false => {
50+
let (first, stmt) = super::get_first_query(query, Driver::Sqlite)?;
51+
(first, Some(stmt))
52+
},
53+
};
4854
let pool = self.pool.clone().unwrap();
4955
self.task = Some(SqliteTask::Query(tokio::spawn(async move {
5056
let results = query_with_pool(pool, first_query.clone()).await;
@@ -125,18 +131,18 @@ impl Database for SqliteDriver<'_> {
125131
(
126132
QueryResultsWithMetadata {
127133
results: Ok(Rows { headers: vec![], rows: vec![], rows_affected: Some(rows_affected) }),
128-
statement_type,
134+
statement_type: Some(statement_type),
129135
},
130136
tx,
131137
)
132138
},
133139
Ok(Either::Right(rows)) => {
134140
log::info!("{:?} rows affected", rows.rows_affected);
135-
(QueryResultsWithMetadata { results: Ok(rows), statement_type }, tx)
141+
(QueryResultsWithMetadata { results: Ok(rows), statement_type: Some(statement_type) }, tx)
136142
},
137143
Err(e) => {
138144
log::error!("{e:?}");
139-
(QueryResultsWithMetadata { results: Err(e), statement_type }, tx)
145+
(QueryResultsWithMetadata { results: Err(e), statement_type: Some(statement_type) }, tx)
140146
},
141147
}
142148
})));

src/popups/confirm_query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ impl PopUp for ConfirmQuery {
3434
Statement::Explain { statement, .. } => {
3535
format!(
3636
"Are you sure you want to run an EXPLAIN ANALYZE that will run a {} statement?",
37-
statement_type_string(&statement).to_uppercase(),
37+
statement_type_string(Some(*statement.clone())).to_uppercase(),
3838
)
3939
},
4040
_ => {
4141
format!(
4242
"Are you sure you want to use a {} statement?",
43-
statement_type_string(&self.statement_type).to_uppercase()
43+
statement_type_string(Some(self.statement_type.clone())).to_uppercase()
4444
)
4545
},
4646
}

src/popups/confirm_tx.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::database::statement_type_string;
77
#[derive(Debug)]
88
pub struct ConfirmTx {
99
rows_affected: Option<u64>,
10-
statement_type: Statement,
10+
statement_type: Option<Statement>,
1111
}
1212

1313
impl ConfirmTx {
14-
pub fn new(rows_affected: Option<u64>, statement_type: Statement) -> Self {
14+
pub fn new(rows_affected: Option<u64>, statement_type: Option<Statement>) -> Self {
1515
Self { rows_affected, statement_type }
1616
}
1717
}
@@ -32,23 +32,26 @@ impl PopUp for ConfirmTx {
3232
fn get_cta_text(&self, app_state: &crate::app::AppState) -> String {
3333
let rows_affected = self.rows_affected.unwrap_or_default();
3434
match self.statement_type.clone() {
35-
Statement::Delete(_) | Statement::Insert(_) | Statement::Update { .. } => {
35+
None => {
36+
format!("Are you sure you want to commit a transaction that will affect {rows_affected} rows?")
37+
},
38+
Some(Statement::Delete(_)) | Some(Statement::Insert(_)) | Some(Statement::Update { .. }) => {
3639
format!(
3740
"Are you sure you want to {} {} rows?",
38-
statement_type_string(&self.statement_type).to_uppercase(),
41+
statement_type_string(self.statement_type.clone()).to_uppercase(),
3942
rows_affected
4043
)
4144
},
42-
Statement::Explain { statement, .. } => {
45+
Some(Statement::Explain { statement, .. }) => {
4346
format!(
4447
"Are you sure you want to run an EXPLAIN ANALYZE that will {} rows?",
45-
statement_type_string(&statement).to_uppercase(),
48+
statement_type_string(Some(*statement.clone())).to_uppercase(),
4649
)
4750
},
4851
_ => {
4952
format!(
5053
"Are you sure you want to use a {} statement?",
51-
statement_type_string(&self.statement_type).to_uppercase()
54+
statement_type_string(self.statement_type.clone()).to_uppercase()
5255
)
5356
},
5457
}

0 commit comments

Comments
 (0)