Actually make use of the checkConnected calls.

This commit is contained in:
GJ 2013-08-28 11:44:58 -04:00
parent 73d0b377ae
commit c98d298cf1

View File

@ -51,12 +51,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
private int reconnectAttempt = 0; private int reconnectAttempt = 0;
protected SQLDatabaseManager() { protected SQLDatabaseManager() {
checkConnected();
checkStructure(); checkStructure();
} }
public void purgePowerlessUsers() { public void purgePowerlessUsers() {
checkConnected(); if (!checkConnected()) {
return;
}
mcMMO.p.getLogger().info("Purging powerless users..."); mcMMO.p.getLogger().info("Purging powerless users...");
Collection<ArrayList<String>> usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0").values(); Collection<ArrayList<String>> usernames = read("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0").values();
@ -73,7 +75,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public void purgeOldUsers() { public void purgeOldUsers() {
checkConnected(); if (!checkConnected()) {
return;
}
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
mcMMO.p.getLogger().info("Purging old users..."); mcMMO.p.getLogger().info("Purging old users...");
@ -92,7 +97,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public boolean removeUser(String playerName) { public boolean removeUser(String playerName) {
checkConnected(); if (!checkConnected()) {
return false;
}
boolean success = update("DELETE FROM u, e, h, s, c " + boolean success = update("DELETE FROM u, e, h, s, c " +
"USING " + tablePrefix + "users u " + "USING " + tablePrefix + "users u " +
"JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
@ -107,7 +115,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public void saveUser(PlayerProfile profile) { public void saveUser(PlayerProfile profile) {
checkConnected(); if (!checkConnected()) {
return;
}
int userId = readId(profile.getPlayerName()); int userId = readId(profile.getPlayerName());
if (userId == -1) { if (userId == -1) {
newUser(profile.getPlayerName()); newUser(profile.getPlayerName());
@ -303,7 +314,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public void newUser(String playerName) { public void newUser(String playerName) {
checkConnected(); if (!checkConnected()) {
return;
}
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
@ -331,7 +345,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public PlayerProfile loadPlayerProfile(String playerName, boolean create) { public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
checkConnected(); if (!checkConnected()) {
return loadPlayerProfile(playerName, false); //Retry if not connected
}
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
@ -396,7 +413,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public void convertUsers(DatabaseManager destination) { public void convertUsers(DatabaseManager destination) {
checkConnected(); if (!checkConnected()) {
return;
}
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
@ -532,8 +552,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public List<String> getStoredUsers() { public List<String> getStoredUsers() {
checkConnected();
ArrayList<String> users = new ArrayList<String>(); ArrayList<String> users = new ArrayList<String>();
if (checkConnected()) {
Statement stmt = null; Statement stmt = null;
try { try {
stmt = connection.createStatement(); stmt = connection.createStatement();
@ -555,6 +576,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
} }
}
return users; return users;
} }
@ -599,6 +622,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
* Checks that the database structure is present and correct * Checks that the database structure is present and correct
*/ */
private void checkStructure() { private void checkStructure() {
if (!checkConnected()) {
return;
}
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` (" write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("
+ "`id` int(10) unsigned NOT NULL AUTO_INCREMENT," + "`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
+ "`user` varchar(40) NOT NULL," + "`user` varchar(40) NOT NULL,"