Reorganize SQL (BREAKING)

Removed welcomeEnabled & serverProtection
welcomeEnabled was removed due to its redundancy.
serverProtection was removed due to being out of scope.
This commit is contained in:
Anya 2022-05-21 20:19:31 -07:00
parent 67dafe50bb
commit ab72638a94
2 changed files with 42 additions and 129 deletions

View File

@ -17,17 +17,17 @@ import static moe.oko.Kiafumi.Kiafumi.*;
* @apiNote I HATE SQL I HATE SQL AAAAAAAAAAAAAA
*/
public class KiafumiDB {
//Our actual MySQL Connection, this is created on class construction.
// Our actual MySQL Connection, this is created on class construction.
private Connection connection;
//The prepared statement strings
// The prepared statement strings
private final String CREATE_SERVERINFO_TABLE = "CREATE TABLE IF NOT EXISTS `serverInfo`" +
"(`id` LONGTEXT NOT NULL,`welcomeEnabled` TINYINT NOT NULL,`welcomeChannel` LONGTEXT NULL, joinRole LONGTEXT NULL, `serverProtected` TINYINT NOT NULL, `modRole` LONGTEXT NULL);";
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id,welcomeEnabled,welcomeChannel, joinRole, serverProtected, modRole) values (?,?,?,?,?, ?)";
"(`id` LONGTEXT NOT NULL, `welcomeChannel` LONGTEXT NULL, `modChannel` LONGTEXT NULL, joinRole LONGTEXT NULL);";
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id, welcomeChannel, modChannel, joinRole) values (?,?,?,?,?, ?)";
private final String SERVERINFO_EXISTS = "select * from serverInfo where id = ?";
private final String SERVER_INFO_LOAD = "select * from serverInfo";
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = Guild_ID (non-changeable)
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeEnabled=?, welcomeChannel=?, joinRole=?,serverProtected=?, modRole=? WHERE id = ?";
// 1 = welcomeChannel, 2 = modChannel , 3 = joinRole, 4 = Guild_ID (immutable)
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeChannel=?, modChannel=?, joinRole=? WHERE id = ?";
/**
* KiafumiDB Config Constructor
@ -41,7 +41,7 @@ public class KiafumiDB {
try {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
} catch (Exception ex) {
error("Failed to initialize the MySQL instance. Contact a developer.");
error("Failed to initialize the MySQL instance.");
ex.printStackTrace();
return;
}
@ -107,24 +107,10 @@ public class KiafumiDB {
while(rs.next()) {
info("Starting new load for server: " + rs.getString(1));
String id = rs.getString(1);
boolean welcomeEnabled = false;
switch(rs.getInt(2)) {
case 0:
welcomeEnabled = false;
case 1:
welcomeEnabled = true;
}
String welcomeChannel = rs.getString(3);
String welcomeChannel = rs.getString(2);
String modChannel = rs.getString(3);
String joinRole = rs.getString(4);
boolean protectionEnabled = false;
switch(rs.getInt(5)) {
case 0:
protectionEnabled = false;
case 1:
protectionEnabled = true;
}
String modRole = rs.getString(6);
Server server = new Server(id, welcomeEnabled, welcomeChannel, joinRole, protectionEnabled, modRole);
Server server = new Server(id, welcomeChannel, modChannel, joinRole);
debug("Loaded " + server + "from database.");
servers.add(server);
}
@ -144,19 +130,19 @@ public class KiafumiDB {
Collection<Server> servers = Kiafumi.instance.getServerManager().getServers();
info("Starting save on " + servers.size() + " servers.");
try {
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = modRole 6 = Guild_ID (non-changeable)
// 1 = welcomeChannel, 2 = modChannel, 3 = joinRole, 4 = Guild_ID (immutable)
PreparedStatement ps = connection.prepareStatement(SERVER_INFO_MODIFY);
int i = 0;
for (Server server : servers) {
if(!server.isModified()) { continue; } //Skip, non modified server.
if(!server.isModified()) { continue; } // Skip, unmodified server.
info("Starting save on modified " + server);
if(server.isWelcomeEnabled()) {
ps.setInt(1, 1);
} else {
ps.setInt(1, 0);
}
if(server.getWelcomeChannel() != null) {
ps.setString(2, server.getWelcomeChannel());
ps.setString(1, server.getWelcomeChannel());
} else {
ps.setNull(1, Types.LONGVARCHAR);
}
if(server.getModChannel() != null) {
ps.setString(2, server.getModChannel());
} else {
ps.setNull(2, Types.LONGVARCHAR);
}
@ -165,17 +151,7 @@ public class KiafumiDB {
} else {
ps.setNull(3, Types.LONGVARCHAR);
}
if(server.isServerProtected()) {
ps.setInt(4, 1);
} else {
ps.setInt(4, 0);
}
if(server.getModRole() != null) {
ps.setString(5, server.getModRole());
} else {
ps.setNull(5, Types.LONGVARCHAR);
}
ps.setString(6, server.getId());
ps.setString(4, server.getId());
ps.addBatch();
i++;
}

View File

@ -11,18 +11,14 @@ import javax.annotation.Nullable;
* @implNote This class is where all server info is stored per-server, so be liberal with additions.
*/
public class Server {
//Guild ID
// Guild ID
private String id;
//Whether the welcome feature is enabled (Join/Leave Logs)
private boolean welcomeEnabled;
//If enabled, the channel for logs to be posted to.
// The channel for welcome logs to be posted to.
private String welcomeChannel;
//The role to be assigned on join, if null ignored.
// The role to be assigned on join, if null ignored.
private String joinRole;
//TODO Whether the server has protections enabled (do protections)
private boolean serverProtected;
//Moderation role, used for /mod
private String modRole;
private String modChannel;
//If the server has been modified in memory, for saving persistently.
private boolean modified;
@ -32,28 +28,22 @@ public class Server {
*/
public Server(String id) {
this.id = id;
this.welcomeEnabled = false;
this.welcomeChannel = null;
this.modChannel = null;
this.joinRole = null;
this.serverProtected = false;
this.modRole = null;
this.modified = false;
}
/**
* Database Constructor
* @param id - id of the server
* @param welcomeEnabled - is welcome enabled for the server
* @param welcomeChannel - channel for welcome messages, if enabled
* @param serverProtected - is the server under protection rules. (/archive, /stop, etc.)
*/
public Server(String id, boolean welcomeEnabled, @Nullable String welcomeChannel, @Nullable String joinRole, boolean serverProtected, String modRole) {
public Server(String id, @Nullable String welcomeChannel, @Nullable String joinRole, String modChannel) {
this.id = id;
this.welcomeEnabled = welcomeEnabled;
this.welcomeChannel = welcomeChannel;
this.modChannel = modChannel;
this.joinRole = joinRole;
this.serverProtected = serverProtected;
this.modRole = modRole;
this.modified = false;
}
@ -61,28 +51,17 @@ public class Server {
public String getJoinRole() { return joinRole; }
public String getModRole() { return modRole; }
public boolean isWelcomeEnabled() {
return welcomeEnabled;
}
public String getModChannel() { return modChannel; }
public String getWelcomeChannel() {
return welcomeChannel;
}
public boolean isServerProtected() { return serverProtected; }
public void setJoinRole(String joinRole) {
this.modified = true;
this.joinRole = joinRole;
}
public void setWelcomeEnabled(boolean welcomeEnabled) {
this.modified = true;
this.welcomeEnabled = welcomeEnabled;
}
public void setWelcomeChannel(String welcomeChannel) {
this.modified = true;
this.welcomeChannel = welcomeChannel;
@ -114,28 +93,12 @@ public class Server {
* @return - the value (if applicable) to return
*/
public String getOptionByString(String string) {
switch(string.toLowerCase()) {
case "welcomeenabled":
if(!welcomeEnabled) {
return "False";
} else {
return "True";
}
case "joinrole":
return joinRole;
case "welcomechannel":
return welcomeChannel;
case "serverprotected":
if(!serverProtected) {
return "False";
} else {
return "True";
}
case "modrole":
return modRole;
default:
return "INVALID";
}
return switch (string.toLowerCase()) {
case "joinrole" -> joinRole;
case "welcomechannel" -> welcomeChannel;
case "modchannel" -> modChannel;
default -> "INVALID";
};
}
/**
@ -145,21 +108,15 @@ public class Server {
*/
public String resetOptionByString(String name) {
switch(name.toLowerCase()) {
case "welcomeenabled":
welcomeEnabled = false;
return "Reset Join/Leave logs to default.";
case "joinrole":
joinRole = null;
return "Auto-role on join is now set to disabled (Default).";
case "welcomechannel":
welcomeChannel = null;
return "Welcome channel is now unset.";
case "serverprotected":
serverProtected = true;
return "Server protection set to default.";
case "modrole":
modRole = null;
return "Mod role is now unset.";
case "modchannel":
modChannel = null;
return "Mod channel is now unset.";
default:
return "INVALID SETTING";
}
@ -172,18 +129,8 @@ public class Server {
* @return - whether the name and value were valid and the option was set.
*/
public String setOptionByString(String name, String value) {
modified = true; //If this is being used just tell it it's been modified.
modified = true; // If this is being used set it to modified.
switch (name.toLowerCase()) {
case "welcomeenabled":
if(value.equalsIgnoreCase("false")) {
welcomeEnabled = false;
return "Join/Leave logs are now disabled.";
} else if(value.equalsIgnoreCase("true")) {
welcomeEnabled = true;
return "Join/Leave logs are now enabled. Make sure you set the `welcomeChannel` setting.";
} else {
return "Bad Value, use either true or false";
}
case "joinrole":
try {
if (Kiafumi.JDA.getRoleById(value) == null) {
@ -206,23 +153,13 @@ public class Server {
} catch (Exception ex) {
return "Bad Value";
}
case "serverprotected":
if(value.equalsIgnoreCase("false")) {
serverProtected = false;
return "Server protection is now disabled.";
} else if(value.equalsIgnoreCase("true")) {
serverProtected = true;
return "Server protection is now enabled.";
} else {
return "Bad Value, use either true or false";
}
case "modrole":
case "modchannel":
try {
if(Kiafumi.JDA.getRoleById(value) == null) {
return "That role ID is invalid.";
} else {
modRole = value;
return "Successfully set modRole ID to " + modRole;
modChannel = value;
return "Successfully set modChannel ID to " + modChannel;
}
} catch (Exception ex) {
return "Bad Value";
@ -234,7 +171,7 @@ public class Server {
@Override
public String toString() {
return "Server{id=" + this.id + ",welcomeEnabled=" + welcomeEnabled + ",welcomeChannel="+welcomeChannel+
",joinrole=" + joinRole +",serverProtected="+serverProtected+ ",modRole=" + modRole +"}";
return "Server [id=" + this.id + ",welcomeChannel="+welcomeChannel+
",joinrole=" + joinRole + ",modChannel=" + modChannel +"]";
}
}