diff --git a/asyncdbconnection.cpp b/asyncdbconnection.cpp index 2c23490..2092cc0 100644 --- a/asyncdbconnection.cpp +++ b/asyncdbconnection.cpp @@ -40,6 +40,12 @@ void ASyncDBConnection::setStateCallback(on_state_callback state_callback) m_threadData.m_stateCallback.m_func = state_callback; } +void ASyncDBConnection::setNoticeCallback(on_notice_callback notice_callback) +{ + std::lock_guard lg(m_threadData.m_noticeCallback.m_mutex); + m_threadData.m_noticeCallback.m_func = notice_callback; +} + ASyncDBConnection::Thread::Thread() : m_stopEvent(Win32Event::Reset::Manual, Win32Event::Initial::Clear) {} @@ -51,6 +57,8 @@ void ASyncDBConnection::Thread::run() // make or recover connection if (makeConnection()) { + m_connection.setNoticeReceiver( + [this](const PGresult *result) { processNotice(result); }); m_canceller = m_connection.getCancel(); @@ -278,3 +286,12 @@ void ASyncDBConnection::Thread::waitForResult() // When last result received, remove command from queue } +void ASyncDBConnection::Thread::processNotice(const PGresult *result) +{ +// Pgsql::Result res(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); + } +} diff --git a/asyncdbconnection.h b/asyncdbconnection.h index 888ad5f..e9b9425 100644 --- a/asyncdbconnection.h +++ b/asyncdbconnection.h @@ -22,6 +22,7 @@ public: using on_result_callback = std::function)>; using on_state_callback = std::function; + using on_notice_callback = std::function; ASyncDBConnection(); @@ -29,6 +30,7 @@ public: void closeConnection(); void setStateCallback(on_state_callback state_callback); + void setNoticeCallback(on_notice_callback notice_callback); /** Sends command to the server. @@ -61,6 +63,10 @@ private: std::mutex m_mutex; on_state_callback m_func; } m_stateCallback; + struct { + std::mutex m_mutex; + on_notice_callback m_func; + } m_noticeCallback; struct t_Command { std::mutex m_mutex; @@ -102,6 +108,9 @@ private: void waitForAndSendCommand(); void doNewCommand(); void waitForResult(); + + + void processNotice(const PGresult *result); }; Thread m_threadData; diff --git a/mainwindow.cpp b/mainwindow.cpp index c291e91..2a4236c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -29,7 +29,6 @@ const char * test_query = MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) -// , queryCancel(nullptr) { ui->setupUi(this); @@ -59,6 +58,11 @@ MainWindow::MainWindow(QWidget *parent) { QueueTask([this, st]() { connectionStateChanged(st); }); }); + + m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details) + { + QueueTask([this, details]() { receiveNotice(details); }); + }); } MainWindow::~MainWindow() @@ -171,7 +175,7 @@ void MainWindow::query_ready(std::shared_ptr dbres) else { statusBar()->showMessage(tr("No tuples returned, possibly an error...")); } - //receiveNotice(dbres->diagDetails()); + receiveNotice(dbres->diagDetails()); } } else { @@ -236,29 +240,22 @@ void MainWindow::cancel_query() m_dbConnection.cancel(); } -//void MainWindow::processNotice(const PGresult *result) -//{ -// qRegisterMetaType("Pgsql::ErrorDetails"); -// pg::ErrorDetails details = pg::ErrorDetails::createErrorDetailsFromPGresult(result); -// QMetaObject::invokeMethod(this, "receiveNotice", Qt::AutoConnection, Q_ARG(Pgsql::ErrorDetails, details)); // queues on main thread -//} - -//void MainWindow::receiveNotice(Pgsql::ErrorDetails notice) -//{ -// QTextCursor cursor = ui->messagesEdit->textCursor(); -// cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); +void MainWindow::receiveNotice(Pgsql::ErrorDetails notice) +{ + QTextCursor cursor = ui->messagesEdit->textCursor(); + cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); -//// QString msg; -//// cursor.insertText("TEST\r\n"); +// QString msg; +// cursor.insertText("TEST\r\n"); -// QTextTable *table = cursor.insertTable(4, 2); -// if (table) { -// table->cellAt(1, 0).firstCursorPosition().insertText("State"); -// table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state)); -// table->cellAt(2, 0).firstCursorPosition().insertText("Primary"); -// table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary)); -// table->cellAt(3, 0).firstCursorPosition().insertText("Detail"); -// table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail)); -// } -//} + QTextTable *table = cursor.insertTable(4, 2); + if (table) { + table->cellAt(1, 0).firstCursorPosition().insertText("State"); + table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state)); + table->cellAt(2, 0).firstCursorPosition().insertText("Primary"); + table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary)); + table->cellAt(3, 0).firstCursorPosition().insertText("Detail"); + table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail)); + } +} diff --git a/mainwindow.h b/mainwindow.h index 9a724f2..bea7b72 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -60,12 +60,6 @@ private: std::unique_ptr resultModel; std::unique_ptr explainModel; -// struct { -// std::unique_ptr notifier; -// PostgresPollingStatusType poll_state; -// } connectingState; - -// void processNotice(const PGresult *result); void query_ready(std::shared_ptr res); void explain_ready(std::shared_ptr explain); private slots: @@ -74,10 +68,9 @@ private slots: void performQuery(); void performExplain(); -// void socket_activate_connect(int socket); void cancel_query(); -// void receiveNotice(Pgsql::ErrorDetails notice); + void receiveNotice(Pgsql::ErrorDetails notice); void processCallableQueue(); };