Merge pull request #520 from t00thpick1/patch-2

SQL stuff
This commit is contained in:
Glitchfinder 2013-01-15 19:35:06 -08:00
commit 46a5245a1d
2 changed files with 51 additions and 20 deletions

View File

@ -51,15 +51,12 @@ public class McremoveCommand implements CommandExecutor {
/* MySQL */ /* MySQL */
if (Config.getInstance().getUseMySQL()) { if (Config.getInstance().getUseMySQL()) {
Database database = mcMMO.getPlayerDatabase(); Database database = mcMMO.getPlayerDatabase();
int userId = 0; int affected = 0;
userId = database.getInt("SELECT id FROM " + tablePrefix + "users WHERE user = '" + playerName + "'"); affected = database.update("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.user = '" + playerName + "'");
if (userId > 0) { if (affected > 0) {
database.write("DELETE FROM " + databaseName + "." + tablePrefix + "users WHERE " + tablePrefix + "users.id IN " + userId);
sender.sendMessage(success); sender.sendMessage(success);
} else {
}
else {
sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
} }
} }

View File

@ -243,6 +243,7 @@ public class Database {
try { try {
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
statement.executeUpdate(); statement.executeUpdate();
statement.close();
return true; return true;
} }
catch (SQLException ex) { catch (SQLException ex) {
@ -263,6 +264,39 @@ public class Database {
return false; return false;
} }
/**
* Returns the number of rows affected by either a DELETE or UPDATE query
*
* @param sql SQL query to execute
* @return the number of rows affected
*/
public int update(String sql) {
int ret = 0;
if (checkConnected()) {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
ret = statement.executeUpdate();
statement.close();
return ret;
} catch (SQLException ex) {
printErrors(ex);
return 0;
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
printErrors(e);
return 0;
}
}
}
}
return ret;
}
/** /**
* Get the Integer. Only return first row / first field. * Get the Integer. Only return first row / first field.
* *
@ -425,19 +459,19 @@ public class Database {
if (checkConnected()) { if (checkConnected()) {
try { try {
String sql = "SELECT " String sql = "SELECT "
+ "(SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'ALL'" + "(SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'ALL'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.fishing desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'FISHING'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.fishing desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'FISHING'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.taming desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'TAMING'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.taming desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'TAMING'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.woodcutting desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'WOODCUTTING'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.woodcutting desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'WOODCUTTING'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.repair desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'REPAIR'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.repair desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'REPAIR'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.unarmed desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'UNARMED'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.unarmed desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'UNARMED'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.herbalism desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'HERBALISM'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.herbalism desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'HERBALISM'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.excavation desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'EXCAVATION'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.excavation desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'EXCAVATION'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.archery desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'ARCHERY'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.archery desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'ARCHERY'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.swords desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'SWORDS'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.swords desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'SWORDS'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.axes desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'AXES'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.axes desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'AXES'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.acrobatics desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'ACROBATICS'" + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.acrobatics desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'ACROBATICS'"
+ ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.mining desc ) AS p) AS d) WHERE user = '" + playerName + "') AS 'MINING'"; + ", (SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.user AS user FROM (SELECT @rownum:=0) AS rank, ((SELECT u.user AS user FROM " + tablePrefix + "users u, " + tablePrefix + "skills s WHERE u.id = s.user_id ORDER BY s.mining desc) AS p)) AS d) WHERE user = '" + playerName + "') AS 'MINING'";
PreparedStatement statement = connection.prepareStatement(sql); PreparedStatement statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {