forked from Limework/skript-db
reformat code (thanks TPGamesNL)
This commit is contained in:
@@ -1,13 +1,5 @@
|
||||
package com.btk5h.skriptdb.skript;
|
||||
|
||||
import com.btk5h.skriptdb.SkriptDB;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import ch.njol.skript.Skript;
|
||||
import ch.njol.skript.lang.Expression;
|
||||
import ch.njol.skript.lang.ExpressionType;
|
||||
@@ -15,11 +7,17 @@ import ch.njol.skript.lang.SkriptParser;
|
||||
import ch.njol.skript.lang.util.SimpleExpression;
|
||||
import ch.njol.skript.util.Timespan;
|
||||
import ch.njol.util.Kleenean;
|
||||
import com.btk5h.skriptdb.SkriptDB;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Stores the connection information for a data source. This should be saved to a variable in a
|
||||
* `script load` event or manually through an effect command.
|
||||
*
|
||||
* <p>
|
||||
* The url format for your database may vary! The example provided uses a MySQL database.
|
||||
*
|
||||
* @name Data Source
|
||||
@@ -30,74 +28,74 @@ import ch.njol.util.Kleenean;
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
||||
static {
|
||||
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
||||
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
||||
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%]");
|
||||
}
|
||||
private static final Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
||||
|
||||
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
||||
|
||||
private Expression<String> url;
|
||||
private Expression<Timespan> maxLifetime;
|
||||
|
||||
@Override
|
||||
protected HikariDataSource[] get(Event e) {
|
||||
String jdbcUrl = url.getSingle(e);
|
||||
if (jdbcUrl == null) {
|
||||
return null;
|
||||
static {
|
||||
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
||||
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
||||
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%]");
|
||||
}
|
||||
|
||||
if (!jdbcUrl.startsWith("jdbc:")) {
|
||||
jdbcUrl = "jdbc:" + jdbcUrl;
|
||||
private Expression<String> url;
|
||||
private Expression<Timespan> maxLifetime;
|
||||
|
||||
@Override
|
||||
protected HikariDataSource[] get(Event e) {
|
||||
String jdbcUrl = url.getSingle(e);
|
||||
if (jdbcUrl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!jdbcUrl.startsWith("jdbc:")) {
|
||||
jdbcUrl = "jdbc:" + jdbcUrl;
|
||||
}
|
||||
|
||||
if (connectionCache.containsKey(jdbcUrl)) {
|
||||
return new HikariDataSource[]{connectionCache.get(jdbcUrl)};
|
||||
}
|
||||
|
||||
HikariDataSource ds = new HikariDataSource();
|
||||
|
||||
//allow specifying of own sql driver class name
|
||||
if (!SkriptDB.getInstance().getConfig().getString("sql-driver-class-name", "default").equals("default")) {
|
||||
ds.setDriverClassName(SkriptDB.getInstance().getConfig().getString("sql-driver-class-name"));
|
||||
}
|
||||
ds.setJdbcUrl(jdbcUrl);
|
||||
|
||||
if (maxLifetime != null) {
|
||||
Timespan l = maxLifetime.getSingle(e);
|
||||
|
||||
if (l != null) {
|
||||
ds.setMaxLifetime(l.getMilliSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
connectionCache.put(jdbcUrl, ds);
|
||||
|
||||
return new HikariDataSource[]{ds};
|
||||
}
|
||||
|
||||
if (connectionCache.containsKey(jdbcUrl)) {
|
||||
return new HikariDataSource[]{connectionCache.get(jdbcUrl)};
|
||||
@Override
|
||||
public boolean isSingle() {
|
||||
return true;
|
||||
}
|
||||
|
||||
HikariDataSource ds = new HikariDataSource();
|
||||
|
||||
//allow specifying of own sql driver class name
|
||||
if (!SkriptDB.getInstance().getConfig().getString("sql-driver-class-name", "default").equals("default")) {
|
||||
ds.setDriverClassName(SkriptDB.getInstance().getConfig().getString("sql-driver-class-name"));
|
||||
}
|
||||
ds.setJdbcUrl(jdbcUrl);
|
||||
|
||||
if (maxLifetime != null) {
|
||||
Timespan l = maxLifetime.getSingle(e);
|
||||
|
||||
if (l != null) {
|
||||
ds.setMaxLifetime(l.getMilliSeconds());
|
||||
}
|
||||
@Override
|
||||
public Class<? extends HikariDataSource> getReturnType() {
|
||||
return HikariDataSource.class;
|
||||
}
|
||||
|
||||
connectionCache.put(jdbcUrl, ds);
|
||||
@Override
|
||||
public String toString(Event e, boolean debug) {
|
||||
return "datasource " + url.toString(e, debug);
|
||||
}
|
||||
|
||||
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];
|
||||
maxLifetime = (Expression<Timespan>) exprs[1];
|
||||
return true;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
|
||||
SkriptParser.ParseResult parseResult) {
|
||||
url = (Expression<String>) exprs[0];
|
||||
maxLifetime = (Expression<Timespan>) exprs[1];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user