From 608a28642857e024c6792ec95d84985b822dec9d Mon Sep 17 00:00:00 2001 From: MagicBot Date: Mon, 22 May 2023 07:29:22 -0400 Subject: [PATCH] Refactor to remove abstraction --- src/engine/db/handlers/dbMobHandler.java | 131 ++++++++++++++++------- 1 file changed, 94 insertions(+), 37 deletions(-) diff --git a/src/engine/db/handlers/dbMobHandler.java b/src/engine/db/handlers/dbMobHandler.java index b594793f..c94d57a4 100644 --- a/src/engine/db/handlers/dbMobHandler.java +++ b/src/engine/db/handlers/dbMobHandler.java @@ -132,60 +132,117 @@ public class dbMobHandler extends dbHandlerBase { } public boolean ADD_TO_GUARDS(final long captainUID, final int mobBaseID, final String name, final int slot) { - prepareCallable("INSERT INTO `dyn_guards` (`captainUID`, `mobBaseID`,`name`, `slot`) VALUES (?,?,?,?)"); - setLong(1, captainUID); - setInt(2, mobBaseID); - setString(3, name); - setInt(4, slot); - return (executeUpdate() > 0); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_guards` (`captainUID`, `mobBaseID`,`name`, `slot`) VALUES (?,?,?,?)")) { + + preparedStatement.setLong(1, captainUID); + preparedStatement.setInt(2, mobBaseID); + preparedStatement.setString(3, name); + preparedStatement.setInt(4, slot); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } } public boolean REMOVE_FROM_GUARDS(final long captainUID, final int mobBaseID, final int slot) { - prepareCallable("DELETE FROM `dyn_guards` WHERE `captainUID`=? AND `mobBaseID`=? AND `slot` =?"); - setLong(1, captainUID); - setInt(2, mobBaseID); - setInt(3, slot); - return (executeUpdate() > 0); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_guards` WHERE `captainUID`=? AND `mobBaseID`=? AND `slot` =?")) { + + preparedStatement.setLong(1, captainUID); + preparedStatement.setInt(2, mobBaseID); + preparedStatement.setInt(3, slot); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } } public ArrayList GET_ALL_MOBS_FOR_ZONE(Zone zone) { - prepareCallable("SELECT `obj_mob`.*, `object`.`parent` FROM `object` INNER JOIN `obj_mob` ON `obj_mob`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;"); - setLong(1, zone.getObjectUUID()); - return getLargeObjectList(); + + ArrayList mobileList = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_mob`.*, `object`.`parent` FROM `object` INNER JOIN `obj_mob` ON `obj_mob`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;")) { + + preparedStatement.setLong(1, zone.getObjectUUID()); + + ResultSet rs = preparedStatement.executeQuery(); + mobileList = getObjectsFromRs(rs, 1000); + + } catch (SQLException e) { + Logger.error(e); + } + + return mobileList; } public Mob GET_MOB(final int objectUUID) { - prepareCallable("SELECT `obj_mob`.*, `object`.`parent` FROM `object` INNER JOIN `obj_mob` ON `obj_mob`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;"); - setLong(1, objectUUID); - return (Mob) getObjectSingle(objectUUID); + + Mob mobile = null; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_mob`.*, `object`.`parent` FROM `object` INNER JOIN `obj_mob` ON `obj_mob`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;")) { + + preparedStatement.setLong(1, objectUUID); + ResultSet rs = preparedStatement.executeQuery(); + mobile = (Mob) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); + } + return mobile; } public int MOVE_MOB(long mobID, long parentID, float locX, float locY, float locZ) { - prepareCallable("UPDATE `object` INNER JOIN `obj_mob` On `object`.`UID` = `obj_mob`.`UID` SET `object`.`parent`=?, `obj_mob`.`mob_spawnX`=?, `obj_mob`.`mob_spawnY`=?, `obj_mob`.`mob_spawnZ`=? WHERE `obj_mob`.`UID`=?;"); - setLong(1, parentID); - setFloat(2, locX); - setFloat(3, locY); - setFloat(4, locZ); - setLong(5, mobID); - return executeUpdate(); + + int row_count = 0; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `object` INNER JOIN `obj_mob` On `object`.`UID` = `obj_mob`.`UID` SET `object`.`parent`=?, `obj_mob`.`mob_spawnX`=?, `obj_mob`.`mob_spawnY`=?, `obj_mob`.`mob_spawnZ`=? WHERE `obj_mob`.`UID`=?;")) { + + preparedStatement.setLong(1, parentID); + preparedStatement.setFloat(2, locX); + preparedStatement.setFloat(3, locY); + preparedStatement.setFloat(4, locZ); + preparedStatement.setLong(5, mobID); + + ResultSet rs = preparedStatement.executeQuery(); + row_count = preparedStatement.executeUpdate(); + + } catch (SQLException e) { + Logger.error(e); + } + return row_count; } public String SET_PROPERTY(final Mob m, String name, Object new_value) { - prepareCallable("CALL mob_SETPROP(?,?,?)"); - setLong(1, m.getObjectUUID()); - setString(2, name); - setString(3, String.valueOf(new_value)); - return getResult(); - } - public String SET_PROPERTY(final Mob m, String name, Object new_value, Object old_value) { - prepareCallable("CALL mob_GETSETPROP(?,?,?,?)"); - setLong(1, m.getObjectUUID()); - setString(2, name); - setString(3, String.valueOf(new_value)); - setString(4, String.valueOf(old_value)); - return getResult(); + String result = ""; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("CALL mob_SETPROP(?,?,?)")) { + + preparedStatement.setLong(1, m.getObjectUUID()); + preparedStatement.setString(2, name); + preparedStatement.setString(3, String.valueOf(new_value)); + + ResultSet rs = preparedStatement.executeQuery(); + result = rs.getString("result"); + + } catch (SQLException e) { + Logger.error(e); + } + return result; } }