Moved everything associated with executing and explaining queries to querytab.
It is now possible to create multiple independent query tabs. However somethings are currently a bit broken.
This commit is contained in:
parent
6c268bd774
commit
7f379f3b80
13 changed files with 668 additions and 634 deletions
BIN
icons/page_white_add.png
Normal file
BIN
icons/page_white_add.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
494
mainwindow.cpp
494
mainwindow.cpp
|
|
@ -1,76 +1,22 @@
|
|||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
#include "QueryResultModel.h"
|
||||
#include "QueryExplainModel.h"
|
||||
#include "sqlhighlighter.h"
|
||||
//#include "QueryResultModel.h"
|
||||
//#include "QueryExplainModel.h"
|
||||
#include <QStandardPaths>
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QTextDocumentFragment>
|
||||
#include <QTextStream>
|
||||
#include <QTextTable>
|
||||
#include <QTimer>
|
||||
#include <QElapsedTimer>
|
||||
#include <windows.h>
|
||||
#include "json/json.h"
|
||||
#include "explaintreemodelitem.h"
|
||||
#include <algorithm>
|
||||
#include <QCloseEvent>
|
||||
#include <querytab.h>
|
||||
|
||||
#include "util.h"
|
||||
//#include <thread>
|
||||
|
||||
namespace {
|
||||
|
||||
// Supported range from microseconds to seconds
|
||||
// min:sec to hours::min::sec
|
||||
QString msfloatToHumanReadableString(float ms)
|
||||
{
|
||||
QString unit;
|
||||
float val;
|
||||
int deci = 2;
|
||||
if (ms < 1.0f) {
|
||||
val = ms * 1000.f;
|
||||
//result = QString::asprintf("%0.3f", ms * 1000.0f);
|
||||
unit = u8"μs";
|
||||
}
|
||||
else if (ms >= 1000.0) {
|
||||
val = ms / 1000.0f;
|
||||
unit = "s";
|
||||
if (val >= 60.0) {
|
||||
int secs = val;
|
||||
int min = secs / 60.0;
|
||||
secs -= min * 60;
|
||||
if (min >= 60) {
|
||||
int hour = min / 60;
|
||||
min -= hour * 60;
|
||||
return QString::asprintf("%d:%02d:%02d", hour, min, secs);
|
||||
}
|
||||
else {
|
||||
return QString::asprintf("%02d:%02d", min, secs);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
val = ms;
|
||||
unit = "ms";
|
||||
}
|
||||
|
||||
// if (val >= 1000.f) {
|
||||
// deci = 0;
|
||||
// }
|
||||
// else
|
||||
if (val >= 100.f) {
|
||||
deci = 0;
|
||||
}
|
||||
else if (val >= 10.f) {
|
||||
deci = 1;
|
||||
}
|
||||
QString result = QString::asprintf("%0.*f", deci, val);
|
||||
return result + unit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace pg = Pgsql;
|
||||
|
||||
|
|
@ -89,71 +35,56 @@ const char * test_query =
|
|||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindow)
|
||||
, m_queryTab(new QueryTab)
|
||||
{
|
||||
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");
|
||||
|
||||
// QAction *action;
|
||||
// action = ui->mainToolBar->addAction("connect");
|
||||
// connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
||||
|
||||
// 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);
|
||||
|
||||
//ui->ResultView->setItemDelegate();
|
||||
|
||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||
{
|
||||
QueueTask([this, st]() { connectionStateChanged(st); });
|
||||
});
|
||||
|
||||
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
|
||||
{
|
||||
QueueTask([this, details]() { receiveNotice(details); });
|
||||
});
|
||||
|
||||
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &MainWindow::queryTextChanged);
|
||||
|
||||
m_timeElapsedLabel = new QLabel(this);
|
||||
statusBar()->addPermanentWidget(m_timeElapsedLabel);
|
||||
|
||||
|
||||
{
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
layout->addWidget(m_queryTab);
|
||||
layout->setMargin(4);
|
||||
ui->tab_2->setLayout(layout);
|
||||
}
|
||||
// {
|
||||
// QVBoxLayout *layout = new QVBoxLayout;
|
||||
// layout->addWidget(m_queryTab);
|
||||
// layout->setMargin(4);
|
||||
// ui->tab_2->setLayout(layout);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
m_dbConnection.closeConnection();
|
||||
m_dbConnection.setStateCallback(nullptr);
|
||||
// m_dbConnection.closeConnection();
|
||||
// m_dbConnection.setStateCallback(nullptr);
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::newSqlPage()
|
||||
{
|
||||
QueryTab *qt = new QueryTab(this);
|
||||
qt->setConfig(m_config);
|
||||
// QVBoxLayout *layout = new QVBoxLayout;
|
||||
// layout->addWidget(qt);
|
||||
// layout->setMargin(4);
|
||||
|
||||
ui->tabWidget->addTab(qt, "Tab");
|
||||
|
||||
}
|
||||
|
||||
QueryTab *MainWindow::GetActiveQueryTab()
|
||||
{
|
||||
QWidget *widget = ui->tabWidget->currentWidget();
|
||||
QueryTab *qt = dynamic_cast<QueryTab*>(widget);
|
||||
return qt;
|
||||
}
|
||||
|
||||
void MainWindow::setConfig(const ConnectionConfig &config)
|
||||
{
|
||||
m_config = config;
|
||||
QString title = "pglab - ";
|
||||
title += m_config.name().c_str();
|
||||
setWindowTitle(title);
|
||||
QueueTask([this]() { startConnect(); });
|
||||
newSqlPage();
|
||||
}
|
||||
|
||||
void MainWindow::QueueTask(TSQueue::t_Callable c)
|
||||
|
|
@ -175,255 +106,6 @@ 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;
|
||||
}
|
||||
addLog(status_str);
|
||||
statusBar()->showMessage(status_str);
|
||||
|
||||
bool connected = ASyncDBConnection::State::Connected == state;
|
||||
ui->actionExecute_SQL->setEnabled(connected);
|
||||
ui->actionExplain_Analyze->setEnabled(connected);
|
||||
ui->actionCancel->setEnabled(ASyncDBConnection::State::QuerySend == state);
|
||||
}
|
||||
|
||||
void MainWindow::startConnect()
|
||||
{
|
||||
m_dbConnection.setupConnection(m_config);
|
||||
}
|
||||
|
||||
std::string MainWindow::getCommand() const
|
||||
{
|
||||
QString command;
|
||||
QTextCursor cursor = ui->queryEdit->textCursor();
|
||||
if (cursor.hasSelection()) {
|
||||
command = cursor.selection().toPlainText();
|
||||
}
|
||||
else {
|
||||
command = ui->queryEdit->toPlainText();
|
||||
}
|
||||
return command.toUtf8().data();
|
||||
}
|
||||
|
||||
void MainWindow::performQuery()
|
||||
{
|
||||
if (m_dbConnection.state() == ASyncDBConnection::State::Connected) {
|
||||
addLog("Query clicked");
|
||||
|
||||
ui->ResultView->setModel(nullptr);
|
||||
resultModel.reset();
|
||||
ui->messagesEdit->clear();
|
||||
|
||||
std::string cmd = getCommand();
|
||||
startTimer();
|
||||
m_dbConnection.send(cmd,
|
||||
[this](std::shared_ptr<Pgsql::Result> res)
|
||||
{
|
||||
QueueTask([this, res]() { query_ready(res); });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::query_ready(std::shared_ptr<Pgsql::Result> dbres)
|
||||
{
|
||||
endTimer();
|
||||
if (dbres) {
|
||||
addLog("query_ready with result");
|
||||
auto st = dbres->resultStatus();
|
||||
if (st == PGRES_TUPLES_OK) {
|
||||
resultModel.reset(new QueryResultModel(nullptr , dbres));
|
||||
ui->ResultView->setModel(resultModel.get());
|
||||
ui->tabWidget->setCurrentWidget(ui->dataTab);
|
||||
statusBar()->showMessage(tr("Query ready."));
|
||||
}
|
||||
else {
|
||||
if (st == PGRES_COMMAND_OK) {
|
||||
statusBar()->showMessage(tr("Command OK."));
|
||||
QString msg = tr("Query returned succesfully: %1 rows affected, %2 execution time.")
|
||||
.arg(QString::number(dbres->tuplesAffected()))
|
||||
.arg(msfloatToHumanReadableString(elapsedTime.count()));
|
||||
ui->messagesEdit->append(msg);
|
||||
|
||||
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
||||
}
|
||||
else {
|
||||
if (st == PGRES_EMPTY_QUERY) {
|
||||
statusBar()->showMessage(tr("Empty query."));
|
||||
}
|
||||
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("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..."));
|
||||
}
|
||||
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
||||
receiveNotice(dbres->diagDetails());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
addLog("query_ready with NO result");
|
||||
statusBar()->showMessage(tr("Query cancelled."));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::performExplain()
|
||||
{
|
||||
ui->explainTreeView->setModel(nullptr);
|
||||
explainModel.reset();
|
||||
ui->messagesEdit->clear();
|
||||
|
||||
addLog("Explain clicked");
|
||||
|
||||
startTimer();
|
||||
std::string cmd = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
|
||||
m_dbConnection.send(cmd,
|
||||
[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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MainWindow::explain_ready(ExplainRoot::SPtr explain)
|
||||
{
|
||||
endTimer();
|
||||
if (explain) {
|
||||
addLog("Explain ready");
|
||||
QString times_str = QString("Execution time: %1, Planning time: %2")
|
||||
.arg(
|
||||
msfloatToHumanReadableString(explain->executionTime)
|
||||
, msfloatToHumanReadableString(explain->planningTime));
|
||||
ui->lblTimes->setText(times_str);
|
||||
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);
|
||||
ui->tabWidget->setCurrentWidget(ui->explainTab);
|
||||
statusBar()->showMessage(tr("Explain ready."));
|
||||
}
|
||||
else {
|
||||
addLog("Explain no result");
|
||||
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
||||
statusBar()->showMessage(tr("Explain failed."));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::cancel_query()
|
||||
{
|
||||
m_dbConnection.cancel();
|
||||
}
|
||||
|
||||
void MainWindow::receiveNotice(Pgsql::ErrorDetails notice)
|
||||
{
|
||||
ui->messagesEdit->append(QString::fromStdString(notice.errorMessage));
|
||||
|
||||
ui->messagesEdit->append(QString::fromStdString(notice.severity));
|
||||
ui->messagesEdit->append(QString("At position: %1").arg(notice.statementPosition));
|
||||
ui->messagesEdit->append(QString::fromStdString("State: " + notice.state));
|
||||
ui->messagesEdit->append(QString::fromStdString("Primary: " + notice.messagePrimary));
|
||||
ui->messagesEdit->append(QString::fromStdString("Detail: " + notice.messageDetail));
|
||||
ui->messagesEdit->append(QString::fromStdString("Hint: " + notice.messageHint));
|
||||
ui->messagesEdit->append(QString::fromStdString("Context: " + notice.context));
|
||||
// std::string state; ///< PG_DIAG_SQLSTATE Error code as listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
|
||||
// std::string severity;
|
||||
// std::string messagePrimary;
|
||||
// std::string messageDetail;
|
||||
// std::string messageHint;
|
||||
// int statementPosition; ///< First character is one, measured in characters not bytes!
|
||||
// std::string context;
|
||||
|
||||
// int internalPosition;
|
||||
// std::string internalQuery;
|
||||
// std::string schemaName;
|
||||
// std::string tableName;
|
||||
// std::string columnName;
|
||||
// std::string datatypeName;
|
||||
// std::string constraintName;
|
||||
// std::string sourceFile;
|
||||
// std::string sourceLine;
|
||||
// std::string sourceFunction;
|
||||
|
||||
// QTextCursor cursor = ui->messagesEdit->textCursor();
|
||||
// cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
||||
|
||||
// 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));
|
||||
// }
|
||||
|
||||
// syntax error at or near "limit
|
||||
// statementPosition
|
||||
}
|
||||
|
||||
void MainWindow::addLog(QString s)
|
||||
{
|
||||
QTextCursor text_cursor = QTextCursor(ui->edtLog->document());
|
||||
text_cursor.movePosition(QTextCursor::End);
|
||||
text_cursor.insertText(s + "\r\n");
|
||||
}
|
||||
|
||||
void MainWindow::startTimer()
|
||||
{
|
||||
m_startTime = std::chrono::steady_clock::now();
|
||||
|
|
@ -464,83 +146,36 @@ void MainWindow::endTimer()
|
|||
}
|
||||
}
|
||||
|
||||
bool MainWindow::continueWithoutSaving()
|
||||
{
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText("The document has been modified.");
|
||||
msgBox.setInformativeText("The current query has unsaved changes, do you want to continue without saving those changes?");
|
||||
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
msgBox.setDefaultButton(QMessageBox::No);
|
||||
int ret = msgBox.exec();
|
||||
return ret == QMessageBox::Yes;
|
||||
}
|
||||
|
||||
void MainWindow::on_actionLoad_SQL_triggered()
|
||||
{
|
||||
//
|
||||
if (m_queryTextChanged && !continueWithoutSaving()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
QString file_name = QFileDialog::getOpenFileName(this,
|
||||
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
|
||||
if ( ! file_name.isEmpty()) {
|
||||
QFile file(file_name);
|
||||
if (file.open(QIODevice::ReadWrite)) {
|
||||
QTextStream stream(&file);
|
||||
ui->queryEdit->clear();
|
||||
while (!stream.atEnd()){
|
||||
QString line = stream.readLine();
|
||||
ui->queryEdit->appendPlainText(line);
|
||||
}
|
||||
m_queryTextChanged = false;
|
||||
m_fileName = file_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::saveSqlTo(const QString &filename)
|
||||
{
|
||||
QFile file(filename);
|
||||
if (file.open(QIODevice::ReadWrite)) {
|
||||
QTextStream stream(&file);
|
||||
QString text = ui->queryEdit->toPlainText();
|
||||
stream << text;
|
||||
m_queryTextChanged = false;
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->open();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_actionSave_SQL_triggered()
|
||||
{
|
||||
if (m_fileName.isEmpty()) {
|
||||
on_actionSave_SQL_as_triggered();
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->save();
|
||||
}
|
||||
else {
|
||||
saveSqlTo(m_fileName);
|
||||
}
|
||||
}
|
||||
|
||||
QString MainWindow::promptUserForSaveSqlFilename()
|
||||
{
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
return QFileDialog::getSaveFileName(this, tr("Save query"), home_dir, tr("SQL file (*.sql)"));
|
||||
}
|
||||
|
||||
void MainWindow::on_actionSave_SQL_as_triggered()
|
||||
{
|
||||
QString filename = promptUserForSaveSqlFilename();
|
||||
if (!filename.isEmpty()) {
|
||||
saveSqlTo(filename);
|
||||
m_fileName = filename;
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->saveAs();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MainWindow::on_actionSave_copy_of_SQL_as_triggered()
|
||||
{
|
||||
QString filename = promptUserForSaveSqlFilename();
|
||||
if (!filename.isEmpty()) {
|
||||
saveSqlTo(filename);
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->saveCopyAs();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -595,38 +230,44 @@ void Copy( )
|
|||
|
||||
void MainWindow::on_actionExecute_SQL_triggered()
|
||||
{
|
||||
performQuery();
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->execute();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_actionExplain_Analyze_triggered()
|
||||
{
|
||||
performExplain();
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->explainAnalyze();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_actionCancel_triggered()
|
||||
{
|
||||
cancel_query();
|
||||
}
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
tab->cancel();
|
||||
}
|
||||
|
||||
void MainWindow::queryTextChanged()
|
||||
{
|
||||
m_queryTextChanged = true;
|
||||
}
|
||||
|
||||
void MainWindow::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
if (!m_queryTextChanged || continueWithoutSaving()) {
|
||||
event->accept();
|
||||
}
|
||||
else {
|
||||
event->ignore();
|
||||
}
|
||||
// TODO collect which files need saving
|
||||
// if (!m_queryTextChanged || continueWithoutSaving()) {
|
||||
// event->accept();
|
||||
// }
|
||||
// else {
|
||||
// event->ignore();
|
||||
// }
|
||||
}
|
||||
|
||||
void MainWindow::showEvent(QShowEvent *event)
|
||||
{
|
||||
if (!event->spontaneous()) {
|
||||
m_queryTextChanged = false;
|
||||
// m_queryTextChanged = false;
|
||||
}
|
||||
event->accept();
|
||||
}
|
||||
|
|
@ -634,3 +275,8 @@ void MainWindow::showEvent(QShowEvent *event)
|
|||
|
||||
|
||||
|
||||
|
||||
void MainWindow::on_actionNew_SQL_triggered()
|
||||
{
|
||||
newSqlPage();
|
||||
}
|
||||
|
|
|
|||
44
mainwindow.h
44
mainwindow.h
|
|
@ -14,10 +14,9 @@
|
|||
#include <deque>
|
||||
#include <mutex>
|
||||
|
||||
class ExplainRoot;
|
||||
class QueryResultModel;
|
||||
class QueryExplainModel;
|
||||
class SqlHighlighter;
|
||||
//class ExplainRoot;
|
||||
//class QueryResultModel;
|
||||
//class QueryExplainModel;
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
|
@ -44,55 +43,35 @@ public:
|
|||
*/
|
||||
void QueueTask(TSQueue::t_Callable c);
|
||||
|
||||
|
||||
private:
|
||||
TSQueue m_taskQueue;
|
||||
QLabel *m_timeElapsedLabel;
|
||||
std::chrono::duration<float, std::milli> elapsedTime;
|
||||
|
||||
ConnectionConfig m_config;
|
||||
|
||||
|
||||
std::unique_ptr<QTimer> m_timer;
|
||||
std::chrono::time_point<std::chrono::steady_clock> m_startTime;
|
||||
ConnectionConfig m_config;
|
||||
bool m_queryTextChanged = false;
|
||||
QString m_fileName;
|
||||
|
||||
|
||||
void startTimer();
|
||||
void endTimer();
|
||||
std::chrono::duration<float, std::milli> elapsedTime;
|
||||
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
QueryTab *m_queryTab = nullptr;
|
||||
std::unique_ptr<SqlHighlighter> highlighter;
|
||||
|
||||
ASyncDBConnection m_dbConnection;
|
||||
QueryTab *GetActiveQueryTab();
|
||||
|
||||
void connectionStateChanged(ASyncDBConnection::State state);
|
||||
|
||||
std::unique_ptr<QueryResultModel> resultModel;
|
||||
std::unique_ptr<QueryExplainModel> explainModel;
|
||||
|
||||
void query_ready(std::shared_ptr<Pgsql::Result> res);
|
||||
void explain_ready(std::shared_ptr<ExplainRoot> explain);
|
||||
std::string getCommand() const;
|
||||
|
||||
bool continueWithoutSaving();
|
||||
void closeEvent(QCloseEvent *event);
|
||||
void showEvent(QShowEvent *event);
|
||||
QString promptUserForSaveSqlFilename();
|
||||
void saveSqlTo(const QString &filename);
|
||||
void newSqlPage();
|
||||
private slots:
|
||||
|
||||
void startConnect();
|
||||
|
||||
void performQuery();
|
||||
void performExplain();
|
||||
|
||||
void cancel_query();
|
||||
void queryTextChanged();
|
||||
void receiveNotice(Pgsql::ErrorDetails notice);
|
||||
|
||||
void processCallableQueue();
|
||||
|
||||
void addLog(QString s);
|
||||
void updateTimer();
|
||||
void on_actionLoad_SQL_triggered();
|
||||
void on_actionSave_SQL_triggered();
|
||||
|
|
@ -104,6 +83,7 @@ private slots:
|
|||
void on_actionCancel_triggered();
|
||||
void on_actionSave_SQL_as_triggered();
|
||||
void on_actionSave_copy_of_SQL_as_triggered();
|
||||
void on_actionNew_SQL_triggered();
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
|
|
|||
187
mainwindow.ui
187
mainwindow.ui
|
|
@ -31,178 +31,10 @@
|
|||
<number>4</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget_2">
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>-1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tab">
|
||||
<attribute name="title">
|
||||
<string>Tab 1</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QSplitter" name="splitter">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<widget class="QPlainTextEdit" name="queryEdit"/>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="messageTab">
|
||||
<attribute name="title">
|
||||
<string>Messages</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QTextEdit" name="messagesEdit">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="dataTab">
|
||||
<attribute name="title">
|
||||
<string>Data</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QTableView" name="ResultView">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Source Sans Pro</family>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="editTriggers">
|
||||
<set>QAbstractItemView::NoEditTriggers</set>
|
||||
</property>
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="verticalScrollMode">
|
||||
<enum>QAbstractItemView::ScrollPerPixel</enum>
|
||||
</property>
|
||||
<property name="horizontalScrollMode">
|
||||
<enum>QAbstractItemView::ScrollPerPixel</enum>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<attribute name="verticalHeaderMinimumSectionSize">
|
||||
<number>20</number>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="explainTab">
|
||||
<attribute name="title">
|
||||
<string>Explain</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<property name="leftMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<item row="1" column="0">
|
||||
<widget class="QTreeView" name="explainTreeView">
|
||||
<property name="editTriggers">
|
||||
<set>QAbstractItemView::NoEditTriggers</set>
|
||||
</property>
|
||||
<property name="showDropIndicator" stdset="0">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="indentation">
|
||||
<number>10</number>
|
||||
</property>
|
||||
<property name="uniformRowHeights">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<attribute name="headerStretchLastSection">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="lblTimes">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="logTab">
|
||||
<attribute name="title">
|
||||
<string>Log</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="edtLog"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_2">
|
||||
<attribute name="title">
|
||||
<string>Tab 2</string>
|
||||
</attribute>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
|
@ -220,6 +52,7 @@
|
|||
<property name="title">
|
||||
<string>File</string>
|
||||
</property>
|
||||
<addaction name="actionNew_SQL"/>
|
||||
<addaction name="actionLoad_SQL"/>
|
||||
<addaction name="actionSave_SQL"/>
|
||||
<addaction name="actionSave_SQL_as"/>
|
||||
|
|
@ -244,6 +77,7 @@
|
|||
<attribute name="toolBarBreak">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
<addaction name="actionNew_SQL"/>
|
||||
<addaction name="actionLoad_SQL"/>
|
||||
<addaction name="actionSave_SQL"/>
|
||||
<addaction name="actionExport_data"/>
|
||||
|
|
@ -344,6 +178,19 @@
|
|||
<string>Save copy of SQL as</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionNew_SQL">
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<normalon>:/icons/page_white_add.png</normalon>
|
||||
</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>New SQL</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+N</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources>
|
||||
|
|
|
|||
|
|
@ -37,7 +37,9 @@ SOURCES += main.cpp\
|
|||
backuprestore.cpp \
|
||||
databaseoverviewform.cpp \
|
||||
dbschema_database.cpp \
|
||||
querytab.cpp
|
||||
querytab.cpp \
|
||||
stopwatch.cpp \
|
||||
util.cpp
|
||||
|
||||
HEADERS += mainwindow.h \
|
||||
serverproperties.h \
|
||||
|
|
@ -60,7 +62,9 @@ HEADERS += mainwindow.h \
|
|||
expected.h \
|
||||
databaseoverviewform.h \
|
||||
dbschema_database.h \
|
||||
querytab.h
|
||||
querytab.h \
|
||||
stopwatch.h \
|
||||
util.h
|
||||
|
||||
FORMS += mainwindow.ui \
|
||||
serverproperties.ui \
|
||||
|
|
|
|||
392
querytab.cpp
392
querytab.cpp
|
|
@ -1,14 +1,400 @@
|
|||
#include "querytab.h"
|
||||
#include "querytab.h"
|
||||
#include "ui_querytab.h"
|
||||
|
||||
QueryTab::QueryTab(QWidget *parent) :
|
||||
#include "sqlhighlighter.h"
|
||||
|
||||
#include <QStandardPaths>
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QTextDocumentFragment>
|
||||
#include <QTextStream>
|
||||
#include "explaintreemodelitem.h"
|
||||
#include "json/json.h"
|
||||
#include "mainwindow.h"
|
||||
#include "util.h"
|
||||
|
||||
QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::QueryTab)
|
||||
ui(new Ui::QueryTab),
|
||||
m_win(win)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
|
||||
{
|
||||
m_win->QueueTask([this, st]() { connectionStateChanged(st); });
|
||||
});
|
||||
|
||||
m_dbConnection.setNoticeCallback([this](Pgsql::ErrorDetails details)
|
||||
{
|
||||
m_win->QueueTask([this, details]() { receiveNotice(details); });
|
||||
});
|
||||
|
||||
QFont font;
|
||||
font.setFamily("Source Code Pro");
|
||||
font.setFixedPitch(true);
|
||||
font.setPointSize(10);
|
||||
ui->queryEdit->setFont(font);
|
||||
highlighter.reset(new SqlHighlighter(ui->queryEdit->document()));
|
||||
|
||||
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTab::queryTextChanged);
|
||||
|
||||
}
|
||||
|
||||
QueryTab::~QueryTab()
|
||||
{
|
||||
m_dbConnection.closeConnection();
|
||||
m_dbConnection.setStateCallback(nullptr);
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void QueryTab::setConfig(const ConnectionConfig &config)
|
||||
{
|
||||
m_config = config;
|
||||
// QString title = "pglab - ";
|
||||
// title += m_config.name().c_str();
|
||||
// setWindowTitle(title);
|
||||
m_win->QueueTask([this]() { startConnect(); });
|
||||
}
|
||||
|
||||
void QueryTab::open()
|
||||
{
|
||||
if (m_queryTextChanged && !continueWithoutSavingWarning()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
QString file_name = QFileDialog::getOpenFileName(this,
|
||||
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
|
||||
if ( ! file_name.isEmpty()) {
|
||||
QFile file(file_name);
|
||||
if (file.open(QIODevice::ReadWrite)) {
|
||||
QTextStream stream(&file);
|
||||
ui->queryEdit->clear();
|
||||
while (!stream.atEnd()){
|
||||
QString line = stream.readLine();
|
||||
ui->queryEdit->appendPlainText(line);
|
||||
}
|
||||
m_queryTextChanged = false;
|
||||
setFileName(file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QueryTab::save()
|
||||
{
|
||||
if (m_fileName.isEmpty()) {
|
||||
saveAs();
|
||||
}
|
||||
else {
|
||||
saveSqlTo(m_fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void QueryTab::saveAs()
|
||||
{
|
||||
QString filename = promptUserForSaveSqlFilename();
|
||||
if (!filename.isEmpty()) {
|
||||
saveSqlTo(filename);
|
||||
setFileName(filename);
|
||||
}
|
||||
}
|
||||
|
||||
void QueryTab::saveCopyAs()
|
||||
{
|
||||
QString filename = promptUserForSaveSqlFilename();
|
||||
if (!filename.isEmpty()) {
|
||||
saveSqlTo(filename);
|
||||
}
|
||||
}
|
||||
|
||||
void QueryTab::execute()
|
||||
{
|
||||
if (m_dbConnection.state() == ASyncDBConnection::State::Connected) {
|
||||
addLog("Query clicked");
|
||||
|
||||
ui->ResultView->setModel(nullptr);
|
||||
resultModel.reset();
|
||||
ui->messagesEdit->clear();
|
||||
|
||||
std::string cmd = getCommand();
|
||||
//startTimer();
|
||||
m_dbConnection.send(cmd,
|
||||
[this](std::shared_ptr<Pgsql::Result> res)
|
||||
{
|
||||
m_win->QueueTask([this, res]() { query_ready(res); });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void QueryTab::explainAnalyze()
|
||||
{
|
||||
ui->explainTreeView->setModel(nullptr);
|
||||
explainModel.reset();
|
||||
ui->messagesEdit->clear();
|
||||
|
||||
addLog("Explain clicked");
|
||||
|
||||
//startTimer();
|
||||
std::string cmd = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
|
||||
m_dbConnection.send(cmd,
|
||||
[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);
|
||||
}
|
||||
}
|
||||
m_win->QueueTask([this, explain]() { explain_ready(explain); });
|
||||
}).detach();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void QueryTab::cancel()
|
||||
{
|
||||
m_dbConnection.cancel();
|
||||
}
|
||||
|
||||
|
||||
void QueryTab::setFileName(const QString &filename)
|
||||
{
|
||||
m_fileName = filename;
|
||||
}
|
||||
|
||||
bool QueryTab::continueWithoutSavingWarning()
|
||||
{
|
||||
QMessageBox msgBox;
|
||||
msgBox.setIcon(QMessageBox::Warning);
|
||||
msgBox.setText("The document has been modified.");
|
||||
msgBox.setInformativeText("The current query has unsaved changes, do you want to continue without saving those changes?");
|
||||
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
msgBox.setDefaultButton(QMessageBox::No);
|
||||
int ret = msgBox.exec();
|
||||
return ret == QMessageBox::Yes;
|
||||
}
|
||||
|
||||
void QueryTab::saveSqlTo(const QString &filename)
|
||||
{
|
||||
QFile file(filename);
|
||||
if (file.open(QIODevice::ReadWrite)) {
|
||||
QTextStream stream(&file);
|
||||
QString text = ui->queryEdit->toPlainText();
|
||||
stream << text;
|
||||
m_queryTextChanged = false;
|
||||
}
|
||||
}
|
||||
|
||||
QString QueryTab::promptUserForSaveSqlFilename()
|
||||
{
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
return QFileDialog::getSaveFileName(this, tr("Save query"), home_dir, tr("SQL file (*.sql)"));
|
||||
}
|
||||
|
||||
void QueryTab::queryTextChanged()
|
||||
{
|
||||
m_queryTextChanged = true;
|
||||
}
|
||||
|
||||
|
||||
void QueryTab::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;
|
||||
}
|
||||
addLog(status_str);
|
||||
// statusBar()->showMessage(status_str);
|
||||
|
||||
// bool connected = ASyncDBConnection::State::Connected == state;
|
||||
// ui->actionExecute_SQL->setEnabled(connected);
|
||||
// ui->actionExplain_Analyze->setEnabled(connected);
|
||||
// ui->actionCancel->setEnabled(ASyncDBConnection::State::QuerySend == state);
|
||||
}
|
||||
|
||||
|
||||
void QueryTab::addLog(QString s)
|
||||
{
|
||||
QTextCursor text_cursor = QTextCursor(ui->edtLog->document());
|
||||
text_cursor.movePosition(QTextCursor::End);
|
||||
text_cursor.insertText(s + "\r\n");
|
||||
}
|
||||
|
||||
void QueryTab::receiveNotice(Pgsql::ErrorDetails notice)
|
||||
{
|
||||
ui->messagesEdit->append(QString::fromStdString(notice.errorMessage));
|
||||
|
||||
ui->messagesEdit->append(QString::fromStdString(notice.severity));
|
||||
ui->messagesEdit->append(QString("At position: %1").arg(notice.statementPosition));
|
||||
ui->messagesEdit->append(QString::fromStdString("State: " + notice.state));
|
||||
ui->messagesEdit->append(QString::fromStdString("Primary: " + notice.messagePrimary));
|
||||
ui->messagesEdit->append(QString::fromStdString("Detail: " + notice.messageDetail));
|
||||
ui->messagesEdit->append(QString::fromStdString("Hint: " + notice.messageHint));
|
||||
ui->messagesEdit->append(QString::fromStdString("Context: " + notice.context));
|
||||
// std::string state; ///< PG_DIAG_SQLSTATE Error code as listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
|
||||
// std::string severity;
|
||||
// std::string messagePrimary;
|
||||
// std::string messageDetail;
|
||||
// std::string messageHint;
|
||||
// int statementPosition; ///< First character is one, measured in characters not bytes!
|
||||
// std::string context;
|
||||
|
||||
// int internalPosition;
|
||||
// std::string internalQuery;
|
||||
// std::string schemaName;
|
||||
// std::string tableName;
|
||||
// std::string columnName;
|
||||
// std::string datatypeName;
|
||||
// std::string constraintName;
|
||||
// std::string sourceFile;
|
||||
// std::string sourceLine;
|
||||
// std::string sourceFunction;
|
||||
|
||||
// QTextCursor cursor = ui->messagesEdit->textCursor();
|
||||
// cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
||||
|
||||
// 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));
|
||||
// }
|
||||
|
||||
// syntax error at or near "limit
|
||||
// statementPosition
|
||||
}
|
||||
|
||||
void QueryTab::startConnect()
|
||||
{
|
||||
m_dbConnection.setupConnection(m_config);
|
||||
}
|
||||
|
||||
void QueryTab::explain_ready(ExplainRoot::SPtr explain)
|
||||
{
|
||||
// endTimer();
|
||||
if (explain) {
|
||||
addLog("Explain ready");
|
||||
QString times_str = QString("Execution time: %1, Planning time: %2")
|
||||
.arg(
|
||||
msfloatToHumanReadableString(explain->executionTime)
|
||||
, msfloatToHumanReadableString(explain->planningTime));
|
||||
ui->lblTimes->setText(times_str);
|
||||
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);
|
||||
ui->tabWidget->setCurrentWidget(ui->explainTab);
|
||||
// statusBar()->showMessage(tr("Explain ready."));
|
||||
}
|
||||
else {
|
||||
addLog("Explain no result");
|
||||
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
||||
// statusBar()->showMessage(tr("Explain failed."));
|
||||
}
|
||||
}
|
||||
|
||||
std::string QueryTab::getCommand() const
|
||||
{
|
||||
QString command;
|
||||
QTextCursor cursor = ui->queryEdit->textCursor();
|
||||
if (cursor.hasSelection()) {
|
||||
command = cursor.selection().toPlainText();
|
||||
}
|
||||
else {
|
||||
command = ui->queryEdit->toPlainText();
|
||||
}
|
||||
return command.toUtf8().data();
|
||||
}
|
||||
|
||||
void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
|
||||
{
|
||||
//endTimer();
|
||||
if (dbres) {
|
||||
addLog("query_ready with result");
|
||||
auto st = dbres->resultStatus();
|
||||
if (st == PGRES_TUPLES_OK) {
|
||||
resultModel.reset(new QueryResultModel(nullptr , dbres));
|
||||
ui->ResultView->setModel(resultModel.get());
|
||||
ui->tabWidget->setCurrentWidget(ui->dataTab);
|
||||
//statusBar()->showMessage(tr("Query ready."));
|
||||
}
|
||||
else {
|
||||
if (st == PGRES_COMMAND_OK) {
|
||||
// statusBar()->showMessage(tr("Command OK."));
|
||||
QString msg = tr("Query returned succesfully: %1 rows affected, %2 execution time.")
|
||||
.arg(QString::number(dbres->tuplesAffected()))
|
||||
.arg(0); //msfloatToHumanReadableString(elapsedTime.count()));
|
||||
ui->messagesEdit->append(msg);
|
||||
|
||||
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
||||
}
|
||||
else {
|
||||
// if (st == PGRES_EMPTY_QUERY) {
|
||||
// statusBar()->showMessage(tr("Empty query."));
|
||||
// }
|
||||
// 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("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..."));
|
||||
// }
|
||||
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
||||
receiveNotice(dbres->diagDetails());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
addLog("query_ready with NO result");
|
||||
// statusBar()->showMessage(tr("Query cancelled."));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
55
querytab.h
55
querytab.h
|
|
@ -1,22 +1,73 @@
|
|||
#ifndef QUERYTAB_H
|
||||
#ifndef QUERYTAB_H
|
||||
#define QUERYTAB_H
|
||||
|
||||
#include "asyncdbconnection.h"
|
||||
#include "QueryResultModel.h"
|
||||
#include "QueryExplainModel.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <memory>
|
||||
|
||||
namespace Ui {
|
||||
class QueryTab;
|
||||
}
|
||||
|
||||
class MainWindow;
|
||||
class SqlHighlighter;
|
||||
class ExplainRoot;
|
||||
class QueryResultModel;
|
||||
class QueryExplainModel;
|
||||
|
||||
|
||||
class QueryTab : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit QueryTab(QWidget *parent = 0);
|
||||
QueryTab(MainWindow *win, QWidget *parent = 0);
|
||||
~QueryTab();
|
||||
|
||||
void setConfig(const ConnectionConfig &config);
|
||||
|
||||
void open();
|
||||
void save();
|
||||
void saveAs();
|
||||
void saveCopyAs();
|
||||
|
||||
void execute();
|
||||
void explainAnalyze();
|
||||
void cancel();
|
||||
private:
|
||||
Ui::QueryTab *ui;
|
||||
MainWindow *m_win;
|
||||
std::unique_ptr<SqlHighlighter> highlighter;
|
||||
ConnectionConfig m_config;
|
||||
|
||||
QString m_fileName; ///< use setFileName function to set
|
||||
bool m_queryTextChanged = false;
|
||||
|
||||
void setFileName(const QString &filename);
|
||||
bool continueWithoutSavingWarning();
|
||||
void saveSqlTo(const QString &filename);
|
||||
QString promptUserForSaveSqlFilename();
|
||||
|
||||
|
||||
ASyncDBConnection m_dbConnection;
|
||||
std::unique_ptr<QueryResultModel> resultModel;
|
||||
std::unique_ptr<QueryExplainModel> explainModel;
|
||||
|
||||
void addLog(QString s);
|
||||
|
||||
std::string getCommand() const;
|
||||
void explain_ready(ExplainRoot::SPtr explain);
|
||||
void query_ready(std::shared_ptr<Pgsql::Result> dbres);
|
||||
private slots:
|
||||
|
||||
void queryTextChanged();
|
||||
void connectionStateChanged(ASyncDBConnection::State state);
|
||||
void receiveNotice(Pgsql::ErrorDetails notice);
|
||||
|
||||
void startConnect();
|
||||
};
|
||||
|
||||
#endif // QUERYTAB_H
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@
|
|||
<widget class="QPlainTextEdit" name="queryEdit"/>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>3</number>
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="messageTab">
|
||||
<attribute name="title">
|
||||
|
|
|
|||
|
|
@ -9,5 +9,6 @@
|
|||
<file>icons/script_save.png</file>
|
||||
<file>icons/lightbulb.png</file>
|
||||
<file>icons/table_save.png</file>
|
||||
<file>icons/page_white_add.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
|
|||
38
stopwatch.cpp
Normal file
38
stopwatch.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include "stopwatch.h"
|
||||
#include "util.h"
|
||||
|
||||
StopWatch::StopWatch()
|
||||
: m_elapsed(std::make_unique<QElapsedTimer>())
|
||||
{}
|
||||
|
||||
void StopWatch::start()
|
||||
{
|
||||
m_elapsed->start();
|
||||
m_timer = std::make_unique<QTimer>(nullptr);
|
||||
connect(m_timer.get(), SIGNAL(timeout()), this, SLOT(updateTimer()));
|
||||
m_timer->start(18);
|
||||
}
|
||||
|
||||
void StopWatch::updateTimer()
|
||||
{
|
||||
// auto nu = std::chrono::steady_clock::now();
|
||||
// std::chrono::duration<float, std::milli> diff = nu - m_startTime;
|
||||
// elapsedTime = diff;
|
||||
// m_timeElapsedLabel->setText(msfloatToHumanReadableString(diff.count()));
|
||||
|
||||
qint64 ms = m_elapsed->elapsed();
|
||||
msfloatToHumanReadableString(ms);
|
||||
|
||||
if (m_timer) {
|
||||
int interval = 18;
|
||||
if (ms >= 10000) {
|
||||
int rem = ms % 1000;
|
||||
interval = 1000 - rem;
|
||||
}
|
||||
else if (ms >= 1000) {
|
||||
interval = 100;
|
||||
}
|
||||
|
||||
m_timer->start(interval);
|
||||
}
|
||||
}
|
||||
24
stopwatch.h
Normal file
24
stopwatch.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef STOPWATCH_H
|
||||
#define STOPWATCH_H
|
||||
|
||||
#include <QTimer>
|
||||
#include <QElapsedTimer>
|
||||
#include <memory>
|
||||
|
||||
class StopWatch : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
StopWatch();
|
||||
|
||||
void start();
|
||||
qint64 elapsed();
|
||||
private:
|
||||
std::unique_ptr<QElapsedTimer> m_elapsed = nullptr; ///< Keeps time
|
||||
std::unique_ptr<QTimer> m_timer = nullptr; ///< triggers updates
|
||||
|
||||
|
||||
private slots:
|
||||
void updateTimer();
|
||||
};
|
||||
|
||||
#endif // STOPWATCH_H
|
||||
49
util.cpp
Normal file
49
util.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "util.h"
|
||||
|
||||
// Supported range from microseconds to seconds
|
||||
// min:sec to hours::min::sec
|
||||
QString msfloatToHumanReadableString(float ms)
|
||||
{
|
||||
QString unit;
|
||||
float val;
|
||||
int deci = 2;
|
||||
if (ms < 1.0f) {
|
||||
val = ms * 1000.f;
|
||||
//result = QString::asprintf("%0.3f", ms * 1000.0f);
|
||||
unit = u8"μs";
|
||||
}
|
||||
else if (ms >= 1000.0) {
|
||||
val = ms / 1000.0f;
|
||||
unit = "s";
|
||||
if (val >= 60.0) {
|
||||
int secs = val;
|
||||
int min = secs / 60.0;
|
||||
secs -= min * 60;
|
||||
if (min >= 60) {
|
||||
int hour = min / 60;
|
||||
min -= hour * 60;
|
||||
return QString::asprintf("%d:%02d:%02d", hour, min, secs);
|
||||
}
|
||||
else {
|
||||
return QString::asprintf("%02d:%02d", min, secs);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
val = ms;
|
||||
unit = "ms";
|
||||
}
|
||||
|
||||
// if (val >= 1000.f) {
|
||||
// deci = 0;
|
||||
// }
|
||||
// else
|
||||
if (val >= 100.f) {
|
||||
deci = 0;
|
||||
}
|
||||
else if (val >= 10.f) {
|
||||
deci = 1;
|
||||
}
|
||||
QString result = QString::asprintf("%0.*f", deci, val);
|
||||
return result + unit;
|
||||
}
|
||||
8
util.h
Normal file
8
util.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
QString msfloatToHumanReadableString(float ms);
|
||||
|
||||
#endif // UTIL_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue