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;
|
return result != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExecStatusType Result::getResultStatus()
|
ExecStatusType Result::resultStatus()
|
||||||
{
|
{
|
||||||
return PQresultStatus(result);
|
return PQresultStatus(result);
|
||||||
}
|
}
|
||||||
|
|
@ -159,8 +159,6 @@ Result Connection::query(const char * command)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool Connection::sendQuery(const char *query)
|
bool Connection::sendQuery(const char *query)
|
||||||
{
|
{
|
||||||
int res = PQsendQuery(conn, query);
|
int res = PQsendQuery(conn, query);
|
||||||
|
|
@ -177,3 +175,15 @@ std::unique_ptr<Result> Connection::getResult()
|
||||||
return nullptr;
|
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;
|
// int row;
|
||||||
// };
|
// };
|
||||||
|
|
||||||
|
Result() = default;
|
||||||
Result(PGresult *result);
|
Result(PGresult *result);
|
||||||
~Result();
|
~Result();
|
||||||
|
|
||||||
|
|
@ -74,7 +75,7 @@ namespace Pgsql {
|
||||||
|
|
||||||
operator bool() const;
|
operator bool() const;
|
||||||
|
|
||||||
ExecStatusType getResultStatus();
|
ExecStatusType resultStatus();
|
||||||
std::string getResStatus();
|
std::string getResStatus();
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -129,6 +130,11 @@ namespace Pgsql {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sendQuery(const char * query);
|
bool sendQuery(const char * query);
|
||||||
|
bool sendQuery(const QString &command)
|
||||||
|
{
|
||||||
|
return sendQuery(command.toUtf8().data());
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Result> getResult();
|
std::unique_ptr<Result> getResult();
|
||||||
|
|
||||||
bool consumeInput();
|
bool consumeInput();
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,21 @@
|
||||||
#include "sqlhighlighter.h"
|
#include "sqlhighlighter.h"
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
//#include <thread>
|
||||||
|
|
||||||
namespace pg = Pgsql;
|
namespace pg = Pgsql;
|
||||||
|
|
||||||
const char * test_query =
|
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, "
|
//"SELECT id, program, version, lic_bedrijf, lic_plaats, "
|
||||||
"exception_message \nFROM foutrapport";
|
//"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) :
|
MainWindow::MainWindow(QWidget *parent) :
|
||||||
QMainWindow(parent),
|
QMainWindow(parent),
|
||||||
|
|
@ -29,8 +37,6 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
|
|
||||||
ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin");
|
ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin");
|
||||||
|
|
||||||
//performQuery();
|
|
||||||
|
|
||||||
QAction *action;
|
QAction *action;
|
||||||
action = ui->mainToolBar->addAction("connect");
|
action = ui->mainToolBar->addAction("connect");
|
||||||
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
||||||
|
|
@ -97,9 +103,24 @@ void MainWindow::performQuery()
|
||||||
ui->ResultView->setModel(nullptr);
|
ui->ResultView->setModel(nullptr);
|
||||||
resultModel.reset();
|
resultModel.reset();
|
||||||
|
|
||||||
if (connection->status() == CONNECTION_OK) {
|
QString command = ui->queryEdit->toPlainText();
|
||||||
pg::Result dbres = connection->query(ui->queryEdit->toPlainText());
|
queryFuture = std::async(std::launch::async, [this,command]()-> Pgsql::Result
|
||||||
resultModel.reset(new QueryResultModel(this , std::move(dbres)));
|
{
|
||||||
|
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());
|
ui->ResultView->setModel(resultModel.get());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QSocketNotifier>
|
#include <QSocketNotifier>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <future>
|
||||||
#include "PgsqlConn.h"
|
#include "PgsqlConn.h"
|
||||||
|
|
||||||
class QueryResultModel;
|
class QueryResultModel;
|
||||||
|
|
@ -34,17 +35,25 @@ private:
|
||||||
std::unique_ptr<Pgsql::Connection> connection;
|
std::unique_ptr<Pgsql::Connection> connection;
|
||||||
std::unique_ptr<QueryResultModel> resultModel;
|
std::unique_ptr<QueryResultModel> resultModel;
|
||||||
|
|
||||||
|
std::future<Pgsql::Result> queryFuture;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
std::unique_ptr<QSocketNotifier> notifier;
|
std::unique_ptr<QSocketNotifier> notifier;
|
||||||
PostgresPollingStatusType poll_state;
|
PostgresPollingStatusType poll_state;
|
||||||
} connectingState;
|
} connectingState;
|
||||||
|
|
||||||
|
// struct {
|
||||||
|
// std::unique_ptr<QSocketNotifier> notifierRead;
|
||||||
|
// std::unique_ptr<QSocketNotifier> notifierWrite;
|
||||||
|
// } queryState;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void startConnect();
|
void startConnect();
|
||||||
|
|
||||||
void performQuery();
|
void performQuery();
|
||||||
void socket_activate_connect(int socket);
|
void socket_activate_connect(int socket);
|
||||||
|
void query_ready();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue