refactor: use try-with-resources for buddy list db operations
This commit is contained in:
@@ -141,26 +141,24 @@ public class BuddyList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadFromDb(int characterId) {
|
public void loadFromDb(int characterId) {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (PreparedStatement ps = con.prepareStatement("SELECT b.buddyid, b.pending, b.group, c.name as buddyname FROM buddies as b, characters as c WHERE c.id = b.buddyid AND b.characterid = ?")) {
|
||||||
|
ps.setInt(1, characterId);
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT b.buddyid, b.pending, b.group, c.name as buddyname FROM buddies as b, characters as c WHERE c.id = b.buddyid AND b.characterid = ?");
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
ps.setInt(1, characterId);
|
while (rs.next()) {
|
||||||
ResultSet rs = ps.executeQuery();
|
if (rs.getInt("pending") == 1) {
|
||||||
while (rs.next()) {
|
pendingRequests.push(new CharacterNameAndId(rs.getInt("buddyid"), rs.getString("buddyname")));
|
||||||
if (rs.getInt("pending") == 1) {
|
} else {
|
||||||
pendingRequests.push(new CharacterNameAndId(rs.getInt("buddyid"), rs.getString("buddyname")));
|
put(new BuddylistEntry(rs.getString("buddyname"), rs.getString("group"), rs.getInt("buddyid"), (byte) -1, true));
|
||||||
} else {
|
}
|
||||||
put(new BuddylistEntry(rs.getString("buddyname"), rs.getString("group"), rs.getInt("buddyid"), (byte) -1, true));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rs.close();
|
|
||||||
ps.close();
|
try (PreparedStatement ps = con.prepareStatement("DELETE FROM buddies WHERE pending = 1 AND characterid = ?")) {
|
||||||
ps = con.prepareStatement("DELETE FROM buddies WHERE pending = 1 AND characterid = ?");
|
ps.setInt(1, characterId);
|
||||||
ps.setInt(1, characterId);
|
ps.executeUpdate();
|
||||||
ps.executeUpdate();
|
}
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,18 +61,19 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CharacterIdNameBuddyCapacity getCharacterIdAndNameFromDatabase(String name) throws SQLException {
|
private CharacterIdNameBuddyCapacity getCharacterIdAndNameFromDatabase(String name) throws SQLException {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
CharacterIdNameBuddyCapacity ret = null;
|
||||||
CharacterIdNameBuddyCapacity ret;
|
|
||||||
try (PreparedStatement ps = con.prepareStatement("SELECT id, name, buddyCapacity FROM characters WHERE name LIKE ?")) {
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement ps = con.prepareStatement("SELECT id, name, buddyCapacity FROM characters WHERE name LIKE ?")) {
|
||||||
ps.setString(1, name);
|
ps.setString(1, name);
|
||||||
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
ret = null;
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
ret = new CharacterIdNameBuddyCapacity(rs.getInt("id"), rs.getString("name"), rs.getInt("buddyCapacity"));
|
ret = new CharacterIdNameBuddyCapacity(rs.getInt("id"), rs.getString("name"), rs.getInt("buddyCapacity"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
con.close();
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,27 +111,30 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
|||||||
if (channel != -1) {
|
if (channel != -1) {
|
||||||
buddyAddResult = world.requestBuddyAdd(addName, c.getChannel(), player.getId(), player.getName());
|
buddyAddResult = world.requestBuddyAdd(addName, c.getChannel(), player.getId(), player.getName());
|
||||||
} else {
|
} else {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as buddyCount FROM buddies WHERE characterid = ? AND pending = 0");
|
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as buddyCount FROM buddies WHERE characterid = ? AND pending = 0")) {
|
||||||
ps.setInt(1, charWithId.getId());
|
ps.setInt(1, charWithId.getId());
|
||||||
ResultSet rs = ps.executeQuery();
|
|
||||||
if (!rs.next()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
throw new RuntimeException("Result set expected");
|
if (!rs.next()) {
|
||||||
} else if (rs.getInt("buddyCount") >= charWithId.getBuddyCapacity()) {
|
throw new RuntimeException("Result set expected");
|
||||||
buddyAddResult = BuddyAddResult.BUDDYLIST_FULL;
|
} else if (rs.getInt("buddyCount") >= charWithId.getBuddyCapacity()) {
|
||||||
|
buddyAddResult = BuddyAddResult.BUDDYLIST_FULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (PreparedStatement ps = con.prepareStatement("SELECT pending FROM buddies WHERE characterid = ? AND buddyid = ?")) {
|
||||||
|
ps.setInt(1, charWithId.getId());
|
||||||
|
ps.setInt(2, player.getId());
|
||||||
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
buddyAddResult = BuddyAddResult.ALREADY_ON_LIST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
ps = con.prepareStatement("SELECT pending FROM buddies WHERE characterid = ? AND buddyid = ?");
|
|
||||||
ps.setInt(1, charWithId.getId());
|
|
||||||
ps.setInt(2, player.getId());
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
if (rs.next()) {
|
|
||||||
buddyAddResult = BuddyAddResult.ALREADY_ON_LIST;
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
}
|
}
|
||||||
if (buddyAddResult == BuddyAddResult.BUDDYLIST_FULL) {
|
if (buddyAddResult == BuddyAddResult.BUDDYLIST_FULL) {
|
||||||
c.announce(MaplePacketCreator.serverNotice(1, "\"" + addName + "\"'s Buddylist is full"));
|
c.announce(MaplePacketCreator.serverNotice(1, "\"" + addName + "\"'s Buddylist is full"));
|
||||||
@@ -142,13 +146,12 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
|||||||
displayChannel = channel;
|
displayChannel = channel;
|
||||||
notifyRemoteChannel(c, channel, otherCid, ADDED);
|
notifyRemoteChannel(c, channel, otherCid, ADDED);
|
||||||
} else if (buddyAddResult != BuddyAddResult.ALREADY_ON_LIST && channel == -1) {
|
} else if (buddyAddResult != BuddyAddResult.ALREADY_ON_LIST && channel == -1) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`) VALUES (?, ?, 1)")) {
|
PreparedStatement ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`) VALUES (?, ?, 1)")) {
|
||||||
ps.setInt(1, charWithId.getId());
|
ps.setInt(1, charWithId.getId());
|
||||||
ps.setInt(2, player.getId());
|
ps.setInt(2, player.getId());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
con.close();
|
|
||||||
}
|
}
|
||||||
buddylist.put(new BuddylistEntry(charWithId.getName(), group, otherCid, displayChannel, true));
|
buddylist.put(new BuddylistEntry(charWithId.getName(), group, otherCid, displayChannel, true));
|
||||||
c.announce(MaplePacketCreator.updateBuddylist(buddylist.getBuddies()));
|
c.announce(MaplePacketCreator.updateBuddylist(buddylist.getBuddies()));
|
||||||
@@ -171,16 +174,16 @@ public class BuddylistModifyHandler extends AbstractMaplePacketHandler {
|
|||||||
String otherName = null;
|
String otherName = null;
|
||||||
MapleCharacter otherChar = c.getChannelServer().getPlayerStorage().getCharacterById(otherCid);
|
MapleCharacter otherChar = c.getChannelServer().getPlayerStorage().getCharacterById(otherCid);
|
||||||
if (otherChar == null) {
|
if (otherChar == null) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
try (PreparedStatement ps = con.prepareStatement("SELECT name FROM characters WHERE id = ?")) {
|
PreparedStatement ps = con.prepareStatement("SELECT name FROM characters WHERE id = ?")) {
|
||||||
ps.setInt(1, otherCid);
|
ps.setInt(1, otherCid);
|
||||||
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
otherName = rs.getString("name");
|
otherName = rs.getString("name");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
con.close();
|
|
||||||
} else {
|
} else {
|
||||||
otherName = otherChar.getName();
|
otherName = otherChar.getName();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user