2017-11-19 03:49:49 +00:00
|
|
|
package com.btk5h.skriptdb.skript;
|
|
|
|
|
2020-05-02 17:25:36 +00:00
|
|
|
import ch.njol.skript.Skript;
|
|
|
|
import ch.njol.skript.effects.Delay;
|
|
|
|
import ch.njol.skript.lang.*;
|
|
|
|
import ch.njol.skript.variables.Variables;
|
|
|
|
import ch.njol.util.Kleenean;
|
|
|
|
import ch.njol.util.Pair;
|
2017-11-19 03:49:49 +00:00
|
|
|
import com.btk5h.skriptdb.SkriptDB;
|
|
|
|
import com.btk5h.skriptdb.SkriptUtil;
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.event.Event;
|
|
|
|
|
2020-05-02 17:25:36 +00:00
|
|
|
import javax.sql.DataSource;
|
|
|
|
import javax.sql.rowset.CachedRowSet;
|
2017-11-19 03:49:49 +00:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSetMetaData;
|
|
|
|
import java.sql.SQLException;
|
2020-05-02 17:25:36 +00:00
|
|
|
import java.util.*;
|
2017-11-19 03:49:49 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
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.
|
2018-05-09 22:35:47 +00:00
|
|
|
* <p>
|
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.
|
2018-05-09 22:35:47 +00:00
|
|
|
* <p>
|
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>}`
|
|
|
|
*
|
|
|
|
* @name Execute Statement
|
2020-05-18 10:20:01 +00:00
|
|
|
* @pattern [synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
|
2017-11-22 00:58:47 +00:00
|
|
|
* [the] [var[iable]] %-objects%]
|
|
|
|
* @example execute "select * from table" in {sql} and store the result in {output::*}
|
2020-05-02 17:25:36 +00:00
|
|
|
* @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
|
2017-11-22 00:58:47 +00:00
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
2020-07-31 08:50:44 +00:00
|
|
|
public class EffExecuteStatement extends Effect {
|
2017-11-19 03:49:49 +00:00
|
|
|
static {
|
2020-05-02 17:25:36 +00:00
|
|
|
Skript.registerEffect(EffExecuteStatement.class,
|
|
|
|
"execute %string% (in|on) %datasource% " +
|
2020-05-18 10:20:01 +00:00
|
|
|
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]", "synchronously execute %string% (in|on) %datasource% " +
|
2020-05-02 17:25:36 +00:00
|
|
|
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String lastError;
|
|
|
|
|
|
|
|
private static final ExecutorService threadPool =
|
2020-05-18 12:34:43 +00:00
|
|
|
Executors.newFixedThreadPool(SkriptDB.getInstance().getConfig().getInt("thread-pool-size"));
|
2017-11-19 03:49:49 +00:00
|
|
|
|
|
|
|
private Expression<String> query;
|
|
|
|
private Expression<HikariDataSource> dataSource;
|
|
|
|
private VariableString var;
|
|
|
|
private boolean isLocal;
|
|
|
|
private boolean isList;
|
2020-05-18 10:20:01 +00:00
|
|
|
private boolean isSync;
|
2017-11-19 03:49:49 +00:00
|
|
|
|
2020-07-31 08:50:44 +00:00
|
|
|
private void continueScriptExecution(Event e, Object populatedVariables) {
|
|
|
|
lastError = null;
|
|
|
|
if (populatedVariables instanceof String) {
|
|
|
|
lastError = (String) populatedVariables;
|
|
|
|
} else {
|
2020-05-18 10:20:01 +00:00
|
|
|
|
2020-07-31 08:50:44 +00:00
|
|
|
if (getNext() != null) {
|
|
|
|
((Map<String, Object>) populatedVariables).forEach((name, value) -> setVariable(e, name, value));
|
|
|
|
}
|
2020-05-18 10:20:01 +00:00
|
|
|
}
|
2020-07-31 08:50:44 +00:00
|
|
|
TriggerItem.walk(getNext(), e);
|
2020-05-18 10:20:01 +00:00
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
@Override
|
|
|
|
protected void execute(Event e) {
|
2020-05-02 17:25:36 +00:00
|
|
|
DataSource ds = dataSource.getSingle(e);
|
|
|
|
Pair<String, List<Object>> query = parseQuery(e);
|
|
|
|
String baseVariable = var != null ? var.toString(e).toLowerCase(Locale.ENGLISH) : null;
|
2017-11-19 03:49:49 +00:00
|
|
|
|
2020-05-02 17:25:36 +00:00
|
|
|
if (ds == null)
|
|
|
|
return;
|
2020-05-18 10:20:01 +00:00
|
|
|
if (isSync) {
|
2020-07-31 08:50:44 +00:00
|
|
|
Object populatedVariables = executeStatement(ds, baseVariable, query);
|
|
|
|
continueScriptExecution(e, populatedVariables);
|
2020-05-18 10:20:01 +00:00
|
|
|
} else {
|
|
|
|
Object locals = Variables.removeLocals(e);
|
2020-07-31 08:50:44 +00:00
|
|
|
CompletableFuture<Object> sql =
|
2020-05-18 10:20:01 +00:00
|
|
|
CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
|
2017-11-19 03:49:49 +00:00
|
|
|
|
2020-05-18 10:20:01 +00:00
|
|
|
sql.whenComplete((res, err) -> {
|
|
|
|
if (err != null) {
|
|
|
|
err.printStackTrace();
|
|
|
|
}
|
2020-05-02 17:25:36 +00:00
|
|
|
|
2020-05-18 10:20:01 +00:00
|
|
|
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
2020-07-31 08:50:44 +00:00
|
|
|
lastError = null;
|
|
|
|
if (res instanceof String) {
|
|
|
|
lastError = (String) res;
|
|
|
|
}
|
2019-06-22 19:09:17 +00:00
|
|
|
|
2020-05-18 10:20:01 +00:00
|
|
|
if (getNext() != null) {
|
|
|
|
if (locals != null)
|
|
|
|
Variables.setLocalVariables(e, locals);
|
2020-07-31 08:50:44 +00:00
|
|
|
|
|
|
|
if (!(res instanceof String)) {
|
|
|
|
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
|
|
|
|
}
|
|
|
|
//doLater.clear();
|
2020-05-18 10:20:01 +00:00
|
|
|
TriggerItem.walk(getNext(), e);
|
|
|
|
Variables.removeLocals(e);
|
|
|
|
}
|
|
|
|
});
|
2017-11-19 03:49:49 +00:00
|
|
|
});
|
2020-05-18 10:20:01 +00:00
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected TriggerItem walk(Event e) {
|
|
|
|
debug(e, true);
|
2020-05-18 10:20:01 +00:00
|
|
|
if (!isSync) {
|
|
|
|
Delay.addDelayedEvent(e);
|
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
execute(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-02 17:25:36 +00:00
|
|
|
private Pair<String, List<Object>> parseQuery(Event e) {
|
2017-11-19 03:49:49 +00:00
|
|
|
if (!(query instanceof VariableString)) {
|
2020-05-02 17:25:36 +00:00
|
|
|
return new Pair<>(query.getSingle(e), null);
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
2020-05-02 17:25:36 +00:00
|
|
|
VariableString q = (VariableString) query;
|
|
|
|
if (q.isSimple()) {
|
|
|
|
return new Pair<>(q.toString(e), null);
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
List<Object> parameters = new ArrayList<>();
|
2020-05-02 17:25:36 +00:00
|
|
|
Object[] objects = SkriptUtil.getTemplateString(q);
|
|
|
|
|
2018-05-09 22:35:47 +00:00
|
|
|
for (int i = 0; i < objects.length; i++) {
|
|
|
|
Object o = objects[i];
|
2017-11-19 03:49:49 +00:00
|
|
|
if (o instanceof String) {
|
|
|
|
sb.append(o);
|
|
|
|
} else {
|
|
|
|
Expression<?> expr = SkriptUtil.getExpressionFromInfo(o);
|
2018-05-09 22:35:47 +00:00
|
|
|
|
|
|
|
String before = getString(objects, i - 1);
|
|
|
|
String after = getString(objects, i + 1);
|
|
|
|
boolean standaloneString = false;
|
|
|
|
|
|
|
|
if (before != null && after != null) {
|
|
|
|
if (before.endsWith("'") && after.endsWith("'")) {
|
|
|
|
standaloneString = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object expressionValue = expr.getSingle(e);
|
|
|
|
|
2017-11-19 03:49:49 +00:00
|
|
|
if (expr instanceof ExprUnsafe) {
|
2018-05-09 22:35:47 +00:00
|
|
|
sb.append(expressionValue);
|
|
|
|
|
|
|
|
if (standaloneString && expressionValue instanceof String) {
|
|
|
|
String rawExpression = ((ExprUnsafe) expr).getRawExpression();
|
|
|
|
Skript.warning(
|
|
|
|
String.format("Unsafe may have been used unnecessarily. Try replacing 'unsafe %1$s' with %1$s",
|
|
|
|
rawExpression));
|
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
} else {
|
2018-05-09 22:35:47 +00:00
|
|
|
parameters.add(expressionValue);
|
2017-11-19 03:49:49 +00:00
|
|
|
sb.append('?');
|
2018-05-09 22:35:47 +00:00
|
|
|
|
|
|
|
if (standaloneString) {
|
|
|
|
Skript.warning("Do not surround expressions with quotes!");
|
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 17:25:36 +00:00
|
|
|
return new Pair<>(sb.toString(), parameters);
|
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
|
2020-07-31 08:50:44 +00:00
|
|
|
private Object executeStatement(DataSource ds, String baseVariable, Pair<String, List<Object>> query) {
|
2020-05-02 17:25:36 +00:00
|
|
|
if (ds == null) {
|
|
|
|
return "Data source is not set";
|
|
|
|
}
|
2020-07-31 08:50:44 +00:00
|
|
|
Map<String, Object> variableList = new HashMap<>();
|
2020-05-02 17:25:36 +00:00
|
|
|
try (Connection conn = ds.getConnection();
|
|
|
|
PreparedStatement stmt = createStatement(conn, query)) {
|
|
|
|
|
|
|
|
boolean hasResultSet = stmt.execute();
|
|
|
|
|
|
|
|
if (baseVariable != null) {
|
|
|
|
if (isList) {
|
|
|
|
baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasResultSet) {
|
|
|
|
CachedRowSet crs = SkriptDB.getRowSetFactory().createCachedRowSet();
|
|
|
|
crs.populate(stmt.getResultSet());
|
|
|
|
|
|
|
|
if (isList) {
|
2020-07-31 08:50:44 +00:00
|
|
|
ResultSetMetaData meta = crs.getMetaData();
|
|
|
|
int columnCount = meta.getColumnCount();
|
|
|
|
|
|
|
|
for (int i = 1; i <= columnCount; i++) {
|
|
|
|
String label = meta.getColumnLabel(i);
|
|
|
|
variableList.put(baseVariable + label, label);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rowNumber = 1;
|
|
|
|
try {
|
|
|
|
while (crs.next()) {
|
|
|
|
for (int i = 1; i <= columnCount; i++) {
|
|
|
|
variableList.put(baseVariable + meta.getColumnLabel(i).toLowerCase(Locale.ENGLISH)
|
|
|
|
+ Variable.SEPARATOR + rowNumber, crs.getObject(i));
|
|
|
|
}
|
|
|
|
rowNumber++;
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
return ex.getMessage();
|
|
|
|
}
|
2020-05-02 17:25:36 +00:00
|
|
|
} else {
|
|
|
|
crs.last();
|
2020-07-31 08:50:44 +00:00
|
|
|
variableList.put(baseVariable, crs.getRow());
|
2020-05-02 17:25:36 +00:00
|
|
|
}
|
|
|
|
} else if (!isList) {
|
2020-07-31 08:50:44 +00:00
|
|
|
variableList.put(baseVariable, stmt.getUpdateCount());
|
2020-05-02 17:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
return ex.getMessage();
|
|
|
|
}
|
2020-07-31 08:50:44 +00:00
|
|
|
return variableList;
|
2020-05-02 17:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private PreparedStatement createStatement(Connection conn, Pair<String, List<Object>> query) throws SQLException {
|
|
|
|
PreparedStatement stmt = conn.prepareStatement(query.getFirst());
|
|
|
|
List<Object> parameters = query.getSecond();
|
|
|
|
|
|
|
|
if (parameters != null) {
|
|
|
|
for (int i = 0; i < parameters.size(); i++) {
|
|
|
|
stmt.setObject(i + 1, parameters.get(i));
|
|
|
|
}
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:35:47 +00:00
|
|
|
private String getString(Object[] objects, int index) {
|
|
|
|
if (index < 0 || index >= objects.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object object = objects[index];
|
|
|
|
|
|
|
|
if (object instanceof String) {
|
|
|
|
return (String) object;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-19 03:49:49 +00:00
|
|
|
private void setVariable(Event e, String name, Object obj) {
|
|
|
|
Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-02 17:25:36 +00:00
|
|
|
public String toString(Event e, boolean debug) {
|
2017-11-19 03:49:49 +00:00
|
|
|
return "execute " + query.toString(e, debug) + " in " + dataSource.toString(e, debug);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
|
|
|
|
SkriptParser.ParseResult parseResult) {
|
|
|
|
Expression<String> statementExpr = (Expression<String>) exprs[0];
|
|
|
|
if (statementExpr instanceof VariableString || statementExpr instanceof ExprUnsafe) {
|
|
|
|
query = statementExpr;
|
|
|
|
} else {
|
|
|
|
Skript.error("Database statements must be string literals. If you must use an expression, " +
|
|
|
|
"you may use \"%unsafe (your expression)%\", but keep in mind, you may be vulnerable " +
|
|
|
|
"to SQL injection attacks!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dataSource = (Expression<HikariDataSource>) exprs[1];
|
|
|
|
Expression<?> expr = exprs[2];
|
2020-05-18 10:20:01 +00:00
|
|
|
isSync = matchedPattern == 1;
|
2017-11-19 03:49:49 +00:00
|
|
|
if (expr instanceof Variable) {
|
|
|
|
Variable<?> varExpr = (Variable<?>) expr;
|
2020-05-02 17:25:36 +00:00
|
|
|
var = varExpr.getName();
|
2017-11-19 03:49:49 +00:00
|
|
|
isLocal = varExpr.isLocal();
|
|
|
|
isList = varExpr.isList();
|
|
|
|
} else if (expr != null) {
|
|
|
|
Skript.error(expr + " is not a variable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|