mariadb async query: Difference between revisions

From Onset Developer Wiki
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
{{Notice|This function is provided by the official [[MariaDB]] plugin.}}
{{Notice|This function is provided by the official [[MariaDB]] plugin.}}


{{FuncDescription|Executes a query in different parallel threads. Execution of order can not be guaranteed. Better use [[mariadb_query]] if you are unsure about it.}}
{{FuncDescription|Executes a query in different parallel threads. Order of execution cannot be guaranteed. Better use [[mariadb_query]] if you are unsure about it.}}


{{FuncSyntax|mariadb_async_query(handle, query_str [, callback_func, callback_args...])}}
{{FuncSyntax|mariadb_async_query(handle, query_str [, callback_func, callback_args...])}}
Line 15: Line 15:


== Example ==
== Example ==
__EDIT_ME__
<syntaxhighlight lang="Lua">
function GetCurrentPlayTime(player)
  CreateTimer(function(player)
      for k, v in pairs (GetAllPlayers()) do
          if GetAllPlayers() == true then
              local query = mariadb_prepare(sql, "SELECT * FROM accounts WHERE id = '?';",
              PlayerData[v].accountid)
              mariadb_async_query(sql, query, GetPlayerTime, v)
          end
      end
  end, 15000, player)
end
AddEvent("OnPackageStart", GetCurrentPlayTime)


function GetPlayerTime(player)
  local result = mariadb_get_assoc(1)
  local playtime = math.tointeger(result['time']) -- total time played on the server without session time.
  PlayerData[player].time = math.floor(PlayerData[player].time + (GetTimeSeconds() - PlayerData[player].play_time)) -- session play time.
  PlayerData[player].play_time = GetTimeSeconds()
  return PlayerData[player].time + playtime -- returns the total play time on the server.
end
</syntaxhighlight>
{{RelatedFunctions}}
{{RelatedFunctions}}
__EDIT_ME__
{{MariaDBFunctions}}

Latest revision as of 18:25, 30 August 2020

mariadb async query

Type: Function
Context: Server
Introduced: v1.0

NOTICE

This function is provided by the official MariaDB plugin.

Description

Executes a query in different parallel threads. Order of execution cannot be guaranteed. Better use mariadb_query if you are unsure about it.

Syntax

mariadb_async_query(handle, query_str [, callback_func, callback_args...])

Parameters

  • handle
    Connection handle.
  • query_str
    The query to be executed.
  • callback_func (optional)
    The function that will be called when the query was executed.
  • callback_args (optional)
    Multiple arguments to pass to the callback_func.

Return Value

  • Returns true if the query was successfully queued.

Example

function GetCurrentPlayTime(player)
   CreateTimer(function(player)
       for k, v in pairs (GetAllPlayers()) do
           if GetAllPlayers() == true then
              local query = mariadb_prepare(sql, "SELECT * FROM accounts WHERE id = '?';",
              PlayerData[v].accountid)
              mariadb_async_query(sql, query, GetPlayerTime, v) 
           end
       end 
   end, 15000, player)
end
AddEvent("OnPackageStart", GetCurrentPlayTime)

function GetPlayerTime(player)
   local result = mariadb_get_assoc(1)
   local playtime = math.tointeger(result['time']) -- total time played on the server without session time.
   PlayerData[player].time = math.floor(PlayerData[player].time + (GetTimeSeconds() - PlayerData[player].play_time)) -- session play time.
   PlayerData[player].play_time = GetTimeSeconds()
   return PlayerData[player].time + playtime -- returns the total play time on the server.
end

See also