2019-07-27 22:44:32 +08:00
|
|
|
|
#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
|
|
|
|
|
2019-05-19 16:54:20 +08:00
|
|
|
|
#if !defined(ACL_DB_DISABLE)
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
typedef struct sqlite3 sqlite3;
|
|
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
|
|
|
|
|
class charset_conv;
|
2018-09-02 20:38:33 +08:00
|
|
|
|
class sqlite_cursor;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
class ACL_CPP_API db_sqlite : public db_handle
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-10-25 14:15:07 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 构造函数
|
|
|
|
|
* @param charset {const char*} 本地字符集(gbk, utf-8, ...)
|
2015-10-25 14:15:07 +08:00
|
|
|
|
*/
|
|
|
|
|
db_sqlite(const char* dbfile, const char* charset = "utf-8");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
~db_sqlite(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 返回当前的 sqlite 的版本信息
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
const char* version(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当数据库打开后通过此函数对数据库的操作引擎进行配置,
|
|
|
|
|
* 进行配置的内容需要严格遵循 sqlite 本身的配置选项要求
|
|
|
|
|
* @param pragma {const char*} 配置选项内容,格式为:
|
2014-11-19 00:25:21 +08:00
|
|
|
|
* PRAGMA xxx=xxx
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 如:PRAGMA synchronous = NORMAL
|
|
|
|
|
* @return {bool} 配置数据库是否成功
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
bool set_conf(const char* pragma);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当数据库打开调用此函数获得数据引擎的配置选项
|
|
|
|
|
* @param pragma {const char*} 配置选项内容,格式为:
|
2014-11-19 00:25:21 +08:00
|
|
|
|
* PRAGMA xxx
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 如:PRAGMA synchronous
|
|
|
|
|
* @param out {string&} 如果返回值非空则存储结果
|
|
|
|
|
* @return {const char*} 为空则说明该配置不存在或数据库未打开
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
const char* get_conf(const char* pragma, string& out);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 在数据库打开的情况下输入数据库引擎的配置选项
|
|
|
|
|
* @param pragma {const char*} 指定的配置选项,如果该参数为空,
|
|
|
|
|
* 则输出所有的配置选项,格式为:PRAGMA xxx,如:PRAGMA synchronous
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
|
|
|
|
void show_conf(const char* pragma = NULL);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 自数据库打开后所有的影响的记录行数
|
|
|
|
|
* @return {int} 影响的行数,-1 表示出错
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
int affect_total_count(void) const;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 直接获得 sqlite 的句柄,如果返回 NULL 则表示 sqlite 还没有打开
|
|
|
|
|
* 或出错时内部自动关闭了 sqlite
|
2014-11-19 00:25:21 +08:00
|
|
|
|
* @return {sqlite3*}
|
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
sqlite3* get_conn(void) const
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
return db_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-02 20:38:33 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 准备游标
|
2018-09-02 20:38:33 +08:00
|
|
|
|
* @param cursor {sqlite_cursor&}
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool prepare(sqlite_cursor& cursor);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 执行下一步,如果是查询类过程,则将查询结果存入给定的参数中
|
2018-09-02 20:38:33 +08:00
|
|
|
|
* @param cursor {sqlite_cursor&}
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool next(sqlite_cursor& cursor, bool* done);
|
|
|
|
|
|
2016-10-24 11:44:12 +08:00
|
|
|
|
/********************************************************************/
|
2019-07-27 22:44:32 +08:00
|
|
|
|
/* 以下为基类 db_handle 的虚接口 */
|
2016-10-24 11:44:12 +08:00
|
|
|
|
/********************************************************************/
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
const char* dbtype(void) const;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
int get_errno(void) const;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
const char* get_error(void) const;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +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-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
bool is_opened(void) const;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-02 18:21:24 +08:00
|
|
|
|
bool close(void);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-02 18:21:24 +08:00
|
|
|
|
bool tbl_exists(const char* tbl_name);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-03-25 23:05:44 +08:00
|
|
|
|
bool sql_select(const char* sql, db_rows* result = NULL);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-02 18:21:24 +08:00
|
|
|
|
bool sql_update(const char* sql);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2017-06-28 12:25:29 +08:00
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
int affect_count(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
bool begin_transaction(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
bool commit(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @override
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2017-06-28 12:25:29 +08:00
|
|
|
|
bool set_busy_timeout(int nMillisecs);
|
2015-08-02 18:21:24 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
private:
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// sqlite 引擎
|
2014-11-19 00:25:21 +08:00
|
|
|
|
sqlite3* db_;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 数据存储文件
|
2014-11-19 00:25:21 +08:00
|
|
|
|
string dbfile_;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 字符集转码器
|
2014-11-19 00:25:21 +08:00
|
|
|
|
charset_conv* conv_;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 本地字符集
|
2015-10-25 14:15:07 +08:00
|
|
|
|
string charset_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 真正执行SQL查询的函数
|
2017-03-25 23:05:44 +08:00
|
|
|
|
bool exec_sql(const char* sql, db_rows* result = NULL);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|
2019-05-19 16:54:20 +08:00
|
|
|
|
|
|
|
|
|
#endif // !defined(ACL_DB_DISABLE)
|