From 4cc0d0ba4e4b60460b0e8a5975f3ac7cd73ce645 Mon Sep 17 00:00:00 2001 From: Govindas <16716020+Govindass@users.noreply.github.com> Date: Wed, 12 May 2021 13:17:42 +0300 Subject: [PATCH] finish experimental (disabled) on complete of sql query event --- README.md | 5 +- .../events/SQLQueryCompleteEvent.java | 3 +- .../skriptdb/skript/EffExecuteStatement.java | 15 +++-- .../btk5h/skriptdb/skript/ExprSQLQuery.java | 60 +++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/btk5h/skriptdb/skript/ExprSQLQuery.java diff --git a/README.md b/README.md index 5db5eaf..b43222d 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,10 @@ Executes a statement on a database and optionally stores the result in a variabl If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list variable in the form `{test::::}` - If `synchronously` is specified, the SQL query will be done on the current thread. + If `quickly` is specified, the SQL query will be done without jumping back to main thread, which speeds it up by 50ms, however that makes code after it to also be on separate thread, you can jump back to main thread by adding `wait a tick` #### Syntax ``` -[synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%] +[quickly] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%] ``` #### Examples @@ -77,4 +77,3 @@ execute unsafe {fully dynamic query} in {sql} ``` --- - diff --git a/src/main/java/com/btk5h/skriptdb/events/SQLQueryCompleteEvent.java b/src/main/java/com/btk5h/skriptdb/events/SQLQueryCompleteEvent.java index a43d0b4..75e613a 100644 --- a/src/main/java/com/btk5h/skriptdb/events/SQLQueryCompleteEvent.java +++ b/src/main/java/com/btk5h/skriptdb/events/SQLQueryCompleteEvent.java @@ -6,7 +6,6 @@ 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); @@ -26,7 +25,7 @@ public class SQLQueryCompleteEvent extends Event { return HANDLERS; } - public String getArgument() { + public String getQuery() { return argument; } diff --git a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java index ad105c5..a26e237 100644 --- a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java +++ b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java @@ -45,7 +45,7 @@ public class EffExecuteStatement extends Effect { static { Skript.registerEffect(EffExecuteStatement.class, "execute %string% (in|on) %datasource% " + - "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]", "synchronously execute %string% (in|on) %datasource% " + + "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]", "quickly execute %string% (in|on) %datasource% " + "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]"); } @@ -78,11 +78,13 @@ public class EffExecuteStatement extends Effect { DataSource ds = dataSource.getSingle(e); Pair> query = parseQuery(e); String baseVariable = var != null ? var.toString(e).toLowerCase(Locale.ENGLISH) : null; - //if data source isn't set if (ds == null) return; boolean sync = false; + + //if current thread is not main thread, then make this query to not have delays + if (!Bukkit.isPrimaryThread()) { sync = true; } @@ -113,9 +115,12 @@ public class EffExecuteStatement extends Effect { //also set variables in the sql query complete event - SQLQueryCompleteEvent event = new SQLQueryCompleteEvent("something"); - ((Map) res).forEach((name, value) -> setVariable(event, name, value)); - SkriptDB.getPlugin(SkriptDB.class).getServer().getPluginManager().callEvent(event); + //TEMPORARILY DISABLED, AS THIS WOULD WORSEN PERFORMANCE OF THE QUERIES AND NOT BE USED BY MOST PEOPLE. + //I may add config option to enable this later? + + //SQLQueryCompleteEvent event = new SQLQueryCompleteEvent(this.query.getSingle(e)); + //((Map) res).forEach((name, value) -> setVariable(event, name, value)); + //SkriptDB.getPlugin(SkriptDB.class).getServer().getPluginManager().callEvent(event); } if (isSync || finalSync) { diff --git a/src/main/java/com/btk5h/skriptdb/skript/ExprSQLQuery.java b/src/main/java/com/btk5h/skriptdb/skript/ExprSQLQuery.java new file mode 100644 index 0000000..e27e3ec --- /dev/null +++ b/src/main/java/com/btk5h/skriptdb/skript/ExprSQLQuery.java @@ -0,0 +1,60 @@ +package com.btk5h.skriptdb.skript; + +import ch.njol.skript.ScriptLoader; +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.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; +import com.btk5h.skriptdb.events.SQLQueryCompleteEvent; +import org.bukkit.event.Event; + +/** + * Stores the error from the last executed statement, if there was one. + * + * @name Last Data Source Error + * @pattern [the] [last] (sql|db|data(base|[ ]source)) error + * @return text + * @since 0.1.0 + */ +public class ExprSQLQuery extends SimpleExpression { + static { + Skript.registerExpression(ExprSQLQuery.class, String.class, + ExpressionType.SIMPLE, "sql query"); + } + + + @Override + protected String[] get(Event e) { + if (e instanceof SQLQueryCompleteEvent){ + return new String[]{((SQLQueryCompleteEvent) e).getQuery()}; + } + return null; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return String.class; + } + + @Override + public String toString(Event e, boolean debug) { + return "sql query"; + } + + @Override + public boolean init(final Expression[] expressions, final int matchedPattern, final Kleenean isDelayed, final SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(SQLQueryCompleteEvent.class)) { + Skript.error("Cannot use 'sql query' outside of a complete of sql query event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } +}