From 5b3dfbcc215be7937272afd58eac337c6eaa0f3e Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sat, 20 May 2023 19:58:47 -0400 Subject: [PATCH] Refactor to remove abstraction --- src/engine/db/handlers/dbAccountHandler.java | 6 +- src/engine/db/handlers/dbBuildingHandler.java | 76 ++++++++++++------- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/src/engine/db/handlers/dbAccountHandler.java b/src/engine/db/handlers/dbAccountHandler.java index 577c264e..49d625bc 100644 --- a/src/engine/db/handlers/dbAccountHandler.java +++ b/src/engine/db/handlers/dbAccountHandler.java @@ -43,11 +43,11 @@ public class dbAccountHandler extends dbHandlerBase { return account; try (Connection connection = DbManager.getConnection(); - PreparedStatement accountQuery = connection.prepareStatement("SELECT * FROM `obj_account` WHERE `UID`=?")) { + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `obj_account` WHERE `UID`=?")) { - accountQuery.setLong(1, accountID); + preparedStatement.setLong(1, accountID); - ResultSet rs = accountQuery.executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); account = (Account) getObjectFromRs(rs); } catch (SQLException e) { diff --git a/src/engine/db/handlers/dbBuildingHandler.java b/src/engine/db/handlers/dbBuildingHandler.java index 4b282281..252c3c45 100644 --- a/src/engine/db/handlers/dbBuildingHandler.java +++ b/src/engine/db/handlers/dbBuildingHandler.java @@ -19,6 +19,8 @@ import engine.objects.*; import org.joda.time.DateTime; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; @@ -40,25 +42,25 @@ public class dbBuildingHandler extends dbHandlerBase { DateTime upgradeDate, int blueprintUUID, float w, float rotY) { Building toCreate = null; - try { - prepareCallable("CALL `building_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);"); - - setInt(1, parentZoneID); - setInt(2, OwnerUUID); - setString(3, name); - setInt(4, meshUUID); - setFloat(5, location.x); - setFloat(6, location.y); - setFloat(7, location.z); - setFloat(8, meshScale); - setInt(9, currentHP); - setString(10, protectionState.name()); - setInt(11, currentGold); - setInt(12, rank); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("CALL `building_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);")) { + + preparedStatement.setInt(1, parentZoneID); + preparedStatement.setInt(2, OwnerUUID); + preparedStatement.setString(3, name); + preparedStatement.setInt(4, meshUUID); + preparedStatement.setFloat(5, location.x); + preparedStatement.setFloat(6, location.y); + preparedStatement.setFloat(7, location.z); + preparedStatement.setFloat(8, meshScale); + preparedStatement.setInt(9, currentHP); + preparedStatement.setString(10, protectionState.name()); + preparedStatement.setInt(11, currentGold); + preparedStatement.setInt(12, rank); if (upgradeDate != null) - setTimeStamp(13, upgradeDate.getMillis()); + preparedStatement.setTimestamp(13, new java.sql.Timestamp(upgradeDate.getMillis())); else setNULL(13, java.sql.Types.DATE); @@ -66,16 +68,15 @@ public class dbBuildingHandler extends dbHandlerBase { setFloat(15, w); setFloat(16, rotY); - int objectUUID = (int) getUUID(); + ResultSet rs = preparedStatement.executeQuery(); + + int objectUUID = (int) rs.getLong("UID"); if (objectUUID > 0) toCreate = GET_BUILDINGBYUUID(objectUUID); - } catch (Exception e) { - Logger.error("CREATE_BUILDING", e.getMessage()); - return null; - } finally { - closeCallable(); + } catch (SQLException e) { + throw new RuntimeException(e); } return toCreate; @@ -93,6 +94,7 @@ public class dbBuildingHandler extends dbHandlerBase { } public Building GET_BUILDINGBYUUID(int uuid) { + if (uuid == 0) return null; @@ -101,11 +103,19 @@ public class dbBuildingHandler extends dbHandlerBase { if (building != null) return building; - prepareCallable("SELECT `obj_building`.*, `object`.`parent` FROM `object` INNER JOIN `obj_building` ON `obj_building`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_building`.*, `object`.`parent` FROM `object` INNER JOIN `obj_building` ON `obj_building`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;")) { + + preparedStatement.setLong(1, (long) uuid); - setLong(1, (long) uuid); - return (Building) getObjectSingle(uuid); + ResultSet rs = preparedStatement.executeQuery(); + building = (Building) getObjectFromRs(rs); + } catch (SQLException e) { + Logger.error(e); + } + + return building; } public Building GET_BUILDING_BY_MESH(final int meshID) { @@ -154,9 +164,19 @@ public class dbBuildingHandler extends dbHandlerBase { } private boolean removeFromBuildings(final Building b) { - prepareCallable("DELETE FROM `object` WHERE `UID` = ?"); - setLong(1, b.getObjectUUID()); - return (executeUpdate() > 0); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `object` WHERE `UID` = ?")) { + + preparedStatement.setLong(1, b.getObjectUUID()); + preparedStatement.execute(); + + } catch (SQLException e) { + Logger.error(e); + return false; + } + + return true; } public boolean CHANGE_NAME(Building b, String newName) {