From 3e604b53faa93372db1bdf021fae9785deb6f14a Mon Sep 17 00:00:00 2001 From: MagicBot Date: Mon, 22 May 2023 12:58:16 -0400 Subject: [PATCH] Refactor to remove abstraction. --- .../db/handlers/dbRuneBaseEffectHandler.java | 1 - src/engine/db/handlers/dbRuneBaseHandler.java | 152 +++++++++--------- 2 files changed, 79 insertions(+), 74 deletions(-) diff --git a/src/engine/db/handlers/dbRuneBaseEffectHandler.java b/src/engine/db/handlers/dbRuneBaseEffectHandler.java index 14c51683..c8b790a1 100644 --- a/src/engine/db/handlers/dbRuneBaseEffectHandler.java +++ b/src/engine/db/handlers/dbRuneBaseEffectHandler.java @@ -55,7 +55,6 @@ public class dbRuneBaseEffectHandler extends dbHandlerBase { try (Connection connection = DbManager.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_rune_baseeffect`;")) { - ResultSet rs = preparedStatement.executeQuery(); runeBaseEffectsList = getObjectsFromRs(rs, 250); diff --git a/src/engine/db/handlers/dbRuneBaseHandler.java b/src/engine/db/handlers/dbRuneBaseHandler.java index 44e58179..235f96a9 100644 --- a/src/engine/db/handlers/dbRuneBaseHandler.java +++ b/src/engine/db/handlers/dbRuneBaseHandler.java @@ -9,14 +9,16 @@ package engine.db.handlers; +import engine.gameManager.DbManager; import engine.objects.RuneBase; import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; public class dbRuneBaseHandler extends dbHandlerBase { @@ -26,66 +28,90 @@ public class dbRuneBaseHandler extends dbHandlerBase { } public void GET_RUNE_REQS(final RuneBase rb) { - prepareCallable("SELECT * FROM `static_rune_runereq` WHERE `runeID` = ?"); - setInt(1, rb.getObjectUUID()); - try { - ResultSet rs = executeQuery(); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_rune_runereq` WHERE `runeID` = ?")) { + + preparedStatement.setInt(1, rb.getObjectUUID()); + + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { int type = rs.getInt("type"); switch (type) { - case 1: - rb.getRace().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); - break; - case 2: - rb.getBaseClass().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); - break; - case 3: - rb.getPromotionClass().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); - break; - case 4: - rb.getDiscipline().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); - break; - case 5: - rb.getOverwrite().add(rs.getInt("requiredRuneID")); - break; - case 6: - rb.setLevelRequired(rs.getInt("requiredRuneID")); - break; + case 1: + rb.getRace().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); + break; + case 2: + rb.getBaseClass().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); + break; + case 3: + rb.getPromotionClass().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); + break; + case 4: + rb.getDiscipline().put(rs.getInt("requiredRuneID"), rs.getBoolean("isAllowed")); + break; + case 5: + rb.getOverwrite().add(rs.getInt("requiredRuneID")); + break; + case 6: + rb.setLevelRequired(rs.getInt("requiredRuneID")); + break; } } - rs.close(); + } catch (SQLException e) { - Logger.error("SQL Error number: " + e.getErrorCode()); - } finally { - closeCallable(); + Logger.error(e); } + } public RuneBase GET_RUNEBASE(final int id) { - prepareCallable("SELECT * FROM `static_rune_runebase` WHERE `ID` = ?"); - setInt(1, id); - return (RuneBase) getObjectSingle(id); + + RuneBase runeBase = null; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_rune_runebase` WHERE `ID` = ?")) { + + preparedStatement.setInt(1, id); + + ResultSet rs = preparedStatement.executeQuery(); + runeBase = (RuneBase) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); + } + return runeBase; } public ArrayList LOAD_ALL_RUNEBASES() { - prepareCallable("SELECT * FROM `static_rune_runebase`;"); - return getObjectList(); + + ArrayList runeBasesList = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_rune_runebase`;")) { + + ResultSet rs = preparedStatement.executeQuery(); + runeBasesList = getObjectsFromRs(rs, 1000); + + } catch (SQLException e) { + Logger.error(e); + } + + return runeBasesList; } public HashMap> LOAD_ALLOWED_STARTING_RUNES_FOR_BASECLASS() { - HashMap> runeSets; + HashMap> runeSets = new HashMap<>(); - runeSets = new HashMap<>(); int recordsRead = 0; - prepareCallable("SELECT * FROM static_rune_baseclassrune"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_rune_baseclassrune")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -94,25 +120,23 @@ public class dbRuneBaseHandler extends dbHandlerBase { int baseClassID = rs.getInt("BaseClassesID"); int runeBaseID = rs.getInt("RuneBaseID"); - if (runeSets.get(baseClassID) == null){ + if (runeSets.get(baseClassID) == null) { ArrayList runeList = new ArrayList<>(); runeList.add(runeBaseID); runeSets.put(baseClassID, runeList); - } - else{ - ArrayListruneList = runeSets.get(baseClassID); + } else { + ArrayList runeList = runeSets.get(baseClassID); runeList.add(runeBaseID); runeSets.put(baseClassID, runeList); } } - Logger.info("read: " + recordsRead + " cached: " + runeSets.size()); - } catch (SQLException e) { - Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); + Logger.error(e); } + + Logger.info("read: " + recordsRead + " cached: " + runeSets.size()); + return runeSets; } @@ -123,10 +147,10 @@ public class dbRuneBaseHandler extends dbHandlerBase { runeSets = new HashMap<>(); int recordsRead = 0; - prepareCallable("SELECT * FROM static_rune_racerune"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_rune_racerune")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -135,39 +159,21 @@ public class dbRuneBaseHandler extends dbHandlerBase { int raceID = rs.getInt("RaceID"); int runeBaseID = rs.getInt("RuneBaseID"); - if (runeSets.get(raceID) == null){ + if (runeSets.get(raceID) == null) { ArrayList runeList = new ArrayList<>(); runeList.add(runeBaseID); runeSets.put(raceID, runeList); - } - else{ - ArrayListruneList = runeSets.get(raceID); + } else { + ArrayList runeList = runeSets.get(raceID); runeList.add(runeBaseID); runeSets.put(raceID, runeList); } } - - Logger.info( "read: " + recordsRead + " cached: " + runeSets.size()); - } catch (SQLException e) { - Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); + Logger.error(e); } - return runeSets; - } - - public ArrayList GET_RUNEBASE_FOR_BASECLASS(final int id) { - prepareCallable("SELECT rb.* FROM static_rune_baseclassrune bcr, static_rune_runebase rb WHERE bcr.RuneBaseID = rb.ID " - + "&& ( bcr.BaseClassesID = 111111 || bcr.BaseClassesID = ? )"); - setInt(1, id); - return getObjectList(); - } - public HashSet GET_RUNEBASE_FOR_RACE(final int id) { - prepareCallable("SELECT rb.* FROM static_rune_racerune rr, static_rune_runebase rb" - + " WHERE rr.RuneBaseID = rb.ID && ( rr.RaceID = 111111 || rr.RaceID = ?)"); - setInt(1, id); - return new HashSet<>(getObjectList()); + Logger.info("read: " + recordsRead + " cached: " + runeSets.size()); + return runeSets; } }