Finish implementing add command
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Bea 2022-10-24 20:56:16 +02:00
parent e7e57506c4
commit a78ef16095
2 changed files with 52 additions and 5 deletions

View File

@ -23,8 +23,19 @@ public class NounsAddSubCommand
if(Cache.dbManager.getPronounId(pronoun) >= 0) if(Cache.dbManager.getPronounId(pronoun) >= 0)
{ {
sender.sendMessage("Pronoun already exists!"); sender.sendMessage("Pronoun already exists!");
return true;
} }
return false; String format = args[2];
if(Cache.dbManager.addNewPronoun(pronoun, format)) {
int id = Cache.dbManager.getPronounId(pronoun);
sender.sendMessage("New pronoun " + pronoun + " added with id " + id + "! Format is " + format);
} else {
sender.sendMessage("Error creating new pronoun! Check console for details.");
}
return true;
} }
} }

View File

@ -100,10 +100,10 @@ public class DatabaseManager
* -------------------------------------- * --------------------------------------
* | pronoun | id | format | * | pronoun | id | format |
* -------------------------------------- * --------------------------------------
* | she | 1 | She | * | she | 1 | She |
* | her | 2 | Her | * | her | 2 | Her |
* | he | 3 | He | * | he | 3 | He |
* ------------------------------------- * --------------------------------------
* *
*/ */
@ -129,6 +129,42 @@ public class DatabaseManager
return -1; return -1;
} }
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;
}
} }