| 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 | |
| 19 | #ifdef WITH_SQL_MYSQL |
| 20 | #include "common/setup_before.h" |
| 21 | #define NO_CLIENT_LONG_LONG |
| 22 | #include "sql_mysql.h" |
| 23 | |
| 24 | #ifdef HAVE_WINDOWS_H |
| 25 | #include <windows.h> |
| 26 | #endif |
| 27 | #include <mysql.h> |
| 28 | #include <cstdlib> |
| 29 | |
| 30 | #include "common/eventlog.h" |
| 31 | #include "common/xalloc.h" |
| 32 | #include "storage_sql.h" |
| 33 | |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | namespace bnetd |
| 40 | { |
| 41 | |
| 42 | static int sql_mysql_init(const char *, const char *, const char *, const char *, const char *, const char *); |
| 43 | static int sql_mysql_close(void); |
| 44 | static t_sql_res * sql_mysql_query_res(const char *); |
| 45 | static int sql_mysql_query(const char *); |
| 46 | static t_sql_row * sql_mysql_fetch_row(t_sql_res *); |
| 47 | static void sql_mysql_free_result(t_sql_res *); |
| 48 | static unsigned int sql_mysql_num_rows(t_sql_res *); |
| 49 | static unsigned int sql_mysql_num_fields(t_sql_res *); |
| 50 | static unsigned int sql_mysql_affected_rows(void); |
| 51 | static t_sql_field * sql_mysql_fetch_fields(t_sql_res *); |
| 52 | static int sql_mysql_free_fields(t_sql_field *); |
| 53 | static void sql_mysql_escape_string(char *, const char *, int); |
| 54 | |
| 55 | t_sql_engine sql_mysql = { |
| 56 | sql_mysql_init, |
| 57 | sql_mysql_close, |
| 58 | sql_mysql_query_res, |
| 59 | sql_mysql_query, |
| 60 | sql_mysql_fetch_row, |
| 61 | sql_mysql_free_result, |
| 62 | sql_mysql_num_rows, |
| 63 | sql_mysql_num_fields, |
| 64 | sql_mysql_affected_rows, |
| 65 | sql_mysql_fetch_fields, |
| 66 | sql_mysql_free_fields, |
| 67 | sql_mysql_escape_string |
| 68 | }; |
| 69 | |
| 70 | static MYSQL *mysql = NULL; |
| 71 | |
| 72 | #ifndef RUNTIME_LIBS |
| 73 | #define p_mysql_affected_rows mysql_affected_rows |
| 74 | #define p_mysql_close mysql_close |
| 75 | #define p_mysql_error mysql_error |
| 76 | #define p_mysql_fetch_fields mysql_fetch_fields |
| 77 | #define p_mysql_fetch_row mysql_fetch_row |
| 78 | #define p_mysql_free_result mysql_free_result |
| 79 | #define p_mysql_init mysql_init |
| 80 | #define p_mysql_num_fields mysql_num_fields |
| 81 | #define p_mysql_num_rows mysql_num_rows |
| 82 | #define p_mysql_query mysql_query |
| 83 | #define p_mysql_real_connect mysql_real_connect |
| 84 | #define p_mysql_real_escape_string mysql_real_escape_string |
| 85 | #define p_mysql_store_result mysql_store_result |
| 86 | #else |
| 87 | /* RUNTIME_LIBS */ |
| 88 | static int mysql_load_dll(void); |
| 89 | |
| 90 | typedef my_ulonglong(STDCALL *f_mysql_affected_rows)(MYSQL*); |
| 91 | typedef void (STDCALL *f_mysql_close)(MYSQL*); |
| 92 | typedef const char* (STDCALL *f_mysql_error)(MYSQL*); |
| 93 | typedef MYSQL_FIELD* (STDCALL *f_mysql_fetch_fields)(MYSQL_RES*); |
| 94 | typedef MYSQL_ROW(STDCALL *f_mysql_fetch_row)(MYSQL_RES*); |
| 95 | typedef my_bool(STDCALL *f_mysql_free_result)(MYSQL_RES*); |
| 96 | typedef MYSQL* (STDCALL *f_mysql_init)(MYSQL*); |
| 97 | typedef unsigned int (STDCALL *f_mysql_num_fields)(MYSQL_RES*); |
| 98 | typedef my_ulonglong(STDCALL *f_mysql_num_rows)(MYSQL_RES*); |
| 99 | typedef int (STDCALL *f_mysql_query)(MYSQL*, const char*); |
| 100 | typedef MYSQL* (STDCALL *f_mysql_real_connect)(MYSQL*, const char*, const char*, const char*, const char*, unsigned int, const char*, unsigned long); |
| 101 | typedef unsigned long (STDCALL *f_mysql_real_escape_string)(MYSQL*, char*, const char*, unsigned long); |
| 102 | typedef MYSQL_RES* (STDCALL *f_mysql_store_result)(MYSQL*); |
| 103 | |
| 104 | static f_mysql_affected_rows p_mysql_affected_rows; |
| 105 | static f_mysql_close p_mysql_close; |
| 106 | static f_mysql_error p_mysql_error; |
| 107 | static f_mysql_fetch_fields p_mysql_fetch_fields; |
| 108 | static f_mysql_fetch_row p_mysql_fetch_row; |
| 109 | static f_mysql_free_result p_mysql_free_result; |
| 110 | static f_mysql_init p_mysql_init; |
| 111 | static f_mysql_num_fields p_mysql_num_fields; |
| 112 | static f_mysql_num_rows p_mysql_num_rows; |
| 113 | static f_mysql_query p_mysql_query; |
| 114 | static f_mysql_real_connect p_mysql_real_connect; |
| 115 | static f_mysql_real_escape_string p_mysql_real_escape_string; |
| 116 | static f_mysql_store_result p_mysql_store_result; |
| 117 | |
| 118 | #include "compat/runtime_libs.h" /* defines OpenLibrary(), GetFunction(), CloseLibrary() & MYSQL_LIB */ |
| 119 | |
| 120 | static void * handle = NULL; |
| 121 | |
| 122 | static int mysql_load_dll(void) |
| 123 | { |
| 124 | if ((handle = OpenLibrary(MYSQL_LIB)) == NULL) return -1; |
| 125 | |
| 126 | if (((p_mysql_affected_rows = (f_mysql_affected_rows)GetFunction(handle, "mysql_affected_rows")) == NULL) || |
| 127 | ((p_mysql_close = (f_mysql_close)GetFunction(handle, "mysql_close")) == NULL) || |
| 128 | ((p_mysql_error = (f_mysql_error)GetFunction(handle, "mysql_error")) == NULL) || |
| 129 | ((p_mysql_fetch_fields = (f_mysql_fetch_fields)GetFunction(handle, "mysql_fetch_fields")) == NULL) || |
| 130 | ((p_mysql_fetch_row = (f_mysql_fetch_row)GetFunction(handle, "mysql_fetch_row")) == NULL) || |
| 131 | ((p_mysql_free_result = (f_mysql_free_result)GetFunction(handle, "mysql_free_result")) == NULL) || |
| 132 | ((p_mysql_init = (f_mysql_init)GetFunction(handle, "mysql_init")) == NULL) || |
| 133 | ((p_mysql_num_fields = (f_mysql_num_fields)GetFunction(handle, "mysql_num_fields")) == NULL) || |
| 134 | ((p_mysql_num_rows = (f_mysql_num_rows)GetFunction(handle, "mysql_num_rows")) == NULL) || |
| 135 | ((p_mysql_query = (f_mysql_query)GetFunction(handle, "mysql_query")) == NULL) || |
| 136 | ((p_mysql_real_connect = (f_mysql_real_connect)GetFunction(handle, "mysql_real_connect")) == NULL) || |
| 137 | ((p_mysql_real_escape_string = (f_mysql_real_escape_string)GetFunction(handle, "mysql_real_escape_string")) == NULL) || |
| 138 | ((p_mysql_store_result = (f_mysql_store_result)GetFunction(handle, "mysql_store_result")) == NULL)) |
| 139 | { |
| 140 | CloseLibrary(handle); |
| 141 | handle = NULL; |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | #endif /* RUNTIME_LIBS */ |
| 148 | |
| 149 | static int sql_mysql_init(const char *host, const char *port, const char *socket, const char *name, const char *user, const char *pass) |
| 150 | { |
| 151 | if (name == NULL || user == NULL) { |
| 152 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL parameter"); |
| 153 | return -1; |
| 154 | } |
| 155 | #ifdef RUNTIME_LIBS |
| 156 | if (mysql_load_dll()) { |
| 157 | eventlog(eventlog_level_error, __FUNCTION__, "error loading library file \"{}\"", MYSQL_LIB); |
| 158 | return -1; |
| 159 | } |
| 160 | #endif |
| 161 | if ((mysql = p_mysql_init(NULL)) == NULL) { |
| 162 | eventlog(eventlog_level_error, __FUNCTION__, "got error from mysql_init"); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | #if MYSQL_VERSION_ID >= 50003 |
| 167 | #if MYSQL_VERSION_ID < 50013 |
| 168 | eventlog(eventlog_level_warn, __FUNCTION__, "Your mySQL client lib version does not support reconnecting after a timeout."); |
| 169 | eventlog(eventlog_level_warn, __FUNCTION__, "If this causes you any trouble you are advices to upgrade"); |
| 170 | eventlog(eventlog_level_warn, __FUNCTION__, "your mySQL client libs to at least mySQL 5.0.13 to resolve this problem."); |
| 171 | // we might try a dirty hack like the following, but I'm not sure if it will work |
| 172 | // mysql->reconnect = 1; |
| 173 | #endif |
| 174 | #if MYSQL_VERSION_ID >= 50019 |
| 175 | my_bool my_true = true; |
| 176 | if (mysql_options(mysql, MYSQL_OPT_RECONNECT, &my_true)){ |
| 177 | eventlog(eventlog_level_warn, __FUNCTION__, "Failed to turn on MYSQL_OPT_RECONNECT."); |
| 178 | } |
| 179 | else{ |
| 180 | eventlog(eventlog_level_info, __FUNCTION__, "Successfully turned on MYSQL_OPT_RECONNECT."); |
| 181 | } |
| 182 | #endif |
| 183 | #endif |
| 184 | |
| 185 | if (p_mysql_real_connect(mysql, host, user, pass, name, port ? atoi(port) : 0, socket, CLIENT_FOUND_ROWS) == NULL) { |
| 186 | eventlog(eventlog_level_error, __FUNCTION__, "error connecting to database (db said: '{}')", p_mysql_error(mysql)); |
| 187 | p_mysql_close(mysql); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | #if MYSQL_VERSION_ID >= 50013 |
| 192 | #if MYSQL_VERSION_ID < 50019 |
| 193 | my_bool my_true = true; |
| 194 | if (mysql_options(mysql, MYSQL_OPT_RECONNECT, &my_true)){ |
| 195 | eventlog(eventlog_level_warn, __FUNCTION__, "Failed to turn on MYSQL_OPT_RECONNECT."); |
| 196 | } |
| 197 | else{ |
| 198 | eventlog(eventlog_level_info, __FUNCTION__, "Successfully turned on MYSQL_OPT_RECONNECT."); |
| 199 | } |
| 200 | #endif |
| 201 | #endif |
| 202 | |
| 203 | /* allows identifers (specificly column names) to be quoted using double quotes (") in addition to ticks (`) */ |
| 204 | sql_mysql_query("SET sql_mode='ANSI_QUOTES'"); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int sql_mysql_close(void) |
| 210 | { |
| 211 | if (mysql) { |
| 212 | p_mysql_close(mysql); |
| 213 | mysql = NULL; |
| 214 | } |
| 215 | #ifdef RUNTIME_LIBS |
| 216 | if (handle) { |
| 217 | CloseLibrary(handle); |
| 218 | handle = NULL; |
| 219 | } |
| 220 | #endif |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static t_sql_res * sql_mysql_query_res(const char * query) |
| 225 | { |
| 226 | t_sql_res *res; |
| 227 | |
| 228 | if (mysql == NULL) { |
| 229 | eventlog(eventlog_level_error, __FUNCTION__, "mysql driver not initilized"); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | if (query == NULL) { |
| 234 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL query"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | if (p_mysql_query(mysql, query)) { |
| 239 | // eventlog(eventlog_level_debug, __FUNCTION__, "got error from query ({})", query); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | res = p_mysql_store_result(mysql); |
| 244 | if (res == NULL) { |
| 245 | eventlog(eventlog_level_error, __FUNCTION__, "got error from store result"); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | return res; |
| 250 | } |
| 251 | |
| 252 | static int sql_mysql_query(const char * query) |
| 253 | { |
| 254 | if (mysql == NULL) { |
| 255 | eventlog(eventlog_level_error, __FUNCTION__, "mysql driver not initilized"); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | if (query == NULL) { |
| 260 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL query"); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | return p_mysql_query(mysql, query) == 0 ? 0 : -1; |
| 265 | } |
| 266 | |
| 267 | static t_sql_row * sql_mysql_fetch_row(t_sql_res *result) |
| 268 | { |
| 269 | if (result == NULL) { |
| 270 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | return p_mysql_fetch_row((MYSQL_RES *)result); |
| 275 | } |
| 276 | |
| 277 | static void sql_mysql_free_result(t_sql_res *result) |
| 278 | { |
| 279 | if (result == NULL) { |
| 280 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | p_mysql_free_result((MYSQL_RES *)result); |
| 285 | } |
| 286 | |
| 287 | static unsigned int sql_mysql_num_rows(t_sql_res *result) |
| 288 | { |
| 289 | if (result == NULL) { |
| 290 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | return p_mysql_num_rows((MYSQL_RES *)result); |
| 295 | } |
| 296 | |
| 297 | static unsigned int sql_mysql_num_fields(t_sql_res *result) |
| 298 | { |
| 299 | if (result == NULL) { |
| 300 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | return p_mysql_num_fields((MYSQL_RES *)result); |
| 305 | } |
| 306 | |
| 307 | static unsigned int sql_mysql_affected_rows(void) |
| 308 | { |
| 309 | return p_mysql_affected_rows(mysql); |
| 310 | } |
| 311 | |
| 312 | static t_sql_field * sql_mysql_fetch_fields(t_sql_res *result) |
| 313 | { |
| 314 | MYSQL_FIELD *fields; |
| 315 | unsigned fieldno, i; |
| 316 | t_sql_field *rfields; |
| 317 | |
| 318 | if (result == NULL) { |
| 319 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL result"); |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | fieldno = p_mysql_num_fields((MYSQL_RES *)result); |
| 324 | fields = p_mysql_fetch_fields((MYSQL_RES *)result); |
| 325 | |
| 326 | rfields = (t_sql_field *)xmalloc(sizeof(t_sql_field)* (fieldno + 1)); |
| 327 | for (i = 0; i < fieldno; i++) |
| 328 | rfields[i] = fields[i].name; |
| 329 | rfields[i] = NULL; |
| 330 | |
| 331 | return rfields; |
| 332 | } |
| 333 | |
| 334 | static int sql_mysql_free_fields(t_sql_field *fields) |
| 335 | { |
| 336 | if (fields == NULL) { |
| 337 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL fields"); |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | xfree((void*)fields); |
| 342 | return 0; /* mysql_free_result() should free the rest properly */ |
| 343 | } |
| 344 | |
| 345 | static void sql_mysql_escape_string(char *escape, const char *from, int len) |
| 346 | { |
| 347 | if (mysql == NULL) { |
| 348 | eventlog(eventlog_level_error, __FUNCTION__, "mysql driver not initilized"); |
| 349 | return; |
| 350 | } |
| 351 | p_mysql_real_escape_string(mysql, escape, from, len); |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
| 356 | } |
| 357 | |
| 358 | #endif /* WITH_SQL_MYSQL */ |