From db3f97eccfdac68f187780a0c32a631b2b50c6ee Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 21 May 2023 17:50:44 -0400 Subject: [PATCH] Refactor to remove abstraction. --- .../db/handlers/dbEnchantmentHandler.java | 62 +++++++++++++------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/src/engine/db/handlers/dbEnchantmentHandler.java b/src/engine/db/handlers/dbEnchantmentHandler.java index 383f3d8c..a80a771c 100644 --- a/src/engine/db/handlers/dbEnchantmentHandler.java +++ b/src/engine/db/handlers/dbEnchantmentHandler.java @@ -9,9 +9,12 @@ package engine.db.handlers; +import engine.gameManager.DbManager; 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; @@ -19,33 +22,54 @@ import java.util.concurrent.ConcurrentHashMap; public class dbEnchantmentHandler extends dbHandlerBase { public ConcurrentHashMap GET_ENCHANTMENTS_FOR_ITEM(final int id) { + ConcurrentHashMap enchants = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); - prepareCallable("SELECT * FROM `dyn_item_enchantment` WHERE `ItemID`=?;"); - setLong(1, (long)id); - try { - ResultSet resultSet = executeQuery(); - while (resultSet.next()) - enchants.put(resultSet.getString("powerAction"), resultSet.getInt("rank")); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_item_enchantment` WHERE `ItemID`=?;")) { + + preparedStatement.setLong(1, (long) id); + + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) + enchants.put(rs.getString("powerAction"), rs.getInt("rank")); + } catch (SQLException e) { - Logger.error("SQL Error number: " + e.getErrorCode()); - } finally { - closeCallable(); + Logger.error(e); } + return enchants; } public boolean CREATE_ENCHANTMENT_FOR_ITEM(long itemID, String powerAction, int rank) { - prepareCallable("INSERT INTO `dyn_item_enchantment` (`itemID`, `powerAction`, `rank`) VALUES (?, ?, ?);"); - setLong(1, itemID); - setString(2, powerAction); - setInt(3, rank); - return (executeUpdate() != 0); - } - public boolean CLEAR_ENCHANTMENTS(long itemID) { - prepareCallable("DELETE FROM `dyn_item_enchantment` WHERE `itemID`=?;"); - setLong(1, itemID); - return (executeUpdate() != 0); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_item_enchantment` (`itemID`, `powerAction`, `rank`) VALUES (?, ?, ?);")) { + + preparedStatement.setLong(1, itemID); + preparedStatement.setString(2, powerAction); + preparedStatement.setInt(3, rank); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } + } + public boolean CLEAR_ENCHANTMENTS(long itemID) { + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_item_enchantment` WHERE `itemID`=?;")) { + + preparedStatement.setLong(1, itemID); + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } + } }