Skip to content

Commit

Permalink
Database transaction & row insert in lua (#4589)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo authored Dec 12, 2023
1 parent b99b1cc commit bc31b72
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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<DBInsert>(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<DBInsert>(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<DBInsert>(L, 1);
if (insert) {
pushBoolean(L, insert->execute());
} else {
lua_pushnil(L);
}
return 1;
}

// DB Transaction
int LuaScriptInterface::luaDBTransactionCreate(lua_State* L)
{
// DBTransaction()
pushUserdata<DBTransaction>(L, new DBTransaction);
setMetatable(L, -1, "DBTransaction");
return 1;
}

int LuaScriptInterface::luaDBTransactionBegin(lua_State* L)
{
// transaction:begin()
DBTransaction* transaction = getUserdata<DBTransaction>(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<DBTransaction>(L, 1);
if (transaction) {
pushBoolean(L, transaction->commit());
} else {
lua_pushnil(L);
}
return 1;
}

int LuaScriptInterface::luaDBTransactionDelete(lua_State* L)
{
DBTransaction** transactionPtr = getRawUserdata<DBTransaction>(L, 1);
if (transactionPtr && *transactionPtr) {
delete *transactionPtr;
*transactionPtr = nullptr;
}
return 0;
}

// Game
int LuaScriptInterface::luaGameGetSpectators(lua_State* L)
{
Expand Down
11 changes: 11 additions & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bc31b72

Please sign in to comment.