Skip to content

Commit

Permalink
Add PostgreSQL helper pg_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMcMac authored and nerzhul committed Nov 10, 2022
1 parent f8c781b commit 00eb659
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/database/database-postgresql.cpp
Expand Up @@ -276,7 +276,7 @@ void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)
argLen, argFmt, false);

if (PQntuples(results))
block->assign(PQgetvalue(results, 0, 0), PQgetlength(results, 0, 0));
*block = pg_to_string(results, 0, 0);
else
block->clear();

Expand Down Expand Up @@ -697,8 +697,8 @@ bool AuthDatabasePostgreSQL::getAuth(const std::string &name, AuthEntry &res)
}

res.id = pg_to_uint(result, 0, 0);
res.name = std::string(PQgetvalue(result, 0, 1), PQgetlength(result, 0, 1));
res.password = std::string(PQgetvalue(result, 0, 2), PQgetlength(result, 0, 2));
res.name = pg_to_string(result, 0, 1);
res.password = pg_to_string(result, 0, 2);
res.last_login = pg_to_int(result, 0, 3);

PQclear(result);
Expand Down Expand Up @@ -878,11 +878,8 @@ bool ModMetadataDatabasePostgreSQL::getModEntries(const std::string &modname, St

int numrows = PQntuples(results);

for (int row = 0; row < numrows; ++row) {
std::string key(PQgetvalue(results, row, 0), PQgetlength(results, row, 0));
std::string value(PQgetvalue(results, row, 1), PQgetlength(results, row, 1));
storage->emplace(std::move(key), std::move(value));
}
for (int row = 0; row < numrows; ++row)
(*storage)[pg_to_string(results, row, 0)] = pg_to_string(results, row, 1);

PQclear(results);

Expand All @@ -904,7 +901,7 @@ bool ModMetadataDatabasePostgreSQL::getModKeys(const std::string &modname,

storage->reserve(storage->size() + numrows);
for (int row = 0; row < numrows; ++row)
storage->emplace_back(PQgetvalue(results, row, 0), PQgetlength(results, row, 0));
storage->push_back(pg_to_string(results, row, 0));

PQclear(results);

Expand All @@ -925,7 +922,7 @@ bool ModMetadataDatabasePostgreSQL::getModEntry(const std::string &modname,
bool found = numrows > 0;

if (found)
value->assign(PQgetvalue(results, 0, 0), PQgetlength(results, 0, 0));
*value = pg_to_string(results, 0, 0);

PQclear(results);

Expand Down Expand Up @@ -1014,7 +1011,7 @@ void ModMetadataDatabasePostgreSQL::listMods(std::vector<std::string> *res)
int numrows = PQntuples(results);

for (int row = 0; row < numrows; ++row)
res->emplace_back(PQgetvalue(results, row, 0), PQgetlength(results, row, 0));
res->push_back(pg_to_string(results, row, 0));

PQclear(results);
}
Expand Down
5 changes: 5 additions & 0 deletions src/database/database-postgresql.h
Expand Up @@ -65,6 +65,11 @@ class Database_PostgreSQL: public Database
);
}

inline std::string pg_to_string(PGresult *res, int row, int col)
{
return std::string(PQgetvalue(res, row, col), PQgetlength(res, row, col));
}

inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
const void **params,
const int *paramsLengths = NULL, const int *paramsFormats = NULL,
Expand Down

0 comments on commit 00eb659

Please sign in to comment.