SQL query parsing
This commit is contained in:
parent
29b7a3cee7
commit
aa8605c3e8
@ -1,18 +1,16 @@
|
|||||||
#modules:
|
|
||||||
# - cz.marwland.mc.features.Borders
|
|
||||||
dataSource:
|
dataSource:
|
||||||
method: 'mariadb'
|
method: mariadb
|
||||||
address: 'localhost'
|
address: localhost
|
||||||
database: 'factions'
|
database: factions
|
||||||
username: 'root'
|
username: root
|
||||||
password: ''
|
password: ''
|
||||||
table-prefix: 'f_'
|
table-prefix: f_
|
||||||
|
|
||||||
pool-settings:
|
pool-settings:
|
||||||
maximum-pool-size: 10
|
maximum-pool-size: 10
|
||||||
minimum-idle: 10
|
minimum-idle: 10
|
||||||
maximum-lifetime: 1800000 # 30 minutes
|
maximum-lifetime: 1800000
|
||||||
connection-timeout: 5000 # 5 seconds
|
connection-timeout: 5000
|
||||||
properties:
|
properties:
|
||||||
useUnicode: true
|
useUnicode: true
|
||||||
characterEncoding: utf8
|
characterEncoding: utf8
|
||||||
|
modules:
|
||||||
|
@ -1,35 +1,43 @@
|
|||||||
package cz.marwland.mc.core.storage;
|
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;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class SQLStorage {
|
public class SQLStorage {
|
||||||
|
|
||||||
private final ConnectionFactory connectionFactory;
|
private final ConnectionFactory connectionFactory;
|
||||||
private final StorageCredentials configuration;
|
private final StorageCredentials configuration;
|
||||||
private final Function<String, String> statementProcessor;
|
private final Function<String, String> statementProcessor;
|
||||||
|
|
||||||
public SQLStorage(ConnectionFactory connectionFactory) {
|
public SQLStorage(ConnectionFactory connectionFactory) {
|
||||||
this.connectionFactory = connectionFactory;
|
this.connectionFactory = connectionFactory;
|
||||||
this.configuration = connectionFactory.getConfiguration();
|
this.configuration = connectionFactory.getConfiguration();
|
||||||
this.statementProcessor = connectionFactory.getStatementProcessor().compose(s -> s.replace("{prefix}", this.configuration.getTablePrefix()));
|
this.statementProcessor = connectionFactory.getStatementProcessor().compose(s -> s.replace("{prefix}", this.configuration.getTablePrefix()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionFactory getConnectionFactory() {
|
public ConnectionFactory getConnectionFactory() {
|
||||||
return this.connectionFactory;
|
return this.connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StorageCredentials getConfiguration() {
|
public StorageCredentials getConfiguration() {
|
||||||
return this.configuration;
|
return this.configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Function<String, String> getStatementProcessor() {
|
public Function<String, String> getStatementProcessor() {
|
||||||
return this.statementProcessor;
|
return this.statementProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
this.connectionFactory.init();
|
this.connectionFactory.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
try {
|
try {
|
||||||
this.connectionFactory.shutdown();
|
this.connectionFactory.shutdown();
|
||||||
@ -37,4 +45,67 @@ public class SQLStorage {
|
|||||||
ex.printStackTrace();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user