Query tab will now show multiple data tabs if it get's multiple results

with tuples.
This commit is contained in:
Eelke Klein 2017-01-25 06:54:21 +01:00
parent 424cbc9e2e
commit b6d986051b
21 changed files with 349 additions and 113 deletions

View file

@ -40,9 +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();
// m_stopwatch.setOutputLabel(ui->lblElapsedTime);
// ui->lblElapsedTime->clear();
// ui->lblRowCount->clear();
}
QueryTab::~QueryTab()
@ -167,14 +167,14 @@ void QueryTab::execute()
std::string cmd = getCommand();
m_stopwatch.start();
m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res)
[this](std::shared_ptr<Pgsql::Result> res, qint64 elapsedms)
{
m_win->QueueTask([this, res]() { query_ready(res); });
m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); });
});
}
}
void QueryTab::explainAnalyze()
void QueryTab::explain(bool analyze)
{
ui->explainTreeView->setModel(nullptr);
explainModel.reset();
@ -182,10 +182,14 @@ void QueryTab::explainAnalyze()
addLog("Explain clicked");
std::string analyze_str;
if (analyze) {
analyze_str = "ANALYZE, ";
}
m_stopwatch.start();
std::string cmd = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res)
[this](std::shared_ptr<Pgsql::Result> res, qint64 )
{
if (res) {
// Process explain data seperately
@ -216,7 +220,9 @@ void QueryTab::cancel()
void QueryTab::setFileName(const QString &filename)
{
m_fileName = filename;
setTabCaption(m_fileName);
QFileInfo fileInfo(filename);
QString fn(fileInfo.fileName());
setTabCaption(fn, m_fileName);
}
bool QueryTab::continueWithoutSavingWarning()
@ -403,7 +409,7 @@ std::string QueryTab::getCommand() const
return command.toUtf8().data();
}
void QueryTab::setTabCaption(const QString &caption)
void QueryTab::setTabCaption(const QString &caption, const QString &tooltip)
{
QWidget * w = parentWidget();
QWidget * p = w->parentWidget();
@ -412,32 +418,45 @@ void QueryTab::setTabCaption(const QString &caption)
int i = tabwidget->indexOf(this);
if (i >= 0) {
tabwidget->setTabText(i, caption);
tabwidget->setTabToolTip(i, tooltip);
}
}
}
void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedms)
{
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);
//QString rowcount_str = QString("rows: %1").arg(dbres->getRows());
auto result_model = std::make_shared<QueryResultModel>(nullptr , dbres);
TuplesResultWidget *trw = new TuplesResultWidget;
trw->setResult(result_model, elapsedms);
resultList.push_back(trw);
ui->tabWidget->addTab(trw, "Data");
// ui->lblRowCount->setText(rowcount_str);
// 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(m_stopwatch.elapsed()); //msfloatToHumanReadableString(elapsedTime.count()));
int tuples_affected = dbres->tuplesAffected();
QString msg;
if (tuples_affected >= 0)
msg = tr("Query returned succesfully: %1 rows affected, execution time %2")
.arg(QString::number(tuples_affected))
.arg(msfloatToHumanReadableString(elapsedms));
else
msg = tr("Query returned succesfully, execution time %1")
.arg(msfloatToHumanReadableString(elapsedms));
ui->messagesEdit->append(msg);
ui->tabWidget->setCurrentWidget(ui->messageTab);
@ -476,6 +495,7 @@ void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
}
}
else {
m_stopwatch.stop();
addLog("query_ready with NO result");
// statusBar()->showMessage(tr("Query cancelled."));
}
@ -483,7 +503,10 @@ void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres)
void QueryTab::clearResult()
{
ui->ResultView->setModel(nullptr);
resultModel.reset();
ui->lblRowCount->clear();
// ui->ResultView->setModel(nullptr);
// resultModel.reset();
for (auto e : resultList)
delete e;
resultList.clear();
// ui->lblRowCount->clear();
}