Implement pronouns table
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-10-22 16:37:32 +02:00
parent ccc45333f8
commit 3b07c687c4
2 changed files with 32 additions and 6 deletions

View File

@ -53,7 +53,7 @@ public class NounsPicker extends JavaPlugin
logger.log("Connecting to database..."); logger.log("Connecting to database...");
String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite"; String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite";
DatabaseManager dbManager = new DatabaseManager(dbFilePath); DatabaseManager dbManager = new DatabaseManager(dbFilePath);
if(dbManager.initialize()) if(dbManager.connect() && dbManager.initDb())
{ {
logger.log("Database connection initialized!"); logger.log("Database connection initialized!");
} else { } else {

View File

@ -3,6 +3,7 @@ package wtf.beatrice.nounspicker.utils;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseManager public class DatabaseManager
{ {
@ -18,10 +19,25 @@ public class DatabaseManager
logger = new ConsoleLogger(getClass()); logger = new ConsoleLogger(getClass());
} }
public boolean initialize() public boolean connect()
{ {
String url = sqliteURL.replace("%path%", dbPath); String url = sqliteURL.replace("%path%", dbPath);
if(!close()) return false;
try {
dbConnection = DriverManager.getConnection(url);
logger.log("Database connection established!");
return true;
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
}
public boolean close()
{
if (dbConnection != null) if (dbConnection != null)
{ {
try { try {
@ -37,16 +53,26 @@ public class DatabaseManager
dbConnection = null; dbConnection = null;
} }
try {
dbConnection = DriverManager.getConnection(url);
logger.log("Database connection established!");
return true; return true;
}
public boolean initDb()
{
String sql = "CREATE TABLE IF NOT EXISTS pronouns (" +
"player text NOT NULL," +
"main_pronoun text," +
"secondary_pronoun text" +
");";
try (Statement stmt = dbConnection.createStatement()) {
// create a new table
stmt.execute(sql);
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); logger.err(e.getMessage());
return false; return false;
} }
return true;
} }
} }