2017-11-19 03:49:49 +00:00
|
|
|
package com.btk5h.skriptdb.skript;
|
|
|
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
|
|
|
|
import org.bukkit.event.Event;
|
|
|
|
|
2017-12-20 01:01:55 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2017-11-19 03:49:49 +00:00
|
|
|
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;
|
2018-02-19 21:24:34 +00:00
|
|
|
import ch.njol.skript.util.Timespan;
|
2017-11-19 03:49:49 +00:00
|
|
|
import ch.njol.util.Kleenean;
|
|
|
|
|
2017-11-22 00:58:47 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The url format for your database may vary! The example provided uses a MySQL database.
|
|
|
|
*
|
|
|
|
* @name Data Source
|
|
|
|
* @index -1
|
2018-02-19 21:24:34 +00:00
|
|
|
* @pattern [the] data(base|[ ]source) [(of|at)] %string% [with [a] [max[imum]] [connection] life[ ]time of %timespan%]"
|
2017-11-22 00:58:47 +00:00
|
|
|
* @return datasource
|
|
|
|
* @example set {sql} to the database "mysql://localhost:3306/mydatabase?user=admin&password=12345&useSSL=false"
|
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
2017-11-19 03:49:49 +00:00
|
|
|
public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
|
|
|
static {
|
|
|
|
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
2018-02-19 21:24:34 +00:00
|
|
|
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
|
|
|
"[with [a] [max[imum]] [connection] life[ ]time of %timespan%]");
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 01:01:55 +00:00
|
|
|
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
|
|
|
|
2017-11-19 03:49:49 +00:00
|
|
|
private Expression<String> url;
|
2018-02-19 21:24:34 +00:00
|
|
|
private Expression<Timespan> maxLifetime;
|
2017-11-19 03:49:49 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected HikariDataSource[] get(Event e) {
|
|
|
|
String jdbcUrl = url.getSingle(e);
|
|
|
|
if (jdbcUrl == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jdbcUrl.startsWith("jdbc:")) {
|
|
|
|
jdbcUrl = "jdbc:" + jdbcUrl;
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:01:55 +00:00
|
|
|
if (connectionCache.containsKey(jdbcUrl)) {
|
|
|
|
return new HikariDataSource[]{connectionCache.get(jdbcUrl)};
|
|
|
|
}
|
|
|
|
|
2017-11-19 03:49:49 +00:00
|
|
|
HikariDataSource ds = new HikariDataSource();
|
|
|
|
ds.setJdbcUrl(jdbcUrl);
|
|
|
|
|
2018-02-19 21:24:34 +00:00
|
|
|
if (maxLifetime != null) {
|
|
|
|
Timespan l = maxLifetime.getSingle(e);
|
|
|
|
|
|
|
|
if (l != null) {
|
|
|
|
ds.setMaxLifetime(l.getMilliSeconds());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:01:55 +00:00
|
|
|
connectionCache.put(jdbcUrl, ds);
|
|
|
|
|
2017-11-22 00:58:47 +00:00
|
|
|
return new HikariDataSource[]{ds};
|
2017-11-19 03:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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];
|
2018-02-19 21:24:34 +00:00
|
|
|
maxLifetime = (Expression<Timespan>) exprs[1];
|
2017-11-19 03:49:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|