forked from Limework/skript-db
Initial commit
This commit is contained in:
84
src/main/java/com/btk5h/skriptdb/SkriptDB.java
Normal file
84
src/main/java/com/btk5h/skriptdb/SkriptDB.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 Bryan Terce
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.btk5h.skriptdb;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.rowset.RowSetFactory;
|
||||
import javax.sql.rowset.RowSetProvider;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.SkriptAddon;
|
||||
|
||||
public final class SkriptDB extends JavaPlugin {
|
||||
|
||||
private static SkriptDB instance;
|
||||
private static SkriptAddon addonInstance;
|
||||
|
||||
private static RowSetFactory rowSetFactory;
|
||||
|
||||
public SkriptDB() {
|
||||
if (instance == null) {
|
||||
instance = this;
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
try {
|
||||
rowSetFactory = RowSetProvider.newFactory();
|
||||
|
||||
getAddonInstance().loadClasses("com.btk5h.skriptdb.skript");
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static SkriptAddon getAddonInstance() {
|
||||
if (addonInstance == null) {
|
||||
addonInstance = Skript.registerAddon(getInstance());
|
||||
}
|
||||
return addonInstance;
|
||||
}
|
||||
|
||||
public static SkriptDB getInstance() {
|
||||
if (instance == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static RowSetFactory getRowSetFactory() {
|
||||
return rowSetFactory;
|
||||
}
|
||||
}
|
||||
123
src/main/java/com/btk5h/skriptdb/SkriptUtil.java
Normal file
123
src/main/java/com/btk5h/skriptdb/SkriptUtil.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package com.btk5h.skriptdb;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.effects.Delay;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.Variable;
|
||||
import ch.njol.skript.lang.VariableString;
|
||||
|
||||
public class SkriptUtil {
|
||||
|
||||
private static final Field STRING;
|
||||
private static final Field SIMPLE;
|
||||
private static final Field DELAYED;
|
||||
private static final Field EXPR;
|
||||
private static final Field VARIABLE_NAME;
|
||||
|
||||
static {
|
||||
Field _FIELD = null;
|
||||
try {
|
||||
_FIELD = VariableString.class.getDeclaredField("string");
|
||||
_FIELD.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
Skript.error("Skript's 'string' field could not be resolved.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
STRING = _FIELD;
|
||||
|
||||
try {
|
||||
_FIELD = VariableString.class.getDeclaredField("simple");
|
||||
_FIELD.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
Skript.error("Skript's 'simple' field could not be resolved.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
SIMPLE = _FIELD;
|
||||
|
||||
try {
|
||||
_FIELD = Delay.class.getDeclaredField("delayed");
|
||||
_FIELD.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
Skript.warning("Skript's 'delayed' method could not be resolved. Some Skript warnings may " +
|
||||
"not be available.");
|
||||
}
|
||||
DELAYED = _FIELD;
|
||||
|
||||
try {
|
||||
Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses())
|
||||
.filter(cls -> cls.getSimpleName().equals("ExpressionInfo"))
|
||||
.findFirst();
|
||||
if (expressionInfo.isPresent()) {
|
||||
Class<?> expressionInfoClass = expressionInfo.get();
|
||||
_FIELD = expressionInfoClass.getDeclaredField("expr");
|
||||
_FIELD.setAccessible(true);
|
||||
} else {
|
||||
Skript.error("Skript's 'ExpressionInfo' class could not be resolved.");
|
||||
}
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
Skript.error("Skript's 'expr' field could not be resolved.");
|
||||
}
|
||||
EXPR = _FIELD;
|
||||
|
||||
try {
|
||||
_FIELD = Variable.class.getDeclaredField("name");
|
||||
_FIELD.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
Skript.error("Skript's 'variable name' method could not be resolved.");
|
||||
}
|
||||
VARIABLE_NAME = _FIELD;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void delay(Event e) {
|
||||
if (DELAYED != null) {
|
||||
try {
|
||||
((Set<Event>) DELAYED.get(null)).add(e);
|
||||
} catch (IllegalAccessException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getSimpleString(VariableString vs) {
|
||||
try {
|
||||
return (String) SIMPLE.get(vs);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object[] getTemplateString(VariableString vs) {
|
||||
try {
|
||||
return (Object[]) STRING.get(vs);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Expression<?> getExpressionFromInfo(Object o) {
|
||||
try {
|
||||
return (Expression<?>) EXPR.get(o);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static VariableString getVariableName(Variable<?> var) {
|
||||
try {
|
||||
return (VariableString) VARIABLE_NAME.get(var);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
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