diff --git a/src/pglab/ASyncDBConnection.cpp b/src/pglab/ASyncDBConnection.cpp index 1cd900c..e9b3a2a 100644 --- a/src/pglab/ASyncDBConnection.cpp +++ b/src/pglab/ASyncDBConnection.cpp @@ -72,13 +72,10 @@ void ASyncDBConnection::doStateCallback(State state) m_connection.setNoticeReceiver( [this](const PGresult *result) { processNotice(result); }); } - - std::lock_guard lg(m_stateCallback.m_mutex); - if (m_stateCallback.m_func) { - m_stateCallback.m_func(state); - } + emit onStateChanged(state); +} -} + void ASyncDBConnection::closeConnection() @@ -155,23 +152,8 @@ bool ASyncDBConnection::cancel() return m_canceller.cancel(nullptr); } -void ASyncDBConnection::setStateCallback(on_state_callback state_callback) -{ - std::lock_guard lg(m_stateCallback.m_mutex); - m_stateCallback.m_func = state_callback; -} - -void ASyncDBConnection::setNoticeCallback(on_notice_callback notice_callback) -{ - std::lock_guard lg(m_noticeCallback.m_mutex); - m_noticeCallback.m_func = notice_callback; -} - void ASyncDBConnection::processNotice(const PGresult *result) { - std::lock_guard lg(m_noticeCallback.m_mutex); - if (m_noticeCallback.m_func) { - Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result); - m_noticeCallback.m_func(details); - } + Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result); + emit onNotice(details); } diff --git a/src/pglab/ASyncDBConnection.h b/src/pglab/ASyncDBConnection.h index 6ddb358..af66760 100644 --- a/src/pglab/ASyncDBConnection.h +++ b/src/pglab/ASyncDBConnection.h @@ -1,16 +1,14 @@ #ifndef ASYNCDBCONNECTION_H #define ASYNCDBCONNECTION_H +#include + #include "Pgsql_Connection.h" #include "Pgsql_Params.h" +#include "Pgsql_Result.h" #include "ConnectionConfig.h" #include -// #include -// #include #include -// #include -// #include -// #include #include #include @@ -19,7 +17,8 @@ * Queries are passed to this class with a routine to call on completion * when the result is on that routine is called. */ -class ASyncDBConnection { +class ASyncDBConnection: public QObject { + Q_OBJECT public: enum class State { NotConnected, @@ -31,8 +30,6 @@ public: }; using on_result_callback = std::function, qint64)>; - using on_state_callback = std::function; - using on_notice_callback = std::function; explicit ASyncDBConnection(boost::asio::io_service &ios); ~ASyncDBConnection(); @@ -41,9 +38,6 @@ public: void setupConnection(const ConnectionConfig &config); void closeConnection(); - void setStateCallback(on_state_callback state_callback); - void setNoticeCallback(on_notice_callback notice_callback); - /** Sends command to the server. When the result is in on_result will be called directly within the thread. @@ -54,6 +48,10 @@ public: bool send(const std::string &command, Pgsql::Params params, on_result_callback on_result); bool cancel(); + +signals: + void onStateChanged(ASyncDBConnection::State state); + void onNotice(Pgsql::ErrorDetails notice); private: Pgsql::Connection m_connection; @@ -61,15 +59,7 @@ private: ConnectionConfig m_config; State m_state = State::NotConnected; Pgsql::Canceller m_canceller; - struct { - std::mutex m_mutex; - on_state_callback m_func; - } m_stateCallback; - struct { - std::mutex m_mutex; - on_notice_callback m_func; - } m_noticeCallback; QElapsedTimer m_timer; void async_connect_handler(boost::system::error_code ec, std::size_t s); @@ -78,4 +68,8 @@ private: void processNotice(const PGresult *result); }; +Q_DECLARE_METATYPE(ASyncDBConnection::State); +Q_DECLARE_METATYPE(Pgsql::ErrorDetails); + + #endif // ASYNCDBCONNECTION_H diff --git a/src/pglab/DatabaseWindow.cpp b/src/pglab/DatabaseWindow.cpp index da890b3..4aab48e 100644 --- a/src/pglab/DatabaseWindow.cpp +++ b/src/pglab/DatabaseWindow.cpp @@ -11,21 +11,13 @@ DatabaseWindow::DatabaseWindow(QWidget *parent) : { ui->setupUi(this); - m_dbConnection.setStateCallback([this](ASyncDBConnection::State st) - { - QueueTask([this, st]() { connectionStateChanged(st); }); - }); - - m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details) - { - QueueTask([this, details]() { receiveNotice(details); }); - }); + connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &DatabaseWindow::connectionStateChanged); + connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &DatabaseWindow::receiveNotice); } DatabaseWindow::~DatabaseWindow() { m_dbConnection.closeConnection(); - m_dbConnection.setStateCallback(nullptr); delete ui; } diff --git a/src/pglab/QueryTab.cpp b/src/pglab/QueryTab.cpp index fd2e01e..a392f3f 100644 --- a/src/pglab/QueryTab.cpp +++ b/src/pglab/QueryTab.cpp @@ -1,10 +1,9 @@ #include "QueryTab.h" #include "ui_QueryTab.h" - #include "SqlSyntaxHighlighter.h" - #include #include + #include #include #include @@ -68,16 +67,9 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) : { ui->setupUi(this); - m_dbConnection.setStateCallback([this](ASyncDBConnection::State st) - { - m_win->QueueTask([this, st]() { connectionStateChanged(st); }); - }); - - m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details) - { - m_win->QueueTask([this, details]() { receiveNotice(details); }); - }); - + connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTab::connectionStateChanged); + connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTab::receiveNotice); + QFont font; font.setFamily("Source Code Pro"); font.setFixedPitch(true); @@ -103,7 +95,6 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) : QueryTab::~QueryTab() { m_dbConnection.closeConnection(); - m_dbConnection.setStateCallback(nullptr); delete ui; } diff --git a/src/pglab/QueryTab.h b/src/pglab/QueryTab.h index 8214b62..4b82b5b 100644 --- a/src/pglab/QueryTab.h +++ b/src/pglab/QueryTab.h @@ -73,14 +73,6 @@ public: bool isNew() const { return m_new; } private: -// struct ResultTab { -// public: -// std::shared_ptr resultModel; -// std::shared_ptr tuplesResult; -//// ResultTab(std::shared_ptr rm, Ui::TuplesResult *tr) -//// : resultModel(rm), tuplesResult(tr) -//// {} -// }; using ResultTabContainer = std::vector; Ui::QueryTab *ui; diff --git a/src/pglab/main.cpp b/src/pglab/main.cpp index 505265d..cf43959 100644 --- a/src/pglab/main.cpp +++ b/src/pglab/main.cpp @@ -5,6 +5,8 @@ #endif #include #include "GlobalIoService.h" +#include "ASyncDBConnection.h" +#include "Pgsql_Result.h" int main(int argc, char *argv[]) { @@ -22,6 +24,9 @@ int main(int argc, char *argv[]) } #endif + qRegisterMetaType(); + qRegisterMetaType(); + QApplication a(argc, argv); QCoreApplication::setOrganizationName("pglab"); diff --git a/src/pgsql/Pgsql_Result.h b/src/pgsql/Pgsql_Result.h index fdcd54a..471ad38 100644 --- a/src/pgsql/Pgsql_Result.h +++ b/src/pgsql/Pgsql_Result.h @@ -132,4 +132,5 @@ namespace Pgsql { } // end namespace Pgsql + #endif // PGSQL_RESULT_H