Skip to content

Commit

Permalink
EasyCommands v0.8.12
Browse files Browse the repository at this point in the history
Updated MySQL driver version.
Implemented new method to verify table validity since old one wasn't working as expected.

MySQL implementation should now stable.
  • Loading branch information
FrostedCA committed Jan 12, 2024
1 parent d88ae4d commit 89ccfd4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.sedmelluq</groupId>
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/ca/tristan/easycommands/EasyCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,7 @@ private void mysqlInit() {
}

try {
if(mysql.checkConnection(0)) {
DatabaseMetaData dbm = mysql.getConnection().getMetaData();
ResultSet tables = dbm.getTables(null, null, "guildproperties", null);
if(tables.next()) {
// Means that the table is valid
// Return the code here to let the bot start and register the guilds using mysql; See more inside: ECGuild.java
Logger.log(LogType.OK, "Database GuildProperties table is valid.");
return;
}

if (!mysql.tableExists("guildproperties")) {
// Create table when missing
String table = "CREATE TABLE guildproperties ( guildId varchar(255) primary key, member_role varchar(255), bot_role varchar(255), music_channel varchar(255), log_channel varchar(255) )";
PreparedStatement preparedStatement = mysql.getConnection().prepareStatement(table);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/ca/tristan/easycommands/database/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQL {
Expand Down Expand Up @@ -52,4 +53,18 @@ public synchronized boolean checkConnection(int timeout) throws SQLException {
}
}

public boolean tableExists(String tableName) throws SQLException {
boolean tExists = false;
try (ResultSet rs = connection.getMetaData().getTables(getDatabase(), null, tableName, null)) {
while (rs.next()) {
String tName = rs.getString("TABLE_NAME");
if (tName != null && tName.equals(tableName)) {
tExists = true;
break;
}
}
}
return tExists;
}

}

0 comments on commit 89ccfd4

Please sign in to comment.