1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-26 20:45:55 +01:00
Jobs/com/gamingmesh/jobs/dao/JobsDAOMySQL.java

842 lines
25 KiB
Java
Raw Normal View History

2015-08-21 15:10:08 +02:00
/**
* Jobs Plugin for Bukkit
* Copyright (C) 2011 Zak Ford <zak.j.ford@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gamingmesh.jobs.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
2016-03-30 15:42:36 +02:00
import java.util.HashMap;
import java.util.UUID;
import java.util.Map.Entry;
2015-08-21 15:10:08 +02:00
2016-03-30 15:42:36 +02:00
import org.bukkit.Bukkit;
2015-08-21 15:10:08 +02:00
import com.gamingmesh.jobs.Jobs;
2016-03-30 15:42:36 +02:00
import com.gamingmesh.jobs.container.PlayerInfo;
2015-08-21 15:10:08 +02:00
import com.gamingmesh.jobs.stuff.ChatColor;
import com.gamingmesh.jobs.stuff.UUIDUtil;
public class JobsDAOMySQL extends JobsDAO {
private String database;
2015-08-21 15:10:08 +02:00
private JobsDAOMySQL(String hostname, String database, String username, String password, String prefix) {
super("com.mysql.jdbc.Driver", "jdbc:mysql://" + hostname + "/" + database, username, password, prefix);
this.database = database;
}
public static JobsDAOMySQL initialize(String hostname, String database, String username, String password, String prefix) {
JobsDAOMySQL dao = new JobsDAOMySQL(hostname, database, username, password, prefix);
try {
dao.setUp();
} catch (SQLException e) {
e.printStackTrace();
2015-08-21 15:10:08 +02:00
}
return dao;
}
2015-08-21 15:10:08 +02:00
@Override
protected synchronized void setupConfig() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement prest = null;
int rows = 0;
try {
// Check for config table
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "config");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
2015-08-21 15:10:08 +02:00
try {
prest.close();
2015-08-21 15:10:08 +02:00
} catch (SQLException e) {
}
}
2015-08-21 15:10:08 +02:00
}
if (rows == 0) {
PreparedStatement insert = null;
try {
executeSQL("CREATE TABLE `" + getPrefix() + "config` (`key` varchar(50) NOT NULL PRIMARY KEY, `value` varchar(100) NOT NULL);");
2015-08-21 15:10:08 +02:00
insert = conn.prepareStatement("INSERT INTO `" + getPrefix() + "config` (`key`, `value`) VALUES (?, ?);");
insert.setString(1, "version");
insert.setString(2, "1");
insert.execute();
} finally {
if (insert != null) {
try {
insert.close();
} catch (SQLException e) {
}
2015-08-21 15:10:08 +02:00
}
}
2015-08-21 15:10:08 +02:00
}
}
2015-08-21 15:10:08 +02:00
@Override
protected synchronized void checkUpdate1() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement prest = null;
int rows = 0;
try {
// Check for jobs table
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "jobs");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
2015-08-21 15:10:08 +02:00
}
}
}
PreparedStatement pst1 = null;
PreparedStatement pst2 = null;
try {
if (rows == 0) {
executeSQL("CREATE TABLE `" + getPrefix()
+ "jobs` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` binary(16) NOT NULL, `job` varchar(20), `experience` int, `level` int);");
} else {
Jobs.getPluginLogger().info("Converting existing usernames to Mojang UUIDs. This could take a long time!");
2015-08-21 15:10:08 +02:00
try {
// Check for jobs table id column
// This is extra check to be sure there is no column by this name already
int idrows = 0;
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "jobs");
prest.setString(3, "id");
ResultSet res = prest.executeQuery();
if (res.next()) {
idrows = res.getInt(1);
}
if (idrows == 0)
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;");
2015-08-21 15:10:08 +02:00
} finally {
2015-08-21 15:10:08 +02:00
}
try {
// Check for jobs table id column
// This is extra check to be sure there is no column by this name already
int uuidrows = 0;
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "jobs");
prest.setString(3, "player_uuid");
ResultSet res = prest.executeQuery();
if (res.next()) {
uuidrows = res.getInt(1);
}
if (uuidrows == 0)
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `player_uuid` binary(16) DEFAULT NULL AFTER `id`;");
} finally {
2015-08-21 15:10:08 +02:00
}
2015-08-21 15:10:08 +02:00
pst1 = conn.prepareStatement("SELECT DISTINCT `username` FROM `" + getPrefix() + "jobs` WHERE `player_uuid` IS NULL;");
ResultSet rs = pst1.executeQuery();
ArrayList<String> usernames = new ArrayList<String>();
while (rs.next()) {
usernames.add(rs.getString(1));
}
2015-08-21 15:10:08 +02:00
pst2 = conn.prepareStatement("UPDATE `" + getPrefix() + "jobs` SET `player_uuid` = ? WHERE `username` = ?;");
2015-08-21 15:10:08 +02:00
int i = 0;
int y = 0;
for (String names : usernames) {
i++;
y++;
if (i >= 10) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "" + y + " of " + usernames.size());
i = 0;
}
2015-11-25 16:14:48 +01:00
2016-03-30 15:42:36 +02:00
Entry<String, PlayerInfo> info = Jobs.getPlayerManager().getPlayerInfoByName(names);
if (info == null)
continue;
2015-11-25 16:14:48 +01:00
2016-03-30 15:42:36 +02:00
pst2.setBytes(1, UUIDUtil.toBytes(UUID.fromString(info.getKey())));
pst2.setString(2, names);
pst2.execute();
}
2015-08-21 15:10:08 +02:00
Jobs.getPluginLogger().info("Mojang UUID conversion complete!");
}
} finally {
if (pst1 != null) {
try {
pst1.close();
} catch (SQLException e) {
}
}
if (pst2 != null) {
try {
pst2.close();
} catch (SQLException e) {
}
}
}
2015-08-21 15:10:08 +02:00
checkUpdate2();
}
2015-08-21 15:10:08 +02:00
@Override
protected synchronized void checkUpdate2() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement prest = null;
int rows = 0;
try {
// Check for jobs table
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "jobs");
prest.setString(3, "username");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
2015-08-21 15:10:08 +02:00
}
}
}
2015-08-21 15:10:08 +02:00
try {
if (rows == 0)
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `username` varchar(20);");
} finally {
2015-08-21 15:10:08 +02:00
}
checkUpdate4();
}
2015-08-21 15:10:08 +02:00
@Override
protected synchronized void checkUpdate4() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement prest = null;
int rows = 0;
try {
// Check for jobs table
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "archive");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
2015-08-21 15:10:08 +02:00
try {
prest.close();
} catch (SQLException e) {
2015-08-21 15:10:08 +02:00
}
}
}
2015-08-21 15:10:08 +02:00
try {
if (rows == 0)
executeSQL("CREATE TABLE `" + getPrefix()
+ "archive` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` binary(16) NOT NULL, `username` varchar(20), `job` varchar(20), `experience` int, `level` int);");
} finally {
2015-08-21 15:10:08 +02:00
}
}
2015-08-21 15:10:08 +02:00
@Override
protected synchronized void checkUpdate5() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement prest = null;
int rows = 0;
try {
// Check for jobs table
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "log");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
2015-08-21 15:10:08 +02:00
try {
prest.close();
} catch (SQLException e) {
2015-08-21 15:10:08 +02:00
}
}
}
2015-08-21 15:10:08 +02:00
try {
if (rows == 0)
executeSQL("CREATE TABLE `" + getPrefix()
2015-11-25 16:14:48 +01:00
+ "log` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` binary(16) NOT NULL, `username` varchar(20), `time` bigint, `action` varchar(20), `itemname` varchar(60), `count` int, `money` double, `exp` double);");
} finally {
2015-08-21 15:10:08 +02:00
}
}
2015-11-25 16:14:48 +01:00
@Override
protected synchronized void checkUpdate6() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
try {
executeSQL("ALTER TABLE `" + getPrefix() + "log` MODIFY `itemname` VARCHAR(60);");
} catch (Exception e) {
}
2015-11-25 16:14:48 +01:00
}
2015-12-10 14:13:57 +01:00
@Override
protected synchronized void checkUpdate7() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
boolean convertJobs = true;
PreparedStatement tempPst = conn.prepareStatement("SELECT * FROM `" + getPrefix() + "jobs`;");
ResultSet tempRes = tempPst.executeQuery();
2015-12-10 14:13:57 +01:00
2016-04-21 16:26:17 +02:00
boolean noJobsdata = true;
try {
while (tempRes.next()) {
2016-04-21 16:26:17 +02:00
noJobsdata = false;
tempRes.getByte("player_uuid");
break;
}
} catch (Exception e) {
convertJobs = false;
} finally {
tempRes.close();
tempPst.close();
}
2016-04-21 16:26:17 +02:00
if (noJobsdata) {
dropDataBase("jobs");
createDefaultJobsBase();
convertJobs = false;
}
2015-12-10 14:13:57 +01:00
if (convertJobs) {
Jobs.getPluginLogger().info("Converting byte uuids to string. This could take a long time!!!");
// Converting jobs players byte uuid into string
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "jobs_temp` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL,`username` varchar(20), `job` varchar(20), `experience` int, `level` int);");
} catch (Exception e) {
}
PreparedStatement pst1 = conn.prepareStatement("SELECT * FROM `" + getPrefix() + "jobs`;");
ResultSet rs = pst1.executeQuery();
PreparedStatement insert = null;
2015-12-10 14:13:57 +01:00
conn.setAutoCommit(false);
while (rs.next()) {
byte[] uuidBytes = rs.getBytes("player_uuid");
2015-12-10 14:13:57 +01:00
if (uuidBytes == null)
continue;
String uuid = UUIDUtil.fromBytes(uuidBytes).toString();
if (uuid != null) {
insert = conn.prepareStatement("INSERT INTO `" + getPrefix()
+ "jobs_temp` (`player_uuid`, `username`, `job`, `experience`, `level`) VALUES (?, ?, ?, ?, ?);");
insert.setString(1, uuid);
insert.setString(2, rs.getString("username"));
insert.setString(3, rs.getString("job"));
insert.setInt(4, rs.getInt("experience"));
insert.setInt(5, rs.getInt("level"));
insert.addBatch();
}
2015-12-10 14:13:57 +01:00
}
if (insert != null)
insert.executeBatch();
conn.commit();
conn.setAutoCommit(true);
rs.close();
if (insert != null)
insert.close();
executeSQL("DROP TABLE IF EXISTS `" + getPrefix() + "jobs`;");
executeSQL("ALTER TABLE `" + getPrefix() + "jobs_temp` RENAME TO `" + getPrefix() + "jobs`;");
2015-12-10 14:13:57 +01:00
}
boolean convertArchive = true;
PreparedStatement tempArchivePst = conn.prepareStatement("SELECT * FROM `" + getPrefix() + "archive`;");
ResultSet tempArchiveRes = tempArchivePst.executeQuery();
2016-04-21 16:26:17 +02:00
boolean noArchivedata = true;
try {
while (tempArchiveRes.next()) {
2016-04-21 16:26:17 +02:00
noArchivedata = false;
tempArchiveRes.getByte("player_uuid");
break;
2015-12-10 14:13:57 +01:00
}
} catch (Exception e) {
convertArchive = false;
} finally {
tempArchiveRes.close();
tempArchivePst.close();
2015-12-10 14:13:57 +01:00
}
2016-04-21 16:26:17 +02:00
if (noArchivedata) {
dropDataBase("archive");
createDefaultArchiveBase();
convertArchive = false;
}
if (convertArchive) {
// Converting archive players byte uuid into string
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "archive_temp` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL, `username` varchar(20), `job` varchar(20), `experience` int, `level` int);");
} catch (Exception e) {
2015-12-10 14:13:57 +01:00
}
PreparedStatement pst11 = conn.prepareStatement("SELECT * FROM `" + getPrefix() + "archive`;");
ResultSet rs1 = pst11.executeQuery();
PreparedStatement insert1 = null;
conn.setAutoCommit(false);
while (rs1.next()) {
String uuid = UUIDUtil.fromBytes(rs1.getBytes("player_uuid")).toString();
if (uuid != null) {
insert1 = conn.prepareStatement("INSERT INTO `" + getPrefix()
+ "archive_temp` (`player_uuid`, `username`, `job`, `experience`, `level`) VALUES (?, ?, ?, ?, ?);");
insert1.setString(1, uuid);
insert1.setString(2, rs1.getString("username"));
insert1.setString(3, rs1.getString("job"));
insert1.setInt(4, rs1.getInt("experience"));
insert1.setInt(5, rs1.getInt("level"));
insert1.addBatch();
}
}
if (insert1 != null)
insert1.executeBatch();
conn.commit();
conn.setAutoCommit(true);
rs1.close();
if (insert1 != null)
insert1.close();
executeSQL("DROP TABLE IF EXISTS `" + getPrefix() + "archive`;");
try {
executeSQL("ALTER TABLE `" + getPrefix() + "archive_temp` RENAME TO `" + getPrefix() + "archive`;");
} catch (Exception e) {
}
}
boolean convertLog = true;
PreparedStatement tempLogPst = conn.prepareStatement("SELECT * FROM `" + getPrefix() + "log`;");
ResultSet tempLogRes = tempLogPst.executeQuery();
2016-04-21 16:26:17 +02:00
boolean nodata = true;
try {
while (tempLogRes.next()) {
2016-04-21 16:26:17 +02:00
nodata = false;
tempLogRes.getByte("player_uuid");
break;
}
} catch (Exception e) {
convertLog = false;
} finally {
tempLogRes.close();
tempLogPst.close();
2015-12-10 14:13:57 +01:00
}
2016-04-21 16:26:17 +02:00
if (nodata) {
dropDataBase("log");
createDefaultLogBase();
convertLog = false;
}
2015-12-10 14:13:57 +01:00
if (convertLog) {
2016-04-21 16:26:17 +02:00
Bukkit.getConsoleSender().sendMessage("Converting log database");
// Converting log players byte uuid into string
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "log_temp` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL, `username` varchar(20), `time` bigint, `action` varchar(20), `itemname` varchar(60), `count` int, `money` double, `exp` double);");
} catch (Exception e) {
}
PreparedStatement pst111 = conn.prepareStatement("SELECT * FROM `" + getPrefix() + "log`;");
ResultSet rs11 = pst111.executeQuery();
PreparedStatement insert11 = null;
conn.setAutoCommit(false);
while (rs11.next()) {
String uuid = UUIDUtil.fromBytes(rs11.getBytes("player_uuid")).toString();
if (uuid != null) {
insert11 = conn.prepareStatement("INSERT INTO `" + getPrefix()
+ "log_temp` (`player_uuid`, `username`, `time`, `action`, `itemname`, `count`, `money`, `exp`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
insert11.setString(1, uuid);
insert11.setString(2, rs11.getString("username"));
insert11.setLong(3, rs11.getLong("time"));
insert11.setString(4, rs11.getString("action"));
insert11.setString(5, rs11.getString("itemname"));
insert11.setInt(6, rs11.getInt("count"));
insert11.setDouble(7, rs11.getDouble("money"));
insert11.setDouble(8, rs11.getDouble("exp"));
insert11.addBatch();
}
}
if (insert11 != null)
insert11.executeBatch();
conn.commit();
conn.setAutoCommit(true);
rs11.close();
if (insert11 != null)
insert11.close();
executeSQL("DROP TABLE IF EXISTS `" + getPrefix() + "log`;");
try {
executeSQL("ALTER TABLE `" + getPrefix() + "log_temp` RENAME TO `" + getPrefix() + "log`;");
} catch (Exception e) {
}
}
2015-12-10 14:13:57 +01:00
}
2016-01-09 13:58:33 +01:00
@Override
protected synchronized void checkUpdate8() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement prest = null;
int rows = 0;
try {
// Check for jobs table
prest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;");
prest.setString(1, database);
prest.setString(2, getPrefix() + "explore");
ResultSet res = prest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
}
}
try {
if (rows == 0)
executeSQL("CREATE TABLE `" + getPrefix()
+ "explore` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `worldname` varchar(64), `chunkX` int, `chunkZ` int, `playerName` varchar(32));");
} catch (Exception e) {
2016-01-09 13:58:33 +01:00
} finally {
}
}
2016-03-30 15:42:36 +02:00
@Override
protected synchronized void checkUpdate9() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.getPluginLogger().severe("Could not run database updates! Could not connect to MySQL!");
return;
}
PreparedStatement tempPrest = null;
int rows = 0;
2016-03-30 15:42:36 +02:00
try {
// Check for jobs table
tempPrest = conn.prepareStatement("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?;");
tempPrest.setString(1, database);
tempPrest.setString(2, getPrefix() + "users");
ResultSet res = tempPrest.executeQuery();
if (res.next()) {
rows = res.getInt(1);
2016-03-30 15:42:36 +02:00
}
} finally {
if (tempPrest != null) {
2016-03-30 15:42:36 +02:00
try {
tempPrest.close();
2016-03-30 15:42:36 +02:00
} catch (SQLException e) {
}
}
}
if (rows == 0) {
HashMap<String, String> tempMap = new HashMap<String, String>();
PreparedStatement prest = null;
try {
prest = conn.prepareStatement("SELECT DISTINCT(player_uuid),username FROM " + getPrefix() + "jobs;");
ResultSet res = prest.executeQuery();
while (res.next()) {
tempMap.put(res.getString("player_uuid"), res.getString("username"));
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
}
try {
prest = conn.prepareStatement("SELECT DISTINCT(player_uuid),username FROM " + getPrefix() + "archive;");
ResultSet res = prest.executeQuery();
while (res.next()) {
tempMap.put(res.getString("player_uuid"), res.getString("username"));
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
}
2016-03-30 15:42:36 +02:00
}
try {
prest = conn.prepareStatement("SELECT DISTINCT(player_uuid),username FROM " + getPrefix() + "log;");
ResultSet res = prest.executeQuery();
while (res.next()) {
tempMap.put(res.getString("player_uuid"), res.getString("username"));
2016-03-30 15:42:36 +02:00
}
} finally {
if (prest != null)
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "users` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL, `username` varchar(20));");
} catch (Exception e) {
2016-03-30 15:42:36 +02:00
}
try {
prest = conn.prepareStatement("INSERT INTO `" + getPrefix() + "users` (`player_uuid`, `username`) VALUES (?, ?);");
conn.setAutoCommit(false);
for (Entry<String, String> users : tempMap.entrySet()) {
prest.setString(1, users.getKey());
prest.setString(2, users.getValue());
prest.addBatch();
}
prest.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
}
HashMap<String, PlayerInfo> tempPlayerMap = new HashMap<String, PlayerInfo>();
2016-03-30 15:42:36 +02:00
try {
prest = conn.prepareStatement("SELECT * FROM " + getPrefix() + "users;");
ResultSet res = prest.executeQuery();
while (res.next()) {
tempPlayerMap.put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id")));
}
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
}
// Modifying jobs main table
try {
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` ADD COLUMN `userid` int;");
} catch (Exception e) {
2016-03-30 15:42:36 +02:00
}
try {
prest = conn.prepareStatement("UPDATE `" + getPrefix() + "jobs` SET `userid` = ? WHERE `player_uuid` = ?;");
conn.setAutoCommit(false);
for (Entry<String, PlayerInfo> users : tempPlayerMap.entrySet()) {
prest.setInt(1, users.getValue().getID());
prest.setString(2, users.getKey());
prest.addBatch();
}
prest.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
}
try {
executeSQL("ALTER TABLE `" + getPrefix() + "jobs` DROP COLUMN `player_uuid`, DROP COLUMN `username`;");
} catch (Exception e) {
2016-03-30 15:42:36 +02:00
}
// Modifying jobs archive table
try {
executeSQL("ALTER TABLE `" + getPrefix() + "archive` ADD COLUMN `userid` int;");
} catch (Exception e) {
}
try {
prest = conn.prepareStatement("UPDATE `" + getPrefix() + "archive` SET `userid` = ? WHERE `player_uuid` = ?;");
conn.setAutoCommit(false);
for (Entry<String, PlayerInfo> users : tempPlayerMap.entrySet()) {
prest.setInt(1, users.getValue().getID());
prest.setString(2, users.getKey());
prest.addBatch();
}
prest.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
}
try {
executeSQL("ALTER TABLE `" + getPrefix() + "archive` DROP COLUMN `player_uuid`, DROP COLUMN `username`;");
} catch (Exception e) {
2016-03-30 15:42:36 +02:00
}
// Modifying jobs log table
try {
executeSQL("ALTER TABLE `" + getPrefix() + "log` ADD COLUMN `userid` int;");
} catch (Exception e) {
}
try {
prest = conn.prepareStatement("UPDATE `" + getPrefix() + "log` SET `userid` = ? WHERE `player_uuid` = ?;");
conn.setAutoCommit(false);
for (Entry<String, PlayerInfo> users : tempPlayerMap.entrySet()) {
prest.setInt(1, users.getValue().getID());
prest.setString(2, users.getKey());
prest.addBatch();
}
prest.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} finally {
if (prest != null) {
try {
prest.close();
} catch (SQLException e) {
}
2016-03-30 15:42:36 +02:00
}
}
try {
executeSQL("ALTER TABLE `" + getPrefix() + "log` DROP COLUMN `player_uuid`, DROP COLUMN `username`;");
} catch (Exception e) {
}
// Create new points table
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "points` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `userid` int, `totalpoints` double, `currentpoints` double);");
} catch (Exception e) {
}
2016-03-30 15:42:36 +02:00
}
}
2016-04-21 16:26:17 +02:00
private boolean createDefaultLogBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "log` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `userid` int, `time` bigint, `action` varchar(20), `itemname` varchar(60), `count` int, `money` double, `exp` double);");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultArchiveBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "archive` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `userid` int, `job` varchar(20), `experience` int, `level` int);");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean createDefaultJobsBase() {
try {
executeSQL("CREATE TABLE `" + getPrefix()
+ "jobs` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `userid` int, `job` varchar(20), `experience` int, `level` int);");
} catch (SQLException e) {
return false;
}
return true;
}
private boolean dropDataBase(String name) {
try {
executeSQL("DROP TABLE IF EXISTS `" + getPrefix() + name + "`;");
} catch (SQLException e) {
return false;
}
return true;
}
2015-08-21 15:10:08 +02:00
}