Switch back to old format for now

This commit is contained in:
ceze88 2023-08-10 11:46:37 +02:00
parent c3952e9bd7
commit f5a038aa0a

View File

@ -1,5 +1,6 @@
package com.craftaro.epichoppers.utils;
import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.core.third_party.org.jooq.Query;
import com.craftaro.core.third_party.org.jooq.impl.DSL;
import com.craftaro.core.utils.ItemSerializer;
@ -10,7 +11,10 @@ import com.craftaro.epichoppers.hopper.ItemType;
import com.craftaro.epichoppers.hopper.LinkType;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Base64;
@ -40,55 +44,57 @@ public class DataHelper {
}
public static void updateItems(HopperImpl hopper, ItemType type, List<ItemStack> items) {
// try (Connection connection = this.databaseConnector.getConnection()) {
// String clearItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE hopper_id = ? AND item_type = ?";
// try (PreparedStatement statement = connection.prepareStatement(clearItems)) {
// statement.setInt(1, hopper.getId());
// statement.setString(2, type.name());
// statement.executeUpdate();
// }
//
// String createItem = "INSERT INTO " + this.getTablePrefix() + "items (hopper_id, item_type, item) VALUES (?, ?, ?)";
// try (PreparedStatement statement = connection.prepareStatement(createItem)) {
// for (ItemStack item : items) {
// statement.setInt(1, hopper.getId());
// statement.setString(2, type.name());
//
// try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
// bukkitStream.writeObject(item);
// statement.setString(3, Base64.getEncoder().encodeToString(stream.toByteArray()));
// } catch (IOException e) {
// e.printStackTrace();
// continue;
// }
// statement.addBatch();
// }
// statement.executeBatch();
// }
// } catch (Exception ex) {
// ex.printStackTrace();
// }
DatabaseConnector databaseConnector = EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getDatabaseConnector();
String tablePrefix = EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getTablePrefix();
try (Connection connection = databaseConnector.getConnection()) {
String clearItems = "DELETE FROM " + tablePrefix + "items WHERE hopper_id = ? AND item_type = ?";
try (PreparedStatement statement = connection.prepareStatement(clearItems)) {
statement.setInt(1, hopper.getId());
statement.setString(2, type.name());
statement.executeUpdate();
}
String createItem = "INSERT INTO " + tablePrefix + "items (hopper_id, item_type, item) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createItem)) {
for (ItemStack item : items) {
statement.setInt(1, hopper.getId());
statement.setString(2, type.name());
try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
bukkitStream.writeObject(item);
statement.setString(3, Base64.getEncoder().encodeToString(stream.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
continue;
}
statement.addBatch();
}
statement.executeBatch();
}
} catch (Exception ex) {
ex.printStackTrace();
}
//Recreate with jooq
EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getDatabaseConnector().connectDSL(dslContext -> {
dslContext.deleteFrom(DSL.table(EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getTablePrefix() + "items"))
.where(DSL.field("hopper_id").eq(hopper.getId()))
.and(DSL.field("item_type").eq(type.name()))
.execute();
dslContext.batch(
items.stream().map(item -> dslContext.insertInto(DSL.table(EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getTablePrefix() + "items"))
.columns(
DSL.field("hopper_id"),
DSL.field("item_type"),
DSL.field("item"))
.values(
hopper.getId(),
type.name(),
Base64.getEncoder().encodeToString(ItemSerializer.serializeItem(item)))
).toArray(Query[]::new)
).execute();
});
// EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getDatabaseConnector().connectDSL(dslContext -> {
// dslContext.deleteFrom(DSL.table(EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getTablePrefix() + "items"))
// .where(DSL.field("hopper_id").eq(hopper.getId()))
// .and(DSL.field("item_type").eq(type.name()))
// .execute();
//
// dslContext.batch(
// items.stream().map(item -> dslContext.insertInto(DSL.table(EpicHoppers.getPlugin(EpicHoppers.class).getDataManager().getTablePrefix() + "items"))
// .columns(
// DSL.field("hopper_id"),
// DSL.field("item_type"),
// DSL.field("item"))
// .values(
// hopper.getId(),
// type.name(),
// Base64.getEncoder().encodeToString(ItemSerializer.serializeItem(item)))
// ).toArray(Query[]::new)
// ).execute();
// });
}
public static void deleteLinks(Hopper hopper) {