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

176 lines
3.7 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_DB_DISABLE)
2014-11-19 00:25:21 +08:00
typedef struct sqlite3 sqlite3;
namespace acl {
class charset_conv;
class sqlite_cursor;
2014-11-19 00:25:21 +08:00
class ACL_CPP_API db_sqlite : public db_handle
{
public:
/**
*
* @param charset {const char*} (gbk, utf-8, ...)
*/
db_sqlite(const char* dbfile, const char* charset = "utf-8");
2014-11-19 00:25:21 +08:00
~db_sqlite(void);
/**
* sqlite
2014-11-19 00:25:21 +08:00
*/
const char* version(void) const;
/**
*
* sqlite
* @param pragma {const char*}
2014-11-19 00:25:21 +08:00
* PRAGMA xxx=xxx
* PRAGMA synchronous = NORMAL
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set_conf(const char* pragma);
/**
*
* @param pragma {const char*}
2014-11-19 00:25:21 +08:00
* PRAGMA xxx
* 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);
/**
*
* @param pragma {const char*}
* PRAGMA xxxPRAGMA synchronous
2014-11-19 00:25:21 +08:00
*/
void show_conf(const char* pragma = NULL);
/**
*
* @return {int} -1
2014-11-19 00:25:21 +08:00
*/
int affect_total_count(void) const;
2014-11-19 00:25:21 +08:00
/**
* sqlite NULL sqlite
* sqlite
2014-11-19 00:25:21 +08:00
* @return {sqlite3*}
*/
sqlite3* get_conn(void) const
2014-11-19 00:25:21 +08:00
{
return db_;
}
/**
*
* @param cursor {sqlite_cursor&}
* @return {bool}
*/
bool prepare(sqlite_cursor& cursor);
/**
*
* @param cursor {sqlite_cursor&}
* @return {bool}
*/
bool next(sqlite_cursor& cursor, bool* done);
/********************************************************************/
/* 以下为基类 db_handle 的虚接口 */
/********************************************************************/
2014-11-19 00:25:21 +08:00
/**
* @override
2014-11-19 00:25:21 +08:00
*/
const char* dbtype(void) const;
2014-11-19 00:25:21 +08:00
/**
* @override
2014-11-19 00:25:21 +08:00
*/
int get_errno(void) const;
2014-11-19 00:25:21 +08:00
/**
* @override
2014-11-19 00:25:21 +08:00
*/
const char* get_error(void) const;
2014-11-19 00:25:21 +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
/**
* @override
2014-11-19 00:25:21 +08:00
*/
bool is_opened(void) const;
2014-11-19 00:25:21 +08:00
/**
* @override
2014-11-19 00:25:21 +08:00
*/
bool close(void);
2014-11-19 00:25:21 +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
/**
* @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
/**
* @override
2014-11-19 00:25:21 +08:00
*/
bool sql_update(const char* sql);
2014-11-19 00:25:21 +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
*/
bool set_busy_timeout(int nMillisecs);
2014-11-19 00:25:21 +08:00
private:
// sqlite 引擎
2014-11-19 00:25:21 +08:00
sqlite3* db_;
// 数据存储文件
2014-11-19 00:25:21 +08:00
string dbfile_;
// 字符集转码器
2014-11-19 00:25:21 +08:00
charset_conv* conv_;
// 本地字符集
string charset_;
2014-11-19 00:25:21 +08:00
// 真正执行SQL查询的函数
bool exec_sql(const char* sql, db_rows* result = NULL);
2014-11-19 00:25:21 +08:00
};
} // namespace acl
#endif // !defined(ACL_DB_DISABLE)