fastdo  0.6.8
eiendb_mysql.hpp
浏览该文件的文档.
1 #ifndef __EIENDB_MYSQL_HPP__
2 #define __EIENDB_MYSQL_HPP__
3 
4 #include "eiendb_base.hpp"
5 
6 struct st_mysql;
7 struct st_mysql_res;
8 struct st_mysql_stmt;
9 
10 namespace eiendb
11 {
14 {
15  int _mysqlErrNo;
16 public:
17  MysqlDbError( int mysqlErrNo, winux::AnsiString const & s ) : DbError( DbError::dbeMysqlError, s ), _mysqlErrNo(mysqlErrNo) { }
18  int getMysqlErrCode() const throw() { return _mysqlErrNo; }
19 };
20 
22 {
23 public:
24  MysqlConnection( winux::String const & host, winux::String const & user, winux::String const & pwd, winux::String const & dbName, winux::String const & linkCharset, bool doConnect = true );
25  virtual ~MysqlConnection();
26 
27  virtual bool connect() override;
28  virtual bool close() override;
29  virtual void alive() override;
30  virtual bool setLinkCharset( winux::String const & charset ) override;
31  virtual bool selectDb( winux::String const & database ) override;
32  virtual bool createDb( winux::String const & database ) override;
33  virtual bool dropDb( winux::String const & database ) override;
34  virtual size_t affectedRows() override;
35  virtual size_t insertId() override;
36  virtual int errNo() override;
37  virtual winux::String error() override;
38  virtual winux::String escape( winux::String const & str, winux::String const & addQuote = "\'" ) override;
39  virtual winux::String escape( void const * buf, size_t size, winux::String const & addQuote = "\'" ) override;
40  virtual bool exec( winux::String const & sql ) override;
41  virtual bool exec( winux::SharedPointer<IDbStatement> stmt ) override;
42  virtual winux::SharedPointer<IDbResult> query( winux::String const & sql ) override;
44  virtual winux::SharedPointer<IDbResult> listDbs() override;
45  virtual winux::SharedPointer<IDbResult> listFields( winux::String const & tableName ) override;
46  virtual winux::SharedPointer<IDbResult> listTables() override;
47  virtual winux::String tableDdl( winux::String const & tableName ) override;
48  virtual winux::String symbolQuotes( winux::String const & str ) override;
49 
50  virtual size_t loadSql( winux::String const & sqlText, winux::StringArray * arrSql ) override;
51  virtual size_t loadSqlFile( winux::IFile * sqlScriptFile, winux::StringArray * arrSql ) override;
52  virtual size_t getPrimaryKey( winux::String const & tableName, winux::StringArray * arrKeyColumn ) override;
53 
54  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql ) override;
55  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::Mixed const & params ) override;
56  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::MixedArray const & params ) override;
57  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::StringMixedMap const & params ) override;
58 
59  virtual operator bool() const override { return _mysql != NULL; }
60  operator struct st_mysql * () const { return _mysql; }
61 private:
62  struct st_mysql * _mysql; // 代表MYSQL连接
63  winux::String _hostAddr; // 主机地址
64  int _port; // 端口
65  winux::String _user; // 用户名
66  winux::String _pwd; // 密码
67  winux::String _dbName; // 数据库名
68  winux::String _linkCharset; // 连接校验字符集
69 
71 };
72 
74 {
75 public:
77  MysqlStatement( MysqlConnection * cnn, winux::String const & sql );
78  MysqlStatement( MysqlConnection * cnn, winux::String const & sql, winux::Mixed const & params );
79  MysqlStatement( MysqlConnection * cnn, winux::String const & sql, winux::MixedArray const & params );
80  MysqlStatement( MysqlConnection * cnn, winux::String const & sql, winux::StringMixedMap const & params );
81  virtual ~MysqlStatement();
83  virtual bool build( winux::String const & sql ) override;
84  virtual bool build( winux::String const & sql, winux::Mixed const & params ) override;
85  virtual bool build( winux::String const & sql, winux::MixedArray const & params ) override;
86  virtual bool build( winux::String const & sql, winux::StringMixedMap const & params ) override;
88  virtual bool bind( size_t paramIndex, winux::Mixed const & val ) override;
90  virtual bool bind( winux::String const & paramName, winux::Mixed const & val ) override;
91 
92  operator bool() const { return !_orgSql.empty(); }
93  MysqlConnection * getCnn() const { return _cnn; }
94  winux::String const & getSql();
95 
96  // paramIndex base on 1
97  winux::Mixed const & getParam( size_t paramIndex ) const;
98  // paramName prefix with ':'
99  winux::Mixed const & getParam( winux::String const & paramName ) const;
100 private:
101  void _generateSql();
102 
103  MysqlConnection * _cnn;
104  winux::String _orgSql; // 原SQL
105  winux::String _sql;
106  bool _isGeneratedSql; // 是否已经产生SQL
107  bool _isNameBinding; // 是否采用参数名称绑定
108  std::vector< std::pair< size_t, winux::Mixed > > _bindingParams; // 已绑定的参数
109  std::vector< std::pair< winux::String, winux::Mixed > > _bindingNameParams; // 已绑定的名称参数
110 
112 };
113 
115 {
116 public:
118  virtual ~MysqlResult();
119  virtual bool free() override;
120 
121  virtual bool dataSeek( size_t index ) override;
122  virtual bool fetchRow( winux::Mixed * fields, int type = 0 ) override;
123  virtual bool fetchRow( winux::MixedArray * fields ) override;
124  virtual bool fetchRow( winux::StringMixedMap * fields ) override;
125  virtual winux::String fieldName( size_t fieldIndex ) override;
126  virtual size_t fieldsCount() override;
127  virtual size_t rowsCount() override;
128  virtual winux::String fieldType( size_t fieldIndex ) override;
129 
130  bool reset();
131  operator bool() const { return _mysqlRes != NULL; }
132  operator struct st_mysql_res * () const { return _mysqlRes; }
133 private:
134  struct st_mysql_res * _mysqlRes;
136  winux::StringArray _fieldNames; // 非原始字段名
137  std::vector<winux::uint> _fieldTypes; // 字段类型
138  std::vector<winux::uint> _fieldFlags; // 字段Flag
139 
141 };
142 
144 {
145 public:
146  MysqlModifier( MysqlConnection * cnn, winux::String const & tableName );
147  virtual ~MysqlModifier();
148 
149  /*virtual bool addNew( winux::StringArray const & fieldNames, winux::MixedArray const & values );
150  virtual bool addNew( winux::Mixed const & fields );
151  virtual bool modify( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::Mixed const & prkValue );
152  virtual bool modify( winux::Mixed const & fields, winux::Mixed const & prkValue );
153  virtual int modifyEx( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::String const & where );
154  virtual int modifyEx( winux::Mixed const & fields, winux::String const & where );
155  virtual bool deleteOne( winux::Mixed const & prkValue );
156  virtual int deleteEx( winux::String const & where );*/
157 
158 protected:
159  virtual void _getTableInfo() override;
160 /* MysqlConnection * _cnn;
161  winux::String _tableName;
162  winux::StringArray _fieldNames; // 原始字段名
163  winux::String _prkName; // 主键名 */
164 
166 };
167 
168 }
169 
170 #endif // __EIENDB_MYSQL_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
数据库连接接口
Definition: eiendb_base.hpp:63
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:233
XString< tchar > String
Definition: utilities.hpp:216
eiendb数据库错误
Definition: eiendb_base.hpp:41
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
MySQL数据库错误
SQL语句接口,构造SQL语句,参数绑定
std::vector< Mixed > MixedArray
Definition: utilities.hpp:232
数据结果操作接口
#define EIENDB_DLL
Definition: eiendb_base.hpp:25
混合体,能表示多种类型的值
Definition: utilities.hpp:750
数据库修改器部分通用实现
数据库通用接口
Definition: eiendb_base.hpp:7
int getMysqlErrCode() const
文件接口
Definition: filesys.hpp:206
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
MysqlConnection * getCnn() const
MysqlDbError(int mysqlErrNo, winux::AnsiString const &s)