refactor: use try-with-resources for BBS db operations
This commit is contained in:
@@ -23,15 +23,16 @@ package net.server.channel.handlers;
|
|||||||
|
|
||||||
import client.MapleCharacter;
|
import client.MapleCharacter;
|
||||||
import client.MapleClient;
|
import client.MapleClient;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import net.AbstractMaplePacketHandler;
|
import net.AbstractMaplePacketHandler;
|
||||||
import tools.DatabaseConnection;
|
import tools.DatabaseConnection;
|
||||||
import tools.MaplePacketCreator;
|
import tools.MaplePacketCreator;
|
||||||
import tools.data.input.SeekableLittleEndianAccessor;
|
import tools.data.input.SeekableLittleEndianAccessor;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
||||||
|
|
||||||
private String correctLength(String in, int maxSize) {
|
private String correctLength(String in, int maxSize) {
|
||||||
@@ -96,17 +97,14 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void listBBSThreads(MapleClient c, int start) {
|
private static void listBBSThreads(MapleClient c, int start) {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
Connection con = DatabaseConnection.getConnection();
|
PreparedStatement ps = con.prepareStatement("SELECT * FROM bbs_threads WHERE guildid = ? ORDER BY localthreadid DESC",
|
||||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM bbs_threads WHERE guildid = ? ORDER BY localthreadid DESC",
|
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
|
||||||
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
|
|
||||||
ps.setInt(1, c.getPlayer().getGuildId());
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
|
||||||
c.announce(MaplePacketCreator.BBSThreadList(rs, start));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
con.close();
|
ps.setInt(1, c.getPlayer().getGuildId());
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
c.announce(MaplePacketCreator.BBSThreadList(rs, start));
|
||||||
|
}
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -116,33 +114,34 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
if (c.getPlayer().getGuildId() <= 0) {
|
if (c.getPlayer().getGuildId() <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Connection con = null;
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
try {
|
final int threadid;
|
||||||
con = DatabaseConnection.getConnection();
|
try (PreparedStatement ps = con.prepareStatement("SELECT threadid FROM bbs_threads WHERE guildid = ? AND localthreadid = ?")) {
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT threadid FROM bbs_threads WHERE guildid = ? AND localthreadid = ?");
|
ps.setInt(1, c.getPlayer().getGuildId());
|
||||||
ps.setInt(1, c.getPlayer().getGuildId());
|
ps.setInt(2, localthreadid);
|
||||||
ps.setInt(2, localthreadid);
|
|
||||||
ResultSet threadRS = ps.executeQuery();
|
try (ResultSet threadRS = ps.executeQuery()) {
|
||||||
if (!threadRS.next()) {
|
if (!threadRS.next()) {
|
||||||
threadRS.close();
|
return;
|
||||||
ps.close();
|
}
|
||||||
return;
|
|
||||||
|
threadid = threadRS.getInt("threadid");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int threadid = threadRS.getInt("threadid");
|
|
||||||
threadRS.close();
|
try (PreparedStatement ps = con.prepareStatement("INSERT INTO bbs_replies " + "(`threadid`, `postercid`, `timestamp`, `content`) VALUES " + "(?, ?, ?, ?)")) {
|
||||||
ps.close();
|
ps.setInt(1, threadid);
|
||||||
ps = con.prepareStatement("INSERT INTO bbs_replies " + "(`threadid`, `postercid`, `timestamp`, `content`) VALUES " + "(?, ?, ?, ?)");
|
ps.setInt(2, c.getPlayer().getId());
|
||||||
ps.setInt(1, threadid);
|
ps.setLong(3, currentServerTime());
|
||||||
ps.setInt(2, c.getPlayer().getId());
|
ps.setString(4, text);
|
||||||
ps.setLong(3, currentServerTime());
|
ps.executeUpdate();
|
||||||
ps.setString(4, text);
|
}
|
||||||
ps.execute();
|
|
||||||
ps.close();
|
try (PreparedStatement ps = con.prepareStatement("UPDATE bbs_threads SET replycount = replycount + 1 WHERE threadid = ?")) {
|
||||||
ps = con.prepareStatement("UPDATE bbs_threads SET replycount = replycount + 1 WHERE threadid = ?");
|
ps.setInt(1, threadid);
|
||||||
ps.setInt(1, threadid);
|
ps.executeUpdate();
|
||||||
ps.execute();
|
}
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
displayThread(c, localthreadid);
|
displayThread(c, localthreadid);
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
@@ -154,20 +153,19 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
if (chr.getGuildId() < 1) {
|
if (chr.getGuildId() < 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
Connection con = DatabaseConnection.getConnection();
|
PreparedStatement ps = con.prepareStatement("UPDATE bbs_threads SET `name` = ?, `timestamp` = ?, " + "`icon` = ?, " + "`startpost` = ? WHERE guildid = ? AND localthreadid = ? AND (postercid = ? OR ?)")) {
|
||||||
try (PreparedStatement ps = con.prepareStatement("UPDATE bbs_threads SET `name` = ?, `timestamp` = ?, " + "`icon` = ?, " + "`startpost` = ? WHERE guildid = ? AND localthreadid = ? AND (postercid = ? OR ?)")) {
|
|
||||||
ps.setString(1, title);
|
ps.setString(1, title);
|
||||||
ps.setLong(2, currentServerTime());
|
ps.setLong(2, currentServerTime());
|
||||||
ps.setInt(3, icon);
|
ps.setInt(3, icon);
|
||||||
ps.setString(4, text);
|
ps.setString(4, text);
|
||||||
ps.setInt(5, chr.getGuildId());
|
ps.setInt(5, chr.getGuildId());
|
||||||
ps.setInt(6, localthreadid);
|
ps.setInt(6, localthreadid);
|
||||||
ps.setInt(7, chr.getId());
|
ps.setInt(7, chr.getId());
|
||||||
ps.setBoolean(8, chr.getGuildRank() < 3);
|
ps.setBoolean(8, chr.getGuildRank() < 3);
|
||||||
ps.execute();
|
ps.execute();
|
||||||
}
|
|
||||||
con.close();
|
|
||||||
displayThread(client, localthreadid);
|
displayThread(client, localthreadid);
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
@@ -180,29 +178,28 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int nextId = 0;
|
int nextId = 0;
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
Connection con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps;
|
|
||||||
if (!bNotice) {
|
if (!bNotice) {
|
||||||
ps = con.prepareStatement("SELECT MAX(localthreadid) AS lastLocalId FROM bbs_threads WHERE guildid = ?");
|
try (PreparedStatement ps = con.prepareStatement("SELECT MAX(localthreadid) AS lastLocalId FROM bbs_threads WHERE guildid = ?")) {
|
||||||
ps.setInt(1, chr.getGuildId());
|
ps.setInt(1, chr.getGuildId());
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
rs.next();
|
rs.next();
|
||||||
nextId = rs.getInt("lastLocalId") + 1;
|
nextId = rs.getInt("lastLocalId") + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ps.close();
|
|
||||||
}
|
}
|
||||||
ps = con.prepareStatement("INSERT INTO bbs_threads " + "(`postercid`, `name`, `timestamp`, `icon`, `startpost`, " + "`guildid`, `localthreadid`) " + "VALUES(?, ?, ?, ?, ?, ?, ?)");
|
|
||||||
ps.setInt(1, chr.getId());
|
try (PreparedStatement ps = con.prepareStatement("INSERT INTO bbs_threads (`postercid`, `name`, `timestamp`, `icon`, `startpost`, `guildid`, `localthreadid`) VALUES(?, ?, ?, ?, ?, ?, ?)")) {
|
||||||
ps.setString(2, title);
|
ps.setInt(1, chr.getId());
|
||||||
ps.setLong(3, currentServerTime());
|
ps.setString(2, title);
|
||||||
ps.setInt(4, icon);
|
ps.setLong(3, currentServerTime());
|
||||||
ps.setString(5, text);
|
ps.setInt(4, icon);
|
||||||
ps.setInt(6, chr.getGuildId());
|
ps.setString(5, text);
|
||||||
ps.setInt(7, nextId);
|
ps.setInt(6, chr.getGuildId());
|
||||||
ps.execute();
|
ps.setInt(7, nextId);
|
||||||
ps.close();
|
ps.executeUpdate();
|
||||||
con.close();
|
}
|
||||||
|
|
||||||
displayThread(client, nextId);
|
displayThread(client, nextId);
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
@@ -215,35 +212,36 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
if (mc.getGuildId() <= 0) {
|
if (mc.getGuildId() <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Connection con = null;
|
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT threadid, postercid FROM bbs_threads WHERE guildid = ? AND localthreadid = ?");
|
final int threadid;
|
||||||
ps.setInt(1, mc.getGuildId());
|
try (PreparedStatement ps = con.prepareStatement("SELECT threadid, postercid FROM bbs_threads WHERE guildid = ? AND localthreadid = ?")) {
|
||||||
ps.setInt(2, localthreadid);
|
ps.setInt(1, mc.getGuildId());
|
||||||
ResultSet threadRS = ps.executeQuery();
|
ps.setInt(2, localthreadid);
|
||||||
if (!threadRS.next()) {
|
|
||||||
threadRS.close();
|
try (ResultSet threadRS = ps.executeQuery()) {
|
||||||
ps.close();
|
if (!threadRS.next()) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mc.getId() != threadRS.getInt("postercid") && mc.getGuildRank() > 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
threadid = threadRS.getInt("threadid");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (mc.getId() != threadRS.getInt("postercid") && mc.getGuildRank() > 2) {
|
|
||||||
threadRS.close();
|
try (PreparedStatement ps = con.prepareStatement("DELETE FROM bbs_replies WHERE threadid = ?")) {
|
||||||
ps.close();
|
ps.setInt(1, threadid);
|
||||||
return;
|
ps.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (PreparedStatement ps = con.prepareStatement("DELETE FROM bbs_threads WHERE threadid = ?")) {
|
||||||
|
ps.setInt(1, threadid);
|
||||||
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
int threadid = threadRS.getInt("threadid");
|
|
||||||
ps.close();
|
|
||||||
ps = con.prepareStatement("DELETE FROM bbs_replies WHERE threadid = ?");
|
|
||||||
ps.setInt(1, threadid);
|
|
||||||
ps.execute();
|
|
||||||
ps.close();
|
|
||||||
ps = con.prepareStatement("DELETE FROM bbs_threads WHERE threadid = ?");
|
|
||||||
ps.setInt(1, threadid);
|
|
||||||
ps.execute();
|
|
||||||
threadRS.close();
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -254,35 +252,36 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
if (mc.getGuildId() <= 0) {
|
if (mc.getGuildId() <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int threadid;
|
|
||||||
Connection con = null;
|
final int threadid;
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT postercid, threadid FROM bbs_replies WHERE replyid = ?");
|
try (PreparedStatement ps = con.prepareStatement("SELECT postercid, threadid FROM bbs_replies WHERE replyid = ?")) {
|
||||||
ps.setInt(1, replyid);
|
ps.setInt(1, replyid);
|
||||||
ResultSet rs = ps.executeQuery();
|
|
||||||
if (!rs.next()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
rs.close();
|
if (!rs.next()) {
|
||||||
ps.close();
|
return;
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
if (mc.getId() != rs.getInt("postercid") && mc.getGuildRank() > 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
threadid = rs.getInt("threadid");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (mc.getId() != rs.getInt("postercid") && mc.getGuildRank() > 2) {
|
|
||||||
rs.close();
|
try (PreparedStatement ps = con.prepareStatement("DELETE FROM bbs_replies WHERE replyid = ?")) {
|
||||||
ps.close();
|
ps.setInt(1, replyid);
|
||||||
return;
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
threadid = rs.getInt("threadid");
|
|
||||||
rs.close();
|
try (PreparedStatement ps = con.prepareStatement("UPDATE bbs_threads SET replycount = replycount - 1 WHERE threadid = ?")) {
|
||||||
ps.close();
|
ps.setInt(1, threadid);
|
||||||
ps = con.prepareStatement("DELETE FROM bbs_replies WHERE replyid = ?");
|
ps.executeUpdate();
|
||||||
ps.setInt(1, replyid);
|
}
|
||||||
ps.execute();
|
|
||||||
ps.close();
|
|
||||||
ps = con.prepareStatement("UPDATE bbs_threads SET replycount = replycount - 1 WHERE threadid = ?");
|
|
||||||
ps.setInt(1, threadid);
|
|
||||||
ps.execute();
|
|
||||||
ps.close();
|
|
||||||
con.close();
|
|
||||||
displayThread(client, threadid, false);
|
displayThread(client, threadid, false);
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
@@ -298,9 +297,9 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
if (mc.getGuildId() <= 0) {
|
if (mc.getGuildId() <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Connection con;
|
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
con = DatabaseConnection.getConnection();
|
// TODO clean up this block and use try-with-resources
|
||||||
PreparedStatement ps2;
|
PreparedStatement ps2;
|
||||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM bbs_threads WHERE guildid = ? AND " + (bIsThreadIdLocal ? "local" : "") + "threadid = ?")) {
|
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM bbs_threads WHERE guildid = ? AND " + (bIsThreadIdLocal ? "local" : "") + "threadid = ?")) {
|
||||||
ps.setInt(1, mc.getGuildId());
|
ps.setInt(1, mc.getGuildId());
|
||||||
@@ -324,8 +323,6 @@ public final class BBSOperationHandler extends AbstractMaplePacketHandler {
|
|||||||
if (ps2 != null) {
|
if (ps2 != null) {
|
||||||
ps2.close();
|
ps2.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException se) {
|
} catch (SQLException se) {
|
||||||
se.printStackTrace();
|
se.printStackTrace();
|
||||||
} catch (RuntimeException re) {//btw we get this everytime for some reason, but replies work!
|
} catch (RuntimeException re) {//btw we get this everytime for some reason, but replies work!
|
||||||
|
|||||||
Reference in New Issue
Block a user