Skip to content

Commit 3bc3c44

Browse files
VelalanneNiam5
authored andcommitted
Hotfix DB methods implemented
Merges #520
1 parent 34a7e78 commit 3bc3c44

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
@@ -1576,6 +1576,101 @@ namespace LuaGlobalFunctions
15761576
return 0;
15771577
}
15781578

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

0 commit comments

Comments
 (0)