diff --git a/src/pglab/ASyncDBConnection.cpp b/src/pglab/ASyncDBConnection.cpp index adb20ea..4892327 100644 --- a/src/pglab/ASyncDBConnection.cpp +++ b/src/pglab/ASyncDBConnection.cpp @@ -148,7 +148,8 @@ void ASyncDBConnection::async_query_handler(boost::system::error_code ec, std::s } else { // error during consume - + auto error_msg = m_connection.getErrorMessage(); + } //return finished; if (!finished) { diff --git a/src/pglab/ASyncDBConnection.h b/src/pglab/ASyncDBConnection.h index af66760..60543e9 100644 --- a/src/pglab/ASyncDBConnection.h +++ b/src/pglab/ASyncDBConnection.h @@ -6,6 +6,7 @@ #include "Pgsql_Connection.h" #include "Pgsql_Params.h" #include "Pgsql_Result.h" +#include "Expected.h" #include "ConnectionConfig.h" #include #include @@ -29,7 +30,7 @@ public: Terminating ///< shutting down }; - using on_result_callback = std::function, qint64)>; + using on_result_callback = std::function>, qint64)>; explicit ASyncDBConnection(boost::asio::io_service &ios); ~ASyncDBConnection(); diff --git a/src/pglab/QueryTab.cpp b/src/pglab/QueryTab.cpp index 035f2e6..6d4d10f 100644 --- a/src/pglab/QueryTab.cpp +++ b/src/pglab/QueryTab.cpp @@ -205,14 +205,14 @@ void QueryTab::execute() if (m_queryParamListController->empty()) m_dbConnection.send(cmd, - [this](std::shared_ptr res, qint64 elapsedms) + [this](Expected> res, qint64 elapsedms) { m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); }); }); else m_dbConnection.send(cmd, m_queryParamListController->params(), - [this](std::shared_ptr res, qint64 elapsedms) + [this](Expected> res, qint64 elapsedms) { m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); }); }); @@ -234,10 +234,11 @@ void QueryTab::explain(bool analyze) m_stopwatch.start(); std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, BUFFERS, FORMAT JSON) " + getCommand(); m_dbConnection.send(cmd, - [this](std::shared_ptr res, qint64 ) + [this](Expected> exp_res, qint64 ) { - if (res) { + if (exp_res.valid()) { // Process explain data seperately + auto res = exp_res.get(); std::thread([this,res]() { std::shared_ptr explain; @@ -299,15 +300,14 @@ bool QueryTab::saveSqlTo(const QString &filename) QString text = ui->queryEdit->toPlainText(); stream << text; /* - - 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(); - }*/ + } +*/ stream.flush(); if (stream.status() == QTextStream::Ok) { @@ -483,77 +483,83 @@ void QueryTab::setTabCaption(const QString &caption, const QString &tooltip) } -void QueryTab::query_ready(std::shared_ptr dbres, qint64 elapsedms) +void QueryTab::query_ready(Expected> exp_res, qint64 elapsedms) { - 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()); + if (exp_res.valid()) { + auto dbres = exp_res.get(); + 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()); - auto result_model = std::make_shared(nullptr , dbres); - TuplesResultWidget *trw = new TuplesResultWidget; - trw->setResult(result_model, elapsedms); - resultList.push_back(trw); - ui->tabWidget->addTab(trw, "Data"); - if (resultList.size() == 1) - ui->tabWidget->setCurrentWidget(trw); + auto result_model = std::make_shared(nullptr , dbres); + TuplesResultWidget *trw = new TuplesResultWidget; + trw->setResult(result_model, elapsedms); + resultList.push_back(trw); + ui->tabWidget->addTab(trw, "Data"); + if (resultList.size() == 1) + ui->tabWidget->setCurrentWidget(trw); - } - else { - if (st == PGRES_COMMAND_OK) { - 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)); + } + else { + if (st == PGRES_COMMAND_OK) { + 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->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 { - m_stopwatch.stop(); - addLog("query_ready with NO result"); - } + 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 { + m_stopwatch.stop(); + addLog("query_ready with NO result"); + } + } + else { + // we have an error + } } void QueryTab::clearResult() diff --git a/src/pglab/QueryTab.h b/src/pglab/QueryTab.h index 11b8b8e..d8b973e 100644 --- a/src/pglab/QueryTab.h +++ b/src/pglab/QueryTab.h @@ -105,7 +105,7 @@ private: std::string getCommand() const; void explain_ready(ExplainRoot::SPtr explain); - void query_ready(std::shared_ptr dbres, qint64 elapsedms); + void query_ready(Expected> dbres, qint64 elapsedms); QTabWidget *getTabWidget(); void setTabCaption(const QString &caption, const QString &tooltip);