2017-08-23 13:27:23 +02:00
|
|
|
|
#include "DatabaseWindow.h"
|
|
|
|
|
|
#include "ui_DatabaseWindow.h"
|
2017-01-15 21:01:40 +01:00
|
|
|
|
#include <QTimer>
|
2017-08-24 21:12:32 +02:00
|
|
|
|
#include "GlobalIoService.h"
|
|
|
|
|
|
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
|
|
|
|
|
DatabaseWindow::DatabaseWindow(QWidget *parent) :
|
|
|
|
|
|
QMainWindow(parent),
|
2017-08-24 21:12:32 +02:00
|
|
|
|
ui(new Ui::DatabaseWindow),
|
|
|
|
|
|
m_dbConnection(*getGlobalAsioIoService())
|
2017-01-14 20:07:12 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
2017-01-15 21:01:40 +01:00
|
|
|
|
|
2017-12-28 07:28:21 +01:00
|
|
|
|
m_dbConnection.setStateChangeReceiver(
|
|
|
|
|
|
[this](auto s) { QueueTask([this, s]() { connectionStateChanged(s); }); });
|
|
|
|
|
|
m_dbConnection.setNoticeReceiver(
|
|
|
|
|
|
[this](auto n) { QueueTask([this, n]() { receiveNotice(n); }); });
|
2017-01-14 20:07:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DatabaseWindow::~DatabaseWindow()
|
|
|
|
|
|
{
|
2017-01-15 21:01:40 +01:00
|
|
|
|
m_dbConnection.closeConnection();
|
2017-01-14 20:07:12 +01:00
|
|
|
|
delete ui;
|
|
|
|
|
|
}
|
2017-01-15 21:01:40 +01:00
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::setConfig(const ConnectionConfig &config)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_config = config;
|
|
|
|
|
|
QString title = "pglab - ";
|
|
|
|
|
|
title += m_config.name().c_str();
|
|
|
|
|
|
setWindowTitle(title);
|
|
|
|
|
|
QueueTask([this]() { startConnect(); });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::QueueTask(TSQueue::t_Callable c)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_taskQueue.add(c);
|
|
|
|
|
|
// Theoretically this needs to be only called if the queue was empty because otherwise it already would
|
|
|
|
|
|
// be busy emptying the queue. For now however I think it is safer to call it just to make sure.
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCallableQueue", Qt::QueuedConnection); // queues on main thread
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::processCallableQueue()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_taskQueue.empty()) {
|
|
|
|
|
|
auto c = m_taskQueue.pop();
|
|
|
|
|
|
c();
|
|
|
|
|
|
if (!m_taskQueue.empty()) {
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(processCallableQueue()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::startConnect()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_dbConnection.setupConnection(m_config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::connectionStateChanged(ASyncDBConnection::State state)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString status_str;
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
|
case ASyncDBConnection::State::NotConnected:
|
|
|
|
|
|
status_str = tr("Geen verbinding");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::Connecting:
|
|
|
|
|
|
status_str = tr("Verbinden");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::Connected:
|
|
|
|
|
|
status_str = tr("Verbonden");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::QuerySend:
|
|
|
|
|
|
status_str = tr("Query verstuurd");
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::CancelSend:
|
|
|
|
|
|
status_str = tr("Query geannuleerd");
|
|
|
|
|
|
break;
|
2017-08-22 12:45:45 +02:00
|
|
|
|
case ASyncDBConnection::State::Terminating:
|
|
|
|
|
|
break;
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
// addLog(status_str);
|
|
|
|
|
|
statusBar()->showMessage(status_str);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DatabaseWindow::receiveNotice(Pgsql::ErrorDetails notice)
|
|
|
|
|
|
{
|
|
|
|
|
|
// QTextCursor cursor = ui->messagesEdit->textCursor();
|
|
|
|
|
|
// cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
|
|
|
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|