This commit is contained in:
t00thpick1 2014-08-08 00:42:33 -04:00
parent 6ee2d7d823
commit 5a47f9fa86

View File

@ -900,14 +900,22 @@ public final class SQLDatabaseManager implements DatabaseManager {
*/ */
private void checkStructure() { private void checkStructure() {
Statement statement = null; PreparedStatement statement = null;
Statement createStatement = null;
ResultSet resultSet = null;
Connection connection = null; Connection connection = null;
try { try {
connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT); connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
statement = connection.createStatement(); statement = connection.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES"
+ " WHERE table_schema = ?"
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` (" + " AND table_name = ?");
statement.setString(1, Config.getInstance().getMySQLDatabaseName());
statement.setString(2, tablePrefix + "users");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
createStatement = connection.createStatement();
createStatement.executeUpdate("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,"
+ "`uuid` varchar(36) NULL DEFAULT NULL," + "`uuid` varchar(36) NULL DEFAULT NULL,"
@ -915,76 +923,121 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "PRIMARY KEY (`id`)," + "PRIMARY KEY (`id`),"
+ "UNIQUE KEY `user` (`user`)," + "UNIQUE KEY `user` (`user`),"
+ "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"); + "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` (" createStatement.close();
+ "`user_id` int(10) unsigned NOT NULL," }
+ "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "'," resultSet.close();
+ "PRIMARY KEY (`user_id`)) " statement.setString(1, Config.getInstance().getMySQLDatabaseName());
+ "DEFAULT CHARSET=latin1;"); statement.setString(2, tablePrefix + "huds");
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "cooldowns` (" resultSet = statement.executeQuery();
+ "`user_id` int(10) unsigned NOT NULL," if (!resultSet.next()) {
+ "`taming` int(32) unsigned NOT NULL DEFAULT '0'," createStatement = connection.createStatement();
+ "`mining` int(32) unsigned NOT NULL DEFAULT '0'," createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` ("
+ "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0'," + "`user_id` int(10) unsigned NOT NULL,"
+ "`repair` int(32) unsigned NOT NULL DEFAULT '0'," + "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "',"
+ "`unarmed` int(32) unsigned NOT NULL DEFAULT '0'," + "PRIMARY KEY (`user_id`)) "
+ "`herbalism` int(32) unsigned NOT NULL DEFAULT '0'," + "DEFAULT CHARSET=latin1;");
+ "`excavation` int(32) unsigned NOT NULL DEFAULT '0'," createStatement.close();
+ "`archery` int(32) unsigned NOT NULL DEFAULT '0'," }
+ "`swords` int(32) unsigned NOT NULL DEFAULT '0'," resultSet.close();
+ "`axes` int(32) unsigned NOT NULL DEFAULT '0'," statement.setString(1, Config.getInstance().getMySQLDatabaseName());
+ "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0'," statement.setString(2, tablePrefix + "cooldowns");
+ "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0'," resultSet = statement.executeQuery();
+ "PRIMARY KEY (`user_id`)) " if (!resultSet.next()) {
+ "DEFAULT CHARSET=latin1;"); createStatement = connection.createStatement();
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` (" createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "cooldowns` ("
+ "`user_id` int(10) unsigned NOT NULL," + "`user_id` int(10) unsigned NOT NULL,"
+ "`taming` int(10) unsigned NOT NULL DEFAULT '0'," + "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`mining` int(10) unsigned NOT NULL DEFAULT '0'," + "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," + "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`repair` int(10) unsigned NOT NULL DEFAULT '0'," + "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," + "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," + "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0'," + "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`archery` int(10) unsigned NOT NULL DEFAULT '0'," + "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`swords` int(10) unsigned NOT NULL DEFAULT '0'," + "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`axes` int(10) unsigned NOT NULL DEFAULT '0'," + "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," + "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`fishing` int(10) unsigned NOT NULL DEFAULT '0'," + "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0',"
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT '0'," + "PRIMARY KEY (`user_id`)) "
+ "PRIMARY KEY (`user_id`)) " + "DEFAULT CHARSET=latin1;");
+ "DEFAULT CHARSET=latin1;"); createStatement.close();
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "experience` (" }
+ "`user_id` int(10) unsigned NOT NULL," resultSet.close();
+ "`taming` int(10) unsigned NOT NULL DEFAULT '0'," statement.setString(1, Config.getInstance().getMySQLDatabaseName());
+ "`mining` int(10) unsigned NOT NULL DEFAULT '0'," statement.setString(2, tablePrefix + "skills");
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0'," resultSet = statement.executeQuery();
+ "`repair` int(10) unsigned NOT NULL DEFAULT '0'," if (!resultSet.next()) {
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0'," createStatement = connection.createStatement();
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0'," createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` ("
+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0'," + "`user_id` int(10) unsigned NOT NULL,"
+ "`archery` int(10) unsigned NOT NULL DEFAULT '0'," + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`swords` int(10) unsigned NOT NULL DEFAULT '0'," + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`axes` int(10) unsigned NOT NULL DEFAULT '0'," + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0'," + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`fishing` int(10) unsigned NOT NULL DEFAULT '0'," + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT '0'," + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
+ "PRIMARY KEY (`user_id`)) " + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
+ "DEFAULT CHARSET=latin1;"); + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT '0',"
+ "PRIMARY KEY (`user_id`)) "
+ "DEFAULT CHARSET=latin1;");
createStatement.close();
}
resultSet.close();
statement.setString(1, Config.getInstance().getMySQLDatabaseName());
statement.setString(2, tablePrefix + "experience");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
createStatement = connection.createStatement();
createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "experience` ("
+ "`user_id` int(10) unsigned NOT NULL,"
+ "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT '0',"
+ "PRIMARY KEY (`user_id`)) "
+ "DEFAULT CHARSET=latin1;");
createStatement.close();
}
resultSet.close();
statement.close();
for (UpgradeType updateType : UpgradeType.values()) { for (UpgradeType updateType : UpgradeType.values()) {
checkDatabaseStructure(connection, updateType); checkDatabaseStructure(connection, updateType);
} }
mcMMO.p.getLogger().info("Killing orphans"); mcMMO.p.getLogger().info("Killing orphans");
statement.executeUpdate("DELETE FROM `" + tablePrefix + "experience` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "experience`.`user_id` = `u`.`id`)"); createStatement = connection.createStatement();
statement.executeUpdate("DELETE FROM `" + tablePrefix + "huds` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "huds`.`user_id` = `u`.`id`)"); createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "experience` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "experience`.`user_id` = `u`.`id`)");
statement.executeUpdate("DELETE FROM `" + tablePrefix + "cooldowns` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "cooldowns`.`user_id` = `u`.`id`)"); createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "huds` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "huds`.`user_id` = `u`.`id`)");
statement.executeUpdate("DELETE FROM `" + tablePrefix + "skills` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "skills`.`user_id` = `u`.`id`)"); createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "cooldowns` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "cooldowns`.`user_id` = `u`.`id`)");
createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "skills` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "skills`.`user_id` = `u`.`id`)");
} }
catch (SQLException ex) { catch (SQLException ex) {
printErrors(ex); printErrors(ex);
} }
finally { finally {
if (resultSet != null) {
try {
resultSet.close();
}
catch (SQLException e) {
// Ignore
}
}
if (statement != null) { if (statement != null) {
try { try {
statement.close(); statement.close();
@ -993,6 +1046,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
// Ignore // Ignore
} }
} }
if (createStatement != null) {
try {
createStatement.close();
}
catch (SQLException e) {
// Ignore
}
}
if (connection != null) { if (connection != null) {
try { try {
connection.close(); connection.close();