From f299fcaffc65479fe373cb3461862be6d27db8dd Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 21 May 2023 15:10:27 -0400 Subject: [PATCH] Refactor to remove abstraction. --- src/engine/db/handlers/dbAccountHandler.java | 20 +-- .../db/handlers/dbCharacterPowerHandler.java | 135 +++++++++++++----- src/engine/db/handlers/dbKitHandler.java | 8 -- 3 files changed, 107 insertions(+), 56 deletions(-) diff --git a/src/engine/db/handlers/dbAccountHandler.java b/src/engine/db/handlers/dbAccountHandler.java index 49d625bc..850e4eac 100644 --- a/src/engine/db/handlers/dbAccountHandler.java +++ b/src/engine/db/handlers/dbAccountHandler.java @@ -63,14 +63,14 @@ public class dbAccountHandler extends dbHandlerBase { public void WRITE_ADMIN_LOG(String adminName, String logEntry) { try (Connection connection = DbManager.getConnection(); - PreparedStatement accountQuery = connection.prepareStatement("INSERT INTO dyn_admin_log(`dateTime`, `charName`, `eventString`)" + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO dyn_admin_log(`dateTime`, `charName`, `eventString`)" + " VALUES (?, ?, ?)")) { - accountQuery.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis())); - accountQuery.setString(2, adminName); - accountQuery.setString(3, logEntry); + preparedStatement.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis())); + preparedStatement.setString(2, adminName); + preparedStatement.setString(3, logEntry); - accountQuery.execute(); + preparedStatement.execute(); } catch (SQLException e) { Logger.error(e); @@ -97,9 +97,9 @@ public class dbAccountHandler extends dbHandlerBase { ArrayList machineList = new ArrayList<>(); try (Connection connection = DbManager.getConnection(); - PreparedStatement accountQuery = connection.prepareStatement("select `machineID` from `dyn_trash`")) { + PreparedStatement preparedStatement = connection.prepareStatement("select `machineID` from `dyn_trash`")) { - ResultSet rs = accountQuery.executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) machineList.add(rs.getString(1)); @@ -130,12 +130,12 @@ public class dbAccountHandler extends dbHandlerBase { ArrayList trashList = new ArrayList<>(); try (Connection connection = DbManager.getConnection(); - PreparedStatement accountQuery = connection.prepareStatement("select DISTINCT UID from object \n" + + PreparedStatement preparedStatement = connection.prepareStatement("select DISTINCT UID from object \n" + "where parent IN (select AccountID from dyn_login_history " + " WHERE`machineID`=?)")) { - accountQuery.setString(1, machineID); - ResultSet rs = accountQuery.executeQuery(); + preparedStatement.setString(1, machineID); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { diff --git a/src/engine/db/handlers/dbCharacterPowerHandler.java b/src/engine/db/handlers/dbCharacterPowerHandler.java index 31a64f8a..cbed4c38 100644 --- a/src/engine/db/handlers/dbCharacterPowerHandler.java +++ b/src/engine/db/handlers/dbCharacterPowerHandler.java @@ -16,6 +16,8 @@ import engine.objects.PlayerCharacter; import engine.server.MBServerStatics; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.ConcurrentHashMap; @@ -28,86 +30,143 @@ public class dbCharacterPowerHandler extends dbHandlerBase { } public CharacterPower ADD_CHARACTER_POWER(CharacterPower toAdd) { + + CharacterPower characterPower = null; + if (CharacterPower.getOwner(toAdd) == null || toAdd.getPower() == null) { Logger.error("dbCharacterSkillHandler.ADD_Power", toAdd.getObjectUUID() + " missing owner or powersBase"); return null; } - prepareCallable("INSERT INTO `dyn_character_power` (`CharacterID`, `powersBaseToken`, `trains`) VALUES (?, ?, ?);"); - setLong(1, (long)CharacterPower.getOwner(toAdd).getObjectUUID()); - setInt(2, toAdd.getPower().getToken()); - setInt(3, toAdd.getTrains()); - int powerID = insertGetUUID(); - return GET_CHARACTER_POWER(powerID); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_character_power` (`CharacterID`, `powersBaseToken`, `trains`) VALUES (?, ?, ?);")) { + + preparedStatement.setLong(1, CharacterPower.getOwner(toAdd).getObjectUUID()); + preparedStatement.setInt(2, toAdd.getPower().getToken()); + preparedStatement.setInt(3, toAdd.getTrains()); + preparedStatement.executeUpdate(); + ResultSet rs = preparedStatement.getGeneratedKeys(); + + if (rs.next()) + characterPower = GET_CHARACTER_POWER(rs.getInt(1)); + + } catch (SQLException e) { + Logger.error(e); + return null; + } + return characterPower; } public int DELETE_CHARACTER_POWER(final int objectUUID) { - prepareCallable("DELETE FROM `dyn_character_power` WHERE `UID` = ?"); - setLong(1, (long)objectUUID); - return executeUpdate(); + + int rowCount = 0; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_character_power` WHERE `UID` = ?")) { + + preparedStatement.setLong(1, objectUUID); + rowCount = preparedStatement.executeUpdate(); + + } catch (SQLException e) { + Logger.error(e); + } + + return rowCount; } public CharacterPower GET_CHARACTER_POWER(int objectUUID) { - CharacterPower cp = (CharacterPower) DbManager.getFromCache(Enum.GameObjectType.CharacterPower, objectUUID); - if (cp != null) - return cp; - prepareCallable("SELECT * FROM `dyn_character_power` WHERE `UID` = ?"); - setLong(1, (long)objectUUID); - return (CharacterPower) getObjectSingle(objectUUID); + CharacterPower characterPower = (CharacterPower) DbManager.getFromCache(Enum.GameObjectType.CharacterPower, objectUUID); + + if (characterPower != null) + return characterPower; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_character_power` WHERE `UID` = ?")) { + + preparedStatement.setLong(1, objectUUID); + ResultSet rs = preparedStatement.executeQuery(); + characterPower = (CharacterPower) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); + } + + return characterPower; } public ConcurrentHashMap GET_POWERS_FOR_CHARACTER(PlayerCharacter pc) { + ConcurrentHashMap powers = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); int objectUUID = pc.getObjectUUID(); - prepareCallable("SELECT * FROM `dyn_character_power` WHERE CharacterID = ?"); - setLong(1, (long)objectUUID); - ResultSet rs = executeQuery(); - try { + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_character_power` WHERE CharacterID = ?")) { + + preparedStatement.setLong(1, (long) objectUUID); + ResultSet rs = preparedStatement.executeQuery(); + while (rs.next()) { CharacterPower cp = new CharacterPower(rs, pc); if (cp.getPower() != null) powers.put(cp.getPower().getToken(), cp); } - rs.close(); + } catch (SQLException e) { - Logger.error("CharacterPower.getCharacterPowerForCharacter", "Exception:" + e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } + return powers; } - public void UPDATE_TRAINS(final CharacterPower pow) { + public void UPDATE_TRAINS(final CharacterPower characterPower) { + //skip update if nothing changed - if (!pow.isTrained()) + + if (!characterPower.isTrained()) return; - prepareCallable("UPDATE `dyn_character_power` SET `trains`=? WHERE `UID`=?"); - setShort(1, (short)pow.getTrains()); - setInt(2, pow.getObjectUUID()); - executeUpdate(); - pow.setTrained(false); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_character_power` SET `trains`=? WHERE `UID`=?")) { + + preparedStatement.setShort(1, (short) characterPower.getTrains()); + preparedStatement.setInt(2, characterPower.getObjectUUID()); + + preparedStatement.execute(); + + } catch (SQLException e) { + Logger.error(e); + } + + characterPower.setTrained(false); } public void updateDatabase(final CharacterPower pow) { + if (pow.getPower() == null) { - Logger.error( "Failed to find powersBase for Power " + pow.getObjectUUID()); + Logger.error("Failed to find powersBase for Power " + pow.getObjectUUID()); return; } + if (CharacterPower.getOwner(pow) == null) { - Logger.error( "Failed to find owner for Power " + pow.getObjectUUID()); + Logger.error("Failed to find owner for Power " + pow.getObjectUUID()); return; } + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_character_power` SET `PowersBaseToken`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?")) { + + preparedStatement.setInt(1, pow.getPower().getToken()); + preparedStatement.setInt(2, CharacterPower.getOwner(pow).getObjectUUID()); + preparedStatement.setShort(3, (short) pow.getTrains()); + preparedStatement.setInt(4, pow.getObjectUUID()); + preparedStatement.execute(); + + } catch (SQLException e) { + Logger.error(e); + } - prepareCallable("UPDATE `dyn_character_power` SET `PowersBaseToken`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?"); - setInt(1, pow.getPower().getToken()); - setInt(2, CharacterPower.getOwner(pow).getObjectUUID()); - setShort(3, (short)pow.getTrains()); - setInt(4, pow.getObjectUUID()); - executeUpdate(); pow.setTrained(false); } } diff --git a/src/engine/db/handlers/dbKitHandler.java b/src/engine/db/handlers/dbKitHandler.java index b558ec82..c0fa92ac 100644 --- a/src/engine/db/handlers/dbKitHandler.java +++ b/src/engine/db/handlers/dbKitHandler.java @@ -20,14 +20,6 @@ public class dbKitHandler extends dbHandlerBase { this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); } - public ArrayList GET_KITS_FOR_RACE_AND_BASECLASS(int raceID, int baseClassID) { - prepareCallable("SELECT vk.* FROM `static_rune_validkit` vk, `static_rune_racebaseclass` rbc WHERE rbc.`RaceID` = ? " - + "&& rbc.`BaseClassID` = ? && rbc.`ID` = vk.`RaceBaseClassesID`"); - setInt(1, raceID); - setInt(2, baseClassID); - return getObjectList(); - } - public ArrayList GET_ALL_KITS() { prepareCallable("SELECT * FROM `static_rune_validkit`");