diff --git a/src/engine/db/handlers/dbShrineHandler.java b/src/engine/db/handlers/dbShrineHandler.java index fbf23eff..2dc1289e 100644 --- a/src/engine/db/handlers/dbShrineHandler.java +++ b/src/engine/db/handlers/dbShrineHandler.java @@ -18,7 +18,8 @@ import engine.objects.Shrine; import org.joda.time.DateTime; import org.pmw.tinylog.Logger; -import java.net.UnknownHostException; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -35,78 +36,77 @@ public class dbShrineHandler extends dbHandlerBase { ProtectionState protectionState, int currentGold, int rank, DateTime upgradeDate, int blueprintUUID, float w, float rotY, String shrineType) { - prepareCallable("CALL `shrine_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?,?);"); - - - setInt(1, parentZoneID); - setInt(2, OwnerUUID); - setString(3, name); - setInt(4, meshUUID); - setFloat(5, location.x); - setFloat(6, location.y); - setFloat(7, location.z); - setFloat(8, meshScale); - setInt(9, currentHP); - setString(10, protectionState.name()); - setInt(11, currentGold); - setInt(12, rank); - - if (upgradeDate != null) { - setTimeStamp(13, upgradeDate.getMillis()); - } else { - setNULL(13, java.sql.Types.DATE); - } + ArrayList shrineList = new ArrayList<>(); - setInt(14, blueprintUUID); - setFloat(15, w); - setFloat(16, rotY); - setString(17, shrineType); - - ArrayList list = new ArrayList<>(); - //System.out.println(this.cs.get().toString()); - try { - boolean work = execute(); - if (work) { - ResultSet rs = this.callableStatement.get().getResultSet(); - while (rs.next()) { - addObject(list, rs); - } - rs.close(); - } else { - Logger.info("Shrine Creation Failed: " + this.callableStatement.get().toString()); - return list; //city creation failure - } - while (this.callableStatement.get().getMoreResults()) { - ResultSet rs = this.callableStatement.get().getResultSet(); - while (rs.next()) { - addObject(list, rs); - } - rs.close(); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("CALL `shrine_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?,?);")) { + + preparedStatement.setInt(1, parentZoneID); + preparedStatement.setInt(2, OwnerUUID); + preparedStatement.setString(3, name); + preparedStatement.setInt(4, meshUUID); + preparedStatement.setFloat(5, location.x); + preparedStatement.setFloat(6, location.y); + preparedStatement.setFloat(7, location.z); + preparedStatement.setFloat(8, meshScale); + preparedStatement.setInt(9, currentHP); + preparedStatement.setString(10, protectionState.name()); + preparedStatement.setInt(11, currentGold); + preparedStatement.setInt(12, rank); + + if (upgradeDate != null) + preparedStatement.setTimestamp(13, new java.sql.Timestamp(upgradeDate.getMillis())); + else + preparedStatement.setNull(13, java.sql.Types.DATE); + + preparedStatement.setInt(14, blueprintUUID); + preparedStatement.setFloat(15, w); + preparedStatement.setFloat(16, rotY); + preparedStatement.setString(17, shrineType); + + preparedStatement.execute(); + + ResultSet rs = preparedStatement.getResultSet(); + + while (rs.next()) + addObject(shrineList, rs); + + while (preparedStatement.getMoreResults()) { + rs = preparedStatement.getResultSet(); + + while (rs.next()) + addObject(shrineList, rs); } + } catch (SQLException e) { - Logger.info("Shrine Creation Failed, SQLException: " + this.callableStatement.get().toString() + e.toString()); - return list; //city creation failure - } catch (UnknownHostException e) { - Logger.info("Shrine Creation Failed, UnknownHostException: " + this.callableStatement.get().toString()); - return list; //city creation failure - } finally { - closeCallable(); + Logger.error(e); } - return list; + return shrineList; } public boolean updateFavors(Shrine shrine, int amount, int oldAmount) { - prepareCallable("UPDATE `obj_shrine` SET `shrine_favors`=? WHERE `UID` = ? AND `shrine_favors` = ?"); - setInt(1, amount); - setLong(2, (long) shrine.getObjectUUID()); - setInt(3, oldAmount); - return (executeUpdate() != 0); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_shrine` SET `shrine_favors`=? WHERE `UID` = ? AND `shrine_favors` = ?")) { + + preparedStatement.setInt(1, amount); + preparedStatement.setLong(2, shrine.getObjectUUID()); + preparedStatement.setInt(3, oldAmount); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } + } - public static void addObject(ArrayList list, ResultSet rs) throws SQLException, UnknownHostException { + public static void addObject(ArrayList list, ResultSet rs) throws SQLException { + String type = rs.getString("type"); + switch (type) { case "building": Building building = new Building(rs); @@ -123,25 +123,21 @@ public class dbShrineHandler extends dbHandlerBase { public void LOAD_ALL_SHRINES() { - Shrine thisShrine; + Shrine shrine; - prepareCallable("SELECT `obj_shrine`.*, `object`.`parent`, `object`.`type` FROM `object` LEFT JOIN `obj_shrine` ON `object`.`UID` = `obj_shrine`.`UID` WHERE `object`.`type` = 'shrine';"); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_shrine`.*, `object`.`parent`, `object`.`type` FROM `object` LEFT JOIN `obj_shrine` ON `object`.`UID` = `obj_shrine`.`UID` WHERE `object`.`type` = 'shrine';")) { - try { - ResultSet rs = executeQuery(); + ResultSet rs = preparedStatement.executeQuery(); - //shrines cached in rs for easy cache on creation. while (rs.next()) { - thisShrine = new Shrine(rs); - thisShrine.getShrineType().addShrineToServerList(thisShrine); + shrine = new Shrine(rs); + shrine.getShrineType().addShrineToServerList(shrine); } } catch (SQLException e) { - Logger.error( e.getErrorCode() + ' ' + e.getMessage(), e); - } finally { - closeCallable(); + Logger.error(e); } - } }