1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-26 04:25:15 +01:00

Database conversion fix

This commit is contained in:
Zrips 2021-10-26 17:14:39 +03:00
parent 7320c1a349
commit 89f51b091a
3 changed files with 104 additions and 7 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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
super(plugin, path, "jdbc:mysql://" + hostname + "/" + database
+ "?maxReconnects=1&characterEncoding=" + characterEncoding + "&encoding="
+ encoding + "&useUnicode=true&autoReconnect=" + autoReconnect + "&useSSL=" + ssl
+ "&verifyServerCertificate=" + certificate, username, password, prefix);