Added explain functionality.
Uses json format with jsoncpp as a parser. Then show it in a QTreeView. Shows inclusive/exclusive times like explain.despesz does. Also a similar coloring scheme as applied.
This commit is contained in:
parent
0d30dc9080
commit
8af6bc4ac5
14 changed files with 9089 additions and 33 deletions
144
mainwindow.cpp
144
mainwindow.cpp
|
|
@ -2,9 +2,13 @@
|
|||
#include "ui_mainwindow.h"
|
||||
|
||||
#include "QueryResultModel.h"
|
||||
#include "QueryExplainModel.h"
|
||||
#include "sqlhighlighter.h"
|
||||
|
||||
#include <QTextTable>
|
||||
#include <windows.h>
|
||||
#include "json/json.h"
|
||||
#include "explaintreemodelitem.h"
|
||||
|
||||
//#include <thread>
|
||||
|
||||
namespace pg = Pgsql;
|
||||
|
|
@ -23,7 +27,8 @@ const char * test_query =
|
|||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
ui(new Ui::MainWindow),
|
||||
queryCancel(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
|
|
@ -44,6 +49,11 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
action = ui->mainToolBar->addAction("execute");
|
||||
connect(action, &QAction::triggered, this, &MainWindow::performQuery);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
|
@ -78,6 +88,8 @@ void MainWindow::socket_activate_connect(int )
|
|||
|
||||
|
||||
if (connectingState.poll_state == PGRES_POLLING_OK) {
|
||||
connection->setNoticeReceiver(
|
||||
std::bind(&MainWindow::processNotice, this, std::placeholders::_1));
|
||||
statusBar()->showMessage(tr("Connected"));
|
||||
connectingState.notifier.reset();
|
||||
}
|
||||
|
|
@ -102,6 +114,9 @@ void MainWindow::performQuery()
|
|||
{
|
||||
ui->ResultView->setModel(nullptr);
|
||||
resultModel.reset();
|
||||
ui->messagesEdit->clear();
|
||||
|
||||
queryCancel = std::move(connection->getCancel());
|
||||
|
||||
QString command = ui->queryEdit->toPlainText();
|
||||
queryFuture = std::async(std::launch::async, [this,command]()-> Pgsql::Result
|
||||
|
|
@ -115,12 +130,129 @@ void MainWindow::performQuery()
|
|||
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());
|
||||
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("No tuples returned, possibly an error..."));
|
||||
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::unique_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);
|
||||
}
|
||||
}
|
||||
QMetaObject::invokeMethod(this, "explain_ready", Qt::QueuedConnection); // queues on main thread
|
||||
return explain;
|
||||
});
|
||||
}
|
||||
|
||||
void MainWindow::explain_ready()
|
||||
{
|
||||
std::unique_ptr<ExplainRoot> explain(explainFuture.get());
|
||||
if (explain) {
|
||||
explainModel.reset(new QueryExplainModel(nullptr, std::move(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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue