From 59d209540babbcf527f26fe97263eed02bde02c8 Mon Sep 17 00:00:00 2001 From: Govindass Date: Fri, 31 Jul 2020 11:50:44 +0300 Subject: [PATCH] Fix local variables in sql queries within the same tick & first startup errors --- build.gradle | 2 +- .../java/com/btk5h/skriptdb/SkriptDB.java | 19 ++-- .../skriptdb/skript/EffExecuteStatement.java | 87 ++++++++++--------- src/main/resources/plugin.yml | 2 +- 4 files changed, 59 insertions(+), 51 deletions(-) diff --git a/build.gradle b/build.gradle index d24e15e..1583b56 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.btk5h.skript-db' -version '1.1.0' +version '1.2.0' buildscript { repositories { diff --git a/src/main/java/com/btk5h/skriptdb/SkriptDB.java b/src/main/java/com/btk5h/skriptdb/SkriptDB.java index a94ecde..dfcffc8 100644 --- a/src/main/java/com/btk5h/skriptdb/SkriptDB.java +++ b/src/main/java/com/btk5h/skriptdb/SkriptDB.java @@ -91,19 +91,20 @@ public final class SkriptDB extends JavaPlugin { @Override public void onEnable() { - try { - rowSetFactory = RowSetProvider.newFactory(); - - getAddonInstance().loadClasses("com.btk5h.skriptdb.skript"); - } catch (SQLException e) { - throw new RuntimeException(e); - } catch (IOException e) { - e.printStackTrace(); - } try { setupConfig(); } catch (IOException e) { e.printStackTrace(); + } finally { + try { + rowSetFactory = RowSetProvider.newFactory(); + + getAddonInstance().loadClasses("com.btk5h.skriptdb.skript"); + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java index a38b7f6..9da09a7 100644 --- a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java +++ b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java @@ -40,7 +40,7 @@ import java.util.concurrent.Executors; * @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*} * @since 0.1.0 */ -public class EffExecuteStatement extends Delay { +public class EffExecuteStatement extends Effect { static { Skript.registerEffect(EffExecuteStatement.class, "execute %string% (in|on) %datasource% " + @@ -58,17 +58,19 @@ public class EffExecuteStatement extends Delay { private VariableString var; private boolean isLocal; private boolean isList; - private Map doLater = new HashMap<>(); private boolean isSync; - private void continueScriptExecution(Event e, String res) { - lastError = res; + private void continueScriptExecution(Event e, Object populatedVariables) { + lastError = null; + if (populatedVariables instanceof String) { + lastError = (String) populatedVariables; + } else { - if (getNext() != null) { - doLater.forEach((name, value) -> setVariable(e, name, value)); - doLater.clear(); - TriggerItem.walk(getNext(), e); + if (getNext() != null) { + ((Map) populatedVariables).forEach((name, value) -> setVariable(e, name, value)); + } } + TriggerItem.walk(getNext(), e); } @Override protected void execute(Event e) { @@ -79,11 +81,11 @@ public class EffExecuteStatement extends Delay { if (ds == null) return; if (isSync) { - String result = executeStatement(ds, baseVariable, query); - continueScriptExecution(e, result); + Object populatedVariables = executeStatement(ds, baseVariable, query); + continueScriptExecution(e, populatedVariables); } else { Object locals = Variables.removeLocals(e); - CompletableFuture sql = + CompletableFuture sql = CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool); sql.whenComplete((res, err) -> { @@ -92,13 +94,19 @@ public class EffExecuteStatement extends Delay { } Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> { - lastError = res; + lastError = null; + if (res instanceof String) { + lastError = (String) res; + } if (getNext() != null) { if (locals != null) Variables.setLocalVariables(e, locals); - doLater.forEach((name, value) -> setVariable(e, name, value)); - doLater.clear(); + + if (!(res instanceof String)) { + ((Map) res).forEach((name, value) -> setVariable(e, name, value)); + } + //doLater.clear(); TriggerItem.walk(getNext(), e); Variables.removeLocals(e); } @@ -171,11 +179,11 @@ public class EffExecuteStatement extends Delay { return new Pair<>(sb.toString(), parameters); } - private String executeStatement(DataSource ds, String baseVariable, Pair> query) { + private Object executeStatement(DataSource ds, String baseVariable, Pair> query) { if (ds == null) { return "Data source is not set"; } - + Map variableList = new HashMap<>(); try (Connection conn = ds.getConnection(); PreparedStatement stmt = createStatement(conn, query)) { @@ -191,19 +199,38 @@ public class EffExecuteStatement extends Delay { crs.populate(stmt.getResultSet()); if (isList) { - populateVariable(crs, baseVariable); + ResultSetMetaData meta = crs.getMetaData(); + int columnCount = meta.getColumnCount(); + + for (int i = 1; i <= columnCount; i++) { + String label = meta.getColumnLabel(i); + variableList.put(baseVariable + label, label); + } + + int rowNumber = 1; + try { + while (crs.next()) { + for (int i = 1; i <= columnCount; i++) { + variableList.put(baseVariable + meta.getColumnLabel(i).toLowerCase(Locale.ENGLISH) + + Variable.SEPARATOR + rowNumber, crs.getObject(i)); + } + rowNumber++; + } + } catch (SQLException ex) { + return ex.getMessage(); + } } else { crs.last(); - doLater.put(baseVariable, crs.getRow()); + variableList.put(baseVariable, crs.getRow()); } } else if (!isList) { - doLater.put(baseVariable, stmt.getUpdateCount()); + variableList.put(baseVariable, stmt.getUpdateCount()); } } } catch (SQLException ex) { return ex.getMessage(); } - return null; + return variableList; } private PreparedStatement createStatement(Connection conn, Pair> query) throws SQLException { @@ -237,26 +264,6 @@ public class EffExecuteStatement extends Delay { Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal); } - private void populateVariable(CachedRowSet crs, String baseVariable) - throws SQLException { - ResultSetMetaData meta = crs.getMetaData(); - int columnCount = meta.getColumnCount(); - - for (int i = 1; i <= columnCount; i++) { - String label = meta.getColumnLabel(i); - doLater.put(baseVariable + label, label); - } - - int rowNumber = 1; - while (crs.next()) { - for (int i = 1; i <= columnCount; i++) { - doLater.put(baseVariable + meta.getColumnLabel(i).toLowerCase(Locale.ENGLISH) - + Variable.SEPARATOR + rowNumber, crs.getObject(i)); - } - rowNumber++; - } - } - @Override public String toString(Event e, boolean debug) { return "execute " + query.toString(e, debug) + " in " + dataSource.toString(e, debug); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2288390..f4684cf 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: skript-db -version: 1.1.0 +version: 1.2.0 main: com.btk5h.skriptdb.SkriptDB depend: [Skript] authors: [btk5h, FranKusmiruk, Govindas]