My Project
ICDBManager.h
1 #pragma once
2 #include "ParaDatabase.h"
3 #include <map>
4 #include <vector>
5 #include <util/mutex.h>
6 
7 struct sqlite3_stmt;
8 struct sqlite3;
9 
11 #ifndef MAX_SQL_LENGTH
12 #define MAX_SQL_LENGTH 2047
13 #endif
14 
15 namespace ParaInfoCenter
16 {
17  class CICRecordSet;
18  class CICDBManager;
19  class DBEntity;
20 
21  typedef pair<CICRecordSet*,bool> RSpair;
22  typedef pair<DBEntity*,bool> DBpair;
23 
28  {
29  public:
30  CICSQLException(int code,const char *zFormat,...);
31  ~CICSQLException();
32  int errcode;
33  string errmsg;
34  };
35 
39  class DBEntity
40  {
41  friend class CICMapTrace;
42  friend class CICRoot;
43  friend class CICGenManager;
44  friend class CICDBManager;
45  public:
46 
47  PE_CORE_DECL ~DBEntity();
49  //
50  // database entity member functions
51  //
53  PE_CORE_DECL void Release();
54 
57  void addref()
58  {
59  ++m_refcount;
60  }
61 
65  bool delref()
66  {
67  assert(m_refcount>0);
68  return --m_refcount<=0;
69  }
72  {
73  return m_refcount;
74  }
75 
80  PE_CORE_DECL int ExecuteSqlStringFormated(const char *sql,...);
81  PE_CORE_DECL int ExecuteSqlStringFormated(const char16_t *sql,...);
82 
90  PE_CORE_DECL int ExecuteSqlScript(const char *sql, bool bBreakOnError = false);
91 
92 
95  PE_CORE_DECL int sql_prepare(const char *sql,...);
96  PE_CORE_DECL int sql_prepare(const char16_t *sql,...);
97 
103  PE_CORE_DECL int sql_step();
104 
106  PE_CORE_DECL int sql_finalize();
107 
109  sqlite3_stmt* GetStatement(){return m_stmt;}
110 
112  PE_CORE_DECL void OpenDB();
113 
115  PE_CORE_DECL void CloseDB();
116 
121  PE_CORE_DECL bool IsValid() const {return m_isValid;};
126  PE_CORE_DECL const string& GetConnectionString(){return m_filepath;};
127 
132  sqlite3 *GetDBHandle(){return m_db;};
133 
140  PE_CORE_DECL CICRecordSet* CreateRecordSet(const char* sql,...);
141  PE_CORE_DECL CICRecordSet* CreateRecordSet(const char16_t* sql,...);
143  PE_CORE_DECL CICRecordSet* CreateRecordSet1(const char* sql);
144  PE_CORE_DECL CICRecordSet* CreateRecordSet1(const char16_t* sql);
145  PE_CORE_DECL CICRecordSet *CreateRecordSet();
150  PE_CORE_DECL void DeleteRecordSet(CICRecordSet* recordset);
151  PE_CORE_DECL int64 GetLastInsertRowID();
152 
156  PE_CORE_DECL bool IsCreateFile();
157 
161  PE_CORE_DECL void SetCreateFile(bool bCreateFile);
162 
163  protected:
164  /* @obsolete the following exec_sql* function are not used. Maybe obsolete soon
165  * These functions do not guarantee the ' in text field are properly replaced by ''
166  * The exec_sql* function will execute the given sql string completely. It will execute the string as a batch operation
167  */
168 
173  void exec_sql16(const char16_t *sql);
174  void exec_sql(const char *sql);
180  void prepare_sql(const char16_t *sql,...);
181  void prepare_sql(const char *sql,...);
182  void exec_sql();
183  void exec_sql16();
184 
185  sqlite3_stmt* exec_sql_prepare();
186  sqlite3_stmt* exec_sql_prepare16();
187  int exec_sql_finish();
188  int exec_sql_finish(sqlite3_stmt* stmt);
189  int exec_sql_step();
190  int exec_sql_step(sqlite3_stmt* stmt);
191 
196  void OpenDB16(const char16_t* dbname);
197  void OpenDB(const char* dbname);
203  string PrepareDatabaseFile(const string& filename);
204 
205  protected:
206  DBEntity();
207  void init();
209  sqlite3 *m_db;
211  vector<RSpair> m_RSpool;
213  u16string m_name;
215  string m_filepath;
217  bool m_isValid;
220 
223 
226 
227  /* the following only used for sqlite3 wrapper. */
228  std::u16string m_wsql;
229  std::string m_sql;
230  void * m_trail;
231  sqlite3_stmt *m_stmt;
232 
235 
243  };
244 
283  {
284 
285  public:
287  //
288  // database management functions
289  //
291 
296  PE_CORE_DECL static DBEntity * GetDB(const char16_t* name);
297  PE_CORE_DECL static DBEntity *OpenDBEntity();
303  PE_CORE_DECL static DBEntity *OpenDBEntity(const char16_t* dbname);
304  PE_CORE_DECL static DBEntity *OpenDBEntity(const char* dbname);
310  PE_CORE_DECL static DBEntity *OpenDBEntity(const char16_t* name, const char16_t* dbname);
311  PE_CORE_DECL static DBEntity *OpenDBEntity(const char* name, const char* dbname);
316  PE_CORE_DECL static bool CloseDBEntity(DBEntity * pEntity);
317 
319  PE_CORE_DECL static void Finalize();
320  PE_CORE_DECL static void StaticInit();
321 
322  protected:
324  static vector<DBpair> m_DBpool;
325 
328 
329  public:
330  friend class CICMapTrace;
331  friend class CICRoot;
332  friend class CICGenManager;
333  };
334 }
it represents both a database entity and a database manager the static functions are for database man...
Definition: ICDBManager.h:282
int m_refcount
reference count of the asset.
Definition: ICDBManager.h:242
string m_filepath
db file path
Definition: ICDBManager.h:215
Definition: ICMapTrace.h:28
vector< RSpair > m_RSpool
a pool of sql statements created by the user.
Definition: ICDBManager.h:211
ParaEngine::mutex m_mutex
this makes all db query thread-safe
Definition: ICDBManager.h:234
exceptions thrown from database
Definition: ICDBManager.h:27
bool m_bEncodingUTF8
whether the database use UTF-8 encoding; if false it will be UTF-16 unicode encoding.
Definition: ICDBManager.h:219
sqlite3_stmt * GetStatement()
get the current statement
Definition: ICDBManager.h:109
Definition: ICGenManager.h:11
int m_nSQLite_OpenFlags
the opening flags for sqlite3_open_v2().
Definition: ICDBManager.h:225
PE_CORE_DECL const string & GetConnectionString()
get the connection string.
Definition: ICDBManager.h:126
ParaInfoCenter::DBEntity DBEntity
database entity
Definition: ParaDatabase.h:13
void addref()
add reference count of the asset.
Definition: ICDBManager.h:57
Definition: inftrees.h:24
Definition: PEtypes.h:503
u16string m_name
alias name of the db
Definition: ICDBManager.h:213
PE_CORE_DECL bool IsValid() const
return true if database is now successfully opened.
Definition: ICDBManager.h:121
a single database file.
Definition: ICDBManager.h:39
sqlite3 * GetDBHandle()
get sqlite3 object
Definition: ICDBManager.h:132
static ParaEngine::mutex m_mutex
this makes all db query thread-safe
Definition: ICDBManager.h:327
int GetRefCount()
get the reference count
Definition: ICDBManager.h:71
bool delref()
decrease reference count of the asset.
Definition: ICDBManager.h:65
cross platform mutex
Definition: mutex.h:95
bool m_isValid
whether db is opened.
Definition: ICDBManager.h:217
sqlite3 * m_db
database handle
Definition: ICDBManager.h:209
bool m_bIsCreateFile
this is true if database file is opened by creating a new database file.
Definition: ICDBManager.h:222
static vector< DBpair > m_DBpool
all databases
Definition: ICDBManager.h:324
CICRecordSet is both the result and wrapper of a sql statement(procedure).
Definition: ICRecordSet.h:90