forked from Limework/skript-db
Add synchronous support on current thread
This commit is contained in:
parent
0d8d834929
commit
efa37217c2
@ -43,6 +43,7 @@ task buildReadme(type: Javadoc) {
|
|||||||
options.addStringOption('file', 'README.md')
|
options.addStringOption('file', 'README.md')
|
||||||
options.addStringOption('markdown', '-quiet')
|
options.addStringOption('markdown', '-quiet')
|
||||||
}
|
}
|
||||||
|
|
||||||
task fatJar(type: Jar) {
|
task fatJar(type: Jar) {
|
||||||
manifest {
|
manifest {
|
||||||
attributes 'Implementation-Title': 'Gradle Jar File Example',
|
attributes 'Implementation-Title': 'Gradle Jar File Example',
|
||||||
@ -52,4 +53,4 @@ task fatJar(type: Jar) {
|
|||||||
baseName = project.name + '-all'
|
baseName = project.name + '-all'
|
||||||
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
|
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
|
||||||
with jar
|
with jar
|
||||||
}
|
}
|
@ -34,7 +34,7 @@ import java.util.concurrent.Executors;
|
|||||||
* variable in the form `{test::<column name>::<row number>}`
|
* variable in the form `{test::<column name>::<row number>}`
|
||||||
*
|
*
|
||||||
* @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 * from %{table variable}%" in {sql} and store the result in {output::*}
|
||||||
@ -44,6 +44,7 @@ public class EffExecuteStatement extends Delay {
|
|||||||
static {
|
static {
|
||||||
Skript.registerEffect(EffExecuteStatement.class,
|
Skript.registerEffect(EffExecuteStatement.class,
|
||||||
"execute %string% (in|on) %datasource% " +
|
"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%]");
|
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +59,17 @@ public class EffExecuteStatement extends Delay {
|
|||||||
private boolean isLocal;
|
private boolean isLocal;
|
||||||
private boolean isList;
|
private boolean isList;
|
||||||
private Map<String, Object> doLater = new HashMap<>();
|
private Map<String, Object> doLater = new HashMap<>();
|
||||||
|
private boolean isSync;
|
||||||
|
|
||||||
|
private void continueScriptExecution(Event e, String res) {
|
||||||
|
lastError = res;
|
||||||
|
|
||||||
|
if (getNext() != null) {
|
||||||
|
doLater.forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
doLater.clear();
|
||||||
|
TriggerItem.walk(getNext(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void execute(Event e) {
|
protected void execute(Event e) {
|
||||||
DataSource ds = dataSource.getSingle(e);
|
DataSource ds = dataSource.getSingle(e);
|
||||||
@ -67,35 +78,41 @@ public class EffExecuteStatement extends Delay {
|
|||||||
|
|
||||||
if (ds == null)
|
if (ds == null)
|
||||||
return;
|
return;
|
||||||
|
if (isSync) {
|
||||||
|
String result = executeStatement(ds, baseVariable, query);
|
||||||
|
continueScriptExecution(e, result);
|
||||||
|
} else {
|
||||||
|
Object locals = Variables.removeLocals(e);
|
||||||
|
CompletableFuture<String> sql =
|
||||||
|
CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
|
||||||
|
|
||||||
Object locals = Variables.removeLocals(e);
|
sql.whenComplete((res, err) -> {
|
||||||
CompletableFuture<String> sql =
|
if (err != null) {
|
||||||
CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
|
err.printStackTrace();
|
||||||
|
|
||||||
sql.whenComplete((res, err) -> {
|
|
||||||
if (err != null) {
|
|
||||||
err.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
|
||||||
lastError = res;
|
|
||||||
|
|
||||||
if (getNext() != null) {
|
|
||||||
if (locals != null)
|
|
||||||
Variables.setLocalVariables(e, locals);
|
|
||||||
doLater.forEach((name, value) -> setVariable(e, name, value));
|
|
||||||
doLater.clear();
|
|
||||||
TriggerItem.walk(getNext(), e);
|
|
||||||
Variables.removeLocals(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
||||||
|
lastError = res;
|
||||||
|
|
||||||
|
if (getNext() != null) {
|
||||||
|
if (locals != null)
|
||||||
|
Variables.setLocalVariables(e, locals);
|
||||||
|
doLater.forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
doLater.clear();
|
||||||
|
TriggerItem.walk(getNext(), e);
|
||||||
|
Variables.removeLocals(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TriggerItem walk(Event e) {
|
protected TriggerItem walk(Event e) {
|
||||||
debug(e, true);
|
debug(e, true);
|
||||||
Delay.addDelayedEvent(e);
|
if (!isSync) {
|
||||||
|
Delay.addDelayedEvent(e);
|
||||||
|
}
|
||||||
execute(e);
|
execute(e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -260,6 +277,8 @@ public class EffExecuteStatement extends Delay {
|
|||||||
}
|
}
|
||||||
dataSource = (Expression<HikariDataSource>) exprs[1];
|
dataSource = (Expression<HikariDataSource>) exprs[1];
|
||||||
Expression<?> expr = exprs[2];
|
Expression<?> expr = exprs[2];
|
||||||
|
System.out.println(matchedPattern);
|
||||||
|
isSync = matchedPattern == 1;
|
||||||
if (expr instanceof Variable) {
|
if (expr instanceof Variable) {
|
||||||
Variable<?> varExpr = (Variable<?>) expr;
|
Variable<?> varExpr = (Variable<?>) expr;
|
||||||
var = varExpr.getName();
|
var = varExpr.getName();
|
||||||
|
Loading…
Reference in New Issue
Block a user