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

@ -25,17 +25,21 @@ ArrayParser::NextElemResult ArrayParser::GetNextElem()
{
// We should be at the start of an element or at the end of the array
NextElemResult result = { false, std::nullopt };
if (pos < end && *pos != ArrayEnd) {
if (*pos == Quote) {
if (pos < end && *pos != ArrayEnd)
{
if (*pos == Quote)
{
// parse quoted value, slow path removing escapes
parseQuotedValue();
result.ok = true;
result.value = std::string_view(temp);
}
else {
else
{
// parse unquoted value, fast path no escapes
const char *start = pos;
while (pos < end && *pos != Seperator && *pos != ArrayEnd) ++pos;
while (pos < end && *pos != Seperator && *pos != ArrayEnd)
++pos;
if (pos == end) // reached end of data shouldn't happen
throw std::runtime_error("Invalid input");
@ -47,7 +51,6 @@ ArrayParser::NextElemResult ArrayParser::GetNextElem()
// move to start of next element
++pos; // skip seperator
skipWhitespace();
}
return result;
}
@ -62,12 +65,15 @@ void ArrayParser::parseQuotedValue()
if (pos == end)
throw std::runtime_error("Invalid input");
while (pos < end) {
if (*pos == Quote) {
while (pos < end)
{
if (*pos == Quote)
{
++pos;
break;
}
if (*pos == '\\') {
if (*pos == '\\')
{
++pos;
if (pos == end)
throw std::runtime_error("Invalid input");
@ -84,9 +90,11 @@ void ArrayParser::initializeParse()
// Test if non empty string (empty string is an empty array)
//
skipWhitespace();
if (pos < end) {
if (pos < end)
{
// first character should be opening brace
if (*pos != ArrayStart) {
if (*pos != ArrayStart)
{
throw std::runtime_error("Unexpected input");
}
++pos;

View file

@ -14,19 +14,18 @@ namespace Pgsql {
Canceller& Canceller::operator=(Canceller&& rhs)
{
if (m_cancel) {
if (m_cancel)
PQfreeCancel(m_cancel);
}
m_cancel = rhs.m_cancel;
m_cancel = rhs.m_cancel;
rhs.m_cancel = nullptr;
return *this;
}
Canceller::~Canceller()
{
if (m_cancel) {
if (m_cancel)
PQfreeCancel(m_cancel);
}
}
bool Canceller::cancel(std::string *error)
@ -34,9 +33,8 @@ namespace Pgsql {
const int errbuf_size = 256;
char errbuf[errbuf_size];
bool res = PQcancel(m_cancel, errbuf, errbuf_size);
if (!res && error) {
if (!res && error)
*error = errbuf;
}
return res;
}

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 */