diff --git a/build.gradle.kts b/build.gradle.kts index d920fea..0a3b4c1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -69,6 +69,8 @@ dependencies { } modCompileOnly("maven.modrinth:enchancement:1.20-26") + + implementation(include("com.mysql:mysql-connector-j:8.0.31")!!) } tasks.processResources { diff --git a/gradle.properties b/gradle.properties index 229cd62..7976fd7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java b/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java index 5fb2757..9c3f823 100644 --- a/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java +++ b/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java @@ -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; @@ -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 $dump_hephaestus_packets() { diff --git a/src/main/java/dev/ithundxr/railwaystweaks/database/DumpDatabase.java b/src/main/java/dev/ithundxr/railwaystweaks/database/DumpDatabase.java new file mode 100644 index 0000000..fefd8fb --- /dev/null +++ b/src/main/java/dev/ithundxr/railwaystweaks/database/DumpDatabase.java @@ -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); + } +} \ No newline at end of file