Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: database.h
- ===================================================================
- --- database.h (revision 6108)
- +++ database.h (working copy)
- @@ -64,6 +64,8 @@
- typedef DATABASE_CLASS Database;
- typedef DBRES_CLASS DBResult;
- +typedef std::map<const std::string, uint32_t> listNames_t;
- +
- class DBQuery;
- enum DBParam_t{
- @@ -171,6 +173,21 @@
- */
- DATABASE_VIRTUAL void freeResult(DBResult *res) {};
- + /**
- + * Retrieve id of last inserted row
- + *
- + * @return id on success, 0 if last query did not result on any rows with auto_increment keys
- + */
- + DATABASE_VIRTUAL uint64_t getLastInsertId() {return 0;}
- +
- + /**
- + * Get case insensitive string comparison operator
- + *
- + * @return the case insensitive operator
- + */
- + DATABASE_VIRTUAL std::string getStringComparer() {return "= ";}
- + DATABASE_VIRTUAL std::string getUpdateLimiter() {return " LIMIT 1;";}
- +
- protected:
- _Database() : m_connected(false) {};
- DATABASE_VIRTUAL ~_Database() {};
- @@ -216,10 +233,14 @@
- * \return true if moved, false if there are no more results.
- */
- DATABASE_VIRTUAL bool next() {return false;};
- +
- + listNames_t getListNames() const {return m_listNames;}
- protected:
- _DBResult() {};
- DATABASE_VIRTUAL ~_DBResult() {};
- +
- + listNames_t m_listNames;
- };
- /**
- Index: databasemysql.h
- ===================================================================
- --- databasemysql.h (revision 6108)
- +++ databasemysql.h (working copy)
- @@ -60,6 +60,9 @@
- DATABASE_VIRTUAL void freeResult(DBResult *res);
- + DATABASE_VIRTUAL uint64_t getLastInsertId() {return (uint64_t)mysql_insert_id(&m_handle);}
- +
- +
- protected:
- MYSQL m_handle;
- };
- Index: databasesqlite.h
- ===================================================================
- --- databasesqlite.h (revision 6108)
- +++ databasesqlite.h (working copy)
- @@ -54,6 +54,11 @@
- DATABASE_VIRTUAL void freeResult(DBResult *res);
- + DATABASE_VIRTUAL uint64_t getLastInsertId() {return (uint64_t)sqlite3_last_insert_rowid(m_handle);}
- +
- + DATABASE_VIRTUAL std::string getStringComparer() {return "LIKE ";}
- + DATABASE_VIRTUAL std::string getUpdateLimiter() {return ";";}
- +
- protected:
- std::string _parse(const std::string &s);
- Index: luascript.cpp
- ===================================================================
- --- luascript.cpp (revision 6108)
- +++ luascript.cpp (working copy)
- @@ -65,6 +65,9 @@
- uint32_t ScriptEnviroment::m_lastCombatId = 0;
- ScriptEnviroment::ConditionMap ScriptEnviroment::m_conditionMap;
- uint32_t ScriptEnviroment::m_lastConditionId = 0;
- +ScriptEnviroment::DBResultMap ScriptEnviroment::m_tempResults;
- +uint32_t ScriptEnviroment::m_lastResultId = 0;
- +
- ScriptEnviroment::StorageMap ScriptEnviroment::m_globalStorageMap;
- ScriptEnviroment::TempItemListMap ScriptEnviroment::m_tempItems;
- @@ -108,8 +111,19 @@
- }
- }
- }
- -
- m_tempItems.clear();
- +
- + if(!m_tempResults.empty())
- + {
- + Database* db = Database::instance();
- + for(DBResultMap::iterator it = m_tempResults.begin(); it != m_tempResults.end(); ++it)
- + {
- + if(it->second)
- + db->freeResult(it->second);
- + }
- + }
- +
- + m_tempResults.clear();
- m_realPos.x = 0;
- m_realPos.y = 0;
- @@ -439,6 +453,37 @@
- }
- }
- +uint32_t ScriptEnviroment::addResult(DBResult* res)
- +{
- + uint32_t newResultId = m_lastResultId + 1;
- + m_tempResults[newResultId] = res;
- +
- + m_lastResultId++;
- + return m_lastResultId;
- +}
- +
- +bool ScriptEnviroment::removeResult(uint32_t id)
- +{
- + DBResultMap::iterator it = m_tempResults.find(id);
- + if(it == m_tempResults.end())
- + return false;
- +
- + if(it->second)
- + Database::instance()->freeResult(it->second);
- +
- + m_tempResults.erase(it);
- + return true;
- +}
- +
- +DBResult* ScriptEnviroment::getResultByID(uint32_t id)
- +{
- + DBResultMap::iterator it = m_tempResults.find(id);
- + if(it != m_tempResults.end())
- + return it->second;
- +
- + return NULL;
- +}
- +
- void ScriptEnviroment::addGlobalStorageValue(const uint32_t key, const int32_t value)
- {
- m_globalStorageMap[key] = value;
- @@ -720,7 +765,6 @@
- //And here you load both "safe" and "unsafe" libraries
- luaL_openlibs(m_luaState);
- #endif
- -
- std::string datadir = g_config.getString(ConfigManager::DATA_DIRECTORY);
- registerFunctions();
- @@ -1986,7 +2030,13 @@
- //bit operations for Lua, based on bitlib project release 24
- //bit.bnot, bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift
- luaL_register(m_luaState, "bit", LuaScriptInterface::luaBitReg);
- +
- + //db table
- + luaL_register(m_luaState, "db", LuaScriptInterface::luaDatabaseTable);
- + //result table
- + luaL_register(m_luaState, "result", LuaScriptInterface::luaResultTable);
- +
- //isGmInvisible(cid)
- lua_register(m_luaState, "isGmInvisible", LuaScriptInterface::luaIsGmInvisible);
- @@ -8958,6 +9008,209 @@
- SHIFTOP(uint32_t, ULeftShift, <<)
- SHIFTOP(uint32_t, URightShift, >>)
- +
- +const luaL_Reg LuaScriptInterface::luaDatabaseTable[] =
- +{
- + {"query", LuaScriptInterface::luaDatabaseExecute},
- + {"storeQuery", LuaScriptInterface::luaDatabaseStoreQuery},
- + {"escapeString", LuaScriptInterface::luaDatabaseEscapeString},
- + {"escapeBlob", LuaScriptInterface::luaDatabaseEscapeBlob},
- + {"lastInsertId", LuaScriptInterface::luaDatabaseLastInsertId},
- + {"stringComparer", LuaScriptInterface::luaDatabaseStringComparer},
- + {"updateLimiter", LuaScriptInterface::luaDatabaseUpdateLimiter},
- + {"connected", LuaScriptInterface::luaDatabaseConnected},
- + {"tableExists", LuaScriptInterface::luaDatabaseTableExists},
- + {NULL,NULL}
- +};
- +
- +int32_t LuaScriptInterface::luaDatabaseExecute(lua_State* L)
- +{
- + DBQuery query;
- + lua_pushboolean(L, Database::instance()->executeQuery(popString(L)));
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseStoreQuery(lua_State* L)
- +{
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBQuery query;
- + if(DBResult* res = Database::instance()->storeQuery(popString(L)))
- + lua_pushnumber(L, env->addResult(res));
- + else
- + lua_pushboolean(L, false);
- +
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseEscapeString(lua_State* L)
- +{
- + DBQuery query;
- + lua_pushstring(L, Database::instance()->escapeString(popString(L)).c_str());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseEscapeBlob(lua_State* L)
- +{
- + uint32_t length = popNumber(L);
- + DBQuery query;
- +
- + lua_pushstring(L, Database::instance()->escapeBlob(popString(L).c_str(), length).c_str());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseLastInsertId(lua_State* L)
- +{
- + DBQuery query;
- + lua_pushnumber(L, Database::instance()->getLastInsertId());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseStringComparer(lua_State* L)
- +{
- + lua_pushstring(L, Database::instance()->getStringComparer().c_str());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseUpdateLimiter(lua_State* L)
- +{
- + lua_pushstring(L, Database::instance()->getUpdateLimiter().c_str());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseConnected(lua_State* L)
- +{
- + lua_pushboolean(L, Database::instance()->isConnected());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaDatabaseTableExists(lua_State* L)
- +{
- + //lua_pushboolean(L, DatabaseManager::getInstance()->tableExists(popString(L)));
- + lua_pushboolean(L, true);
- + return 1;
- +}
- +
- +const luaL_Reg LuaScriptInterface::luaResultTable[] =
- +{
- + {"getDataInt", LuaScriptInterface::luaResultGetDataInt},
- + {"getDataLong", LuaScriptInterface::luaResultGetDataLong},
- + {"getDataString", LuaScriptInterface::luaResultGetDataString},
- + {"getDataStream", LuaScriptInterface::luaResultGetDataStream},
- + {"getAllData", LuaScriptInterface::luaResultGetAllData},
- + {"next", LuaScriptInterface::luaResultNext},
- + {"free", LuaScriptInterface::luaResultFree},
- + {NULL, NULL}
- +};
- +
- +int32_t LuaScriptInterface::luaResultGetDataInt(lua_State* L)
- +{
- + const std::string& s = popString(L);
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBResult* res = env->getResultByID(popNumber(L));
- + if(!res)
- + {
- + lua_pushboolean(L, false);
- + return 1;
- + }
- +
- + lua_pushnumber(L, res->getDataInt(s));
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaResultGetDataLong(lua_State* L)
- +{
- + const std::string& s = popString(L);
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBResult* res = env->getResultByID(popNumber(L));
- + if(!res)
- + {
- + lua_pushboolean(L, false);
- + return 1;
- + }
- +
- + lua_pushnumber(L, res->getDataLong(s));
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaResultGetDataString(lua_State* L)
- +{
- + const std::string& s = popString(L);
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBResult* res = env->getResultByID(popNumber(L));
- + if(!res)
- + {
- + lua_pushboolean(L, false);
- + return 1;
- + }
- +
- + lua_pushstring(L, res->getDataString(s).c_str());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaResultGetDataStream(lua_State* L)
- +{
- + const std::string& s = popString(L);
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBResult* res = env->getResultByID(popNumber(L));
- + if(!res)
- + {
- + lua_pushboolean(L, false);
- + return 1;
- + }
- +
- + unsigned long length = 0;
- + lua_pushstring(L, res->getDataStream(s, length));
- + lua_pushnumber(L, length);
- + return 2;
- +}
- +
- +int32_t LuaScriptInterface::luaResultGetAllData(lua_State* L)
- +{
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBResult* res = env->getResultByID(popNumber(L));
- + if(!res)
- + {
- + lua_pushboolean(L, false);
- + return 1;
- + }
- +
- + lua_newtable(L);
- + listNames_t listNames = res->getListNames();
- + for(listNames_t::iterator it = listNames.begin(); it != listNames.end(); ++it)
- + setField(L, it->first.c_str(), res->getDataString(it->first));
- +
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaResultNext(lua_State* L)
- +{
- + ScriptEnviroment* env = getScriptEnv();
- +
- + DBResult* res = env->getResultByID(popNumber(L));
- + if(!res)
- + {
- + lua_pushboolean(L, false);
- + return 1;
- + }
- +
- + lua_pushboolean(L, res->next());
- + return 1;
- +}
- +
- +int32_t LuaScriptInterface::luaResultFree(lua_State* L)
- +{
- + ScriptEnviroment* env = getScriptEnv();
- + lua_pushboolean(L, env->removeResult(popNumber(L)));
- + return 1;
- +}
- +
- +
- int LuaScriptInterface::luaGetItemWeaponType(lua_State *L)
- {
- //getItemWeaponType(itemid)
- Index: luascript.h
- ===================================================================
- --- luascript.h (revision 6108)
- +++ luascript.h (working copy)
- @@ -24,6 +24,7 @@
- #include "definitions.h"
- #include "position.h"
- +#include "database.h"
- #include <string>
- #include <map>
- #include <list>
- @@ -109,6 +110,10 @@
- uint32_t addThing(Thing* thing);
- void insertThing(uint32_t uid, Thing* thing);
- + DBResult* getResultByID(uint32_t id);
- + uint32_t addResult(DBResult* res);
- + bool removeResult(uint32_t id);
- +
- void addGlobalStorageValue(const uint32_t key, const int32_t value);
- bool getGlobalStorageValue(const uint32_t key, int32_t& value) const;
- @@ -150,6 +155,7 @@
- typedef std::map<uint32_t, Combat*> CombatMap;
- typedef std::map<uint32_t, Condition*> ConditionMap;
- typedef std::list<Item*> ItemList;
- + typedef std::map<uint32_t, DBResult*> DBResultMap;
- //script file id
- int32_t m_scriptId;
- @@ -185,6 +191,10 @@
- static uint32_t m_lastConditionId;
- static ConditionMap m_conditionMap;
- + //result map
- + static uint32_t m_lastResultId;
- + static DBResultMap m_tempResults;
- +
- //for npc scripts
- Npc* m_curNpc;
- @@ -677,6 +687,26 @@
- static int luaBitULeftShift(lua_State *L);
- static int luaBitURightShift(lua_State *L);
- + static const luaL_Reg luaDatabaseTable[10];
- + static int32_t luaDatabaseExecute(lua_State* L);
- + static int32_t luaDatabaseStoreQuery(lua_State* L);
- + static int32_t luaDatabaseEscapeString(lua_State* L);
- + static int32_t luaDatabaseEscapeBlob(lua_State* L);
- + static int32_t luaDatabaseLastInsertId(lua_State* L);
- + static int32_t luaDatabaseStringComparer(lua_State* L);
- + static int32_t luaDatabaseUpdateLimiter(lua_State* L);
- + static int32_t luaDatabaseConnected(lua_State* L);
- + static int32_t luaDatabaseTableExists(lua_State* L);
- +
- + static const luaL_Reg luaResultTable[8];
- + static int32_t luaResultGetDataInt(lua_State* L);
- + static int32_t luaResultGetDataLong(lua_State* L);
- + static int32_t luaResultGetDataString(lua_State* L);
- + static int32_t luaResultGetDataStream(lua_State* L);
- + static int32_t luaResultGetAllData(lua_State* L);
- + static int32_t luaResultNext(lua_State* L);
- + static int32_t luaResultFree(lua_State* L);
- +
- static int luaGetItemWeaponType(lua_State *L);
- static int luaGetItemAttack(lua_State *L);
- static int luaGetItemDefense(lua_State *L);
Add Comment
Please, Sign In to add comment