| 1 | /* |
| 2 | * Copyright (C) 2002,2003 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #ifdef WITH_SQL_PGSQL |
| 19 | |
| 20 | #include "common/setup_before.h" |
| 21 | #include <libpq-fe.h> |
| 22 | #include <cstdlib> |
| 23 | #include "common/eventlog.h" |
| 24 | #include "common/xalloc.h" |
| 25 | #include "storage_sql.h" |
| 26 | #include "sql_pgsql.h" |
| 27 | #include "common/setup_after.h" |
| 28 | |
| 29 | namespace pvpgn |
| 30 | { |
| 31 | |
| 32 | namespace bnetd |
| 33 | { |
| 34 | |
| 35 | static int sql_pgsql_init(const char *, const char *, const char *, const char *, const char *, const char *); |
| 36 | static int sql_pgsql_close(void); |
| 37 | static t_sql_res * sql_pgsql_query_res(const char *); |
| 38 | static int sql_pgsql_query(const char *); |
| 39 | static t_sql_row * sql_pgsql_fetch_row(t_sql_res *); |
| 40 | static void sql_pgsql_free_result(t_sql_res *); |
| 41 | static unsigned int sql_pgsql_num_rows(t_sql_res *); |
| 42 | static unsigned int sql_pgsql_num_fields(t_sql_res *); |
| 43 | static unsigned int sql_pgsql_affected_rows(void); |
| 44 | static t_sql_field * sql_pgsql_fetch_fields(t_sql_res *); |
| 45 | static int sql_pgsql_free_fields(t_sql_field *); |
| 46 | static void sql_pgsql_escape_string(char *, const char *, int); |
| 47 | |
| 48 | t_sql_engine sql_pgsql = { |
| 49 | sql_pgsql_init, |
| 50 | sql_pgsql_close, |
| 51 | sql_pgsql_query_res, |
| 52 | sql_pgsql_query, |
| 53 | sql_pgsql_fetch_row, |
| 54 | sql_pgsql_free_result, |
| 55 | sql_pgsql_num_rows, |
| 56 | sql_pgsql_num_fields, |
| 57 | sql_pgsql_affected_rows, |
| 58 | sql_pgsql_fetch_fields, |
| 59 | sql_pgsql_free_fields, |
| 60 | sql_pgsql_escape_string |
| 61 | }; |
| 62 | |
| 63 | static PGconn *pgsql = NULL; |
| 64 | static unsigned int lastarows = 0; |
| 65 | |
| 66 | typedef struct { |
| 67 | int crow; |
| 68 | char ** rowbuf; |
| 69 | PGresult *pgres; |
| 70 | } t_pgsql_res; |
| 71 | |
| 72 | #ifndef RUNTIME_LIBS |
| 73 | #define p_PQclear PQclear |
| 74 | #define p_PQcmdTuples PQcmdTuples |
| 75 | #define p_PQerrorMessage PQerrorMessage |
| 76 | #define p_PQescapeString PQescapeString |
| 77 | #define p_PQexec PQexec |
| 78 | #define p_PQfinish PQfinish |
| 79 | #define p_PQfname PQfname |
| 80 | #define p_PQgetvalue PQgetvalue |
| 81 | #define p_PQnfields PQnfields |
| 82 | #define p_PQntuples PQntuples |
| 83 | #define p_PQresultStatus PQresultStatus |
| 84 | #define p_PQsetdbLogin PQsetdbLogin |
| 85 | #define p_PQstatus PQstatus |
| 86 | #else |
| 87 | /* RUNTIME_LIBS */ |
| 88 | static int pgsql_load_dll(void); |
| 89 | |
| 90 | typedef void(*f_PQclear)(PGresult*); |
| 91 | typedef char* (*f_PQcmdTuples)(PGresult*); |
| 92 | typedef char* (*f_PQerrorMessage)(const PGconn*); |
| 93 | typedef size_t(*f_PQescapeString)(char*, const char*, size_t); |
| 94 | typedef PGresult* (*f_PQexec)(PGconn*, const char*); |
| 95 | typedef void(*f_PQfinish)(PGconn*); |
| 96 | typedef char* (*f_PQfname)(const PGresult*, int); |
| 97 | typedef char* (*f_PQgetvalue)(const PGresult*, int, int); |
| 98 | typedef int(*f_PQnfields)(const PGresult*); |
| 99 | typedef int(*f_PQntuples)(const PGresult*); |
| 100 | typedef ExecStatusType(*f_PQresultStatus)(const PGresult*); |
| 101 | typedef PGconn* (*f_PQsetdbLogin)(const char*, const char*, const char*, const char*, const char*, const char*, const char*); |
| 102 | typedef ConnStatusType(*f_PQstatus)(const PGconn*); |
| 103 | |
| 104 | static f_PQclear p_PQclear; |
| 105 | static f_PQcmdTuples p_PQcmdTuples; |
| 106 | static f_PQerrorMessage p_PQerrorMessage; |
| 107 | static f_PQescapeString p_PQescapeString; |
| 108 | static f_PQexec p_PQexec; |
| 109 | static f_PQfinish p_PQfinish; |
| 110 | static f_PQfname p_PQfname; |
| 111 | static f_PQgetvalue p_PQgetvalue; |
| 112 | static f_PQnfields p_PQnfields; |
| 113 | static f_PQntuples p_PQntuples; |
| 114 | static f_PQresultStatus p_PQresultStatus; |
| 115 | static f_PQsetdbLogin p_PQsetdbLogin; |
| 116 | static f_PQstatus p_PQstatus; |
| 117 | |
| 118 | #include "compat/runtime_libs.h" /* defines OpenLibrary(), GetFunction(), CloseLibrary() & PGSQL_LIB */ |
| 119 | |
| 120 | static void * handle = NULL; |
| 121 | |
| 122 | static int pgsql_load_dll(void) |
| 123 | { |
| 124 | if ((handle = OpenLibrary(PGSQL_LIB)) == NULL) return -1; |
| 125 | |
| 126 | if (((p_PQclear = (f_PQclear)GetFunction(handle, "PQclear")) == NULL) || |
| 127 | ((p_PQcmdTuples = (f_PQcmdTuples)GetFunction(handle, "PQcmdTuples")) == NULL) || |
| 128 | ((p_PQerrorMessage = (f_PQerrorMessage)GetFunction(handle, "PQerrorMessage")) == NULL) || |
| 129 | ((p_PQescapeString = (f_PQescapeString)GetFunction(handle, "PQescapeString")) == NULL) || |
| 130 | ((p_PQexec = (f_PQexec)GetFunction(handle, "PQexec")) == NULL) || |
| 131 | ((p_PQfinish = (f_PQfinish)GetFunction(handle, "PQfinish")) == NULL) || |
| 132 | ((p_PQfname = (f_PQfname)GetFunction(handle, "PQfname")) == NULL) || |
| 133 | ((p_PQgetvalue = (f_PQgetvalue)GetFunction(handle, "PQgetvalue")) == NULL) || |
| 134 | ((p_PQnfields = (f_PQnfields)GetFunction(handle, "PQnfields")) == NULL) || |
| 135 | ((p_PQntuples = (f_PQntuples)GetFunction(handle, "PQntuples")) == NULL) || |
| 136 | ((p_PQresultStatus = (f_PQresultStatus)GetFunction(handle, "PQresultStatus")) == NULL) || |
| 137 | ((p_PQsetdbLogin = (f_PQsetdbLogin)GetFunction(handle, "PQsetdbLogin")) == NULL) || |
| 138 | ((p_PQstatus = (f_PQstatus)GetFunction(handle, "PQstatus")) == NULL)) |
| 139 | { |
| 140 | CloseLibrary(handle); |
| 141 | handle = NULL; |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | static int sql_pgsql_init(const char *host, const char *port, const char *socket, const char *name, const char *user, const char *pass) |
| 150 | { |
| 151 | const char *tmphost; |
| 152 | |
| 153 | if (name == NULL || user == NULL) { |
| 154 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL parameter"); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | tmphost = host != NULL ? host : socket; |
| 159 | #ifdef RUNTIME_LIBS |
| 160 | if (pgsql_load_dll()) { |
| 161 | eventlog(eventlog_level_error, __FUNCTION__, "error loading library file \"{}\"", PGSQL_LIB); |
| 162 | return -1; |
| 163 | } |
| 164 | #endif |
| 165 | if ((pgsql = p_PQsetdbLogin(host, port, NULL, NULL, name, user, pass)) == NULL) { |
| 166 | eventlog(eventlog_level_error, __FUNCTION__, "not enougn memory for new pgsql connection"); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | if (p_PQstatus(pgsql) != CONNECTION_OK) { |
| 171 | eventlog(eventlog_level_error, __FUNCTION__, "error connecting to database (db said: '{}')", p_PQerrorMessage(pgsql)); |
| 172 | p_PQfinish(pgsql); |
| 173 | pgsql = NULL; |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int sql_pgsql_close(void) |
| 181 | { |
| 182 | if (pgsql) { |
| 183 | p_PQfinish(pgsql); |
| 184 | pgsql = NULL; |
| 185 | } |
| 186 | #ifdef RUNTIME_LIBS |
| 187 | if (handle) { |
| 188 | CloseLibrary(handle); |
| 189 | handle = NULL; |
| 190 | } |
| 191 | #endif |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static t_sql_res * sql_pgsql_query_res(const char * query) |
| 196 | { |
| 197 | t_pgsql_res *res; |
| 198 | PGresult *pgres; |
| 199 | |
| 200 | if (pgsql == NULL) { |
| 201 | eventlog(eventlog_level_error, __FUNCTION__, "pgsql driver not initilized"); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | if (query == NULL) { |
| 206 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL query"); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | if ((pgres = p_PQexec(pgsql, query)) == NULL) { |
| 211 | eventlog(eventlog_level_error, __FUNCTION__, "not enough memory for query ({})", query); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | if (p_PQresultStatus(pgres) != PGRES_TUPLES_OK) { |
| 216 | /* eventlog(eventlog_level_debug, __FUNCTION__, "got error from query ({})", query); */ |
| 217 | p_PQclear(pgres); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | res = (t_pgsql_res *)xmalloc(sizeof(t_pgsql_res)); |
| 222 | res->rowbuf = (char **)xmalloc(sizeof(char *)* p_PQnfields(pgres)); |
| 223 | res->pgres = pgres; |
| 224 | res->crow = 0; |
| 225 | |
| 226 | /* eventlog(eventlog_level_debug, __FUNCTION__, "res: {:p} res->rowbuf: {:p} res->crow: {} res->pgres: {:p}", res, res->rowbuf, res->crow, res->pgres); */ |
| 227 | return res; |
| 228 | } |
| 229 | |
| 230 | static void _pgsql_update_arows(const char *str) |
| 231 | { |
| 232 | if (!str || str[0] == '\0') lastarows = 0; |
| 233 | lastarows = (unsigned int)std::atoi(str); |
| 234 | } |
| 235 | |
| 236 | static int sql_pgsql_query(const char * query) |
| 237 | { |
| 238 | PGresult *pgres; |
| 239 | int res; |
| 240 | |
| 241 | if (pgsql == NULL) { |
| 242 | eventlog(eventlog_level_error, __FUNCTION__, "pgsql driver not initilized"); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (query == NULL) { |
| 247 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL query"); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | if ((pgres = p_PQexec(pgsql, query)) == NULL) { |
| 252 | eventlog(eventlog_level_error, __FUNCTION__, "not enough memory for result"); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | res = p_PQresultStatus(pgres) == PGRES_COMMAND_OK ? 0 : -1; |
| 257 | /* Dizzy: HACK ALERT! cache affected rows here before destroying result */ |
| 258 | if (!res) _pgsql_update_arows(p_PQcmdTuples(pgres)); |
| 259 | p_PQclear(pgres); |
| 260 | |
| 261 | return res; |
| 262 | } |
| 263 | |
| 264 | static t_sql_row * sql_pgsql_fetch_row(t_sql_res *result) |
| 265 | { |
| 266 | int nofields, i; |
| 267 | t_pgsql_res *res = (t_pgsql_res *)result; |
| 268 | |
| 269 | if (res == NULL) { |
| 270 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | if (res->crow < 0) { |
| 275 | eventlog(eventlog_level_error, __FUNCTION__, "got called without a proper res query"); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | if (res->crow >= p_PQntuples(res->pgres)) return NULL; /* end of result */ |
| 280 | |
| 281 | nofields = p_PQnfields(res->pgres); |
| 282 | for (i = 0; i < nofields; i++) { |
| 283 | res->rowbuf[i] = p_PQgetvalue(res->pgres, res->crow, i); |
| 284 | /* the next line emulates the mysql way where NULL containing fields return NULL */ |
| 285 | if (res->rowbuf[i] && res->rowbuf[i][0] == '\0') res->rowbuf[i] = NULL; |
| 286 | } |
| 287 | |
| 288 | res->crow++; |
| 289 | |
| 290 | /* eventlog(eventlog_level_debug, __FUNCTION__, "res: {:p} res->rowbuf: {:p} res->crow: {} res->pgres: {:p}", res, res->rowbuf, res->crow, res->pgres); */ |
| 291 | return res->rowbuf; |
| 292 | } |
| 293 | |
| 294 | static void sql_pgsql_free_result(t_sql_res *result) |
| 295 | { |
| 296 | t_pgsql_res *res = (t_pgsql_res *)result; |
| 297 | |
| 298 | if (res == NULL) return; |
| 299 | /* eventlog(eventlog_level_debug, __FUNCTION__, "res: {:p} res->rowbuf: {:p} res->crow: {} res->pgres: {:p}", res, res->rowbuf, res->crow, res->pgres); */ |
| 300 | |
| 301 | if (res->pgres) p_PQclear(res->pgres); |
| 302 | if (res->rowbuf) xfree((void*)res->rowbuf); |
| 303 | xfree((void*)res); |
| 304 | } |
| 305 | |
| 306 | static unsigned int sql_pgsql_num_rows(t_sql_res *result) |
| 307 | { |
| 308 | if (result == NULL) { |
| 309 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | return p_PQntuples(((t_pgsql_res *)result)->pgres); |
| 314 | } |
| 315 | |
| 316 | static unsigned int sql_pgsql_num_fields(t_sql_res *result) |
| 317 | { |
| 318 | if (result == NULL) { |
| 319 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | return p_PQnfields(((t_pgsql_res *)result)->pgres); |
| 324 | } |
| 325 | |
| 326 | static unsigned int sql_pgsql_affected_rows(void) |
| 327 | { |
| 328 | return lastarows; |
| 329 | } |
| 330 | |
| 331 | static t_sql_field * sql_pgsql_fetch_fields(t_sql_res *result) |
| 332 | { |
| 333 | t_pgsql_res *res = (t_pgsql_res *)result; |
| 334 | unsigned fieldno, i; |
| 335 | t_sql_field *rfields; |
| 336 | |
| 337 | if (result == NULL) { |
| 338 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | fieldno = p_PQnfields(res->pgres); |
| 343 | |
| 344 | rfields = (t_sql_field *)xmalloc(sizeof(t_sql_field)* (fieldno + 1)); |
| 345 | for (i = 0; i < fieldno; i++) |
| 346 | rfields[i] = p_PQfname(res->pgres, i); |
| 347 | rfields[i] = NULL; |
| 348 | |
| 349 | return rfields; |
| 350 | } |
| 351 | |
| 352 | static int sql_pgsql_free_fields(t_sql_field *fields) |
| 353 | { |
| 354 | if (fields == NULL) { |
| 355 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL fields"); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | xfree((void*)fields); |
| 360 | return 0; /* PQclear() should free the rest properly */ |
| 361 | } |
| 362 | |
| 363 | static void sql_pgsql_escape_string(char *escape, const char *from, int len) |
| 364 | { |
| 365 | p_PQescapeString(escape, from, len); |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | } |
| 371 | |
| 372 | #endif /* WITH_SQL_PGSQL */ |