2025-01-03 04:05:16 +00:00
package jesse.keeblarcraft.ConfigMgr ;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.Statement ;
2025-01-10 02:44:17 +00:00
import jesse.keeblarcraft.Utils.CommonStructures.Pair ;
2025-01-03 04:05:16 +00:00
public class SQLConfig {
private static SQLConfig static_inst ;
2025-01-10 02:44:17 +00:00
private String dbName ;
private String dbUser ;
private String dbPass ;
private String dbAddr ;
private Boolean connected = false ;
private Connection conn = null ; // Actual connection object
2025-01-03 04:05:16 +00:00
public static SQLConfig GetInstance ( ) {
if ( static_inst = = null ) {
static_inst = new SQLConfig ( ) ;
}
return static_inst ;
}
public SQLConfig ( ) {
2025-01-10 02:44:17 +00:00
connected = false ;
2025-01-03 04:05:16 +00:00
try {
// According to some random online tutorial; this loads the driver!
Class . forName ( " com.mysql.cj.jdbc.Driver " ) ;
2025-01-10 02:44:17 +00:00
connected = true ;
2025-01-03 04:05:16 +00:00
} catch ( Exception e ) {
System . out . println ( " Could not find the proper SQL JDBC Drivers. Cannot connect to SQL Config " ) ;
e . printStackTrace ( ) ;
}
2025-01-10 02:44:17 +00:00
if ( connected ) {
2025-01-03 04:05:16 +00:00
try {
2025-01-10 02:44:17 +00:00
dbName = GeneralConfig . GetInstance ( ) . GetSQLDatabase ( ) ;
dbUser = GeneralConfig . GetInstance ( ) . GetSQLUsername ( ) ;
dbPass = GeneralConfig . GetInstance ( ) . GetSQLPassword ( ) ;
dbAddr = " jdbc:mysql:// " + GeneralConfig . GetInstance ( ) . GetSQLAddress ( ) + " / " + dbName ;
conn = DriverManager . getConnection ( dbAddr , dbUser , dbPass ) ;
2025-01-03 04:05:16 +00:00
Statement stmnt = conn . createStatement ( ) ;
String testSql = " SELECT * FROM test_table " ;
ResultSet rs = stmnt . executeQuery ( testSql ) ;
System . out . println ( " Printing out result set from test query " ) ;
while ( rs . next ( ) ) {
System . out . println ( " [RS]: " + rs . getString ( " test_name " ) ) ;
}
} catch ( SQLException e ) { }
}
}
2025-01-10 02:44:17 +00:00
private ResultSet RunCommand ( String sqlCommand ) {
ResultSet cmdResult = null ;
if ( conn ! = null ) {
try {
Statement statement = conn . createStatement ( ) ;
cmdResult = statement . executeQuery ( sqlCommand ) ;
} catch ( Exception e ) { }
}
return cmdResult ;
}
// Re-attempt the connection
public Boolean Connect ( ) {
if ( conn ! = null ) {
try {
conn . close ( ) ;
} catch ( Exception e ) { }
}
Boolean success = false ;
try {
conn = DriverManager . getConnection ( dbAddr , dbUser , dbPass ) ;
success = true ;
} catch ( Exception e ) { }
return success ;
}
public Boolean IsConnected ( ) {
return conn ! = null & & this . connected ;
}
private Boolean TableExists ( String name ) {
boolean tableExists = false ;
try ( ResultSet rs = conn . getMetaData ( ) . getTables ( null , null , name , null ) ) {
while ( rs . next ( ) ) {
String tName = rs . getString ( " TABLE_NAME " ) ;
if ( tName ! = null & & tName . equals ( name ) ) {
tableExists = true ;
break ;
}
}
} catch ( SQLException e ) { }
return tableExists ;
}
// Might fix heap pollution decay in future with enum types or something. For now we assume the user isn't horrifically stupid and will give a SQL-able type
public Boolean CreateTable ( String tableName , Pair < String , Object > . . . columnPairs ) {
Boolean success = false ;
if ( ! TableExists ( tableName . toUpperCase ( ) ) ) {
String sqlCommand = " CREATE TABLE " + tableName . toUpperCase ( ) + " ( " ;
for ( Pair < String , Object > colPair : columnPairs ) {
sqlCommand = sqlCommand + " " + colPair . GetKey ( ) + " " + String . valueOf ( colPair . GetValue ( ) ) + " , " ;
}
System . out . println ( " DEBUG STATEMENT, SQL STATEMENT: " + sqlCommand ) ;
ResultSet rs = RunCommand ( sqlCommand ) ;
}
return success ;
}
2025-01-03 04:05:16 +00:00
}