diff --git a/resources/config.yml b/resources/config.yml index 7accd47..46214a9 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -1,18 +1,16 @@ -#modules: -# - cz.marwland.mc.features.Borders dataSource: - method: 'mariadb' - address: 'localhost' - database: 'factions' - username: 'root' + method: mariadb + address: localhost + database: factions + username: root password: '' - table-prefix: 'f_' - + table-prefix: f_ pool-settings: maximum-pool-size: 10 minimum-idle: 10 - maximum-lifetime: 1800000 # 30 minutes - connection-timeout: 5000 # 5 seconds + maximum-lifetime: 1800000 + connection-timeout: 5000 properties: useUnicode: true characterEncoding: utf8 +modules: diff --git a/src/cz/marwland/mc/core/storage/SQLStorage.java b/src/cz/marwland/mc/core/storage/SQLStorage.java index 278a4c1..1b2eafb 100644 --- a/src/cz/marwland/mc/core/storage/SQLStorage.java +++ b/src/cz/marwland/mc/core/storage/SQLStorage.java @@ -1,35 +1,43 @@ package cz.marwland.mc.core.storage; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; import java.util.function.Function; public class SQLStorage { - + private final ConnectionFactory connectionFactory; private final StorageCredentials configuration; private final Function statementProcessor; - + public SQLStorage(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; this.configuration = connectionFactory.getConfiguration(); this.statementProcessor = connectionFactory.getStatementProcessor().compose(s -> s.replace("{prefix}", this.configuration.getTablePrefix())); } - + public ConnectionFactory getConnectionFactory() { return this.connectionFactory; } - + public StorageCredentials getConfiguration() { return this.configuration; } - + public Function getStatementProcessor() { - return this.statementProcessor; - } - + return this.statementProcessor; + } + public void init() { this.connectionFactory.init(); } - + public void shutdown() { try { this.connectionFactory.shutdown(); @@ -37,4 +45,67 @@ public class SQLStorage { ex.printStackTrace(); } } + + public void executeRaw(InputStream is) throws SQLException, IOException { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { + try (Connection connection = this.connectionFactory.getConnection()) { + try (Statement s = connection.createStatement()) { + StringBuilder sb = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + if (line.startsWith("--") || line.startsWith("#")) + continue; + + sb.append(line); + + // check for end of declaration + if (line.endsWith(";")) { + sb.deleteCharAt(sb.length() - 1); + + String result = this.statementProcessor.apply(sb.toString().trim()); + if (!result.isEmpty()) + s.addBatch(result); + + // reset + sb = new StringBuilder(); + } + } + s.executeBatch(); + } finally { + connection.close(); + } + } + } finally { + is.close(); + } + } + + public void executeRaw(String sql) throws SQLException, IOException { + try (Connection connection = this.connectionFactory.getConnection()) { + try (Statement s = connection.createStatement()) { + StringBuilder sb = new StringBuilder(); + for (String line : sql.split("\n")) { + if (line.startsWith("--") || line.startsWith("#")) + continue; + + sb.append(line); + + // check for end of declaration + if (line.endsWith(";")) { + sb.deleteCharAt(sb.length() - 1); + + String result = this.statementProcessor.apply(sb.toString().trim()); + if (!result.isEmpty()) + s.addBatch(result); + + // reset + sb = new StringBuilder(); + } + } + s.executeBatch(); + } finally { + connection.close(); + } + } + } }