Query is now executed using std::async
This commit is contained in:
parent
3a8cc3d7f0
commit
0d30dc9080
4 changed files with 58 additions and 12 deletions
|
|
@ -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<Result> Connection::getResult()
|
|||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool Connection::consumeInput()
|
||||
{
|
||||
int res = PQconsumeInput(conn);
|
||||
return res == 1;
|
||||
}
|
||||
|
||||
bool Connection::isBusy()
|
||||
{
|
||||
int res = PQisBusy(conn);
|
||||
return res == 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Result> getResult();
|
||||
|
||||
bool consumeInput();
|
||||
|
|
|
|||
|
|
@ -5,13 +5,21 @@
|
|||
#include "sqlhighlighter.h"
|
||||
|
||||
#include <windows.h>
|
||||
//#include <thread>
|
||||
|
||||
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..."));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <QMainWindow>
|
||||
#include <QSocketNotifier>
|
||||
#include <memory>
|
||||
#include <future>
|
||||
#include "PgsqlConn.h"
|
||||
|
||||
class QueryResultModel;
|
||||
|
|
@ -34,17 +35,25 @@ private:
|
|||
std::unique_ptr<Pgsql::Connection> connection;
|
||||
std::unique_ptr<QueryResultModel> resultModel;
|
||||
|
||||
std::future<Pgsql::Result> queryFuture;
|
||||
|
||||
struct {
|
||||
std::unique_ptr<QSocketNotifier> notifier;
|
||||
PostgresPollingStatusType poll_state;
|
||||
} connectingState;
|
||||
|
||||
// struct {
|
||||
// std::unique_ptr<QSocketNotifier> notifierRead;
|
||||
// std::unique_ptr<QSocketNotifier> notifierWrite;
|
||||
// } queryState;
|
||||
|
||||
private slots:
|
||||
|
||||
void startConnect();
|
||||
|
||||
void performQuery();
|
||||
void socket_activate_connect(int socket);
|
||||
void query_ready();
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue