database.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef EQ2EMU_DATABASE_H
  17. #define EQ2EMU_DATABASE_H
  18. #ifdef WIN32
  19. #include <WinSock2.h>
  20. #include <windows.h>
  21. #endif
  22. #include <mysql.h>
  23. #include "dbcore.h"
  24. #include "types.h"
  25. #include "linked_list.h"
  26. #include "EQStream.h"
  27. #include "MiscFunctions.h"
  28. #include "Mutex.h"
  29. #include <string>
  30. #include <vector>
  31. #include <map>
  32. using namespace std;
  33. class Database : public DBcore
  34. {
  35. public:
  36. Database();
  37. ~Database();
  38. bool Init();
  39. bool LoadVariables();
  40. void HandleMysqlError(int32 errnum);
  41. map<string, uint16> GetOpcodes(int16 version);
  42. map<int16, int16> GetVersions();
  43. protected:
  44. private:
  45. void InitVars();
  46. };
  47. class Query{
  48. public:
  49. Query(){
  50. result = 0;
  51. affected_rows = 0;
  52. last_insert_id = 0;
  53. errnum = 0;
  54. row = 0;
  55. retry = true;
  56. escaped_name = 0;
  57. escaped_pass = 0;
  58. escaped_data1 = 0;
  59. multiple_results = 0;
  60. memset(errbuf, 0, sizeof(errbuf));
  61. }
  62. ~Query(){
  63. if(result)
  64. mysql_free_result(result);
  65. result = 0;
  66. safe_delete(affected_rows);
  67. safe_delete(last_insert_id);
  68. safe_delete_array(escaped_name);
  69. safe_delete_array(escaped_pass);
  70. safe_delete_array(escaped_data1);
  71. if(multiple_results){
  72. vector<MYSQL_RES*>::iterator itr;
  73. for(itr = multiple_results->begin(); itr != multiple_results->end(); itr++){
  74. mysql_free_result(*itr);
  75. }
  76. safe_delete(multiple_results);
  77. }
  78. }
  79. int32 GetLastInsertedID() { return *last_insert_id; }
  80. int32 GetAffectedRows() { return *affected_rows; }
  81. MYSQL_RES* GetResult() { return result; }
  82. MYSQL_RES* RunQuery2(string in_query, QUERY_TYPE type);
  83. char* GetError() { return errbuf; }
  84. int32 GetErrorNumber(){ return errnum; }
  85. const char* GetQuery() { return query.c_str(); }
  86. char* GetField(int8 field_num) {
  87. if(!row && result)
  88. *row = mysql_fetch_row(result);
  89. if(row && result && field_num < mysql_num_fields(result))
  90. return *row[field_num];
  91. else
  92. return NULL;
  93. }
  94. void NextRow(){
  95. if(result)
  96. *row = mysql_fetch_row(result);
  97. }
  98. MYSQL_RES* RunQuery2(QUERY_TYPE type, const char* format, ...);
  99. char* escaped_name;
  100. char* escaped_pass;
  101. char* escaped_data1;
  102. private:
  103. string query;
  104. char errbuf[MYSQL_ERRMSG_SIZE];
  105. MYSQL_RES *result;
  106. vector<MYSQL_RES*>* multiple_results;
  107. int32* affected_rows;
  108. int32* last_insert_id;
  109. int32 errnum;
  110. bool retry;
  111. MYSQL_ROW* row;
  112. MYSQL mysql;
  113. };
  114. #endif