Principle

A script does not read the DB directly while processing (so that GameServer does not freeze). Instead:

  1. Call DB.Query(sql, label, param). The query runs on the DB thread of GameServer (the game database).
  2. When the result is ready, GameServer calls onDBQueryResult(label, param, result) on the main thread (at the start of the next loop).
  3. You get back the label and param exactly as you sent them so you know which query the result belongs to.

Query functions

FunctionEffectReturns
DB.Query(sql, label, param)Sends a SELECT query; the result comes back to onDBQueryResult.1 if sent; 0 if sql is empty
DB.Execute(sql)Runs INSERT / UPDATE / DELETE on the DB thread, with no result returned.return value is not used
DB.Escape(text)Escapes special characters so a string can be placed in an SQL statement inside quotes '...'.the escaped string
  • label: a string you choose, used to tell query types apart (should have the module name as a prefix).
  • param: a string you choose, usually containing the information needed when the result arrives (for example aIndex:character ID). Pass aIndex together with the character ID to know whether the player is still the same person (an index can be reused by someone else).
Every string concatenated into an SQL statement (account name, character name, text typed by the player) must go through DB.Escape to prevent SQL injection. For numeric values, use %d in string.format.
The SQL statement is sent verbatim to the game database, so use the syntax of that database system (the current server version uses SQL Server, for example SELECT TOP 1 ...).

Result: onDBQueryResult

function onDBQueryResult(label, param, result)
	return BridgeFunction_OnSQLAsyncResult(label, param, result)
end

This is the default content in System\CallbacksDB.lua: it passes the result to the modules that attached OnSQLAsyncResult.

ParameterTypeMeaning
labelstringThe label sent with the query.
paramstringThe string sent with the query.
resulttable or 0A table of rows: result[1], result[2]... each row is a table { column_name = value }. At most 10000 rows are returned to Lua (extra rows are dropped and GameServer writes a warning to the log). Numeric columns (integer, decimal, real) give a number value; the other columns give a string; for a NULL value the key is nil (not present in the table). If there are no rows or the query fails: result = 0 (the number 0, not an empty table).

Always check type(result) == "table" before reading:

function MyMod_OnQueryResult(label, param, result)
	if label ~= "MyMod_Check" then
		return 0                       -- not my own query
	end

	if type(result) == "table" then
		local total = result[1].total  -- column named "total" in the SELECT statement
		-- ...
	end

	return 1                           -- fully handled
end

BridgeFunctionAttach("OnSQLAsyncResult", "MyMod_OnQueryResult")

-- Send the query:
DB.Query(string.format("SELECT TOP 1 TotalAmount AS total FROM acc_recharge WHERE AccountId = %d", accountId),
	"MyMod_Check", tostring(aIndex))

Running a query again

  • For a function attached with OnSQLAsyncResult: return 1 = received, finished; return 2 = run the same query again; any other value = pass on to the next function.
  • If you write onDBQueryResult yourself (not through modules): return 1 = run the query again, any other value = finished.
  • A query can be run again at most 20 times; beyond that it stops and writes the log label '...' was run again too many times, stopped.

Limits

  • At most 5000 queries can be waiting for results at the same time. Beyond this number, new queries are dropped and the log LuaAPI: N DB queries waiting, query dropped (label) is written.
  • Results are delivered only while LuaAPI is still running: if LuaAPI is reloaded while waiting, the pending results are dropped (queries already sent still finish in the DB but Lua does not receive them).
  • If the onDBQueryResult function errors, the error is logged and that query ends.