9
3

DatabaseNew.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator 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. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. #include "Log.h"
  21. #include "DatabaseNew.h"
  22. //increase this if large queries are being run frequently to make less calls to malloc()
  23. #define QUERY_INITIAL_SIZE 512
  24. #if defined WORLD
  25. #define DB_INI "world_db.ini"
  26. #elif defined LOGIN
  27. #define DB_INI "login_db.ini"
  28. #elif defined PARSER
  29. #define DB_INI "parser_db.ini"
  30. #endif
  31. DatabaseNew::DatabaseNew() {
  32. mysql_init(&mysql);
  33. MMysql.SetName("DatabaseNew::mysql");
  34. }
  35. DatabaseNew::~DatabaseNew() {
  36. mysql_close(&mysql);
  37. #if MYSQL_VERSION_ID >= 50003
  38. mysql_library_end();
  39. #else
  40. mysql_server_end();
  41. #endif
  42. }
  43. bool DatabaseNew::Connect() {
  44. char line[256], *key, *val;
  45. char host[256], user[64], password[64], database[64];
  46. bool found_section = false;
  47. FILE *f;
  48. if ((f = fopen(DB_INI, "r")) == NULL) {
  49. LogWrite(DATABASE__ERROR, 0, "Database", "Unable to read %s\n", DB_INI);
  50. return false;
  51. }
  52. memset(host, 0, sizeof(host));
  53. memset(user, 0, sizeof(user));
  54. memset(password, 0, sizeof(password));
  55. memset(database, 0, sizeof(database));
  56. while (fgets(line, sizeof(line), f) != NULL) {
  57. if (line[0] == '#' || line[0] == '\n' || line[0] == '\r')
  58. continue;
  59. if (!found_section) {
  60. if (strncasecmp(line, "[Database]", 10) == 0)
  61. found_section = true;
  62. }
  63. else {
  64. if ((key = strtok(line, "=")) != NULL) {
  65. if ((val = strtok(NULL, "\r\n")) != NULL) {
  66. if (strncasecmp(line, "host", 4) == 0)
  67. strncpy(host, val, sizeof(host) - 1);
  68. else if (strncasecmp(line, "user", 4) == 0)
  69. strncpy(user, val, sizeof(user) - 1);
  70. else if (strncasecmp(line, "password", 8) == 0)
  71. strncpy(password, val, sizeof(password) - 1);
  72. else if (strncasecmp(line, "database", 8) == 0)
  73. strncpy(database, val, sizeof(database) - 1);
  74. }
  75. }
  76. }
  77. }
  78. fclose(f);
  79. if (host[0] == '\0') {
  80. LogWrite(DATABASE__ERROR, 0, "Database", "Unknown 'host' in '%s'\n", DB_INI);
  81. return false;
  82. }
  83. if (user[0] == '\0') {
  84. LogWrite(DATABASE__ERROR, 0, "Database", "Unknown 'user' in '%s'\n", DB_INI);
  85. return false;
  86. }
  87. if (password[0] == '\0') {
  88. LogWrite(DATABASE__ERROR, 0, "Database", "Unknown 'password' in '%s'\n", DB_INI);
  89. return false;
  90. }
  91. if (database[0] == '\0') {
  92. LogWrite(DATABASE__ERROR, 0, "Database", "Unknown 'database' in '%s'\n", DB_INI);
  93. return false;
  94. }
  95. return Connect(host, user, password, database);
  96. }
  97. bool DatabaseNew::Connect(const char *host, const char *user, const char *password, const char *database) {
  98. return Connect(host, user, password, database, 3306);
  99. }
  100. bool DatabaseNew::Connect(const char *host, const char *user, const char *password, const char *database, unsigned int port) {
  101. if (mysql_real_connect(&mysql, host, user, password, database, port, NULL, 0) == NULL) {
  102. LogWrite(DATABASE__ERROR, 0, "Database", "Unable to connect to MySQL server at %s:%u: %s\n", host, port, mysql_error(&mysql));
  103. return false;
  104. }
  105. return true;
  106. }
  107. bool DatabaseNew::Query(const char *query, ...) {
  108. char *buf;
  109. size_t size = QUERY_INITIAL_SIZE;
  110. int num_chars;
  111. va_list args;
  112. bool ret = true;
  113. MMysql.writelock(__FUNCTION__, __LINE__);
  114. while (true) {
  115. if ((buf = (char *)malloc(size)) == NULL) {
  116. LogWrite(DATABASE__ERROR, 0, "Database", "Out of memory trying to allocate database query of %u bytes\n", size);
  117. MMysql.releasewritelock(__FUNCTION__, __LINE__);
  118. return false;
  119. }
  120. va_start(args, query);
  121. num_chars = vsnprintf(buf, size, query, args);
  122. va_end(args);
  123. if (num_chars > -1 && (size_t)num_chars < size)
  124. break;
  125. if (num_chars > -1)
  126. size = num_chars + 1;
  127. else
  128. size *= 2;
  129. free(buf);
  130. }
  131. if (mysql_real_query(&mysql, buf, (unsigned long)num_chars) != 0) {
  132. if (!IsIgnoredErrno(mysql_errno(&mysql))) {
  133. LogWrite(DATABASE__ERROR, 0, "Database", "Error %i running MySQL query: %s\n%s\n", mysql_errno(&mysql), mysql_error(&mysql), buf);
  134. ret = false;
  135. }
  136. }
  137. free(buf);
  138. MMysql.releasewritelock(__FUNCTION__, __LINE__);
  139. return ret;
  140. }
  141. bool DatabaseNew::Select(DatabaseResult *result, const char *query, ...) {
  142. char *buf;
  143. size_t size = QUERY_INITIAL_SIZE;
  144. int num_chars;
  145. va_list args;
  146. MYSQL_RES *res;
  147. bool ret = true;
  148. MMysql.writelock(__FUNCTION__, __LINE__);
  149. while (true) {
  150. if ((buf = (char *)malloc(size)) == NULL) {
  151. LogWrite(DATABASE__ERROR, 0, "Database", "Out of memory trying to allocate database query of %u bytes\n", size);
  152. MMysql.releasewritelock(__FUNCTION__, __LINE__);
  153. return false;
  154. }
  155. va_start(args, query);
  156. num_chars = vsnprintf(buf, size, query, args);
  157. va_end(args);
  158. if (num_chars > -1 && (size_t)num_chars < size)
  159. break;
  160. if (num_chars > -1)
  161. size = num_chars + 1;
  162. else
  163. size *= 2;
  164. free(buf);
  165. }
  166. if (mysql_real_query(&mysql, buf, (unsigned long)num_chars) != 0) {
  167. if (!IsIgnoredErrno(mysql_errno(&mysql))) {
  168. LogWrite(DATABASE__ERROR, 0, "Database", "Error %i running MySQL query: %s\n%s\n", mysql_errno(&mysql), mysql_error(&mysql), buf);
  169. ret = false;
  170. }
  171. }
  172. if (ret && !IsIgnoredErrno(mysql_errno(&mysql))) {
  173. res = mysql_store_result(&mysql);
  174. if (res != NULL)
  175. ret = result->StoreResult(res);
  176. else {
  177. LogWrite(DATABASE__ERROR, 0, "Database", "Error storing MySql query result (%d): %s\n%s", mysql_errno(&mysql), mysql_error(&mysql), buf);
  178. ret = false;
  179. }
  180. }
  181. free(buf);
  182. MMysql.releasewritelock(__FUNCTION__, __LINE__);
  183. return ret;
  184. }
  185. int32 DatabaseNew::LastInsertID()
  186. {
  187. return (int32)mysql_insert_id(&mysql);
  188. }
  189. long DatabaseNew::AffectedRows()
  190. {
  191. return mysql_affected_rows(&mysql);
  192. }
  193. char * DatabaseNew::Escape(const char *str, size_t len) {
  194. char *buf = (char *)malloc(len * 2 + 1);
  195. if (buf == NULL) {
  196. LogWrite(DATABASE__ERROR, 0, "Database", "Out of memory trying to allocate %u bytes in %s:%u\n", len * 2 + 1, __FUNCTION__, __LINE__);
  197. return NULL;
  198. }
  199. mysql_real_escape_string(&mysql, buf, str, len);
  200. return buf;
  201. }
  202. char * DatabaseNew::Escape(const char *str) {
  203. return Escape(str, strlen(str));
  204. }
  205. string DatabaseNew::EscapeStr(const char *str, size_t len) {
  206. char *buf = (char *)malloc(len * 2 + 1);
  207. string ret;
  208. if (buf == NULL) {
  209. LogWrite(DATABASE__ERROR, 0, "Database", "Out of memory trying to allocate %u bytes in %s:%u\n", len * 2 + 1, __FUNCTION__, __LINE__);
  210. return NULL;
  211. }
  212. mysql_real_escape_string(&mysql, buf, str, len);
  213. ret.append(buf);
  214. free(buf);
  215. return ret;
  216. }
  217. string DatabaseNew::EscapeStr(const char *str) {
  218. return EscapeStr(str, strlen(str));
  219. }
  220. string DatabaseNew::EscapeStr(string str) {
  221. return EscapeStr(str.c_str(), str.length());
  222. }
  223. bool DatabaseNew::QueriesFromFile(const char * file) {
  224. bool success = true;
  225. long size;
  226. char *buf;
  227. int ret;
  228. MYSQL_RES *res;
  229. FILE *f;
  230. f = fopen(file, "rb");
  231. if (f == NULL) {
  232. LogWrite(DATABASE__ERROR, 0, "Database", "Unable to open '%s' for reading: %s", file, strerror(errno));
  233. return false;
  234. }
  235. fseek(f, 0, SEEK_END);
  236. size = ftell(f);
  237. fseek(f, 0, SEEK_SET);
  238. buf = (char *)malloc(size + 1);
  239. if (buf == NULL) {
  240. fclose(f);
  241. LogWrite(DATABASE__ERROR, 0, "Database", "Out of memory trying to allocate %u bytes in %s:%u\n", size + 1, __FUNCTION__, __LINE__);
  242. return false;
  243. }
  244. if (fread(buf, sizeof(*buf), size, f) != (size_t)size) {
  245. LogWrite(DATABASE__ERROR, 0, "Database", "Failed to read from '%s': %s", file, strerror(errno));
  246. fclose(f);
  247. free(buf);
  248. return false;
  249. }
  250. buf[size] = '\0';
  251. fclose(f);
  252. mysql_set_server_option(&mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON);
  253. ret = mysql_real_query(&mysql, buf, size);
  254. free(buf);
  255. if (ret != 0) {
  256. LogWrite(DATABASE__ERROR, 0, "Database", "Error running MySQL queries from file '%s' (%d): %s", file, mysql_errno(&mysql), mysql_error(&mysql));
  257. success = false;
  258. }
  259. else {
  260. //all results must be processed
  261. do {
  262. res = mysql_store_result(&mysql);
  263. if (res != NULL)
  264. mysql_free_result(res);
  265. ret = mysql_next_result(&mysql);
  266. if (ret > 0) {
  267. LogWrite(DATABASE__ERROR, 0, "Database", "Error running MySQL queries from file '%s' (%d): %s", file, mysql_errno(&mysql), mysql_error(&mysql));
  268. success = false;
  269. }
  270. } while (ret == 0);
  271. }
  272. mysql_set_server_option(&mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
  273. return success;
  274. }
  275. void DatabaseNew::SetIgnoredErrno(unsigned int db_errno) {
  276. vector<unsigned int>::iterator itr;
  277. for (itr = ignored_errnos.begin(); itr != ignored_errnos.end(); itr++) {
  278. if ((*itr) == db_errno)
  279. return;
  280. }
  281. ignored_errnos.push_back(db_errno);
  282. }
  283. void DatabaseNew::RemoveIgnoredErrno(unsigned int db_errno) {
  284. vector<unsigned int>::iterator itr;
  285. for (itr = ignored_errnos.begin(); itr != ignored_errnos.end(); itr++) {
  286. if ((*itr) == db_errno) {
  287. ignored_errnos.erase(itr);
  288. break;
  289. }
  290. }
  291. }
  292. bool DatabaseNew::IsIgnoredErrno(unsigned int db_errno) {
  293. vector<unsigned int>::iterator itr;
  294. for (itr = ignored_errnos.begin(); itr != ignored_errnos.end(); itr++) {
  295. if ((*itr) == db_errno)
  296. return true;
  297. }
  298. return false;
  299. }