9
3

database.h 4.3 KB

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