Closes issue #32.

This commit is contained in:
Joe0 2011-12-18 20:49:22 -06:00
parent 46d13c60cf
commit 0145a249a8

View File

@ -3,6 +3,7 @@ package org.moparscape.msc.ls.persistence.impl;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -44,6 +45,7 @@ class MySQL implements StorageMedium {
statement.addBatch(); statement.addBatch();
} }
statement.executeBatch(); statement.executeBatch();
close(statement);
} }
updateLongs(Statements.save_DeleteInv, s.getUser()); updateLongs(Statements.save_DeleteInv, s.getUser());
@ -60,6 +62,7 @@ class MySQL implements StorageMedium {
statement.addBatch(); statement.addBatch();
} }
statement.executeBatch(); statement.executeBatch();
close(statement);
} }
updateLongs(Statements.save_DeleteQuests, s.getUser()); updateLongs(Statements.save_DeleteQuests, s.getUser());
@ -73,6 +76,7 @@ class MySQL implements StorageMedium {
statement.addBatch(); statement.addBatch();
} }
statement.executeBatch(); statement.executeBatch();
close(statement);
statement = conn.prepareStatement(Statements.save_UpdateBasicInfo); statement = conn.prepareStatement(Statements.save_UpdateBasicInfo);
statement.setInt(1, s.getCombat()); statement.setInt(1, s.getCombat());
@ -92,6 +96,7 @@ class MySQL implements StorageMedium {
statement.setInt(15, s.getQuestPoints()); statement.setInt(15, s.getQuestPoints());
statement.setLong(16, s.getUser()); statement.setLong(16, s.getUser());
statement.executeUpdate(); statement.executeUpdate();
close(statement);
String query = "UPDATE `" + Statements.PREFIX + "experience` SET "; String query = "UPDATE `" + Statements.PREFIX + "experience` SET ";
for (int i = 0; i < 18; i++) for (int i = 0; i < 18; i++)
@ -108,7 +113,8 @@ class MySQL implements StorageMedium {
conn.updateQuery(query.substring(0, query.length() - 1) conn.updateQuery(query.substring(0, query.length() - 1)
+ " WHERE `user`=" + s.getUser()); + " WHERE `user`=" + s.getUser());
updateLongs(Statements.save_SetEventCD, s.getEventCD() / 1000, s.getUser()); updateLongs(Statements.save_SetEventCD, s.getEventCD() / 1000,
s.getUser());
return true; return true;
} catch (Exception e) { } catch (Exception e) {
@ -143,6 +149,7 @@ class MySQL implements StorageMedium {
logTrade.setInt(8, type); logTrade.setInt(8, type);
logTrade.executeUpdate(); logTrade.executeUpdate();
close(logTrade);
} catch (SQLException e) { } catch (SQLException e) {
if (logTrade != null) if (logTrade != null)
e.printStackTrace(); e.printStackTrace();
@ -177,6 +184,7 @@ class MySQL implements StorageMedium {
+ Statements.logReport); + Statements.logReport);
} }
close(logReport);
} }
@Override @Override
@ -199,6 +207,7 @@ class MySQL implements StorageMedium {
+ Statements.logKill); + Statements.logKill);
} }
close(logKill);
} }
@Override @Override
@ -218,6 +227,7 @@ class MySQL implements StorageMedium {
+ Statements.resetOnlineFlag); + Statements.resetOnlineFlag);
} }
close(resetOnlineFlag);
} }
@Override @Override
@ -311,38 +321,47 @@ class MySQL implements StorageMedium {
@Override @Override
public boolean isBanned(long user) { public boolean isBanned(long user) {
ResultSet res = null;
try { try {
ResultSet res = resultSetFromLongs(Statements.basicInfo, user); res = resultSetFromLongs(Statements.basicInfo, user);
res.next(); res.next();
return res.getInt("banned") == 1; return res.getInt("banned") == 1;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return true; return true;
} finally {
close(res);
} }
} }
@Override @Override
public int getGroupID(long user) { public int getGroupID(long user) {
ResultSet res = null;
try { try {
ResultSet res = resultSetFromLongs(Statements.basicInfo, user); res = resultSetFromLongs(Statements.basicInfo, user);
res.next(); res.next();
return res.getInt("group_id"); return res.getInt("group_id");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
// Normal user = 1 // Normal user = 1
return 1; return 1;
} finally {
close(res);
} }
} }
@Override @Override
public long getOwner(long user) { public long getOwner(long user) {
ResultSet res = null;
try { try {
ResultSet res = resultSetFromLongs(Statements.basicInfo, user); res = resultSetFromLongs(Statements.basicInfo, user);
res.next(); res.next();
return res.getLong("owner"); return res.getLong("owner");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return 0L; return 0L;
} finally {
close(res);
} }
} }
@ -425,17 +444,24 @@ class MySQL implements StorageMedium {
Config.statArray, user)); Config.statArray, user));
save.setCurStats(intArrayFromStringArray(Statements.playerCurExp, save.setCurStats(intArrayFromStringArray(Statements.playerCurExp,
"cur_", Config.statArray, user)); "cur_", Config.statArray, user));
close(result);
result = resultSetFromLongs(Statements.playerInvItems, user); result = resultSetFromLongs(Statements.playerInvItems, user);
while (result.next()) { while (result.next()) {
save.addInvItem(result.getInt("id"), result.getInt("amount"), save.addInvItem(result.getInt("id"), result.getInt("amount"),
result.getInt("wielded") == 1); result.getInt("wielded") == 1);
} }
close(result);
result = resultSetFromLongs(Statements.playerBankItems, user); result = resultSetFromLongs(Statements.playerBankItems, user);
while (result.next()) { while (result.next()) {
save.addBankItem(result.getInt("id"), result.getInt("amount")); save.addBankItem(result.getInt("id"), result.getInt("amount"));
} }
close(result);
save.addFriends(longListFromResultSet( save.addFriends(longListFromResultSet(
resultSetFromLongs(Statements.playerFriends, user), resultSetFromLongs(Statements.playerFriends, user),
"friend")); "friend"));
@ -447,6 +473,7 @@ class MySQL implements StorageMedium {
while (result.next()) { while (result.next()) {
save.setQuestStage(result.getInt("id"), result.getInt("stage")); save.setQuestStage(result.getInt("id"), result.getInt("stage"));
} }
close(result);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -470,6 +497,7 @@ class MySQL implements StorageMedium {
System.out.println("Failed to create prepared statement: " System.out.println("Failed to create prepared statement: "
+ statement); + statement);
} }
close(prepared);
} }
private void updateIntsLongs(String statement, int[] intA, long[] longA) { private void updateIntsLongs(String statement, int[] intA, long[] longA) {
@ -493,6 +521,7 @@ class MySQL implements StorageMedium {
System.out.println("Failed to create prepared statement: " System.out.println("Failed to create prepared statement: "
+ statement); + statement);
} }
close(prepared);
} }
private int[] intArrayFromStringArray(String statement, String prefix, private int[] intArrayFromStringArray(String statement, String prefix,
@ -516,6 +545,7 @@ class MySQL implements StorageMedium {
return null; return null;
} }
} }
close(result);
return data; return data;
} }
@ -526,7 +556,7 @@ class MySQL implements StorageMedium {
while (result.next()) { while (result.next()) {
list.add(result.getLong(param)); list.add(result.getLong(param));
} }
close(result);
return list; return list;
} }
@ -548,7 +578,6 @@ class MySQL implements StorageMedium {
System.out.println("Failed to create prepared statement: " System.out.println("Failed to create prepared statement: "
+ statement); + statement);
} }
return result; return result;
} }
@ -575,6 +604,32 @@ class MySQL implements StorageMedium {
return result.next(); return result.next();
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} finally {
close(result);
}
}
private void close(ResultSet res) {
if(res == null) {
return;
}
try {
close(res.getStatement());
} catch (SQLException e) {
e.printStackTrace();
}
}
private void close(Statement s) {
if(s == null) {
return;
}
try {
s.close();
} catch (SQLException e) {
e.printStackTrace();
} }
} }
@ -692,6 +747,7 @@ class MySQL implements StorageMedium {
+ Statements.logLogin); + Statements.logLogin);
} }
close(logLogin);
} }
@Override @Override
@ -713,6 +769,7 @@ class MySQL implements StorageMedium {
+ Statements.logIn); + Statements.logIn);
} }
close(login);
} }
@Override @Override
@ -725,9 +782,10 @@ class MySQL implements StorageMedium {
return result.getString("pass"); return result.getString("pass");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
close(result);
} }
return null; return null;
} }
} }