MiniEngine/MiniEngine_SQLite.cpp

107 lines
1.9 KiB
C++
Raw Normal View History

2017-04-16 20:24:54 +08:00
#include "MiniEngine_SQLite.h"
2017-04-16 16:00:44 +08:00
#include <cstring>
#include <cstdio>
namespace MiniEngine
{
namespace SQL
{
// private
void SQLStatement::_set(sqlite3_stmt* p)
2017-04-16 16:00:44 +08:00
{
_st.reset(p,sqlite3_finalize);
2017-04-16 16:00:44 +08:00
}
// private
sqlite3_stmt* SQLStatement::_get() const
2017-04-16 16:00:44 +08:00
{
return _st.get();
2017-04-16 16:00:44 +08:00
}
void SQLStatement::_setStmt(sqlite3_stmt* p)
{
_set(p);
2017-04-16 16:00:44 +08:00
}
sqlite3_stmt* SQLStatement::_getStmt() const
{
return _get();
2017-04-16 16:00:44 +08:00
}
bool SQLStatement::isReady() const
{
return _get()!=nullptr;
}
2017-04-16 16:00:44 +08:00
int _global_sqldb_executor(void* ExParam,int colNum,char** colVal,char** colName)
{
auto p=reinterpret_cast<std::function<int(int,char**,char**)>*>(ExParam);
return (*p)(colNum,colVal,colName);
}
void SQLDB::_set(sqlite3* p)
2017-04-16 16:00:44 +08:00
{
_db.reset(p,sqlite3_close);
2017-04-16 16:00:44 +08:00
}
sqlite3* SQLDB::_get()
2017-04-16 16:00:44 +08:00
{
return _db.get();
2017-04-16 16:00:44 +08:00
}
int SQLDB::open(const std::string& filename)
{
sqlite3* _temp=nullptr;
int ret=sqlite3_open(filename.c_str(),&_temp);
_set(_temp);
return ret;
2017-04-16 16:00:44 +08:00
}
SQLStatement SQLDB::prepare(const std::string& SQLCommand)
{
SQLStatement stmt;
sqlite3_stmt* pstmt=nullptr;
int ret=sqlite3_prepare(_get(),SQLCommand.c_str(),SQLCommand.size(),&pstmt,NULL);
2017-04-16 16:00:44 +08:00
if(ret<0) return stmt;
stmt._setStmt(pstmt);
return stmt;
}
int SQLDB::step(const SQLStatement& Statement)
{
return sqlite3_step(Statement._getStmt());
}
int SQLDB::exec(const std::string& SQLCommand)
{
return _exec_real(SQLCommand,nullptr,nullptr);
}
2017-04-22 10:33:43 +08:00
int SQLDB::exec_raw(const std::string& SQLCommand,SQLCallback callback,void* param)
{
return _exec_real(SQLCommand,callback,param);
}
int SQLDB::_exec_real(const std::string& SQLCommand,SQLCallback callback,void* param)
2017-04-16 16:00:44 +08:00
{
return sqlite3_exec(_get(),SQLCommand.c_str(),callback,param,&_errmsg);
2017-04-16 16:00:44 +08:00
}
const char* SQLDB::getErrorMsg()
{
return _errmsg;
}
void SQLDB::clearError()
{
_errmsg=nullptr;
}
}/// End of namespace MiniEngine::SQL
}/// End of namespace MiniEngine