From e6534c54e171f276ccb7562527d35cb9204c064d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 21:51:19 +0200 Subject: [PATCH] Fix multiple SQL syntax issues --- .../nounspicker/utils/DatabaseManager.java | 61 +++++++++++++++---- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 8c1922e..37da3f2 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -29,7 +29,7 @@ public class DatabaseManager logger.log("Database connection established!"); return true; } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); return false; } @@ -45,7 +45,7 @@ public class DatabaseManager dbConnection.close(); } } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); return false; } @@ -77,7 +77,7 @@ public class DatabaseManager // execute the statement stmt.execute(sql); } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); return false; } } @@ -124,7 +124,7 @@ public class DatabaseManager } } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); } return -1; @@ -149,7 +149,7 @@ public class DatabaseManager return true; } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); } return false; @@ -163,9 +163,10 @@ public class DatabaseManager try (Statement stmt = dbConnection.createStatement()) { ResultSet rSet = stmt.executeQuery(query); + if(rSet.isClosed()) return -1; return rSet.getInt("id"); } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); } return -1; @@ -184,7 +185,7 @@ public class DatabaseManager preparedStatement.setString(1, pronoun); preparedStatement.execute(); } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); return false; } @@ -203,7 +204,7 @@ public class DatabaseManager try(Statement stmt = dbConnection.createStatement()) { stmt.executeUpdate(q); } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); return false; } } @@ -214,9 +215,25 @@ public class DatabaseManager public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) { + // if the player is not in the table, add them + if(!hasPronouns(playerName)) + { + String query = "INSERT INTO players (player) VALUES (?);"; + + try(PreparedStatement pStatement = dbConnection.prepareStatement(query)) { + pStatement.setString(1, playerName); + pStatement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + // get pronouns ids int mainPronounId = getPronounId(mainPronoun); int secondaryPronounId = getPronounId(secondaryPronoun); + // update player values in the table String query = "UPDATE players " + "SET (main_pronoun_id, secondary_pronoun_id) = (?,?) " + "WHERE player = ?;"; @@ -228,27 +245,47 @@ public class DatabaseManager pStatement.executeUpdate(); return true; } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); } return false; } public String getPronounFormat(int pronounId) { - String query = "SELECT format FROM pronouns" + + 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"); + return rSet.getString("format"); } catch (SQLException e) { - logger.err(e.getMessage()); + e.printStackTrace(); } return null; } + public boolean hasPronouns(String playerName) { + String query = "SELECT player FROM players " + + "WHERE player = ? " + + "LIMIT 1;"; + + try (PreparedStatement pStatement = dbConnection.prepareStatement(query)) { + pStatement.setString(1, playerName); + ResultSet rSet = pStatement.executeQuery(); + if(rSet.isClosed()) return false; + while(rSet.next()) + { + if(rSet.getString("player").equals(playerName)) return true; + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return false; + } + }