fastdo  0.6.16
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 #ifdef HAVE_DB_SQLITE
12 
15 {
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 private:
21  int _sqliteErrNo;
22 };
23 
25 {
26 public:
27  SqliteConnection( winux::String const & path = "", winux::AnsiString const & dbKey = "", winux::String linkCharset = "", bool doConnect = true );
28  virtual ~SqliteConnection();
29 
30  virtual bool connect() override;
31  virtual bool close() override;
32  virtual void alive() override;
33  virtual bool setLinkCharset( winux::String const & charset ) override;
34  virtual bool selectDb( winux::String const & database ) override;
35  virtual bool createDb( winux::String const & database ) override;
36  virtual bool dropDb( winux::String const & database ) override;
37  virtual size_t affectedRows() override;
38  virtual size_t insertId() override;
39  virtual int errNo() override;
40  virtual winux::String error() override;
41  virtual winux::String escape( winux::String const & str, winux::String const & addQuote = "\'" ) override;
42  virtual winux::String escape( void const * buf, size_t size, winux::String const & addQuote = "\'" ) override;
43  virtual winux::SharedPointer<IDbResult> query( winux::String const & sql ) override;
45  virtual bool exec( winux::String const & sql ) override;
46  virtual bool exec( winux::SharedPointer<IDbStatement> stmt ) override;
47  virtual winux::SharedPointer<IDbResult> listDbs() override;
48  virtual winux::SharedPointer<IDbResult> listFields( winux::String const & tableName ) override;
49  virtual winux::SharedPointer<IDbResult> listTables() override;
50  virtual winux::String tableDdl( winux::String const & tableName ) override;
51  virtual winux::String symbolQuotes( winux::String const & str, bool periodAsSeparator = true ) override;
52  virtual size_t loadSqlFile( winux::IFile * sqlFile, winux::StringArray * arrSql ) override;
53  virtual size_t getPrimaryKey( winux::String const & tableName, winux::StringArray * arrKeyColumn ) override;
54  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::Mixed const & params = winux::mxNull ) override;
55  virtual operator bool() const override { return _db != NULL; }
56 
57  operator sqlite3 * () const { return _db; }
58  // SQLite字符串转到winux::String,NULL也能正确处理
59  winux::String sqliteStrToString( char const * s );
60  // winux::String转到SQLite字符串
61  winux::AnsiString stringToSqliteStr( winux::String const & s );
62 
63 private:
64  bool _open( winux::String const & database, winux::uint flags );
65 
66  sqlite3 * _db;
67  winux::String _path; // 输入的路径参数
68  winux::String _dirPath; // 解析到的目录路径
69  winux::String _database; // 解析到的数据库名
70  winux::String _linkCharset; // 连接校验字符集
71  winux::AnsiString _dbKey; // 数据库密码,可以为二进制数据
72 
74 };
75 
77 {
78 public:
80  SqliteStatement( SqliteConnection * cnn, winux::String const & sql, winux::Mixed const & params = winux::mxNull );
81  virtual ~SqliteStatement();
82 
83  virtual bool build( winux::String const & sql, winux::Mixed const & params = winux::mxNull ) override;
84  virtual bool bind( size_t paramIndex, winux::Mixed const & val ) override;
85  virtual bool bind( winux::String const & paramName, winux::Mixed const & val ) override;
86 
87  operator sqlite3_stmt * () const { return _stmt; }
88  SqliteConnection * getCnn() const { return _cnn; }
89  winux::String getSql() const { return _sql; }
90 
91 private:
92  void _free();
93 
94  SqliteConnection * _cnn;
95  sqlite3_stmt * _stmt;
96  winux::String _sql;
97  std::vector< std::pair< size_t, winux::Mixed > > _bindingParams; // 已绑定的参数
98  friend class SqliteResult;
99 
101 };
102 
104 {
105 public:
107  virtual ~SqliteResult();
108  virtual bool free() override;
109 
110  virtual bool fetchRow( winux::Mixed * fields, int type = 0 ) override;
111  virtual bool fetchArray( winux::MixedArray * fields ) override;
112  virtual bool fetchMap( winux::StringMixedMap * fields ) override;
113  virtual winux::String fieldName( size_t fieldIndex ) override;
114  virtual size_t fieldsCount() override;
115  virtual size_t rowsCount() override;
116  virtual winux::String fieldType( size_t fieldIndex ) override;
117 
118  // 重置结果集,以便再次fetch
119  bool reset();
120  int retCode() const { return _retCode; }
121 
122 private:
124  int _retCode;
125  bool _first;
126  bool _hasRow;
127  winux::StringArray _fieldNames; // 非原始字段名
128 
130 };
131 
133 {
134 public:
135  SqliteModifier( SqliteConnection * cnn, winux::String const & tableName );
136  virtual ~SqliteModifier();
137 
139 };
140 
141 #endif // HAVE_DB_SQLITE
142 
143 } // namespace eiendb
144 
145 #endif // __EIENDB_SQLITE_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
数据库连接接口
Definition: eiendb_base.hpp:65
Mixed const mxNull
Mixed(MT_NULL)常量对象
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:278
winux::String getSql() const
SqliteDbError(int sqliteErrNo, winux::AnsiString const &s)
XString< tchar > String
Definition: utilities.hpp:261
SqliteConnection * getCnn() const
eiendb数据库错误
Definition: eiendb_base.hpp:42
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
SQLite数据库错误
Definition: eiendb_base.hpp:53
SQL语句接口。构造SQL语句、参数绑定
引用计数共享指针
Definition: smartptr.hpp:456
std::vector< Mixed > MixedArray
Definition: utilities.hpp:277
int getSqliteErrCode() const
数据结果操作接口
#define EIENDB_DLL
Definition: eiendb_base.hpp:25
unsigned int uint
Definition: utilities.hpp:215
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
数据库修改器类通用实现
数据库通用接口
Definition: eiendb_base.hpp:7
SQLite数据库错误
文件接口
Definition: filesys.hpp:313
XStringArray< tchar > StringArray
Definition: utilities.hpp:272