Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add async queries #113

Merged
merged 2 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ Eluna API for AC:
- Added `ItemTemplate` methods: https://github.com/azerothcore/mod-eluna/pull/84
- Added logging with `ELUNA_LOG_INFO` for `RunCommand()`: https://github.com/azerothcore/mod-eluna/pull/75
- Added `GetOwnerHalaa` and `SetOwnerHalaa`: https://github.com/azerothcore/mod-eluna/pull/79
- Added `WorldDBQueryAsync`, `CharDBQueryAsync` and `AuthDBQueryAsync`: https://github.com/azerothcore/mod-eluna/pull/113
98 changes: 95 additions & 3 deletions src/LuaEngine/GlobalMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,11 +1271,46 @@ namespace LuaGlobalFunctions
return 0;
}

template <typename T>
int DBQueryAsync(lua_State* L, DatabaseWorkerPool<T>& db)
{
const char* query = Eluna::CHECKVAL<const char*>(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2);
int funcRef = luaL_ref(L, LUA_REGISTRYINDEX);
if (funcRef == LUA_REFNIL || funcRef == LUA_NOREF)
{
luaL_argerror(L, 2, "unable to make a ref to function");
return 0;
}

Eluna::GEluna->queryProcessor.AddCallback(db.AsyncQuery(query).WithCallback([L, funcRef](QueryResult result)
{
ElunaQuery* eq = result ? new ElunaQuery(result) : nullptr;

LOCK_ELUNA;

// Get function
lua_rawgeti(L, LUA_REGISTRYINDEX, funcRef);

// Push parameters
Eluna::Push(L, eq);

// Call function
Eluna::GEluna->ExecuteCall(1, 0);

luaL_unref(L, LUA_REGISTRYINDEX, funcRef);
}));

return 0;
}

/**
* Executes a SQL query on the world database and returns an [ElunaQuery].
*
* The query is always executed synchronously
* (i.e. execution halts until the query has finished and then results are returned).
* If you need to execute the query asynchronously, use [Global:WorldDBQueryAsync] instead.
*
* local Q = WorldDBQuery("SELECT entry, name FROM creature_template LIMIT 10")
* if Q then
Expand Down Expand Up @@ -1308,14 +1343,37 @@ namespace LuaGlobalFunctions
return 1;
}

/**
* Executes an asynchronous SQL query on the world database and passes an [ElunaQuery] to a callback function.
*
* The query is executed asynchronously
* (i.e. the server keeps running while the query is executed in parallel, and results are passed to a callback function).
* If you need to execute the query synchronously, use [Global:WorldDBQuery] instead.
*
* WorldDBQueryAsync("SELECT entry, name FROM creature_template LIMIT 10", function(Q)
* if Q then
* repeat
* local entry, name = Q:GetUInt32(0), Q:GetString(1)
* print(entry, name)
* until not Q:NextRow()
* end
* end)
*
* @param string sql : query to execute
*/
int WorldDBQueryAsync(lua_State* L)
{
return DBQueryAsync(L, WorldDatabase);
}

/**
* Executes a SQL query on the world database.
*
* The query may be executed *asynchronously* (at a later, unpredictable time).
* If you need to execute the query synchronously, use [Global:WorldDBQuery] instead.
*
* Any results produced are ignored.
* If you need results from the query, use [Global:WorldDBQuery] instead.
* If you need results from the query, use [Global:WorldDBQuery] or [Global:WorldDBQueryAsync] instead.
*
* WorldDBExecute("DELETE FROM my_table")
*
Expand All @@ -1333,6 +1391,7 @@ namespace LuaGlobalFunctions
*
* The query is always executed synchronously
* (i.e. execution halts until the query has finished and then results are returned).
* If you need to execute the query asynchronously, use [Global:CharDBQueryAsync] instead.
*
* For an example see [Global:WorldDBQuery].
*
Expand All @@ -1359,14 +1418,30 @@ namespace LuaGlobalFunctions
return 1;
}

/**
* Executes an asynchronous SQL query on the character database and passes an [ElunaQuery] to a callback function.
*
* The query is executed asynchronously
* (i.e. the server keeps running while the query is executed in parallel, and results are passed to a callback function).
* If you need to execute the query synchronously, use [Global:CharDBQuery] instead.
*
* For an example see [Global:WorldDBQueryAsync].
*
* @param string sql : query to execute
*/
int CharDBQueryAsync(lua_State* L)
{
return DBQueryAsync(L, CharacterDatabase);
}

/**
* Executes a SQL query on the character database.
*
* The query may be executed *asynchronously* (at a later, unpredictable time).
* If you need to execute the query synchronously, use [Global:CharDBQuery] instead.
*
* Any results produced are ignored.
* If you need results from the query, use [Global:CharDBQuery] instead.
* If you need results from the query, use [Global:CharDBQuery] or [Global:CharDBQueryAsync] instead.
*
* CharDBExecute("DELETE FROM my_table")
*
Expand All @@ -1384,6 +1459,7 @@ namespace LuaGlobalFunctions
*
* The query is always executed synchronously
* (i.e. execution halts until the query has finished and then results are returned).
* If you need to execute the query asynchronously, use [Global:AuthDBQueryAsync] instead.
*
* For an example see [Global:WorldDBQuery].
*
Expand All @@ -1410,14 +1486,30 @@ namespace LuaGlobalFunctions
return 1;
}

/**
* Executes an asynchronous SQL query on the character database and passes an [ElunaQuery] to a callback function.
*
* The query is executed asynchronously
* (i.e. the server keeps running while the query is executed in parallel, and results are passed to a callback function).
* If you need to execute the query synchronously, use [Global:AuthDBQuery] instead.
*
* For an example see [Global:WorldDBQueryAsync].
*
* @param string sql : query to execute
*/
int AuthDBQueryAsync(lua_State* L)
{
return DBQueryAsync(L, LoginDatabase);
}

/**
* Executes a SQL query on the login database.
*
* The query may be executed *asynchronously* (at a later, unpredictable time).
* If you need to execute the query synchronously, use [Global:AuthDBQuery] instead.
*
* Any results produced are ignored.
* If you need results from the query, use [Global:AuthDBQuery] instead.
* If you need results from the query, use [Global:AuthDBQuery] or [Global:AuthDBQueryAsync] instead.
*
* AuthDBExecute("DELETE FROM my_table")
*
Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ enabled(false),
L(NULL),
eventMgr(NULL),
httpManager(),
queryProcessor(),

ServerEventBindings(NULL),
PlayerEventBindings(NULL),
Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class ELUNA_GAME_API Eluna
lua_State* L;
EventMgr* eventMgr;
HttpManager httpManager;
QueryCallbackProcessor queryProcessor;

BindingMap< EventKey<Hooks::ServerEvents> >* ServerEventBindings;
BindingMap< EventKey<Hooks::PlayerEvents> >* PlayerEventBindings;
Expand Down
3 changes: 3 additions & 0 deletions src/LuaEngine/LuaFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ luaL_Reg GlobalMethods[] =
{ "RunCommand", &LuaGlobalFunctions::RunCommand },
{ "SendWorldMessage", &LuaGlobalFunctions::SendWorldMessage },
{ "WorldDBQuery", &LuaGlobalFunctions::WorldDBQuery },
{ "WorldDBQueryAsync", &LuaGlobalFunctions::WorldDBQueryAsync },
{ "WorldDBExecute", &LuaGlobalFunctions::WorldDBExecute },
{ "CharDBQuery", &LuaGlobalFunctions::CharDBQuery },
{ "CharDBQueryAsync", &LuaGlobalFunctions::CharDBQueryAsync },
{ "CharDBExecute", &LuaGlobalFunctions::CharDBExecute },
{ "AuthDBQuery", &LuaGlobalFunctions::AuthDBQuery },
{ "AuthDBQueryAsync", &LuaGlobalFunctions::AuthDBQueryAsync },
{ "AuthDBExecute", &LuaGlobalFunctions::AuthDBExecute },
{ "CreateLuaEvent", &LuaGlobalFunctions::CreateLuaEvent },
{ "RemoveEventById", &LuaGlobalFunctions::RemoveEventById },
Expand Down
1 change: 1 addition & 0 deletions src/LuaEngine/ServerHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ void Eluna::OnWorldUpdate(uint32 diff)

eventMgr->globalProcessor->Update(diff);
httpManager.HandleHttpResponses();
queryProcessor.ProcessReadyCallbacks();

START_HOOK(WORLD_EVENT_ON_UPDATE);
Push(diff);
Expand Down