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

520 lines
16 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
#include <vector>
2017-06-02 14:47:24 +08:00
#include "../stdlib/string.hpp"
#include "../stdlib/noncopyable.hpp"
2017-06-02 14:47:24 +08:00
#include "../connpool/connect_client.hpp"
2014-11-19 00:25:21 +08:00
#if !defined(ACL_DB_DISABLE)
2014-11-19 00:25:21 +08:00
namespace acl {
/**
*
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API db_row : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
/**
*
* @param names {const std::vector<const char*>&}
2014-11-19 00:25:21 +08:00
*/
db_row(const std::vector<const char*>& names);
~db_row(void);
2014-11-19 00:25:21 +08:00
/**
*
* @param ifield {size_t}
* @return {const char*}
2014-11-19 00:25:21 +08:00
*/
const char* field_name(size_t ifield) const;
/**
*
* @param name {const char*}
* @return {const char*}
*
2014-11-19 00:25:21 +08:00
*/
const char* field_value(const char* name) const;
/**
*
* field_value
* @param name {const char*}
* @return {const char*}
*
2014-11-19 00:25:21 +08:00
*/
const char* operator[](const char* name) const;
/**
*
* @param ifield {size_t} <
* @return {const char*}
*
2014-11-19 00:25:21 +08:00
*/
const char* field_value(size_t ifield) const;
/**
* field_value
* @param ifield {size_t} <
* @return {const char*}
*
2014-11-19 00:25:21 +08:00
*/
const char* operator[](size_t ifield) const;
/**
*
* @param ifield {size_t}
* @param null_value {int}
* @return {int} null_value
2014-11-19 00:25:21 +08:00
*/
int field_int(size_t ifield, int null_value = 0) const;
/**
*
* @param name {const char*}
* @param null_value {int}
* @return {int} null_value
2014-11-19 00:25:21 +08:00
*/
int field_int(const char* name, int null_value = 0) const;
/**
*
* @param ifield {size_t}
* @param null_value {acl_int64}
* @return {acl_int64} null_value
*
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
__int64 field_int64(size_t ifield, __int64 null_value = 0) const;
#else
long long int field_int64(size_t ifield,
long long int null_value = 0) const;
2014-11-19 00:25:21 +08:00
#endif
/**
*
* @param name {const char*}
* @param null_value {acl_int64}
* @return {acl_int64} null_value
*
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
__int64 field_int64(const char* name, __int64 null_value = 0) const;
#else
long long int field_int64(const char* name,
long long int null_value = 0) const;
2014-11-19 00:25:21 +08:00
#endif
/**
*
* @param ifield {size_t}
* @param null_value {double}
* @return {double} null_value
*
*/
double field_double(size_t ifield, double null_value = 0.0) const;
/**
*
* @param name {const char*}
* @param null_value {double}
* @return {double} null_value
*
*/
double field_double(const char* name, double null_value = 0.0) const;
2014-11-19 00:25:21 +08:00
/**
*
* @param ifield {size_t}
* @return {const char*} NULL
2014-11-19 00:25:21 +08:00
*/
const char* field_string(size_t ifield) const;
/**
*
* @param name {const char*}
* @return {const char*} NULL
2014-11-19 00:25:21 +08:00
*/
const char* field_string(const char* name) const;
/**
*
* @param ifield {size_t}
* @return {size_t}
*/
size_t field_length(size_t ifield) const;
/**
*
* @param name {const char*}
* @return {size_t}
*/
size_t field_length(const char* name) const;
2014-11-19 00:25:21 +08:00
/**
*
* @param value {const char*}
* @param len {size_t} value
2014-11-19 00:25:21 +08:00
*/
void push_back(const char* value, size_t len);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
* @return {size_t}
*/
size_t length(void) const;
/**
* values_
*/
void clear(void);
2014-11-19 00:25:21 +08:00
private:
// 数据表的字段名集合的引用
2014-11-19 00:25:21 +08:00
const std::vector<const char*>& names_;
// 数据结果行的字段集合
2014-11-19 00:25:21 +08:00
std::vector<const char*> values_;
// 数据结果行字段长度集合
std::vector<size_t> lengths_;
2014-11-19 00:25:21 +08:00
};
/**
*
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API db_rows : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
db_rows();
virtual ~db_rows();
/**
*
* @param name {const char*} ()
* @param value {const char*} ()
* @return {const std::vector<const db_row*>&}
* db_rows.empty()
2014-11-19 00:25:21 +08:00
*/
const std::vector<const db_row*>& get_rows(
const char* name, const char* value);
/**
*
* @return {const std::vector<db_row*>&}
* db_rows.empty()
2014-11-19 00:25:21 +08:00
*/
const std::vector<db_row*>& get_rows() const;
/**
*
* @param idx {size_t} <
* @return {const db_row*}
*
2014-11-19 00:25:21 +08:00
*/
const db_row* operator[](size_t idx) const;
/**
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool empty() const;
/**
*
* @return {size_t}
2014-11-19 00:25:21 +08:00
*/
size_t length() const;
public:
// 数据表字段名
2014-11-19 00:25:21 +08:00
std::vector<const char*> names_;
// 查询结果行集合,其中的元素 db_row 必须是动态添加进去的,
// 因为在本类对象析构时会自动 delete rows_ 中的所有元素对象
2014-11-19 00:25:21 +08:00
std::vector<db_row*> rows_;
// 临时结果行集合
2014-11-19 00:25:21 +08:00
std::vector<const db_row*> rows_tmp_;
// 存储临时结果集对象
void* result_tmp_;
// 用来释放临时结果集对象
void (*result_free)(void* result);
2014-11-19 00:25:21 +08:00
};
class db_pool;
class query;
/**
*
2014-11-19 00:25:21 +08:00
*/
2015-08-12 16:48:24 +08:00
class ACL_CPP_API db_handle : public connect_client
2014-11-19 00:25:21 +08:00
{
public:
db_handle(void);
2014-11-19 00:25:21 +08:00
virtual ~db_handle(void);
2015-08-12 16:48:24 +08:00
/////////////////////////////////////////////////////////////////////
/**
* connect_client
* @return {bool}
2015-08-12 16:48:24 +08:00
*/
bool open();
/////////////////////////////////////////////////////////////////////
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
* @return {const char*}
*/
virtual const char* dbtype() const = 0;
/**
*
2014-11-19 00:25:21 +08:00
* @return {int}
*/
virtual int get_errno() const
{
return -1;
}
/**
*
2014-11-19 00:25:21 +08:00
* @return {const char*}
*/
virtual const char* get_error() const
{
return "unkonwn error";
}
/**
*
* @param charset {const char*}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
2015-11-09 22:11:21 +08:00
virtual bool dbopen(const char* charset = NULL) = 0;
2014-11-19 00:25:21 +08:00
/**
*
* @return {bool} true
2014-11-19 00:25:21 +08:00
*/
virtual bool is_opened() const = 0;
/**
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
virtual bool tbl_exists(const char* tbl_name) = 0;
/**
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
virtual bool close() = 0;
/**
* SELECT SQL
* @param sql {const char*} SQL
* SQL SQL
* @param result {db_rows*}
* db_handle
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
virtual bool sql_select(const char* sql, db_rows* result = NULL) = 0;
2014-11-19 00:25:21 +08:00
/**
* INSERT/UPDATE/DELETE SQL
* @param sql {const char*} SQL
* SQL SQL
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
virtual bool sql_update(const char* sql) = 0;
/**
*
* @return {bool}
*/
virtual bool begin_transaction() { return false; }
/**
*
* @return {bool}
*/
virtual bool commit() { return false; }
/**
*
* @return {bool}
*/
virtual bool rollback() { return false; }
/**
* sql_select
* query sql sql
* SELECT SQL
2014-11-19 00:25:21 +08:00
* @param query {query&}
* @param result {db_rows*}
* db_handle
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool exec_select(query& query, db_rows* result = NULL);
2014-11-19 00:25:21 +08:00
/**
* sql_update
* query sql sql
* INSERT/UPDATE/DELETE SQL
2014-11-19 00:25:21 +08:00
* @param query {query&}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool exec_update(query& query);
/**
* sql
*
*
* @param in {const char*}
* @param len {size_t}
* @param out {string&}
* @return {string&} 便
* SQL 便
2014-11-19 00:25:21 +08:00
*/
virtual string& escape_string(const char* in, size_t len, string& out);
/**
* sql
* @return {int} -1
2014-11-19 00:25:21 +08:00
*/
virtual int affect_count() const = 0;
/////////////////////////////////////////////////////////////////////
/**
* SQL
* @return {const db_rows*}
* free_result()
2014-11-19 00:25:21 +08:00
*/
const db_rows* get_result() const;
/**
*
* @param name {const char*} ()
* @param value {const char*} ()
* @return {const std::vector<const db_row*>*}
* free_result()
2014-11-19 00:25:21 +08:00
*/
const std::vector<const db_row*>* get_rows(
const char* name, const char* value);
/**
*
* @return {const std::vector<db_row*>*}
* free_result()
2014-11-19 00:25:21 +08:00
*/
const std::vector<db_row*>* get_rows() const;
/**
* SQL 便
* @return {const db_row*}
* free_result()
2014-11-19 00:25:21 +08:00
*/
const db_row* get_first_row() const;
/**
*
* result_ ,
* SQL
*
*
2014-11-19 00:25:21 +08:00
*/
void free_result();
/**
*
* @param idx {size_t}
* @return {const db_row*}
*
2014-11-19 00:25:21 +08:00
*/
const db_row* operator[](size_t idx) const;
/**
* (sql_select)
* @return {size_t} 0
2014-11-19 00:25:21 +08:00
*/
size_t length() const;
/**
* (sql_select)
* @return {bool} true
2014-11-19 00:25:21 +08:00
*/
bool empty() const;
/**
*
* @param max {size_t} 0
*
2014-11-19 00:25:21 +08:00
*/
void print_out(size_t max = 0) const;
/////////////////////////////////////////////////////////////////
/**
* ID
* @param id {const char*} ID
2014-11-19 00:25:21 +08:00
* @return {db_handle&}
*/
db_handle& set_id(const char* id);
/**
* ID
* @return {const char*} ID
2014-11-19 00:25:21 +08:00
*/
const char* get_id() const
{
return id_;
}
/**
* 使
2014-11-19 00:25:21 +08:00
* @param now {time_t}
* @return {db_handle&}
*/
db_handle& set_when(time_t now);
/**
* 使
2014-11-19 00:25:21 +08:00
* @return {time_t}
*/
time_t get_when() const
{
return when_;
}
/**
* 使
*/
static void set_loadpath(const char* path);
/**
*
* @return {const char*} NULL
*/
static const char* get_loadpath();
2014-11-19 00:25:21 +08:00
protected:
// 临时结果对象
2014-11-19 00:25:21 +08:00
db_rows* result_;
// 实例唯一 ID
2014-11-19 00:25:21 +08:00
char* id_;
// 该数据库连接句柄最近被使用的时间
2014-11-19 00:25:21 +08:00
time_t when_;
};
} // namespace acl
#endif // !defined(ACL_DB_DISABLE)