Add SQLite migration

This commit is contained in:
William committed 2022-08-29 13:07:34 +01:00
1 parent 70ed11fc65
commit dcc4fe68ff
5 files changed
+156 -10

No files matched your search

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