diff --git a/MiniEngine_SQLite.cpp b/MiniEngine_SQLite.cpp index b595e3d..31e1089 100644 --- a/MiniEngine_SQLite.cpp +++ b/MiniEngine_SQLite.cpp @@ -36,6 +36,13 @@ bool SQLStatement::isReady() const } +int _global_sqldb_executor(void* ExParam,int colNum,char** colVal,char** colName) +{ + auto p=reinterpret_cast*>(ExParam); + return (*p)(colNum,colVal,colName); +} + + void SQLDB::_set(sqlite3* p) { _db.reset(p,sqlite3_close); @@ -69,7 +76,17 @@ int SQLDB::step(const SQLStatement& Statement) return sqlite3_step(Statement._getStmt()); } +int SQLDB::exec(const std::string& SQLCommand) +{ + return _exec_real(SQLCommand,nullptr,nullptr); +} + int SQLDB::exec(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) { return sqlite3_exec(_get(),SQLCommand.c_str(),callback,param,&_errmsg); } diff --git a/MiniEngine_SQLite.h b/MiniEngine_SQLite.h index 9102fa6..5c97aa5 100644 --- a/MiniEngine_SQLite.h +++ b/MiniEngine_SQLite.h @@ -27,6 +27,9 @@ private: using SQLCallback = int (*)(void* ExParam,int colNum,char** colVal,char** colName); +/// Global Executor +int _global_sqldb_executor(void* ExParam,int colNum,char** colVal,char** colName); + class SQLDB { public: @@ -37,10 +40,25 @@ public: int open(const std::string& filename); SQLStatement prepare(const std::string& SQLCommand); int step(const SQLStatement& Statement); + + template + 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 func=realCall; + return _exec_real(SQLCommand,_global_sqldb_executor,&func); + } + + int exec(const std::string& SQLCommand); + + /// Reserved For Capability int exec(const std::string& SQLCommand,SQLCallback callback,void* param); + const char* getErrorMsg(); void clearError(); private: + int _exec_real(const std::string& SQLCommand,SQLCallback callback,void* param); + sqlite3* _get(); void _set(sqlite3*); std::shared_ptr _db;