-
Notifications
You must be signed in to change notification settings - Fork 9
/
cxdatabase.h
337 lines (226 loc) · 10.2 KB
/
cxdatabase.h
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef CXDATABASE_H
#define CXDATABASE_H
#include "cxglobal.h"
static const std::string CS_SectionDataBase = "DataBase";
class GM_CCXX_CORE_API CxDatabase {
public:
typedef enum {
ColumnTypeNone = 0,
ColumnTypeBoolean,
ColumnTypeInt,
ColumnTypeLongint,
ColumnTypeDouble,
ColumnTypeDatetime,
ColumnTypeString,
ColumnTypeBlob,
ColumnTypeRes1,
ColumnTypeRes2,
ColumnTypeRes3,
ColumnTypeRes4,
ColumnTypeRes5
} ColumnTypeEnum;
typedef enum {
CursorTypeNone = 0,
CursorTypeOdbc,
CursorTypeSqlite,
CursorTypeRes1,
CursorTypeRes2,
CursorTypeRes3,
CursorTypeRes4,
CursorTypeRes5
} CursorTypeEnum;
class CursorBase {
public:
CursorBase(CursorTypeEnum eCursorType=CursorTypeNone, int iPrefetchArraySize=0):_cursorType(eCursorType),_prefetchArraySize(iPrefetchArraySize){}
virtual ~CursorBase(){}
inline CursorTypeEnum getCursorType() { return _cursorType; }
inline int getPrefetchArraySize() { return _prefetchArraySize; }
inline const std::vector<std::string> & getColumnNames() { return _columnNames; };
inline const std::vector<int> & getColumnTypes() { return _columnTypes; };
inline const std::vector<int> & getColumnSizes() { return _columnSizes; };
protected:
CursorTypeEnum _cursorType;
int _prefetchArraySize;
std::vector<std::string> _columnNames;
std::vector<int> _columnTypes;
std::vector<int> _columnSizes;
friend class CxDatabaseOdbc;
friend class CxDatabaseSqlite;
};
public:
static CxDatabase *getDefaultDb();
CxDatabase();
virtual ~CxDatabase();
inline const std::string &lastErrorString() const { return _lastErrorString; }
inline int lastErrorId() const { return _lastErrorId; }
inline const std::string &connectSource() const { return _connectSource; }
inline const std::string &connectType() const { return _connectType; }
inline const std::map<std::string, std::string> &connectParams() const { return _connectParams; }
inline const std::map<std::string, std::string> &databaseAttrs() const { return _databaseAttrs; }
inline bool isOpen() const { return isOpenImpl(); }
bool openDatabase(const std::string &sConnectSource, std::string sConnectType = "", bool bCreate = true, const std::map<std::string, std::string> *oParams = NULL);
bool openDatabase();
void closeDatabase();
// ### DDL
inline bool createTable(const std::string &sTableName, const std::map<std::string, std::string> &columns) { return createTableImpl(sTableName, columns); }
inline bool alterPrimaryKeyAdd(const std::string &sTableName, const std::vector<std::string> &columns) { return alterPrimaryKeyAddImpl(sTableName, columns); }
// ### load table
int loadTable(const std::string &sTableName, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL, int iMaxRowCount = -1);
// ### load data by sql
int loadSql(const std::string &sSql, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL);
int loadSql1(const std::string &sSql, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL, int iMaxRowCount = -1);
int loadSql2(const std::string &sSql, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL, std::vector<int> *oColumnTypes = NULL);
// get one value data
std::string queryValue(const std::string &sSql);
// get one value data
bool queryValue(const std::string &sSql, std::string &sValue);
// get one row data
std::vector<std::string> queryValueToList(const std::string &sSql);
// get one row data
std::map<std::string, std::string> queryValueToMap(const std::string &sSql);
// get mult row key=value data
std::vector<std::map<std::string, std::string> > queryToMapVector(const std::string &sSql);
std::string queryToJsonString(const std::string &sSql);
std::map<std::string, std::string> loadVerticalTableObject(const std::string & sSql);
// ### update table
int saveTable(const std::string &sTableName, const std::vector<std::string> &columnNames, const std::vector<std::vector<std::string> > &rows, const std::vector<int> &columnTypes = std::vector<int>(), bool bTransaction = true);
int saveTable(const std::string &sSql, const std::vector<std::vector<std::string> > &rows, const std::vector<int> *oColumnTypes = NULL, bool bTransaction = false);
int updateTable(const std::string &sTableName, const std::vector<std::string> &columnNames, const std::vector<std::vector<std::string> > &rows, const std::vector<int> &columnTypes = std::vector<int>(), bool bTransaction = true);
// ### exec sql
int execSql(const std::string &sSql);
int execSqlList(const std::vector<std::string> &sqlList);
// ### GetDb
inline void * getDb() { return getDbImpl(); }
// ### Cursor
CursorBase * cursorLoad(const std::string &sSql, int iPrefetchArraySize = 100);
bool cursorIsEnd(CursorBase * oCursor);
std::vector<std::string> cursorNext(CursorBase * oCursor);
int cursorPut(CursorBase * oCursor, std::vector<std::vector<std::string> > &rows, int iMaxRowCount = -1);
int cursorClose(CursorBase * oCursor);
inline std::vector<CursorBase *> cursorGetOnlineAll() { return _cursors; }
protected:
inline void setLastError(int iErrorId, const std::string &sErrorString)
{
_lastErrorId = iErrorId;
_lastErrorString = sErrorString;
}
void setSqlLastSuccessTime(int rSqlExec);
protected:
virtual bool
openDatabaseImpl(const std::string &sDatabase, const std::string &sDatabaseType, bool bCreate, const std::map<std::string, std::string> *oParams) = 0;
virtual void
closeDatabaseImpl() = 0;
virtual bool
isOpenImpl() const = 0;
virtual bool
createTableImpl(const std::string &sTableName, const std::map<std::string, std::string> &columns) = 0;
virtual bool
alterPrimaryKeyAddImpl(const std::string &sTableName, const std::vector<std::string> &columns) = 0;
virtual int
loadTableImpl(const std::string &sTableName, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL, int iMaxRowCount = -1) = 0;
virtual int
saveTableImpl(const std::string &sTableName, const std::vector<std::string> &columnNames, const std::vector<std::vector<std::string> > &rows, const std::vector<int> &columnTypes = std::vector<int>(), bool beginTransaction = false) = 0;
virtual int
saveTableImpl(const std::string &sSql, const std::vector<std::vector<std::string> > &rows, const std::vector<int> *oColumnTypes = NULL, bool beginTransaction = false) = 0;
virtual int
updateTableImpl(const std::string &sTableName, const std::vector<std::string> &columnNames, const std::vector<std::vector<std::string> > &rows, const std::vector<int> &columnTypes = std::vector<int>(), bool beginTransaction = false) = 0;
virtual int
execSqlImpl(const std::string &sSql) = 0;
virtual int
execSqlListImpl(const std::vector<std::string> &sqlList) = 0;
virtual int
loadSqlImpl(const std::string &sSql, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL, int iMaxRowCount = -1) = 0;
virtual int
loadSql2Impl(const std::string &sSql, std::vector<std::vector<std::string> > &rows, std::vector<std::string> *oColumnNames = NULL, std::vector<int> *oColumnTypes = NULL, int iMaxRowCount = -1) = 0;
virtual void *
getDbImpl() = 0;
virtual CursorBase *
cursorLoadImpl(const std::string &sSql, int iPrefetchArraySize) = 0;
virtual bool
cursorIsEndImpl(CursorBase * oCursor) = 0;
virtual int
cursorPutImpl(CursorBase * oCursor, std::vector<std::vector<std::string> > &rows, int iMaxRowCount) = 0;
virtual int
cursorCloseImpl(CursorBase * oCursor) = 0;
protected:
std::map<std::string, std::string> _databaseAttrs;
long long _sqlLastSuccessTime;
long long _reconnectTime;
protected:
int _lastErrorId;
std::string _lastErrorString;
std::string _connectSource;
std::string _connectType;
std::map<std::string, std::string> _connectParams;
std::vector<CursorBase *> _cursors;
friend class CxDatabaseManager;
};
typedef bool (*fn_isMyDatabase_t)(const std::string &sConnectSource);
typedef CxDatabase *(*fn_createDatabase_t)(void);
class GM_CCXX_CORE_API CxDatabaseManager {
public:
static CxDatabase *createDatabase(const std::string &sConnectSource, std::string sConnectType = "");
static CxDatabase *findDb(const std::string &sSource);
static CxDatabase *getDefaultDb();
static std::string getDefaultDatabasePath();
static CxDatabase *getDbByIndex(int index);
static void start();
static void stop();
static bool closeDatabase(const std::string &sConnectSource);
static const std::map<std::string, CxDatabase *> &dbs();
static void registDatabaseConstructor(fn_isMyDatabase_t fn_isMyDatabase, fn_createDatabase_t fn_createDatabase);
static void connectCheck(int iInterval);
};
template<typename T1>
class CxTableTemple1 {
public:
};
template<typename T1, typename T2>
class CxTableTemple2 {
public:
struct DataStruct {
T1 f1;
T2 f2;
};
std::vector<DataStruct> load();
std::vector<DataStruct> load(const std::string &sTableName);
int save(const std::vector<DataStruct> &datas);
};
template<typename T1, typename T2, typename T3>
class CxTableTemple3 {
public:
struct DataStruct {
T1 f1;
T2 f2;
T3 f3;
};
std::vector<DataStruct> load();
std::vector<DataStruct> load(const std::string &sTableName);
int save(const std::vector<DataStruct> &datas);
};
class CxDbDriverBase {
};
class CxDbDriverSqlite : public CxDbDriverBase {
};
class CxDbDriverOtl : public CxDbDriverBase {
};
class CxField {
public:
CxField()
{}
const std::string &fieldName() const;
int valueType();
int toInt();
bool toBoolean();
};
class CxRow {
public:
};
class CxTable {
public:
const std::vector<CxField> &fields() const;
void *s;
};
class CxDb {
};
#endif // CXDATABASE_H