SQL query parsing

This commit is contained in:
Erik Bročko 2018-10-01 23:54:03 +02:00
parent 29b7a3cee7
commit aa8605c3e8
2 changed files with 88 additions and 19 deletions

View File

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

View File

@ -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<String, String> 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<String, String> 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();
}
}
}
}