kiafumi/src/main/java/moe/oko/Kiafumi/model/KiafumiDB.java

88 lines
3.2 KiB
Java
Raw Normal View History

2022-03-24 04:10:50 +00:00
package moe.oko.Kiafumi.model;
import com.mysql.cj.xdevapi.Type;
import net.dv8tion.jda.api.entities.Guild;
import java.sql.*;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.info;
2022-03-24 04:28:51 +00:00
/**
* Kiafumi DB Class
* Basically our helpful MySQL functions that pertain to data persistence.
*/
2022-03-24 04:10:50 +00:00
public class KiafumiDB {
2022-03-24 04:28:51 +00:00
//Our actual MySQL Connection, this is created on class construction.
2022-03-24 04:10:50 +00:00
private Connection connection;
2022-03-24 04:28:51 +00:00
//The prepared statement strings
2022-03-24 04:10:50 +00:00
private final String CREATE_SERVERINFO_TABLE = "CREATE TABLE IF NOT EXISTS `serverInfo`" +
"(`id` LONGTEXT NOT NULL,`welcomeEnabled` TINYINT NOT NULL,`welcomeChannel` LONGTEXT NULL);";
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id,welcomeEnabled,welcomeChannel) values (?,?,?)";
private final String SERVERINFO_EXISTS = "select * from serverInfo where id = ?";
2022-03-24 04:28:51 +00:00
/**
* KiafumiDB Config Constructor
* @param username - username to access database
* @param password - password to access database
* @param host - host for server
* @param port - port for server (3306 is default)
* @param db - database name
*/
2022-03-24 04:10:50 +00:00
public KiafumiDB(String username, String password, String host, int port, String db) {
try {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
} catch (Exception ex) {
error("Failed to initialize the MySQL instance. Contact a developer.");
ex.printStackTrace();
return;
}
initializeTables();
}
2022-03-24 04:28:51 +00:00
/**
* Table initialization function, ran on every startup.
*/
2022-03-24 04:10:50 +00:00
private void initializeTables() {
try {
connection.prepareStatement(CREATE_SERVERINFO_TABLE).execute();
} catch (Exception ex) {
ex.printStackTrace();
error("Failed to initialize SQL tables. They may need to be created manually.");
}
}
2022-03-24 04:28:51 +00:00
/**
* Creates the default server information when the bot joins a discord.
* @param guild - the guild that the bot joined
* @return whether the function succeeded.
*/
2022-03-24 04:10:50 +00:00
public boolean createServerInformation(Guild guild) {
try {
PreparedStatement prep = connection.prepareStatement(SERVERINFO_EXISTS);
prep.setInt(1, (int) guild.getIdLong());
ResultSet rsCheck = prep.executeQuery();
while (rsCheck.next()) {
//Server already exists, no need to initialize a section for it.
if(rsCheck.getInt(1) != 0) {
info("Server already existed. Skipping initialization.");
return true;
}
}
//Proceed with making defaults.
PreparedStatement ps = connection.prepareStatement(INSERT_SERVERINFO_DEFAULT);
ps.setString(1, guild.getId());
ps.setInt(2, 0); //default is false.
ps.setNull(3, Types.LONGVARCHAR);
ps.execute();
return true;
} catch (Exception ex) {
error("Failed to create default server info for guild " + guild.getId());
ex.printStackTrace();
return false;
}
}
}