Het maken van de DB connectie gebeurd nu asynchroon.

This commit is contained in:
Eelke Klein 2016-12-27 15:41:11 +01:00
parent 27abce5a11
commit 3a8cc3d7f0
5 changed files with 207 additions and 18 deletions

View file

@ -4,6 +4,8 @@
#include "QueryResultModel.h"
#include "sqlhighlighter.h"
#include <windows.h>
namespace pg = Pgsql;
const char * test_query =
@ -29,22 +31,74 @@ MainWindow::MainWindow(QWidget *parent) :
//performQuery();
QAction *action = ui->mainToolBar->addAction("execute");
QAction *action;
action = ui->mainToolBar->addAction("connect");
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
action = ui->mainToolBar->addAction("execute");
connect(action, &QAction::triggered, this, &MainWindow::performQuery);
}
MainWindow::~MainWindow()
{}
void MainWindow::startConnect()
{
if (connection == nullptr) {
connection = std::make_unique<pg::Connection>();
}
QString connstr = ui->connectionStringEdit->text();
bool ok = connection->connectStart(connstr + " application_name=Ivory client_encoding=utf8");
if (ok && connection->status() != CONNECTION_BAD) {
// Start polling
int s = connection->socket();
connectingState.notifier = std::make_unique<QSocketNotifier>(s, QSocketNotifier::Write);
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
connectingState.poll_state = PGRES_POLLING_WRITING;
statusBar()->showMessage(tr("Connecting"));
}
else {
statusBar()->showMessage(tr("Connecting fail"));
}
}
void MainWindow::socket_activate_connect(int )
{
connectingState.poll_state = connection->connectPoll();
if (connectingState.poll_state == PGRES_POLLING_OK) {
statusBar()->showMessage(tr("Connected"));
connectingState.notifier.reset();
}
else if (connectingState.poll_state = PGRES_POLLING_FAILED) {
statusBar()->showMessage(tr("Connection failed"));
connectingState.notifier.reset();
}
else if (connectingState.poll_state == PGRES_POLLING_READING) {
statusBar()->showMessage(tr("Connecting.."));
connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Read);
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
}
else if (connectingState.poll_state == PGRES_POLLING_WRITING) {
statusBar()->showMessage(tr("Connecting..."));
connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Write);
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
}
}
void MainWindow::performQuery()
{
ui->ResultView->setModel(nullptr);
resultModel.reset();
QString connstr = ui->connectionStringEdit->text();
pg::Connection conn;
if (conn.connect(connstr + " application_name=Ivory client_encoding=utf8")) {
pg::Result dbres = conn.Query(ui->queryEdit->toPlainText());
if (connection->status() == CONNECTION_OK) {
pg::Result dbres = connection->query(ui->queryEdit->toPlainText());
resultModel.reset(new QueryResultModel(this , std::move(dbres)));
ui->ResultView->setModel(resultModel.get());
}