mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 14:38:53 +00:00
Add SQLite migration
This commit is contained in:
1 parent
70ed11fc65
commit
dcc4fe68ff
5 files changed
+156
-10
No files matched your search
@@ -11,10 +11,12 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -167,6 +169,15 @@ public abstract class Database {
|
||||
*/
|
||||
public abstract boolean initialize();
|
||||
|
||||
/**
|
||||
* Execute a MySQL script file read as an InputStream
|
||||
*
|
||||
* @param inputStream The input stream to read the script from
|
||||
* @return A future returning void when complete
|
||||
*/
|
||||
public abstract CompletableFuture<Void> runScript(@NotNull InputStream inputStream,
|
||||
@NotNull Map<String, String> replacements);
|
||||
|
||||
/**
|
||||
* <b>(Internal use only)</b> - Sets a position to the position table in the database
|
||||
*
|
||||
|
||||
@@ -13,11 +13,10 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -100,6 +99,27 @@ public class MySqlDatabase extends Database {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> runScript(@NotNull InputStream inputStream, @NotNull Map<String, String> replacements) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try (Connection connection = getConnection()) {
|
||||
final String[] scriptString;
|
||||
scriptString = new String[]{new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)};
|
||||
replacements.forEach((key, value) -> scriptString[0] = scriptString[0].replaceAll(key, value));
|
||||
connection.setAutoCommit(false);
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
for (String statementString : scriptString[0].split(";")) {
|
||||
statement.addBatch(statementString);
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
} catch (IOException | SQLException e) {
|
||||
getLogger().log(Level.SEVERE, "An error occurred running a script on the MySQL database: ", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException {
|
||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
||||
|
||||
@@ -14,11 +14,10 @@ import org.sqlite.SQLiteConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -110,6 +109,31 @@ public class SqLiteDatabase extends Database {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> runScript(@NotNull InputStream inputStream, @NotNull Map<String, String> replacements) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
final String[] scriptString;
|
||||
scriptString = new String[]{new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)};
|
||||
replacements.forEach((key, value) -> scriptString[0] = scriptString[0].replaceAll(key, value));
|
||||
final boolean autoCommit = getConnection().getAutoCommit();
|
||||
|
||||
// Execute batched SQLite script
|
||||
getConnection().setAutoCommit(false);
|
||||
try (Statement statement = getConnection().createStatement()) {
|
||||
for (String statementString : scriptString[0].split(";")) {
|
||||
statement.addBatch(statementString);
|
||||
}
|
||||
statement.executeBatch();
|
||||
}
|
||||
getConnection().setAutoCommit(autoCommit);
|
||||
} catch (IOException | SQLException e) {
|
||||
getLogger().log(Level.SEVERE, "An exception occurred running script on the SQLite database", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException {
|
||||
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||
|
||||
Reference in new issue
Block a user