2017-04-16 16:00:44 +08:00
|
|
|
#pragma once
|
|
|
|
#include "sqlite/sqlite3.h"
|
|
|
|
#include <string>
|
2017-04-16 17:32:39 +08:00
|
|
|
#include <memory>
|
2017-04-16 16:00:44 +08:00
|
|
|
|
2017-04-16 18:15:17 +08:00
|
|
|
namespace MiniEngine
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace SQL
|
|
|
|
{
|
|
|
|
|
2017-04-16 16:00:44 +08:00
|
|
|
class SQLStatement
|
|
|
|
{
|
|
|
|
public:
|
2017-04-16 17:32:39 +08:00
|
|
|
bool isReady() const;
|
|
|
|
SQLStatement()=default;
|
|
|
|
~SQLStatement()=default;
|
2017-04-16 16:00:44 +08:00
|
|
|
|
|
|
|
void _setStmt(sqlite3_stmt*);
|
|
|
|
sqlite3_stmt* _getStmt() const;
|
|
|
|
private:
|
2017-04-16 17:32:39 +08:00
|
|
|
sqlite3_stmt* _get() const;
|
|
|
|
void _set(sqlite3_stmt*);
|
|
|
|
std::shared_ptr<sqlite3_stmt> _st;
|
2017-04-16 16:00:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
using SQLCallback = int (*)(void* ExParam,int colNum,char** colVal,char** colName);
|
|
|
|
|
2017-04-19 09:16:28 +08:00
|
|
|
/// Global Executor
|
|
|
|
int _global_sqldb_executor(void* ExParam,int colNum,char** colVal,char** colName);
|
|
|
|
|
2017-04-16 16:00:44 +08:00
|
|
|
class SQLDB
|
|
|
|
{
|
|
|
|
public:
|
2017-04-16 17:32:39 +08:00
|
|
|
SQLDB()=default;
|
|
|
|
~SQLDB()=default;
|
|
|
|
|
2017-04-16 16:00:44 +08:00
|
|
|
bool isOpened();
|
|
|
|
int open(const std::string& filename);
|
|
|
|
SQLStatement prepare(const std::string& SQLCommand);
|
|
|
|
int step(const SQLStatement& Statement);
|
2017-04-19 09:16:28 +08:00
|
|
|
|
|
|
|
template<typename Callable,typename... Args>
|
|
|
|
int exec(const std::string& SQLCommand,Callable&& callable,Args&&... args)
|
|
|
|
{
|
|
|
|
auto realCall=[&](int colNum,char** colVal,char** colName)->int{return callable(colNum,colVal,colName,args...);};
|
|
|
|
std::function<int(int,char**,char**)> func=realCall;
|
|
|
|
return _exec_real(SQLCommand,_global_sqldb_executor,&func);
|
|
|
|
}
|
|
|
|
|
|
|
|
int exec(const std::string& SQLCommand);
|
|
|
|
|
2017-04-21 13:20:21 +08:00
|
|
|
int exec_raw(const std::string& SQLCommand,SQLCallback callback,void* param);
|
2017-04-19 09:16:28 +08:00
|
|
|
|
2017-04-16 18:15:17 +08:00
|
|
|
const char* getErrorMsg();
|
|
|
|
void clearError();
|
2017-04-16 16:00:44 +08:00
|
|
|
private:
|
2017-04-19 09:16:28 +08:00
|
|
|
int _exec_real(const std::string& SQLCommand,SQLCallback callback,void* param);
|
|
|
|
|
2017-06-09 22:59:53 +08:00
|
|
|
sqlite3* _get() const;
|
2017-04-16 17:32:39 +08:00
|
|
|
void _set(sqlite3*);
|
|
|
|
std::shared_ptr<sqlite3> _db;
|
2017-04-16 18:15:17 +08:00
|
|
|
char* _errmsg;
|
2017-04-16 16:00:44 +08:00
|
|
|
};
|
|
|
|
|
2017-04-16 18:15:17 +08:00
|
|
|
}/// End of namespace MiniEngine::SQL
|
2017-04-16 16:00:44 +08:00
|
|
|
|
2017-04-16 18:15:17 +08:00
|
|
|
}/// End of namespace MiniEngine
|