Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3e7cf7a6dd | ||
|
185a324e8c | ||
|
e75941d75a | ||
|
efa37217c2 | ||
|
0d8d834929 | ||
|
b2a53078a6 | ||
|
83f71e147b | ||
|
e610fc1357 | ||
|
aa1a1d14c7 | ||
|
3a1ec76a0b | ||
|
2eb691cd69 | ||
|
6dbd2effb3 | ||
|
88b76f1b5b | ||
|
b692047878 | ||
|
3e57cae866 | ||
|
1e95b818eb | ||
|
688ea9d46b | ||
|
dd6d574479 | ||
|
1f6091eb95 | ||
|
74d4918f44 |
23
build.gradle
23
build.gradle
@ -1,12 +1,12 @@
|
|||||||
group 'com.btk5h.skript-db'
|
group 'com.btk5h.skript-db'
|
||||||
version '0.1.1'
|
version '1.1.0'
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
|
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.2'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,14 +24,14 @@ repositories {
|
|||||||
url 'https://oss.sonatype.org/content/groups/public/'
|
url 'https://oss.sonatype.org/content/groups/public/'
|
||||||
}
|
}
|
||||||
maven {
|
maven {
|
||||||
url 'http://maven.njol.ch/repo/'
|
url 'http://jitpack.io/'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
shadow 'org.spigotmc:spigot-api:1.11-R0.1-SNAPSHOT'
|
shadow 'org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT'
|
||||||
shadow 'ch.njol:skript:2.2-SNAPSHOT'
|
shadow 'com.github.SkriptLang:Skript:2.3.6'
|
||||||
compile 'com.zaxxer:HikariCP:2.6.2'
|
compile 'com.zaxxer:HikariCP:3.4.5'
|
||||||
}
|
}
|
||||||
|
|
||||||
task buildReadme(type: Javadoc) {
|
task buildReadme(type: Javadoc) {
|
||||||
@ -43,3 +43,14 @@ task buildReadme(type: Javadoc) {
|
|||||||
options.addStringOption('file', 'README.md')
|
options.addStringOption('file', 'README.md')
|
||||||
options.addStringOption('markdown', '-quiet')
|
options.addStringOption('markdown', '-quiet')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task fatJar(type: Jar) {
|
||||||
|
manifest {
|
||||||
|
attributes 'Implementation-Title': 'Gradle Jar File Example',
|
||||||
|
'Implementation-Version': version,
|
||||||
|
'Main-Class': 'com.mkyong.DateUtils'
|
||||||
|
}
|
||||||
|
baseName = project.name + '-all'
|
||||||
|
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
|
||||||
|
with jar
|
||||||
|
}
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
|||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
|
||||||
|
3
src/main/java/META-INF/MANIFEST.MF
Normal file
3
src/main/java/META-INF/MANIFEST.MF
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Main-Class: com.btk5h.skriptdb.SkriptDB
|
||||||
|
|
@ -25,9 +25,11 @@
|
|||||||
|
|
||||||
package com.btk5h.skriptdb;
|
package com.btk5h.skriptdb;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import javax.sql.rowset.RowSetFactory;
|
import javax.sql.rowset.RowSetFactory;
|
||||||
@ -49,6 +51,7 @@ public final class SkriptDB extends JavaPlugin {
|
|||||||
private static SkriptAddon addonInstance;
|
private static SkriptAddon addonInstance;
|
||||||
|
|
||||||
private static RowSetFactory rowSetFactory;
|
private static RowSetFactory rowSetFactory;
|
||||||
|
protected FileConfiguration config;
|
||||||
|
|
||||||
public SkriptDB() {
|
public SkriptDB() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
@ -58,6 +61,34 @@ public final class SkriptDB extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupConfig() throws IOException {
|
||||||
|
//don't check if it exists, because mkdir already does that
|
||||||
|
File file = new File("plugins/skript-db/config.yml");
|
||||||
|
if (file.getParentFile().mkdirs()) {
|
||||||
|
BufferedWriter out = null;
|
||||||
|
try {
|
||||||
|
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("plugins/skript-db/config.yml", false), StandardCharsets.UTF_8));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (out == null) return;
|
||||||
|
|
||||||
|
out.write("# How many connections can be awaited for simultaneously, may be useful to increase if mysql database is hosted on a separate machine to account for ping.\n");
|
||||||
|
out.write("# If it is hosted within the same machine, set it to the count of cores your processor has or the count of threads your processor can process at once.\n");
|
||||||
|
out.write("thread-pool-size: " + (Runtime.getRuntime().availableProcessors() + 1) + "\n");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
try {
|
try {
|
||||||
@ -69,6 +100,11 @@ public final class SkriptDB extends JavaPlugin {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
setupConfig();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SkriptAddon getAddonInstance() {
|
public static SkriptAddon getAddonInstance() {
|
||||||
|
@ -1,25 +1,17 @@
|
|||||||
package com.btk5h.skriptdb;
|
package com.btk5h.skriptdb;
|
||||||
|
|
||||||
import org.bukkit.event.Event;
|
import ch.njol.skript.Skript;
|
||||||
|
import ch.njol.skript.lang.Expression;
|
||||||
|
import ch.njol.skript.lang.VariableString;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import ch.njol.skript.Skript;
|
|
||||||
import ch.njol.skript.effects.Delay;
|
|
||||||
import ch.njol.skript.lang.Expression;
|
|
||||||
import ch.njol.skript.lang.Variable;
|
|
||||||
import ch.njol.skript.lang.VariableString;
|
|
||||||
|
|
||||||
public class SkriptUtil {
|
public class SkriptUtil {
|
||||||
|
|
||||||
private static final Field STRING;
|
private static final Field STRING;
|
||||||
private static final Field SIMPLE;
|
|
||||||
private static final Field DELAYED;
|
|
||||||
private static final Field EXPR;
|
private static final Field EXPR;
|
||||||
private static final Field VARIABLE_NAME;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Field _FIELD = null;
|
Field _FIELD = null;
|
||||||
@ -32,25 +24,6 @@ public class SkriptUtil {
|
|||||||
}
|
}
|
||||||
STRING = _FIELD;
|
STRING = _FIELD;
|
||||||
|
|
||||||
try {
|
|
||||||
_FIELD = VariableString.class.getDeclaredField("simple");
|
|
||||||
_FIELD.setAccessible(true);
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
Skript.error("Skript's 'simple' field could not be resolved.");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
SIMPLE = _FIELD;
|
|
||||||
|
|
||||||
try {
|
|
||||||
_FIELD = Delay.class.getDeclaredField("delayed");
|
|
||||||
_FIELD.setAccessible(true);
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Skript.warning("Skript's 'delayed' method could not be resolved. Some Skript warnings may " +
|
|
||||||
"not be available.");
|
|
||||||
}
|
|
||||||
DELAYED = _FIELD;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses())
|
Optional<Class<?>> expressionInfo = Arrays.stream(VariableString.class.getDeclaredClasses())
|
||||||
.filter(cls -> cls.getSimpleName().equals("ExpressionInfo"))
|
.filter(cls -> cls.getSimpleName().equals("ExpressionInfo"))
|
||||||
@ -67,33 +40,6 @@ public class SkriptUtil {
|
|||||||
Skript.error("Skript's 'expr' field could not be resolved.");
|
Skript.error("Skript's 'expr' field could not be resolved.");
|
||||||
}
|
}
|
||||||
EXPR = _FIELD;
|
EXPR = _FIELD;
|
||||||
|
|
||||||
try {
|
|
||||||
_FIELD = Variable.class.getDeclaredField("name");
|
|
||||||
_FIELD.setAccessible(true);
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Skript.error("Skript's 'variable name' method could not be resolved.");
|
|
||||||
}
|
|
||||||
VARIABLE_NAME = _FIELD;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static void delay(Event e) {
|
|
||||||
if (DELAYED != null) {
|
|
||||||
try {
|
|
||||||
((Set<Event>) DELAYED.get(null)).add(e);
|
|
||||||
} catch (IllegalAccessException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getSimpleString(VariableString vs) {
|
|
||||||
try {
|
|
||||||
return (String) SIMPLE.get(vs);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object[] getTemplateString(VariableString vs) {
|
public static Object[] getTemplateString(VariableString vs) {
|
||||||
@ -112,12 +58,4 @@ public class SkriptUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VariableString getVariableName(Variable<?> var) {
|
|
||||||
try {
|
|
||||||
return (VariableString) VARIABLE_NAME.get(var);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,28 @@
|
|||||||
package com.btk5h.skriptdb.skript;
|
package com.btk5h.skriptdb.skript;
|
||||||
|
|
||||||
|
import ch.njol.skript.Skript;
|
||||||
|
import ch.njol.skript.effects.Delay;
|
||||||
|
import ch.njol.skript.lang.*;
|
||||||
|
import ch.njol.skript.variables.Variables;
|
||||||
|
import ch.njol.util.Kleenean;
|
||||||
|
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.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;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import javax.sql.rowset.CachedRowSet;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSetMetaData;
|
import java.sql.ResultSetMetaData;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import javax.sql.rowset.CachedRowSet;
|
|
||||||
|
|
||||||
import ch.njol.skript.Skript;
|
|
||||||
import ch.njol.skript.effects.Delay;
|
|
||||||
import ch.njol.skript.lang.Expression;
|
|
||||||
import ch.njol.skript.lang.SkriptParser;
|
|
||||||
import ch.njol.skript.lang.TriggerItem;
|
|
||||||
import ch.njol.skript.lang.Variable;
|
|
||||||
import ch.njol.skript.lang.VariableString;
|
|
||||||
import ch.njol.skript.variables.Variables;
|
|
||||||
import ch.njol.util.Kleenean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes a statement on a database and optionally stores the result in a variable. Expressions
|
* Executes a statement on a database and optionally stores the result in a variable. Expressions
|
||||||
* embedded in the query will be escaped to avoid SQL injection.
|
* embedded in the query will be escaped to avoid SQL injection.
|
||||||
@ -42,7 +34,7 @@ import ch.njol.util.Kleenean;
|
|||||||
* variable in the form `{test::<column name>::<row number>}`
|
* variable in the form `{test::<column name>::<row number>}`
|
||||||
*
|
*
|
||||||
* @name Execute Statement
|
* @name Execute Statement
|
||||||
* @pattern execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
|
* @pattern [synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
|
||||||
* [the] [var[iable]] %-objects%]
|
* [the] [var[iable]] %-objects%]
|
||||||
* @example execute "select * from table" in {sql} and store the result in {output::*}
|
* @example execute "select * from table" in {sql} and store the result in {output::*}
|
||||||
* @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
|
* @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
|
||||||
@ -52,99 +44,92 @@ public class EffExecuteStatement extends Delay {
|
|||||||
static {
|
static {
|
||||||
Skript.registerEffect(EffExecuteStatement.class,
|
Skript.registerEffect(EffExecuteStatement.class,
|
||||||
"execute %string% (in|on) %datasource% " +
|
"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%]");
|
"[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static String lastError;
|
static String lastError;
|
||||||
|
|
||||||
private static final ExecutorService threadPool =
|
private static final ExecutorService threadPool =
|
||||||
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
Executors.newFixedThreadPool(SkriptDB.getInstance().getConfig().getInt("thread-pool-size"));
|
||||||
|
|
||||||
private Expression<String> query;
|
private Expression<String> query;
|
||||||
private Expression<HikariDataSource> dataSource;
|
private Expression<HikariDataSource> dataSource;
|
||||||
private VariableString var;
|
private VariableString var;
|
||||||
private boolean isLocal;
|
private boolean isLocal;
|
||||||
private boolean isList;
|
private boolean isList;
|
||||||
|
private Map<String, Object> doLater = new HashMap<>();
|
||||||
|
private boolean isSync;
|
||||||
|
|
||||||
|
private void continueScriptExecution(Event e, String res) {
|
||||||
|
lastError = res;
|
||||||
|
|
||||||
|
if (getNext() != null) {
|
||||||
|
doLater.forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
doLater.clear();
|
||||||
|
TriggerItem.walk(getNext(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void execute(Event e) {
|
protected void execute(Event e) {
|
||||||
CompletableFuture<String> sql =
|
DataSource ds = dataSource.getSingle(e);
|
||||||
CompletableFuture.supplyAsync(() -> executeStatement(e), threadPool);
|
Pair<String, List<Object>> query = parseQuery(e);
|
||||||
|
String baseVariable = var != null ? var.toString(e).toLowerCase(Locale.ENGLISH) : null;
|
||||||
|
|
||||||
sql.whenComplete((res, err) -> {
|
if (ds == null)
|
||||||
if (err != null) {
|
return;
|
||||||
err.printStackTrace();
|
if (isSync) {
|
||||||
}
|
String result = executeStatement(ds, baseVariable, query);
|
||||||
|
continueScriptExecution(e, result);
|
||||||
|
} else {
|
||||||
|
Object locals = Variables.removeLocals(e);
|
||||||
|
CompletableFuture<String> sql =
|
||||||
|
CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
sql.whenComplete((res, err) -> {
|
||||||
lastError = res;
|
if (err != null) {
|
||||||
|
err.printStackTrace();
|
||||||
if (getNext() != null) {
|
|
||||||
TriggerItem.walk(getNext(), e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bukkit.getScheduler().runTask(SkriptDB.getInstance(), () -> {
|
||||||
|
lastError = res;
|
||||||
|
|
||||||
|
if (getNext() != null) {
|
||||||
|
if (locals != null)
|
||||||
|
Variables.setLocalVariables(e, locals);
|
||||||
|
doLater.forEach((name, value) -> setVariable(e, name, value));
|
||||||
|
doLater.clear();
|
||||||
|
TriggerItem.walk(getNext(), e);
|
||||||
|
Variables.removeLocals(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TriggerItem walk(Event e) {
|
protected TriggerItem walk(Event e) {
|
||||||
debug(e, true);
|
debug(e, true);
|
||||||
SkriptUtil.delay(e);
|
if (!isSync) {
|
||||||
|
Delay.addDelayedEvent(e);
|
||||||
|
}
|
||||||
execute(e);
|
execute(e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String executeStatement(Event e) {
|
private Pair<String, List<Object>> parseQuery(Event e) {
|
||||||
HikariDataSource ds = dataSource.getSingle(e);
|
|
||||||
|
|
||||||
if (ds == null) {
|
|
||||||
return "Data source is not set";
|
|
||||||
}
|
|
||||||
|
|
||||||
try (Connection conn = ds.getConnection();
|
|
||||||
PreparedStatement stmt = createStatement(e, conn)) {
|
|
||||||
|
|
||||||
boolean hasResultSet = stmt.execute();
|
|
||||||
|
|
||||||
if (var != null) {
|
|
||||||
String baseVariable = var.toString(e)
|
|
||||||
.toLowerCase(Locale.ENGLISH);
|
|
||||||
if (isList) {
|
|
||||||
baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasResultSet) {
|
|
||||||
CachedRowSet crs = SkriptDB.getRowSetFactory().createCachedRowSet();
|
|
||||||
crs.populate(stmt.getResultSet());
|
|
||||||
|
|
||||||
if (isList) {
|
|
||||||
populateVariable(e, crs, baseVariable);
|
|
||||||
} else {
|
|
||||||
crs.last();
|
|
||||||
setVariable(e, baseVariable, crs.getRow());
|
|
||||||
}
|
|
||||||
} else if (!isList) {
|
|
||||||
setVariable(e, baseVariable, stmt.getUpdateCount());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException ex) {
|
|
||||||
return ex.getMessage();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private PreparedStatement createStatement(Event e, Connection conn) throws SQLException {
|
|
||||||
if (!(query instanceof VariableString)) {
|
if (!(query instanceof VariableString)) {
|
||||||
return conn.prepareStatement(query.getSingle(e));
|
return new Pair<>(query.getSingle(e), null);
|
||||||
}
|
}
|
||||||
|
VariableString q = (VariableString) query;
|
||||||
if (((VariableString) query).isSimple()) {
|
if (q.isSimple()) {
|
||||||
return conn.prepareStatement(SkriptUtil.getSimpleString(((VariableString) query)));
|
return new Pair<>(q.toString(e), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
List<Object> parameters = new ArrayList<>();
|
List<Object> parameters = new ArrayList<>();
|
||||||
Object[] objects = SkriptUtil.getTemplateString(((VariableString) query));
|
Object[] objects = SkriptUtil.getTemplateString(q);
|
||||||
|
|
||||||
for (int i = 0; i < objects.length; i++) {
|
for (int i = 0; i < objects.length; i++) {
|
||||||
Object o = objects[i];
|
Object o = objects[i];
|
||||||
if (o instanceof String) {
|
if (o instanceof String) {
|
||||||
@ -183,11 +168,52 @@ public class EffExecuteStatement extends Delay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return new Pair<>(sb.toString(), parameters);
|
||||||
|
}
|
||||||
|
|
||||||
PreparedStatement stmt = conn.prepareStatement(sb.toString());
|
private String executeStatement(DataSource ds, String baseVariable, Pair<String, List<Object>> query) {
|
||||||
|
if (ds == null) {
|
||||||
|
return "Data source is not set";
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < parameters.size(); i++) {
|
try (Connection conn = ds.getConnection();
|
||||||
stmt.setObject(i + 1, parameters.get(i));
|
PreparedStatement stmt = createStatement(conn, query)) {
|
||||||
|
|
||||||
|
boolean hasResultSet = stmt.execute();
|
||||||
|
|
||||||
|
if (baseVariable != null) {
|
||||||
|
if (isList) {
|
||||||
|
baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasResultSet) {
|
||||||
|
CachedRowSet crs = SkriptDB.getRowSetFactory().createCachedRowSet();
|
||||||
|
crs.populate(stmt.getResultSet());
|
||||||
|
|
||||||
|
if (isList) {
|
||||||
|
populateVariable(crs, baseVariable);
|
||||||
|
} else {
|
||||||
|
crs.last();
|
||||||
|
doLater.put(baseVariable, crs.getRow());
|
||||||
|
}
|
||||||
|
} else if (!isList) {
|
||||||
|
doLater.put(baseVariable, stmt.getUpdateCount());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
return ex.getMessage();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PreparedStatement createStatement(Connection conn, Pair<String, List<Object>> query) throws SQLException {
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(query.getFirst());
|
||||||
|
List<Object> parameters = query.getSecond();
|
||||||
|
|
||||||
|
if (parameters != null) {
|
||||||
|
for (int i = 0; i < parameters.size(); i++) {
|
||||||
|
stmt.setObject(i + 1, parameters.get(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stmt;
|
return stmt;
|
||||||
@ -211,20 +237,20 @@ public class EffExecuteStatement extends Delay {
|
|||||||
Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal);
|
Variables.setVariable(name.toLowerCase(Locale.ENGLISH), obj, e, isLocal);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populateVariable(Event e, CachedRowSet crs, String baseVariable)
|
private void populateVariable(CachedRowSet crs, String baseVariable)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
ResultSetMetaData meta = crs.getMetaData();
|
ResultSetMetaData meta = crs.getMetaData();
|
||||||
int columnCount = meta.getColumnCount();
|
int columnCount = meta.getColumnCount();
|
||||||
|
|
||||||
for (int i = 1; i <= columnCount; i++) {
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
String label = meta.getColumnLabel(i);
|
String label = meta.getColumnLabel(i);
|
||||||
setVariable(e, baseVariable + label, label);
|
doLater.put(baseVariable + label, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rowNumber = 1;
|
int rowNumber = 1;
|
||||||
while (crs.next()) {
|
while (crs.next()) {
|
||||||
for (int i = 1; i <= columnCount; i++) {
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
setVariable(e, baseVariable + meta.getColumnLabel(i).toLowerCase(Locale.ENGLISH)
|
doLater.put(baseVariable + meta.getColumnLabel(i).toLowerCase(Locale.ENGLISH)
|
||||||
+ Variable.SEPARATOR + rowNumber, crs.getObject(i));
|
+ Variable.SEPARATOR + rowNumber, crs.getObject(i));
|
||||||
}
|
}
|
||||||
rowNumber++;
|
rowNumber++;
|
||||||
@ -232,7 +258,7 @@ public class EffExecuteStatement extends Delay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(@Nullable Event e, boolean debug) {
|
public String toString(Event e, boolean debug) {
|
||||||
return "execute " + query.toString(e, debug) + " in " + dataSource.toString(e, debug);
|
return "execute " + query.toString(e, debug) + " in " + dataSource.toString(e, debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,9 +277,10 @@ public class EffExecuteStatement extends Delay {
|
|||||||
}
|
}
|
||||||
dataSource = (Expression<HikariDataSource>) exprs[1];
|
dataSource = (Expression<HikariDataSource>) exprs[1];
|
||||||
Expression<?> expr = exprs[2];
|
Expression<?> expr = exprs[2];
|
||||||
|
isSync = matchedPattern == 1;
|
||||||
if (expr instanceof Variable) {
|
if (expr instanceof Variable) {
|
||||||
Variable<?> varExpr = (Variable<?>) expr;
|
Variable<?> varExpr = (Variable<?>) expr;
|
||||||
var = SkriptUtil.getVariableName(varExpr);
|
var = varExpr.getName();
|
||||||
isLocal = varExpr.isLocal();
|
isLocal = varExpr.isLocal();
|
||||||
isList = varExpr.isList();
|
isList = varExpr.isList();
|
||||||
} else if (expr != null) {
|
} else if (expr != null) {
|
||||||
|
@ -32,7 +32,7 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
|
|||||||
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 static Map<String, HikariDataSource> connectionCache = new HashMap<>();
|
||||||
|
@ -64,7 +64,7 @@ public class Types {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canBeInstantiated(Class<? extends HikariDataSource> c) {
|
protected boolean canBeInstantiated() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
name: skript-db
|
name: skript-db
|
||||||
version: 0.1.1
|
version: 1.1.0
|
||||||
main: com.btk5h.skriptdb.SkriptDB
|
main: com.btk5h.skriptdb.SkriptDB
|
||||||
depend: [Skript]
|
depend: [Skript]
|
||||||
|
authors: [btk5h, FranKusmiruk, Govindas]
|
||||||
|
Loading…
Reference in New Issue
Block a user