fastdo  0.6.8
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 
42 {
43 public:
44  enum
45  {
53  };
54 
55  DbError( int errType, winux::AnsiString const & s ) throw() : winux::Error( errType, s ) { }
56 };
57 
58 interface IDbConnection;
59 interface IDbStatement;
60 interface IDbResult;
61 
64 {
65  virtual ~IDbConnection() { }
66 
68  virtual bool connect();
69 
71  virtual bool close();
72 
74  virtual void alive();
75 
80  virtual bool selectDb( winux::String const & database );
81 
87  virtual bool setLinkCharset( winux::String const & charset );
88 
93  virtual size_t affectedRows();
94 
99  virtual bool createDb( winux::String const & database );
100 
105  virtual bool dropDb( winux::String const & database );
106 
112  virtual int errNo();
113 
117  virtual winux::String error();
118 
123  virtual winux::SharedPointer<IDbResult> query( winux::String const & sql );
125 
131  virtual bool exec( winux::String const & sql );
132  virtual bool exec( winux::SharedPointer<IDbStatement> stmt );
133 
138  virtual size_t insertId();
139 
145  virtual winux::String escape( winux::String const & str, winux::String const & addQuote = "\'" );
146 
153  virtual winux::String escape( void const * buf, size_t size, winux::String const & addQuote = "\'" );
154 
158  virtual winux::SharedPointer<IDbResult> listDbs();
159 
164  virtual winux::SharedPointer<IDbResult> listFields( winux::String const & tableName );
165 
169  virtual winux::SharedPointer<IDbResult> listTables();
170 
172  virtual winux::String tableDdl( winux::String const & tableName );
173 
177  virtual winux::String symbolQuotes( winux::String const & str );
178 
180  virtual size_t loadSql( winux::String const & sqlText, winux::StringArray * arrSql );
182  virtual size_t loadSqlFile( winux::IFile * sqlScriptFile, winux::StringArray * arrSql );
184  virtual size_t getPrimaryKey( winux::String const & tableName, winux::StringArray * arrKeyColumn );
185 
187  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql );
189  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::Mixed const & params );
191  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::MixedArray const & params );
193  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::StringMixedMap const & params );
194 
196  virtual operator bool() const = 0;
197 };
198 
201 {
202  virtual ~IDbStatement() { }
204  virtual bool build( winux::String const & sql );
206  virtual bool build( winux::String const & sql, winux::Mixed const & params );
208  virtual bool build( winux::String const & sql, winux::MixedArray const & params );
210  virtual bool build( winux::String const & sql, winux::StringMixedMap const & params );
212  virtual bool bind( size_t paramIndex, winux::Mixed const & val );
214  virtual bool bind( winux::String const & paramName, winux::Mixed const & val );
215 };
216 
219 {
220  virtual ~IDbResult() { }
221 
226  virtual bool dataSeek( size_t index );
227 
231  virtual bool fetchRow( winux::Mixed * fields, int type = 0 );
232 
234  virtual bool fetchRow( winux::MixedArray * fields );
235 
237  virtual bool fetchRow( winux::StringMixedMap * fields );
238 
243  virtual winux::String fieldName( size_t fieldIndex );
244 
248  virtual bool free();
249 
253  virtual size_t fieldsCount();
254 
258  virtual size_t rowsCount();
259 
264  virtual winux::String fieldType( size_t fieldIndex );
265 
266  /* \brief 获取一个结果集中指定字段相关的标记
267  \param fieldIndex int 字段索引
268  \return string
269  virtual winux::String fieldFlags( int fieldIndex )
270  {
271  throw DBException("fieldFlags() not implemented");
272  }*/
273 
274  /* \brief 返回结果集中指定字段的长度
275  * \param fieldIndex int 字段索引
276  * \return int
277  virtual int fieldLen( int fieldIndex )
278  {
279  throw DBException("fieldLen() not implemented");
280  }*/
281 
282  /* \brief 获取指定字段所在表的名称
283  * \param fieldIndex int 字段索引
284  * \return string
285  virtual winux::String fieldTable( int fieldIndex )
286  {
287  throw DBException("fieldTable() not implemented");
288  }*/
289 
290  /* \brief 从结果集里获取一个单元格的内容
291  \param row int 记录索引
292  \param field int 字段索引
293  \return string
294  virtual winux::String result( int row, int field )
295  {
296  throw DBException("result() not implemented");
297  }*/
298 
299 };
300 
303 {
304  virtual ~IDbModifier() { }
305 
311  virtual bool addNew( winux::StringArray const & fieldNames, winux::MixedArray const & values );
312 
317  virtual bool addNew( winux::Mixed const & fields );
318 
325  virtual bool modify( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::Mixed const & prkValue );
326 
332  virtual bool modify( winux::Mixed const & fields, winux::Mixed const & prkValue );
333 
340  virtual bool modifyEx( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::String const & where );
341 
347  virtual bool modifyEx( winux::Mixed const & fields, winux::String const & where );
348 
353  virtual bool deleteOne( winux::Mixed const & prkValue );
354 
359  virtual bool deleteEx( winux::String const & where );
360 };
361 
363 EIENDB_FUNC_DECL(winux::String) MixedToEscapedStr( winux::Mixed const & m, IDbConnection * cnn, winux::String const & addQuote = "\'" );
364 
367 {
368 public:
369  DbModifier( IDbConnection * cnn, winux::String const & tableName );
370  virtual ~DbModifier();
371 
372  virtual bool addNew( winux::StringArray const & fieldNames, winux::MixedArray const & values ) override;
373  virtual bool addNew( winux::Mixed const & fields ) override;
374  virtual bool modify( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::Mixed const & prkValue ) override;
375  virtual bool modify( winux::Mixed const & fields, winux::Mixed const & prkValue ) override;
376  virtual bool modifyEx( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::String const & where ) override;
377  virtual bool modifyEx( winux::Mixed const & fields, winux::String const & where ) override;
378  virtual bool deleteOne( winux::Mixed const & prkValue ) override;
379  virtual bool deleteEx( winux::String const & where ) override;
380 
381 protected:
382  virtual void _getTableInfo() = 0;
386  winux::StringArray _fieldNames; // 原始字段名
387  //bool _prkAutoIncrement; // 自动增长
388 
390 };
391 
392 
393 }
394 
395 #endif // __EIENDB_BASE_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
数据库连接接口
Definition: eiendb_base.hpp:63
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
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:233
#define EIENDB_FUNC_DECL(ret)
Definition: eiendb_base.hpp:29
XString< tchar > String
Definition: utilities.hpp:216
eiendb数据库错误
Definition: eiendb_base.hpp:41
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
DbError(int errType, winux::AnsiString const &s)
Definition: eiendb_base.hpp:55
SQLite数据库错误
Definition: eiendb_base.hpp:51
SQL语句接口,构造SQL语句,参数绑定
PgSQL数据库错误
Definition: eiendb_base.hpp:52
std::vector< Mixed > MixedArray
Definition: utilities.hpp:232
数据结果操作接口
#define EIENDB_DLL
Definition: eiendb_base.hpp:25
混合体,能表示多种类型的值
Definition: utilities.hpp:750
MySQL数据库错误
Definition: eiendb_base.hpp:50
数据库修改器部分通用实现
错误类
Definition: utilities.hpp:505
数据库通用接口
Definition: eiendb_base.hpp:7
文件接口
Definition: filesys.hpp:206
数据库自身的错误
Definition: eiendb_base.hpp:47
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
winux::StringArray _fieldNames
修改器接口
virtual ~IDbResult()