Added explain functionality.

Uses json format with jsoncpp as a parser. Then show it in a QTreeView.
Shows inclusive/exclusive times like explain.despesz does. Also a similar
coloring scheme as applied.
This commit is contained in:
Eelke Klein 2016-12-29 13:48:35 +01:00
parent 0d30dc9080
commit 8af6bc4ac5
14 changed files with 9089 additions and 33 deletions

View file

@ -5,6 +5,44 @@
using namespace Pgsql;
namespace {
void set_stdstring_with_charptr(std::string &s, const char *p)
{
if (p) {
s = p;
}
else {
s.clear();
}
}
} // einde unnamed namespace
ErrorDetails ErrorDetails::createErrorDetailsFromPGresult(const PGresult *result)
{
ErrorDetails r;
set_stdstring_with_charptr(r.state, PQresultErrorField(result, PG_DIAG_SQLSTATE)); ///< PG_DIAG_SQLSTATE Error code as listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
set_stdstring_with_charptr(r.severity, PQresultErrorField(result, PG_DIAG_SEVERITY));
set_stdstring_with_charptr(r.messagePrimary, PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY));
set_stdstring_with_charptr(r.messageDetail, PQresultErrorField(result, PG_DIAG_MESSAGE_DETAIL));
set_stdstring_with_charptr(r.messageHint, PQresultErrorField(result, PG_DIAG_MESSAGE_HINT));
const char * p = PQresultErrorField(result, PG_DIAG_STATEMENT_POSITION);
r.statementPosition = p != nullptr ? atoi(p) : -1; ///< First character is one, measured in characters not bytes!
p = PQresultErrorField(result, PG_DIAG_INTERNAL_POSITION);
r.internalPosition = p != nullptr ? atoi(p) : -1;
set_stdstring_with_charptr(r.internalQuery, PQresultErrorField(result, PG_DIAG_INTERNAL_QUERY));
set_stdstring_with_charptr(r.context, PQresultErrorField(result, PG_DIAG_CONTEXT));
set_stdstring_with_charptr(r.schemaName, PQresultErrorField(result, PG_DIAG_SCHEMA_NAME));
set_stdstring_with_charptr(r.tableName, PQresultErrorField(result, PG_DIAG_TABLE_NAME));
set_stdstring_with_charptr(r.columnName, PQresultErrorField(result, PG_DIAG_COLUMN_NAME));
set_stdstring_with_charptr(r.datatypeName, PQresultErrorField(result, PG_DIAG_DATATYPE_NAME));
set_stdstring_with_charptr(r.constraintName, PQresultErrorField(result, PG_DIAG_CONSTRAINT_NAME));
set_stdstring_with_charptr(r.sourceFile, PQresultErrorField(result, PG_DIAG_SOURCE_FILE));
set_stdstring_with_charptr(r.sourceLine, PQresultErrorField(result, PG_DIAG_SOURCE_LINE));
set_stdstring_with_charptr(r.sourceFunction, PQresultErrorField(result, PG_DIAG_SOURCE_FUNCTION));
return r;
}
Result::Result(PGresult *res)
: result(res)
@ -52,6 +90,39 @@ std::string Result::getResStatus()
return "";
}
std::string Result::diagSqlState()
{
std::string s(PQresultErrorField(result, PG_DIAG_SQLSTATE));
return s;
}
ErrorDetails Result::diagDetails()
{
// ErrorDetails r;
// r.state = PQresultErrorField(result, PG_DIAG_SQLSTATE); ///< PG_DIAG_SQLSTATE Error code as listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
// r.severity = PQresultErrorField(result, PG_DIAG_SEVERITY);
// r.messagePrimary = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
// r.messageDetail = PQresultErrorField(result, PG_DIAG_MESSAGE_DETAIL);
// r.messageHint = PQresultErrorField(result, PG_DIAG_MESSAGE_HINT);
// r.statementPosition = atoi(PQresultErrorField(result, PG_DIAG_STATEMENT_POSITION)); ///< First character is one, measured in characters not bytes!
// r.internalPosition = atoi(PQresultErrorField(result, PG_DIAG_INTERNAL_POSITION));
// r.internalQuery = PQresultErrorField(result, PG_DIAG_INTERNAL_QUERY);
// r.context = PQresultErrorField(result, PG_DIAG_CONTEXT);
// r.schemaName = PQresultErrorField(result, PG_DIAG_SCHEMA_NAME);
// r.tableName = PQresultErrorField(result, PG_DIAG_TABLE_NAME);
// r.columnName = PQresultErrorField(result, PG_DIAG_COLUMN_NAME);
// r.datatypeName = PQresultErrorField(result, PG_DIAG_DATATYPE_NAME);
// r.constraintName = PQresultErrorField(result, PG_DIAG_CONSTRAINT_NAME);
// r.sourceFile = PQresultErrorField(result, PG_DIAG_SOURCE_FILE);
// r.sourceLine = PQresultErrorField(result, PG_DIAG_SOURCE_LINE);
// r.sourceFunction = PQresultErrorField(result, PG_DIAG_SOURCE_FUNCTION);
// return r;
return ErrorDetails::createErrorDetailsFromPGresult(result);
}
int Result::getRows() const
{
return PQntuples(result);
@ -73,6 +144,42 @@ const char * Result::getVal(int col, int row) const
}
Canceller::Canceller(PGcancel *c)
: m_cancel(c)
{}
Canceller::Canceller(Canceller&& rhs)
: m_cancel(rhs.m_cancel)
{
rhs.m_cancel = nullptr;
}
Canceller& Canceller::operator=(Canceller&& rhs)
{
if (m_cancel) {
PQfreeCancel(m_cancel);
}
m_cancel = rhs.m_cancel;
rhs.m_cancel = nullptr;
return *this;
}
Canceller::~Canceller()
{
if (m_cancel) {
PQfreeCancel(m_cancel);
}
}
void Canceller::cancel()
{
const int errbuf_size = 256;
char errbuf[errbuf_size];
PQcancel(m_cancel, errbuf, errbuf_size);
}
Connection::Connection() = default;
Connection::~Connection()
@ -102,6 +209,11 @@ void Connection::close()
}
}
Canceller Connection::getCancel()
{
Canceller c(PQgetCancel(conn));
return c;
}
bool Connection::connect(const char *params)
{
@ -150,12 +262,7 @@ std::string Connection::getErrorMessage() const
Result Connection::query(const char * command)
{
PGresult *result = PQexec(conn, command);
if (result) {
return Result(result);
}
else {
throw std::runtime_error("Failed to allocate result object");
}
return Result(result);
}
@ -187,3 +294,17 @@ bool Connection::isBusy()
int res = PQisBusy(conn);
return res == 1;
}
void Connection::setNoticeReceiver(std::function<void(const PGresult *)> callback)
{
notifyReceiver = callback;
PQsetNoticeReceiver(conn,
Connection::notifyReceiveFunc
, reinterpret_cast<void*>(this));
}
void Connection::notifyReceiveFunc(void *arg, const PGresult *result)
{
Connection *c = reinterpret_cast<Connection *>(arg);
c->notifyReceiver(result);
}