Guest User

Untitled

a guest
May 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 KB | None | 0 0
  1. Index: database.h
  2. ===================================================================
  3. --- database.h (revision 6108)
  4. +++ database.h (working copy)
  5. @@ -64,6 +64,8 @@
  6. typedef DATABASE_CLASS Database;
  7. typedef DBRES_CLASS DBResult;
  8.  
  9. +typedef std::map<const std::string, uint32_t> listNames_t;
  10. +
  11. class DBQuery;
  12.  
  13. enum DBParam_t{
  14. @@ -171,6 +173,21 @@
  15. */
  16. DATABASE_VIRTUAL void freeResult(DBResult *res) {};
  17.  
  18. + /**
  19. + * Retrieve id of last inserted row
  20. + *
  21. + * @return id on success, 0 if last query did not result on any rows with auto_increment keys
  22. + */
  23. + DATABASE_VIRTUAL uint64_t getLastInsertId() {return 0;}
  24. +
  25. + /**
  26. + * Get case insensitive string comparison operator
  27. + *
  28. + * @return the case insensitive operator
  29. + */
  30. + DATABASE_VIRTUAL std::string getStringComparer() {return "= ";}
  31. + DATABASE_VIRTUAL std::string getUpdateLimiter() {return " LIMIT 1;";}
  32. +
  33. protected:
  34. _Database() : m_connected(false) {};
  35. DATABASE_VIRTUAL ~_Database() {};
  36. @@ -216,10 +233,14 @@
  37. * \return true if moved, false if there are no more results.
  38. */
  39. DATABASE_VIRTUAL bool next() {return false;};
  40. +
  41. + listNames_t getListNames() const {return m_listNames;}
  42.  
  43. protected:
  44. _DBResult() {};
  45. DATABASE_VIRTUAL ~_DBResult() {};
  46. +
  47. + listNames_t m_listNames;
  48. };
  49.  
  50. /**
  51. Index: databasemysql.h
  52. ===================================================================
  53. --- databasemysql.h (revision 6108)
  54. +++ databasemysql.h (working copy)
  55. @@ -60,6 +60,9 @@
  56.  
  57. DATABASE_VIRTUAL void freeResult(DBResult *res);
  58.  
  59. + DATABASE_VIRTUAL uint64_t getLastInsertId() {return (uint64_t)mysql_insert_id(&m_handle);}
  60. +
  61. +
  62. protected:
  63. MYSQL m_handle;
  64. };
  65. Index: databasesqlite.h
  66. ===================================================================
  67. --- databasesqlite.h (revision 6108)
  68. +++ databasesqlite.h (working copy)
  69. @@ -54,6 +54,11 @@
  70.  
  71. DATABASE_VIRTUAL void freeResult(DBResult *res);
  72.  
  73. + DATABASE_VIRTUAL uint64_t getLastInsertId() {return (uint64_t)sqlite3_last_insert_rowid(m_handle);}
  74. +
  75. + DATABASE_VIRTUAL std::string getStringComparer() {return "LIKE ";}
  76. + DATABASE_VIRTUAL std::string getUpdateLimiter() {return ";";}
  77. +
  78. protected:
  79. std::string _parse(const std::string &s);
  80.  
  81. Index: luascript.cpp
  82. ===================================================================
  83. --- luascript.cpp (revision 6108)
  84. +++ luascript.cpp (working copy)
  85. @@ -65,6 +65,9 @@
  86. uint32_t ScriptEnviroment::m_lastCombatId = 0;
  87. ScriptEnviroment::ConditionMap ScriptEnviroment::m_conditionMap;
  88. uint32_t ScriptEnviroment::m_lastConditionId = 0;
  89. +ScriptEnviroment::DBResultMap ScriptEnviroment::m_tempResults;
  90. +uint32_t ScriptEnviroment::m_lastResultId = 0;
  91. +
  92. ScriptEnviroment::StorageMap ScriptEnviroment::m_globalStorageMap;
  93. ScriptEnviroment::TempItemListMap ScriptEnviroment::m_tempItems;
  94.  
  95. @@ -108,8 +111,19 @@
  96. }
  97. }
  98. }
  99. -
  100. m_tempItems.clear();
  101. +
  102. + if(!m_tempResults.empty())
  103. + {
  104. + Database* db = Database::instance();
  105. + for(DBResultMap::iterator it = m_tempResults.begin(); it != m_tempResults.end(); ++it)
  106. + {
  107. + if(it->second)
  108. + db->freeResult(it->second);
  109. + }
  110. + }
  111. +
  112. + m_tempResults.clear();
  113.  
  114. m_realPos.x = 0;
  115. m_realPos.y = 0;
  116. @@ -439,6 +453,37 @@
  117. }
  118. }
  119.  
  120. +uint32_t ScriptEnviroment::addResult(DBResult* res)
  121. +{
  122. + uint32_t newResultId = m_lastResultId + 1;
  123. + m_tempResults[newResultId] = res;
  124. +
  125. + m_lastResultId++;
  126. + return m_lastResultId;
  127. +}
  128. +
  129. +bool ScriptEnviroment::removeResult(uint32_t id)
  130. +{
  131. + DBResultMap::iterator it = m_tempResults.find(id);
  132. + if(it == m_tempResults.end())
  133. + return false;
  134. +
  135. + if(it->second)
  136. + Database::instance()->freeResult(it->second);
  137. +
  138. + m_tempResults.erase(it);
  139. + return true;
  140. +}
  141. +
  142. +DBResult* ScriptEnviroment::getResultByID(uint32_t id)
  143. +{
  144. + DBResultMap::iterator it = m_tempResults.find(id);
  145. + if(it != m_tempResults.end())
  146. + return it->second;
  147. +
  148. + return NULL;
  149. +}
  150. +
  151. void ScriptEnviroment::addGlobalStorageValue(const uint32_t key, const int32_t value)
  152. {
  153. m_globalStorageMap[key] = value;
  154. @@ -720,7 +765,6 @@
  155. //And here you load both "safe" and "unsafe" libraries
  156. luaL_openlibs(m_luaState);
  157. #endif
  158. -
  159. std::string datadir = g_config.getString(ConfigManager::DATA_DIRECTORY);
  160.  
  161. registerFunctions();
  162. @@ -1986,7 +2030,13 @@
  163. //bit operations for Lua, based on bitlib project release 24
  164. //bit.bnot, bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift
  165. luaL_register(m_luaState, "bit", LuaScriptInterface::luaBitReg);
  166. +
  167. + //db table
  168. + luaL_register(m_luaState, "db", LuaScriptInterface::luaDatabaseTable);
  169.  
  170. + //result table
  171. + luaL_register(m_luaState, "result", LuaScriptInterface::luaResultTable);
  172. +
  173. //isGmInvisible(cid)
  174. lua_register(m_luaState, "isGmInvisible", LuaScriptInterface::luaIsGmInvisible);
  175.  
  176. @@ -8958,6 +9008,209 @@
  177. SHIFTOP(uint32_t, ULeftShift, <<)
  178. SHIFTOP(uint32_t, URightShift, >>)
  179.  
  180. +
  181. +const luaL_Reg LuaScriptInterface::luaDatabaseTable[] =
  182. +{
  183. + {"query", LuaScriptInterface::luaDatabaseExecute},
  184. + {"storeQuery", LuaScriptInterface::luaDatabaseStoreQuery},
  185. + {"escapeString", LuaScriptInterface::luaDatabaseEscapeString},
  186. + {"escapeBlob", LuaScriptInterface::luaDatabaseEscapeBlob},
  187. + {"lastInsertId", LuaScriptInterface::luaDatabaseLastInsertId},
  188. + {"stringComparer", LuaScriptInterface::luaDatabaseStringComparer},
  189. + {"updateLimiter", LuaScriptInterface::luaDatabaseUpdateLimiter},
  190. + {"connected", LuaScriptInterface::luaDatabaseConnected},
  191. + {"tableExists", LuaScriptInterface::luaDatabaseTableExists},
  192. + {NULL,NULL}
  193. +};
  194. +
  195. +int32_t LuaScriptInterface::luaDatabaseExecute(lua_State* L)
  196. +{
  197. + DBQuery query;
  198. + lua_pushboolean(L, Database::instance()->executeQuery(popString(L)));
  199. + return 1;
  200. +}
  201. +
  202. +int32_t LuaScriptInterface::luaDatabaseStoreQuery(lua_State* L)
  203. +{
  204. + ScriptEnviroment* env = getScriptEnv();
  205. +
  206. + DBQuery query;
  207. + if(DBResult* res = Database::instance()->storeQuery(popString(L)))
  208. + lua_pushnumber(L, env->addResult(res));
  209. + else
  210. + lua_pushboolean(L, false);
  211. +
  212. + return 1;
  213. +}
  214. +
  215. +int32_t LuaScriptInterface::luaDatabaseEscapeString(lua_State* L)
  216. +{
  217. + DBQuery query;
  218. + lua_pushstring(L, Database::instance()->escapeString(popString(L)).c_str());
  219. + return 1;
  220. +}
  221. +
  222. +int32_t LuaScriptInterface::luaDatabaseEscapeBlob(lua_State* L)
  223. +{
  224. + uint32_t length = popNumber(L);
  225. + DBQuery query;
  226. +
  227. + lua_pushstring(L, Database::instance()->escapeBlob(popString(L).c_str(), length).c_str());
  228. + return 1;
  229. +}
  230. +
  231. +int32_t LuaScriptInterface::luaDatabaseLastInsertId(lua_State* L)
  232. +{
  233. + DBQuery query;
  234. + lua_pushnumber(L, Database::instance()->getLastInsertId());
  235. + return 1;
  236. +}
  237. +
  238. +int32_t LuaScriptInterface::luaDatabaseStringComparer(lua_State* L)
  239. +{
  240. + lua_pushstring(L, Database::instance()->getStringComparer().c_str());
  241. + return 1;
  242. +}
  243. +
  244. +int32_t LuaScriptInterface::luaDatabaseUpdateLimiter(lua_State* L)
  245. +{
  246. + lua_pushstring(L, Database::instance()->getUpdateLimiter().c_str());
  247. + return 1;
  248. +}
  249. +
  250. +int32_t LuaScriptInterface::luaDatabaseConnected(lua_State* L)
  251. +{
  252. + lua_pushboolean(L, Database::instance()->isConnected());
  253. + return 1;
  254. +}
  255. +
  256. +int32_t LuaScriptInterface::luaDatabaseTableExists(lua_State* L)
  257. +{
  258. + //lua_pushboolean(L, DatabaseManager::getInstance()->tableExists(popString(L)));
  259. + lua_pushboolean(L, true);
  260. + return 1;
  261. +}
  262. +
  263. +const luaL_Reg LuaScriptInterface::luaResultTable[] =
  264. +{
  265. + {"getDataInt", LuaScriptInterface::luaResultGetDataInt},
  266. + {"getDataLong", LuaScriptInterface::luaResultGetDataLong},
  267. + {"getDataString", LuaScriptInterface::luaResultGetDataString},
  268. + {"getDataStream", LuaScriptInterface::luaResultGetDataStream},
  269. + {"getAllData", LuaScriptInterface::luaResultGetAllData},
  270. + {"next", LuaScriptInterface::luaResultNext},
  271. + {"free", LuaScriptInterface::luaResultFree},
  272. + {NULL, NULL}
  273. +};
  274. +
  275. +int32_t LuaScriptInterface::luaResultGetDataInt(lua_State* L)
  276. +{
  277. + const std::string& s = popString(L);
  278. + ScriptEnviroment* env = getScriptEnv();
  279. +
  280. + DBResult* res = env->getResultByID(popNumber(L));
  281. + if(!res)
  282. + {
  283. + lua_pushboolean(L, false);
  284. + return 1;
  285. + }
  286. +
  287. + lua_pushnumber(L, res->getDataInt(s));
  288. + return 1;
  289. +}
  290. +
  291. +int32_t LuaScriptInterface::luaResultGetDataLong(lua_State* L)
  292. +{
  293. + const std::string& s = popString(L);
  294. + ScriptEnviroment* env = getScriptEnv();
  295. +
  296. + DBResult* res = env->getResultByID(popNumber(L));
  297. + if(!res)
  298. + {
  299. + lua_pushboolean(L, false);
  300. + return 1;
  301. + }
  302. +
  303. + lua_pushnumber(L, res->getDataLong(s));
  304. + return 1;
  305. +}
  306. +
  307. +int32_t LuaScriptInterface::luaResultGetDataString(lua_State* L)
  308. +{
  309. + const std::string& s = popString(L);
  310. + ScriptEnviroment* env = getScriptEnv();
  311. +
  312. + DBResult* res = env->getResultByID(popNumber(L));
  313. + if(!res)
  314. + {
  315. + lua_pushboolean(L, false);
  316. + return 1;
  317. + }
  318. +
  319. + lua_pushstring(L, res->getDataString(s).c_str());
  320. + return 1;
  321. +}
  322. +
  323. +int32_t LuaScriptInterface::luaResultGetDataStream(lua_State* L)
  324. +{
  325. + const std::string& s = popString(L);
  326. + ScriptEnviroment* env = getScriptEnv();
  327. +
  328. + DBResult* res = env->getResultByID(popNumber(L));
  329. + if(!res)
  330. + {
  331. + lua_pushboolean(L, false);
  332. + return 1;
  333. + }
  334. +
  335. + unsigned long length = 0;
  336. + lua_pushstring(L, res->getDataStream(s, length));
  337. + lua_pushnumber(L, length);
  338. + return 2;
  339. +}
  340. +
  341. +int32_t LuaScriptInterface::luaResultGetAllData(lua_State* L)
  342. +{
  343. + ScriptEnviroment* env = getScriptEnv();
  344. +
  345. + DBResult* res = env->getResultByID(popNumber(L));
  346. + if(!res)
  347. + {
  348. + lua_pushboolean(L, false);
  349. + return 1;
  350. + }
  351. +
  352. + lua_newtable(L);
  353. + listNames_t listNames = res->getListNames();
  354. + for(listNames_t::iterator it = listNames.begin(); it != listNames.end(); ++it)
  355. + setField(L, it->first.c_str(), res->getDataString(it->first));
  356. +
  357. + return 1;
  358. +}
  359. +
  360. +int32_t LuaScriptInterface::luaResultNext(lua_State* L)
  361. +{
  362. + ScriptEnviroment* env = getScriptEnv();
  363. +
  364. + DBResult* res = env->getResultByID(popNumber(L));
  365. + if(!res)
  366. + {
  367. + lua_pushboolean(L, false);
  368. + return 1;
  369. + }
  370. +
  371. + lua_pushboolean(L, res->next());
  372. + return 1;
  373. +}
  374. +
  375. +int32_t LuaScriptInterface::luaResultFree(lua_State* L)
  376. +{
  377. + ScriptEnviroment* env = getScriptEnv();
  378. + lua_pushboolean(L, env->removeResult(popNumber(L)));
  379. + return 1;
  380. +}
  381. +
  382. +
  383. int LuaScriptInterface::luaGetItemWeaponType(lua_State *L)
  384. {
  385. //getItemWeaponType(itemid)
  386. Index: luascript.h
  387. ===================================================================
  388. --- luascript.h (revision 6108)
  389. +++ luascript.h (working copy)
  390. @@ -24,6 +24,7 @@
  391.  
  392. #include "definitions.h"
  393. #include "position.h"
  394. +#include "database.h"
  395. #include <string>
  396. #include <map>
  397. #include <list>
  398. @@ -109,6 +110,10 @@
  399. uint32_t addThing(Thing* thing);
  400. void insertThing(uint32_t uid, Thing* thing);
  401.  
  402. + DBResult* getResultByID(uint32_t id);
  403. + uint32_t addResult(DBResult* res);
  404. + bool removeResult(uint32_t id);
  405. +
  406. void addGlobalStorageValue(const uint32_t key, const int32_t value);
  407. bool getGlobalStorageValue(const uint32_t key, int32_t& value) const;
  408.  
  409. @@ -150,6 +155,7 @@
  410. typedef std::map<uint32_t, Combat*> CombatMap;
  411. typedef std::map<uint32_t, Condition*> ConditionMap;
  412. typedef std::list<Item*> ItemList;
  413. + typedef std::map<uint32_t, DBResult*> DBResultMap;
  414.  
  415. //script file id
  416. int32_t m_scriptId;
  417. @@ -185,6 +191,10 @@
  418. static uint32_t m_lastConditionId;
  419. static ConditionMap m_conditionMap;
  420.  
  421. + //result map
  422. + static uint32_t m_lastResultId;
  423. + static DBResultMap m_tempResults;
  424. +
  425. //for npc scripts
  426. Npc* m_curNpc;
  427.  
  428. @@ -677,6 +687,26 @@
  429. static int luaBitULeftShift(lua_State *L);
  430. static int luaBitURightShift(lua_State *L);
  431.  
  432. + static const luaL_Reg luaDatabaseTable[10];
  433. + static int32_t luaDatabaseExecute(lua_State* L);
  434. + static int32_t luaDatabaseStoreQuery(lua_State* L);
  435. + static int32_t luaDatabaseEscapeString(lua_State* L);
  436. + static int32_t luaDatabaseEscapeBlob(lua_State* L);
  437. + static int32_t luaDatabaseLastInsertId(lua_State* L);
  438. + static int32_t luaDatabaseStringComparer(lua_State* L);
  439. + static int32_t luaDatabaseUpdateLimiter(lua_State* L);
  440. + static int32_t luaDatabaseConnected(lua_State* L);
  441. + static int32_t luaDatabaseTableExists(lua_State* L);
  442. +
  443. + static const luaL_Reg luaResultTable[8];
  444. + static int32_t luaResultGetDataInt(lua_State* L);
  445. + static int32_t luaResultGetDataLong(lua_State* L);
  446. + static int32_t luaResultGetDataString(lua_State* L);
  447. + static int32_t luaResultGetDataStream(lua_State* L);
  448. + static int32_t luaResultGetAllData(lua_State* L);
  449. + static int32_t luaResultNext(lua_State* L);
  450. + static int32_t luaResultFree(lua_State* L);
  451. +
  452. static int luaGetItemWeaponType(lua_State *L);
  453. static int luaGetItemAttack(lua_State *L);
  454. static int luaGetItemDefense(lua_State *L);
Add Comment
Please, Sign In to add comment