Repository navigation
Expand file tree
/
Copy pathdb_interface.h
More file actions
211 lines (170 loc) · 4.74 KB
/
Copy pathdb_interface.h
File metadata and controls
211 lines (170 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KBE_DB_INTERFACE_H
#define KBE_DB_INTERFACE_H
#include "common/common.h"
#include "common/singleton.h"
#include "helper/debug_helper.h"
#include "db_interface/entity_table.h"
#include "server/serverconfig.h"
namespace KBEngine {
namespace thread
{
class ThreadPool;
}
class DBUtil;
/*
数据库接口
*/
class DBInterface
{
public:
enum DB_OP_STATE
{
DB_OP_READ,
DB_OP_WRITE,
};
friend class DBUtil;
DBInterface(const char* name) :
db_port_(3306),
db_numConnections_(1),
lastquery_()
{
strncpy(name_, name, MAX_NAME);
int dbIndex = g_kbeSrvConfig.dbInterfaceName2dbInterfaceIndex(this->name());
KBE_ASSERT(dbIndex >= 0);
dbIndex_ = dbIndex;
};
virtual ~DBInterface()
{
};
/**
检查环境
*/
virtual bool checkEnvironment() = 0;
/**
检查错误, 对错误的内容进行纠正
如果纠正不成功返回失败
*/
virtual bool checkErrors() = 0;
/**
与某个数据库关联
*/
virtual bool attach(const char* databaseName = NULL) = 0;
virtual bool detach() = 0;
/**
获取数据库所有的表名
*/
virtual bool getTableNames( std::vector<std::string>& tableNames, const char * pattern) = 0;
/**
获取数据库某个表所有的字段名称
*/
virtual bool getTableItemNames(const char* tableName, std::vector<std::string>& itemNames) = 0;
/**
查询表
*/
virtual bool query(const char* cmd, uint32 size, bool printlog = true, MemoryStream * result = NULL) = 0;
virtual bool query(const std::string& cmd, bool printlog = true, MemoryStream * result = NULL)
{
return query(cmd.c_str(), (uint32)cmd.size(), printlog, result);
}
/**
返回这个接口的名称
*/
const char* name() const { return name_; }
/**
返回这个接口的索引
*/
uint16 dbIndex() const { return dbIndex_; }
/**
返回这个接口的描述
*/
virtual const char* c_str() = 0;
/**
获取错误
*/
virtual const char* getstrerror() = 0;
/**
获取错误编号
*/
virtual int getlasterror() = 0;
/**
创建一个entity存储表
*/
virtual EntityTable* createEntityTable(EntityTables* pEntityTables) = 0;
/**
从数据库删除entity表
*/
virtual bool dropEntityTableFromDB(const char* tableName) = 0;
/**
从数据库删除entity表字段
*/
virtual bool dropEntityTableItemFromDB(const char* tableName, const char* tableItemName) = 0;
/**
锁住接口操作
*/
virtual bool lock() = 0;
virtual bool unlock() = 0;
/**
处理异常
*/
virtual bool processException(std::exception & e) = 0;
/**
获取最后一次查询的sql语句
*/
virtual const std::string& lastquery() const{ return lastquery_; }
protected:
char name_[MAX_BUF]; // 数据库接口的名称
char db_type_[MAX_BUF]; // 数据库的类别
uint32 db_port_; // 数据库的端口
char db_ip_[MAX_IP]; // 数据库的ip地址
char db_username_[MAX_BUF]; // 数据库的用户名
char db_password_[MAX_BUF]; // 数据库的密码
char db_name_[MAX_BUF]; // 数据库名
uint16 db_numConnections_; // 数据库最大连接
std::string lastquery_; // 最后一次查询描述
uint16 dbIndex_; // 对应的数据库接口索引
};
/*
数据库操作单元
*/
class DBUtil : public Singleton<DBUtil>
{
public:
DBUtil();
~DBUtil();
static bool initialize();
static void finalise();
static bool initializeWatcher();
static bool initThread(const std::string& dbinterfaceName);
static bool finiThread(const std::string& dbinterfaceName);
static DBInterface* createInterface(const std::string& name, bool showinfo = true);
static const char* accountScriptName();
static bool initInterface(DBInterface* pdbi);
static void handleMainTick();
typedef KBEUnordered_map<std::string, thread::ThreadPool*> DBThreadPoolMap;
static thread::ThreadPool* pThreadPool(const std::string& name)
{
DBThreadPoolMap::iterator iter = pThreadPoolMaps_.find(name);
if (iter != pThreadPoolMaps_.end())
return iter->second;
return NULL;
}
private:
static DBThreadPoolMap pThreadPoolMaps_;
};
}
#endif // KBE_DB_INTERFACE_H