From 89f51b091affefc14dc386416a2b8ad05d0c68c5 Mon Sep 17 00:00:00 2001 From: Zrips Date: Tue, 26 Oct 2021 17:14:39 +0300 Subject: [PATCH] Database conversion fix --- src/main/java/com/gamingmesh/jobs/Jobs.java | 10 +++ .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 65 ++++++++++++++++++- .../com/gamingmesh/jobs/dao/JobsMySQL.java | 36 ++++++++-- 3 files changed, 104 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 802e80c6..737aa401 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -76,6 +76,7 @@ import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; +import com.gamingmesh.jobs.container.JobsWorld; import com.gamingmesh.jobs.container.Log; import com.gamingmesh.jobs.container.PlayerInfo; import com.gamingmesh.jobs.container.PlayerPoints; @@ -102,6 +103,7 @@ import com.gamingmesh.jobs.selection.SelectionManager; import com.gamingmesh.jobs.stuff.Loging; import com.gamingmesh.jobs.stuff.TabComplete; import com.gamingmesh.jobs.stuff.ToggleBarHandling; +import com.gamingmesh.jobs.stuff.Util; import com.gamingmesh.jobs.stuff.VersionChecker; import com.gamingmesh.jobs.stuff.complement.Complement; import com.gamingmesh.jobs.stuff.complement.Complement1; @@ -583,6 +585,14 @@ public final class Jobs extends JavaPlugin { getPlayerManager().reload(); dao.truncateAllTables(); + + for (Job one : Jobs.getJobs()) { + dao.recordNewJobName(one, one.getId()); + } + for (JobsWorld one : Util.getJobsWorlds().values()) { + dao.recordNewWorld(one.getName(), one.getId()); + } + getPlayerManager().convertChacheOfPlayers(true); dao.continueConvertions(archivelist); diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 92d01eab..2ac701f0 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -43,6 +43,7 @@ import com.gamingmesh.jobs.stuff.TimeManage; import com.gamingmesh.jobs.stuff.Util; import net.Zrips.CMILib.Logs.CMIDebug; +import net.Zrips.CMILib.Messages.CMIMessages; public abstract class JobsDAO { @@ -479,8 +480,12 @@ public abstract class JobsDAO { } public final synchronized void setUp() { - if (getConnection() == null) + if (getConnection() == null) { + CMIMessages.consoleMessage("&cFAILED to connect to database"); return; + } + + CMIMessages.consoleMessage("&2Connected to database (" + dbType + ")"); vacuum(); @@ -511,8 +516,9 @@ public abstract class JobsDAO { public abstract boolean drop(String table); public boolean isConnected() { - if (pool == null) + if (pool == null) { return false; + } try { JobsConnection conn = pool.getConnection(); @@ -953,6 +959,29 @@ public abstract class JobsDAO { } } + public void recordNewWorld(String worldName, int id) { + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.WorldTable.getTableName() + "` (`id`,`" + worldsTableFields.name.getCollumn() + "`) VALUES (?,?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setInt(1, id); + prestt.setString(2, worldName); + prestt.executeUpdate(); + + res2 = prestt.getGeneratedKeys(); + Util.addJobsWorld(new JobsWorld(worldName, res2.next() ? res2.getInt(1) : 0)); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestt); + close(res2); + } + } + public synchronized void loadAllJobsWorlds() { JobsConnection conn = getConnection(); if (conn == null) @@ -1177,6 +1206,38 @@ public abstract class JobsDAO { } } + public void recordNewJobName(Job job, int id) { + JobsConnection conn = getConnection(); + if (conn == null) + return; + + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + conn.setAutoCommit(false); + + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.JobNameTable.getTableName() + "` (`id`,`" + jobsNameTableFields.name.getCollumn() + "`) VALUES (?,?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setInt(1, id); + prestt.setString(2, job.getName()); + int rowAffected = prestt.executeUpdate(); + + res2 = prestt.getGeneratedKeys(); + + job.setId(res2.next() ? res2.getInt(1) : 0); + + if (rowAffected != 1) { + conn.getConnection().rollback(); + } + + conn.commit(); + } catch (SQLException e) { + } finally { + close(prestt); + close(res2); + } + } + public synchronized void loadAllJobsNames() { JobsConnection conn = getConnection(); if (conn == null) diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java index 68cf656a..c3be73a6 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java @@ -8,11 +8,37 @@ import java.sql.Statement; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.dao.JobsManager.DataBaseType; +import net.Zrips.CMILib.Messages.CMIMessages; + public class JobsMySQL extends JobsDAO { + + private static String path = ""; + + static { + + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + path = "com.mysql.cj.jdbc.Driver"; + } catch (Throwable e) { + path = "com.mysql.jdbc.Driver"; + } + + String major = ""; + + String[] javaVersionElements = System.getProperty("java.runtime.version").split("\\.|_|-b"); + try { + major = javaVersionElements[0]; + } catch (Throwable e) { + String d = System.getProperty("java.version"); + String[] s = d.split("_")[0].split("."); + major = s[1]; + } + CMIMessages.consoleMessage("Detected Java" + major); + } JobsMySQL(Jobs plugin, String hostname, String database, String username, String password, String prefix, boolean certificate, boolean ssl, boolean autoReconnect, - String characterEncoding, String encoding) { - super(plugin, "com.mysql.jdbc.Driver", "jdbc:mysql://" + hostname + "/" + database + String characterEncoding, String encoding) { + super(plugin, path, "jdbc:mysql://" + hostname + "/" + database + "?maxReconnects=1&characterEncoding=" + characterEncoding + "&encoding=" + encoding + "&useUnicode=true&autoReconnect=" + autoReconnect + "&useSSL=" + ssl + "&verifyServerCertificate=" + certificate, username, password, prefix); @@ -24,9 +50,9 @@ public class JobsMySQL extends JobsDAO { } public JobsMySQL initialize(Jobs plugin, String hostname, String database, String username, String password, String prefix, boolean certificate, boolean ssl, boolean autoReconnect, - String characterEncoding, String encoding) { + String characterEncoding, String encoding) { JobsMySQL dao = new JobsMySQL(plugin, hostname, database, username, password, prefix, certificate, ssl, autoReconnect, - characterEncoding, encoding); + characterEncoding, encoding); dao.setUp(); return dao; } @@ -40,7 +66,7 @@ public class JobsMySQL extends JobsDAO { } executeSQL("CREATE TABLE `" + getPrefix() - + "users` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL, `username` varchar(20), `seen` bigint);"); + + "users` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `player_uuid` varchar(36) NOT NULL, `username` varchar(20), `seen` bigint);"); } @Override