aboutsummaryrefslogtreecommitdiff
path: root/src/database_postgres.cc
diff options
context:
space:
mode:
authorfusion32 <marcopuzziello@gmail.com>2025-10-14 23:24:42 -0300
committerfusion32 <marcopuzziello@gmail.com>2025-10-14 23:24:42 -0300
commita4b5623497d10febf17231b47d9a1b4cd3c6767d (patch)
treed6be4e31125960683d3069c1218159e6320dc290 /src/database_postgres.cc
parent00d91512b48c70c3cebcdb5ed37c601653425bf6 (diff)
downloadquerymanager-a4b5623497d10febf17231b47d9a1b4cd3c6767d.tar.gz
querymanager-a4b5623497d10febf17231b47d9a1b4cd3c6767d.zip
make query output parameter names more specific
This also avoids conflicts with database RESULT objects that we lacked with SQLite but show up with PostgreSQL and MySQL.
Diffstat (limited to 'src/database_postgres.cc')
-rw-r--r--src/database_postgres.cc29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/database_postgres.cc b/src/database_postgres.cc
index d6bd15b..3fe9e11 100644
--- a/src/database_postgres.cc
+++ b/src/database_postgres.cc
@@ -150,7 +150,7 @@ static void ParamBool(ParamBuffer *Params, bool Value){
}
}
-static void ParamInteger(ParamBuffer *Params, int Value){
+static void ParamInt(ParamBuffer *Params, int Value){
if(Params->PreferredFormat == 1){ // BINARY FORMAT
uint8 Data[4];
BufferWrite32BE(Data, (uint32)Value);
@@ -1027,7 +1027,7 @@ bool GetWorldConfig(TDatabase *Database, int WorldID, TWorldConfig *WorldConfig)
ParamBuffer Params = {};
ParamBegin(&Params, 1, 1);
- ParamInteger(&Params, WorldID);
+ ParamInt(&Params, WorldID);
PGresult *Result = PQexecPrepared(Database->Handle, Stmt, Params.NumParams,
Params.Values, Params.Lengths, Params.Formats, 1);
AutoResultClear ResultGuard(Result);
@@ -1054,8 +1054,29 @@ bool GetWorldConfig(TDatabase *Database, int WorldID, TWorldConfig *WorldConfig)
return true;
}
-bool AccountExists(TDatabase *Database, int AccountID, const char *Email, bool *Result){
- return false;
+bool AccountExists(TDatabase *Database, int AccountID, const char *Email, bool *Exists){
+ const char *Stmt = PrepareQuery(Database,
+ "SELECT 1 FROM Accounts"
+ " WHERE AccountID = $1::INTEGER OR Email = $2::TEXT");
+ if(Stmt == NULL){
+ LOG_ERR("Failed to prepare query");
+ return false;
+ }
+
+ ParamBuffer Params = {};
+ ParamBegin(&Params, 2, 1);
+ ParamInt(&Params, AccountID);
+ ParamText(&Params, Email);
+ PGresult *Result = PQexecPrepared(Database->Handle, Stmt, Params.NumParams,
+ Params.Values, Params.Lengths, Params.Formats, 1);
+ AutoResultClear ResultGuard(Result);
+ if(PQresultStatus(Result) != PGRES_TUPLES_OK){
+ LOG_ERR("Failed to execute query: %s", PQerrorMessage(Database->Handle));
+ return false;
+ }
+
+ *Exists = (PQntuples(Result) > 0);
+ return true;
}
bool AccountNumberExists(TDatabase *Database, int AccountID, bool *Result){