From bc31b72a9770aa32387bfa1827620ef4823224b7 Mon Sep 17 00:00:00 2001 From: Ramon Bernardo Date: Tue, 12 Dec 2023 19:42:23 -0300 Subject: [PATCH] Database transaction & row insert in lua (#4589) --- src/luascript.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++++ src/luascript.h | 11 ++++++ 2 files changed, 106 insertions(+) diff --git a/src/luascript.cpp b/src/luascript.cpp index 915fd80af4..e69958b271 100644 --- a/src/luascript.cpp +++ b/src/luascript.cpp @@ -2204,6 +2204,21 @@ void LuaScriptInterface::registerFunctions() registerMethod("table", "create", LuaScriptInterface::luaTableCreate); registerMethod("table", "pack", LuaScriptInterface::luaTablePack); + // DB Insert + registerClass("DBInsert", "", LuaScriptInterface::luaDBInsertCreate); + + registerMethod("DBInsert", "addRow", LuaScriptInterface::luaDBInsertAddRow); + registerMethod("DBInsert", "execute", LuaScriptInterface::luaDBInsertExecute); + + // DB Transaction + registerClass("DBTransaction", "", LuaScriptInterface::luaDBTransactionCreate); + registerMetaMethod("DBTransaction", "__eq", LuaScriptInterface::luaUserdataCompare); + registerMetaMethod("DBTransaction", "__gc", LuaScriptInterface::luaDBTransactionDelete); + + registerMethod("DBTransaction", "begin", LuaScriptInterface::luaDBTransactionBegin); + registerMethod("DBTransaction", "commit", LuaScriptInterface::luaDBTransactionCommit); + registerMethod("DBTransaction", "rollback", LuaScriptInterface::luaDBTransactionDelete); + // Game registerTable("Game"); @@ -4392,6 +4407,86 @@ int LuaScriptInterface::luaTablePack(lua_State* L) return 1; /* return table */ } +// DB Insert +int LuaScriptInterface::luaDBInsertCreate(lua_State* L) +{ + // DBInsert(query) + if (isString(L, 2)) { + pushUserdata(L, new DBInsert(getString(L, 2))); + setMetatable(L, -1, "DBInsert"); + } else { + lua_pushnil(L); + } + return 1; +} + +int LuaScriptInterface::luaDBInsertAddRow(lua_State* L) +{ + // insert:addRow(row) + DBInsert* insert = getUserdata(L, 1); + if (insert) { + pushBoolean(L, insert->addRow(getString(L, 2))); + } else { + lua_pushnil(L); + } + return 1; +} + +int LuaScriptInterface::luaDBInsertExecute(lua_State* L) +{ + // insert:execute() + DBInsert* insert = getUserdata(L, 1); + if (insert) { + pushBoolean(L, insert->execute()); + } else { + lua_pushnil(L); + } + return 1; +} + +// DB Transaction +int LuaScriptInterface::luaDBTransactionCreate(lua_State* L) +{ + // DBTransaction() + pushUserdata(L, new DBTransaction); + setMetatable(L, -1, "DBTransaction"); + return 1; +} + +int LuaScriptInterface::luaDBTransactionBegin(lua_State* L) +{ + // transaction:begin() + DBTransaction* transaction = getUserdata(L, 1); + if (transaction) { + pushBoolean(L, transaction->begin()); + } else { + lua_pushnil(L); + } + return 1; +} + +int LuaScriptInterface::luaDBTransactionCommit(lua_State* L) +{ + // transaction:commit() + DBTransaction* transaction = getUserdata(L, 1); + if (transaction) { + pushBoolean(L, transaction->commit()); + } else { + lua_pushnil(L); + } + return 1; +} + +int LuaScriptInterface::luaDBTransactionDelete(lua_State* L) +{ + DBTransaction** transactionPtr = getRawUserdata(L, 1); + if (transactionPtr && *transactionPtr) { + delete *transactionPtr; + *transactionPtr = nullptr; + } + return 0; +} + // Game int LuaScriptInterface::luaGameGetSpectators(lua_State* L) { diff --git a/src/luascript.h b/src/luascript.h index b11fd37286..fc472e7df1 100644 --- a/src/luascript.h +++ b/src/luascript.h @@ -499,6 +499,17 @@ class LuaScriptInterface static int luaTableCreate(lua_State* L); static int luaTablePack(lua_State* L); + // DB Insert + static int luaDBInsertCreate(lua_State* L); + static int luaDBInsertAddRow(lua_State* L); + static int luaDBInsertExecute(lua_State* L); + + // DB Transaction + static int luaDBTransactionCreate(lua_State* L); + static int luaDBTransactionDelete(lua_State* L); + static int luaDBTransactionBegin(lua_State* L); + static int luaDBTransactionCommit(lua_State* L); + // Game static int luaGameGetSpectators(lua_State* L); static int luaGameGetPlayers(lua_State* L);