Big cleanup

This commit is contained in:
eelke 2022-05-26 08:25:31 +02:00
parent d3080a08bb
commit 8b671090a0
55 changed files with 214 additions and 3967 deletions

View file

@ -31,7 +31,8 @@ Connection& Connection::operator=(Connection &&rhs)
void Connection::close()
{
if (conn) {
if (conn)
{
PQfinish(conn);
conn = nullptr;
}
@ -86,12 +87,10 @@ int Connection::socket()
std::string Connection::getErrorMessage() const
{
std::string result;
if (conn) {
if (conn)
result = PQerrorMessage(conn);
}
else {
else
result = "no connection";
}
return result;
}
@ -134,24 +133,22 @@ bool Connection::sendQueryParams(const char * command, const Params &params)
std::shared_ptr<Result> Connection::getResult()
{
PGresult *r = PQgetResult(conn);
if (r) {
if (r)
{
throwError(r);
return std::make_shared<Result>(r);
}
else {
else
return nullptr;
}
}
std::shared_ptr<Result> Connection::getResultNoThrow()
{
PGresult *r = PQgetResult(conn);
if (r) {
if (r)
return std::make_shared<Result>(r);
}
else {
else
return nullptr;
}
}
bool Connection::consumeInput()
@ -179,9 +176,8 @@ std::string Connection::escapeLiteral(const std::string_view &literal)
std::unique_ptr<char, void(*)(char*)> result(
PQescapeLiteral(conn, literal.data(), literal.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
if (result)
return std::string(result.get());
}
throw std::runtime_error("escapeLiteral(string_view) failed");
}
@ -191,9 +187,8 @@ QString Connection::escapeLiteral(const QString &literal)
std::unique_ptr<char, void(*)(char*)> result(
PQescapeLiteral(conn, u8.data(), u8.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
if (result)
return QString::fromUtf8(result.get());
}
throw std::runtime_error("escapeLiteral(QString) failed");
}
@ -202,9 +197,8 @@ std::string Connection::escapeIdentifier(const std::string_view &ident)
std::unique_ptr<char, void(*)(char*)> result(
PQescapeIdentifier(conn, ident.data(), ident.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
if (result)
return std::string(result.get());
}
throw std::runtime_error("escapeIdentifier failed");
}
@ -214,9 +208,8 @@ QString Connection::escapeIdentifier(const QString &ident)
std::unique_ptr<char, void(*)(char*)> result(
PQescapeIdentifier(conn, u8.data(), u8.length()),
[](char*p) { if (p) PQfreemem(p); });
if (result) {
if (result)
return QString::fromUtf8(result.get());
}
throw std::runtime_error("escapeIdentifier(QString) failed");
}
@ -234,44 +227,28 @@ QString Connection::getDBName() const
void Connection::testForConnectionError(PGconn *conn)
{
std::string error_msg;
if (conn) {
if (conn)
{
ConnStatusType status = PQstatus(conn);
if (status == CONNECTION_OK)
return;
error_msg = PQerrorMessage(conn);
}
else {
else
error_msg = "Unknown connection failure (maybe out of memory?)";
}
throw PgConnectionError(error_msg);
}
void Connection::throwError(PGresult *result) const
{
auto state = PQresultStatus(result);
if (state == PGRES_BAD_RESPONSE) {
// communication problem
}
else if (state == PGRES_FATAL_ERROR) {
if (state == PGRES_BAD_RESPONSE)
; // communication problem
else if (state == PGRES_FATAL_ERROR)
{
auto details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
throw PgResultError(details);
}
}
//PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
//PGRES_COMMAND_OK, /* a query command that doesn't return
// * anything was executed properly by the
// * backend */
//PGRES_TUPLES_OK, /* a query command that returns tuples was
// * executed properly by the backend, PGresult
// * contains the result tuples */
//PGRES_COPY_OUT, /* Copy Out data transfer in progress */
//PGRES_COPY_IN, /* Copy In data transfer in progress */
//PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
// * backend */
//PGRES_NONFATAL_ERROR, /* notice or warning message */
//PGRES_FATAL_ERROR, /* query failed */
//PGRES_COPY_BOTH, /* Copy In/Out data transfer in progress */
//PGRES_SINGLE_TUPLE /* single tuple from larger resultset */