2
0

SQLDatabaseManager.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. package com.gmail.nossr50.database;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Properties;
  12. import com.gmail.nossr50.mcMMO;
  13. import com.gmail.nossr50.config.Config;
  14. import com.gmail.nossr50.datatypes.database.DatabaseUpdateType;
  15. import com.gmail.nossr50.datatypes.skills.SkillType;
  16. import com.gmail.nossr50.runnables.database.SQLReconnectTask;
  17. import com.gmail.nossr50.util.Misc;
  18. public final class SQLDatabaseManager {
  19. private static String connectionString;
  20. private static String tablePrefix = Config.getInstance().getMySQLTablePrefix();
  21. private static Connection connection = null;
  22. // Scale waiting time by this much per failed attempt
  23. private static final double SCALING_FACTOR = 40;
  24. // Minimum wait in nanoseconds (default 500ms)
  25. private static final long MIN_WAIT = 500L * 1000000L;
  26. // Maximum time to wait between reconnects (default 5 minutes)
  27. private static final long MAX_WAIT = 5L * 60L * 1000L * 1000000L;
  28. // How long to wait when checking if connection is valid (default 3 seconds)
  29. private static final int VALID_TIMEOUT = 3;
  30. // When next to try connecting to Database in nanoseconds
  31. private static long nextReconnectTimestamp = 0L;
  32. // How many connection attempts have failed
  33. private static int reconnectAttempt = 0;
  34. private static final long ONE_MONTH = 2630000000L;
  35. private SQLDatabaseManager() {}
  36. /**
  37. * Attempt to connect to the mySQL database.
  38. */
  39. public static void connect() {
  40. Config configInstance = Config.getInstance();
  41. connectionString = "jdbc:mysql://" + configInstance.getMySQLServerName() + ":" + configInstance.getMySQLServerPort() + "/" + configInstance.getMySQLDatabaseName();
  42. try {
  43. mcMMO.p.getLogger().info("Attempting connection to MySQL...");
  44. // Force driver to load if not yet loaded
  45. Class.forName("com.mysql.jdbc.Driver");
  46. Properties connectionProperties = new Properties();
  47. connectionProperties.put("user", configInstance.getMySQLUserName());
  48. connectionProperties.put("password", configInstance.getMySQLUserPassword());
  49. connectionProperties.put("autoReconnect", "false");
  50. connectionProperties.put("maxReconnects", "0");
  51. connection = DriverManager.getConnection(connectionString, connectionProperties);
  52. mcMMO.p.getLogger().info("Connection to MySQL was a success!");
  53. }
  54. catch (SQLException ex) {
  55. connection = null;
  56. if (reconnectAttempt == 0 || reconnectAttempt >= 11) {
  57. mcMMO.p.getLogger().info("Connection to MySQL failed!");
  58. }
  59. }
  60. catch (ClassNotFoundException ex) {
  61. connection = null;
  62. if (reconnectAttempt == 0 || reconnectAttempt >= 11) {
  63. mcMMO.p.getLogger().info("MySQL database driver not found!");
  64. }
  65. }
  66. }
  67. /**
  68. * Attempt to create the database structure.
  69. */
  70. public static void createStructure() {
  71. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("
  72. + "`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
  73. + "`user` varchar(40) NOT NULL,"
  74. + "`lastlogin` int(32) unsigned NOT NULL,"
  75. + "PRIMARY KEY (`id`),"
  76. + "UNIQUE KEY `user` (`user`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
  77. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` ("
  78. + "`user_id` int(10) unsigned NOT NULL,"
  79. + "`hudtype` varchar(50) NOT NULL DEFAULT 'STANDARD',"
  80. + "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "',"
  81. + "PRIMARY KEY (`user_id`)) "
  82. + "DEFAULT CHARSET=latin1;");
  83. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "cooldowns` ("
  84. + "`user_id` int(10) unsigned NOT NULL,"
  85. + "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
  86. + "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
  87. + "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
  88. + "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
  89. + "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
  90. + "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
  91. + "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
  92. + "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
  93. + "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
  94. + "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
  95. + "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
  96. + "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0',"
  97. + "PRIMARY KEY (`user_id`)) "
  98. + "DEFAULT CHARSET=latin1;");
  99. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` ("
  100. + "`user_id` int(10) unsigned NOT NULL,"
  101. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  102. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  103. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  104. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  105. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  106. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  107. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  108. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  109. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  110. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  111. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  112. + "PRIMARY KEY (`user_id`)) "
  113. + "DEFAULT CHARSET=latin1;");
  114. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "experience` ("
  115. + "`user_id` int(10) unsigned NOT NULL,"
  116. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  117. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  118. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  119. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  120. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  121. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  122. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  123. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  124. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  125. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  126. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  127. + "PRIMARY KEY (`user_id`)) "
  128. + "DEFAULT CHARSET=latin1;");
  129. checkDatabaseStructure(DatabaseUpdateType.FISHING);
  130. checkDatabaseStructure(DatabaseUpdateType.BLAST_MINING);
  131. checkDatabaseStructure(DatabaseUpdateType.INDEX);
  132. checkDatabaseStructure(DatabaseUpdateType.MOB_HEALTHBARS);
  133. checkDatabaseStructure(DatabaseUpdateType.PARTY_NAMES);
  134. checkDatabaseStructure(DatabaseUpdateType.USER_COLUMN);
  135. }
  136. /**
  137. * Attempt to write the SQL query.
  138. *
  139. * @param sql Query to write.
  140. * @return true if the query was successfully written, false otherwise.
  141. */
  142. public static boolean write(String sql) {
  143. if (!checkConnected()) {
  144. return false;
  145. }
  146. PreparedStatement statement = null;
  147. try {
  148. statement = connection.prepareStatement(sql);
  149. statement.executeUpdate();
  150. return true;
  151. }
  152. catch (SQLException ex) {
  153. if (!sql.equalsIgnoreCase("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party` ;")) {
  154. printErrors(ex);
  155. }
  156. return false;
  157. }
  158. finally {
  159. if (statement != null) {
  160. try {
  161. statement.close();
  162. }
  163. catch (SQLException e) {
  164. printErrors(e);
  165. }
  166. }
  167. }
  168. }
  169. public static boolean removeUserSQL(String playerName) {
  170. return SQLDatabaseManager.update("DELETE FROM u, e, h, s, c " +
  171. "USING " + tablePrefix + "users u " +
  172. "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
  173. "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
  174. "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
  175. "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
  176. "WHERE u.user = '" + playerName + "'") != 0;
  177. }
  178. /**
  179. * Returns the number of rows affected by either a DELETE or UPDATE query
  180. *
  181. * @param sql SQL query to execute
  182. * @return the number of rows affected
  183. */
  184. public static int update(String sql) {
  185. if (!checkConnected()) {
  186. return 0;
  187. }
  188. int rows = 0;
  189. PreparedStatement statement = null;
  190. try {
  191. statement = connection.prepareStatement(sql);
  192. rows = statement.executeUpdate();
  193. }
  194. catch (SQLException ex) {
  195. printErrors(ex);
  196. }
  197. finally {
  198. if (statement != null) {
  199. try {
  200. statement.close();
  201. }
  202. catch (SQLException e) {
  203. printErrors(e);
  204. }
  205. }
  206. }
  207. return rows;
  208. }
  209. /**
  210. * Get the Integer. Only return first row / first field.
  211. *
  212. * @param sql SQL query to execute
  213. * @return the value in the first row / first field
  214. */
  215. public static int getInt(String sql) {
  216. if (!checkConnected()) {
  217. return 0;
  218. }
  219. int result = 0;
  220. PreparedStatement statement = null;
  221. try {
  222. statement = connection.prepareStatement(sql);
  223. ResultSet resultSet = statement.executeQuery();
  224. if (resultSet.next()) {
  225. result = resultSet.getInt(1);
  226. }
  227. }
  228. catch (SQLException ex) {
  229. printErrors(ex);
  230. }
  231. finally {
  232. if (statement != null) {
  233. try {
  234. statement.close();
  235. }
  236. catch (SQLException e) {
  237. printErrors(e);
  238. }
  239. }
  240. }
  241. return result;
  242. }
  243. /**
  244. * Check connection status and re-establish if dead or stale.
  245. *
  246. * If the very first immediate attempt fails, further attempts
  247. * will be made in progressively larger intervals up to MAX_WAIT
  248. * intervals.
  249. *
  250. * This allows for MySQL to time out idle connections as needed by
  251. * server operator, without affecting McMMO, while still providing
  252. * protection against a database outage taking down Bukkit's tick
  253. * processing loop due to attemping a database connection each
  254. * time McMMO needs the database.
  255. *
  256. * @return the boolean value for whether or not we are connected
  257. */
  258. public static boolean checkConnected() {
  259. boolean isClosed = true;
  260. boolean isValid = false;
  261. boolean exists = (connection != null);
  262. // If we're waiting for server to recover then leave early
  263. if (nextReconnectTimestamp > 0 && nextReconnectTimestamp > System.nanoTime()) {
  264. return false;
  265. }
  266. if (exists) {
  267. try {
  268. isClosed = connection.isClosed();
  269. }
  270. catch (SQLException e) {
  271. isClosed = true;
  272. e.printStackTrace();
  273. printErrors(e);
  274. }
  275. if (!isClosed) {
  276. try {
  277. isValid = connection.isValid(VALID_TIMEOUT);
  278. }
  279. catch (SQLException e) {
  280. // Don't print stack trace because it's valid to lose idle connections to the server and have to restart them.
  281. isValid = false;
  282. }
  283. }
  284. }
  285. // Leave if all ok
  286. if (exists && !isClosed && isValid) {
  287. // Housekeeping
  288. nextReconnectTimestamp = 0;
  289. reconnectAttempt = 0;
  290. return true;
  291. }
  292. // Cleanup after ourselves for GC and MySQL's sake
  293. if (exists && !isClosed) {
  294. try {
  295. connection.close();
  296. }
  297. catch (SQLException ex) {
  298. // This is a housekeeping exercise, ignore errors
  299. }
  300. }
  301. // Try to connect again
  302. connect();
  303. // Leave if connection is good
  304. try {
  305. if (connection != null && !connection.isClosed()) {
  306. // Schedule a database save if we really had an outage
  307. if (reconnectAttempt > 1) {
  308. new SQLReconnectTask().runTaskLater(mcMMO.p, 5);
  309. }
  310. nextReconnectTimestamp = 0;
  311. reconnectAttempt = 0;
  312. return true;
  313. }
  314. }
  315. catch (SQLException e) {
  316. // Failed to check isClosed, so presume connection is bad and attempt later
  317. e.printStackTrace();
  318. printErrors(e);
  319. }
  320. reconnectAttempt++;
  321. nextReconnectTimestamp = (long)(System.nanoTime() + Math.min(MAX_WAIT, (reconnectAttempt * SCALING_FACTOR * MIN_WAIT)));
  322. return false;
  323. }
  324. /**
  325. * Read SQL query.
  326. *
  327. * @param sql SQL query to read
  328. * @return the rows in this SQL query
  329. */
  330. public static HashMap<Integer, ArrayList<String>> read(String sql) {
  331. ResultSet resultSet;
  332. HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
  333. if (checkConnected()) {
  334. PreparedStatement statement = null;
  335. try {
  336. statement = connection.prepareStatement(sql);
  337. resultSet = statement.executeQuery();
  338. while (resultSet.next()) {
  339. ArrayList<String> column = new ArrayList<String>();
  340. for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  341. column.add(resultSet.getString(i));
  342. }
  343. rows.put(resultSet.getRow(), column);
  344. }
  345. }
  346. catch (SQLException ex) {
  347. printErrors(ex);
  348. }
  349. finally {
  350. if (statement != null) {
  351. try {
  352. statement.close();
  353. }
  354. catch (SQLException e) {
  355. printErrors(e);
  356. }
  357. }
  358. }
  359. }
  360. return rows;
  361. }
  362. public static Map<String, Integer> readSQLRank(String playerName) {
  363. ResultSet resultSet;
  364. Map<String, Integer> skills = new HashMap<String, Integer>();
  365. if (checkConnected()) {
  366. try {
  367. for (SkillType skillType : SkillType.values()) {
  368. if (skillType.isChildSkill()) {
  369. continue;
  370. }
  371. String skillName = skillType.name().toLowerCase();
  372. String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
  373. "AND " + skillName + " > (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  374. "WHERE user = ?)";
  375. PreparedStatement statement = connection.prepareStatement(sql);
  376. statement.setString(1, playerName);
  377. resultSet = statement.executeQuery();
  378. resultSet.next();
  379. int rank = resultSet.getInt("rank");
  380. sql = "SELECT user, " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
  381. "AND " + skillName + " = (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  382. "WHERE user = '" + playerName + "') ORDER BY user";
  383. statement = connection.prepareStatement(sql);
  384. resultSet = statement.executeQuery();
  385. while (resultSet.next()) {
  386. if (resultSet.getString("user").equalsIgnoreCase(playerName)) {
  387. skills.put(skillType.name(), rank + resultSet.getRow());
  388. break;
  389. }
  390. }
  391. statement.close();
  392. }
  393. String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  394. "WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 " +
  395. "AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > " +
  396. "(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
  397. "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?)";
  398. PreparedStatement statement = connection.prepareStatement(sql);
  399. statement.setString(1, playerName);
  400. resultSet = statement.executeQuery();
  401. resultSet.next();
  402. int rank = resultSet.getInt("rank");
  403. sql = "SELECT user, taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
  404. "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  405. "WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 " +
  406. "AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing = " +
  407. "(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
  408. "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?) ORDER BY user";
  409. statement = connection.prepareStatement(sql);
  410. statement.setString(1, playerName);
  411. resultSet = statement.executeQuery();
  412. while (resultSet.next()) {
  413. if (resultSet.getString("user").equalsIgnoreCase(playerName)) {
  414. skills.put("ALL", rank + resultSet.getRow());
  415. break;
  416. }
  417. }
  418. statement.close();
  419. }
  420. catch (SQLException ex) {
  421. printErrors(ex);
  422. }
  423. }
  424. return skills;
  425. }
  426. public static int purgePowerlessSQL() {
  427. HashMap<Integer, 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");
  428. write("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
  429. "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
  430. "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
  431. "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
  432. "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
  433. "WHERE (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");
  434. return processPurge(usernames.values());
  435. }
  436. public static int purgeOldSQL() {
  437. long currentTime = System.currentTimeMillis();
  438. long purgeTime = ONE_MONTH * Config.getInstance().getOldUsersCutoff();
  439. HashMap<Integer, ArrayList<String>> usernames = read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")");
  440. write("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
  441. "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
  442. "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
  443. "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
  444. "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
  445. "WHERE ((" + currentTime + " - lastlogin*1000) > " + purgeTime + ")");
  446. return processPurge(usernames.values());
  447. }
  448. private static int processPurge(Collection<ArrayList<String>> usernames) {
  449. int purgedUsers = 0;
  450. for (ArrayList<String> user : usernames) {
  451. Misc.profileCleanup(user.get(0));
  452. purgedUsers++;
  453. }
  454. return purgedUsers;
  455. }
  456. /**
  457. * Check database structure for missing values.
  458. *
  459. * @param update Type of data to check updates for
  460. */
  461. private static void checkDatabaseStructure(DatabaseUpdateType update) {
  462. String sql = null;
  463. ResultSet resultSet = null;
  464. HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
  465. switch (update) {
  466. case BLAST_MINING:
  467. sql = "SELECT * FROM `" + tablePrefix + "cooldowns` ORDER BY `" + tablePrefix + "cooldowns`.`blast_mining` ASC LIMIT 0 , 30";
  468. break;
  469. case FISHING:
  470. sql = "SELECT * FROM `" + tablePrefix + "experience` ORDER BY `" + tablePrefix + "experience`.`fishing` ASC LIMIT 0 , 30";
  471. break;
  472. case INDEX:
  473. if (read("SHOW INDEX FROM " + tablePrefix + "skills").size() != 13 && checkConnected()) {
  474. mcMMO.p.getLogger().info("Indexing tables, this may take a while on larger databases");
  475. write("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_taming` (`taming`) USING BTREE, "
  476. + "ADD INDEX `idx_mining` (`mining`) USING BTREE, "
  477. + "ADD INDEX `idx_woodcutting` (`woodcutting`) USING BTREE, "
  478. + "ADD INDEX `idx_repair` (`repair`) USING BTREE, "
  479. + "ADD INDEX `idx_unarmed` (`unarmed`) USING BTREE, "
  480. + "ADD INDEX `idx_herbalism` (`herbalism`) USING BTREE, "
  481. + "ADD INDEX `idx_excavation` (`excavation`) USING BTREE, "
  482. + "ADD INDEX `idx_archery` (`archery`) USING BTREE, "
  483. + "ADD INDEX `idx_swords` (`swords`) USING BTREE, "
  484. + "ADD INDEX `idx_axes` (`axes`) USING BTREE, "
  485. + "ADD INDEX `idx_acrobatics` (`acrobatics`) USING BTREE, "
  486. + "ADD INDEX `idx_fishing` (`fishing`) USING BTREE;");
  487. }
  488. break;
  489. case MOB_HEALTHBARS:
  490. sql = "SELECT * FROM `" + tablePrefix + "huds` ORDER BY `" + tablePrefix + "huds`.`mobhealthbar` ASC LIMIT 0 , 30";
  491. break;
  492. case PARTY_NAMES:
  493. write("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party` ;");
  494. break;
  495. case USER_COLUMN:
  496. sql = "SELECT * FROM `" + tablePrefix + "users` ORDER BY `" + tablePrefix + "users`.`user` ASC LIMIT 0 , 30";
  497. break;
  498. default:
  499. break;
  500. }
  501. PreparedStatement statement = null;
  502. try {
  503. if (!checkConnected()) {
  504. return;
  505. }
  506. statement = connection.prepareStatement(sql);
  507. resultSet = statement.executeQuery();
  508. while (resultSet.next()) {
  509. ArrayList<String> column = new ArrayList<String>();
  510. for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  511. column.add(resultSet.getString(i));
  512. }
  513. rows.put(resultSet.getRow(), column);
  514. }
  515. }
  516. catch (SQLException ex) {
  517. switch (update) {
  518. case BLAST_MINING:
  519. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
  520. write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
  521. break;
  522. case FISHING:
  523. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
  524. write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  525. write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  526. break;
  527. case MOB_HEALTHBARS:
  528. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for mob healthbars...");
  529. write("ALTER TABLE `" + tablePrefix + "huds` ADD `mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "' ;");
  530. break;
  531. case USER_COLUMN:
  532. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for user column name...");
  533. write("ALTER TABLE `" + tablePrefix + "users` RENAME COLUMN `userr` to `user`;");
  534. break;
  535. default:
  536. break;
  537. }
  538. }
  539. finally {
  540. if (statement != null) {
  541. try {
  542. statement.close();
  543. }
  544. catch (SQLException e) {
  545. // Ignore the error, we're leaving
  546. }
  547. }
  548. }
  549. }
  550. private static void printErrors(SQLException ex) {
  551. mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
  552. mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
  553. mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());
  554. }
  555. }