Query, Explain and Cancel are going throught the asyncdbconnection now.
Todo: Notice processing and error reporting.
This commit is contained in:
parent
fce51a7b7e
commit
a36bf5f7f4
11 changed files with 335 additions and 217 deletions
|
|
@ -171,11 +171,15 @@ Canceller::~Canceller()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Canceller::cancel()
|
bool Canceller::cancel(std::string *error)
|
||||||
{
|
{
|
||||||
const int errbuf_size = 256;
|
const int errbuf_size = 256;
|
||||||
char errbuf[errbuf_size];
|
char errbuf[errbuf_size];
|
||||||
PQcancel(m_cancel, errbuf, errbuf_size);
|
bool res = PQcancel(m_cancel, errbuf, errbuf_size);
|
||||||
|
if (!res && error) {
|
||||||
|
*error = errbuf;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -272,11 +276,11 @@ bool Connection::sendQuery(const char *query)
|
||||||
return res == 1;
|
return res == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Result> Connection::getResult()
|
std::shared_ptr<Result> Connection::getResult()
|
||||||
{
|
{
|
||||||
PGresult *r = PQgetResult(conn);
|
PGresult *r = PQgetResult(conn);
|
||||||
if (r) {
|
if (r) {
|
||||||
return std::make_unique<Result>(r);
|
return std::make_shared<Result>(r);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
||||||
12
PgsqlConn.h
12
PgsqlConn.h
|
|
@ -129,15 +129,17 @@ namespace Pgsql {
|
||||||
|
|
||||||
class Canceller {
|
class Canceller {
|
||||||
public:
|
public:
|
||||||
|
Canceller() = default;
|
||||||
Canceller(PGcancel *c);
|
Canceller(PGcancel *c);
|
||||||
Canceller(const Canceller&) = delete;
|
Canceller(const Canceller&) = delete;
|
||||||
Canceller& operator=(const Canceller&) = delete;
|
Canceller& operator=(const Canceller&) = delete;
|
||||||
Canceller(Canceller&& rhs);
|
Canceller(Canceller&& rhs);
|
||||||
Canceller& operator=(Canceller&& rhs);
|
Canceller& operator=(Canceller&& rhs);
|
||||||
~Canceller();
|
~Canceller();
|
||||||
void cancel();
|
|
||||||
|
bool cancel(std::string *error);
|
||||||
private:
|
private:
|
||||||
PGcancel *m_cancel;
|
PGcancel *m_cancel = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -187,12 +189,16 @@ namespace Pgsql {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sendQuery(const char * query);
|
bool sendQuery(const char * query);
|
||||||
|
bool sendQuery(const std::string &command)
|
||||||
|
{
|
||||||
|
return sendQuery(command.c_str());
|
||||||
|
}
|
||||||
bool sendQuery(const QString &command)
|
bool sendQuery(const QString &command)
|
||||||
{
|
{
|
||||||
return sendQuery(command.toUtf8().data());
|
return sendQuery(command.toUtf8().data());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Result> getResult();
|
std::shared_ptr<Result> getResult();
|
||||||
|
|
||||||
bool consumeInput();
|
bool consumeInput();
|
||||||
bool isBusy();
|
bool isBusy();
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,25 @@ void ASyncDBConnection::closeConnection()
|
||||||
m_thread.join();
|
m_thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ASyncDBConnection::send(const std::string &command, on_result_callback on_result)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lg(m_threadData.m_commandQueue.m_mutex);
|
||||||
|
m_threadData.m_commandQueue.m_queue.emplace(command, on_result);
|
||||||
|
m_threadData.m_commandQueue.m_newEvent.set();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ASyncDBConnection::cancel()
|
||||||
|
{
|
||||||
|
return m_threadData.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
std::lock_guard<std::mutex> lg(m_threadData.m_stateCallback.m_mutex);
|
||||||
m_threadData.m_stateCallback = state_callback;
|
m_threadData.m_stateCallback.m_func = state_callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASyncDBConnection::Thread::Thread()
|
ASyncDBConnection::Thread::Thread()
|
||||||
|
|
@ -31,21 +46,39 @@ ASyncDBConnection::Thread::Thread()
|
||||||
|
|
||||||
void ASyncDBConnection::Thread::run()
|
void ASyncDBConnection::Thread::run()
|
||||||
{
|
{
|
||||||
|
m_terminated = false;
|
||||||
while (!terminateRequested) {
|
while (!terminateRequested) {
|
||||||
|
|
||||||
// make or recover connection
|
// make or recover connection
|
||||||
if (makeConnection()) {
|
if (makeConnection()) {
|
||||||
|
m_canceller = m_connection.getCancel();
|
||||||
|
|
||||||
|
|
||||||
// send commands and receive results
|
// send commands and receive results
|
||||||
communicate();
|
communicate();
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// It is not possible to determine the source of the problem.
|
||||||
|
// Accept for PQconnectionNeedsPassword
|
||||||
|
|
||||||
|
// Pass problem to main thread and stop this thread
|
||||||
|
|
||||||
|
// Main thread needs to know it has to restart connecting if it want's to.
|
||||||
|
// TODO: add status functions to help main thread so it doesn't have to remember
|
||||||
|
// everything reported through callbacks.
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
m_terminated = true;
|
||||||
// close connection
|
// close connection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ASyncDBConnection::Thread::cancel()
|
||||||
|
{
|
||||||
|
return m_canceller.cancel(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
bool ASyncDBConnection::Thread::makeConnection()
|
bool ASyncDBConnection::Thread::makeConnection()
|
||||||
{
|
{
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
@ -122,18 +155,16 @@ void ASyncDBConnection::Thread::communicate()
|
||||||
// - 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(
|
if (m_state == State::Connected) {
|
||||||
whl.count(), // _In_ DWORD nCount,
|
waitForAndSendCommand();
|
||||||
whl, // _In_ const HANDLE *pHandles,
|
}
|
||||||
INFINITE, // _In_ DWORD dwMilliseconds,
|
else if (m_state == State::QuerySend || m_state == State::CancelSend) {
|
||||||
0, // _In_ DWORD dwWakeMask,
|
// Wait for result, even after a cancel we should wait, for all results
|
||||||
0 // _In_ DWORD dwFlags
|
// New command's are not excepted when one has been send
|
||||||
);
|
waitForResult();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,8 +176,105 @@ void ASyncDBConnection::Thread::stop()
|
||||||
|
|
||||||
void ASyncDBConnection::Thread::doStateCallback(State state)
|
void ASyncDBConnection::Thread::doStateCallback(State state)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lg(m_stateCallbackMutex);
|
m_state = state;
|
||||||
if (m_stateCallback) {
|
std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
|
||||||
m_stateCallback(state);
|
if (m_stateCallback.m_func) {
|
||||||
|
m_stateCallback.m_func(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ASyncDBConnection::Thread::waitForAndSendCommand()
|
||||||
|
{
|
||||||
|
WaitHandleList whl;
|
||||||
|
auto wait_result_new_command = whl.add(m_commandQueue.m_newEvent);
|
||||||
|
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
|
||||||
|
);
|
||||||
|
if (res == wait_result_new_command) {
|
||||||
|
doNewCommand();
|
||||||
|
}
|
||||||
|
// if (res == wait_result_stop) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASyncDBConnection::Thread::doNewCommand()
|
||||||
|
{
|
||||||
|
// todo: send command
|
||||||
|
// get command from top of queue (but leave it in the queue, we need the callback)
|
||||||
|
std::string command;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lg(m_commandQueue.m_mutex);
|
||||||
|
if (! m_commandQueue.m_queue.empty()) {
|
||||||
|
command = m_commandQueue.m_queue.front().command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!command.empty() && m_connection.sendQuery(command)) {
|
||||||
|
doStateCallback(State::QuerySend);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::string error = m_connection.getErrorMessage();
|
||||||
|
// todo: need to report the error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASyncDBConnection::Thread::waitForResult()
|
||||||
|
{
|
||||||
|
|
||||||
|
int sock = m_connection.socket();
|
||||||
|
Win32Event socket_event(Win32Event::Reset::Auto, Win32Event::Initial::Clear);
|
||||||
|
|
||||||
|
long fd = FD_WRITE;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
WSAEventSelect(sock, socket_event.handle(), fd);
|
||||||
|
|
||||||
|
WaitHandleList whl;
|
||||||
|
auto wait_result_socket = 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
|
||||||
|
);
|
||||||
|
if (res == wait_result_socket) {
|
||||||
|
if (m_connection.consumeInput()) {
|
||||||
|
if ( ! m_connection.isBusy()) {
|
||||||
|
auto res(m_connection.getResult());
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lg(m_commandQueue.m_mutex);
|
||||||
|
m_commandQueue.m_queue.front().on_result(res);
|
||||||
|
if (res == nullptr) {
|
||||||
|
m_commandQueue.m_queue.pop();
|
||||||
|
doStateCallback(State::Connected);
|
||||||
|
break; // leave the while loop
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// else is still waiting for more data
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// error during consume
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (res == wait_result_stop) {
|
||||||
|
// Send cancel, close connection and terminate thread
|
||||||
|
cancel();
|
||||||
|
doStateCallback(State::Terminating);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} // end while
|
||||||
|
// When last result received, remove command from queue
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include "win32event.h"
|
#include "win32event.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <queue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
|
@ -15,10 +16,11 @@ public:
|
||||||
Connecting,
|
Connecting,
|
||||||
Connected,
|
Connected,
|
||||||
QuerySend,
|
QuerySend,
|
||||||
CancelSend
|
CancelSend,
|
||||||
|
Terminating
|
||||||
};
|
};
|
||||||
|
|
||||||
using on_result_callback = std::function<void(Pgsql::Result)>;
|
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>)>;
|
||||||
using on_state_callback = std::function<void(State)>;
|
using on_state_callback = std::function<void(State)>;
|
||||||
|
|
||||||
ASyncDBConnection();
|
ASyncDBConnection();
|
||||||
|
|
@ -30,26 +32,44 @@ public:
|
||||||
|
|
||||||
/** Sends command to the server.
|
/** Sends command to the server.
|
||||||
|
|
||||||
When the result is in and no result_task_queue was passed then on_result will be
|
When the result is in on_result will be called directly within the thread.
|
||||||
called directly within the thread.
|
|
||||||
|
|
||||||
If the command gives multiple results on_result will be called for each result.
|
If the command gives multiple results on_result will be called for each result.
|
||||||
*/
|
*/
|
||||||
bool send(const std::string &command, on_result_callback on_result);
|
bool send(const std::string &command, on_result_callback on_result);
|
||||||
|
|
||||||
|
bool cancel();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
std::string command;
|
std::string command;
|
||||||
on_result_callback on_result;
|
on_result_callback on_result;
|
||||||
|
|
||||||
|
Command() = default;
|
||||||
|
Command(const std::string &cmd, on_result_callback cb)
|
||||||
|
: command(cmd), on_result(cb)
|
||||||
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// 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_stateCallback;
|
using t_CommandQueue = std::queue<Command>;
|
||||||
std::mutex m_stateCallbackMutex;
|
struct {
|
||||||
|
std::mutex m_mutex;
|
||||||
|
on_state_callback m_func;
|
||||||
|
} m_stateCallback;
|
||||||
|
|
||||||
|
struct t_Command {
|
||||||
|
std::mutex m_mutex;
|
||||||
|
t_CommandQueue m_queue;
|
||||||
|
Win32Event m_newEvent;
|
||||||
|
t_Command()
|
||||||
|
: m_newEvent(Win32Event::Reset::Auto, Win32Event::Initial::Clear)
|
||||||
|
{}
|
||||||
|
} m_commandQueue;
|
||||||
|
|
||||||
std::string m_initString;
|
std::string m_initString;
|
||||||
|
|
||||||
|
|
@ -59,21 +79,29 @@ private:
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
/// Sends a cancel request to the DB server
|
/// Sends a cancel request to the DB server
|
||||||
void cancel();
|
bool cancel();
|
||||||
|
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
State m_state = State::NotConnected;
|
||||||
|
|
||||||
Win32Event m_stopEvent;
|
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 m_terminated = true;
|
||||||
|
Pgsql::Canceller m_canceller;
|
||||||
|
|
||||||
|
|
||||||
bool makeConnection();
|
bool makeConnection();
|
||||||
void communicate();
|
void communicate();
|
||||||
|
|
||||||
void doStateCallback(State state);
|
void doStateCallback(State state);
|
||||||
|
/// Wait's for a command to come in and send's it to the server
|
||||||
|
void waitForAndSendCommand();
|
||||||
|
void doNewCommand();
|
||||||
|
void waitForResult();
|
||||||
};
|
};
|
||||||
|
|
||||||
Thread m_threadData;
|
Thread m_threadData;
|
||||||
|
|
|
||||||
283
mainwindow.cpp
283
mainwindow.cpp
|
|
@ -46,14 +46,14 @@ 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)
|
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||||
{
|
{
|
||||||
|
|
@ -112,174 +112,129 @@ void MainWindow::connectionStateChanged(ASyncDBConnection::State state)
|
||||||
|
|
||||||
void MainWindow::startConnect()
|
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"));
|
|
||||||
// }
|
|
||||||
|
|
||||||
std::string connstr = ui->connectionStringEdit->text().toUtf8().data();
|
std::string connstr = ui->connectionStringEdit->text().toUtf8().data();
|
||||||
m_dbConnection.setupConnection(connstr);
|
m_dbConnection.setupConnection(connstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
//void MainWindow::socket_activate_connect(int )
|
void MainWindow::performQuery()
|
||||||
//{
|
{
|
||||||
// connectingState.poll_state = connection->connectPoll();
|
ui->ResultView->setModel(nullptr);
|
||||||
|
resultModel.reset();
|
||||||
|
ui->messagesEdit->clear();
|
||||||
|
|
||||||
|
QString command = ui->queryEdit->toPlainText();
|
||||||
|
std::string cmd = command.toUtf8().data();
|
||||||
|
m_dbConnection.send(cmd,
|
||||||
|
[this](std::shared_ptr<Pgsql::Result> res)
|
||||||
|
{
|
||||||
|
QueueTask([this, res]() { query_ready(res); });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// if (connectingState.poll_state == PGRES_POLLING_OK) {
|
void MainWindow::query_ready(std::shared_ptr<Pgsql::Result> dbres)
|
||||||
// connection->setNoticeReceiver(
|
{
|
||||||
// std::bind(&MainWindow::processNotice, this, std::placeholders::_1));
|
if (dbres) {
|
||||||
// statusBar()->showMessage(tr("Connected"));
|
auto st = dbres->resultStatus();
|
||||||
// connectingState.notifier.reset();
|
if (st == PGRES_TUPLES_OK) {
|
||||||
// }
|
resultModel.reset(new QueryResultModel(nullptr , dbres));
|
||||||
// else if (connectingState.poll_state == PGRES_POLLING_FAILED) {
|
ui->ResultView->setModel(resultModel.get());
|
||||||
// statusBar()->showMessage(tr("Connection failed"));
|
statusBar()->showMessage(tr("Query ready."));
|
||||||
// connectingState.notifier.reset();
|
}
|
||||||
// }
|
else {
|
||||||
// else if (connectingState.poll_state == PGRES_POLLING_READING) {
|
if (st == PGRES_EMPTY_QUERY) {
|
||||||
// statusBar()->showMessage(tr("Connecting.."));
|
statusBar()->showMessage(tr("Empty query."));
|
||||||
// connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Read);
|
}
|
||||||
// connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
else if (st == PGRES_COMMAND_OK) {
|
||||||
// }
|
statusBar()->showMessage(tr("Command OK."));
|
||||||
// else if (connectingState.poll_state == PGRES_POLLING_WRITING) {
|
}
|
||||||
// statusBar()->showMessage(tr("Connecting..."));
|
else if (st == PGRES_COPY_OUT) {
|
||||||
// connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Write);
|
statusBar()->showMessage(tr("COPY OUT."));
|
||||||
// connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
}
|
||||||
// }
|
else if (st == PGRES_COPY_IN) {
|
||||||
//}
|
statusBar()->showMessage(tr("COPY IN."));
|
||||||
|
}
|
||||||
|
else if (st == PGRES_BAD_RESPONSE) {
|
||||||
|
statusBar()->showMessage(tr("BAD RESPONSE."));
|
||||||
|
}
|
||||||
|
else if (st == PGRES_NONFATAL_ERROR) {
|
||||||
|
statusBar()->showMessage(tr("NON FATAL ERROR."));
|
||||||
|
}
|
||||||
|
else if (st == PGRES_FATAL_ERROR) {
|
||||||
|
statusBar()->showMessage(tr("FATAL ERROR."));
|
||||||
|
}
|
||||||
|
else if (st == PGRES_COPY_BOTH) {
|
||||||
|
statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
|
||||||
|
}
|
||||||
|
else if (st == PGRES_SINGLE_TUPLE) {
|
||||||
|
statusBar()->showMessage(tr("SINGLE TUPLE result."));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
|
||||||
|
}
|
||||||
|
//receiveNotice(dbres->diagDetails());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
statusBar()->showMessage(tr("Query cancelled."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::performExplain()
|
||||||
|
{
|
||||||
|
ui->ResultView->setModel(nullptr);
|
||||||
|
resultModel.reset();
|
||||||
|
ui->messagesEdit->clear();
|
||||||
|
|
||||||
//void MainWindow::performQuery()
|
QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) ";
|
||||||
//{
|
command += ui->queryEdit->toPlainText();
|
||||||
// ui->ResultView->setModel(nullptr);
|
std::string cmd = command.toUtf8().data();
|
||||||
// resultModel.reset();
|
m_dbConnection.send(cmd,
|
||||||
// ui->messagesEdit->clear();
|
[this](std::shared_ptr<Pgsql::Result> res)
|
||||||
|
{
|
||||||
|
if (res) {
|
||||||
|
// Process explain data seperately
|
||||||
|
std::thread([this,res]()
|
||||||
|
{
|
||||||
|
std::shared_ptr<ExplainRoot> explain;
|
||||||
|
if (res->getCols() == 1 && res->getRows() == 1) {
|
||||||
|
std::string s = res->getVal(0, 0);
|
||||||
|
Json::Value root; // will contains the root value after parsing.
|
||||||
|
Json::Reader reader;
|
||||||
|
bool parsingSuccessful = reader.parse(s, root);
|
||||||
|
if (parsingSuccessful) {
|
||||||
|
explain = ExplainRoot::createFromJson(root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QueueTask([this, explain]() { explain_ready(explain); });
|
||||||
|
}).detach();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// queryCancel = std::move(connection->getCancel());
|
void MainWindow::explain_ready(ExplainRoot::SPtr explain)
|
||||||
|
{
|
||||||
|
if (explain) {
|
||||||
|
explainModel.reset(new QueryExplainModel(nullptr, explain));
|
||||||
|
ui->explainTreeView->setModel(explainModel.get());
|
||||||
|
ui->explainTreeView->expandAll();
|
||||||
|
ui->explainTreeView->setColumnWidth(0, 200);
|
||||||
|
ui->explainTreeView->setColumnWidth(1, 80);
|
||||||
|
ui->explainTreeView->setColumnWidth(2, 80);
|
||||||
|
ui->explainTreeView->setColumnWidth(3, 80);
|
||||||
|
ui->explainTreeView->setColumnWidth(4, 80);
|
||||||
|
ui->explainTreeView->setColumnWidth(5, 80);
|
||||||
|
ui->explainTreeView->setColumnWidth(6, 600);
|
||||||
|
statusBar()->showMessage(tr("Explain ready."));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
statusBar()->showMessage(tr("Explain failed."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// QString command = ui->queryEdit->toPlainText();
|
void MainWindow::cancel_query()
|
||||||
|
{
|
||||||
// std::thread([this,command]()
|
m_dbConnection.cancel();
|
||||||
// {
|
}
|
||||||
// auto res = std::make_shared<Pgsql::Result>(connection->query(command));
|
|
||||||
// QueueTask([this, res]() { query_ready(std::move(*res)); });
|
|
||||||
// }).detach();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void MainWindow::query_ready(Pgsql::Result dbres)
|
|
||||||
//{
|
|
||||||
// if (dbres) {
|
|
||||||
// auto st = dbres.resultStatus();
|
|
||||||
// if (st == PGRES_TUPLES_OK) {
|
|
||||||
// resultModel.reset(new QueryResultModel(nullptr , std::move(dbres)));
|
|
||||||
// ui->ResultView->setModel(resultModel.get());
|
|
||||||
// statusBar()->showMessage(tr("Query ready."));
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// if (st == PGRES_EMPTY_QUERY) {
|
|
||||||
// statusBar()->showMessage(tr("Empty query."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_COMMAND_OK) {
|
|
||||||
// statusBar()->showMessage(tr("Command OK."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_COPY_OUT) {
|
|
||||||
// statusBar()->showMessage(tr("COPY OUT."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_COPY_IN) {
|
|
||||||
// statusBar()->showMessage(tr("COPY IN."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_BAD_RESPONSE) {
|
|
||||||
// statusBar()->showMessage(tr("BEAD RESPONSE."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_NONFATAL_ERROR) {
|
|
||||||
// statusBar()->showMessage(tr("NON FATAL ERROR."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_FATAL_ERROR) {
|
|
||||||
// statusBar()->showMessage(tr("FATAL ERROR."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_COPY_BOTH) {
|
|
||||||
// statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
|
|
||||||
// }
|
|
||||||
// else if (st == PGRES_SINGLE_TUPLE) {
|
|
||||||
// statusBar()->showMessage(tr("SINGLE TUPLE result."));
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
|
|
||||||
// }
|
|
||||||
// receiveNotice(dbres.diagDetails());
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// statusBar()->showMessage(tr("Query cancelled."));
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void MainWindow::performExplain()
|
|
||||||
//{
|
|
||||||
// ui->ResultView->setModel(nullptr);
|
|
||||||
// resultModel.reset();
|
|
||||||
// ui->messagesEdit->clear();
|
|
||||||
|
|
||||||
// queryCancel = std::move(connection->getCancel());
|
|
||||||
|
|
||||||
// QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) ";
|
|
||||||
// command += ui->queryEdit->toPlainText();
|
|
||||||
//// explainFuture = std::async(std::launch::async, [this,command]()-> std::unique_ptr<ExplainRoot>
|
|
||||||
// std::thread([this,command]()
|
|
||||||
// {
|
|
||||||
// std::shared_ptr<ExplainRoot> explain;
|
|
||||||
// auto res = connection->query(command);
|
|
||||||
// if (res.getCols() == 1 && res.getRows() == 1) {
|
|
||||||
// std::string s = res.getVal(0, 0);
|
|
||||||
// Json::Value root; // will contains the root value after parsing.
|
|
||||||
// Json::Reader reader;
|
|
||||||
// bool parsingSuccessful = reader.parse(s, root);
|
|
||||||
// if (parsingSuccessful) {
|
|
||||||
// explain = ExplainRoot::createFromJson(root);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// QueueTask([this, explain]() { explain_ready(explain); });
|
|
||||||
// }).detach();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void MainWindow::explain_ready(ExplainRoot::SPtr explain)
|
|
||||||
//{
|
|
||||||
// if (explain) {
|
|
||||||
// explainModel.reset(new QueryExplainModel(nullptr, explain));
|
|
||||||
// ui->explainTreeView->setModel(explainModel.get());
|
|
||||||
// ui->explainTreeView->expandAll();
|
|
||||||
// ui->explainTreeView->setColumnWidth(0, 200);
|
|
||||||
// ui->explainTreeView->setColumnWidth(1, 80);
|
|
||||||
// ui->explainTreeView->setColumnWidth(2, 80);
|
|
||||||
// ui->explainTreeView->setColumnWidth(3, 80);
|
|
||||||
// ui->explainTreeView->setColumnWidth(4, 80);
|
|
||||||
// ui->explainTreeView->setColumnWidth(5, 80);
|
|
||||||
// ui->explainTreeView->setColumnWidth(6, 600);
|
|
||||||
// statusBar()->showMessage(tr("Explain ready."));
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// statusBar()->showMessage(tr("Explain failed."));
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void MainWindow::cancel_query()
|
|
||||||
//{
|
|
||||||
// queryCancel.cancel();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void MainWindow::processNotice(const PGresult *result)
|
//void MainWindow::processNotice(const PGresult *result)
|
||||||
//{
|
//{
|
||||||
|
|
|
||||||
18
mainwindow.h
18
mainwindow.h
|
|
@ -57,12 +57,8 @@ private:
|
||||||
|
|
||||||
void connectionStateChanged(ASyncDBConnection::State state);
|
void connectionStateChanged(ASyncDBConnection::State state);
|
||||||
|
|
||||||
// std::unique_ptr<Pgsql::Connection> connection;
|
std::unique_ptr<QueryResultModel> resultModel;
|
||||||
// std::unique_ptr<QueryResultModel> resultModel;
|
std::unique_ptr<QueryExplainModel> explainModel;
|
||||||
// std::unique_ptr<QueryExplainModel> explainModel;
|
|
||||||
|
|
||||||
// Pgsql::Canceller queryCancel;
|
|
||||||
|
|
||||||
|
|
||||||
// struct {
|
// struct {
|
||||||
// std::unique_ptr<QSocketNotifier> notifier;
|
// std::unique_ptr<QSocketNotifier> notifier;
|
||||||
|
|
@ -70,17 +66,17 @@ private:
|
||||||
// } connectingState;
|
// } connectingState;
|
||||||
|
|
||||||
// void processNotice(const PGresult *result);
|
// void processNotice(const PGresult *result);
|
||||||
// void query_ready(Pgsql::Result res);
|
void query_ready(std::shared_ptr<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();
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@
|
||||||
<widget class="QTextEdit" name="queryEdit"/>
|
<widget class="QTextEdit" name="queryEdit"/>
|
||||||
<widget class="QTabWidget" name="tabWidget">
|
<widget class="QTabWidget" name="tabWidget">
|
||||||
<property name="currentIndex">
|
<property name="currentIndex">
|
||||||
<number>2</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="messageTab">
|
<widget class="QWidget" name="messageTab">
|
||||||
<attribute name="title">
|
<attribute name="title">
|
||||||
|
|
@ -147,7 +147,7 @@
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>726</width>
|
<width>726</width>
|
||||||
<height>26</height>
|
<height>25</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QMenu" name="menuTest">
|
<widget class="QMenu" name="menuTest">
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#include "queryresultmodel.h"
|
#include "queryresultmodel.h"
|
||||||
|
|
||||||
QueryResultModel::QueryResultModel(QObject *parent, Pgsql::Result &&r)
|
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r)
|
||||||
: QAbstractTableModel(parent)
|
: QAbstractTableModel(parent)
|
||||||
, result(std::move(r))
|
, result(std::move(r))
|
||||||
{}
|
{}
|
||||||
|
|
@ -8,13 +8,13 @@ QueryResultModel::QueryResultModel(QObject *parent, Pgsql::Result &&r)
|
||||||
|
|
||||||
int QueryResultModel::rowCount(const QModelIndex &) const
|
int QueryResultModel::rowCount(const QModelIndex &) const
|
||||||
{
|
{
|
||||||
int r = result.getRows();
|
int r = result->getRows();
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int QueryResultModel::columnCount(const QModelIndex &) const
|
int QueryResultModel::columnCount(const QModelIndex &) const
|
||||||
{
|
{
|
||||||
int r = result.getCols();
|
int r = result->getCols();
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
||||||
int col = index.column();
|
int col = index.column();
|
||||||
if (role == Qt::DisplayRole) {
|
if (role == Qt::DisplayRole) {
|
||||||
|
|
||||||
r = QString(result.getVal(col, rij));
|
r = QString(result->getVal(col, rij));
|
||||||
}
|
}
|
||||||
else if (role == Qt::TextAlignmentRole) {
|
else if (role == Qt::TextAlignmentRole) {
|
||||||
if (col == 0) {
|
if (col == 0) {
|
||||||
|
|
@ -43,7 +43,7 @@ QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation,
|
||||||
QVariant r;
|
QVariant r;
|
||||||
if (role == Qt::DisplayRole) {
|
if (role == Qt::DisplayRole) {
|
||||||
if (orientation == Qt::Horizontal) {
|
if (orientation == Qt::Horizontal) {
|
||||||
r = QString(result.getColName(section));
|
r = QString(result->getColName(section));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
r = QString::number(section + 1);
|
r = QString::number(section + 1);
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ class QueryResultModel : public QAbstractTableModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
QueryResultModel(QObject *parent, Pgsql::Result &&r);
|
QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r);
|
||||||
|
|
||||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
|
|
@ -17,7 +17,7 @@ public:
|
||||||
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
// virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pgsql::Result result;
|
std::shared_ptr<Pgsql::Result> result;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QUERYRESULTMODEL_H
|
#endif // QUERYRESULTMODEL_H
|
||||||
|
|
|
||||||
|
|
@ -24,4 +24,5 @@ private:
|
||||||
t_CallableQueue futureQueue;
|
t_CallableQueue futureQueue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // TSQUEUE_H
|
#endif // TSQUEUE_H
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ public:
|
||||||
MainWindow->setCentralWidget(centralWidget);
|
MainWindow->setCentralWidget(centralWidget);
|
||||||
menuBar = new QMenuBar(MainWindow);
|
menuBar = new QMenuBar(MainWindow);
|
||||||
menuBar->setObjectName(QStringLiteral("menuBar"));
|
menuBar->setObjectName(QStringLiteral("menuBar"));
|
||||||
menuBar->setGeometry(QRect(0, 0, 726, 26));
|
menuBar->setGeometry(QRect(0, 0, 726, 25));
|
||||||
menuTest = new QMenu(menuBar);
|
menuTest = new QMenu(menuBar);
|
||||||
menuTest->setObjectName(QStringLiteral("menuTest"));
|
menuTest->setObjectName(QStringLiteral("menuTest"));
|
||||||
MainWindow->setMenuBar(menuBar);
|
MainWindow->setMenuBar(menuBar);
|
||||||
|
|
@ -151,7 +151,7 @@ public:
|
||||||
|
|
||||||
retranslateUi(MainWindow);
|
retranslateUi(MainWindow);
|
||||||
|
|
||||||
tabWidget->setCurrentIndex(2);
|
tabWidget->setCurrentIndex(1);
|
||||||
|
|
||||||
|
|
||||||
QMetaObject::connectSlotsByName(MainWindow);
|
QMetaObject::connectSlotsByName(MainWindow);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue