Some mysql stuff

This commit is contained in:
Henrique 2012-08-03 08:44:11 -03:00
parent 9e917ece88
commit 4954d5fecb
4 changed files with 19 additions and 7 deletions

View File

@ -709,9 +709,20 @@ void Application::registerLuaFunctions()
// Database
g_lua.registerClass<Database>();
// DBResult
g_lua.registerClass<DBResult>();
g_lua.bindClassMemberFunction<DBResult>("getDataInt", &DBResult::getDataInt);
g_lua.bindClassMemberFunction<DBResult>("getDataLong", &DBResult::getDataLong);
g_lua.bindClassMemberFunction<DBResult>("getDataString", &DBResult::getDataString);
//g_lua.bindClassMemberFunction<DBResult>("getDataStream", &DBResult::getDataStream);
g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next);
// Mysql
g_lua.registerClass<DatabaseMySQL>();
g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); });
g_lua.bindClassMemberFunction<DatabaseMySQL>("connect", &DatabaseMySQL::connect);
g_lua.bindClassMemberFunction<DatabaseMySQL>("executeQuery", &DatabaseMySQL::executeQuery);
g_lua.bindClassMemberFunction<DatabaseMySQL>("storeQuery", &DatabaseMySQL::storeQuery);
g_lua.bindClassMemberFunction<DatabaseMySQL>("escapeString", &DatabaseMySQL::escapeString);
#endif
}

View File

@ -25,8 +25,10 @@
#include <framework/global.h>
class DBResult;
class DatabaseMySQL;
typedef stdext::shared_object_ptr<DBResult> DBResultPtr;
typedef stdext::shared_object_ptr<DatabaseMySQL> DatabaseMySQLPtr;
#endif

View File

@ -117,7 +117,7 @@ bool DatabaseMySQL::executeQuery(const std::string &query)
return false;
}
DBResult* DatabaseMySQL::storeQuery(const std::string &query)
DBResultPtr DatabaseMySQL::storeQuery(const std::string &query)
{
//LOG_ONDELAY(500);
@ -125,11 +125,11 @@ DBResult* DatabaseMySQL::storeQuery(const std::string &query)
MYSQL_RES *m_res = mysql_store_result(&m_mysqlHandle);
if(m_res) {
DBResult* res = new DBResult(m_res);
DBResultPtr res = DBResultPtr(new DBResult(m_res));
if(res->next()) {
return res;
} else {
delete res;
//delete res;
break;
}
} else if(mysql_errno(&m_mysqlHandle) != 0) {

View File

@ -24,7 +24,7 @@ public:
bool commit();
bool executeQuery(const std::string &query);
DBResult* storeQuery(const std::string &query);
DBResultPtr storeQuery(const std::string &query);
uint64_t getLastInsertedRowID();
@ -41,15 +41,14 @@ protected:
MYSQL m_mysqlHandle;
};
class DBResult
class DBResult : public LuaObject
{
protected:
public:
DBResult(MYSQL_RES* res);
~DBResult();
friend class DatabaseMySQL;
public:
int32_t getDataInt(const std::string &s);
int64_t getDataLong(const std::string &s);
std::string getDataString(const std::string &s);