The querytab now shows the elapsed time of the query using the new stopwatch class.

The old elapsedtime code from the mainwindow has been removed.
This commit is contained in:
Eelke Klein 2017-01-22 08:50:41 +01:00
parent 7f379f3b80
commit 6e852f466f
7 changed files with 232 additions and 163 deletions

View file

@ -6,6 +6,8 @@
#include <QStandardPaths>
#include <QFileDialog>
#include <QMessageBox>
#include <QTabWidget>
#include <QTextCodec>
#include <QTextDocumentFragment>
#include <QTextStream>
#include "explaintreemodelitem.h"
@ -38,7 +40,9 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
highlighter.reset(new SqlHighlighter(ui->queryEdit->document()));
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTab::queryTextChanged);
m_stopwatch.setOutputLabel(ui->lblElapsedTime);
ui->lblElapsedTime->clear();
ui->lblRowCount->clear();
}
QueryTab::~QueryTab()
@ -57,28 +61,72 @@ void QueryTab::setConfig(const ConnectionConfig &config)
m_win->QueueTask([this]() { startConnect(); });
}
void QueryTab::open()
bool QueryTab::canClose()
{
if (m_queryTextChanged && !continueWithoutSavingWarning()) {
return;
bool can_close;
if (m_queryTextChanged) {
can_close = continueWithoutSavingWarning();
}
else {
can_close = true;
}
return can_close;
}
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)) {
//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::newdoc()
{
ui->queryEdit->clear();
setFileName(QString());
m_queryTextChanged = false;
}
bool QueryTab::load(const QString &filename)
{
bool result = false;
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QByteArray ba = file.readAll();
const char *ptr = ba.constData();
QTextCodec *codec = QTextCodec::codecForUtfText(ba, QTextCodec::codecForName("utf-8"));
QTextCodec::ConverterState state;
QString text = codec->toUnicode(ptr, ba.size(), &state);
if (state.invalidChars > 0) {
file.reset();
QTextStream stream(&file);
ui->queryEdit->clear();
while (!stream.atEnd()){
QString line = stream.readLine();
ui->queryEdit->appendPlainText(line);
}
m_queryTextChanged = false;
setFileName(file_name);
text = stream.readAll();
}
ui->queryEdit->setPlainText(text);
m_queryTextChanged = false;
setFileName(filename);
result = true;
}
return result;
}
void QueryTab::save()
@ -113,12 +161,11 @@ void QueryTab::execute()
if (m_dbConnection.state() == ASyncDBConnection::State::Connected) {
addLog("Query clicked");
ui->ResultView->setModel(nullptr);
resultModel.reset();
clearResult();
ui->messagesEdit->clear();
std::string cmd = getCommand();
//startTimer();
m_stopwatch.start();
m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res)
{
@ -135,7 +182,7 @@ void QueryTab::explainAnalyze()
addLog("Explain clicked");
//startTimer();
m_stopwatch.start();
std::string cmd = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res)
@ -169,6 +216,7 @@ void QueryTab::cancel()
void QueryTab::setFileName(const QString &filename)
{
m_fileName = filename;
setTabCaption(m_fileName);
}
bool QueryTab::continueWithoutSavingWarning()
@ -183,15 +231,32 @@ bool QueryTab::continueWithoutSavingWarning()
return ret == QMessageBox::Yes;
}
void QueryTab::saveSqlTo(const QString &filename)
bool QueryTab::saveSqlTo(const QString &filename)
{
bool result = false;
QFile file(filename);
if (file.open(QIODevice::ReadWrite)) {
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream.setCodec("utf-8");
QString text = ui->queryEdit->toPlainText();
stream << text;
m_queryTextChanged = false;
/*
QTextDocument *doc = ui->queryEdit->document();
QTextBlock block = doc->firstBlock();
while (stream.status() == QTextStream::Ok && block.isValid()) {
QString plain = block.text();
stream << plain << "\n";
block = block.next();
}*/
if (stream.status() == QTextStream::Ok) {
m_queryTextChanged = false;
result = true;
}
}
return result;
}
QString QueryTab::promptUserForSaveSqlFilename()
@ -297,7 +362,7 @@ void QueryTab::startConnect()
void QueryTab::explain_ready(ExplainRoot::SPtr explain)
{
// endTimer();
m_stopwatch.stop();
if (explain) {
addLog("Explain ready");
QString times_str = QString("Execution time: %1, Planning time: %2")
@ -338,13 +403,30 @@ std::string QueryTab::getCommand() const
return command.toUtf8().data();
}
void QueryTab::setTabCaption(const QString &caption)
{
QWidget * w = parentWidget();
QWidget * p = w->parentWidget();
QTabWidget *tabwidget = dynamic_cast<QTabWidget*>(p);
if (tabwidget) {
int i = tabwidget->indexOf(this);
if (i >= 0) {
tabwidget->setTabText(i, caption);
}
}
}
void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
{
//endTimer();
m_stopwatch.stop();
if (dbres) {
addLog("query_ready with result");
auto st = dbres->resultStatus();
if (st == PGRES_TUPLES_OK) {
//int n_rows = dbres->getRows();
QString rowcount_str = QString("rows: %1").arg(dbres->getRows());
ui->lblRowCount->setText(rowcount_str);
resultModel.reset(new QueryResultModel(nullptr , dbres));
ui->ResultView->setModel(resultModel.get());
ui->tabWidget->setCurrentWidget(ui->dataTab);
@ -355,7 +437,7 @@ void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
// 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()));
.arg(m_stopwatch.elapsed()); //msfloatToHumanReadableString(elapsedTime.count()));
ui->messagesEdit->append(msg);
ui->tabWidget->setCurrentWidget(ui->messageTab);
@ -398,3 +480,10 @@ void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
// statusBar()->showMessage(tr("Query cancelled."));
}
}
void QueryTab::clearResult()
{
ui->ResultView->setModel(nullptr);
resultModel.reset();
ui->lblRowCount->clear();
}