2022-03-24 04:10:50 +00:00
package moe.oko.Kiafumi.model ;
2022-03-28 19:38:25 +00:00
import moe.oko.Kiafumi.Kiafumi ;
2022-03-24 04:10:50 +00:00
import net.dv8tion.jda.api.entities.Guild ;
import java.sql.* ;
2022-03-26 05:04:24 +00:00
import java.util.ArrayList ;
2022-03-28 19:38:25 +00:00
import java.util.Collection ;
2022-03-26 05:04:24 +00:00
import java.util.List ;
2022-03-24 04:10:50 +00:00
2022-05-17 21:48:47 +00:00
import static moe.oko.Kiafumi.Kiafumi.* ;
2022-03-24 04:10:50 +00:00
2022-03-24 04:28:51 +00:00
/ * *
* Kiafumi DB Class
* Basically our helpful MySQL functions that pertain to data persistence .
2022-04-01 17:59:40 +00:00
* @author Kay , with moral support from Yodabird
* @apiNote I HATE SQL I HATE SQL AAAAAAAAAAAAAA
2022-03-24 04:28:51 +00:00
* /
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` " +
2022-03-30 20:58:01 +00:00
" (`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 (?,?,?,?,?, ?) " ;
2022-03-24 04:10:50 +00:00
private final String SERVERINFO_EXISTS = " select * from serverInfo where id = ? " ;
2022-03-26 05:04:24 +00:00
private final String SERVER_INFO_LOAD = " select * from serverInfo " ;
2022-03-28 19:38:25 +00:00
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = Guild_ID (non-changeable)
2022-03-30 20:58:01 +00:00
private final String SERVER_INFO_MODIFY = " UPDATE `serverInfo` SET welcomeEnabled=?, welcomeChannel=?, joinRole=?,serverProtected=?, modRole=? WHERE id = ? " ;
2022-03-24 04:10:50 +00:00
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 ) ;
2022-03-26 05:04:24 +00:00
ps . setNull ( 4 , Types . LONGVARCHAR ) ;
ps . setInt ( 5 , 0 ) ; //Falseeeee
2022-03-30 20:58:01 +00:00
ps . setNull ( 6 , Types . LONGVARCHAR ) ;
2022-03-24 04:10:50 +00:00
ps . execute ( ) ;
return true ;
} catch ( Exception ex ) {
error ( " Failed to create default server info for guild " + guild . getId ( ) ) ;
ex . printStackTrace ( ) ;
return false ;
}
}
2022-03-26 05:04:24 +00:00
2022-03-28 05:42:03 +00:00
/ * *
* Loads all the server information from MySQL into memory .
* @return - a list of all servers loaded from MySQL .
* /
2022-03-26 05:04:24 +00:00
public List < Server > loadServerInformation ( ) {
List < Server > servers = new ArrayList < > ( ) ;
try {
PreparedStatement ps = connection . prepareStatement ( SERVER_INFO_LOAD ) ;
ResultSet rs = ps . executeQuery ( ) ;
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 joinRole = rs . getString ( 4 ) ;
boolean protectionEnabled = false ;
switch ( rs . getInt ( 5 ) ) {
case 0 :
protectionEnabled = false ;
case 1 :
protectionEnabled = true ;
}
2022-03-30 20:58:01 +00:00
String modRole = rs . getString ( 6 ) ;
Server server = new Server ( id , welcomeEnabled , welcomeChannel , joinRole , protectionEnabled , modRole ) ;
2022-05-17 21:48:47 +00:00
debug ( " Loaded " + server + " from database. " ) ;
2022-03-26 05:04:24 +00:00
servers . add ( server ) ;
}
2022-03-28 17:25:03 +00:00
return servers ;
2022-03-26 05:04:24 +00:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
error ( " Failed to load server information, check stack. " ) ;
return null ;
}
}
2022-03-28 19:38:25 +00:00
2022-03-28 20:43:04 +00:00
/ * *
* Saves modified servers into persistent MySQL server .
* @return - whether the method succeeded .
* /
2022-03-28 19:38:25 +00:00
public boolean saveServerInformation ( ) {
Collection < Server > servers = Kiafumi . instance . getServerManager ( ) . getServers ( ) ;
info ( " Starting save on " + servers . size ( ) + " servers. " ) ;
try {
2022-03-30 20:58:01 +00:00
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = modRole 6 = Guild_ID (non-changeable)
2022-03-28 19:38:25 +00:00
PreparedStatement ps = connection . prepareStatement ( SERVER_INFO_MODIFY ) ;
int i = 0 ;
for ( Server server : servers ) {
if ( ! server . isModified ( ) ) { continue ; } //Skip, non modified 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 ( ) ) ;
} else {
ps . setNull ( 2 , Types . LONGVARCHAR ) ;
}
if ( server . getJoinRole ( ) ! = null ) {
ps . setString ( 3 , server . getJoinRole ( ) ) ;
} else {
ps . setNull ( 3 , Types . LONGVARCHAR ) ;
}
if ( server . isServerProtected ( ) ) {
ps . setInt ( 4 , 1 ) ;
} else {
ps . setInt ( 4 , 0 ) ;
}
2022-03-30 20:58:01 +00:00
if ( server . getModRole ( ) ! = null ) {
ps . setString ( 5 , server . getModRole ( ) ) ;
} else {
ps . setNull ( 5 , Types . LONGVARCHAR ) ;
}
ps . setString ( 6 , server . getId ( ) ) ;
2022-03-28 19:38:25 +00:00
ps . addBatch ( ) ;
i + + ;
}
info ( " Total Batches: " + i + " . Starting SQL save. " ) ;
ps . executeBatch ( ) ;
return true ;
} catch ( Exception ex ) {
error ( " Failed to persist server data. Check stack. " ) ;
ex . printStackTrace ( ) ;
return false ;
}
}
2022-03-24 04:10:50 +00:00
}