Skip to content

Commands

Dedmen Miller edited this page Apr 25, 2019 · 27 revisions

Welcome to the intercept-database wiki!

Connection Commands

dbCreateConnection configName

configName: <STRING> - The config name(case-sensitive) of the connection

Returns: <DBConnection>

Creates a connection based on details in the config file in accounts.
Connection is not established until the first query.

dbCreateConnection [ip, port, user, pw, db]

ip: <STRING> - the IP Address or Domain of the database server
port: <NUMBER> - the port of the database server (usually 3306)
user: <STRING> - the user to log in with
pw: <STRING> - the password (duh)
db: <STRING> - the database to use (Equal to use <db> SQL command)

Returns: <DBConnection>

Creates a connection. The actual connection to the database server is only made on first query.
Connection is not established until the first query.

dbIsConnected connection (Since version 1.0)

connection: <DBCONNECTION> - A connection

Returns: <BOOL>

Returns whether the connection is currently connected to the database server.
Also checks if a worker thread is connected

dbPing connection (Since version 1.0)

connection: <DBCONNECTION> - A connection

Returns: <BOOL>

Executes a SELECT 1; on the database server and returns true if it get's 1 back. Returns false on error.
Suspends in scheduled, freezes in unscheduled.
(Should this return the actual error string somehow?, Should this call error handlers?)

connection dbAddErrorHandler code (Since version 1.0)

connection: <DBCONNECTION> - A connection
code: <CODE> - Script code.

Returns: <NOTHING>

Registers a global error handler on the connection, if any query on the connection causes an error, that function will be called with _this = [errorString, errorCode, query].
There can be multiple error handlers, they will be executed from first to last added.
If one of the error handlers returns true the error will be considered handled and the other handlers won't be called.
If error handlers are present, errors won't be printed to RPT.
Example _this:
19:09:39 ["Lost connection to MySQL server at 'reading authorization packet', system error: 10061",2013,"testQuery5"]
["You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'testQuery5' at line 1",1064,"testQuery5"]
19:11:56 ["Unknown column 'none' in 'field list'",1054,"SELECT none"]
#TODO add the query config name to _this too.

connection dbLoadSchema schemaName (Since version 1.1)

connection: <DBCONNECTION> - A connection
schemaName: <STRING> - schema name from config. case-sensitive.

Returns: <NOTHING>

Executes a SQL file. Path is defined in config.

Building Queries

dbPrepareQuery query

query: <STRING> - The SQL Query String

Returns: <QUERY>

Prepares a query.

dbPrepareQuery [query, bindValues]

query: <STRING> - The SQL Query String
bindValues: <ARRAY> - List of values to bind to ? in the query string.

Returns: <QUERY>

Prepares a query and already binds some values to it.

Example: dbPrepareQuery ["SELECT ? FROM ? WHERE ?=?", ["data", "table", "value", 5]]
-> SELECT data FROM table WHERE value=5

dbPrepareQueryConfig configName

configName: <STRING> - The config name(case-sensitive) of the query

Returns: <QUERY>

Prepares a query based on details in the config file in statements.

dbPrepareQueryConfig [configName, bindValues]

configName: <STRING> - The config name(case-sensitive) of the query
bindValues: <ARRAY> - List of values to bind to ? in the query string (See above)

Returns: <QUERY>

Prepares a query based on details in the config file in statements.

query dbBindValue value

query: <QUERY>
value: <STRING> OR <NUMBER> OR <BOOL> - Value to bind to the next unbound ? in the query

Returns: <NOTHING>

This command modifies the value in query. If you want to keep the old query intact you need to dbCopyQuery first.

query dbBindValueArray [value, value...]

query: <QUERY>
value: <STRING> OR <NUMBER> OR <BOOL> - Value to bind to the next unbound ? in the query

Returns: <NOTHING>

Binds multiple values to the next ? in the query, in same order as the ? occur in the query. This command modifies the value in query. If you want to keep the old query intact you need to dbCopyQuery first.

Example: _query = dbPrepareQuery "SELECT ? FROM ? WHERE ?=?"
_query dbBindValueArray ["data", "table", "value", 5]
-> SELECT data FROM table WHERE value=5

dbCopyQuery query

query: <QUERY> - the query object returned by dbPrepareQuery

Returns: <NOTHING>

Copies a query with all currently bound values.
Example: _query = dbPrepareQuery "SELECT ? FROM ? WHERE ?=?"
_query dbBindValueArray ["data", "table"]
_query -> SELECT data FROM table WHERE ?=?
_copyOfQuery = dbCopyQuery _query;
_copyOfQuery -> SELECT data FROM table WHERE ?=?
_copyOfQuery dbBindValueArray ["value", 5]
_copyOfQuery -> SELECT data FROM table WHERE value=5
_query -> SELECT data FROM table WHERE ?=?

Executing queries

connection dbExecute query

connection: <DBConnection> - The connection to execute the query on
query: <QUERY> - the query object returned by dbPrepareQuery

Returns: <RESULT>

This function behaves differently in scheduled and unscheduled.
Scheduled: Suspends the script like a sleep/waitUntil would do, and continues once result is ready.
Unscheduled: Freezes the game until the result is ready. (You probably want to use dbExecuteAsync)

connection dbExecuteAsync query

connection: <DBConnection> - The connection to execute the query on
query: <QUERY> - the query object returned by dbPrepareQuery

Returns: <ASYNC_RESULT>

This function executes the query in a seperate thread and returns a handle to the task.
You can bind callbacks to it, or wait on the task to finish (see below)
IMPORTANT! The Async query creates a copy of the connection in a worker thread, meaning even after the query has successfully executed, the connection itself will still not be connected to the database server, because only a copy of it that you cannot see has connected to the server. This might be changed in the future.

Handling Async results

result dbBindCallback [code, (arguments)]

result: <ASYNC_RESULT> - Value returned by dbExecuteAsync
code: <CODE> - Script to execute once the results are ready
arguments: <ANY> - Arguments passed to the code.

Returns: <NOTHING>

Code will be called with _this = [<RESULT>, arguments]

Example:

_result dbBindCallback [{
    params ["_result", "_args"];
    //_Args=1
    DB_RES = [dbResultToArray _result, _args]; 
    systemChat "got result!";
}, 1];

dbWaitForResult result

result: <ASYNC_RESULT> - Value returned by dbExecuteAsync

Returns: <RESULT>

Does exactly what you think it does. But also freezes the game even in scheduled! (to be changed in future updates)
Essentially converts a ASYNC_RESULT into a normal RESULT

Example: _result = dbWaitForResult _asyncResult;

Getting results

dbResultAffectedRows result

result: <RESULT> - The result

Returns: <NUMBER>

Returns number of affected rows. woah.

dbResultLastInsertId result

result: <RESULT> - The result

Returns: <NUMBER>

Returns last insert id. woah.

dbResultToArray result

result: <RESULT> - The result

Returns: <ARRAY>

Turns the result set into an array of rows.

Like this [row1,row2,row3];

Each row being an array made up of the values in that returned row.

row1 = [value1, value2, value3]

values can be of type NUMBER, STRING, BOOL, NIL (null values from the database will be returned as nil entries in the array)

dbResultToParsedArray result (Since version 1.0)

result: <RESULT> - The result

Returns: <ARRAY>

Like dbResultToArray. But tries to parse all string values from the database.
Turns "true" into true
Turns "[1,2,3,4]" into [1,2,3,4]
Turns "123" into 123
If string starts with [ it get's put through parseSimpleArray
If string starts with t/f/T/F/number it get's wrapped in [] and put through parseSimpleArray

Misc Commands

dbVersion (Since Version 1.0)

Returns: <STRING>

Returns current version of InterceptDB extension.

Future commands that aren't yet implemented

dbResultError result

result: <RESULT> - The result

Returns: <STRING>

Returns error as string if an error occurred while querying. Returns nil if there is no error. (Should it return empty string instead?)

dbResultErrorNum result

result: <RESULT> - The result

Returns: <NUMBER>

Returns error code if there is one. Returns 0 if there is none.

dbResultIsError result

result: <RESULT> - The result

Returns: <BOOL>

Checks if a error occured in the query.

connection dbConnectionEnableThrow bool

connection: <DBCONNECTION> - A connection
bool: <BOOL> - throwing enabled or disabled

Returns: <NOTHING>

Makes dbExecuteQuery and dbWaitForResult throw SQF Exceptions that can be caught using https://community.bistudio.com/wiki/catch

query dbBindNamedValue [name, value]

query: <QUERY>
name: <STRING> - Name of the value to bind
value: <STRING> OR <NUMBER> OR <BOOL> - Value to bind to the next unbound <name> in the query

Returns: <NOTHING>

This command modifies the value in query. If you want to keep the old query intact you need to dbCopyQuery first.
SELECT <value> FROM <table>;
dbBindNamedValue ["value", "onions"];
dbBindNamedValue ["table", "shoppinglist"];
-> SELECT onions FROM shoppinglist

Maybe other syntax would be better? $name? :name ?

:name seems to be standard elsewhere https://www.php.net/manual/de/pdostatement.bindparam.php https://www.javaworld.com/article/2077706/named-parameters-for-preparedstatement.html https://docs.oracle.com/cd/B10501_01/appdev.920/a96584/oci05bnd.htm https://www.sqlite.org/c3ref/bind_blob.html