azerothcore / mod-eluna

Eluna Lua Engine © for WoW Emulators
https://elunaluaengine.github.io/
GNU General Public License v3.0
100 stars 130 forks source link

feat: add async queries #113

Closed r-o-b-o-t-o closed 1 year ago

r-o-b-o-t-o commented 1 year ago

Adds async queries for the world, characters and auth databases (WorldDBQueryAsync, CharDBQueryAsync, AuthDBQueryAsync)

local PLAYER_EVENT_ON_COMMAND = 42

local function SyncQueryExample()
    -- Hangs the server for 5 seconds while the query executes because of the SLEEP(5)
    query = CharDBQuery("SELECT guid, account, name, level, SLEEP(5) FROM characters LIMIT 3")
    if not query then
        return
    end

    repeat
        local guid, account, name, level = query:GetUInt32(0), query:GetUInt32(1), query:GetString(2), query:GetUInt8(3)
        print("GUID: " .. tostring(guid) .. ", account: " .. tostring(account) .. ", name: " .. name .. ", level: " .. tostring(level))
    until not query:NextRow()
end

local function AsyncQueryExample()
    -- Doesn't hang the server even with SLEEP(5) because we're using CharDBQueryAsync
    CharDBQueryAsync("SELECT guid, account, name, level, SLEEP(5) FROM characters LIMIT 3", function(query)
        if not query then
            return
        end

        repeat
            local guid, account, name, level = query:GetUInt32(0), query:GetUInt32(1), query:GetString(2), query:GetUInt8(3)
            print("GUID: " .. tostring(guid) .. ", account: " .. tostring(account) .. ", name: " .. name .. ", level: " .. tostring(level))
        until not query:NextRow()
    end)
end

RegisterPlayerEvent(PLAYER_EVENT_ON_COMMAND, function(event, player, command, chathandler)
    if command == "sync" then
        print("Executing sync query...")
        SyncQueryExample()
        return false
    elseif command == "async" then
        print("Executing async query...")
        AsyncQueryExample()
        return false
    end

    return true
end)