The previous async method's are being updated to the ASyncDBConnection class. Connecting is working.
This commit is contained in:
parent
2d420c0525
commit
fce51a7b7e
7 changed files with 363 additions and 215 deletions
|
|
@ -13,7 +13,7 @@ TEMPLATE = app
|
||||||
|
|
||||||
INCLUDEPATH += C:\prog\include
|
INCLUDEPATH += C:\prog\include
|
||||||
DEFINES += WIN32_LEAN_AND_MEAN
|
DEFINES += WIN32_LEAN_AND_MEAN
|
||||||
LIBS += c:\prog\lib\libpq.lib
|
LIBS += c:\prog\lib\libpq.lib User32.lib
|
||||||
|
|
||||||
SOURCES += main.cpp\
|
SOURCES += main.cpp\
|
||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
|
|
@ -27,7 +27,8 @@ SOURCES += main.cpp\
|
||||||
explaintreemodelitem.cpp \
|
explaintreemodelitem.cpp \
|
||||||
asyncdbconnection.cpp \
|
asyncdbconnection.cpp \
|
||||||
tsqueue.cpp \
|
tsqueue.cpp \
|
||||||
win32event.cpp
|
win32event.cpp \
|
||||||
|
waithandlelist.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
serverproperties.h \
|
serverproperties.h \
|
||||||
|
|
@ -39,7 +40,8 @@ HEADERS += mainwindow.h \
|
||||||
explaintreemodelitem.h \
|
explaintreemodelitem.h \
|
||||||
asyncdbconnection.h \
|
asyncdbconnection.h \
|
||||||
tsqueue.h \
|
tsqueue.h \
|
||||||
win32event.h
|
win32event.h \
|
||||||
|
waithandlelist.h
|
||||||
|
|
||||||
FORMS += mainwindow.ui \
|
FORMS += mainwindow.ui \
|
||||||
serverproperties.ui
|
serverproperties.ui
|
||||||
|
|
|
||||||
|
|
@ -160,6 +160,12 @@ namespace Pgsql {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool connectStart(const char *params);
|
bool connectStart(const char *params);
|
||||||
|
|
||||||
|
bool connectStart(const std::string ¶ms)
|
||||||
|
{
|
||||||
|
return connectStart(params.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
bool connectStart(const QString ¶ms)
|
bool connectStart(const QString ¶ms)
|
||||||
{
|
{
|
||||||
return connectStart(params.toUtf8().data());
|
return connectStart(params.toUtf8().data());
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#include "asyncdbconnection.h"
|
#include "asyncdbconnection.h"
|
||||||
|
#include "waithandlelist.h"
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
ASyncDBConnection::ASyncDBConnection()
|
ASyncDBConnection::ASyncDBConnection()
|
||||||
{
|
{
|
||||||
|
|
@ -6,14 +8,26 @@ ASyncDBConnection::ASyncDBConnection()
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASyncDBConnection::setupConnection(const std::string &connstring)
|
void ASyncDBConnection::setupConnection(const std::string &connstring)
|
||||||
{}
|
{
|
||||||
|
m_threadData.m_initString = connstring;
|
||||||
|
m_thread = std::thread([this] () { m_threadData.run(); });
|
||||||
|
}
|
||||||
|
|
||||||
void ASyncDBConnection::closeConnection()
|
void ASyncDBConnection::closeConnection()
|
||||||
{}
|
{
|
||||||
|
m_threadData.stop();
|
||||||
|
m_thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
void ASyncDBConnection::setStateCallback(on_state_callback state_callback)
|
void ASyncDBConnection::setStateCallback(on_state_callback state_callback)
|
||||||
{}
|
{
|
||||||
|
std::lock_guard<std::mutex> lg(m_threadData.m_stateCallbackMutex);
|
||||||
|
m_threadData.m_stateCallback = state_callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASyncDBConnection::Thread::Thread()
|
||||||
|
: m_stopEvent(Win32Event::Reset::Manual, Win32Event::Initial::Clear)
|
||||||
|
{}
|
||||||
|
|
||||||
void ASyncDBConnection::Thread::run()
|
void ASyncDBConnection::Thread::run()
|
||||||
{
|
{
|
||||||
|
|
@ -21,6 +35,8 @@ void ASyncDBConnection::Thread::run()
|
||||||
|
|
||||||
// make or recover connection
|
// make or recover connection
|
||||||
if (makeConnection()) {
|
if (makeConnection()) {
|
||||||
|
|
||||||
|
|
||||||
// send commands and receive results
|
// send commands and receive results
|
||||||
communicate();
|
communicate();
|
||||||
}
|
}
|
||||||
|
|
@ -32,14 +48,62 @@ void ASyncDBConnection::Thread::run()
|
||||||
|
|
||||||
bool ASyncDBConnection::Thread::makeConnection()
|
bool ASyncDBConnection::Thread::makeConnection()
|
||||||
{
|
{
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
while (!terminateRequested) {
|
while (!terminateRequested) {
|
||||||
|
|
||||||
// start connecting
|
// start connecting
|
||||||
|
bool ok = m_connection.connectStart(m_initString + " client_encoding=utf8");
|
||||||
|
auto start = std::chrono::steady_clock::now();
|
||||||
|
if (ok && m_connection.status() != CONNECTION_BAD) {
|
||||||
|
int sock = m_connection.socket();
|
||||||
|
Win32Event socket_event(Win32Event::Reset::Auto, Win32Event::Initial::Clear);
|
||||||
|
|
||||||
// poll till complete or failed (we can get an abort command)
|
long fd = FD_WRITE;
|
||||||
|
while (true) {
|
||||||
|
// poll till complete or failed (we can get an abort command)
|
||||||
|
WSAEventSelect(sock, socket_event.handle(), fd);
|
||||||
|
WaitHandleList whl;
|
||||||
|
auto wait_result_socket_event = whl.add(socket_event);
|
||||||
|
auto wait_result_stop = whl.add(m_stopEvent);
|
||||||
|
|
||||||
// if connected return true
|
auto nu = std::chrono::steady_clock::now();
|
||||||
// else retry (unless we get a command to stop then return false)
|
std::chrono::duration<float, std::milli> diff = -(nu-start);
|
||||||
|
diff += 30s;
|
||||||
|
DWORD timeout = diff.count();
|
||||||
|
|
||||||
|
DWORD res = MsgWaitForMultipleObjectsEx(
|
||||||
|
whl.count(), // _In_ DWORD nCount,
|
||||||
|
whl, // _In_ const HANDLE *pHandles,
|
||||||
|
timeout, // _In_ DWORD dwMilliseconds,
|
||||||
|
0, // _In_ DWORD dwWakeMask,
|
||||||
|
0 // _In_ DWORD dwFlags
|
||||||
|
);
|
||||||
|
if (res == wait_result_socket_event) {
|
||||||
|
auto poll_state = m_connection.connectPoll();
|
||||||
|
if (poll_state == PGRES_POLLING_OK) {
|
||||||
|
// if connected return true
|
||||||
|
doStateCallback(State::Connected);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (poll_state == PGRES_POLLING_FAILED) {
|
||||||
|
doStateCallback(State::NotConnected);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (poll_state == PGRES_POLLING_READING) {
|
||||||
|
doStateCallback(State::Connecting);
|
||||||
|
fd = FD_READ;
|
||||||
|
}
|
||||||
|
else if (poll_state == PGRES_POLLING_WRITING) {
|
||||||
|
doStateCallback(State::Connecting);
|
||||||
|
fd = FD_WRITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (res == wait_result_stop) {
|
||||||
|
|
||||||
|
}
|
||||||
|
} // end while (true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -57,5 +121,32 @@ void ASyncDBConnection::Thread::communicate()
|
||||||
// - return
|
// - return
|
||||||
// - stop signal
|
// - stop signal
|
||||||
// - return
|
// - return
|
||||||
|
|
||||||
|
// WSAEventSelect(sock, socket_event.handle(), fd);
|
||||||
|
WaitHandleList whl;
|
||||||
|
// auto wait_result_socket_event = whl.add(socket_event);
|
||||||
|
auto wait_result_stop = whl.add(m_stopEvent);
|
||||||
|
|
||||||
|
DWORD res = MsgWaitForMultipleObjectsEx(
|
||||||
|
whl.count(), // _In_ DWORD nCount,
|
||||||
|
whl, // _In_ const HANDLE *pHandles,
|
||||||
|
INFINITE, // _In_ DWORD dwMilliseconds,
|
||||||
|
0, // _In_ DWORD dwWakeMask,
|
||||||
|
0 // _In_ DWORD dwFlags
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASyncDBConnection::Thread::stop()
|
||||||
|
{
|
||||||
|
terminateRequested = true;
|
||||||
|
m_stopEvent.set();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASyncDBConnection::Thread::doStateCallback(State state)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lg(m_stateCallbackMutex);
|
||||||
|
if (m_stateCallback) {
|
||||||
|
m_stateCallback(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,11 @@
|
||||||
#define ASYNCDBCONNECTION_H
|
#define ASYNCDBCONNECTION_H
|
||||||
|
|
||||||
#include "PgsqlConn.h"
|
#include "PgsqlConn.h"
|
||||||
|
#include "win32event.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
class ASyncDBConnection {
|
class ASyncDBConnection {
|
||||||
public:
|
public:
|
||||||
|
|
@ -45,24 +48,36 @@ private:
|
||||||
/// Contains all the members accessed by the thread
|
/// Contains all the members accessed by the thread
|
||||||
class Thread {
|
class Thread {
|
||||||
public:
|
public:
|
||||||
on_state_callback m_state_callback;
|
on_state_callback m_stateCallback;
|
||||||
std::string m_init_string;
|
std::mutex m_stateCallbackMutex;
|
||||||
|
|
||||||
|
std::string m_initString;
|
||||||
|
|
||||||
|
Thread();
|
||||||
|
|
||||||
/// Is started as a seperate thread by ASyncDBConnection
|
/// Is started as a seperate thread by ASyncDBConnection
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
/// Sends a cancel request to the DB server
|
/// Sends a cancel request to the DB server
|
||||||
void cancel();
|
void cancel();
|
||||||
|
|
||||||
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Win32Event m_stopEvent;
|
||||||
Pgsql::Connection m_connection;
|
Pgsql::Connection m_connection;
|
||||||
bool terminateRequested = false; ///< is set when the thread should stop
|
bool terminateRequested = false; ///< is set when the thread should stop
|
||||||
|
|
||||||
|
|
||||||
bool makeConnection();
|
bool makeConnection();
|
||||||
void communicate();
|
void communicate();
|
||||||
|
|
||||||
|
void doStateCallback(State state);
|
||||||
};
|
};
|
||||||
|
|
||||||
Thread thread;
|
Thread m_threadData;
|
||||||
|
std::thread m_thread;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ASYNCDBCONNECTION_H
|
#endif // ASYNCDBCONNECTION_H
|
||||||
|
|
|
||||||
396
mainwindow.cpp
396
mainwindow.cpp
|
|
@ -26,10 +26,10 @@ const char * test_query =
|
||||||
"GROUP BY f1.id"
|
"GROUP BY f1.id"
|
||||||
;
|
;
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent) :
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
QMainWindow(parent),
|
: QMainWindow(parent)
|
||||||
ui(new Ui::MainWindow),
|
, ui(new Ui::MainWindow)
|
||||||
queryCancel(nullptr)
|
// , queryCancel(nullptr)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
|
@ -46,18 +46,27 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
action = ui->mainToolBar->addAction("connect");
|
action = ui->mainToolBar->addAction("connect");
|
||||||
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
||||||
|
|
||||||
action = ui->mainToolBar->addAction("execute");
|
// action = ui->mainToolBar->addAction("execute");
|
||||||
connect(action, &QAction::triggered, this, &MainWindow::performQuery);
|
// connect(action, &QAction::triggered, this, &MainWindow::performQuery);
|
||||||
|
|
||||||
action = ui->mainToolBar->addAction("explain");
|
// action = ui->mainToolBar->addAction("explain");
|
||||||
connect(action, &QAction::triggered, this, &MainWindow::performExplain);
|
// connect(action, &QAction::triggered, this, &MainWindow::performExplain);
|
||||||
|
|
||||||
action = ui->mainToolBar->addAction("cancel");
|
// action = ui->mainToolBar->addAction("cancel");
|
||||||
connect(action, &QAction::triggered, this, &MainWindow::cancel_query);
|
// connect(action, &QAction::triggered, this, &MainWindow::cancel_query);
|
||||||
|
|
||||||
|
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||||
|
{
|
||||||
|
QueueTask([this, st]() { connectionStateChanged(st); });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
{}
|
{
|
||||||
|
m_dbConnection.closeConnection();
|
||||||
|
m_dbConnection.setStateCallback(nullptr);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::QueueTask(TSQueue::t_Callable c)
|
void MainWindow::QueueTask(TSQueue::t_Callable c)
|
||||||
{
|
{
|
||||||
|
|
@ -78,198 +87,223 @@ void MainWindow::processCallableQueue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::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;
|
||||||
|
}
|
||||||
|
statusBar()->showMessage(status_str);
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::startConnect()
|
void MainWindow::startConnect()
|
||||||
{
|
{
|
||||||
if (connection == nullptr) {
|
// if (connection == nullptr) {
|
||||||
connection = std::make_unique<pg::Connection>();
|
// connection = std::make_unique<pg::Connection>();
|
||||||
}
|
// }
|
||||||
QString connstr = ui->connectionStringEdit->text();
|
// QString connstr = ui->connectionStringEdit->text();
|
||||||
bool ok = connection->connectStart(connstr + " application_name=Ivory client_encoding=utf8");
|
// bool ok = connection->connectStart(connstr + " application_name=Ivory client_encoding=utf8");
|
||||||
if (ok && connection->status() != CONNECTION_BAD) {
|
// if (ok && connection->status() != CONNECTION_BAD) {
|
||||||
// Start polling
|
// // Start polling
|
||||||
int s = connection->socket();
|
// int s = connection->socket();
|
||||||
|
|
||||||
connectingState.notifier = std::make_unique<QSocketNotifier>(s, QSocketNotifier::Write);
|
// connectingState.notifier = std::make_unique<QSocketNotifier>(s, QSocketNotifier::Write);
|
||||||
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
// connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
||||||
|
|
||||||
connectingState.poll_state = PGRES_POLLING_WRITING;
|
// connectingState.poll_state = PGRES_POLLING_WRITING;
|
||||||
statusBar()->showMessage(tr("Connecting"));
|
// statusBar()->showMessage(tr("Connecting"));
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
statusBar()->showMessage(tr("Connecting fail"));
|
// statusBar()->showMessage(tr("Connecting fail"));
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
std::string connstr = ui->connectionStringEdit->text().toUtf8().data();
|
||||||
|
m_dbConnection.setupConnection(connstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::socket_activate_connect(int )
|
//void MainWindow::socket_activate_connect(int )
|
||||||
{
|
//{
|
||||||
connectingState.poll_state = connection->connectPoll();
|
// connectingState.poll_state = connection->connectPoll();
|
||||||
|
|
||||||
|
|
||||||
if (connectingState.poll_state == PGRES_POLLING_OK) {
|
// if (connectingState.poll_state == PGRES_POLLING_OK) {
|
||||||
connection->setNoticeReceiver(
|
// connection->setNoticeReceiver(
|
||||||
std::bind(&MainWindow::processNotice, this, std::placeholders::_1));
|
// std::bind(&MainWindow::processNotice, this, std::placeholders::_1));
|
||||||
statusBar()->showMessage(tr("Connected"));
|
// statusBar()->showMessage(tr("Connected"));
|
||||||
connectingState.notifier.reset();
|
// connectingState.notifier.reset();
|
||||||
}
|
// }
|
||||||
else if (connectingState.poll_state = PGRES_POLLING_FAILED) {
|
// else if (connectingState.poll_state == PGRES_POLLING_FAILED) {
|
||||||
statusBar()->showMessage(tr("Connection failed"));
|
// statusBar()->showMessage(tr("Connection failed"));
|
||||||
connectingState.notifier.reset();
|
// connectingState.notifier.reset();
|
||||||
}
|
// }
|
||||||
else if (connectingState.poll_state == PGRES_POLLING_READING) {
|
// else if (connectingState.poll_state == PGRES_POLLING_READING) {
|
||||||
statusBar()->showMessage(tr("Connecting.."));
|
// statusBar()->showMessage(tr("Connecting.."));
|
||||||
connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Read);
|
// connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Read);
|
||||||
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
// connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
||||||
}
|
// }
|
||||||
else if (connectingState.poll_state == PGRES_POLLING_WRITING) {
|
// else if (connectingState.poll_state == PGRES_POLLING_WRITING) {
|
||||||
statusBar()->showMessage(tr("Connecting..."));
|
// statusBar()->showMessage(tr("Connecting..."));
|
||||||
connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Write);
|
// connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Write);
|
||||||
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
// connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
void MainWindow::performQuery()
|
//void MainWindow::performQuery()
|
||||||
{
|
//{
|
||||||
ui->ResultView->setModel(nullptr);
|
// ui->ResultView->setModel(nullptr);
|
||||||
resultModel.reset();
|
// resultModel.reset();
|
||||||
ui->messagesEdit->clear();
|
// ui->messagesEdit->clear();
|
||||||
|
|
||||||
queryCancel = std::move(connection->getCancel());
|
// queryCancel = std::move(connection->getCancel());
|
||||||
|
|
||||||
QString command = ui->queryEdit->toPlainText();
|
// QString command = ui->queryEdit->toPlainText();
|
||||||
|
|
||||||
std::thread([this,command]()
|
// std::thread([this,command]()
|
||||||
{
|
// {
|
||||||
auto res = std::make_shared<Pgsql::Result>(connection->query(command));
|
// auto res = std::make_shared<Pgsql::Result>(connection->query(command));
|
||||||
QueueTask([this, res]() { query_ready(std::move(*res)); });
|
// QueueTask([this, res]() { query_ready(std::move(*res)); });
|
||||||
}).detach();
|
// }).detach();
|
||||||
}
|
//}
|
||||||
|
|
||||||
void MainWindow::query_ready(Pgsql::Result dbres)
|
//void MainWindow::query_ready(Pgsql::Result dbres)
|
||||||
{
|
//{
|
||||||
if (dbres) {
|
// if (dbres) {
|
||||||
auto st = dbres.resultStatus();
|
// auto st = dbres.resultStatus();
|
||||||
if (st == PGRES_TUPLES_OK) {
|
// if (st == PGRES_TUPLES_OK) {
|
||||||
resultModel.reset(new QueryResultModel(nullptr , std::move(dbres)));
|
// resultModel.reset(new QueryResultModel(nullptr , std::move(dbres)));
|
||||||
ui->ResultView->setModel(resultModel.get());
|
// ui->ResultView->setModel(resultModel.get());
|
||||||
statusBar()->showMessage(tr("Query ready."));
|
// statusBar()->showMessage(tr("Query ready."));
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
if (st == PGRES_EMPTY_QUERY) {
|
// if (st == PGRES_EMPTY_QUERY) {
|
||||||
statusBar()->showMessage(tr("Empty query."));
|
// statusBar()->showMessage(tr("Empty query."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_COMMAND_OK) {
|
// else if (st == PGRES_COMMAND_OK) {
|
||||||
statusBar()->showMessage(tr("Command OK."));
|
// statusBar()->showMessage(tr("Command OK."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_COPY_OUT) {
|
// else if (st == PGRES_COPY_OUT) {
|
||||||
statusBar()->showMessage(tr("COPY OUT."));
|
// statusBar()->showMessage(tr("COPY OUT."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_COPY_IN) {
|
// else if (st == PGRES_COPY_IN) {
|
||||||
statusBar()->showMessage(tr("COPY IN."));
|
// statusBar()->showMessage(tr("COPY IN."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_BAD_RESPONSE) {
|
// else if (st == PGRES_BAD_RESPONSE) {
|
||||||
statusBar()->showMessage(tr("BEAD RESPONSE."));
|
// statusBar()->showMessage(tr("BEAD RESPONSE."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_NONFATAL_ERROR) {
|
// else if (st == PGRES_NONFATAL_ERROR) {
|
||||||
statusBar()->showMessage(tr("NON FATAL ERROR."));
|
// statusBar()->showMessage(tr("NON FATAL ERROR."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_FATAL_ERROR) {
|
// else if (st == PGRES_FATAL_ERROR) {
|
||||||
statusBar()->showMessage(tr("FATAL ERROR."));
|
// statusBar()->showMessage(tr("FATAL ERROR."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_COPY_BOTH) {
|
// else if (st == PGRES_COPY_BOTH) {
|
||||||
statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
|
// statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
|
||||||
}
|
// }
|
||||||
else if (st == PGRES_SINGLE_TUPLE) {
|
// else if (st == PGRES_SINGLE_TUPLE) {
|
||||||
statusBar()->showMessage(tr("SINGLE TUPLE result."));
|
// statusBar()->showMessage(tr("SINGLE TUPLE result."));
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
|
// statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
|
||||||
}
|
// }
|
||||||
receiveNotice(dbres.diagDetails());
|
// receiveNotice(dbres.diagDetails());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
statusBar()->showMessage(tr("Query cancelled."));
|
// statusBar()->showMessage(tr("Query cancelled."));
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
void MainWindow::performExplain()
|
//void MainWindow::performExplain()
|
||||||
{
|
//{
|
||||||
ui->ResultView->setModel(nullptr);
|
// ui->ResultView->setModel(nullptr);
|
||||||
resultModel.reset();
|
// resultModel.reset();
|
||||||
ui->messagesEdit->clear();
|
// ui->messagesEdit->clear();
|
||||||
|
|
||||||
queryCancel = std::move(connection->getCancel());
|
// queryCancel = std::move(connection->getCancel());
|
||||||
|
|
||||||
QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) ";
|
// QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) ";
|
||||||
command += ui->queryEdit->toPlainText();
|
// command += ui->queryEdit->toPlainText();
|
||||||
// explainFuture = std::async(std::launch::async, [this,command]()-> std::unique_ptr<ExplainRoot>
|
//// explainFuture = std::async(std::launch::async, [this,command]()-> std::unique_ptr<ExplainRoot>
|
||||||
std::thread([this,command]()
|
// std::thread([this,command]()
|
||||||
{
|
// {
|
||||||
std::shared_ptr<ExplainRoot> explain;
|
// std::shared_ptr<ExplainRoot> explain;
|
||||||
auto res = connection->query(command);
|
// auto res = connection->query(command);
|
||||||
if (res.getCols() == 1 && res.getRows() == 1) {
|
// if (res.getCols() == 1 && res.getRows() == 1) {
|
||||||
std::string s = res.getVal(0, 0);
|
// std::string s = res.getVal(0, 0);
|
||||||
Json::Value root; // will contains the root value after parsing.
|
// Json::Value root; // will contains the root value after parsing.
|
||||||
Json::Reader reader;
|
// Json::Reader reader;
|
||||||
bool parsingSuccessful = reader.parse(s, root);
|
// bool parsingSuccessful = reader.parse(s, root);
|
||||||
if (parsingSuccessful) {
|
// if (parsingSuccessful) {
|
||||||
explain = ExplainRoot::createFromJson(root);
|
// explain = ExplainRoot::createFromJson(root);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
QueueTask([this, explain]() { explain_ready(explain); });
|
// QueueTask([this, explain]() { explain_ready(explain); });
|
||||||
}).detach();
|
// }).detach();
|
||||||
}
|
//}
|
||||||
|
|
||||||
void MainWindow::explain_ready(ExplainRoot::SPtr explain)
|
//void MainWindow::explain_ready(ExplainRoot::SPtr explain)
|
||||||
{
|
//{
|
||||||
if (explain) {
|
// if (explain) {
|
||||||
explainModel.reset(new QueryExplainModel(nullptr, explain));
|
// explainModel.reset(new QueryExplainModel(nullptr, explain));
|
||||||
ui->explainTreeView->setModel(explainModel.get());
|
// ui->explainTreeView->setModel(explainModel.get());
|
||||||
ui->explainTreeView->expandAll();
|
// ui->explainTreeView->expandAll();
|
||||||
ui->explainTreeView->setColumnWidth(0, 200);
|
// ui->explainTreeView->setColumnWidth(0, 200);
|
||||||
ui->explainTreeView->setColumnWidth(1, 80);
|
// ui->explainTreeView->setColumnWidth(1, 80);
|
||||||
ui->explainTreeView->setColumnWidth(2, 80);
|
// ui->explainTreeView->setColumnWidth(2, 80);
|
||||||
ui->explainTreeView->setColumnWidth(3, 80);
|
// ui->explainTreeView->setColumnWidth(3, 80);
|
||||||
ui->explainTreeView->setColumnWidth(4, 80);
|
// ui->explainTreeView->setColumnWidth(4, 80);
|
||||||
ui->explainTreeView->setColumnWidth(5, 80);
|
// ui->explainTreeView->setColumnWidth(5, 80);
|
||||||
ui->explainTreeView->setColumnWidth(6, 600);
|
// ui->explainTreeView->setColumnWidth(6, 600);
|
||||||
statusBar()->showMessage(tr("Explain ready."));
|
// statusBar()->showMessage(tr("Explain ready."));
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
statusBar()->showMessage(tr("Explain failed."));
|
// statusBar()->showMessage(tr("Explain failed."));
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
void MainWindow::cancel_query()
|
//void MainWindow::cancel_query()
|
||||||
{
|
//{
|
||||||
queryCancel.cancel();
|
// queryCancel.cancel();
|
||||||
}
|
//}
|
||||||
|
|
||||||
void MainWindow::processNotice(const PGresult *result)
|
//void MainWindow::processNotice(const PGresult *result)
|
||||||
{
|
//{
|
||||||
qRegisterMetaType<Pgsql::ErrorDetails>("Pgsql::ErrorDetails");
|
// qRegisterMetaType<Pgsql::ErrorDetails>("Pgsql::ErrorDetails");
|
||||||
pg::ErrorDetails details = pg::ErrorDetails::createErrorDetailsFromPGresult(result);
|
// pg::ErrorDetails details = pg::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||||
QMetaObject::invokeMethod(this, "receiveNotice", Qt::AutoConnection, Q_ARG(Pgsql::ErrorDetails, details)); // queues on main thread
|
// QMetaObject::invokeMethod(this, "receiveNotice", Qt::AutoConnection, Q_ARG(Pgsql::ErrorDetails, details)); // queues on main thread
|
||||||
}
|
//}
|
||||||
|
|
||||||
void MainWindow::receiveNotice(Pgsql::ErrorDetails notice)
|
//void MainWindow::receiveNotice(Pgsql::ErrorDetails notice)
|
||||||
{
|
//{
|
||||||
QTextCursor cursor = ui->messagesEdit->textCursor();
|
// QTextCursor cursor = ui->messagesEdit->textCursor();
|
||||||
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
// cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
||||||
|
|
||||||
|
|
||||||
// QString msg;
|
//// QString msg;
|
||||||
// cursor.insertText("TEST\r\n");
|
//// cursor.insertText("TEST\r\n");
|
||||||
|
|
||||||
QTextTable *table = cursor.insertTable(4, 2);
|
// QTextTable *table = cursor.insertTable(4, 2);
|
||||||
if (table) {
|
// if (table) {
|
||||||
table->cellAt(1, 0).firstCursorPosition().insertText("State");
|
// table->cellAt(1, 0).firstCursorPosition().insertText("State");
|
||||||
table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state));
|
// table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state));
|
||||||
table->cellAt(2, 0).firstCursorPosition().insertText("Primary");
|
// table->cellAt(2, 0).firstCursorPosition().insertText("Primary");
|
||||||
table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary));
|
// table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary));
|
||||||
table->cellAt(3, 0).firstCursorPosition().insertText("Detail");
|
// table->cellAt(3, 0).firstCursorPosition().insertText("Detail");
|
||||||
table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail));
|
// table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail));
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
|
||||||
38
mainwindow.h
38
mainwindow.h
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef MAINWINDOW_H
|
#ifndef MAINWINDOW_H
|
||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
|
#include "asyncdbconnection.h"
|
||||||
#include "tsqueue.h"
|
#include "tsqueue.h"
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QSocketNotifier>
|
#include <QSocketNotifier>
|
||||||
|
|
@ -52,36 +53,35 @@ private:
|
||||||
std::unique_ptr<Ui::MainWindow> ui;
|
std::unique_ptr<Ui::MainWindow> ui;
|
||||||
std::unique_ptr<SqlHighlighter> highlighter;
|
std::unique_ptr<SqlHighlighter> highlighter;
|
||||||
|
|
||||||
std::unique_ptr<Pgsql::Connection> connection;
|
ASyncDBConnection m_dbConnection;
|
||||||
std::unique_ptr<QueryResultModel> resultModel;
|
|
||||||
std::unique_ptr<QueryExplainModel> explainModel;
|
|
||||||
|
|
||||||
Pgsql::Canceller queryCancel;
|
void connectionStateChanged(ASyncDBConnection::State state);
|
||||||
|
|
||||||
|
// std::unique_ptr<Pgsql::Connection> connection;
|
||||||
|
// std::unique_ptr<QueryResultModel> resultModel;
|
||||||
|
// std::unique_ptr<QueryExplainModel> explainModel;
|
||||||
|
|
||||||
|
// Pgsql::Canceller queryCancel;
|
||||||
|
|
||||||
struct {
|
|
||||||
std::unique_ptr<QSocketNotifier> notifier;
|
|
||||||
PostgresPollingStatusType poll_state;
|
|
||||||
} connectingState;
|
|
||||||
|
|
||||||
// struct {
|
// struct {
|
||||||
// std::unique_ptr<QSocketNotifier> notifierRead;
|
// std::unique_ptr<QSocketNotifier> notifier;
|
||||||
// std::unique_ptr<QSocketNotifier> notifierWrite;
|
// PostgresPollingStatusType poll_state;
|
||||||
// } queryState;
|
// } connectingState;
|
||||||
|
|
||||||
void processNotice(const PGresult *result);
|
// void processNotice(const PGresult *result);
|
||||||
void query_ready(Pgsql::Result res);
|
// void query_ready(Pgsql::Result res);
|
||||||
void explain_ready(std::shared_ptr<ExplainRoot> explain);
|
// void explain_ready(std::shared_ptr<ExplainRoot> explain);
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void startConnect();
|
void startConnect();
|
||||||
|
|
||||||
void performQuery();
|
// void performQuery();
|
||||||
void performExplain();
|
// void performExplain();
|
||||||
void socket_activate_connect(int socket);
|
// void socket_activate_connect(int socket);
|
||||||
|
|
||||||
void cancel_query();
|
// void cancel_query();
|
||||||
void receiveNotice(Pgsql::ErrorDetails notice);
|
// void receiveNotice(Pgsql::ErrorDetails notice);
|
||||||
|
|
||||||
void processCallableQueue();
|
void processCallableQueue();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ void TSQueue::add(t_Callable callable)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> g(m);
|
std::lock_guard<std::mutex> g(m);
|
||||||
futureQueue.push_back(std::move(callable));
|
futureQueue.push_back(std::move(callable));
|
||||||
newData.Set();
|
newData.set();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TSQueue::empty()
|
bool TSQueue::empty()
|
||||||
|
|
@ -23,12 +23,12 @@ TSQueue::t_Callable TSQueue::pop()
|
||||||
auto f = std::move(futureQueue.front());
|
auto f = std::move(futureQueue.front());
|
||||||
futureQueue.pop_front();
|
futureQueue.pop_front();
|
||||||
if (futureQueue.empty()) {
|
if (futureQueue.empty()) {
|
||||||
newData.Reset();
|
newData.reset();
|
||||||
}
|
}
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
HANDLE TSQueue::getNewDataEventHandle()
|
HANDLE TSQueue::getNewDataEventHandle()
|
||||||
{
|
{
|
||||||
return newData.GetHandle();
|
return newData.handle();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue