25 Commits

Author SHA1 Message Date
Govindass
efa37217c2 Add synchronous support on current thread 2020-05-18 13:20:01 +03:00
Govindass
0d8d834929 Merge FranKusmiruk's Pull Request 2020-05-02 20:25:36 +03:00
Govindass
b2a53078a6 add gradle fatJar task 2020-05-02 20:02:25 +03:00
Govindas
83f71e147b Update HikariCP, should fix a lot of issues 2020-05-02 19:48:35 +03:00
Govindass
e610fc1357 Remove unused class 2020-05-02 19:30:48 +03:00
Govindass
aa1a1d14c7 add synchronous 2019-09-30 15:28:32 +03:00
Govindass
3a1ec76a0b Delete EffSyncExecuteStatement.java 2019-09-30 15:27:26 +03:00
Govindass
2eb691cd69 Delete EffExecuteStatement.java 2019-09-30 15:27:16 +03:00
Govindass
6dbd2effb3 Delete plugin.yml 2019-09-30 15:26:48 +03:00
Govindass
88b76f1b5b Add files via upload 2019-09-30 15:25:37 +03:00
Bryan Terce
b692047878 0.2.1 Hotfix 2019-06-26 01:56:29 -07:00
Bryan Terce
3e57cae866 Fix sync execute logic (fixes #16) 2019-06-26 01:55:08 -07:00
Bryan Terce
1e95b818eb 0.2.0 Update 2019-06-22 12:12:27 -07:00
Bryan Terce
688ea9d46b Manually fix README 2019-06-22 12:11:29 -07:00
Bryan Terce
dd6d574479 Update README 2019-06-22 12:09:59 -07:00
Bryan Terce
1f6091eb95 Add synchronous execution flag 2019-06-22 12:09:17 -07:00
Bryan Terce
74d4918f44 Fix lifespan syntax 2019-06-22 12:05:22 -07:00
Bryan Terce
39bb3b0b72 Add some warnings for misusing SQL injection protection (#4) 2018-05-09 15:35:47 -07:00
Bryan Terce
cef0c4c816 Add connection lifespan option 2018-02-19 13:24:34 -08:00
Bryan Terce
3016a3c078 0.1.1 update 2018-01-23 18:02:06 -08:00
Bryan Terce
3c485cf542 Cache connections with the same jdbc url (fixes #2) 2017-12-19 17:01:55 -08:00
Bryan Terce
b3c5c36d28 Update SkriptDoclet 2017-11-29 22:23:21 -08:00
Bryan Terce
4e629cdf11 Remove unnecessary imports 2017-11-21 17:00:40 -08:00
Bryan Terce
3edaa7d107 Add experimental support for SkriptDoclet 2017-11-21 16:58:47 -08:00
Bryan Terce
e1bbd37a35 Change database name to something more friendly 2017-11-18 20:13:13 -08:00
13 changed files with 306 additions and 193 deletions

View File

@@ -1,72 +1,72 @@
# skript-db # skript-db
> Awesome direct database access for Skript > Sensible SQL support for Skript.
---
## Syntax
### Expression `Data Source` => `datasource` ### Expression `Data Source` => `datasource`
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.
This 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. 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 below uses a MySQL database.
#### Syntax #### Syntax
`[the] data(base|[ ]source) [(of|at)] %string%`
#### Example
``` ```
set {sql} to the database "mysql://localhost:3306/sys?user=admin&password=12345&useSSL=false" [the] data(base|[ ]source) [(of|at)] %string%
```
#### Examples
```
set {sql} to the database "mysql://localhost:3306/mydatabase?user=admin&password=12345&useSSL=false"
``` ```
--- ---
### Effect `Execute Statement` ### Effect `Execute Statement`
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.
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. If a single variable, such as `{test}`, is passed, the variable will be set to the number of
affected rows.
If a single variable, such as `{test}`, is passed, the variable will be set to the number of affected rows.
If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list variable in the form `{test::<column name>::<row number>}`
If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list
variable in the form `{test::<column name>::<row number>}`
#### Syntax #### Syntax
```
execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]
```
`execute %text% (in|on) %datasource% #### Examples
[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %variable%]`
#### Example
``` ```
execute "select * from table" in {sql} and store the result in {output::*} execute "select * from table" in {sql} and store the result in {output::*}
```
```
execute "select * from %{table variable}%" in {sql} and store the result in {output::*} execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
``` ```
--- ---
### Expression `Unsafe Expression` => `text` ### Expression `Last Data Source Error` => `text`
Stores the error from the last executed statement, if there was one.
Opts out of automatic SQL injection protection for a specific expression in a statement.
#### Syntax #### Syntax
`unsafe %text%`
#### Example
``` ```
execute "select %unsafe {columns variable}% from %{table variable}%" in {sql} and store the result in {output::*} [the] [last] (sql|db|data(base|[ ]source)) error
```
---
### Expression `Unsafe Expression` => `text`
Opts out of automatic SQL injection protection for a specific expression in a statement.
#### Syntax
```
unsafe %text%
```
#### Examples
```
execute "select %unsafe {columns variable}% from %{table variable}%" in {sql}
```
```
execute unsafe {fully dynamic query} in {sql} execute unsafe {fully dynamic query} in {sql}
``` ```
--- ---
### Expression `Last Data Source Error` => `text`
Stores the error from the last executed statement, if there was one.
#### Syntax
`[the] [last] (sql|db|data(base|[ ]source)) error`
---

View File

@@ -1,12 +1,12 @@
group 'com.btk5h.skript-db' group 'com.btk5h.skript-db'
version '0.1.0' version '0.1.1'
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,12 +24,33 @@ 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.3'
} }
task buildReadme(type: Javadoc) {
source = sourceSets.main.allJava
classpath = sourceSets.main.compileClasspath
destinationDir = projectDir
options.docletpath = [file('tools/skriptdoclet.jar')]
options.doclet = 'com.btk5h.skriptdoclet.SkriptDoclet'
options.addStringOption('file', 'README.md')
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
}

View File

@@ -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

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.btk5h.skriptdb.SkriptDB

View File

@@ -36,6 +36,13 @@ import javax.sql.rowset.RowSetProvider;
import ch.njol.skript.Skript; import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon; import ch.njol.skript.SkriptAddon;
/**
* # skript-db
*
* > Sensible SQL support for Skript.
*
* @index -1
*/
public final class SkriptDB extends JavaPlugin { public final class SkriptDB extends JavaPlugin {
private static SkriptDB instance; private static SkriptDB instance;

View File

@@ -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;
}
} }

View File

@@ -1,40 +1,50 @@
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; /**
* Executes a statement on a database and optionally stores the result in a variable. Expressions
import ch.njol.skript.Skript; * embedded in the query will be escaped to avoid SQL injection.
import ch.njol.skript.effects.Delay; * <p>
import ch.njol.skript.lang.Expression; * If a single variable, such as `{test}`, is passed, the variable will be set to the number of
import ch.njol.skript.lang.SkriptParser; * affected rows.
import ch.njol.skript.lang.TriggerItem; * <p>
import ch.njol.skript.lang.Variable; * If a list variable, such as `{test::*}`, is passed, the query result will be mapped to the list
import ch.njol.skript.lang.VariableString; * variable in the form `{test::<column name>::<row number>}`
import ch.njol.skript.variables.Variables; *
import ch.njol.util.Kleenean; * @name Execute Statement
* @pattern [synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
* [the] [var[iable]] %-objects%]
* @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::*}
* @since 0.1.0
*/
public class EffExecuteStatement extends Delay { 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%]");
} }
@@ -48,50 +58,130 @@ public class EffExecuteStatement extends Delay {
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 (!(query instanceof VariableString)) {
return new Pair<>(query.getSingle(e), null);
}
VariableString q = (VariableString) query;
if (q.isSimple()) {
return new Pair<>(q.toString(e), null);
}
StringBuilder sb = new StringBuilder();
List<Object> parameters = new ArrayList<>();
Object[] objects = SkriptUtil.getTemplateString(q);
for (int i = 0; i < objects.length; i++) {
Object o = objects[i];
if (o instanceof String) {
sb.append(o);
} else {
Expression<?> expr = SkriptUtil.getExpressionFromInfo(o);
String before = getString(objects, i - 1);
String after = getString(objects, i + 1);
boolean standaloneString = false;
if (before != null && after != null) {
if (before.endsWith("'") && after.endsWith("'")) {
standaloneString = true;
}
}
Object expressionValue = expr.getSingle(e);
if (expr instanceof ExprUnsafe) {
sb.append(expressionValue);
if (standaloneString && expressionValue instanceof String) {
String rawExpression = ((ExprUnsafe) expr).getRawExpression();
Skript.warning(
String.format("Unsafe may have been used unnecessarily. Try replacing 'unsafe %1$s' with %1$s",
rawExpression));
}
} else {
parameters.add(expressionValue);
sb.append('?');
if (standaloneString) {
Skript.warning("Do not surround expressions with quotes!");
}
}
}
}
return new Pair<>(sb.toString(), parameters);
}
private String executeStatement(DataSource ds, String baseVariable, Pair<String, List<Object>> query) {
if (ds == null) { if (ds == null) {
return "Data source is not set"; return "Data source is not set";
} }
try (Connection conn = ds.getConnection(); try (Connection conn = ds.getConnection();
PreparedStatement stmt = createStatement(e, conn)) { PreparedStatement stmt = createStatement(conn, query)) {
boolean hasResultSet = stmt.execute(); boolean hasResultSet = stmt.execute();
if (var != null) { if (baseVariable != null) {
String baseVariable = var.toString(e)
.toLowerCase(Locale.ENGLISH);
if (isList) { if (isList) {
baseVariable = baseVariable.substring(0, baseVariable.length() - 1); baseVariable = baseVariable.substring(0, baseVariable.length() - 1);
} }
@@ -101,13 +191,13 @@ public class EffExecuteStatement extends Delay {
crs.populate(stmt.getResultSet()); crs.populate(stmt.getResultSet());
if (isList) { if (isList) {
populateVariable(e, crs, baseVariable); populateVariable(crs, baseVariable);
} else { } else {
crs.last(); crs.last();
setVariable(e, baseVariable, crs.getRow()); doLater.put(baseVariable, crs.getRow());
} }
} else if (!isList) { } else if (!isList) {
setVariable(e, baseVariable, stmt.getUpdateCount()); doLater.put(baseVariable, stmt.getUpdateCount());
} }
} }
} catch (SQLException ex) { } catch (SQLException ex) {
@@ -116,67 +206,59 @@ public class EffExecuteStatement extends Delay {
return null; return null;
} }
private PreparedStatement createStatement(Event e, Connection conn) throws SQLException { private PreparedStatement createStatement(Connection conn, Pair<String, List<Object>> query) throws SQLException {
if (!(query instanceof VariableString)) { PreparedStatement stmt = conn.prepareStatement(query.getFirst());
return conn.prepareStatement(query.getSingle(e)); List<Object> parameters = query.getSecond();
}
if (((VariableString) query).isSimple()) { if (parameters != null) {
return conn.prepareStatement(SkriptUtil.getSimpleString(((VariableString) query))); for (int i = 0; i < parameters.size(); i++) {
} stmt.setObject(i + 1, parameters.get(i));
StringBuilder sb = new StringBuilder();
List<Object> parameters = new ArrayList<>();
Object[] objects = SkriptUtil.getTemplateString(((VariableString) query));
for (Object o : objects) {
if (o instanceof String) {
sb.append(o);
} else {
Expression<?> expr = SkriptUtil.getExpressionFromInfo(o);
if (expr instanceof ExprUnsafe) {
sb.append(expr.getSingle(e));
} else {
parameters.add(expr.getSingle(e));
sb.append('?');
}
} }
} }
PreparedStatement stmt = conn.prepareStatement(sb.toString()); return stmt;
}
for (int i = 0; i < parameters.size(); i++) { private String getString(Object[] objects, int index) {
stmt.setObject(i + 1, parameters.get(i)); if (index < 0 || index >= objects.length) {
return null;
} }
return stmt; Object object = objects[index];
if (object instanceof String) {
return (String) object;
}
return null;
} }
private void setVariable(Event e, String name, Object obj) { private void setVariable(Event e, String name, Object obj) {
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++;
} }
} }
@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);
} }
@@ -195,9 +277,11 @@ public class EffExecuteStatement extends Delay {
} }
dataSource = (Expression<HikariDataSource>) exprs[1]; dataSource = (Expression<HikariDataSource>) exprs[1];
Expression<?> expr = exprs[2]; Expression<?> expr = exprs[2];
System.out.println(matchedPattern);
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) {

View File

@@ -9,6 +9,14 @@ 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;
/**
* 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 ExprDBError extends SimpleExpression<String> { public class ExprDBError extends SimpleExpression<String> {
static { static {
Skript.registerExpression(ExprDBError.class, String.class, Skript.registerExpression(ExprDBError.class, String.class,

View File

@@ -4,20 +4,41 @@ import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.event.Event; 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;
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.skript.util.Timespan;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
/**
* 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.
*
* The url format for your database may vary! The example provided uses a MySQL database.
*
* @name Data Source
* @index -1
* @pattern [the] data(base|[ ]source) [(of|at)] %string% [with [a] [max[imum]] [connection] life[ ]time of %timespan%]"
* @return datasource
* @example set {sql} to the database "mysql://localhost:3306/mydatabase?user=admin&password=12345&useSSL=false"
* @since 0.1.0
*/
public class ExprDataSource extends SimpleExpression<HikariDataSource> { 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%]");
} }
private static Map<String, HikariDataSource> connectionCache = new HashMap<>();
private Expression<String> url; private Expression<String> url;
private Expression<Timespan> maxLifetime;
@Override @Override
protected HikariDataSource[] get(Event e) { protected HikariDataSource[] get(Event e) {
@@ -30,10 +51,24 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
jdbcUrl = "jdbc:" + jdbcUrl; jdbcUrl = "jdbc:" + jdbcUrl;
} }
if (connectionCache.containsKey(jdbcUrl)) {
return new HikariDataSource[]{connectionCache.get(jdbcUrl)};
}
HikariDataSource ds = new HikariDataSource(); HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(jdbcUrl); ds.setJdbcUrl(jdbcUrl);
return new HikariDataSource[] {ds}; if (maxLifetime != null) {
Timespan l = maxLifetime.getSingle(e);
if (l != null) {
ds.setMaxLifetime(l.getMilliSeconds());
}
}
connectionCache.put(jdbcUrl, ds);
return new HikariDataSource[]{ds};
} }
@Override @Override
@@ -56,6 +91,7 @@ public class ExprDataSource extends SimpleExpression<HikariDataSource> {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
SkriptParser.ParseResult parseResult) { SkriptParser.ParseResult parseResult) {
url = (Expression<String>) exprs[0]; url = (Expression<String>) exprs[0];
maxLifetime = (Expression<Timespan>) exprs[1];
return true; return true;
} }
} }

View File

@@ -9,17 +9,32 @@ 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;
/**
* Opts out of automatic SQL injection protection for a specific expression in a statement.
*
* @name Unsafe Expression
* @pattern unsafe %text%
* @return text
* @example execute "select %unsafe {columns variable}% from %{table variable}%" in {sql}
* @example execute unsafe {fully dynamic query} in {sql}
* @since 0.1.0
*/
public class ExprUnsafe extends SimpleExpression<String> { public class ExprUnsafe extends SimpleExpression<String> {
static { static {
Skript.registerExpression(ExprUnsafe.class, String.class, ExpressionType.COMBINED, Skript.registerExpression(ExprUnsafe.class, String.class, ExpressionType.COMBINED,
"unsafe %string%"); "unsafe %string%");
} }
private Expression<String> str; private Expression<String> stringExpression;
private String rawExpression;
public String getRawExpression() {
return rawExpression;
}
@Override @Override
protected String[] get(Event e) { protected String[] get(Event e) {
return str.getArray(e); return stringExpression.getArray(e);
} }
@Override @Override
@@ -34,14 +49,15 @@ public class ExprUnsafe extends SimpleExpression<String> {
@Override @Override
public String toString(Event e, boolean debug) { public String toString(Event e, boolean debug) {
return "unsafe " + str.toString(e, debug); return "unsafe " + stringExpression.toString(e, debug);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
SkriptParser.ParseResult parseResult) { SkriptParser.ParseResult parseResult) {
str = (Expression<String>) exprs[0]; stringExpression = (Expression<String>) exprs[0];
rawExpression = parseResult.expr.substring("unsafe".length()).trim();
return true; return true;
} }
} }

View File

@@ -64,7 +64,7 @@ public class Types {
} }
@Override @Override
public boolean canBeInstantiated(Class<? extends HikariDataSource> c) { protected boolean canBeInstantiated() {
return false; return false;
} }
})); }));

View File

@@ -1,4 +1,4 @@
name: skript-db name: skript-db
version: 0.1.0 version: 0.1.1
main: com.btk5h.skriptdb.SkriptDB main: com.btk5h.skriptdb.SkriptDB
depend: [Skript] depend: [Skript]

BIN
tools/skriptdoclet.jar Normal file

Binary file not shown.