diff --git a/PgsqlConn.cpp b/PgsqlConn.cpp index 6e5fdaf..d808a0d 100644 --- a/PgsqlConn.cpp +++ b/PgsqlConn.cpp @@ -41,7 +41,7 @@ Result::operator bool() const return result != nullptr; } -ExecStatusType Result::getResultStatus() +ExecStatusType Result::resultStatus() { return PQresultStatus(result); } @@ -159,8 +159,6 @@ Result Connection::query(const char * command) } - - bool Connection::sendQuery(const char *query) { int res = PQsendQuery(conn, query); @@ -177,3 +175,15 @@ std::unique_ptr Connection::getResult() return nullptr; } } + +bool Connection::consumeInput() +{ + int res = PQconsumeInput(conn); + return res == 1; +} + +bool Connection::isBusy() +{ + int res = PQisBusy(conn); + return res == 1; +} diff --git a/PgsqlConn.h b/PgsqlConn.h index 99285fa..2839281 100644 --- a/PgsqlConn.h +++ b/PgsqlConn.h @@ -63,6 +63,7 @@ namespace Pgsql { // int row; // }; + Result() = default; Result(PGresult *result); ~Result(); @@ -74,7 +75,7 @@ namespace Pgsql { operator bool() const; - ExecStatusType getResultStatus(); + ExecStatusType resultStatus(); std::string getResStatus(); @@ -129,6 +130,11 @@ namespace Pgsql { } bool sendQuery(const char * query); + bool sendQuery(const QString &command) + { + return sendQuery(command.toUtf8().data()); + } + std::unique_ptr getResult(); bool consumeInput(); diff --git a/mainwindow.cpp b/mainwindow.cpp index b6760e8..97d8ff9 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,13 +5,21 @@ #include "sqlhighlighter.h" #include +//#include namespace pg = Pgsql; const char * test_query = - "SELECT id, program, version, lic_bedrijf, lic_plaats, " - "lic_number, callstack_crc_1, callstack_crc_2, callstack_crc_3, exception_class, " - "exception_message \nFROM foutrapport"; + +//"SELECT id, program, version, lic_bedrijf, lic_plaats, " +//"lic_number, callstack_crc_1, callstack_crc_2, callstack_crc_3, exception_class, " +//"exception_message \nFROM foutrapport" + +"SELECT f1.id, f1.program, f1.version, f1.lic_number, f1.callstack_crc_1, f1.callstack_crc_2, array_agg(f2.id) \n" +"FROM foutrapport f1 JOIN foutrapport f2 USING (callstack_crc_2) \n" +"WHERE f1.actief \n" +"GROUP BY f1.id" +; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -29,8 +37,6 @@ MainWindow::MainWindow(QWidget *parent) : ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin"); - //performQuery(); - QAction *action; action = ui->mainToolBar->addAction("connect"); connect(action, &QAction::triggered, this, &MainWindow::startConnect); @@ -97,9 +103,24 @@ void MainWindow::performQuery() ui->ResultView->setModel(nullptr); resultModel.reset(); - if (connection->status() == CONNECTION_OK) { - pg::Result dbres = connection->query(ui->queryEdit->toPlainText()); - resultModel.reset(new QueryResultModel(this , std::move(dbres))); + QString command = ui->queryEdit->toPlainText(); + queryFuture = std::async(std::launch::async, [this,command]()-> Pgsql::Result + { + auto res = connection->query(command); + QMetaObject::invokeMethod(this, "query_ready", Qt::QueuedConnection); // queues on main thread + return res; + }); +} + +void MainWindow::query_ready() +{ + pg::Result dbres(std::move(queryFuture.get())); + if (dbres.resultStatus() == PGRES_TUPLES_OK) { + resultModel.reset(new QueryResultModel(nullptr , std::move(dbres))); ui->ResultView->setModel(resultModel.get()); } + else { + statusBar()->showMessage(tr("No tuples returned, possibly an error...")); + } } + diff --git a/mainwindow.h b/mainwindow.h index 0498929..7b1fdc6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "PgsqlConn.h" class QueryResultModel; @@ -34,17 +35,25 @@ private: std::unique_ptr connection; std::unique_ptr resultModel; + std::future queryFuture; + struct { std::unique_ptr notifier; PostgresPollingStatusType poll_state; } connectingState; +// struct { +// std::unique_ptr notifierRead; +// std::unique_ptr notifierWrite; +// } queryState; + private slots: void startConnect(); void performQuery(); void socket_activate_connect(int socket); + void query_ready(); }; #endif // MAINWINDOW_H