-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccorm.h
More file actions
297 lines (262 loc) · 4.94 KB
/
ccorm.h
File metadata and controls
297 lines (262 loc) · 4.94 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#pragma once
#include <vector>
#include <map>
#include <typeinfo>
#include <sstream>
#include "ccspring.h"
class FieldInfo {
public:
/// 字段类型
int type;
/// 字段名称
std::string name;
};
class TableInfo {
public:
unsigned int size()
{
return _fields.size();
}
const char* getTableName()
{
return _tableName.c_str();
}
FieldInfo * getField(unsigned int index);
bool addField(int type, const std::string& name);
private:
std::vector<FieldInfo> _fields;
std::string _tableName;
};
/**
* 一条记录
*/
class Record {
public:
Record(){
}
virtual ~Record()
{}
AnyValue& operator[](const std::string &name)
{
return _values[name];
}
void put(const char *fieldName);
TableInfo *tableInfo = 0;
unsigned int size()
{
return _values.size();
}
void put(const char* fieldName, const AnyValue& value);
void put(const char* fieldName, const char* value, unsigned int size);
bool find(const std::string& fieldName);
private:
std::map<std::string, AnyValue> _values;
};
/**
* 记录集合
*/
class RecordSet {
public:
/**
* \brief 重载operator[]运算符
*
* 通过指定的行数,获取相应的记录
*
* \param idx:指定的行数
*
* \return 如果指定的行数有效,则返回相应的记录指针,如果无效,则返回NULL
*/
Record* operator[](unsigned int idx)
{
if (idx < _records.size())
{
return &_records[idx];
}
return 0;
}
/**
* \brief 获取记录数
*
* \return 返回记录数,如果没有记录,返回为0
*/
unsigned int size()
{
return _records.size();
}
/**
* \brief 获取记录数
*
* \return 返回记录数,如果没有记录,返回为0
*/
bool empty(){ return _records.empty(); }
/**
* \brief 添加记录
*
*/
void put(const Record& rec)
{
_records.push_back(rec);
}
/**
* \brief 清空所有记录
*
*/
void clear()
{
_records.clear();
}
/**
* \brief 获取指定的行
*
* 功能与重载的operator[]运算符相同。
*/
Record* get(unsigned int idx)
{
if (idx < _records.size())
{
return &_records[idx];
}
return 0;
}
private:
std::vector<Record> _records;
};
/**
* 数据库连接
*/
class IDBConnection {
public:
/**
* 选择
*/
virtual RecordSet * execSelect(TableInfo *table, Record *column, const std::string &condition){
return 0;
}
/**
* 删除
*/
virtual unsigned int exeDelete(TableInfo* table, const std::string &condition){
return 0;
}
/**
* 更新
*/
virtual unsigned int exeUpdate(TableInfo* table, Record* data, const std::string &condition){
return 0;
}
/**
* 插入
*/
virtual unsigned long exeInsert(TableInfo* table, Record* rec){
return 0;
}
/**
* \brief 事务提交
* \param handleID 操作的链接句柄
* \return 成功返回true,失败返回false
*/
virtual bool commit(){
return false;
}
/**
* \brief 事务回滚
* \param handleID 操作的链接句柄
* \return 成功返回true,失败返回false
*/
virtual bool rollback(){
return false;
}
/**
* \brief 设置此链接是否支持事务
* \param handleID 操作的链接句柄
* \param supportTransactions 是否支持事务
* \return 成功返回true,失败返回false
*/
virtual bool setTransactions(bool supportTransactions){
return false;
}
/**
* \brief 检查此链接是否支持事务
* \param handleID 操作的链接句柄
* \return 支持返回true,否则返回false
*/
virtual bool supportTransactions() {
return false;
}
};
/**
* 数据库操作
*/
class IDB :public Interface<IDB>{
public:
/**
* 获取连接
*/
virtual IDBConnection *getConnection() = 0;
/**
* 回收连接
*/
virtual void putConnection(IDBConnection *conn) = 0;
/**
* 获取表
*/
virtual TableInfo * getTable(const std::string &name) = 0;
};
/**
* 基本封装前的使用
* IDB *db = core::getBean("db");
* TableInfo * t_test= db->getTable("T_TEST");
* if (t_test)
* {
* IDBConnection *conn = db->getConnection(); // 获取连接
* Record col;
* col.put("ID");
* col.put("NAME");
* RecordSet * rs = conn->exeSelect(t_test,&col,"where id=1");
* // 获取的结果集
* for (int i = 0; i < rs.size();++i)
* {
* Record *rec = rset->get(i);
* rec->get("ID"); // 获取ID
* rec->get("NAME"); // 获取NAME
* }
* db->putConnection(conn); // 释放连接
* }
*/
class mytable {
public:
/**
* 数据库
*/
TableInfo *tableInfo;
/**
*
*/
void attach(const std::string &tableName)
{
}
template<typename TOBJECT>
void insert(TOBJECT &object)
{
// 构建Record
// 插入
}
};
/*********************************************************************
* 使用 (使用spring 后 相关的使用 就很简单了 基础的一些方案 可以做热插播)
* IDB *db = ccs::getBean("db"); // 相关配置都从配置中读取
* if (mytable<INFO>::attach(db,"T_INFO"))
* {
* INFO info;
* mytable<INFO>::insert(info);
* }
*********************************************************************
* 初始化
* ccs::initFrom("cc-spring.xml"); // 预构建对象
*********************************************************************
* 配置
* <bean name="db" class="MysqlDB" > <!--MysqlDB 可换成 Sqlite3-->
* <connection url="" port="" username="" passwd="" dbname="" />
* <connectionpool size="10" />
* </bean>
*********************************************************************/