EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dbcore.h
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------
3  * LICENSE:
4  * ------------------------------------------------------------------------------------
5  * This file is part of EVEmu: EVE Online Server Emulator
6  * Copyright 2006 - 2021 The EVEmu Team
7  * For the latest information visit https://evemu.dev
8  * ------------------------------------------------------------------------------------
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the Free Software
11  * Foundation; either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  * http://www.gnu.org/copyleft/lesser.txt.
22  * ------------------------------------------------------------------------------------
23  * Author: Zhur
24  */
25 
26 #ifndef __DATABASE__DBCORE_H__INCL__
27 #define __DATABASE__DBCORE_H__INCL__
28 
29 //this whole file could be interface-ized to support a different database
30 //if you can get over the SQL incompatibilities and mysql auto increment problems.
31 
32 #include "memory/SafeMem.h"
33 #include "utils/Singleton.h"
34 #include "database/dbtype.h"
35 #include "threading/Mutex.h"
36 
37 class DBcore;
38 
39 class DBerror
40 {
41 public:
42  DBerror();
43  ~DBerror();
44 
45  uint32 GetErrNo() const { return mErrNo; }
46  const char* GetError() const { return mErrStr.c_str(); }
47 
48  const char* c_str() const { return GetError(); }
49 
50 protected:
51  //for DBcore:
52  friend class DBcore;
53  void SetError( uint err, const char* str );
54  void ClearError();
55 
56  std::string mErrStr;
57  uint mErrNo;
58 };
59 
60 
61 class DBResultRow;
63 {
64 public:
65  DBQueryResult();
67 
68  /* error during the query, if RunQuery returned false. */
70 
71  bool GetRow( DBResultRow& into );
72  size_t GetRowCount() { return ((mResult == nullptr) ? 0 : (size_t)mResult->row_count); }
73  // this should be called between multiple calls using same DBQueryResult object
74  void Reset();
75 
76  uint32 ColumnCount() const { return mColumnCount; }
77  const char* ColumnName( uint32 index ) const;
78  DBTYPE ColumnType( uint32 index ) const;
79 
80  bool IsUnsigned( uint32 index ) const;
81  bool IsBinary( uint32 index ) const;
82 
83 protected:
84  //for DBcore:
85  friend class DBcore;
86  void SetResult( MYSQL_RES* res, uint32 colCount );
87 
89  MYSQL_RES* mResult;
90  MYSQL_FIELD** mFields;
91 
94 };
95 
97 {
98 public:
99  DBResultRow();
100  ~DBResultRow() { /* do nothing here */ }
101 
102  bool IsNull( uint32 index ) const { return ( NULL == GetText( index ) ); }
103 
104  const char* GetText( uint32 index ) const { return mRow[ index ]; }
105  int32 GetInt( uint32 index ) const;
106  bool GetBool( uint32 index ) const;
107  uint32 GetUInt( uint32 index ) const;
108  int64 GetInt64( uint32 index ) const;
109  float GetFloat( uint32 index ) const;
110  double GetDouble( uint32 index ) const;
111 
112  //proxy methods up to our query result:
113  uint32 ColumnCount() const { return mResult->ColumnCount(); }
114  const char* ColumnName( uint32 index ) const { return mResult->ColumnName( index ); }
115  DBTYPE ColumnType( uint32 index ) const { return mResult->ColumnType( index ); }
116  uint32 ColumnLength( uint32 index ) const;
117 
118  bool IsUnsigned( uint32 index ) const { return mResult->IsUnsigned( index ); }
119  bool IsBinary( uint32 index ) const { return mResult->IsBinary( index ); }
120 
121 protected:
122  //for DBQueryResult
123  friend class DBQueryResult;
124  void SetData( DBQueryResult* res, MYSQL_ROW& row, const ulong* lengths );
125 
126  MYSQL_ROW mRow;
127  const ulong* mLengths;
128 
130 };
131 
132 class DBcore
133 : public Singleton<DBcore>
134 {
135 public:
137 
138  DBcore();
139  ~DBcore() { /* do nothing here */ }
140 
141  void Close();
142  void Initialize(std::string host, std::string user, std::string password, std::string database, bool compress=false, bool SSL=false,
143  int16 port=3306, bool socket=false, bool reconnect=false, bool profile=false);
144 
145  //new shorter syntax:
146  //query which returns a result (error is stored in the result if it occurs)
147  // NOTE: result is cleared before populating with most recent data for multiple statements using same DBQueryResult object.
148  bool RunQuery(DBQueryResult &into, const char *query_fmt, ...);
149  //query which returns no information except error status
150  // NOTE: result is cleared before populating with most recent data for multiple statements using same DBQueryResult object.
151  bool RunQuery(DBerror &err, const char *query_fmt, ...);
152  //query which returns affected rows:
153  // NOTE: result is cleared before populating with most recent data for multiple statements using same DBQueryResult object.
154  bool RunQuery(DBerror &err, uint32 &affected_rows, const char *query_fmt, ...);
155  //query which returns last insert ID:
156  // NOTE: result is cleared before populating with most recent data for multiple statements using same DBQueryResult object.
157  bool RunQueryLID(DBerror& err, uint32& last_insert_id, const char* query_fmt, ...);
158 
159  int32 DoEscapeString(char* tobuf, const char* frombuf, int32 fromlen);
160  void DoEscapeString(std::string &to, const std::string &from);
161  static bool IsSafeString(const char *str);
162  static bool IsSafeString(std::string &s);
163  // check for and remove slashes before sending string to db
164  //static void ReplaceSlash(const char *str);
165  void ping();
166 
167  eStatus GetStatus() const { return pStatus; }
168 
169 protected:
170  MYSQL* getMySQL() { return mysql; }
171 
172  void Connect(uint* errnum = 0, char* errbuf = 0);
173 
174  bool Reconnect();
175  //void CallShutdown();
176 
177 private:
178  //MDatabase must be locked before these calls:
179  bool DoQuery_locked(DBerror &err, const char *query, int querylen, bool retry = true);
180 
181  MYSQL* mysql;
184 
185  bool pCompress;
186  bool pProfile;
188  bool pSocket;
189  bool pSSL;
190 
192 
193  std::string pHost;
194  std::string pUser;
195  std::string pPassword;
196  std::string pDatabase;
197 };
198 
199 #define sDatabase \
200 ( DBcore::get() )
201 
202 #endif /* !__DATABASE__DBCORE_H__INCL__ */
static const DBTYPE MYSQL_DBTYPE_TABLE_UNSIGNED[]
Definition: dbcore.h:93
static const DBTYPE MYSQL_DBTYPE_TABLE_SIGNED[]
Definition: dbcore.h:92
bool RunQueryLID(DBerror &err, uint32 &last_insert_id, const char *query_fmt,...)
Definition: dbcore.cpp:288
uint32 ColumnLength(uint32 index) const
Definition: dbcore.cpp:624
bool IsUnsigned(uint32 index) const
Definition: dbcore.cpp:526
const char * GetText(uint32 index) const
Definition: dbcore.h:104
void SetData(DBQueryResult *res, MYSQL_ROW &row, const ulong *lengths)
Definition: dbcore.cpp:617
int16 pPort
Definition: dbcore.h:191
float GetFloat(uint32 index) const
Definition: dbcore.cpp:682
eStatus GetStatus() const
Definition: dbcore.h:167
int32 GetInt(uint32 index) const
Definition: dbcore.cpp:635
void ClearError()
Definition: dbcore.cpp:426
uint32 GetUInt(uint32 index) const
Definition: dbcore.cpp:658
double GetDouble(uint32 index) const
Definition: dbcore.cpp:693
int32 DoEscapeString(char *tobuf, const char *frombuf, int32 fromlen)
Definition: dbcore.cpp:358
std::string pHost
Definition: dbcore.h:193
const char * GetError() const
Definition: dbcore.h:46
bool RunQuery(DBQueryResult &into, const char *query_fmt,...)
Definition: dbcore.cpp:222
MYSQL * getMySQL()
Definition: dbcore.h:170
Common wrapper for platform-specific mutexes.
Definition: Mutex.h:36
std::string pUser
Definition: dbcore.h:194
bool DoQuery_locked(DBerror &err, const char *query, int querylen, bool retry=true)
Definition: dbcore.cpp:308
MYSQL_ROW mRow
Definition: dbcore.h:126
signed __int32 int32
Definition: eve-compat.h:49
void SetError(uint err, const char *str)
Definition: dbcore.cpp:420
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
Mutex MDatabase
Definition: dbcore.h:182
bool GetBool(uint32 index) const
Definition: dbcore.cpp:647
std::string pPassword
Definition: dbcore.h:195
std::string mErrStr
Definition: dbcore.h:56
DBerror()
Definition: dbcore.cpp:410
const char * c_str() const
Definition: dbcore.h:48
uint32 ColumnCount() const
Definition: dbcore.h:76
DBQueryResult * mResult
Definition: dbcore.h:129
bool pCompress
Definition: dbcore.h:185
DBTYPE
Definition: dbtype.h:67
bool IsUnsigned(uint32 index) const
Definition: dbcore.h:118
void SetResult(MYSQL_RES *res, uint32 colCount)
Definition: dbcore.cpp:538
~DBerror()
Definition: dbcore.cpp:415
const char * ColumnName(uint32 index) const
Definition: dbcore.h:114
bool IsBinary(uint32 index) const
Definition: dbcore.cpp:531
uint32 mColumnCount
Definition: dbcore.h:88
bool pReconnect
Definition: dbcore.h:187
bool IsNull(uint32 index) const
Definition: dbcore.h:102
std::string pDatabase
Definition: dbcore.h:196
~DBResultRow()
Definition: dbcore.h:100
const char * ColumnName(uint32 index) const
Definition: dbcore.cpp:569
unsigned __int32 uint32
Definition: eve-compat.h:50
Definition: dbcore.h:132
bool Reconnect()
Definition: dbcore.cpp:137
void Close()
Definition: dbcore.cpp:192
bool pSocket
Definition: dbcore.h:188
eStatus
Definition: dbcore.h:136
void ping()
Definition: dbcore.cpp:212
uint mErrNo
Definition: dbcore.h:57
MYSQL_FIELD ** mFields
Definition: dbcore.h:90
bool IsBinary(uint32 index) const
Definition: dbcore.h:119
DBerror error
Definition: dbcore.h:69
signed __int64 int64
Definition: eve-compat.h:51
static bool IsSafeString(const char *str)
Definition: dbcore.cpp:373
const ulong * mLengths
Definition: dbcore.h:127
void Initialize(std::string host, std::string user, std::string password, std::string database, bool compress=false, bool SSL=false, int16 port=3306, bool socket=false, bool reconnect=false, bool profile=false)
Definition: dbcore.cpp:154
signed __int16 int16
Definition: eve-compat.h:47
Template used for singleton classes.
Definition: Singleton.h:43
MYSQL_RES * mResult
Definition: dbcore.h:89
uint32 GetErrNo() const
Definition: dbcore.h:45
size_t GetRowCount()
Definition: dbcore.h:72
bool pSSL
Definition: dbcore.h:189
eStatus pStatus
Definition: dbcore.h:183
void Connect(uint *errnum=0, char *errbuf=0)
Definition: dbcore.cpp:60
uint32 ColumnCount() const
Definition: dbcore.h:113
bool pProfile
Definition: dbcore.h:186
~DBcore()
Definition: dbcore.h:139
int64 GetInt64(uint32 index) const
Definition: dbcore.cpp:670
MYSQL * mysql
Definition: dbcore.h:181
DBTYPE ColumnType(uint32 index) const
Definition: dbcore.cpp:580
Definition: dbcore.h:39
void Reset()
Definition: dbcore.cpp:517
DBcore()
Definition: dbcore.cpp:49
DBTYPE ColumnType(uint32 index) const
Definition: dbcore.h:115