From 6df10259adbef538294ad074f2b8b8417e7f9a70 Mon Sep 17 00:00:00 2001 From: Brianna Date: Fri, 2 Aug 2019 14:51:15 -0400 Subject: [PATCH] Improved sql. --- .../ultimatestacker/database/DataManager.java | 22 +++++++-------- .../migrations/_1_InitialMigration.java | 1 + .../ultimatestacker/spawner/SpawnerStack.java | 28 +++++++++++++------ 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/songoda/ultimatestacker/database/DataManager.java b/src/main/java/com/songoda/ultimatestacker/database/DataManager.java index 8f272af..479db02 100644 --- a/src/main/java/com/songoda/ultimatestacker/database/DataManager.java +++ b/src/main/java/com/songoda/ultimatestacker/database/DataManager.java @@ -51,14 +51,11 @@ public class DataManager { public void updateSpawner(SpawnerStack spawnerStack) { this.async(() -> this.databaseConnector.connect(connection -> { - String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE world = ? AND x = ? AND y = ? and z = ?"; + String updateSpawner = "UPDATE " + this.getTablePrefix() + "spawners SET amount = ? WHERE id = ?"; try (PreparedStatement statement = connection.prepareStatement(updateSpawner)) { statement.setInt(1, spawnerStack.getAmount()); - statement.setString(2, spawnerStack.getWorld().getName()); - statement.setInt(3, spawnerStack.getX()); - statement.setInt(4, spawnerStack.getY()); - statement.setInt(5, spawnerStack.getZ()); + statement.setInt(2, spawnerStack.getId()); statement.executeUpdate(); } })); @@ -67,7 +64,8 @@ public class DataManager { public void createSpawner(SpawnerStack spawnerStack) { this.async(() -> this.databaseConnector.connect(connection -> { - String createSpawner = "INSERT INTO " + this.getTablePrefix() + "spawners (amount, world, x, y, z) VALUES (?, ?, ?, ? , ?)"; + + String createSpawner = "INSERT INTO " + this.getTablePrefix() + "spawners (amount, world, x, y, z) VALUES (?, ?, ?, ?, ?)"; try (PreparedStatement statement = connection.prepareStatement(createSpawner)) { statement.setInt(1, spawnerStack.getAmount()); @@ -77,17 +75,16 @@ public class DataManager { statement.setInt(5, spawnerStack.getZ()); statement.executeUpdate(); } + int spawnerId = this.lastInsertedId(connection); + this.sync(() -> spawnerStack.setId(spawnerId)); })); } public void deleteSpawner(SpawnerStack spawnerStack) { this.async(() -> this.databaseConnector.connect(connection -> { - String deleteSpawner = "DELETE FROM " + this.getTablePrefix() + "spawners WHERE world = ? AND x = ? AND y = ? and z = ?"; + String deleteSpawner = "DELETE FROM " + this.getTablePrefix() + "spawners WHERE id = ?"; try (PreparedStatement statement = connection.prepareStatement(deleteSpawner)) { - statement.setString(1, spawnerStack.getWorld().getName()); - statement.setInt(2, spawnerStack.getX()); - statement.setInt(3, spawnerStack.getY()); - statement.setInt(4, spawnerStack.getZ()); + statement.setInt(1, spawnerStack.getId()); statement.executeUpdate(); } })); @@ -103,6 +100,8 @@ public class DataManager { ResultSet result = statement.executeQuery(selectSpawners); while (result.next()) { + int spawnerId = result.getInt("id"); + int amount = result.getInt("amount"); String world = result.getString("world"); @@ -112,6 +111,7 @@ public class DataManager { Location location = new Location(Bukkit.getWorld(world), x, y, z); SpawnerStack spawnerStack = new SpawnerStack(location, amount); + spawnerStack.setId(spawnerId); spawners.put(location, spawnerStack); } } diff --git a/src/main/java/com/songoda/ultimatestacker/database/migrations/_1_InitialMigration.java b/src/main/java/com/songoda/ultimatestacker/database/migrations/_1_InitialMigration.java index 10e6153..7c19f60 100644 --- a/src/main/java/com/songoda/ultimatestacker/database/migrations/_1_InitialMigration.java +++ b/src/main/java/com/songoda/ultimatestacker/database/migrations/_1_InitialMigration.java @@ -21,6 +21,7 @@ public class _1_InitialMigration extends DataMigration { // Create plugin settings table try (Statement statement = connection.createStatement()) { statement.execute("CREATE TABLE " + tablePrefix + "spawners (" + + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "amount INTEGER NOT NULL," + "world TEXT NOT NULL, " + "x DOUBLE NOT NULL, " + diff --git a/src/main/java/com/songoda/ultimatestacker/spawner/SpawnerStack.java b/src/main/java/com/songoda/ultimatestacker/spawner/SpawnerStack.java index 1e94e2e..f3df51d 100644 --- a/src/main/java/com/songoda/ultimatestacker/spawner/SpawnerStack.java +++ b/src/main/java/com/songoda/ultimatestacker/spawner/SpawnerStack.java @@ -13,6 +13,8 @@ import java.util.Random; public class SpawnerStack { + private int id; + private final Location location; private int amount = 1; @@ -46,6 +48,23 @@ public class SpawnerStack { }, 1L); } + public int calculateSpawnCount() { + Random random = new Random(); + int count = 0; + for (int i = 0; i < getAmount(); i ++) { + count += random.nextInt(3 - 1 + 1) + 1; + } + return count; + } + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + public Location getLocation() { return location.clone(); } @@ -78,13 +97,4 @@ public class SpawnerStack { + "}" + "}"; } - - public int calculateSpawnCount() { - Random random = new Random(); - int count = 0; - for (int i = 0; i < getAmount(); i ++) { - count += random.nextInt(3 - 1 + 1) + 1; - } - return count; - } }