database.h 4.4 KB

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