acl/lib_acl_cpp/include/acl_cpp/db/db_mysql.hpp

168 lines
3.8 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "../stdlib/string.hpp"
#include "../db/db_handle.hpp"
2014-11-19 00:25:21 +08:00
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_DB_DISABLE)
2014-11-19 00:25:21 +08:00
typedef struct st_mysql MYSQL;
namespace acl {
class mysql_conf;
2014-11-19 00:25:21 +08:00
class ACL_CPP_API db_mysql : public db_handle
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽһ
* @param dbaddr {const char*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ TCP <EFBFBD>׽ӿڻ<EFBFBD><EFBFBD><EFBFBD> UNIX
* ƽ̨<EFBFBD>µ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿڣ<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>127.0.0.1:3306<EFBFBD><EFBFBD><EFBFBD><EFBFBD> /tmp/mysql.sock
* @param dbname {const char*} <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD> NULL
* @param dbuser {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>
* @param dbpass {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param dbflags {unsigned long} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> MYSQL ʱ<EFBFBD>ı<EFBFBD>־λ
* @param auto_commit {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param conn_timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD>ij<EFBFBD>ʱʱ<EFBFBD><EFBFBD>
* @param rw_timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ij<EFBFBD>ʱʱ<EFBFBD><EFBFBD>
* @param charset {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>ʱ<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>gbk, utf8, ...<EFBFBD><EFBFBD>
*/
2014-11-19 00:25:21 +08:00
db_mysql(const char* dbaddr, const char* dbname,
const char* dbuser, const char* dbpass,
unsigned long dbflags = 0, bool auto_commit = true,
int conn_timeout = 60, int rw_timeout = 60,
const char* charset = "utf8");
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>
* @param conf {const mysql_conf&} mysql <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
db_mysql(const mysql_conf& conf);
2014-11-19 00:25:21 +08:00
~db_mysql(void);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> mysql <EFBFBD>ͻ<EFBFBD><EFBFBD>˿<EFBFBD><EFBFBD>İ<EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
* @return {unsigned long}
*/
unsigned long mysql_libversion(void) const;
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> mysql <EFBFBD>ͻ<EFBFBD><EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2014-11-19 00:25:21 +08:00
* @return {const char*}
*/
const char* mysql_client_info(void) const;
2014-11-19 00:25:21 +08:00
/**
* ֱ<EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD> mysql <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ mysql <EFBFBD><EFBFBD>û<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ڲ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD> mysql <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
* @return {MYSQL*}
*/
MYSQL* get_conn(void) const
2014-11-19 00:25:21 +08:00
{
return conn_;
}
2017-04-14 21:04:33 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD> libmysqlclient.so / libmysqlclient.dll ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><EFBFBD>ñ<EFBFBD>
* <EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD> mysql <EFBFBD>ͻ<EFBFBD><EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>ɲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ db_mysql <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>
* ʹ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> mysql <EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD>
2017-04-14 21:04:33 +08:00
*/
static void load(void);
2015-08-12 16:48:24 +08:00
/********************************************************************/
/* <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> db_handle <20><><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD> */
2015-08-12 16:48:24 +08:00
/********************************************************************/
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
2017-04-27 21:46:28 +08:00
const char* dbtype(void) const;
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
2017-04-27 21:46:28 +08:00
int get_errno(void) const;
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
2017-04-27 21:46:28 +08:00
const char* get_error(void) const;
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
2015-11-09 22:11:21 +08:00
bool dbopen(const char* charset = NULL);
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
2017-04-27 21:46:28 +08:00
bool is_opened(void) const;
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
bool close(void);
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
bool tbl_exists(const char* tbl_name);
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
bool sql_select(const char* sql, db_rows* result = NULL);
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
bool sql_update(const char* sql);
2014-11-19 00:25:21 +08:00
/**
2017-04-27 21:46:28 +08:00
* @override
2014-11-19 00:25:21 +08:00
*/
2017-04-27 21:46:28 +08:00
int affect_count(void) const;
/**
* @override
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> db_handle <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>ʼ<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD> db_mysql <EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD> auto_commit Ϊ false
*/
2017-04-27 21:46:28 +08:00
bool begin_transaction(void);
/**
* @override
*/
2017-04-27 21:46:28 +08:00
bool commit(void);
/**
* @override
*/
2017-04-27 21:46:28 +08:00
bool rollback(void);
2014-11-19 00:25:21 +08:00
private:
char* dbaddr_; // <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
char* dbname_; // <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>
char* dbuser_; // <20><><EFBFBD>ݿ<EFBFBD><DDBF>˺<EFBFBD>
char* dbpass_; // <20><><EFBFBD>ݿ<EFBFBD><DDBF>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD>
string charset_; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>ַ<EFBFBD><D6B7><EFBFBD>
2014-11-19 00:25:21 +08:00
unsigned long dbflags_;
2017-04-27 21:46:28 +08:00
int conn_timeout_;
int rw_timeout_;
bool auto_commit_;
2014-11-19 00:25:21 +08:00
MYSQL* conn_;
bool sane_mysql_query(const char* sql);
void sane_mysql_init(const char* dbaddr, const char* dbname,
const char* dbuser, const char* dbpass,
unsigned long dbflags, bool auto_commit,
int conn_timeout, int rw_timeout,
const char* charset);
2014-11-19 00:25:21 +08:00
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_DB_DISABLE)