fastdo  0.6.16
eiendb_base.hpp
浏览该文件的文档.
1 #ifndef __EIENDB_BASE_HPP__
2 #define __EIENDB_BASE_HPP__
3 
4 #include "winux.hpp"
5 
7 namespace eiendb
8 {
9 
10 #ifdef EIENDB_DLL_USE
11  #if defined(_MSC_VER) || defined(WIN32)
12  #pragma warning( disable: 4251 )
13  #ifdef EIENDB_DLL_EXPORTS
14  #define EIENDB_DLL __declspec(dllexport)
15  #else
16  #define EIENDB_DLL __declspec(dllimport)
17  #endif
18 
19  #define EIENDB_API __stdcall
20  #else
21  #define EIENDB_DLL
22  #define EIENDB_API
23  #endif
24 #else
25  #define EIENDB_DLL
26  #define EIENDB_API
27 #endif
28 
29 #define EIENDB_FUNC_DECL(ret) EIENDB_DLL ret EIENDB_API
30 #define EIENDB_FUNC_IMPL(ret) ret EIENDB_API
31 
32 
33 #ifndef interface
34 #define interface struct
35 #endif
36 
38 typedef void * DbHandle;
39 typedef void * DbResHandle;
40 
43 {
44 public:
45  enum
46  {
55  };
56 
57  DbError( int errType, winux::AnsiString const & s ) throw() : winux::Error( errType, s ) { }
58 };
59 
60 interface IDbConnection;
61 interface IDbStatement;
62 interface IDbResult;
63 
66 {
67  virtual ~IDbConnection() { }
68 
70  virtual bool connect();
71 
73  virtual bool close();
74 
76  virtual void alive();
77 
82  virtual bool selectDb( winux::String const & database );
83 
89  virtual bool setLinkCharset( winux::String const & charset );
90 
95  virtual bool createDb( winux::String const & database );
96 
101  virtual bool dropDb( winux::String const & database );
102 
107  virtual size_t affectedRows();
108 
113  virtual size_t insertId();
114 
120  virtual int errNo();
121 
125  virtual winux::String error();
126 
131  virtual winux::SharedPointer<IDbResult> query( winux::String const & sql );
132 
138 
144  virtual bool exec( winux::String const & sql );
145 
151  virtual bool exec( winux::SharedPointer<IDbStatement> stmt );
152 
158  virtual winux::String escape( winux::String const & str, winux::String const & addQuote = "\'" );
159 
166  virtual winux::String escape( void const * buf, size_t size, winux::String const & addQuote = "\'" );
167 
171  virtual winux::SharedPointer<IDbResult> listDbs();
172 
177  virtual winux::SharedPointer<IDbResult> listFields( winux::String const & tableName );
178 
182  virtual winux::SharedPointer<IDbResult> listTables();
183 
185  virtual winux::String tableDdl( winux::String const & tableName );
186 
192  virtual winux::String symbolQuotes( winux::String const & str, bool periodAsSeparator = true );
193 
195  virtual size_t loadSqlFile( winux::IFile * sqlFile, winux::StringArray * arrSql );
197  virtual size_t getPrimaryKey( winux::String const & tableName, winux::StringArray * arrKeyColumn );
198 
200  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::Mixed const & params = winux::mxNull );
201 
203  virtual operator bool() const = 0;
204 };
205 
208 {
209  virtual ~IDbStatement() { }
211  virtual bool build( winux::String const & sql, winux::Mixed const & params = winux::mxNull );
215  virtual bool bind( size_t paramIndex, winux::Mixed const & val );
219  virtual bool bind( winux::String const & paramName, winux::Mixed const & val );
220 };
221 
224 {
225  virtual ~IDbResult() { }
226 
230  virtual bool free();
231 
236  virtual bool dataSeek( size_t index );
237 
242  virtual bool fetchRow( winux::Mixed * fields, int type = 0 );
243 
245  virtual bool fetchArray( winux::MixedArray * fields );
246 
248  virtual bool fetchMap( winux::StringMixedMap * fields );
249 
254  virtual winux::String fieldName( size_t fieldIndex );
255 
259  virtual size_t fieldsCount();
260 
264  virtual size_t rowsCount();
265 
270  virtual winux::String fieldType( size_t fieldIndex );
271 
272 };
273 
276 {
277  virtual ~IDbModifier() { }
278 
284  virtual bool addNew( winux::StringArray const & fieldNames, winux::MixedArray const & values );
285 
290  virtual bool addNew( winux::Mixed const & fields );
291 
298  virtual bool modify( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::Mixed const & prkValue );
299 
305  virtual bool modify( winux::Mixed const & fields, winux::Mixed const & prkValue );
306 
313  virtual bool modifyEx( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::String const & where );
314 
320  virtual bool modifyEx( winux::Mixed const & fields, winux::String const & where );
321 
326  virtual bool deleteOne( winux::Mixed const & prkValue );
327 
332  virtual bool deleteEx( winux::String const & where );
333 };
334 
336 EIENDB_FUNC_DECL(winux::String) MixedToEscapedStr( winux::Mixed const & m, IDbConnection * cnn, winux::String const & addQuote = "\'" );
337 
340 {
341 public:
342  DbStatement( IDbConnection * cnn );
343  DbStatement( IDbConnection * cnn, winux::String const & sql, winux::Mixed const & params = winux::mxNull );
344 
345  virtual bool build( winux::String const & sql, winux::Mixed const & params = winux::mxNull ) override;
346  virtual bool bind( size_t paramIndex, winux::Mixed const & val ) override;
347  virtual bool bind( winux::String const & paramName, winux::Mixed const & val ) override;
348 
349  operator bool() const { return !_orgSql.empty(); }
350  IDbConnection * getCnn() const { return _cnn; }
351  winux::String const & getSql();
352 
353  // paramIndex base on 1
354  winux::Mixed const & getParam( size_t paramIndex ) const;
355  // paramName prefix with ':'
356  winux::Mixed const & getParam( winux::String const & paramName ) const;
357 
358 private:
359  // 生成SQL
360  void _generateSql();
361 
362  IDbConnection * _cnn;
363  winux::String _orgSql; // 原SQL
364  winux::String _sql; // 生成的SQL
365  bool _isGeneratedSql; // 是否已经生成SQL
366  bool _isNameBinding; // 是否采用参数名称绑定
367  std::vector< std::pair< size_t, winux::Mixed > > _bindingParams; // 已绑定的参数
368  std::vector< std::pair< winux::String, winux::Mixed > > _bindingNameParams; // 已绑定的名称参数
369 
371 };
372 
375 {
376 public:
377  DbModifier( IDbConnection * cnn, winux::String const & tableName );
378  virtual ~DbModifier();
379 
380  virtual bool addNew( winux::StringArray const & fieldNames, winux::MixedArray const & values ) override;
381  virtual bool addNew( winux::Mixed const & fields ) override;
382  virtual bool modify( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::Mixed const & prkValue ) override;
383  virtual bool modify( winux::Mixed const & fields, winux::Mixed const & prkValue ) override;
384  virtual bool modifyEx( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::String const & where ) override;
385  virtual bool modifyEx( winux::Mixed const & fields, winux::String const & where ) override;
386  virtual bool deleteOne( winux::Mixed const & prkValue ) override;
387  virtual bool deleteEx( winux::String const & where ) override;
388 
389 protected:
390  // 获取表信息
391  virtual void _getTableInfo();
392  // 构建`INSERT INTO`语句(含待绑定变量占位符,语句末尾不带';')
393  virtual winux::String _buildInsertInto( winux::StringArray const * fieldNames, size_t valuesCount );
394  // 执行`INSERT INTO`语句
395  virtual bool _execInsertInto( winux::SharedPointer<IDbStatement> stmt );
396 
399  winux::StringArray _prkColumn; // 主键引用列
400  winux::StringArray _fieldNames; // 原始字段名
401 
403 };
404 
405 
406 } // namespace eiendb
407 
408 #endif // __EIENDB_BASE_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
数据库连接接口
Definition: eiendb_base.hpp:65
winux::String MixedToEscapedStr(winux::Mixed const &m, IDbConnection *cnn, winux::String const &addQuote="\'")
Mixed变量连结进SQL语句时的换码操作
IDbConnection * _cnn
winux::String _tableName
void * DbHandle
数据库各种对象资源的指针类型
Definition: eiendb_base.hpp:38
winux::StringArray _prkColumn
Mixed const mxNull
Mixed(MT_NULL)常量对象
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:278
IDbConnection * getCnn() const
#define EIENDB_FUNC_DECL(ret)
Definition: eiendb_base.hpp:29
XString< tchar > String
Definition: utilities.hpp:261
eiendb数据库错误
Definition: eiendb_base.hpp:42
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
DbError(int errType, winux::AnsiString const &s)
Definition: eiendb_base.hpp:57
无效的数据库连接
Definition: eiendb_base.hpp:51
SQLite数据库错误
Definition: eiendb_base.hpp:53
SQL语句接口。构造SQL语句、参数绑定
引用计数共享指针
Definition: smartptr.hpp:456
PostgreSQL数据库错误
Definition: eiendb_base.hpp:54
std::vector< Mixed > MixedArray
Definition: utilities.hpp:277
数据结果操作接口
#define EIENDB_DLL
Definition: eiendb_base.hpp:25
void * DbResHandle
Definition: eiendb_base.hpp:39
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
MySQL数据库错误
Definition: eiendb_base.hpp:52
数据库修改器类通用实现
错误类
Definition: utilities.hpp:838
数据库通用接口
Definition: eiendb_base.hpp:7
数据库语句类通用实现
文件接口
Definition: filesys.hpp:313
数据库自身的错误
Definition: eiendb_base.hpp:48
XStringArray< tchar > StringArray
Definition: utilities.hpp:272
winux::StringArray _fieldNames
修改器接口
virtual ~IDbResult()