Skip to content

Commit 469123a

Browse files
VelalanneNiam5
authored andcommitted
Hotfix DB methods implemented
Merges #520
1 parent 53efa85 commit 469123a

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

methods/TrinityCore/GlobalMethods.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,101 @@ namespace LuaGlobalFunctions
15831583
return 0;
15841584
}
15851585

1586+
#if ELUNA_EXPANSION == EXP_RETAIL
1587+
/**
1588+
* Executes a SQL query on the hotfix database and returns an [ElunaQuery].
1589+
*
1590+
* The query is always executed synchronously
1591+
* (i.e. execution halts until the query has finished and then results are returned).
1592+
*
1593+
* For an example see [Global:WorldDBQuery].
1594+
*
1595+
* @warning This method is flagged as **unsafe** and is **disabled by default**. Use with caution, or transition to Async queries.
1596+
*
1597+
* @param string sql : query to execute
1598+
* @return [ElunaQuery] results or nil if no rows found
1599+
*/
1600+
int HotfixDBQuery(Eluna* E)
1601+
{
1602+
const char* query = E->CHECKVAL<const char*>(1);
1603+
1604+
ElunaQuery result = HotfixDatabase.Query(query);
1605+
if (result)
1606+
E->Push(&result);
1607+
else
1608+
E->Push();
1609+
1610+
return 1;
1611+
}
1612+
1613+
/**
1614+
* Executes a SQL query on the hotfix database.
1615+
*
1616+
* The query may be executed *asynchronously* (at a later, unpredictable time).
1617+
* If you need to execute the query synchronously, use [Global:HotfixDBQuery] instead.
1618+
*
1619+
* Any results produced are ignored.
1620+
* If you need results from the query, use [Global:HotfixDBQuery] instead.
1621+
*
1622+
* HotfixDBExecute("DELETE FROM my_table")
1623+
*
1624+
* @param string sql : query to execute
1625+
*/
1626+
int HotfixDBExecute(Eluna* E)
1627+
{
1628+
const char* query = E->CHECKVAL<const char*>(1);
1629+
HotfixDatabase.Execute(query);
1630+
return 0;
1631+
}
1632+
1633+
/**
1634+
* Initiates an asynchronous SQL query on the hotfix database with a callback function.
1635+
*
1636+
* The query is executed asynchronously, and the provided Lua function is called when the query completes.
1637+
* The callback function parameter is the query result (an [ElunaQuery] or nil if no rows found).
1638+
*
1639+
* For an example see [Global:WorldDBQueryAsync].
1640+
*
1641+
* @param string sql : query to execute asynchronously
1642+
* @param function callback : the callback function to be called with the query results
1643+
*/
1644+
int HotfixDBQueryAsync(Eluna* E)
1645+
{
1646+
const char* query = E->CHECKVAL<const char*>(1);
1647+
luaL_checktype(E->L, 2, LUA_TFUNCTION);
1648+
1649+
// Push the Lua function onto the stack and create a reference
1650+
lua_pushvalue(E->L, 2);
1651+
int funcRef = luaL_ref(E->L, LUA_REGISTRYINDEX);
1652+
1653+
// Validate the function reference
1654+
if (funcRef == LUA_REFNIL || funcRef == LUA_NOREF)
1655+
{
1656+
luaL_argerror(E->L, 2, "unable to make a ref to function");
1657+
return 0;
1658+
}
1659+
1660+
// Add an asynchronous query callback
1661+
E->GetQueryProcessor().AddCallback(HotfixDatabase.AsyncQuery(query).WithCallback([E, funcRef](QueryResult result)
1662+
{
1663+
ElunaQuery* eq = result ? &result : nullptr;
1664+
1665+
// Get the Lua function from the registry
1666+
lua_rawgeti(E->L, LUA_REGISTRYINDEX, funcRef);
1667+
1668+
// Push the query results as a parameter
1669+
E->Push(eq);
1670+
1671+
// Call the Lua function
1672+
E->ExecuteCall(1, 0);
1673+
1674+
// Unreference the Lua function
1675+
luaL_unref(E->L, LUA_REGISTRYINDEX, funcRef);
1676+
}));
1677+
return 0;
1678+
}
1679+
#endif
1680+
15861681
/**
15871682
* Registers a global timed event.
15881683
*
@@ -3294,6 +3389,15 @@ namespace LuaGlobalFunctions
32943389
{ "AuthDBQuery", &LuaGlobalFunctions::AuthDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
32953390
{ "AuthDBExecute", &LuaGlobalFunctions::AuthDBExecute },
32963391
{ "AuthDBQueryAsync", &LuaGlobalFunctions::AuthDBQueryAsync },
3392+
#if ELUNA_EXPANSION == EXP_RETAIL
3393+
{ "HotfixDBQuery", &LuaGlobalFunctions::HotfixDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
3394+
{ "HotfixDBExecute", &LuaGlobalFunctions::HotfixDBExecute },
3395+
{ "HotfixDBQueryAsync", &LuaGlobalFunctions::HotfixDBQueryAsync },
3396+
#else
3397+
{ "HotfixDBQuery", METHOD_REG_NONE },
3398+
{ "HotfixDBExecute", METHOD_REG_NONE },
3399+
{ "HotfixDBQueryAsync", METHOD_REG_NONE },
3400+
#endif
32973401
{ "CreateLuaEvent", &LuaGlobalFunctions::CreateLuaEvent },
32983402
{ "RemoveEventById", &LuaGlobalFunctions::RemoveEventById },
32993403
{ "RemoveEvents", &LuaGlobalFunctions::RemoveEvents },

0 commit comments

Comments
 (0)