restructured sql queries
This commit is contained in:
parent
92a3edc0b6
commit
39af3589e3
@ -6,8 +6,6 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PixelBlockDatabase {
|
||||
@ -15,9 +13,7 @@ public class PixelBlockDatabase {
|
||||
|
||||
private final PreparedStatement getAllPixelBlocks;
|
||||
private final PreparedStatement deletePixelBlock;
|
||||
private final PreparedStatement insertNewPixelBlock;
|
||||
private final PreparedStatement updateExistingPixelBlock;
|
||||
|
||||
private final PreparedStatement insertOrReplacePixelBlock;
|
||||
|
||||
public PixelBlockDatabase(String url) {
|
||||
try {
|
||||
@ -25,45 +21,39 @@ public class PixelBlockDatabase {
|
||||
this.db = DriverManager.getConnection(url);
|
||||
|
||||
this.db.createStatement().execute(
|
||||
"CREATE TABLE IF NOT EXISTS pixelblocks (" +
|
||||
"uuid CHAR(36) PRIMARY KEY, " +
|
||||
"owner CHAR(36), " +
|
||||
"locationWorldName CHAR(36), " +
|
||||
"locationX DOUBLE, " +
|
||||
"locationY DOUBLE, " +
|
||||
"locationZ DOUBLE, " +
|
||||
"entryLocationWorldName CHAR(36), " +
|
||||
"entryLocationX DOUBLE, " +
|
||||
"entryLocationY DOUBLE, " +
|
||||
"entryLocationZ DOUBLE, " +
|
||||
"direction CHAR(36)" +
|
||||
")"
|
||||
"CREATE TABLE IF NOT EXISTS pixelblocks (" +
|
||||
"uuid CHAR(36) PRIMARY KEY, " +
|
||||
"owner CHAR(36), " +
|
||||
"locationWorldName CHAR(36), " +
|
||||
"locationX DOUBLE, " +
|
||||
"locationY DOUBLE, " +
|
||||
"locationZ DOUBLE, " +
|
||||
"entryLocationWorldName CHAR(36), " +
|
||||
"entryLocationX DOUBLE, " +
|
||||
"entryLocationY DOUBLE, " +
|
||||
"entryLocationZ DOUBLE, " +
|
||||
"direction CHAR(36)" +
|
||||
")"
|
||||
);
|
||||
|
||||
this.deletePixelBlock = this.db.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?");
|
||||
this.getAllPixelBlocks = this.db.prepareStatement("SELECT * FROM pixelblocks");
|
||||
this.insertNewPixelBlock = this.db.prepareStatement(
|
||||
"INSERT INTO pixelblocks(uuid, owner, " +
|
||||
"locationWorldName, locationX, locationY, locationZ, " +
|
||||
"entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " +
|
||||
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
);
|
||||
this.updateExistingPixelBlock = this.db.prepareStatement(
|
||||
"UPDATE pixelblocks " +
|
||||
"SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," +
|
||||
"entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=?, direction=? " +
|
||||
"WHERE uuid=?;"
|
||||
this.insertOrReplacePixelBlock = this.db.prepareStatement(
|
||||
"INSERT OR REPLACE INTO pixelblocks(uuid, owner, " +
|
||||
"locationWorldName, locationX, locationY, locationZ, " +
|
||||
"entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " +
|
||||
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
|
||||
);
|
||||
|
||||
} catch (SQLException | RuntimeException | ClassNotFoundException e) {
|
||||
throw new RuntimeException("Failed to load Database", e);
|
||||
throw new RuntimeException("Error while initializing database", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws SQLException {
|
||||
deletePixelBlock.close();
|
||||
getAllPixelBlocks.close();
|
||||
insertNewPixelBlock.close();
|
||||
updateExistingPixelBlock.close();
|
||||
insertOrReplacePixelBlock.close();
|
||||
db.close();
|
||||
}
|
||||
|
||||
@ -72,87 +62,40 @@ public class PixelBlockDatabase {
|
||||
try {
|
||||
this.deletePixelBlock.setString(1, pixelBlock.blockUUID.toString());
|
||||
this.deletePixelBlock.executeUpdate();
|
||||
Main.plugin.getLogger().info("DB: Deleted PixelBlock: " + pixelBlock.blockUUID);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed to delete PixelBlock", e);
|
||||
throw new RuntimeException("Failed to delete PixelBlock from the database", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void savePixelBlock(PixelBlock pixelBlock) {
|
||||
Bukkit.getScheduler().runTask(Main.plugin, () -> {
|
||||
List<UUID> storedPixelBlocks = new ArrayList<>();
|
||||
|
||||
try {
|
||||
ResultSet pixelBlocksResult = this.getAllPixelBlocks.executeQuery();
|
||||
while (pixelBlocksResult.next()) {
|
||||
storedPixelBlocks.add(UUID.fromString(pixelBlocksResult.getString("uuid")));
|
||||
this.insertOrReplacePixelBlock.setString(1, pixelBlock.blockUUID.toString());
|
||||
this.insertOrReplacePixelBlock.setString(2, pixelBlock.ownerUUID.toString());
|
||||
|
||||
this.insertOrReplacePixelBlock.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName());
|
||||
this.insertOrReplacePixelBlock.setDouble(4, pixelBlock.pixelBlockLocation.getX());
|
||||
this.insertOrReplacePixelBlock.setDouble(5, pixelBlock.pixelBlockLocation.getY());
|
||||
this.insertOrReplacePixelBlock.setDouble(6, pixelBlock.pixelBlockLocation.getZ());
|
||||
|
||||
if (pixelBlock.lastEntryLocation != null) {
|
||||
this.insertOrReplacePixelBlock.setString(7, pixelBlock.lastEntryLocation.getWorld().getName());
|
||||
this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.lastEntryLocation.getX());
|
||||
this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.lastEntryLocation.getY());
|
||||
this.insertOrReplacePixelBlock.setDouble(10, pixelBlock.lastEntryLocation.getZ());
|
||||
} else {
|
||||
this.insertOrReplacePixelBlock.setString(7, pixelBlock.pixelBlockLocation.getWorld().getName());
|
||||
this.insertOrReplacePixelBlock.setDouble(8, pixelBlock.pixelBlockLocation.getX());
|
||||
this.insertOrReplacePixelBlock.setDouble(9, pixelBlock.pixelBlockLocation.getY());
|
||||
this.insertOrReplacePixelBlock.setDouble(10, pixelBlock.pixelBlockLocation.getZ());
|
||||
}
|
||||
|
||||
this.insertOrReplacePixelBlock.setString(11, pixelBlock.facingDirection.toString());
|
||||
|
||||
this.insertOrReplacePixelBlock.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed to fetch PixelBlock list", e);
|
||||
}
|
||||
|
||||
if(!storedPixelBlocks.contains(pixelBlock.blockUUID)) {
|
||||
// create new entry if it does not exist
|
||||
try {
|
||||
this.insertNewPixelBlock.setString(1, pixelBlock.blockUUID.toString());
|
||||
this.insertNewPixelBlock.setString(2, pixelBlock.ownerUUID.toString());
|
||||
|
||||
this.insertNewPixelBlock.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName());
|
||||
this.insertNewPixelBlock.setDouble(4, pixelBlock.pixelBlockLocation.getX());
|
||||
this.insertNewPixelBlock.setDouble(5, pixelBlock.pixelBlockLocation.getY());
|
||||
this.insertNewPixelBlock.setDouble(6, pixelBlock.pixelBlockLocation.getZ());
|
||||
|
||||
if(pixelBlock.lastEntryLocation != null) {
|
||||
this.insertNewPixelBlock.setString(7, pixelBlock.lastEntryLocation.getWorld().getName());
|
||||
this.insertNewPixelBlock.setDouble(8, pixelBlock.lastEntryLocation.getX());
|
||||
this.insertNewPixelBlock.setDouble(9, pixelBlock.lastEntryLocation.getY());
|
||||
this.insertNewPixelBlock.setDouble(10, pixelBlock.lastEntryLocation.getZ());
|
||||
} else {
|
||||
this.insertNewPixelBlock.setString(7, Bukkit.getWorlds().getFirst().getName());
|
||||
this.insertNewPixelBlock.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
|
||||
this.insertNewPixelBlock.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
|
||||
this.insertNewPixelBlock.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
|
||||
}
|
||||
|
||||
this.insertNewPixelBlock.setString(11, pixelBlock.facingDirection.toString());
|
||||
|
||||
this.insertNewPixelBlock.executeUpdate();
|
||||
Main.plugin.getLogger().info("DB: Created PixelBlock: " + pixelBlock.blockUUID);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed to save PixelBlock", e);
|
||||
}
|
||||
|
||||
} else {
|
||||
// update existing entry
|
||||
try {
|
||||
this.updateExistingPixelBlock.setString(1, pixelBlock.ownerUUID.toString());
|
||||
|
||||
this.updateExistingPixelBlock.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName());
|
||||
this.updateExistingPixelBlock.setDouble(3, pixelBlock.pixelBlockLocation.getX());
|
||||
this.updateExistingPixelBlock.setDouble(4, pixelBlock.pixelBlockLocation.getY());
|
||||
this.updateExistingPixelBlock.setDouble(5, pixelBlock.pixelBlockLocation.getZ());
|
||||
|
||||
if(pixelBlock.lastEntryLocation != null) {
|
||||
this.updateExistingPixelBlock.setString(6, pixelBlock.lastEntryLocation.getWorld().getName());
|
||||
this.updateExistingPixelBlock.setDouble(7, pixelBlock.lastEntryLocation.getX());
|
||||
this.updateExistingPixelBlock.setDouble(8, pixelBlock.lastEntryLocation.getY());
|
||||
this.updateExistingPixelBlock.setDouble(9, pixelBlock.lastEntryLocation.getZ());
|
||||
} else {
|
||||
this.updateExistingPixelBlock.setString(6, Bukkit.getWorlds().getFirst().getName());
|
||||
this.updateExistingPixelBlock.setDouble(7, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
|
||||
this.updateExistingPixelBlock.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
|
||||
this.updateExistingPixelBlock.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
|
||||
}
|
||||
|
||||
this.updateExistingPixelBlock.setString(10, pixelBlock.blockUUID.toString());
|
||||
this.updateExistingPixelBlock.setString(11, pixelBlock.facingDirection.toString());
|
||||
|
||||
this.updateExistingPixelBlock.executeUpdate();
|
||||
Main.plugin.getLogger().info("DB: Updated PixelBlock: " + pixelBlock.blockUUID);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed updating PixelBlock", e);
|
||||
}
|
||||
throw new RuntimeException("Failed to create or update PixelBlock in the database", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -183,10 +126,9 @@ public class PixelBlockDatabase {
|
||||
Direction.valueOf(allPixelBlocks.getString("direction"))
|
||||
);
|
||||
block.setLastEntryLocation(entryLocation);
|
||||
Main.plugin.getLogger().info("DB: Loaded PixelBlock: " + block.blockUUID);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed loading PixelBlocks", e);
|
||||
throw new RuntimeException("Failed loading PixelBlocks from the database", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user