Preparations for quicker queries (no 50ms delay) feature

This commit is contained in:
Govindass 2021-03-03 14:27:09 +02:00
parent 852e5e0477
commit fbb35937e6
6 changed files with 95 additions and 22 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.btk5h</groupId>
<artifactId>skript-db</artifactId>
<version>1.2.1</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<repositories>
@ -65,7 +65,7 @@
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.1</version>
<version>4.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -75,7 +75,7 @@ public final class SkriptDB extends JavaPlugin {
if (out == null) return;
out.write("# Only change this if you wish to use a different driver than Java's default, like MariaDB driver.\n");
out.write("# If you use MariaDB, its driver is shaded together with skript-db, so you can just specify:" + "\"org.mariadb.jdbc.Driver\"" + ".\n");
out.write("sql-driver-class-name:" + "\"default\"" + "\n");
out.write("sql-driver-class-name: " + "\"default\"" + "\n");
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -0,0 +1,34 @@
package com.btk5h.skriptdb.events;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class SQLQueryCompleteEvent extends Event {
private final static HandlerList HANDLERS = new HandlerList();
private String argument;
private Object variables;
public SQLQueryCompleteEvent(String argument) {
super(true);
this.argument = argument;
// this.variables = variables;
}
@Override
public String getEventName() {
return super.getEventName();
}
@Override
public HandlerList getHandlers() {
return HANDLERS;
}
public static HandlerList getHandlerList() {
return HANDLERS;
}
public String getArgument() {
return argument;
}
// public String getVariables() {return;}
}

View File

@ -8,6 +8,7 @@ import ch.njol.util.Kleenean;
import ch.njol.util.Pair;
import com.btk5h.skriptdb.SkriptDB;
import com.btk5h.skriptdb.SkriptUtil;
import com.btk5h.skriptdb.events.SQLQueryCompleteEvent;
import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
@ -83,10 +84,7 @@ public class EffExecuteStatement extends Effect {
return;
if (isSync) {
Object populatedVariables = executeStatement(ds, baseVariable, query);
continueScriptExecution(e, populatedVariables);
} else {
Object locals = Variables.removeLocals(e);
//execute SQL statement
@ -95,11 +93,9 @@ public class EffExecuteStatement extends Effect {
//when SQL statement is completed
sql.whenComplete((res, err) -> {
if (err != null) {
err.printStackTrace();
}
if (err != null) { err.printStackTrace(); }
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
// Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
//handle last error syntax data
lastError = null;
@ -110,28 +106,42 @@ public class EffExecuteStatement extends Effect {
if (getNext() != null) {
//if local variables are present
if (locals != null)
//bring back local variables
Variables.setLocalVariables(e, locals);
//populate SQL data into variables
if (!(res instanceof String)) {
((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value));
//also set variables in the sql query complete event
SQLQueryCompleteEvent event = new SQLQueryCompleteEvent("something");
((Map<String, Object>) res).forEach((name, value) -> setVariable(event, name, value));
SkriptDB.getPlugin(SkriptDB.class).getServer().getPluginManager().callEvent(event);
}
if (isSync) {
Variables.setLocalVariables(e, locals);
if (!(res instanceof String)) { ((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value)); }
TriggerItem.walk(getNext(), e);
Variables.removeLocals(e);
} else {
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
Variables.setLocalVariables(e, locals);
if (!(res instanceof String)) { ((Map<String, Object>) res).forEach((name, value) -> setVariable(e, name, value)); }
TriggerItem.walk(getNext(), e);
//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
//so it must be kept
Variables.removeLocals(e);
});
}
TriggerItem.walk(getNext(), e);
//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
//so it must be kept
Variables.removeLocals(e);
}
});
});
}
}
@Override
protected TriggerItem walk(Event e) {
debug(e, true);
//I think no longer needed as of 1.3.0, uncomment if something breaks
if (!isSync) {
Delay.addDelayedEvent(e);
}

View File

@ -0,0 +1,29 @@
package com.btk5h.skriptdb.skript;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import com.btk5h.skriptdb.events.SQLQueryCompleteEvent;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
public class EvtSQLQueryComplete extends SkriptEvent {
static {
Skript.registerEvent("complete of sql query", EvtSQLQueryComplete.class, SQLQueryCompleteEvent.class, "complete of [(sql|database)] query");
}
@Override
public boolean init(final Literal<?>[] literals, final int i, final SkriptParser.ParseResult parseResult) {
return true;
}
@Override
public boolean check(Event event) {
return (event instanceof SQLQueryCompleteEvent);
}
@Override
public String toString(@Nullable Event event, boolean debug) {
return "complete of sql query";
}
}

View File

@ -1,5 +1,5 @@
name: skript-db
version: 1.2.0
version: 1.3.0
main: com.btk5h.skriptdb.SkriptDB
depend: [Skript]
authors: [btk5h, FranKusmiruk, Govindas]