Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e95b818eb | ||
|
|
688ea9d46b | ||
|
|
dd6d574479 | ||
|
|
1f6091eb95 | ||
|
|
74d4918f44 |
45
README.md
45
README.md
@@ -3,35 +3,21 @@
|
|||||||
> Sensible SQL support for Skript.
|
> Sensible SQL support for Skript.
|
||||||
---
|
---
|
||||||
|
|
||||||
### 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"
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### Effect `Execute Statement`
|
### Effect `Execute Statement`
|
||||||
Executes a statement on a database and optionally stores the result in a variable. Expressions
|
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.
|
embedded in the query will be escaped to avoid SQL injection.
|
||||||
|
<p>
|
||||||
If a single variable, such as `{test}`, is passed, the variable will be set to the number of
|
If a single variable, such as `{test}`, is passed, the variable will be set to the number of
|
||||||
affected rows.
|
affected rows.
|
||||||
|
<p>
|
||||||
If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list
|
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>}`
|
variable in the form `{test::<column name>::<row number>}`
|
||||||
|
|
||||||
|
Specifying `synchronously` will make skript-db execute the query on the event thread, which is useful for async
|
||||||
|
events. Note that skript-db will ignore this flag if you attempt to run this on the main thread.
|
||||||
#### Syntax
|
#### Syntax
|
||||||
```
|
```
|
||||||
execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]
|
[synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Examples
|
#### Examples
|
||||||
@@ -39,7 +25,7 @@ execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|
|
|||||||
execute "select * from table" in {sql} and store the result in {output::*}
|
execute "select * from table" in {sql} and store the result in {output::*}
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
|
execute "select * where player=%{player}%" in {sql} and store the result in {output::*}
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -70,3 +56,20 @@ execute unsafe {fully dynamic query} in {sql}
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### 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% [with [a] [max[imum]] [connection] life[ ]time of %timespan%]"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
```
|
||||||
|
set {sql} to the database "mysql://localhost:3306/mydatabase?user=admin&password=12345&useSSL=false"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
group 'com.btk5h.skript-db'
|
group 'com.btk5h.skript-db'
|
||||||
version '0.1.1'
|
version '0.2.0'
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
|
|||||||
@@ -41,17 +41,20 @@ import ch.njol.util.Kleenean;
|
|||||||
* If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list
|
* 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>}`
|
* variable in the form `{test::<column name>::<row number>}`
|
||||||
*
|
*
|
||||||
|
* Specifying `synchronously` will make skript-db execute the query on the event thread, which is useful for async
|
||||||
|
* events. Note that skript-db will ignore this flag if you attempt to run this on the main thread.
|
||||||
|
*
|
||||||
* @name Execute Statement
|
* @name Execute Statement
|
||||||
* @pattern execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
|
* @pattern [synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
|
||||||
* [the] [var[iable]] %-objects%]
|
* [the] [var[iable]] %-objects%]
|
||||||
* @example execute "select * from table" in {sql} and store the result in {output::*}
|
* @example execute "select * from table" in {sql} and store the result in {output::*}
|
||||||
* @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
|
* @example execute "select * where player=%{player}%" in {sql} and store the result in {output::*}
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
public class EffExecuteStatement extends Delay {
|
public class EffExecuteStatement extends Delay {
|
||||||
static {
|
static {
|
||||||
Skript.registerEffect(EffExecuteStatement.class,
|
Skript.registerEffect(EffExecuteStatement.class,
|
||||||
"execute %string% (in|on) %datasource% " +
|
"[(1¦synchronously)] execute %string% (in|on) %datasource% " +
|
||||||
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,25 +68,43 @@ public class EffExecuteStatement extends Delay {
|
|||||||
private VariableString var;
|
private VariableString var;
|
||||||
private boolean isLocal;
|
private boolean isLocal;
|
||||||
private boolean isList;
|
private boolean isList;
|
||||||
|
private boolean isSync;
|
||||||
|
|
||||||
|
private void continueScriptExecution(Event e, String res) {
|
||||||
|
lastError = res;
|
||||||
|
|
||||||
|
if (getNext() != null) {
|
||||||
|
TriggerItem.walk(getNext(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void execute(Event e) {
|
protected void execute(Event e) {
|
||||||
CompletableFuture<String> sql =
|
boolean isMainThread = Bukkit.isPrimaryThread();
|
||||||
CompletableFuture.supplyAsync(() -> executeStatement(e), threadPool);
|
|
||||||
|
|
||||||
sql.whenComplete((res, err) -> {
|
if (isSync && !isMainThread) {
|
||||||
if (err != null) {
|
String result = executeStatement(e);
|
||||||
err.printStackTrace();
|
continueScriptExecution(e, result);
|
||||||
|
} else {
|
||||||
|
if (isMainThread) {
|
||||||
|
Skript.warning("A SQL query was attempted on the main thread!");
|
||||||
}
|
}
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
CompletableFuture<String> sql =
|
||||||
lastError = res;
|
CompletableFuture.supplyAsync(() -> executeStatement(e), threadPool);
|
||||||
|
|
||||||
if (getNext() != null) {
|
sql.whenComplete((res, err) -> {
|
||||||
TriggerItem.walk(getNext(), e);
|
if (err != null) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isSync) {
|
||||||
|
continueScriptExecution(e, res);
|
||||||
|
} else {
|
||||||
|
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> continueScriptExecution(e, res));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -251,6 +272,7 @@ public class EffExecuteStatement extends Delay {
|
|||||||
}
|
}
|
||||||
dataSource = (Expression<HikariDataSource>) exprs[1];
|
dataSource = (Expression<HikariDataSource>) exprs[1];
|
||||||
Expression<?> expr = exprs[2];
|
Expression<?> expr = exprs[2];
|
||||||
|
isSync = parseResult.mark == 1;
|
||||||
if (expr instanceof Variable) {
|
if (expr instanceof Variable) {
|
||||||
Variable<?> varExpr = (Variable<?>) expr;
|
Variable<?> varExpr = (Variable<?>) expr;
|
||||||
var = SkriptUtil.getVariableName(varExpr);
|
var = SkriptUtil.getVariableName(varExpr);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
|||||||
static {
|
static {
|
||||||
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
||||||
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
||||||
"[with [a] [max[imum]] [connection] life[ ]time of %timespan%]");
|
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
name: skript-db
|
name: skript-db
|
||||||
version: 0.1.1
|
version: 0.2.0
|
||||||
main: com.btk5h.skriptdb.SkriptDB
|
main: com.btk5h.skriptdb.SkriptDB
|
||||||
depend: [Skript]
|
depend: [Skript]
|
||||||
|
|||||||
Reference in New Issue
Block a user