From a78ef160953f058d018eb3034367e36340499b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 20:56:16 +0200 Subject: [PATCH] Finish implementing add command --- .../subcommands/NounsAddSubCommand.java | 13 +++++- .../nounspicker/utils/DatabaseManager.java | 44 +++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java index 445b361..857e293 100644 --- a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java @@ -23,8 +23,19 @@ public class NounsAddSubCommand if(Cache.dbManager.getPronounId(pronoun) >= 0) { 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; } } diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 3e24839..2b61210 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -100,10 +100,10 @@ public class DatabaseManager * -------------------------------------- * | pronoun | id | format | * -------------------------------------- - * | she | 1 | She | - * | her | 2 | Her | - * | he | 3 | He | - * ------------------------------------- + * | she | 1 | She | + * | her | 2 | Her | + * | he | 3 | He | + * -------------------------------------- * */ @@ -129,6 +129,42 @@ public class DatabaseManager 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; + } + }