Docs

The Lua library

The full classic MySQL API, plus a cache and a write queue. Load waw-sql's library to use them.

Server sideLua

Loading it

Everything written for oxmysql works as it is. To use waw-sql's additions, load its library instead of oxmysql's in your resource's fxmanifest.lua: replace the @oxmysql/lib/MySQL.lua line with this one, above your own server files.

Lua
server_script '@waw-sql/lib/MySQL.lua'

It contains the full classic API as well: MySQL.query, .single, .scalar, .insert, .update, .prepare, .rawExecute, .transaction, .startTransaction, MySQL.ready, their .await forms, and MySQL.Async / MySQL.Sync for mysql-async style scripts.

Keep it working on oxmysql too

The additions below exist only in waw-sql. A script that must also run on oxmysql can check first: GetResourceState('waw-sql') == 'started'.

Your first queries

Every call takes the SQL, then its values in a table. Write a ? where a value goes; the values fill them in order.

Lua
-- every row that matches: a list of rows ({} when none)
local cars = MySQL.query.await('SELECT plate, vehicle FROM player_vehicles WHERE citizenid = ?', { citizenid })
for _, car in ipairs(cars) do print(car.plate) end

-- one row, or nil when there is none
local player = MySQL.single.await('SELECT * FROM players WHERE citizenid = ?', { citizenid })

-- one value: the first column of the first row
local count = MySQL.scalar.await('SELECT COUNT(*) FROM player_vehicles WHERE citizenid = ?', { citizenid })

-- a new row: gives back its id
local id = MySQL.insert.await('INSERT INTO my_garage (plate, owner) VALUES (?, ?)', { plate, citizenid })

-- change rows: gives back how many changed
local changed = MySQL.update.await('UPDATE my_garage SET state = ? WHERE plate = ?', { 0, plate })

Values can also be named, with :name or @name in the SQL and a table with the same names:

Lua
MySQL.update.await('UPDATE my_garage SET state = :state WHERE plate = :plate', { state = 0, plate = plate })
Never join player text into the SQL

'... WHERE name = "' .. name .. '"' lets a player run their own SQL on your database (SQL injection). Always pass values with ? or a name: waw-sql then sends them apart from the SQL, so they can never be read as SQL. Smart alerts flag scripts that do this.

Waiting for the answer

.await pauses your code until the answer is there, so it only works inside a thread: an event handler, a command, CreateThread or MySQL.ready. Anywhere else it stops with "Current execution context is not in the scheduler". There, use the callback form: the same call without .await, with a function at the end.

Lua
MySQL.query('SELECT * FROM my_garage WHERE owner = ?', { citizenid }, function(rows)
    print(('%d cars'):format(#rows))
end)

To run something as soon as the database is connected, for example when your script starts:

Lua
MySQL.ready(function()
    -- runs once the database is connected; .await works in here
end)

All or nothing: transactions

When several changes belong together, run them as one transaction. If one fails, none of them is kept, and the call gives back false.

Lua
local ok = MySQL.transaction.await({
    { query = 'UPDATE my_garage SET owner = ? WHERE plate = ?', values = { buyer, plate } },
    { query = 'INSERT INTO my_garage_log (plate, seller, buyer) VALUES (?, ?, ?)', values = { plate, seller, buyer } },
})
if not ok then print('the sale was not saved') end

Cache

Lua
local job = MySQL.cache.single.await('SELECT * FROM jobs WHERE name = ?', { name }, 60000) -- keep for 60 s
MySQL.cache.clear('jobs') -- or MySQL.cache.clear() for everything
  • MySQL.cache.query, .single and .scalar take the same arguments as the normal calls, plus how long to keep the result in ms.
  • Only SELECT queries are cached.
  • Every INSERT, UPDATE or DELETE that goes through waw-sql clears the affected tables immediately.
  • Changes made outside waw-sql are only picked up when the time runs out. That includes other tools, triggers, cascades, and tables behind a VIEW.

Write queue

Lua
MySQL.batch.add('UPDATE players SET position = ? WHERE citizenid = ?', { json.encode(pos), cid }, 'pos:' .. cid)
MySQL.batch.flush.await()
  • Queued writes are committed together, by default once per second (waw_sql_batch_interval).
  • A key replaces the previous pending write with the same key. Use keys only for absolute writes like SET x = ?, never for relative ones like SET x = x + ?.
  • Any query touching a table with pending writes flushes the queue first, so reads never miss a queued write.
Not for money or inventory

If the server crashes, up to one interval of queued writes can be lost.

Diagnostics

In the server console:

commandwhat it shows
wawsqlpool, cache and queue status
wawsql topresources ranked by total query time, including their slowest query
wawsql debug add <resource>log every query of that resource
wawsql debug remove <resource>stop logging it

From Lua, MySQL.stats() gives the same numbers as wawsql.

Errors in callbacks

With mysql_option 'return_callback_errors' in your fxmanifest, or with the .await and _async forms, your script gets its errors directly: callbacks receive cb(nil, message), and .await raises the error.