forked from Limework/skript-db
Initial commit
This commit is contained in:
209
src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java
Normal file
209
src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java
Normal file
@@ -0,0 +1,209 @@
|
||||
package com.btk5h.skriptdb.skript;
|
||||
|
||||
import com.btk5h.skriptdb.SkriptDB;
|
||||
import com.btk5h.skriptdb.SkriptUtil;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.sql.rowset.CachedRowSet;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.effects.Delay;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.TriggerItem;
|
||||
import ch.njol.skript.lang.Variable;
|
||||
import ch.njol.skript.lang.VariableString;
|
||||
import ch.njol.skript.variables.Variables;
|
||||
import ch.njol.util.Kleenean;
|
||||
|
||||
public class EffExecuteStatement extends Delay {
|
||||
static {
|
||||
Skript.registerEffect(EffExecuteStatement.class,
|
||||
"execute %string% (in|on) %datasource% " +
|
||||
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
||||
}
|
||||
|
||||
static String lastError;
|
||||
|
||||
private static final ExecutorService threadPool =
|
||||
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
private Expression<String> query;
|
||||
private Expression<HikariDataSource> dataSource;
|
||||
private VariableString var;
|
||||
private boolean isLocal;
|
||||
private boolean isList;
|
||||
|
||||
@Override
|
||||
protected void execute(Event e) {
|
||||
CompletableFuture<String> sql =
|
||||
CompletableFuture.supplyAsync(() -> executeStatement(e), threadPool);
|
||||
|
||||
sql.whenComplete((res, err) -> {
|
||||
if (err != null) {
|
||||
err.printStackTrace();
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
||||
lastError = res;
|
||||
|
||||
if (getNext() != null) {
|
||||
TriggerItem.walk(getNext(), e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TriggerItem walk(Event e) {
|
||||
debug(e, true);
|
||||
SkriptUtil.delay(e);
|
||||
execute(e);
|
||||
return null;
|
||||
}
|
||||
|
||||
private String executeStatement(Event e) {
|
||||
HikariDataSource ds = dataSource.getSingle(e);
|
||||
|
||||
if (ds == null) {
|
||||
return "Data source is not set";
|
||||
}
|
||||
|
||||
try (Connection conn = ds.getConnection();
|
||||
PreparedStatement stmt = createStatement(e, conn)) {
|
||||
|
||||
boolean hasResultSet = stmt.execute();
|
||||
|
||||
if (var != null) {
|
||||
String baseVariable = var.toString(e)
|
||||
.toLowerCase(Locale.ENGLISH);
|
||||
if (isList) {
|
||||
baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
|
||||
}
|
||||
|
||||
if (hasResultSet) {
|
||||
CachedRowSet crs = SkriptDB.getRowSetFactory().createCachedRowSet();
|
||||
crs.populate(stmt.getResultSet());
|
||||
|
||||
if (isList) {
|
||||
populateVariable(e, crs, baseVariable);
|
||||
} else {
|
||||
crs.last();
|
||||
setVariable(e, baseVariable, crs.getRow());
|
||||
}
|
||||
} else if (!isList) {
|
||||
setVariable(e, baseVariable, stmt.getUpdateCount());
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
return ex.getMessage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private PreparedStatement createStatement(Event e, Connection conn) throws SQLException {
|
||||
if (!(query instanceof VariableString)) {
|
||||
return conn.prepareStatement(query.getSingle(e));
|
||||
}
|
||||
|
||||
if (((VariableString) query).isSimple()) {
|
||||
return conn.prepareStatement(SkriptUtil.getSimpleString(((VariableString) query)));
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
Object[] objects = SkriptUtil.getTemplateString(((VariableString) query));
|
||||
for (Object o : objects) {
|
||||
if (o instanceof String) {
|
||||
sb.append(o);
|
||||
} else {
|
||||
Expression<?> expr = SkriptUtil.getExpressionFromInfo(o);
|
||||
if (expr instanceof ExprUnsafe) {
|
||||
sb.append(expr.getSingle(e));
|
||||
} else {
|
||||
parameters.add(expr.getSingle(e));
|
||||
sb.append('?');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sb.toString());
|
||||
|
||||
for (int i = 0; i < parameters.size(); i++) {
|
||||
stmt.setObject(i + 1, parameters.get(i));
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
private void setVariable(Event e, String name, Object obj) {
|
||||
Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal);
|
||||
}
|
||||
|
||||
private void populateVariable(Event e, CachedRowSet crs, String baseVariable)
|
||||
throws SQLException {
|
||||
ResultSetMetaData meta = crs.getMetaData();
|
||||
int columnCount = meta.getColumnCount();
|
||||
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
String label = meta.getColumnLabel(i);
|
||||
setVariable(e, baseVariable + label, label);
|
||||
}
|
||||
|
||||
int rowNumber = 1;
|
||||
while (crs.next()) {
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
setVariable(e, baseVariable + meta.getColumnLabel(i).toLowerCase(Locale.ENGLISH)
|
||||
+ Variable.SEPARATOR + rowNumber, crs.getObject(i));
|
||||
}
|
||||
rowNumber++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@Nullable Event e, boolean debug) {
|
||||
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];
|
||||
if (expr instanceof Variable) {
|
||||
Variable<?> varExpr = (Variable<?>) expr;
|
||||
var = SkriptUtil.getVariableName(varExpr);
|
||||
isLocal = varExpr.isLocal();
|
||||
isList = varExpr.isList();
|
||||
} else if (expr != null) {
|
||||
Skript.error(expr + " is not a variable");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
42
src/main/java/com/btk5h/skriptdb/skript/ExprDBError.java
Normal file
42
src/main/java/com/btk5h/skriptdb/skript/ExprDBError.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.btk5h.skriptdb.skript;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.util.Kleenean;
|
||||
|
||||
public class ExprDBError extends SimpleExpression<String> {
|
||||
static {
|
||||
Skript.registerExpression(ExprDBError.class, String.class,
|
||||
ExpressionType.SIMPLE, "[the] [last] (sql|db|data(base|[ ]source)) error");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] get(Event e) {
|
||||
return new String[]{EffExecuteStatement.lastError};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends String> getReturnType() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event e, boolean debug) {
|
||||
return "last database error";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
61
src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java
Normal file
61
src/main/java/com/btk5h/skriptdb/skript/ExprDataSource.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package com.btk5h.skriptdb.skript;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.util.Kleenean;
|
||||
|
||||
public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
||||
static {
|
||||
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
||||
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string%");
|
||||
}
|
||||
|
||||
private Expression<String> url;
|
||||
|
||||
@Override
|
||||
protected HikariDataSource[] get(Event e) {
|
||||
String jdbcUrl = url.getSingle(e);
|
||||
if (jdbcUrl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!jdbcUrl.startsWith("jdbc:")) {
|
||||
jdbcUrl = "jdbc:" + jdbcUrl;
|
||||
}
|
||||
|
||||
HikariDataSource ds = new HikariDataSource();
|
||||
ds.setJdbcUrl(jdbcUrl);
|
||||
|
||||
return new HikariDataSource[] {ds};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HikariDataSource> getReturnType() {
|
||||
return HikariDataSource.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event e, boolean debug) {
|
||||
return "datasource " + url.toString(e, debug);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
|
||||
SkriptParser.ParseResult parseResult) {
|
||||
url = (Expression<String>) exprs[0];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
47
src/main/java/com/btk5h/skriptdb/skript/ExprUnsafe.java
Normal file
47
src/main/java/com/btk5h/skriptdb/skript/ExprUnsafe.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.btk5h.skriptdb.skript;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.util.Kleenean;
|
||||
|
||||
public class ExprUnsafe extends SimpleExpression<String> {
|
||||
static {
|
||||
Skript.registerExpression(ExprUnsafe.class, String.class, ExpressionType.COMBINED,
|
||||
"unsafe %string%");
|
||||
}
|
||||
|
||||
private Expression<String> str;
|
||||
|
||||
@Override
|
||||
protected String[] get(Event e) {
|
||||
return str.getArray(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends String> getReturnType() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Event e, boolean debug) {
|
||||
return "unsafe " + str.toString(e, debug);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
|
||||
SkriptParser.ParseResult parseResult) {
|
||||
str = (Expression<String>) exprs[0];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
72
src/main/java/com/btk5h/skriptdb/skript/Types.java
Normal file
72
src/main/java/com/btk5h/skriptdb/skript/Types.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.btk5h.skriptdb.skript;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import java.io.NotSerializableException;
|
||||
import java.io.StreamCorruptedException;
|
||||
|
||||
import ch.njol.skript.classes.ClassInfo;
|
||||
import ch.njol.skript.classes.Parser;
|
||||
import ch.njol.skript.classes.Serializer;
|
||||
import ch.njol.skript.lang.ParseContext;
|
||||
import ch.njol.skript.registrations.Classes;
|
||||
import ch.njol.yggdrasil.Fields;
|
||||
|
||||
public class Types {
|
||||
static {
|
||||
Classes.registerClass(new ClassInfo<>(HikariDataSource.class, "datasource")
|
||||
.user("datasources?")
|
||||
.parser(new Parser<HikariDataSource>() {
|
||||
@Override
|
||||
public HikariDataSource parse(String s, ParseContext context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(HikariDataSource o, int flags) {
|
||||
return o.getJdbcUrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toVariableNameString(HikariDataSource o) {
|
||||
return o.getJdbcUrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVariableNamePattern() {
|
||||
return "jdbc:.+";
|
||||
}
|
||||
})
|
||||
.serializer(new Serializer<HikariDataSource>() {
|
||||
@Override
|
||||
public Fields serialize(HikariDataSource o) throws NotSerializableException {
|
||||
Fields fields = new Fields();
|
||||
fields.putObject("jdbcurl", o.getJdbcUrl());
|
||||
return fields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(HikariDataSource o, Fields f) throws StreamCorruptedException,
|
||||
NotSerializableException {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HikariDataSource deserialize(Fields fields) throws StreamCorruptedException,
|
||||
NotSerializableException {
|
||||
HikariDataSource ds = new HikariDataSource();
|
||||
ds.setJdbcUrl((String) fields.getObject("jdbcurl"));
|
||||
return ds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mustSyncDeserialization() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBeInstantiated(Class<? extends HikariDataSource> c) {
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user