forked from Limework/skript-db
Removed some duplicated code and improved readability
This commit is contained in:
parent
110f3982ad
commit
2dc1f70f4f
@ -51,105 +51,80 @@ public class EffExecuteStatement extends Effect {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
Skript.registerEffect(EffExecuteStatement.class,
|
Skript.registerEffect(EffExecuteStatement.class,
|
||||||
"execute %string% (in|on) %datasource% " +
|
"[quickly:quickly] execute %string% (in|on) %datasource% " +
|
||||||
"[with arg[ument][s] %-objects%] [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]",
|
|
||||||
"quickly execute %string% (in|on) %datasource% " +
|
|
||||||
"[with arg[ument][s] %-objects%] [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
"[with arg[ument][s] %-objects%] [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Expression<String> query;
|
private Expression<String> query;
|
||||||
private Expression<HikariDataSource> dataSource;
|
private Expression<HikariDataSource> dataSource;
|
||||||
private Expression<Object> queryArguments;
|
private Expression<Object> queryArguments;
|
||||||
private VariableString var;
|
private VariableString resultVariableName;
|
||||||
private boolean isLocal;
|
private boolean isLocal;
|
||||||
private boolean isList;
|
private boolean isList;
|
||||||
private boolean quickly;
|
private boolean quickly;
|
||||||
private boolean isSync = false;
|
private boolean isSync = false;
|
||||||
|
|
||||||
private void continueScriptExecution(Event e, Object populatedVariables) {
|
|
||||||
lastError = null;
|
|
||||||
if (populatedVariables instanceof String) {
|
|
||||||
lastError = (String) populatedVariables;
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (getNext() != null) {
|
|
||||||
((Map<String, Object>) populatedVariables).forEach((name, value) -> setVariable(e, name, value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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);
|
||||||
Pair<String, List<Object>> query = parseQuery(e);
|
|
||||||
String baseVariable = var != null ? var.toString(e).toLowerCase(Locale.ENGLISH) : null;
|
|
||||||
//if data source isn't set
|
//if data source isn't set
|
||||||
if (ds == null) return;
|
if (ds == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pair<String, List<Object>> parsedQuery = parseQuery(e);
|
||||||
|
String baseVariable = resultVariableName != null ? resultVariableName.toString(e).toLowerCase(Locale.ENGLISH) : null;
|
||||||
|
|
||||||
Object locals = Variables.removeLocals(e);
|
Object locals = Variables.removeLocals(e);
|
||||||
|
|
||||||
//execute SQL statement
|
//execute SQL statement
|
||||||
if (Bukkit.isPrimaryThread()) {
|
if (Bukkit.isPrimaryThread()) {
|
||||||
CompletableFuture<Object> sql = CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
|
CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, parsedQuery), threadPool)
|
||||||
sql.whenComplete((res, err) -> {
|
.whenComplete((resources, err) -> {
|
||||||
if (err != null) {
|
//handle last error syntax data
|
||||||
err.printStackTrace();
|
resetLastSQLError();
|
||||||
}
|
if (err instanceof SkriptDBQueryException) {
|
||||||
//handle last error syntax data
|
setLastSQLError(err.getMessage());
|
||||||
lastError = null;
|
}
|
||||||
if (res instanceof String) {
|
//if local variables are present
|
||||||
lastError = (String) res;
|
//bring back local variables
|
||||||
}
|
//populate SQL data into variables
|
||||||
//if local variables are present
|
if (!quickly) {
|
||||||
//bring back local variables
|
Bukkit.getScheduler().runTask(SkriptDB.getInstance(),
|
||||||
//populate SQL data into variables
|
() -> postExecution(e, locals, resources));
|
||||||
if (!quickly) {
|
} else {
|
||||||
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
postExecution(e, locals, resources);
|
||||||
if (locals != null && getNext() != null) {
|
}
|
||||||
Variables.setLocalVariables(e, locals);
|
});
|
||||||
}
|
|
||||||
if (!(res instanceof String)) {
|
|
||||||
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
|
|
||||||
}
|
|
||||||
TriggerItem.walk(getNext(), e);
|
|
||||||
//the line below is required to prevent memory leaks
|
|
||||||
Variables.removeLocals(e);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
if (locals != null && getNext() != null) {
|
|
||||||
Variables.setLocalVariables(e, locals);
|
|
||||||
}
|
|
||||||
if (!(res instanceof String)) {
|
|
||||||
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
|
|
||||||
}
|
|
||||||
TriggerItem.walk(getNext(), e);
|
|
||||||
//the line below is required to prevent memory leaks
|
|
||||||
Variables.removeLocals(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// sync executed SQL query, same as above, just sync
|
// sync executed SQL query, same as above, just sync
|
||||||
} else {
|
} else {
|
||||||
isSync = true;
|
isSync = true;
|
||||||
Object resources = executeStatement(ds, baseVariable, query);
|
Map<String, Object> resources = null;
|
||||||
//handle last error syntax data
|
try {
|
||||||
lastError = null;
|
resources = executeStatement(ds, baseVariable, parsedQuery);
|
||||||
if (resources instanceof String) {
|
resetLastSQLError();
|
||||||
lastError = (String) resources;
|
} catch (SkriptDBQueryException err) {
|
||||||
}
|
//handle last error syntax data
|
||||||
|
setLastSQLError(err.getMessage());
|
||||||
|
}
|
||||||
//if local variables are present
|
//if local variables are present
|
||||||
//bring back local variables
|
//bring back local variables
|
||||||
//populate SQL data into variables
|
//populate SQL data into variables
|
||||||
if (locals != null && getNext() != null) {
|
postExecution(e, locals, resources);
|
||||||
Variables.setLocalVariables(e, locals);
|
|
||||||
}
|
|
||||||
if (!(resources instanceof String)) {
|
|
||||||
((Map<String, Object>) resources).forEach((name, value) -> setVariable(e, name, value));
|
|
||||||
}
|
|
||||||
TriggerItem.walk(getNext(), e);
|
|
||||||
Variables.removeLocals(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void postExecution(Event e, Object locals, Map<String, Object> resources) {
|
||||||
|
if (locals != null && getNext() != null) {
|
||||||
|
Variables.setLocalVariables(e, locals);
|
||||||
|
}
|
||||||
|
if (resources != null) {
|
||||||
|
resources.forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
}
|
||||||
|
TriggerItem.walk(getNext(), e);
|
||||||
|
//the line below is required to prevent memory leaks
|
||||||
|
Variables.removeLocals(e);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TriggerItem walk(Event e) {
|
protected TriggerItem walk(Event e) {
|
||||||
debug(e, true);
|
debug(e, true);
|
||||||
@ -215,9 +190,9 @@ public class EffExecuteStatement extends Effect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object executeStatement(DataSource ds, String baseVariable, Pair<String, List<Object>> query) {
|
private Map<String, Object> executeStatement(DataSource ds, String baseVariable, Pair<String, List<Object>> query) throws SkriptDBQueryException {
|
||||||
if (ds == null) {
|
if (ds == null) {
|
||||||
return "Data source is not set";
|
throw new SkriptDBQueryException("Data source is not set");
|
||||||
}
|
}
|
||||||
try (Connection conn = ds.getConnection()) {
|
try (Connection conn = ds.getConnection()) {
|
||||||
try (PreparedStatement stmt = createStatement(conn, query)) {
|
try (PreparedStatement stmt = createStatement(conn, query)) {
|
||||||
@ -229,11 +204,11 @@ public class EffExecuteStatement extends Effect {
|
|||||||
return Map.of();
|
return Map.of();
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
return ex.getMessage();
|
throw new SkriptDBQueryException(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object processBaseVariable(String baseVariable, PreparedStatement stmt, boolean hasResultSet) throws SQLException {
|
private Map<String, Object> processBaseVariable(String baseVariable, PreparedStatement stmt, boolean hasResultSet) throws SQLException {
|
||||||
Map<String, Object> variableList = new HashMap<>();
|
Map<String, Object> variableList = new HashMap<>();
|
||||||
if (isList) {
|
if (isList) {
|
||||||
baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
|
baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
|
||||||
@ -306,7 +281,7 @@ public class EffExecuteStatement extends Effect {
|
|||||||
|
|
||||||
//fix mediumblob and similar column types, so they return a String correctly
|
//fix mediumblob and similar column types, so they return a String correctly
|
||||||
if (obj != null) {
|
if (obj != null) {
|
||||||
if (obj.getClass().getName().equals("[B")) {
|
if (obj instanceof byte[]) {
|
||||||
obj = new String((byte[]) obj);
|
obj = new String((byte[]) obj);
|
||||||
|
|
||||||
//in some servers instead of being byte array, it appears as SerialBlob (depends on mc version, 1.12.2 is bvte array, 1.16.5 SerialBlob)
|
//in some servers instead of being byte array, it appears as SerialBlob (depends on mc version, 1.12.2 is bvte array, 1.16.5 SerialBlob)
|
||||||
@ -321,6 +296,14 @@ public class EffExecuteStatement extends Effect {
|
|||||||
Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal);
|
Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void resetLastSQLError() {
|
||||||
|
lastError = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setLastSQLError(String error) {
|
||||||
|
lastError = error;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(Event e, boolean debug) {
|
public String toString(Event e, boolean debug) {
|
||||||
return "execute " + query.toString(e, debug) + " in " + dataSource.toString(e, debug);
|
return "execute " + query.toString(e, debug) + " in " + dataSource.toString(e, debug);
|
||||||
@ -328,8 +311,7 @@ public class EffExecuteStatement extends Effect {
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
|
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
|
||||||
SkriptParser.ParseResult parseResult) {
|
|
||||||
Expression<String> statementExpr = (Expression<String>) exprs[0];
|
Expression<String> statementExpr = (Expression<String>) exprs[0];
|
||||||
if (statementExpr instanceof VariableString || statementExpr instanceof ExprUnsafe) {
|
if (statementExpr instanceof VariableString || statementExpr instanceof ExprUnsafe) {
|
||||||
query = statementExpr;
|
query = statementExpr;
|
||||||
@ -347,10 +329,10 @@ public class EffExecuteStatement extends Effect {
|
|||||||
queryArguments = (Expression<Object>) exprs[2];
|
queryArguments = (Expression<Object>) exprs[2];
|
||||||
}
|
}
|
||||||
Expression<?> resultHolder = exprs[3];
|
Expression<?> resultHolder = exprs[3];
|
||||||
quickly = matchedPattern == 1;
|
quickly = parseResult.hasTag("quickly");
|
||||||
if (resultHolder instanceof Variable) {
|
if (resultHolder instanceof Variable) {
|
||||||
Variable<?> varExpr = (Variable<?>) resultHolder;
|
Variable<?> varExpr = (Variable<?>) resultHolder;
|
||||||
var = varExpr.getName();
|
resultVariableName = varExpr.getName();
|
||||||
isLocal = varExpr.isLocal();
|
isLocal = varExpr.isLocal();
|
||||||
isList = varExpr.isList();
|
isList = varExpr.isList();
|
||||||
} else if (resultHolder != null) {
|
} else if (resultHolder != null) {
|
||||||
@ -359,4 +341,15 @@ public class EffExecuteStatement extends Effect {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class SkriptDBQueryException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -1869895286406538884L;
|
||||||
|
|
||||||
|
public SkriptDBQueryException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user