From c6b8d27d537c28849add2043dadedd26c7154df4 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Tue, 23 May 2023 10:42:34 -0400 Subject: [PATCH] Refactor to remove abstraction. --- .../db/handlers/dbPlayerCharacterHandler.java | 1 - .../db/handlers/dbSkillBaseHandler.java | 103 ++++++++++-------- 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/src/engine/db/handlers/dbPlayerCharacterHandler.java b/src/engine/db/handlers/dbPlayerCharacterHandler.java index a716f05a..06a4d595 100644 --- a/src/engine/db/handlers/dbPlayerCharacterHandler.java +++ b/src/engine/db/handlers/dbPlayerCharacterHandler.java @@ -512,7 +512,6 @@ public class dbPlayerCharacterHandler extends dbHandlerBase { Logger.error(e); } - prepareCallable("SELECT * FROM dyn_character_friends"); } public boolean ADD_HERALDY(int source, AbstractWorldObject character) { diff --git a/src/engine/db/handlers/dbSkillBaseHandler.java b/src/engine/db/handlers/dbSkillBaseHandler.java index 77e55fdf..3bfdefe6 100644 --- a/src/engine/db/handlers/dbSkillBaseHandler.java +++ b/src/engine/db/handlers/dbSkillBaseHandler.java @@ -12,7 +12,6 @@ package engine.db.handlers; import engine.Enum; import engine.Enum.GameObjectType; import engine.gameManager.DbManager; -import engine.objects.AbstractGameObject; import engine.objects.MaxSkills; import engine.objects.SkillsBase; import org.pmw.tinylog.Logger; @@ -34,63 +33,83 @@ public class dbSkillBaseHandler extends dbHandlerBase { public SkillsBase GET_BASE(final int objectUUID) { SkillsBase skillsBase = (SkillsBase) DbManager.getFromCache(GameObjectType.SkillsBase, objectUUID); + if (skillsBase != null) return skillsBase; - prepareCallable("SELECT * FROM static_skill_skillsbase WHERE ID = ?"); - setInt(1, objectUUID); - SkillsBase sb; - sb = (SkillsBase) getObjectSingle(objectUUID); - SkillsBase.putInCache(sb); - return sb; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_skill_skillsbase WHERE ID = ?")) { + + preparedStatement.setInt(1, objectUUID); + ResultSet rs = preparedStatement.executeQuery(); + + skillsBase = (SkillsBase) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); + } + + SkillsBase.putInCache(skillsBase); + return skillsBase; } public SkillsBase GET_BASE_BY_NAME(String name) { - SkillsBase sb = SkillsBase.getFromCache(name); - if (sb != null) { - return sb; - } - prepareCallable("SELECT * FROM static_skill_skillsbase WHERE name = ?"); - setString(1, name); - ArrayList result = getObjectList(); - if (result.size() > 0) { - sb = (SkillsBase) result.get(0); - SkillsBase.putInCache(sb); - return sb; - } else { - return null; + + SkillsBase skillsBase = SkillsBase.getFromCache(name); + + if (skillsBase != null) + return skillsBase; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_skill_skillsbase WHERE name = ?")) { + + preparedStatement.setString(1, name); + ResultSet rs = preparedStatement.executeQuery(); + + skillsBase = (SkillsBase) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); } + + SkillsBase.putInCache(skillsBase); + return skillsBase; } public SkillsBase GET_BASE_BY_TOKEN(final int token) { - SkillsBase sb = SkillsBase.getFromCache(token); - if (sb != null) { - return sb; - } - prepareCallable("SELECT * FROM static_skill_skillsbase WHERE token = ?"); - setInt(1, token); - ArrayList result = getObjectList(); - if (result.size() > 0) { - sb = (SkillsBase) result.get(0); - SkillsBase.putInCache(sb); - return sb; - } else { - return null; + SkillsBase skillsBase = SkillsBase.getFromCache(token); + + if (skillsBase != null) + return skillsBase; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_skill_skillsbase WHERE token = ?")) { + + preparedStatement.setInt(1, token); + ResultSet rs = preparedStatement.executeQuery(); + + skillsBase = (SkillsBase) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); } + + SkillsBase.putInCache(skillsBase); + return skillsBase; } public void LOAD_ALL_MAX_SKILLS_FOR_CONTRACT() { - prepareCallable("SELECT * FROM `static_rune_maxskills`"); - + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_rune_maxskills`")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); - //shrines cached in rs for easy cache on creation. while (rs.next()) { MaxSkills maxSKills = new MaxSkills(rs); + if (MaxSkills.MaxSkillsSet.get(maxSKills.getRuneID()) == null) { ArrayList newMaxSkillsList = new ArrayList<>(); newMaxSkillsList.add(maxSKills); @@ -100,17 +119,13 @@ public class dbSkillBaseHandler extends dbHandlerBase { } - } catch (SQLException e) { - Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); + Logger.error(e); } - } - public void LOAD_ALL_RUNE_SKILLS() { + public void LOAD_ALL_RUNE_SKILLS() { try (Connection connection = DbManager.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_skill_skillsgranted`")) {