Skip to content
imshooterPublic

About

MySQL and MariaDB for open.mp and SA-MP: the usual natives with open.mp names, prepared statements, batches, transactions, an ORM, JSON cells and Argon2id or bcrypt passwords.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

MySQL

release Build

Note

MySQL is experimental and already runs in production. Its natives may still change from one version to the next. It was made with Claude Opus 5.5, a model by Anthropic.

A MySQL and MariaDB plugin for open.mp and SA-MP. Its natives keep the names SA-MP scripts have long used for MySQL, as mysql_tquery and cache_get_value_name_int, so a script that uses them needs no change, and they come again with open.mp names, as MySQL_ThreadedQuery. It adds prepared statements sent in bulk, transactions, JSON cells, passwords hashed with Argon2id or bcrypt, and less work on the game thread.

Features

  • The usual natives, with their names, signatures and behaviour: mysql_tquery, mysql_pquery, mysql_query, the cache, the ORM, mysql_format, options and mysql.ini.
  • open.mp names in full words, as MySQL_ThreadedQuery, MySQL_ExecuteStatement and Cache_GetValueNameInt, in the same include. Both names call the same native.
  • One binary that loads as an open.mp component or as a legacy plugin, on open.mp and on SA-MP 0.3.7.
  • Queries formatted by the plugin with no length limit, as mysql_tquery_format, the values of the public first and those of the query after. A query with no values is sent as it is, so MySQL_Query and MySQL_SendQuery take either.
  • Queries built in pieces, of any length, joined into one with %Q.
  • Prepared statements with the same specifiers as queries. A batch gathers rows and sends them to MariaDB as arrays in one command, split to fit max_allowed_packet.
  • Transactions of queries and statements that apply all together or not at all, run in order on one connection.
  • JSON cells read by key or JSON Pointer, parsed once when first read. Text in the code page of the game is converted to and from UTF-8.
  • Passwords hashed with Argon2id or bcrypt and verified on up to four threads of the plugin.
  • Limits on the length of a query, the queries waiting and the queries per second, and timeouts for connecting, reading and writing.
  • TLS that never falls back to plain text once asked for, with the certificate checked against an authority or a fingerprint.
  • Settings in a file, with ${NAME} taken from the environment, so a password stays out of it.

Installation

  1. Download the latest release from Releases, the windows-x86 or the linux-x86 zip.
  2. On open.mp, place mysql.dll or mysql.so in the components folder. On SA-MP, place it in plugins and add it to the plugins line of server.cfg.
  3. Place mysql.inc in the include folder of the compiler, and include it with #include <mysql>.

Tested with open.mp 1.6.7 and SA-MP 0.3.7-R2, with MariaDB 10.4 on Windows and MariaDB 10.11 on Ubuntu 20.04, Debian 12 and Ubuntu 24.04. The Linux build needs glibc 2.28 or later, and OpenSSL 3.0 is linked into it, so it needs no other library.

Coming from another MySQL plugin

Scripts that use the usual natives compile and run with #include <mysql>. What may differ:

  • POOL_SIZE is 4 by default, and connections of the pool open when they first have work.
  • %f writes a float with as many digits as it needs, and %.4f with four decimals.
  • ERROR_CALLBACK, an option of the connection or error_callback in mysql.ini, also calls the public of a failed query, after OnQueryError, with mysql_errno and cache_get_errno giving its error.
  • mysql_tquery_file and mysql_query_file split the file at semicolons outside quotes and comments, and stop at the first query that fails.

A first script

#include <open.mp>
#include <mysql>

new MySQL:g_SQL;

forward OnScoreRead(playerid);

public OnGameModeInit() {
    g_SQL = mysql_connect_file();

    return 1;
}

public OnPlayerConnect(playerid) {
    new
        name[MAX_PLAYER_NAME + 1]
    ;

    GetPlayerName(playerid, name, sizeof (name));
    mysql_tquery_format(g_SQL, "SELECT score FROM players WHERE name = %q", "OnScoreRead", "i", playerid, name);

    return 1;
}

public OnScoreRead(playerid) {
    new
        score
    ;

    if (cache_get_value_name_int(0, "score", score)) {
        SetPlayerScore(playerid, score);
    }

    return 1;
}

mysql.ini, in scriptfiles or in the folder of the server:

hostname = 127.0.0.1
username = game
password = ${MYSQL_PASSWORD}
database = game

Two examples of accounts register and log players in with Argon2id hashes, load them with prepared statements, record each log in with a transaction and save everyone in one batch:

  • examples/natives uses the snake_case names and plain callbacks, with nothing but open.mp.
  • examples/inline uses the open.mp names and the inlines of SSI, with error_callback on.

Prepared statements and transactions

new
    MySQL:gDatabase,
    Statement:gSaveScore,
    gPlayerId[MAX_PLAYERS]
;

stock PrepareStatements() {
    gSaveScore = MySQL_PrepareStatement(gDatabase, "UPDATE players SET score = %i WHERE id = %i");
}

stock SaveScores() {
    new const
        Batch:batch = MySQL_CreateBatch(gSaveScore)
    ;

    for (new i; i != MAX_PLAYERS; ++i) {
        if (IsPlayerConnected(i)) {
            MySQL_AddBatchRow(batch, GetPlayerScore(i), gPlayerId[i]);
        }
    }

    MySQL_ExecuteBatch(batch);
}

stock BuyItem(playerid, item, price) {
    new const
        Transaction:transaction = MySQL_CreateTransaction(gDatabase)
    ;

    MySQL_AddTransactionQuery(transaction, "UPDATE players SET money = money - %i WHERE id = %i", price, gPlayerId[playerid]);
    MySQL_AddTransactionQuery(transaction, "INSERT INTO inventory (player_id, item) VALUES (%i, %i)", gPlayerId[playerid], item);
    MySQL_ExecuteTransaction(transaction, "OnItemBought", "i", playerid);
}

The values of a statement are copied when a row is added, and never escaped or parsed by the server again. A statement is prepared again when its connection is lost. A transaction that fails is rolled back and calls OnQueryError with the statement that failed.

Performance

Measured on one computer against MariaDB 10.4 on the same machine, open.mp 1.6.7 with PawnJIT, scripts compiled with -d2, medians of five runs.

Time
Sending a query, on the game thread 0.8 µs
500 queries in order 45 ms
500 parallel queries 16 ms
A formatted query into an array 0.64 µs
Six cells of a row by name 0.6 µs
A query waited for 64 µs
2000 rows of a batch 15 ms
5000 inserts in order, in one transaction 310 ms
An Argon2id hash, 19 MiB and two passes 25 ms
Eight Argon2id hashes at once 55 ms

Documentation

The wiki has a page for every native, the settings and guides to queries, results, JSON cells, prepared statements, passwords and TLS.

Tests

The tests in tests run as a gamemode against a database, and shut the server down when they end. Set the database at the top of tests/test.inc, or give the port to the compiler as TEST_PORT=3307. They pass with the binary as an open.mp component, as a legacy plugin on open.mp and on SA-MP 0.3.7-R2.

Building

CMake 3.19 or later fetches the open.mp SDK, the Pawn headers and MariaDB Connector/C 3.4.11. The server is 32 bit, so the plugin must be too.

On Windows, with Visual Studio 2022 or its Build Tools:

cmake -S . -B build -A Win32
cmake --build build --config Release --target mysql

On Linux, with GCC and its 32 bit libraries, and a static 32 bit OpenSSL 3:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_SHARED_LINKER_FLAGS=-m32 -DOPENSSL_ROOT_DIR=/opt/openssl
cmake --build build --target mysql

The plugin is written to build/Release/mysql.dll or build/mysql.so. The Linux release is built on Ubuntu 20.04 with OpenSSL 3.0, as .github/workflows/main.yml does, and GitHub Actions runs the tests on Linux at every push.

Thanks to

License

Released under the MIT License. The libraries in external keep their own licenses, MariaDB Connector/C is under the LGPL 2.1, and OpenSSL, in the Linux build, under the Apache License 2.0.

About

MySQL and MariaDB for open.mp and SA-MP: the usual natives with open.mp names, prepared statements, batches, transactions, an ORM, JSON cells and Argon2id or bcrypt passwords.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages