Finished jOOQ transition
This commit is contained in:
3 files changed
+176
-141
No files matched your search
@@ -2,11 +2,17 @@ package uk.hotten.staffog.data;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DSL;
|
||||
import uk.hotten.staffog.punish.data.PunishType;
|
||||
import uk.hotten.staffog.utils.Console;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
import static uk.hotten.staffog.data.jooq.Tables.STAFFOG_LINKCODE;
|
||||
import static uk.hotten.staffog.data.jooq.Tables.STAFFOG_STAT;
|
||||
|
||||
public class DatabaseManager {
|
||||
|
||||
@Getter private JavaPlugin plugin;
|
||||
@@ -202,12 +208,10 @@ public class DatabaseManager {
|
||||
|
||||
private boolean doesStatIncludeEntry(String entry) {
|
||||
try (Connection connection = createConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `staffog_stat` WHERE `name`=?");
|
||||
statement.setString(1, entry);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
return rs.next();
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
return dsl.fetchExists(dsl.selectFrom(STAFFOG_STAT)
|
||||
.where(STAFFOG_STAT.NAME.eq(entry)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
@@ -217,16 +221,17 @@ public class DatabaseManager {
|
||||
public void setStatEntry(String entry, String stat) {
|
||||
try (Connection connection = createConnection()) {
|
||||
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
if (!doesStatIncludeEntry(entry)) {
|
||||
PreparedStatement statement = connection.prepareStatement("INSERT INTO `staffog_stat` (`name`, `stat`) VALUE (?, ?)");
|
||||
statement.setString(1, entry);
|
||||
statement.setString(2, stat);
|
||||
statement.executeUpdate();
|
||||
dsl.insertInto(STAFFOG_STAT)
|
||||
.set(STAFFOG_STAT.NAME, entry)
|
||||
.set(STAFFOG_STAT.STAT, stat)
|
||||
.execute();
|
||||
} else {
|
||||
PreparedStatement statement = connection.prepareStatement("UPDATE `staffog_stat` SET `stat`=? WHERE `name`=?");
|
||||
statement.setString(1, stat);
|
||||
statement.setString(2, entry);
|
||||
statement.executeUpdate();
|
||||
dsl.update(STAFFOG_STAT)
|
||||
.set(STAFFOG_STAT.STAT, stat)
|
||||
.where(STAFFOG_STAT.NAME.eq(entry))
|
||||
.execute();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -6,13 +6,12 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jooq.DSLContext;
|
||||
import org.jooq.Result;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.impl.DSL;
|
||||
import uk.hotten.staffog.data.DatabaseManager;
|
||||
import uk.hotten.staffog.data.jooq.tables.StaffogChatreport;
|
||||
import uk.hotten.staffog.data.jooq.tables.records.StaffogBanRecord;
|
||||
import uk.hotten.staffog.data.jooq.tables.records.StaffogChatreportRecord;
|
||||
import uk.hotten.staffog.data.jooq.tables.records.StaffogHistoryRecord;
|
||||
import uk.hotten.staffog.data.jooq.tables.records.StaffogMuteRecord;
|
||||
import uk.hotten.staffog.punish.data.ChatReportEntry;
|
||||
import uk.hotten.staffog.punish.data.KickPunishEntry;
|
||||
import uk.hotten.staffog.punish.data.PunishEntry;
|
||||
@@ -23,9 +22,6 @@ import uk.hotten.staffog.utils.Message;
|
||||
import uk.hotten.staffog.utils.TimeUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -50,30 +46,14 @@ public class PunishManager {
|
||||
public void checkNameToUuid(String name, UUID uuid) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
/*PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `staffog_history` WHERE `uuid`=? AND `name`=?");
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, name);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
boolean result = rs.next();
|
||||
|
||||
if (result)
|
||||
return;
|
||||
|
||||
statement = connection.prepareStatement("INSERT INTO `staffog_history` (`uuid`, `name`) VALUE (?, ?)");
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, name);
|
||||
statement.executeUpdate();*/
|
||||
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
StaffogHistoryRecord record = dsl.select(STAFFOG_HISTORY.ID)
|
||||
|
||||
boolean exists = dsl.fetchExists(dsl.select(STAFFOG_HISTORY.ID)
|
||||
.from(STAFFOG_HISTORY)
|
||||
.where(STAFFOG_HISTORY.UUID.eq(uuid.toString()))
|
||||
.and(STAFFOG_HISTORY.NAME.eq(name))
|
||||
.fetchOneInto(STAFFOG_HISTORY);
|
||||
.and(STAFFOG_HISTORY.NAME.eq(name)));
|
||||
|
||||
if (record == null) {
|
||||
Console.info("returniung");
|
||||
if (exists) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -91,16 +71,14 @@ public class PunishManager {
|
||||
public String getNameFromUUID(UUID uuid) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT `name` FROM `staffog_history` WHERE `uuid`=? ORDER BY `id` DESC LIMIT 1");
|
||||
statement.setString(1, uuid.toString());
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
return rs.getString("name");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return dsl.select(STAFFOG_HISTORY.NAME)
|
||||
.from(STAFFOG_HISTORY)
|
||||
.where(STAFFOG_HISTORY.UUID.eq(uuid.toString()))
|
||||
.orderBy(STAFFOG_HISTORY.ID.desc())
|
||||
.limit(1)
|
||||
.fetchOne(STAFFOG_HISTORY.NAME);
|
||||
|
||||
} catch (Exception e) {
|
||||
Console.error("Failed to get name from uuid. " + uuid.toString());
|
||||
@@ -112,16 +90,18 @@ public class PunishManager {
|
||||
public UUID getUUIDFromName(String name) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT `uuid` FROM `staffog_history` WHERE `name`=? ORDER BY `id` DESC LIMIT 1");
|
||||
statement.setString(1, name);
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
String uuidString = dsl.select(STAFFOG_HISTORY.UUID)
|
||||
.from(STAFFOG_HISTORY)
|
||||
.where(STAFFOG_HISTORY.NAME.eq(name))
|
||||
.orderBy(STAFFOG_HISTORY.ID.desc())
|
||||
.limit(1)
|
||||
.fetchOne(STAFFOG_HISTORY.UUID);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
return UUID.fromString(rs.getString("uuid"));
|
||||
} else {
|
||||
if (uuidString == null)
|
||||
return null;
|
||||
}
|
||||
|
||||
return UUID.fromString(uuidString);
|
||||
|
||||
} catch (Exception e) {
|
||||
Console.error("Failed to get name from name. " + name);
|
||||
@@ -132,18 +112,29 @@ public class PunishManager {
|
||||
|
||||
public void newPunishment(PunishEntry entry) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO `" + entry.getType().getTable() + "` (`uuid`, `reason`, `by_uuid`, `by_name`, `time`, `until`, `active`) VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
);
|
||||
statement.setString(1, entry.getUuid().toString());
|
||||
statement.setString(2, entry.getReason());
|
||||
statement.setString(3, entry.getByUuid().toString());
|
||||
statement.setString(4, entry.getByName());
|
||||
statement.setLong(5, entry.getTime());
|
||||
statement.setLong(6, entry.getUntil());
|
||||
statement.setInt(7, entry.isActive() ? 1 : 0);
|
||||
|
||||
statement.executeUpdate();
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
if (entry.getType() == PunishType.BAN) {
|
||||
dsl.insertInto(STAFFOG_BAN)
|
||||
.set(STAFFOG_BAN.UUID, entry.getUuid().toString())
|
||||
.set(STAFFOG_BAN.REASON, entry.getReason())
|
||||
.set(STAFFOG_BAN.BY_UUID, entry.getByUuid())
|
||||
.set(STAFFOG_BAN.BY_NAME, entry.getByName())
|
||||
.set(STAFFOG_BAN.TIME, entry.getTime())
|
||||
.set(STAFFOG_BAN.UNTIL, entry.getUntil())
|
||||
.set(STAFFOG_BAN.ACTIVE, entry.isActive())
|
||||
.execute();
|
||||
} else {
|
||||
dsl.insertInto(STAFFOG_MUTE)
|
||||
.set(STAFFOG_MUTE.UUID, entry.getUuid().toString())
|
||||
.set(STAFFOG_MUTE.REASON, entry.getReason())
|
||||
.set(STAFFOG_MUTE.BY_UUID, entry.getByUuid())
|
||||
.set(STAFFOG_MUTE.BY_NAME, entry.getByName())
|
||||
.set(STAFFOG_MUTE.TIME, entry.getTime())
|
||||
.set(STAFFOG_MUTE.UNTIL, entry.getUntil())
|
||||
.set(STAFFOG_MUTE.ACTIVE, entry.isActive())
|
||||
.execute();
|
||||
}
|
||||
|
||||
String duration = TimeUtils.formatMillisecondTime(entry.calculateDuration());
|
||||
if (entry.getPlayer() != null && entry.getPlayer().isOnline()) {
|
||||
@@ -179,29 +170,56 @@ public class PunishManager {
|
||||
public PunishEntry getPunishment(PunishType type, int id) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `" + type.getTable() + "` WHERE `id`=?");
|
||||
statement.setLong(1, id);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
if (!rs.next())
|
||||
return null;
|
||||
|
||||
PunishEntry entry = new PunishEntry(type);
|
||||
entry.setId(rs.getLong("id"));
|
||||
entry.setUuid(UUID.fromString(rs.getString("uuid")));
|
||||
entry.setName(getNameFromUUID(entry.getUuid()));
|
||||
entry.setReason(rs.getString("reason"));
|
||||
entry.setByUuid(rs.getString("by_uuid"));
|
||||
entry.setByName(rs.getString("by_name"));
|
||||
entry.setTime(rs.getLong("time"));
|
||||
entry.setUntil(rs.getLong("until"));
|
||||
entry.setActive(rs.getInt("active") == 1);
|
||||
if (!entry.isActive()) {
|
||||
entry.setRemovedReason(rs.getString("removed_uuid"));
|
||||
entry.setRemovedName(rs.getString("removed_name"));
|
||||
entry.setRemovedTime(rs.getLong("removed_time"));
|
||||
entry.setRemovedReason(rs.getString("removed_reason"));
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
if (type == PunishType.BAN) {
|
||||
StaffogBanRecord record = dsl.select(STAFFOG_BAN.asterisk())
|
||||
.from(STAFFOG_BAN)
|
||||
.where(STAFFOG_BAN.ID.eq((long) id))
|
||||
.fetchOneInto(STAFFOG_BAN);
|
||||
|
||||
if (record == null)
|
||||
return null;
|
||||
|
||||
entry.setId(record.getId());
|
||||
entry.setUuid(UUID.fromString(record.getUuid()));
|
||||
entry.setName(getNameFromUUID(entry.getUuid()));
|
||||
entry.setReason(record.getReason());
|
||||
entry.setByUuid(record.getByUuid());
|
||||
entry.setByName(record.getByName());
|
||||
entry.setTime(record.getTime());
|
||||
entry.setUntil(record.getUntil());
|
||||
entry.setActive(record.getActive());
|
||||
if (!entry.isActive()) {
|
||||
entry.setRemovedUuid(record.getRemovedUuid());
|
||||
entry.setRemovedName(record.getRemovedName());
|
||||
entry.setRemovedTime(record.getRemovedTime());
|
||||
entry.setRemovedReason(record.getRemovedReason());
|
||||
}
|
||||
} else {
|
||||
StaffogMuteRecord record = dsl.select(STAFFOG_MUTE.asterisk())
|
||||
.from(STAFFOG_MUTE)
|
||||
.where(STAFFOG_MUTE.ID.eq((long) id))
|
||||
.fetchOneInto(STAFFOG_MUTE);
|
||||
|
||||
if (record == null)
|
||||
return null;
|
||||
|
||||
entry.setId(record.getId());
|
||||
entry.setUuid(UUID.fromString(record.getUuid()));
|
||||
entry.setName(getNameFromUUID(entry.getUuid()));
|
||||
entry.setReason(record.getReason());
|
||||
entry.setByUuid(record.getByUuid());
|
||||
entry.setByName(record.getByName());
|
||||
entry.setTime(record.getTime());
|
||||
entry.setUntil(record.getUntil());
|
||||
entry.setActive(record.getActive());
|
||||
if (!entry.isActive()) {
|
||||
entry.setRemovedUuid(record.getRemovedUuid());
|
||||
entry.setRemovedName(record.getRemovedName());
|
||||
entry.setRemovedTime(record.getRemovedTime());
|
||||
entry.setRemovedReason(record.getRemovedReason());
|
||||
}
|
||||
}
|
||||
|
||||
if (entry.checkDurationOver()) {
|
||||
@@ -222,23 +240,47 @@ public class PunishManager {
|
||||
public PunishEntry checkActivePunishment(PunishType type, UUID uuid) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `" + type.getTable() + "` WHERE `uuid`=? AND `active`=1");
|
||||
statement.setString(1, uuid.toString());
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
if (!rs.next())
|
||||
return null;
|
||||
|
||||
PunishEntry entry = new PunishEntry(type);
|
||||
entry.setId(rs.getLong("id"));
|
||||
entry.setUuid(uuid);
|
||||
entry.setReason(rs.getString("reason"));
|
||||
entry.setByUuid(rs.getString("by_uuid"));
|
||||
entry.setByName(rs.getString("by_name"));
|
||||
entry.setTime(rs.getLong("time"));
|
||||
entry.setUntil(rs.getLong("until"));
|
||||
entry.setActive(rs.getInt("active") == 1);
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
if (type == PunishType.BAN) {
|
||||
StaffogBanRecord record = dsl.select(STAFFOG_BAN.asterisk())
|
||||
.from(STAFFOG_BAN)
|
||||
.where(STAFFOG_BAN.UUID.eq(uuid.toString()))
|
||||
.and(STAFFOG_BAN.ACTIVE.eq(true))
|
||||
.fetchOneInto(STAFFOG_BAN);
|
||||
|
||||
if (record == null)
|
||||
return null;
|
||||
|
||||
entry.setId(record.getId());
|
||||
entry.setUuid(UUID.fromString(record.getUuid()));
|
||||
entry.setName(getNameFromUUID(entry.getUuid()));
|
||||
entry.setReason(record.getReason());
|
||||
entry.setByUuid(record.getByUuid());
|
||||
entry.setByName(record.getByName());
|
||||
entry.setTime(record.getTime());
|
||||
entry.setUntil(record.getUntil());
|
||||
entry.setActive(record.getActive());
|
||||
} else {
|
||||
StaffogMuteRecord record = dsl.select(STAFFOG_MUTE.asterisk())
|
||||
.from(STAFFOG_MUTE)
|
||||
.where(STAFFOG_MUTE.UUID.eq(uuid.toString()))
|
||||
.and(STAFFOG_MUTE.ACTIVE.eq(true))
|
||||
.fetchOneInto(STAFFOG_MUTE);
|
||||
|
||||
if (record == null)
|
||||
return null;
|
||||
|
||||
entry.setId(record.getId());
|
||||
entry.setUuid(UUID.fromString(record.getUuid()));
|
||||
entry.setName(getNameFromUUID(entry.getUuid()));
|
||||
entry.setReason(record.getReason());
|
||||
entry.setByUuid(record.getByUuid());
|
||||
entry.setByName(record.getByName());
|
||||
entry.setTime(record.getTime());
|
||||
entry.setUntil(record.getUntil());
|
||||
entry.setActive(record.getActive());
|
||||
}
|
||||
|
||||
if (entry.checkDurationOver()) {
|
||||
expirePunishment(entry);
|
||||
@@ -271,18 +313,26 @@ public class PunishManager {
|
||||
if (entry.getName() == null)
|
||||
entry.setName(getNameFromUUID(entry.getUuid()));
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement(
|
||||
"UPDATE `" + entry.getType().getTable() + "` SET `removed_uuid`=?, `removed_name`=?, `removed_reason`=?, `removed_time`=?, `active`=? WHERE `id`=?"
|
||||
);
|
||||
|
||||
statement.setString(1, entry.getRemovedUuid());
|
||||
statement.setString(2, entry.getRemovedName());
|
||||
statement.setString(3, entry.getRemovedReason());
|
||||
statement.setLong(4, entry.getRemovedTime());
|
||||
statement.setInt(5, 0);
|
||||
statement.setLong(6, entry.getId());
|
||||
|
||||
statement.executeUpdate();
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
if (entry.getType() == PunishType.BAN) {
|
||||
dsl.update(STAFFOG_BAN)
|
||||
.set(STAFFOG_BAN.REMOVED_UUID, entry.getRemovedUuid())
|
||||
.set(STAFFOG_BAN.REMOVED_NAME, entry.getRemovedName())
|
||||
.set(STAFFOG_BAN.REMOVED_REASON, entry.getRemovedReason())
|
||||
.set(STAFFOG_BAN.REMOVED_TIME, entry.getRemovedTime())
|
||||
.set(STAFFOG_BAN.ACTIVE, false)
|
||||
.where(STAFFOG_BAN.ID.eq(entry.getId()))
|
||||
.execute();
|
||||
} else {
|
||||
dsl.update(STAFFOG_MUTE)
|
||||
.set(STAFFOG_MUTE.REMOVED_UUID, entry.getRemovedUuid())
|
||||
.set(STAFFOG_MUTE.REMOVED_NAME, entry.getRemovedName())
|
||||
.set(STAFFOG_MUTE.REMOVED_REASON, entry.getRemovedReason())
|
||||
.set(STAFFOG_MUTE.REMOVED_TIME, entry.getRemovedTime())
|
||||
.set(STAFFOG_MUTE.ACTIVE, false)
|
||||
.where(STAFFOG_MUTE.ID.eq(entry.getId()))
|
||||
.execute();
|
||||
}
|
||||
|
||||
if (entry.getType() == PunishType.MUTE && Bukkit.getServer().getPlayer(entry.getUuid()) != null) {
|
||||
Bukkit.getServer().getPlayer(entry.getUuid()).sendMessage(Message.format(ChatColor.GREEN + "You have been unmuted."));
|
||||
@@ -355,24 +405,6 @@ public class PunishManager {
|
||||
String data = gson.toJson(chatReportEntries);
|
||||
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
/* PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO `staffog_chatreport` (`uuid`, `by_uuid`, `time`, `messages`) VALUES (?, ?, ?, ?)",
|
||||
Statement.RETURN_GENERATED_KEYS
|
||||
);
|
||||
statement.setString(1, reported.toString());
|
||||
statement.setString(2, by.toString());
|
||||
statement.setLong(3, System.currentTimeMillis());
|
||||
statement.setString(4, data);
|
||||
|
||||
int affectedRows = statement.executeUpdate();
|
||||
|
||||
if (affectedRows == 0)
|
||||
return null;
|
||||
|
||||
ResultSet rs = statement.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
return "" + rs.getInt(1);
|
||||
}*/
|
||||
|
||||
DSLContext dsl = DSL.using(connection, SQLDialect.MYSQL);
|
||||
StaffogChatreportRecord result = dsl.insertInto(STAFFOG_CHATREPORT)
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
package uk.hotten.staffog.punish.data;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.jooq.TableLike;
|
||||
import uk.hotten.staffog.data.jooq.Tables;
|
||||
|
||||
public enum PunishType {
|
||||
|
||||
BAN(Tables.STAFFOG_BAN, "banned"),
|
||||
MUTE(Tables.STAFFOG_MUTE, "muted");
|
||||
BAN("staffog_ban", "banned"),
|
||||
MUTE("staffog_mute", "muted");
|
||||
|
||||
@Getter private TableLike table;
|
||||
@Getter private String table;
|
||||
@Getter private String broadcastMessage;
|
||||
|
||||
private PunishType(TableLike table, String broadcastMessage) {
|
||||
private PunishType(String table, String broadcastMessage) {
|
||||
this.table = table;
|
||||
this.broadcastMessage = broadcastMessage;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user