database.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Query;
  34. class Database : public DBcore
  35. {
  36. public:
  37. Database();
  38. ~Database();
  39. bool Init(bool silentLoad=false);
  40. bool LoadVariables();
  41. void HandleMysqlError(int32 errnum);
  42. map<string, uint16> GetOpcodes(int16 version);
  43. map<int16, int16> GetVersions();
  44. #ifdef WORLD
  45. void AddAsyncQuery(Query* query);
  46. void RunAsyncQueries(int32 queryid);
  47. Database* FindFreeInstance();
  48. void RemoveActiveQuery(Query* query);
  49. void AddActiveQuery(Query* query);
  50. bool IsActiveQuery(int32 id, Query* skip=0);
  51. #endif
  52. protected:
  53. private:
  54. void InitVars();
  55. #ifdef WORLD
  56. void PurgeDBInstances();
  57. void FreeDBInstance(Database* cur);
  58. bool continueAsync;
  59. map<int32, deque<Query*>> asyncQueries;
  60. map<int32, Mutex*> asyncQueriesMutex;
  61. map<Database*, bool> dbInstances;
  62. vector<Query*> activeQuerySessions;
  63. Mutex DBAsyncMutex;
  64. Mutex DBInstanceMutex;
  65. Mutex DBQueryMutex;
  66. #endif
  67. };
  68. typedef struct {
  69. int32 queryid;
  70. }DBStruct;
  71. class Query{
  72. public:
  73. Query() {
  74. result = 0;
  75. affected_rows = 0;
  76. last_insert_id = 0;
  77. errnum = 0;
  78. row = 0;
  79. retry = true;
  80. escaped_name = 0;
  81. escaped_pass = 0;
  82. escaped_data1 = 0;
  83. multiple_results = 0;
  84. memset(errbuf, 0, sizeof(errbuf));
  85. queryID = 0;
  86. }
  87. Query(Query* queryPtr, int32 in_id) {
  88. result = 0;
  89. affected_rows = 0;
  90. last_insert_id = 0;
  91. errnum = 0;
  92. row = 0;
  93. retry = true;
  94. escaped_name = 0;
  95. escaped_pass = 0;
  96. escaped_data1 = 0;
  97. multiple_results = 0;
  98. memset(errbuf, 0, sizeof(errbuf));
  99. query = string(queryPtr->GetQuery());
  100. in_type = queryPtr->GetQueryType();
  101. queryID = in_id;
  102. }
  103. ~Query(){
  104. if(result)
  105. mysql_free_result(result);
  106. result = 0;
  107. safe_delete(affected_rows);
  108. safe_delete(last_insert_id);
  109. safe_delete_array(escaped_name);
  110. safe_delete_array(escaped_pass);
  111. safe_delete_array(escaped_data1);
  112. if(multiple_results){
  113. vector<MYSQL_RES*>::iterator itr;
  114. for(itr = multiple_results->begin(); itr != multiple_results->end(); itr++){
  115. mysql_free_result(*itr);
  116. }
  117. safe_delete(multiple_results);
  118. }
  119. }
  120. int32 GetLastInsertedID() { return *last_insert_id; }
  121. int32 GetAffectedRows() { return *affected_rows; }
  122. MYSQL_RES* GetResult() { return result; }
  123. MYSQL_RES* RunQuery2(string in_query, QUERY_TYPE type);
  124. char* GetError() { return errbuf; }
  125. int32 GetErrorNumber(){ return errnum; }
  126. const char* GetQuery() { return query.c_str(); }
  127. char* GetField(int8 field_num) {
  128. if(!row && result)
  129. *row = mysql_fetch_row(result);
  130. if(row && result && field_num < mysql_num_fields(result))
  131. return *row[field_num];
  132. else
  133. return NULL;
  134. }
  135. void NextRow(){
  136. if(result)
  137. *row = mysql_fetch_row(result);
  138. }
  139. void AddQueryAsync(int32 queryID, Database* db, QUERY_TYPE type, const char* format, ...);
  140. void RunQueryAsync(Database* db);
  141. MYSQL_RES* RunQuery2(QUERY_TYPE type, const char* format, ...);
  142. QUERY_TYPE GetQueryType() {
  143. return in_type;
  144. }
  145. int32 GetQueryID() { return queryID; }
  146. char* escaped_name;
  147. char* escaped_pass;
  148. char* escaped_data1;
  149. private:
  150. string query;
  151. char errbuf[MYSQL_ERRMSG_SIZE];
  152. MYSQL_RES *result;
  153. vector<MYSQL_RES*>* multiple_results;
  154. int32* affected_rows;
  155. int32* last_insert_id;
  156. int32 errnum;
  157. QUERY_TYPE in_type;
  158. bool retry;
  159. MYSQL_ROW* row;
  160. MYSQL mysql;
  161. int32 queryID;
  162. };
  163. #endif