fastdo  0.6.8
eiendb_sqlite.hpp
浏览该文件的文档.
1 #ifndef __EIENDB_SQLITE_HPP__
2 #define __EIENDB_SQLITE_HPP__
3 
4 #include "eiendb_base.hpp"
5 
6 struct sqlite3;
7 struct sqlite3_stmt;
8 
9 namespace eiendb
10 {
11 
14 {
15  int _sqliteErrNo;
16 public:
17  SqliteDbError( int sqliteErrNo, winux::AnsiString const & s ) throw() : DbError( DbError::dbeSqliteError, s ), _sqliteErrNo(sqliteErrNo) { }
18  int getSqliteErrCode() const throw() { return _sqliteErrNo; }
19 };
20 
22 {
23 public:
24  SqliteConnection( winux::String const & path = "", winux::AnsiString const & dbKey = "", winux::String linkCharset = "", bool doConnect = true );
25  virtual ~SqliteConnection();
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 int errNo() override;
36  virtual winux::String error() override;
37  // 只是简单的引号double。内部不会进行utf8换码
38  virtual winux::String escape( winux::String const & str, winux::String const & addQuote = "\'" ) override;
39  // 将二进制数据转换成X'******'的形式
40  virtual winux::String escape( void const * buf, size_t size, winux::String const & addQuote = "\'" ) override;
41  // 执行SQL语句查询并返回结果集
42  virtual winux::SharedPointer<IDbResult> query( winux::String const & sql ) override;
44  // 执行SQL语句,返回bool代表是否成功
45  virtual bool exec( winux::String const & sql ) override;
46  virtual bool exec( winux::SharedPointer<IDbStatement> stmt ) override;
47  virtual size_t insertId() override;
48  virtual winux::SharedPointer<IDbResult> listDbs() override;
49  // 执行 PRAGMA table_info(tablename)
50  virtual winux::SharedPointer<IDbResult> listFields( winux::String const & tableName ) override;
51  // 执行 SELECT name FROM sqlite_master WHERE type = 'table' AND substr( name, 1, 7 ) <> 'sqlite_' ORDER BY name;
52  virtual winux::SharedPointer<IDbResult> listTables() override;
53  virtual winux::String tableDdl( winux::String const & tableName ) override;
54  virtual winux::String symbolQuotes( winux::String const & str ) override;
55 
56  virtual size_t loadSql( winux::String const & sqlText, winux::StringArray * arrSql ) override;
57  virtual size_t loadSqlFile( winux::IFile * sqlScriptFile, winux::StringArray * arrSql ) override;
58  virtual size_t getPrimaryKey( winux::String const & tableName, winux::StringArray * arrKeyColumn ) override;
59 
60  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql ) override;
61  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::Mixed const & params ) override;
62  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::MixedArray const & params ) override;
63  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::StringMixedMap const & params ) override;
64 
65  virtual operator bool() const override { return _db != NULL; }
66  operator sqlite3 * () const { return _db; }
67  // SQLite字符串转到winux::String,NULL也能正确处理
68  winux::String sqliteStrToString( char const * s );
69  // winux::String转到SQLite字符串
70  winux::AnsiString stringToSqliteStr( winux::String const & s );
71 private:
72  bool _open( winux::String const & database, winux::uint flags );
73 
74  sqlite3 * _db;
75  winux::String _path; // 输入的路径参数
76  winux::String _dirPath; // 解析到的目录路径
77  winux::String _database; // 解析到的数据库名
78  winux::String _linkCharset; // 连接校验字符集
79  winux::AnsiString _dbKey; // 数据库密码,可以为二进制数据
80 
82 };
83 
85 {
86 public:
88  SqliteStatement( SqliteConnection * cnn, winux::String const & sql );
89  SqliteStatement( SqliteConnection * cnn, winux::String const & sql, winux::Mixed const & params );
90  SqliteStatement( SqliteConnection * cnn, winux::String const & sql, winux::MixedArray const & params );
91  SqliteStatement( SqliteConnection * cnn, winux::String const & sql, winux::StringMixedMap const & params );
92  virtual ~SqliteStatement();
93  //SqliteStatement( SqliteStatement & other );
94  //SqliteStatement & operator = ( SqliteStatement & other );
95 
96  virtual bool build( winux::String const & sql ) override;
97  virtual bool build( winux::String const & sql, winux::Mixed const & params ) override;
98  virtual bool build( winux::String const & sql, winux::MixedArray const & params ) override;
99  virtual bool build( winux::String const & sql, winux::StringMixedMap const & params ) override;
100  virtual bool bind( size_t paramIndex, winux::Mixed const & val ) override;
101  virtual bool bind( winux::String const & paramName, winux::Mixed const & val ) override;
102 
103  operator sqlite3_stmt * () const { return _stmt; }
104  SqliteConnection * getCnn() const { return _cnn; }
105  winux::String getSql() const { return _sql; }
106 
107 private:
108  void _free();
109 
110  SqliteConnection * _cnn;
111  sqlite3_stmt * _stmt;
112  winux::String _sql;
113  std::vector< std::pair< size_t, winux::Mixed > > _bindingParams; // 已绑定的参数
114  friend class SqliteResult;
115 
117 };
118 
120 {
121 public:
123  virtual ~SqliteResult();
124 
125  virtual bool fetchRow( winux::Mixed * fields, int type = 0 ) override;
126  virtual bool fetchRow( winux::MixedArray * fields ) override;
127  virtual bool fetchRow( winux::StringMixedMap * fields ) override;
128  virtual winux::String fieldName( size_t fieldIndex ) override;
129  virtual size_t fieldsCount() override;
130  virtual size_t rowsCount() override;
131  virtual bool free() override;
132  virtual winux::String fieldType( size_t fieldIndex ) override;
133 
134  // 重置结果集,以便再次fetch
135  bool reset();
136  int retCode() const { return _retCode; }
137 private:
139  int _retCode;
140  bool _first;
141  bool _hasRow;
142  winux::StringArray _fieldNames; // 非原始字段名
143 
145 };
146 
148 {
149 public:
150  SqliteModifier( SqliteConnection * cnn, winux::String const & tableName );
151  virtual ~SqliteModifier();
152 
153  /*virtual bool addNew( winux::StringArray const & fieldNames, winux::MixedArray const & values );
154  virtual bool addNew( winux::Mixed const & fields );
155  virtual bool modify( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::Mixed const & prkValue );
156  virtual bool modify( winux::Mixed const & fields, winux::Mixed const & prkValue );
157  virtual int modifyEx( winux::StringArray const & fieldNames, winux::MixedArray const & values, winux::String const & where );
158  virtual int modifyEx( winux::Mixed const & fields, winux::String const & where );
159  virtual bool deleteOne( winux::Mixed const & prkValue );
160  virtual int deleteEx( winux::String const & where ); */
161 
162 protected:
163  virtual void _getTableInfo() override;
164  /*SqliteConnection * _cnn;
165  winux::String _tableName;
166  winux::StringArray _fieldNames; // 原始字段名
167  winux::String _prkName; // 主键名
168  //bool _prkAutoIncrement; // 自动增长 */
169 
171 };
172 
173 }
174 
175 #endif // __EIENDB_SQLITE_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
数据库连接接口
Definition: eiendb_base.hpp:63
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:233
winux::String getSql() const
SqliteDbError(int sqliteErrNo, winux::AnsiString const &s)
XString< tchar > String
Definition: utilities.hpp:216
SqliteConnection * getCnn() const
eiendb数据库错误
Definition: eiendb_base.hpp:41
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
SQLite数据库错误
Definition: eiendb_base.hpp:51
SQL语句接口,构造SQL语句,参数绑定
std::vector< Mixed > MixedArray
Definition: utilities.hpp:232
int getSqliteErrCode() const
数据结果操作接口
#define EIENDB_DLL
Definition: eiendb_base.hpp:25
unsigned int uint
Definition: utilities.hpp:170
混合体,能表示多种类型的值
Definition: utilities.hpp:750
数据库修改器部分通用实现
数据库通用接口
Definition: eiendb_base.hpp:7
数据库错误
文件接口
Definition: filesys.hpp:206
XStringArray< tchar > StringArray
Definition: utilities.hpp:227