diff --git a/src/engine/db/handlers/dbContractHandler.java b/src/engine/db/handlers/dbContractHandler.java index f8b41433..893dbcf4 100644 --- a/src/engine/db/handlers/dbContractHandler.java +++ b/src/engine/db/handlers/dbContractHandler.java @@ -16,9 +16,10 @@ import engine.objects.ItemBase; import engine.objects.MobEquipment; 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; public class dbContractHandler extends dbHandlerBase { @@ -28,48 +29,37 @@ public class dbContractHandler extends dbHandlerBase { } public Contract GET_CONTRACT(final int objectUUID) { + Contract contract = (Contract) DbManager.getFromCache(Enum.GameObjectType.Contract, objectUUID); + if (contract != null) return contract; + if (objectUUID == 0) return null; - prepareCallable("SELECT * FROM `static_npc_contract` WHERE `ID` = ?"); - setInt(1, objectUUID); - return (Contract) getObjectSingle(objectUUID); - } - public ArrayList GET_CONTRACT_BY_RACE(final int objectUUID) { + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_building`.*, `object`.`parent` FROM `object` INNER JOIN `obj_building` ON `obj_building`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;")) { - ArrayList contracts = new ArrayList<>(); + preparedStatement.setInt(1, objectUUID); - prepareCallable("SELECT * FROM static_npc_contract WHERE `mobbaseID` =?;"); - setLong(1, objectUUID); - - try { - ResultSet rs = executeQuery(); - - //shrines cached in rs for easy cache on creation. - while (rs.next()) { - Contract contract = new Contract(rs); - if (contract != null) - contracts.add(contract); - } + ResultSet rs = preparedStatement.executeQuery(); + contract = (Contract) getObjectFromRs(rs); } catch (SQLException e) { - Logger.error( e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); + Logger.error(e); } - return contracts; + return contract; } - public void GET_GENERIC_INVENTORY(final Contract contract) { + public void LOAD_CONTRACT_INVENTORY(final Contract contract) { + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_inventoryset` WHERE `inventorySet` = ?;")) { - prepareCallable("SELECT * FROM `static_npc_inventoryset` WHERE `inventorySet` = ?;"); - setInt(1, contract.inventorySet); + preparedStatement.setInt(1, contract.inventorySet); - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { @@ -102,56 +92,78 @@ public class dbContractHandler extends dbHandlerBase { } } } catch (SQLException e) { - Logger.error("SQL Error number: " + e.getErrorCode() + ' ' + e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } } - public void GET_SELL_LISTS(final Contract con) { - prepareCallable("SELECT * FROM `static_npc_contract_selltype` WHERE `contractID` = ?;"); - setInt(1, con.getObjectUUID()); - try { - ResultSet rs = executeQuery(); - ArrayList item = con.getBuyItemType(); - ArrayList skill = con.getBuySkillToken(); - ArrayList unknown = con.getBuyUnknownToken(); + public void LOAD_SELL_LIST_FOR_CONTRACT(final Contract contract) { + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_contract_selltype` WHERE `contractID` = ?;")) { + + preparedStatement.setInt(1, contract.getObjectUUID()); + + ResultSet rs = preparedStatement.executeQuery(); + while (rs.next()) { + int type = rs.getInt("type"); int value = rs.getInt("value"); - if (type == 1) { - item.add(value); - } else if (type == 2) { - skill.add(value); - } else if (type == 3) { - unknown.add(value); + + switch (type) { + case 1: + contract.getBuyItemType().add(value); + break; + case 2: + contract.getBuySkillToken().add(value); + break; + case 3: + contract.getBuyUnknownToken().add(value); + break; } } } catch (SQLException e) { - Logger.error("SQL Error number: " + e.getErrorCode() + ' ' + e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } } public boolean updateAllowedBuildings(final Contract con, final long slotbitvalue) { - prepareCallable("UPDATE `static_npc_contract` SET `allowedBuildingTypeID`=? WHERE `contractID`=?"); - setLong(1, slotbitvalue); - setInt(2, con.getContractID()); - return (executeUpdate() > 0); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_npc_contract` SET `allowedBuildingTypeID`=? WHERE `contractID`=?")) { + + preparedStatement.setLong(1, slotbitvalue); + preparedStatement.setInt(2, con.getContractID()); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } } public boolean updateDatabase(final Contract con) { - prepareCallable("UPDATE `static_npc_contract` SET `contractID`=?, `name`=?, " - + "`mobbaseID`=?, `classID`=?, vendorDialog=?, iconID=?, allowedBuildingTypeID=? WHERE `ID`=?"); - setInt(1, con.getContractID()); - setString(2, con.getName()); - setInt(3, con.getMobbaseID()); - setInt(4, con.getClassID()); - setInt(5, (con.getVendorDialog() != null) ? con.getVendorDialog().getObjectUUID() : 0); - setInt(6, con.getIconID()); - setInt(8, con.getObjectUUID()); - setLong(7, con.getAllowedBuildings().toLong()); - return (executeUpdate() > 0); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_npc_contract` SET `contractID`=?, `name`=?, " + + "`mobbaseID`=?, `classID`=?, vendorDialog=?, iconID=?, allowedBuildingTypeID=? WHERE `ID`=?")) { + + preparedStatement.setInt(1, con.getContractID()); + preparedStatement.setString(2, con.getName()); + preparedStatement.setInt(3, con.getMobbaseID()); + preparedStatement.setInt(4, con.getClassID()); + preparedStatement.setInt(5, (con.getVendorDialog() != null) ? con.getVendorDialog().getObjectUUID() : 0); + preparedStatement.setInt(6, con.getIconID()); + preparedStatement.setInt(8, con.getObjectUUID()); + preparedStatement.setLong(7, con.getAllowedBuildings().toLong()); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } + } } diff --git a/src/engine/objects/Contract.java b/src/engine/objects/Contract.java index c31f59af..2fb2626d 100644 --- a/src/engine/objects/Contract.java +++ b/src/engine/objects/Contract.java @@ -138,8 +138,8 @@ public class Contract extends AbstractGameObject { //Specify if trainer, merchant, banker, etc via classID private void setBools() { - DbManager.ContractQueries.GET_GENERIC_INVENTORY(this); - DbManager.ContractQueries.GET_SELL_LISTS(this); + DbManager.ContractQueries.LOAD_CONTRACT_INVENTORY(this); + DbManager.ContractQueries.LOAD_SELL_LIST_FOR_CONTRACT(this); this.isTrainer = this.classID > 2499 && this.classID < 3050 || this.classID == 2028;