From a4d2b366b603022670bdff365e30597477074af6 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 28 Dec 2022 18:30:34 -0500 Subject: [PATCH] SQLStudio: Close the current SQL connection before opening a new one --- Userland/DevTools/SQLStudio/MainWidget.cpp | 10 +++++++++- Userland/DevTools/SQLStudio/MainWidget.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/DevTools/SQLStudio/MainWidget.cpp b/Userland/DevTools/SQLStudio/MainWidget.cpp index 421354e1d76..2c0bfdf9911 100644 --- a/Userland/DevTools/SQLStudio/MainWidget.cpp +++ b/Userland/DevTools/SQLStudio/MainWidget.cpp @@ -133,6 +133,11 @@ MainWidget::MainWidget() // TODO select the database to use in UI. constexpr auto database_name = "Test"sv; + if (m_connection_id.has_value()) { + m_sql_client->disconnect(*m_connection_id); + m_connection_id.clear(); + } + if (auto connection_id = m_sql_client->connect(database_name); connection_id.has_value()) { m_connection_id = connection_id.release_value(); read_next_sql_statement_of_editor(); @@ -432,6 +437,9 @@ void MainWidget::drop_event(GUI::DropEvent& drop_event) void MainWidget::read_next_sql_statement_of_editor() { + if (!m_connection_id.has_value()) + return; + StringBuilder piece; do { if (!piece.is_empty()) @@ -482,7 +490,7 @@ void MainWidget::read_next_sql_statement_of_editor() auto sql_statement = piece.to_deprecated_string(); - if (auto statement_id = m_sql_client->prepare_statement(m_connection_id, sql_statement); statement_id.has_value()) { + if (auto statement_id = m_sql_client->prepare_statement(*m_connection_id, sql_statement); statement_id.has_value()) { m_sql_client->async_execute_statement(*statement_id, {}); } else { auto* editor = active_editor(); diff --git a/Userland/DevTools/SQLStudio/MainWidget.h b/Userland/DevTools/SQLStudio/MainWidget.h index 644742c8cb5..4c3a62f9767 100644 --- a/Userland/DevTools/SQLStudio/MainWidget.h +++ b/Userland/DevTools/SQLStudio/MainWidget.h @@ -62,13 +62,13 @@ private: RefPtr m_query_results_table_view; RefPtr m_sql_client; + Optional m_connection_id; Vector> m_results; void read_next_sql_statement_of_editor(); Optional read_next_line_of_editor(); size_t m_current_line_for_parsing { 0 }; int m_editor_line_level { 0 }; - SQL::ConnectionID m_connection_id { 0 }; }; }