Notices and errors are as before switch might need still some work.

This commit is contained in:
Eelke Klein 2017-01-08 10:29:21 +01:00
parent a36bf5f7f4
commit 2502aea9e5
4 changed files with 49 additions and 33 deletions

View file

@ -40,6 +40,12 @@ void ASyncDBConnection::setStateCallback(on_state_callback state_callback)
m_threadData.m_stateCallback.m_func = state_callback; m_threadData.m_stateCallback.m_func = state_callback;
} }
void ASyncDBConnection::setNoticeCallback(on_notice_callback notice_callback)
{
std::lock_guard<std::mutex> lg(m_threadData.m_noticeCallback.m_mutex);
m_threadData.m_noticeCallback.m_func = notice_callback;
}
ASyncDBConnection::Thread::Thread() ASyncDBConnection::Thread::Thread()
: m_stopEvent(Win32Event::Reset::Manual, Win32Event::Initial::Clear) : m_stopEvent(Win32Event::Reset::Manual, Win32Event::Initial::Clear)
{} {}
@ -51,6 +57,8 @@ void ASyncDBConnection::Thread::run()
// make or recover connection // make or recover connection
if (makeConnection()) { if (makeConnection()) {
m_connection.setNoticeReceiver(
[this](const PGresult *result) { processNotice(result); });
m_canceller = m_connection.getCancel(); m_canceller = m_connection.getCancel();
@ -278,3 +286,12 @@ void ASyncDBConnection::Thread::waitForResult()
// When last result received, remove command from queue // When last result received, remove command from queue
} }
void ASyncDBConnection::Thread::processNotice(const PGresult *result)
{
// Pgsql::Result res(result);
std::lock_guard<std::mutex> lg(m_noticeCallback.m_mutex);
if (m_noticeCallback.m_func) {
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
m_noticeCallback.m_func(details);
}
}

View file

@ -22,6 +22,7 @@ public:
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>)>; using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>)>;
using on_state_callback = std::function<void(State)>; using on_state_callback = std::function<void(State)>;
using on_notice_callback = std::function<void(Pgsql::ErrorDetails)>;
ASyncDBConnection(); ASyncDBConnection();
@ -29,6 +30,7 @@ public:
void closeConnection(); void closeConnection();
void setStateCallback(on_state_callback state_callback); void setStateCallback(on_state_callback state_callback);
void setNoticeCallback(on_notice_callback notice_callback);
/** Sends command to the server. /** Sends command to the server.
@ -61,6 +63,10 @@ private:
std::mutex m_mutex; std::mutex m_mutex;
on_state_callback m_func; on_state_callback m_func;
} m_stateCallback; } m_stateCallback;
struct {
std::mutex m_mutex;
on_notice_callback m_func;
} m_noticeCallback;
struct t_Command { struct t_Command {
std::mutex m_mutex; std::mutex m_mutex;
@ -102,6 +108,9 @@ private:
void waitForAndSendCommand(); void waitForAndSendCommand();
void doNewCommand(); void doNewCommand();
void waitForResult(); void waitForResult();
void processNotice(const PGresult *result);
}; };
Thread m_threadData; Thread m_threadData;

View file

@ -29,7 +29,6 @@ const char * test_query =
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
// , queryCancel(nullptr)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -59,6 +58,11 @@ MainWindow::MainWindow(QWidget *parent)
{ {
QueueTask([this, st]() { connectionStateChanged(st); }); QueueTask([this, st]() { connectionStateChanged(st); });
}); });
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
{
QueueTask([this, details]() { receiveNotice(details); });
});
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -171,7 +175,7 @@ void MainWindow::query_ready(std::shared_ptr<Pgsql::Result> dbres)
else { else {
statusBar()->showMessage(tr("No tuples returned, possibly an error...")); statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
} }
//receiveNotice(dbres->diagDetails()); receiveNotice(dbres->diagDetails());
} }
} }
else { else {
@ -236,29 +240,22 @@ void MainWindow::cancel_query()
m_dbConnection.cancel(); m_dbConnection.cancel();
} }
//void MainWindow::processNotice(const PGresult *result) void MainWindow::receiveNotice(Pgsql::ErrorDetails notice)
//{ {
// qRegisterMetaType<Pgsql::ErrorDetails>("Pgsql::ErrorDetails"); QTextCursor cursor = ui->messagesEdit->textCursor();
// pg::ErrorDetails details = pg::ErrorDetails::createErrorDetailsFromPGresult(result); cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
// 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);
//// QString msg; // QString msg;
//// cursor.insertText("TEST\r\n"); // cursor.insertText("TEST\r\n");
// QTextTable *table = cursor.insertTable(4, 2); QTextTable *table = cursor.insertTable(4, 2);
// if (table) { if (table) {
// table->cellAt(1, 0).firstCursorPosition().insertText("State"); table->cellAt(1, 0).firstCursorPosition().insertText("State");
// table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state)); table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state));
// table->cellAt(2, 0).firstCursorPosition().insertText("Primary"); table->cellAt(2, 0).firstCursorPosition().insertText("Primary");
// table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary)); table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary));
// table->cellAt(3, 0).firstCursorPosition().insertText("Detail"); table->cellAt(3, 0).firstCursorPosition().insertText("Detail");
// table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail)); table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail));
// } }
//} }

View file

@ -60,12 +60,6 @@ private:
std::unique_ptr<QueryResultModel> resultModel; std::unique_ptr<QueryResultModel> resultModel;
std::unique_ptr<QueryExplainModel> explainModel; std::unique_ptr<QueryExplainModel> explainModel;
// struct {
// std::unique_ptr<QSocketNotifier> notifier;
// PostgresPollingStatusType poll_state;
// } connectingState;
// void processNotice(const PGresult *result);
void query_ready(std::shared_ptr<Pgsql::Result> res); void query_ready(std::shared_ptr<Pgsql::Result> res);
void explain_ready(std::shared_ptr<ExplainRoot> explain); void explain_ready(std::shared_ptr<ExplainRoot> explain);
private slots: private slots:
@ -74,10 +68,9 @@ private slots:
void performQuery(); void performQuery();
void performExplain(); void performExplain();
// void socket_activate_connect(int socket);
void cancel_query(); void cancel_query();
// void receiveNotice(Pgsql::ErrorDetails notice); void receiveNotice(Pgsql::ErrorDetails notice);
void processCallableQueue(); void processCallableQueue();
}; };