forked from Limework/skript-db
reformat code (thanks TPGamesNL)
This commit is contained in:
parent
9b694206d3
commit
2dcd4edf58
2
pom.xml
2
pom.xml
@ -93,7 +93,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.SkriptLang</groupId>
|
<groupId>com.github.SkriptLang</groupId>
|
||||||
<artifactId>Skript</artifactId>
|
<artifactId>Skript</artifactId>
|
||||||
<version>2.5.3</version>
|
<version>2.6-alpha1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
|
<!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
|
||||||
|
@ -5,13 +5,18 @@ import org.bukkit.event.HandlerList;
|
|||||||
|
|
||||||
public class SQLQueryCompleteEvent extends Event {
|
public class SQLQueryCompleteEvent extends Event {
|
||||||
private final static HandlerList HANDLERS = new HandlerList();
|
private final static HandlerList HANDLERS = new HandlerList();
|
||||||
private String argument;
|
private final String argument;
|
||||||
|
|
||||||
public SQLQueryCompleteEvent(String argument) {
|
public SQLQueryCompleteEvent(String argument) {
|
||||||
super(true);
|
super(true);
|
||||||
this.argument = argument;
|
this.argument = argument;
|
||||||
// this.variables = variables;
|
// this.variables = variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HandlerList getHandlerList() {
|
||||||
|
return HANDLERS;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getEventName() {
|
public String getEventName() {
|
||||||
return super.getEventName();
|
return super.getEventName();
|
||||||
@ -21,9 +26,6 @@ public class SQLQueryCompleteEvent extends Event {
|
|||||||
public HandlerList getHandlers() {
|
public HandlerList getHandlers() {
|
||||||
return HANDLERS;
|
return HANDLERS;
|
||||||
}
|
}
|
||||||
public static HandlerList getHandlerList() {
|
|
||||||
return HANDLERS;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getQuery() {
|
public String getQuery() {
|
||||||
return argument;
|
return argument;
|
||||||
|
@ -8,7 +8,6 @@ import ch.njol.util.Kleenean;
|
|||||||
import ch.njol.util.Pair;
|
import ch.njol.util.Pair;
|
||||||
import com.btk5h.skriptdb.SkriptDB;
|
import com.btk5h.skriptdb.SkriptDB;
|
||||||
import com.btk5h.skriptdb.SkriptUtil;
|
import com.btk5h.skriptdb.SkriptUtil;
|
||||||
import com.btk5h.skriptdb.events.SQLQueryCompleteEvent;
|
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
@ -42,6 +41,10 @@ import java.util.concurrent.Executors;
|
|||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
public class EffExecuteStatement extends Effect {
|
public class EffExecuteStatement extends Effect {
|
||||||
|
private static final ExecutorService threadPool =
|
||||||
|
Executors.newCachedThreadPool();
|
||||||
|
static String lastError;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Skript.registerEffect(EffExecuteStatement.class,
|
Skript.registerEffect(EffExecuteStatement.class,
|
||||||
"execute %string% (in|on) %datasource% " +
|
"execute %string% (in|on) %datasource% " +
|
||||||
@ -49,11 +52,6 @@ public class EffExecuteStatement extends Effect {
|
|||||||
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static String lastError;
|
|
||||||
|
|
||||||
private static final ExecutorService threadPool =
|
|
||||||
Executors.newCachedThreadPool();
|
|
||||||
|
|
||||||
private Expression<String> query;
|
private Expression<String> query;
|
||||||
private Expression<HikariDataSource> dataSource;
|
private Expression<HikariDataSource> dataSource;
|
||||||
private VariableString var;
|
private VariableString var;
|
||||||
@ -73,6 +71,7 @@ public class EffExecuteStatement extends Effect {
|
|||||||
}
|
}
|
||||||
TriggerItem.walk(getNext(), e);
|
TriggerItem.walk(getNext(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void execute(Event e) {
|
protected void execute(Event e) {
|
||||||
DataSource ds = dataSource.getSingle(e);
|
DataSource ds = dataSource.getSingle(e);
|
||||||
@ -81,13 +80,10 @@ public class EffExecuteStatement extends Effect {
|
|||||||
//if data source isn't set
|
//if data source isn't set
|
||||||
if (ds == null) return;
|
if (ds == null) return;
|
||||||
|
|
||||||
boolean sync = false;
|
boolean sync = !Bukkit.isPrimaryThread();
|
||||||
|
|
||||||
//if current thread is not main thread, then make this query to not have delays
|
//if current thread is not main thread, then make this query to not have delays
|
||||||
|
|
||||||
if (!Bukkit.isPrimaryThread()) {
|
|
||||||
sync = true;
|
|
||||||
}
|
|
||||||
Object locals = Variables.removeLocals(e);
|
Object locals = Variables.removeLocals(e);
|
||||||
|
|
||||||
//execute SQL statement
|
//execute SQL statement
|
||||||
@ -97,7 +93,9 @@ public class EffExecuteStatement extends Effect {
|
|||||||
//when SQL statement is completed
|
//when SQL statement is completed
|
||||||
boolean finalSync = sync;
|
boolean finalSync = sync;
|
||||||
sql.whenComplete((res, err) -> {
|
sql.whenComplete((res, err) -> {
|
||||||
if (err != null) { err.printStackTrace(); }
|
if (err != null) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
//handle last error syntax data
|
//handle last error syntax data
|
||||||
lastError = null;
|
lastError = null;
|
||||||
@ -125,13 +123,17 @@ public class EffExecuteStatement extends Effect {
|
|||||||
if (isSync || finalSync) {
|
if (isSync || finalSync) {
|
||||||
|
|
||||||
Variables.setLocalVariables(e, locals);
|
Variables.setLocalVariables(e, locals);
|
||||||
if (!(res instanceof String)) { ((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value)); }
|
if (!(res instanceof String)) {
|
||||||
|
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
}
|
||||||
TriggerItem.walk(getNext(), e);
|
TriggerItem.walk(getNext(), e);
|
||||||
Variables.removeLocals(e);
|
Variables.removeLocals(e);
|
||||||
} else {
|
} else {
|
||||||
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
||||||
Variables.setLocalVariables(e, locals);
|
Variables.setLocalVariables(e, locals);
|
||||||
if (!(res instanceof String)) { ((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value)); }
|
if (!(res instanceof String)) {
|
||||||
|
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
}
|
||||||
TriggerItem.walk(getNext(), e);
|
TriggerItem.walk(getNext(), e);
|
||||||
//the line below is required to prevent memory leaks
|
//the line below is required to prevent memory leaks
|
||||||
//no functionality difference notice with it being removed from my test, but the memory gets filled with leaks
|
//no functionality difference notice with it being removed from my test, but the memory gets filled with leaks
|
||||||
|
@ -12,6 +12,7 @@ public class EvtSQLQueryComplete extends SkriptEvent {
|
|||||||
static {
|
static {
|
||||||
Skript.registerEvent("complete of sql query", EvtSQLQueryComplete.class, SQLQueryCompleteEvent.class, "complete of [(sql|database)] query");
|
Skript.registerEvent("complete of sql query", EvtSQLQueryComplete.class, SQLQueryCompleteEvent.class, "complete of [(sql|database)] query");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean init(final Literal<?>[] literals, final int i, final SkriptParser.ParseResult parseResult) {
|
public boolean init(final Literal<?>[] literals, final int i, final SkriptParser.ParseResult parseResult) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package com.btk5h.skriptdb.skript;
|
package com.btk5h.skriptdb.skript;
|
||||||
|
|
||||||
import org.bukkit.event.Event;
|
|
||||||
|
|
||||||
import ch.njol.skript.Skript;
|
import ch.njol.skript.Skript;
|
||||||
import ch.njol.skript.lang.Expression;
|
import ch.njol.skript.lang.Expression;
|
||||||
import ch.njol.skript.lang.ExpressionType;
|
import ch.njol.skript.lang.ExpressionType;
|
||||||
import ch.njol.skript.lang.SkriptParser;
|
import ch.njol.skript.lang.SkriptParser;
|
||||||
import ch.njol.skript.lang.util.SimpleExpression;
|
import ch.njol.skript.lang.util.SimpleExpression;
|
||||||
import ch.njol.util.Kleenean;
|
import ch.njol.util.Kleenean;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores the error from the last executed statement, if there was one.
|
* Stores the error from the last executed statement, if there was one.
|
||||||
|
@ -1,13 +1,5 @@
|
|||||||
package com.btk5h.skriptdb.skript;
|
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.Skript;
|
||||||
import ch.njol.skript.lang.Expression;
|
import ch.njol.skript.lang.Expression;
|
||||||
import ch.njol.skript.lang.ExpressionType;
|
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.lang.util.SimpleExpression;
|
||||||
import ch.njol.skript.util.Timespan;
|
import ch.njol.skript.util.Timespan;
|
||||||
import ch.njol.util.Kleenean;
|
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
|
* 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.
|
* `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.
|
* The url format for your database may vary! The example provided uses a MySQL database.
|
||||||
*
|
*
|
||||||
* @name Data Source
|
* @name Data Source
|
||||||
@ -30,14 +28,14 @@ import ch.njol.util.Kleenean;
|
|||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
||||||
|
private static final Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
Skript.registerExpression(ExprDataSource.class, HikariDataSource.class,
|
||||||
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
ExpressionType.COMBINED, "[the] data(base|[ ]source) [(of|at)] %string% " +
|
||||||
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%]");
|
"[with [a] [max[imum]] [connection] life[ ]time of %-timespan%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
|
||||||
|
|
||||||
private Expression<String> url;
|
private Expression<String> url;
|
||||||
private Expression<Timespan> maxLifetime;
|
private Expression<Timespan> maxLifetime;
|
||||||
|
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package com.btk5h.skriptdb.skript;
|
package com.btk5h.skriptdb.skript;
|
||||||
|
|
||||||
import org.bukkit.event.Event;
|
|
||||||
|
|
||||||
import ch.njol.skript.Skript;
|
import ch.njol.skript.Skript;
|
||||||
import ch.njol.skript.lang.Expression;
|
import ch.njol.skript.lang.Expression;
|
||||||
import ch.njol.skript.lang.ExpressionType;
|
import ch.njol.skript.lang.ExpressionType;
|
||||||
import ch.njol.skript.lang.SkriptParser;
|
import ch.njol.skript.lang.SkriptParser;
|
||||||
import ch.njol.skript.lang.util.SimpleExpression;
|
import ch.njol.skript.lang.util.SimpleExpression;
|
||||||
import ch.njol.util.Kleenean;
|
import ch.njol.util.Kleenean;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opts out of automatic SQL injection protection for a specific expression in a statement.
|
* Opts out of automatic SQL injection protection for a specific expression in a statement.
|
||||||
|
Loading…
Reference in New Issue
Block a user