Revert "fix: A lot of issues introduced after migrating to the new database API"

This reverts commit 2eec844447.
This commit is contained in:
Christian Koop 2023-10-24 01:51:02 +02:00
parent ab968ea984
commit fadb84f2a9
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
7 changed files with 130 additions and 15 deletions

View File

@ -56,7 +56,6 @@
<excludeDefaults>false</excludeDefaults>
<includes>
<include>**/nms/v*/**</include>
<include>**/core/third_party/org/h2/**</include>
</includes>
</filter>
</filters>

View File

@ -66,7 +66,7 @@ public class AnchorManagerImpl implements AnchorManager {
protected void saveAll() {
for (Set<Anchor> anchorSet : this.anchors.values()) {
Collection<Data> asData = new ArrayList<>(anchorSet.size());
this.dataManager.saveBatchSync(asData);
this.dataManager.saveBatch(asData);
}
}
@ -121,7 +121,7 @@ public class AnchorManagerImpl implements AnchorManager {
if (tmpAnchors != null) {
Collection<Data> asData = new ArrayList<>(tmpAnchors.size());
this.dataManager.saveBatchSync(asData);
this.dataManager.saveBatch(asData);
for (Anchor anchor : tmpAnchors) {
((AnchorImpl) anchor).deInit(this.plugin);
@ -236,7 +236,7 @@ public class AnchorManagerImpl implements AnchorManager {
}
Anchor anchor = new AnchorImpl(dataManager.getNextId("anchors"), owner, loc, ticks);
this.dataManager.saveSync(anchor);
this.dataManager.save(anchor);
Bukkit.getScheduler().runTask(this.plugin, () -> { //TODO: Do we need to run this sync, or we are already on the main thread?
Block block = loc.getBlock();
block.setType(Settings.MATERIAL.getMaterial().parseMaterial());
@ -290,7 +290,7 @@ public class AnchorManagerImpl implements AnchorManager {
anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5);
((AnchorImpl) anchor).deInit(this.plugin);
this.dataManager.deleteSync(anchor);
this.dataManager.delete(anchor);
}
/* Anchor access */

View File

@ -4,6 +4,8 @@ import com.craftaro.core.SongodaCore;
import com.craftaro.core.SongodaPlugin;
import com.craftaro.core.commands.CommandManager;
import com.craftaro.core.configuration.Config;
import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.core.database.SQLiteConnector;
import com.craftaro.core.gui.GuiManager;
import com.craftaro.core.hooks.EconomyManager;
import com.craftaro.core.hooks.HologramManager;
@ -14,8 +16,9 @@ import com.craftaro.epicanchors.commands.sub.GiveCommand;
import com.craftaro.epicanchors.commands.sub.ReloadCommand;
import com.craftaro.epicanchors.commands.sub.SettingsCommand;
import com.craftaro.epicanchors.commands.sub.ShowCommand;
import com.craftaro.epicanchors.files.DataManager;
import com.craftaro.epicanchors.files.Settings;
import com.craftaro.epicanchors.files.migration._2_AnchorMigration;
import com.craftaro.epicanchors.files.migration.AnchorMigration;
import com.craftaro.epicanchors.files.migration._1_InitialMigration;
import com.craftaro.epicanchors.listener.AnchorListener;
import com.craftaro.epicanchors.listener.BlockListener;
@ -44,7 +47,15 @@ public final class EpicAnchors extends SongodaPlugin {
public void onPluginEnable() {
SongodaCore.registerPlugin(this, 31, XMaterial.END_PORTAL_FRAME);
initDatabase(Arrays.asList(new _1_InitialMigration(), new _2_AnchorMigration()));
// Initialize database
// this.getLogger().info("Initializing SQLite...");
// DatabaseConnector dbCon = new SQLiteConnector(this);
// this.dataManager = new DataManager(dbCon, this);
// AnchorMigration anchorMigration = new AnchorMigration(dbCon, this.dataManager, new _1_InitialMigration());
// anchorMigration.runMigrations();
// anchorMigration.migrateLegacyData(this);
initDatabase(Arrays.asList(new _1_InitialMigration(), new AnchorMigration()));
this.anchorManager = new AnchorManagerImpl(this, this.dataManager);
EpicAnchorsApi.initApi(this.anchorManager);

View File

@ -0,0 +1,97 @@
package com.craftaro.epicanchors.files;
import com.craftaro.core.database.DatabaseConnector;
import com.craftaro.epicanchors.AnchorImpl;
import com.craftaro.epicanchors.api.Anchor;
import com.craftaro.epicanchors.files.migration.AnchorMigration;
import com.craftaro.epicanchors.utils.Callback;
import com.craftaro.epicanchors.utils.UpdateCallback;
import com.craftaro.epicanchors.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class DataManager {
// public void updateAnchors(Collection<Anchor> anchors, UpdateCallback callback) {
// this.databaseConnector.connect((con) -> {
// con.setAutoCommit(false);
//
// SQLException err = null;
//
// for (Anchor anchor : anchors) {
// try (PreparedStatement ps = con.prepareStatement("UPDATE " + this.anchorTable +
// " SET ticks_left =? WHERE id =?;")) {
// ps.setInt(1, anchor.getTicksLeft());
// ps.setInt(2, anchor.getDbId());
//
// ps.executeUpdate();
// } catch (SQLException ex) {
// err = ex;
// break;
// }
// }
//
// if (err == null) {
// con.commit();
//
// resolveUpdateCallback(callback, null);
// } else {
// con.rollback();
//
// resolveUpdateCallback(callback, err);
// }
//
// con.setAutoCommit(true);
// });
// }
//
// public void deleteAnchorAsync(Anchor anchor) {
// deleteAnchorAsync(anchor, null);
// }
//
// public void deleteAnchorAsync(Anchor anchor, UpdateCallback callback) {
// this.thread.execute(() ->
// this.databaseConnector.connect((con) -> {
// try (PreparedStatement ps = con.prepareStatement("DELETE FROM " + this.anchorTable +
// " WHERE id =?;")) {
// ps.setInt(1, anchor.getDbId());
//
// ps.executeUpdate();
//
// resolveUpdateCallback(callback, null);
// } catch (Exception ex) {
// resolveUpdateCallback(callback, ex);
// }
// })
// );
// }
//
// public static String getTableName(String prefix, String name) {
// String result = prefix + name;
//
// if (!result.matches("[a-z0-9_]+")) {
// throw new IllegalStateException("The generated table name '" + result + "' contains invalid characters");
// }
//
// return result;
// }
}

View File

@ -18,8 +18,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
public class _2_AnchorMigration extends DataMigration {
public _2_AnchorMigration() {
public class AnchorMigration extends DataMigration {
public AnchorMigration() {
super(2);
}

View File

@ -1,6 +1,7 @@
package com.craftaro.epicanchors.files.migration;
import com.craftaro.core.database.DataMigration;
import com.craftaro.epicanchors.EpicAnchors;
import java.sql.Connection;
import java.sql.SQLException;
@ -14,14 +15,15 @@ public class _1_InitialMigration extends DataMigration {
@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "anchors (" +
"id INTEGER NOT NULL PRIMARY KEY auto_increment," +
statement.execute("CREATE TABLE " + EpicAnchors.getPlugin(EpicAnchors.class).getDataManager().getTablePrefix() + "anchors (" +
"id INTEGER NOT NULL," +
"world_name TEXT NOT NULL," +
"x INTEGER NOT NULL," +
"y INTEGER NOT NULL," +
"z INTEGER NOT NULL," +
"ticks_left INTEGER NOT NULL," +
"owner VARCHAR(36)" +
"owner VARCHAR(36)," +
"PRIMARY KEY(id AUTOINCREMENT)" +
");");
}
}

View File

@ -1,6 +1,8 @@
package com.craftaro.epicanchors.utils;
import com.craftaro.core.database.Data;
import com.craftaro.core.database.DataManager;
import com.craftaro.core.third_party.org.jooq.Queries;
import com.craftaro.core.third_party.org.jooq.Query;
import com.craftaro.core.third_party.org.jooq.Record1;
import com.craftaro.core.third_party.org.jooq.Result;
@ -8,13 +10,17 @@ import com.craftaro.core.third_party.org.jooq.impl.DSL;
import com.craftaro.epicanchors.AnchorImpl;
import com.craftaro.epicanchors.EpicAnchors;
import com.craftaro.epicanchors.api.Anchor;
import com.craftaro.epicanchors.files.migration._2_AnchorMigration;
import com.craftaro.epicanchors.files.migration.AnchorMigration;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class DataHelper {
@ -62,7 +68,7 @@ public class DataHelper {
}
public static void migrateAnchor(List<_2_AnchorMigration.LegacyAnchorEntry> anchorQueue, UpdateCallback callback) {
public static void migrateAnchor(List<AnchorMigration.LegacyAnchorEntry> anchorQueue, UpdateCallback callback) {
DataManager dataManager = EpicAnchors.getPlugin(EpicAnchors.class).getDataManager();
//recreate it with Jooq
@ -71,7 +77,7 @@ public class DataHelper {
connection.setAutoCommit(false);
try {
List<Query> queries = new ArrayList<>();
for (_2_AnchorMigration.LegacyAnchorEntry entry : anchorQueue) {
for (AnchorMigration.LegacyAnchorEntry entry : anchorQueue) {
queries.add(dslContext.insertInto(DSL.table(dataManager.getTablePrefix() + "anchors"))
.columns(
DSL.field("world_name"),