fastdo  0.6.16
eiendb_pgsql.hpp
浏览该文件的文档.
1 #ifndef __EIENDB_PGSQL_HPP__
2 #define __EIENDB_PGSQL_HPP__
3 
4 #include "eiendb_base.hpp"
5 
6 namespace eiendb
7 {
8 #ifdef HAVE_DB_PGSQL
9 
12 {
13 public:
14  PgsqlDbError( int pgsqlErrNo, winux::AnsiString const & s ) throw() : DbError( DbError::dbePgsqlError, s ), _pgsqlErrNo(pgsqlErrNo) { }
15  int getPgsqlErrCode() const throw() { return _pgsqlErrNo; }
16 
17 private:
18  int _pgsqlErrNo;
19 };
20 
22 {
23 public:
36  winux::String const & host,
37  winux::String const & user,
38  winux::String const & pwd,
39  winux::String const & dbName,
40  winux::String const & linkCharset,
41  winux::String const & schema = "public",
42  winux::String const & options = "",
43  winux::Mixed * dbConfig = nullptr,
44  bool doConnect = true
45  );
46  virtual ~PgsqlConnection();
47 
48  virtual bool connect() override;
49  virtual bool close() override;
50  virtual void alive() override;
51  virtual bool selectDb( winux::String const & database ) override;
52  virtual bool setLinkCharset( winux::String const & charset ) override;
53  virtual bool createDb( winux::String const & database ) override;
54  virtual bool dropDb( winux::String const & database ) override;
55  virtual size_t affectedRows() override;
56  virtual size_t insertId() override;
57  virtual int errNo() override;
58  virtual winux::String error() override;
59  virtual winux::SharedPointer<IDbResult> query( winux::String const & sql ) override;
61  virtual bool exec( winux::String const & sql ) override;
62  virtual bool exec( winux::SharedPointer<IDbStatement> stmt ) override;
63  virtual winux::String escape( winux::String const & str, winux::String const & addQuote = "\'" ) override;
64  virtual winux::String escape( void const * buf, size_t size, winux::String const & addQuote = "\'" ) override;
65  virtual winux::SharedPointer<IDbResult> listDbs() override;
66  virtual winux::SharedPointer<IDbResult> listFields( winux::String const & tableName ) override;
67  virtual winux::SharedPointer<IDbResult> listTables() override;
68  virtual winux::String tableDdl( winux::String const & tableName ) override;
69  virtual winux::String symbolQuotes( winux::String const & str, bool periodAsSeparator = true ) override;
70  virtual size_t loadSqlFile( winux::IFile * sqlFile, winux::StringArray * arrSql ) override;
71  virtual size_t getPrimaryKey( winux::String const & tableName, winux::StringArray * arrKeyColumn ) override;
72  virtual winux::SharedPointer<IDbStatement> buildStmt( winux::String const & sql, winux::Mixed const & params = winux::mxNull ) override;
73  virtual operator bool() const override;
74 
75  operator DbHandle() const;
76 
77 private:
78  winux::String _host; // 主机(可多个,逗号分隔)
79  winux::String _port; // 端口(可多个,逗号分隔)
80  winux::String _user; // 用户名
81  winux::String _pwd; // 密码
82  winux::String _dbName; // 数据库名
83  winux::String _linkCharset; // 连接校验字符集
84  winux::String _schema; // 模式名
85  winux::String _options; // 发送给服务器的命令行选项 eg. "-c geqo=off"
86  size_t _affectedRows; // 受影响的行数
87  size_t _insertId; // 最后插入的ID
88  winux::Mixed * _dbConfig; // 数据库特有配置 { single_row(单行查询模式): 0=默认多行模式 1=保持单行模式 2=一次单行模式 }
89 
91  friend class PgsqlResult;
92  friend class PgsqlModifier;
94 };
95 
97 {
98 public:
100  PgsqlStatement( PgsqlConnection * cnn, winux::String const & sql, winux::Mixed const & params = winux::mxNull ) : DbStatement( cnn, sql, params ) { }
101 
102  PgsqlConnection * getCnn() const { return static_cast<PgsqlConnection *>( this->DbStatement::getCnn() ); }
103 
105 };
106 
108 {
109 public:
110  PgsqlResult( winux::SharedPointer<PgsqlStatement> stmt, bool singleRow = false );
111  virtual ~PgsqlResult();
112  virtual bool free() override;
113 
114  virtual bool dataSeek( size_t index ) override;
115  virtual bool fetchRow( winux::Mixed * fields, int type = 0 ) override;
116  virtual bool fetchArray( winux::MixedArray * fields ) override;
117  virtual bool fetchMap( winux::StringMixedMap * fields ) override;
118  virtual winux::String fieldName( size_t fieldIndex ) override;
119  virtual size_t fieldsCount() override;
120  virtual size_t rowsCount() override;
121  virtual winux::String fieldType( size_t fieldIndex ) override;
122 
123  bool reset();
124  operator bool() const;
125 
126 private:
128  size_t _curRowIndex; // 多行模式的当前行
129  size_t _rowsCount; // 多行模式的行数
130  bool _singleRow; // 是否单行模式
131  bool _hadFetch; // 是否完成fetch。单行模式必须fetch完成才可以进行下次查询
134 };
135 
137 {
138 public:
139  PgsqlModifier( PgsqlConnection * cnn, winux::String const & tableName );
140  virtual ~PgsqlModifier();
141 
142 protected:
143  virtual bool _execInsertInto( winux::SharedPointer<IDbStatement> stmt ) override;
144 
146 };
147 
148 #endif // HAVE_DB_PGSQL
149 
150 } // namespace eiendb
151 
152 #endif // __EIENDB_PGSQL_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
数据库连接接口
Definition: eiendb_base.hpp:65
void * DbHandle
数据库各种对象资源的指针类型
Definition: eiendb_base.hpp:38
PgsqlConnection * getCnn() const
Mixed const mxNull
Mixed(MT_NULL)常量对象
std::map< String, Mixed > StringMixedMap
Definition: utilities.hpp:278
IDbConnection * getCnn() const
XString< tchar > String
Definition: utilities.hpp:261
PostgreSQL数据库错误
eiendb数据库错误
Definition: eiendb_base.hpp:42
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
PgsqlStatement(PgsqlConnection *cnn, winux::String const &sql, winux::Mixed const &params=winux::mxNull)
引用计数共享指针
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
PgsqlDbError(int pgsqlErrNo, winux::AnsiString const &s)
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
int getPgsqlErrCode() const
数据库修改器类通用实现
数据库通用接口
Definition: eiendb_base.hpp:7
数据库语句类通用实现
文件接口
Definition: filesys.hpp:313
PgsqlStatement(PgsqlConnection *cnn)
XStringArray< tchar > StringArray
Definition: utilities.hpp:272