NounsPicker/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java

253 lines
7.2 KiB
Java

package wtf.beatrice.nounspicker.utils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DatabaseManager
{
private final static String sqliteURL = "jdbc:sqlite:%path%";
private Connection dbConnection = null;
private ConsoleLogger logger;
private final String dbPath;
public DatabaseManager(String dbPath)
{
this.dbPath = dbPath;
logger = new ConsoleLogger(getClass());
}
public boolean connect()
{
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)
{
try {
if(!dbConnection.isClosed())
{
dbConnection.close();
}
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
dbConnection = null;
}
return true;
}
public boolean initDb()
{
List<String> newTables = new ArrayList<>();
newTables.add("CREATE TABLE IF NOT EXISTS players (" +
"player text NOT NULL," +
"main_pronoun_id INTEGER DEFAULT -1," +
"secondary_pronoun_id INTEGER DEFAULT -1" +
");");
newTables.add("CREATE TABLE IF NOT EXISTS pronouns (" +
"pronoun text," +
"format text," +
"\"id integer" +
");");
for(String sql : newTables)
{
try (Statement stmt = dbConnection.createStatement()) {
// execute the statement
stmt.execute(sql);
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
}
return true;
}
/**
* DB STRUCTURE
* TABLE 1: PLAYERS
* -----------------------------------------------------------------
* | name | main_pronoun_id | secondary_pronoun_id |
* -----------------------------------------------------------------
* | AstroBea | 1 | 2 |
* | Notch | 3 | 1 |
* -----------------------------------------------------------------
*
*
* TABLE 2: PRONOUNS
* --------------------------------------
* | pronoun | id | format |
* --------------------------------------
* | she | 1 | She |
* | her | 2 | Her |
* | he | 3 | He |
* --------------------------------------
*
*/
public int getPronounId(String pronoun) {
String query = "SELECT id " +
"FROM pronouns " +
"WHERE pronoun = ? " +
"LIMIT 1;";
try (PreparedStatement pStatement = dbConnection.prepareStatement(query)) {
pStatement.setString(1, pronoun);
ResultSet resultSet = pStatement.executeQuery();
while(resultSet.next())
{
return resultSet.getInt("id");
}
} catch (SQLException e)
{
logger.err(e.getMessage());
}
return -1;
}
public boolean isPronounValid(String pronoun) {
return getPronounId(pronoun) >= 0;
}
public boolean addNewPronoun(String pronoun, String format) {
String query = "INSERT INTO pronouns " +
"( pronoun, format, id) VALUES " +
" (?, ?, ?);";
int pronounId = getHighestPronounId() + 1;
try (PreparedStatement pStatement = dbConnection.prepareStatement(query)) {
pStatement.setString(1, pronoun);
pStatement.setString(2, format);
pStatement.setInt(3, pronounId);
return pStatement.execute();
} catch (SQLException e)
{
logger.err(e.getMessage());
}
return false;
}
public int getHighestPronounId() {
String query = "SELECT id" +
"FROM pronouns " +
"ORDER BY id DESC " +
"LIMIT 1";
try (Statement stmt = dbConnection.createStatement()) {
ResultSet rSet = stmt.executeQuery(query);
return rSet.getInt("id");
} catch (SQLException e) {
logger.err(e.getMessage());
}
return -1;
}
public boolean deletePronoun(String pronoun) {
// get the pronoun id (so we can remove it from users)
int pronounId = getPronounId(pronoun);
// delete it from the list of pronouns
String query = "DELETE FROM pronouns " +
"WHERE pronoun = ?;";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, pronoun);
preparedStatement.execute();
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
// set players who had this pronoun to -1, which means no pronoun
List<String> queries = new ArrayList<>();
queries.add("UPDATE players " +
"SET main_pronoun_id = -1 " +
"WHERE main_pronoun_id = " + pronounId + ";");
queries.add("UPDATE players " +
"SET secondary_pronoun_id = -1 " +
"WHERE secondary_pronoun_id = " + pronounId + ";");
for(String q : queries) {
try(Statement stmt = dbConnection.createStatement()) {
stmt.execute(q);
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
}
// announce success
return true;
}
public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) {
int mainPronounId = getPronounId(mainPronoun);
int secondaryPronounId = getPronounId(secondaryPronoun);
String query = "UPDATE players " +
"SET (main_pronoun_id, secondary_pronoun_id) = (?,?) " +
"WHERE player = ?;";
try(PreparedStatement pStatement = dbConnection.prepareStatement(query)) {
pStatement.setInt(1, mainPronounId);
pStatement.setInt(2, secondaryPronounId);
pStatement.setString(3, playerName);
return pStatement.execute();
} catch (SQLException e) {
logger.err(e.getMessage());
}
return false;
}
public String getPronounFormat(int pronounId) {
String query = "SELECT format FROM pronouns" +
"WHERE id = " + pronounId + " " +
"LIMIT 1;";
try (Statement stmt = dbConnection.createStatement())
{
ResultSet rSet = stmt.executeQuery(query);
return rSet.getString("id");
} catch (SQLException e) {
logger.err(e.getMessage());
}
return null;
}
}