SQL

This module allows you to connect to the DBMS and make synchronous and asynchronous requests.

Special thanks to the btk5h (Bryan Terce), FranKusmiruk, Govindas, TPGamesNL for the support of the addon Skript-db . Part of the code and the idea was borrowed from here.

Connecting to a DBMS

Initializes the connection to the DBMS and returns this connection. Returns an object of the type datasource (com.zaxxer.hikari.HikariDataSource class).

[the] data(base|[ ]source) [(of|at)] %string% [with [a] [max[imum]] [connection] life[ ]time of %timespan%]
set {_database} to "mysql://IP:3306/DATABASENAME?user=USERNAME&password=PASSWORD&useSSL=false"

Asynchronous request to the DBMS

[async[hronously]] execute %string% [with (data|(param[eter][s])) %objects%] (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %objects%]

The first parameter - is the query being executed.
The second parameter - is the insertion of values for escaping. The first character is ? will be replaced by the first value from the passed list in the second argument, the second ? to the second from the list, etc. For example:\

execute "select * from ? where name=?" with "books","worldatwar" in {_database}

The first ? will be replaced with books before executing the request, and the second one with ‘worldatwar’.

Use value escaping for any queries where user input is used to avoid SQL-injection


The third parameter - connecting to a DBMS, an object of type datasource from a previous expression.
The last parameter - where the query result will be placed, you must specify the list variable. After the query, the column names will become indexes of this list, the column values will be stored in a new sub-list with the column name.

For example, we have the following table:

id (INT)name (VARCHAR)age (INT)
1Lotzy22
2w0w019


When executing the code:

execute "select * from table" in {_database} and store the result in {_output::*}

The list {_output::*} will look like this:

{_output::id::1} = 1  
{_output::name::1} = "Lotzy"  
{_output::age::1} = 22 
{_output::id::2} = 2  
{_output::name::2} = "w0w0"  
{_output::age::2} = 19
{_output::*} = "id", "name" and "age" 

An asynchronous request cannot be used in functions where a value is returned. The result of an asynchronous request will be <none>

Synchronous request to the DBMS

sync[hronously] execute %string% [with (data|(param[eter][s])) %objects%] (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %objects%]

Block the main thread for the duration of the request, use wisely.