2019-06-22 19:11:29 +00:00
# skript-db
> Sensible SQL support for Skript.
---
2021-01-18 12:59:08 +00:00
### Difference from original skript-db
- Fixed local variables disappearance in newer Skript versions (very hacky fix, but it works, so that's good!)
2022-03-20 10:28:25 +00:00
- Uses newer versions of dependencies (Increased performance and security)
2022-03-20 10:28:55 +00:00
- Replaced `synchronously execute` with `quickly execute` , which allows to speed up queries by 50ms with some risk
2022-12-08 20:00:30 +00:00
- If a sql query is detected to be running on non-main thread, it becomes synchronous automatically
- SQL Driver is configurable, comes with shaded MariaDB and PostgreSQL drivers
2022-03-20 10:27:35 +00:00
- A few variable type related bugs fixed
2022-03-20 10:29:17 +00:00
- Uses Java 11 instead of Java 8
2021-01-18 12:59:08 +00:00
2022-03-20 10:21:59 +00:00
### Installation
2022-08-12 08:38:18 +00:00
1. Use 1.8+ Minecraft server version.
2. Use Skript 2.5+ (1.8 Skript fork is needed if you're using 1.8)
3. Use Java 11+ (If you use 1.8, a spigot fork is needed to support Java 11+)
4. Put skript-db in plugins folder and restart the server
2020-05-02 17:25:36 +00:00
### Expression `Data Source` => `datasource`
Stores the connection information for a data source. This should be saved to a variable in a
`script load` event or manually through an effect command.
The url format for your database may vary! The example provided uses a MySQL database.
#### Syntax
```
[the] data(base|[ ]source) [(of|at)] %string%
```
#### Examples
```
set {sql} to the database "mysql://localhost:3306/mydatabase?user=admin& password=12345& useSSL=false"
```
---
2017-11-19 03:49:49 +00:00
### Effect `Execute Statement`
2017-11-22 00:58:47 +00:00
Executes a statement on a database and optionally stores the result in a variable. Expressions
embedded in the query will be escaped to avoid SQL injection.
2020-05-02 17:25:36 +00:00
2017-11-22 00:58:47 +00:00
If a single variable, such as `{test}` , is passed, the variable will be set to the number of
affected rows.
2020-05-02 17:25:36 +00:00
2017-11-22 00:58:47 +00:00
If a list variable, such as `{test::*}` , is passed, the query result will be mapped to the list
variable in the form `{test::<column name>::<row number>}`
2021-05-12 10:17:58 +00:00
2021-05-12 10:17:42 +00:00
If `quickly` is specified, the SQL query will be done without jumping back to main thread, which speeds it up by 50ms, however that makes code after it to also be on separate thread, you can jump back to main thread by adding `wait a tick`
2017-11-19 03:49:49 +00:00
#### Syntax
2017-11-22 00:58:47 +00:00
```
2021-05-12 10:17:42 +00:00
[quickly] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]
2017-11-22 00:58:47 +00:00
```
2017-11-19 03:49:49 +00:00
2017-11-22 00:58:47 +00:00
#### Examples
2017-11-19 03:49:49 +00:00
```
execute "select * from table" in {sql} and store the result in {output::* }
2017-11-22 00:58:47 +00:00
```
```
2020-05-02 17:25:36 +00:00
execute "select * from %{table variable}%" in {sql} and store the result in {output::* }
2017-11-19 03:49:49 +00:00
```
2017-11-19 03:52:52 +00:00
---
2017-11-22 00:58:47 +00:00
### Expression `Last Data Source Error` => `text`
Stores the error from the last executed statement, if there was one.
2017-11-19 03:49:49 +00:00
#### Syntax
```
2017-11-22 00:58:47 +00:00
[the] [last] (sql|db|data(base|[ ]source)) error
2017-11-19 03:49:49 +00:00
```
2017-11-19 03:52:52 +00:00
---
2017-11-22 00:58:47 +00:00
### Expression `Unsafe Expression` => `text`
Opts out of automatic SQL injection protection for a specific expression in a statement.
2017-11-19 03:49:49 +00:00
#### Syntax
2017-11-22 00:58:47 +00:00
```
unsafe %text%
```
2017-11-19 03:49:49 +00:00
2017-11-22 00:58:47 +00:00
#### Examples
```
execute "select %unsafe {columns variable}% from %{table variable}%" in {sql}
```
```
execute unsafe {fully dynamic query} in {sql}
```
2017-11-19 03:52:52 +00:00
2022-12-08 16:48:53 +00:00
#### FAQ: How to return sql data in a function?
You can't because functions don't allow delays, but you can use skript-reflect sections for this:
```
on load:
create new section stored in {-section::getPlayersFromDatabase}:
execute "SELECT uuid FROM table" in {-sql} and store the result in {_result::*}
return {_result::uuid::*}
command /showplayers [< text > ]:
trigger:
run section {-section::getPlayersFromDatabase} async and store result in {_uuids::*} and wait
send "%{_uuids::*}%"
```
2017-11-19 03:52:52 +00:00
---
2022-03-20 10:26:33 +00:00
### Configuration
plugins/skript-db/config.yml
```
# Only change this if you wish to use a different driver than Java's default, like MariaDB driver.
# If you use MariaDB, its driver is shaded together with skript-db, so you can just specify: "org.mariadb.jdbc.Driver"
sql-driver-class-name: "default"
2022-12-08 16:48:53 +00:00
```