Skip to content

Commit

Permalink
Add util to dump db
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Nov 20, 2024
1 parent eb4c739 commit e836251
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ dependencies {
}

modCompileOnly("maven.modrinth:enchancement:1.20-26")

implementation(include("com.mysql:mysql-connector-j:8.0.31")!!)
}

tasks.processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx2G

# Mod Properties
mod_version = 0.0.40
mod_version = 0.0.41
maven_group = dev.ithundxr
archives_base_name = RailwaysTweaks

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.simibubi.create.Create;
import com.simibubi.create.content.trains.entity.CarriageContraptionEntity;
import dev.ithundxr.railwaystweaks.RailwaysTweaks;
import dev.ithundxr.railwaystweaks.database.DumpDatabase;
import dev.ithundxr.railwaystweaks.mixin.compat.tconstruct.SimpleChannelAccessor;
import me.pepperbell.simplenetworking.C2SPacket;
import me.pepperbell.simplenetworking.S2CPacket;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.Util;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import slimeknights.tconstruct.common.network.TinkerNetwork;
Expand Down Expand Up @@ -36,6 +38,16 @@ public static void init() {
.requires(cs -> cs.hasPermission(2))
.executes(ctx -> avgMSPT(ctx.getSource())));
});

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(literal("railwaystweaks")
.then(literal("dump_db")
.requires(cs -> cs.hasPermission(4))
.executes(ctx -> {
Util.ioPool().execute(DumpDatabase::dump);
return 0;
})));
});
}

private static ArgumentBuilder<CommandSourceStack, ?> $dump_hephaestus_packets() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package dev.ithundxr.railwaystweaks.database;

import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
import dev.ithundxr.railwaystweaks.RailwaysTweaks;
import net.fabricmc.loader.api.FabricLoader;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class DumpDatabase {
public static void dump() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");

String address = getProperty("address");
String database = getProperty("database");
String username = getProperty("username");
String password = getProperty("password");

MysqlConnectionPoolDataSource source = new MysqlConnectionPoolDataSource();
source.setUrl("jdbc:mysql://" + address + "/" + database);
source.setUser(username);
source.setPassword(password);
source.setDatabaseName(database);

try (Connection conn = source.getConnection();
Statement stat = conn.createStatement();
FileWriter writer = new FileWriter(Paths.get(FabricLoader.getInstance().getGameDir().toString(), "export.sql").toString())) {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"});

while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
System.out.println("Exporting table: " + tableName);

String createTableQuery = getCreateTableQuery(conn, tableName);
writer.write(createTableQuery + ";\n\n");

exportTableData(conn, tableName, writer);
writer.write("\n\n");
}
}
} catch (Exception e) {
RailwaysTweaks.LOGGER.error("Error occurred while dumping DB", e);
}
}

private static String getCreateTableQuery(Connection connection, String tableName) throws SQLException {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + tableName);
rs.next();
String createTable = rs.getString(2); // The second column contains the CREATE TABLE statement.
rs.close();
stmt.close();
return createTable;
}

private static void exportTableData(Connection connection, String tableName, FileWriter writer) throws SQLException, IOException {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
ResultSetMetaData rsMetaData = rs.getMetaData();
int columnCount = rsMetaData.getColumnCount();

while (rs.next()) {
StringBuilder row = new StringBuilder("INSERT INTO " + tableName + " VALUES (");
for (int i = 1; i <= columnCount; i++) {
String value = rs.getString(i);
if (value == null) {
row.append("NULL");
} else {
row.append("'").append(value.replace("'", "''")).append("'");
}
if (i < columnCount) row.append(", ");
}
row.append(");\n");
writer.write(row.toString());
}

rs.close();
stmt.close();
}

private static String getProperty(String name) {
return System.getProperty("railwayTweaks.database." + name);
}
}

0 comments on commit e836251

Please sign in to comment.