2016-12-26 16:06:55 +01:00
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "QueryResultModel.h"
|
2016-12-29 13:48:35 +01:00
|
|
|
|
#include "QueryExplainModel.h"
|
2016-12-26 16:06:55 +01:00
|
|
|
|
#include "sqlhighlighter.h"
|
2016-12-29 13:48:35 +01:00
|
|
|
|
#include <QTextTable>
|
2016-12-30 10:14:26 +01:00
|
|
|
|
#include <QTimer>
|
2016-12-27 15:41:11 +01:00
|
|
|
|
#include <windows.h>
|
2016-12-29 13:48:35 +01:00
|
|
|
|
#include "json/json.h"
|
|
|
|
|
|
#include "explaintreemodelitem.h"
|
|
|
|
|
|
|
2016-12-27 21:22:49 +01:00
|
|
|
|
//#include <thread>
|
2016-12-27 15:41:11 +01:00
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
namespace pg = Pgsql;
|
|
|
|
|
|
|
|
|
|
|
|
const char * test_query =
|
2016-12-27 21:22:49 +01:00
|
|
|
|
|
|
|
|
|
|
//"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"
|
|
|
|
|
|
;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
|
|
|
|
QMainWindow(parent),
|
2016-12-29 13:48:35 +01:00
|
|
|
|
ui(new Ui::MainWindow),
|
|
|
|
|
|
queryCancel(nullptr)
|
2016-12-26 16:06:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
|
|
|
|
QFont font;
|
|
|
|
|
|
font.setFamily("Source Code Pro");
|
|
|
|
|
|
font.setFixedPitch(true);
|
|
|
|
|
|
font.setPointSize(10);
|
|
|
|
|
|
ui->queryEdit->setFont(font);
|
|
|
|
|
|
highlighter.reset(new SqlHighlighter(ui->queryEdit->document()));
|
|
|
|
|
|
ui->queryEdit->setPlainText(test_query);
|
|
|
|
|
|
|
|
|
|
|
|
ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin");
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
QAction *action;
|
|
|
|
|
|
action = ui->mainToolBar->addAction("connect");
|
|
|
|
|
|
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
|
|
|
|
|
|
|
|
|
|
|
action = ui->mainToolBar->addAction("execute");
|
2016-12-26 16:06:55 +01:00
|
|
|
|
connect(action, &QAction::triggered, this, &MainWindow::performQuery);
|
2016-12-27 15:41:11 +01:00
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
action = ui->mainToolBar->addAction("explain");
|
|
|
|
|
|
connect(action, &QAction::triggered, this, &MainWindow::performExplain);
|
|
|
|
|
|
|
|
|
|
|
|
action = ui->mainToolBar->addAction("cancel");
|
|
|
|
|
|
connect(action, &QAction::triggered, this, &MainWindow::cancel_query);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
2016-12-30 10:14:26 +01:00
|
|
|
|
void MainWindow::QueueTask(callable c)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(m_mutexCallableQueue);
|
|
|
|
|
|
m_callableQueue.emplace_back(std::move(c));
|
|
|
|
|
|
// Theoretically this needs to be only called if the queue was empty because otherwise it already would
|
|
|
|
|
|
// be busy emptying the queue. For now however I think it is safer to call it just to make sure.
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCallableQueue", Qt::QueuedConnection); // queues on main thread
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::processCallableQueue()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool empty;
|
|
|
|
|
|
callable c;
|
|
|
|
|
|
{ // narrow scope for lock guard
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(m_mutexCallableQueue);
|
|
|
|
|
|
c = m_callableQueue.back();
|
|
|
|
|
|
m_callableQueue.pop_back();
|
|
|
|
|
|
empty = m_callableQueue.empty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c();
|
|
|
|
|
|
|
|
|
|
|
|
if (!empty) {
|
|
|
|
|
|
// This gives other events a chance to be processed to keep the UI snappy.
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(processCallableQueue()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
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"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::socket_activate_connect(int )
|
|
|
|
|
|
{
|
|
|
|
|
|
connectingState.poll_state = connection->connectPoll();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (connectingState.poll_state == PGRES_POLLING_OK) {
|
2016-12-29 13:48:35 +01:00
|
|
|
|
connection->setNoticeReceiver(
|
|
|
|
|
|
std::bind(&MainWindow::processNotice, this, std::placeholders::_1));
|
2016-12-27 15:41:11 +01:00
|
|
|
|
statusBar()->showMessage(tr("Connected"));
|
|
|
|
|
|
connectingState.notifier.reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (connectingState.poll_state = PGRES_POLLING_FAILED) {
|
|
|
|
|
|
statusBar()->showMessage(tr("Connection failed"));
|
|
|
|
|
|
connectingState.notifier.reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (connectingState.poll_state == PGRES_POLLING_READING) {
|
|
|
|
|
|
statusBar()->showMessage(tr("Connecting.."));
|
|
|
|
|
|
connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Read);
|
|
|
|
|
|
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (connectingState.poll_state == PGRES_POLLING_WRITING) {
|
|
|
|
|
|
statusBar()->showMessage(tr("Connecting..."));
|
|
|
|
|
|
connectingState.notifier = std::make_unique<QSocketNotifier>(connection->socket(), QSocketNotifier::Write);
|
|
|
|
|
|
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
void MainWindow::performQuery()
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->ResultView->setModel(nullptr);
|
|
|
|
|
|
resultModel.reset();
|
2016-12-29 13:48:35 +01:00
|
|
|
|
ui->messagesEdit->clear();
|
|
|
|
|
|
|
|
|
|
|
|
queryCancel = std::move(connection->getCancel());
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
2016-12-27 21:22:49 +01:00
|
|
|
|
QString command = ui->queryEdit->toPlainText();
|
2016-12-30 10:25:48 +01:00
|
|
|
|
std::thread([this,command]()
|
2016-12-27 21:22:49 +01:00
|
|
|
|
{
|
2016-12-30 10:25:48 +01:00
|
|
|
|
auto res = std::make_shared<Pgsql::Result>(connection->query(command));
|
|
|
|
|
|
QueueTask([this, res]() { query_ready(std::move(*res)); });
|
|
|
|
|
|
}).detach();
|
2016-12-27 21:22:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-30 10:25:48 +01:00
|
|
|
|
void MainWindow::query_ready(Pgsql::Result dbres)
|
2016-12-27 21:22:49 +01:00
|
|
|
|
{
|
2016-12-29 13:48:35 +01:00
|
|
|
|
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();
|
2016-12-30 10:38:46 +01:00
|
|
|
|
// explainFuture = std::async(std::launch::async, [this,command]()-> std::unique_ptr<ExplainRoot>
|
|
|
|
|
|
std::thread([this,command]()
|
2016-12-29 13:48:35 +01:00
|
|
|
|
{
|
2016-12-30 10:38:46 +01:00
|
|
|
|
std::shared_ptr<ExplainRoot> explain;
|
2016-12-29 13:48:35 +01:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-12-30 10:38:46 +01:00
|
|
|
|
QueueTask([this, explain]() { explain_ready(explain); });
|
|
|
|
|
|
}).detach();
|
2016-12-29 13:48:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-30 10:38:46 +01:00
|
|
|
|
void MainWindow::explain_ready(ExplainRoot::SPtr explain)
|
2016-12-29 13:48:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (explain) {
|
2016-12-30 10:38:46 +01:00
|
|
|
|
explainModel.reset(new QueryExplainModel(nullptr, explain));
|
2016-12-29 13:48:35 +01:00
|
|
|
|
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."));
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
2016-12-27 21:22:49 +01:00
|
|
|
|
else {
|
2016-12-29 13:48:35 +01:00
|
|
|
|
statusBar()->showMessage(tr("Explain failed."));
|
2016-12-27 21:22:49 +01:00
|
|
|
|
}
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
2016-12-27 21:22:49 +01:00
|
|
|
|
|
2016-12-29 13:48:35 +01:00
|
|
|
|
void MainWindow::cancel_query()
|
|
|
|
|
|
{
|
|
|
|
|
|
queryCancel.cancel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::processNotice(const PGresult *result)
|
|
|
|
|
|
{
|
|
|
|
|
|
qRegisterMetaType<Pgsql::ErrorDetails>("Pgsql::ErrorDetails");
|
|
|
|
|
|
pg::ErrorDetails details = pg::ErrorDetails::createErrorDetailsFromPGresult(result);
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "receiveNotice", Qt::AutoConnection, Q_ARG(Pgsql::ErrorDetails, details)); // queues on main thread
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::receiveNotice(Pgsql::ErrorDetails notice)
|
|
|
|
|
|
{
|
|
|
|
|
|
QTextCursor cursor = ui->messagesEdit->textCursor();
|
|
|
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QString msg;
|
|
|
|
|
|
// cursor.insertText("TEST\r\n");
|
|
|
|
|
|
|
|
|
|
|
|
QTextTable *table = cursor.insertTable(4, 2);
|
|
|
|
|
|
if (table) {
|
|
|
|
|
|
table->cellAt(1, 0).firstCursorPosition().insertText("State");
|
|
|
|
|
|
table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state));
|
|
|
|
|
|
table->cellAt(2, 0).firstCursorPosition().insertText("Primary");
|
|
|
|
|
|
table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary));
|
|
|
|
|
|
table->cellAt(3, 0).firstCursorPosition().insertText("Detail");
|
|
|
|
|
|
table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|