Improved error reporting.

This commit is contained in:
Eelke Klein 2017-09-10 10:11:58 +02:00
parent b372fb6c6b
commit 56bd304756
4 changed files with 84 additions and 76 deletions

View file

@ -148,6 +148,7 @@ void ASyncDBConnection::async_query_handler(boost::system::error_code ec, std::s
} }
else { else {
// error during consume // error during consume
auto error_msg = m_connection.getErrorMessage();
} }
//return finished; //return finished;

View file

@ -6,6 +6,7 @@
#include "Pgsql_Connection.h" #include "Pgsql_Connection.h"
#include "Pgsql_Params.h" #include "Pgsql_Params.h"
#include "Pgsql_Result.h" #include "Pgsql_Result.h"
#include "Expected.h"
#include "ConnectionConfig.h" #include "ConnectionConfig.h"
#include <QElapsedTimer> #include <QElapsedTimer>
#include <mutex> #include <mutex>
@ -29,7 +30,7 @@ public:
Terminating ///< shutting down Terminating ///< shutting down
}; };
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>, qint64)>; using on_result_callback = std::function<void(Expected<std::shared_ptr<Pgsql::Result>>, qint64)>;
explicit ASyncDBConnection(boost::asio::io_service &ios); explicit ASyncDBConnection(boost::asio::io_service &ios);
~ASyncDBConnection(); ~ASyncDBConnection();

View file

@ -205,14 +205,14 @@ void QueryTab::execute()
if (m_queryParamListController->empty()) if (m_queryParamListController->empty())
m_dbConnection.send(cmd, m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res, qint64 elapsedms) [this](Expected<std::shared_ptr<Pgsql::Result>> res, qint64 elapsedms)
{ {
m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); }); m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); });
}); });
else else
m_dbConnection.send(cmd, m_dbConnection.send(cmd,
m_queryParamListController->params(), m_queryParamListController->params(),
[this](std::shared_ptr<Pgsql::Result> res, qint64 elapsedms) [this](Expected<std::shared_ptr<Pgsql::Result>> res, qint64 elapsedms)
{ {
m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); }); m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); });
}); });
@ -234,10 +234,11 @@ void QueryTab::explain(bool analyze)
m_stopwatch.start(); m_stopwatch.start();
std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, BUFFERS, FORMAT JSON) " + getCommand(); std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
m_dbConnection.send(cmd, m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res, qint64 ) [this](Expected<std::shared_ptr<Pgsql::Result>> exp_res, qint64 )
{ {
if (res) { if (exp_res.valid()) {
// Process explain data seperately // Process explain data seperately
auto res = exp_res.get();
std::thread([this,res]() std::thread([this,res]()
{ {
std::shared_ptr<ExplainRoot> explain; std::shared_ptr<ExplainRoot> explain;
@ -299,15 +300,14 @@ bool QueryTab::saveSqlTo(const QString &filename)
QString text = ui->queryEdit->toPlainText(); QString text = ui->queryEdit->toPlainText();
stream << text; stream << text;
/* /*
QTextDocument *doc = ui->queryEdit->document(); QTextDocument *doc = ui->queryEdit->document();
QTextBlock block = doc->firstBlock(); QTextBlock block = doc->firstBlock();
while (stream.status() == QTextStream::Ok && block.isValid()) { while (stream.status() == QTextStream::Ok && block.isValid()) {
QString plain = block.text(); QString plain = block.text();
stream << plain << "\n"; stream << plain << "\n";
block = block.next(); block = block.next();
}*/ }
*/
stream.flush(); stream.flush();
if (stream.status() == QTextStream::Ok) { 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<Pgsql::Result> dbres, qint64 elapsedms) void QueryTab::query_ready(Expected<std::shared_ptr<Pgsql::Result>> exp_res, qint64 elapsedms)
{ {
if (dbres) { if (exp_res.valid()) {
addLog("query_ready with result"); auto dbres = exp_res.get();
auto st = dbres->resultStatus(); if (dbres) {
if (st == PGRES_TUPLES_OK) { addLog("query_ready with result");
//int n_rows = dbres->getRows(); auto st = dbres->resultStatus();
//QString rowcount_str = QString("rows: %1").arg(dbres->getRows()); 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<QueryResultModel>(nullptr , dbres); auto result_model = std::make_shared<QueryResultModel>(nullptr , dbres);
TuplesResultWidget *trw = new TuplesResultWidget; TuplesResultWidget *trw = new TuplesResultWidget;
trw->setResult(result_model, elapsedms); trw->setResult(result_model, elapsedms);
resultList.push_back(trw); resultList.push_back(trw);
ui->tabWidget->addTab(trw, "Data"); ui->tabWidget->addTab(trw, "Data");
if (resultList.size() == 1) if (resultList.size() == 1)
ui->tabWidget->setCurrentWidget(trw); ui->tabWidget->setCurrentWidget(trw);
} }
else { else {
if (st == PGRES_COMMAND_OK) { if (st == PGRES_COMMAND_OK) {
int tuples_affected = dbres->tuplesAffected(); int tuples_affected = dbres->tuplesAffected();
QString msg; QString msg;
if (tuples_affected >= 0) if (tuples_affected >= 0)
msg = tr("Query returned succesfully: %1 rows affected, execution time %2") msg = tr("Query returned succesfully: %1 rows affected, execution time %2")
.arg(QString::number(tuples_affected)) .arg(QString::number(tuples_affected))
.arg(msfloatToHumanReadableString(elapsedms)); .arg(msfloatToHumanReadableString(elapsedms));
else else
msg = tr("Query returned succesfully, execution time %1") msg = tr("Query returned succesfully, execution time %1")
.arg(msfloatToHumanReadableString(elapsedms)); .arg(msfloatToHumanReadableString(elapsedms));
ui->messagesEdit->append(msg); ui->messagesEdit->append(msg);
ui->tabWidget->setCurrentWidget(ui->messageTab); ui->tabWidget->setCurrentWidget(ui->messageTab);
} }
else { else {
// if (st == PGRES_EMPTY_QUERY) { // if (st == PGRES_EMPTY_QUERY) {
// statusBar()->showMessage(tr("Empty query.")); // statusBar()->showMessage(tr("Empty query."));
// } // }
// else if (st == PGRES_COPY_OUT) { // else if (st == PGRES_COPY_OUT) {
// statusBar()->showMessage(tr("COPY OUT.")); // statusBar()->showMessage(tr("COPY OUT."));
// } // }
// else if (st == PGRES_COPY_IN) { // else if (st == PGRES_COPY_IN) {
// statusBar()->showMessage(tr("COPY IN.")); // statusBar()->showMessage(tr("COPY IN."));
// } // }
// else if (st == PGRES_BAD_RESPONSE) { // else if (st == PGRES_BAD_RESPONSE) {
// statusBar()->showMessage(tr("BAD RESPONSE.")); // statusBar()->showMessage(tr("BAD RESPONSE."));
// } // }
// else if (st == PGRES_NONFATAL_ERROR) { // else if (st == PGRES_NONFATAL_ERROR) {
// statusBar()->showMessage(tr("NON FATAL ERROR.")); // statusBar()->showMessage(tr("NON FATAL ERROR."));
// } // }
// else if (st == PGRES_FATAL_ERROR) { // else if (st == PGRES_FATAL_ERROR) {
// statusBar()->showMessage(tr("FATAL ERROR.")); // statusBar()->showMessage(tr("FATAL ERROR."));
// } // }
// else if (st == PGRES_COPY_BOTH) { // else if (st == PGRES_COPY_BOTH) {
// statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication.")); // statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
// } // }
// else if (st == PGRES_SINGLE_TUPLE) { // else if (st == PGRES_SINGLE_TUPLE) {
// statusBar()->showMessage(tr("SINGLE TUPLE result.")); // statusBar()->showMessage(tr("SINGLE TUPLE result."));
// } // }
// else { // else {
// statusBar()->showMessage(tr("No tuples returned, possibly an error...")); // statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
// } // }
ui->tabWidget->setCurrentWidget(ui->messageTab); ui->tabWidget->setCurrentWidget(ui->messageTab);
receiveNotice(dbres->diagDetails()); receiveNotice(dbres->diagDetails());
} }
} }
} }
else { else {
m_stopwatch.stop(); m_stopwatch.stop();
addLog("query_ready with NO result"); addLog("query_ready with NO result");
} }
}
else {
// we have an error
}
} }
void QueryTab::clearResult() void QueryTab::clearResult()

View file

@ -105,7 +105,7 @@ private:
std::string getCommand() const; std::string getCommand() const;
void explain_ready(ExplainRoot::SPtr explain); void explain_ready(ExplainRoot::SPtr explain);
void query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedms); void query_ready(Expected<std::shared_ptr<Pgsql::Result>> dbres, qint64 elapsedms);
QTabWidget *getTabWidget(); QTabWidget *getTabWidget();
void setTabCaption(const QString &caption, const QString &tooltip); void setTabCaption(const QString &caption, const QString &tooltip);