Fix multiple SQL syntax issues
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-10-24 21:51:19 +02:00
parent 27544accb1
commit e6534c54e1

View File

@ -29,7 +29,7 @@ public class DatabaseManager
logger.log("Database connection established!"); logger.log("Database connection established!");
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
return false; return false;
} }
@ -45,7 +45,7 @@ public class DatabaseManager
dbConnection.close(); dbConnection.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
return false; return false;
} }
@ -77,7 +77,7 @@ public class DatabaseManager
// execute the statement // execute the statement
stmt.execute(sql); stmt.execute(sql);
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
return false; return false;
} }
} }
@ -124,7 +124,7 @@ public class DatabaseManager
} }
} catch (SQLException e) } catch (SQLException e)
{ {
logger.err(e.getMessage()); e.printStackTrace();
} }
return -1; return -1;
@ -149,7 +149,7 @@ public class DatabaseManager
return true; return true;
} catch (SQLException e) } catch (SQLException e)
{ {
logger.err(e.getMessage()); e.printStackTrace();
} }
return false; return false;
@ -163,9 +163,10 @@ public class DatabaseManager
try (Statement stmt = dbConnection.createStatement()) { try (Statement stmt = dbConnection.createStatement()) {
ResultSet rSet = stmt.executeQuery(query); ResultSet rSet = stmt.executeQuery(query);
if(rSet.isClosed()) return -1;
return rSet.getInt("id"); return rSet.getInt("id");
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
} }
return -1; return -1;
@ -184,7 +185,7 @@ public class DatabaseManager
preparedStatement.setString(1, pronoun); preparedStatement.setString(1, pronoun);
preparedStatement.execute(); preparedStatement.execute();
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
return false; return false;
} }
@ -203,7 +204,7 @@ public class DatabaseManager
try(Statement stmt = dbConnection.createStatement()) { try(Statement stmt = dbConnection.createStatement()) {
stmt.executeUpdate(q); stmt.executeUpdate(q);
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
return false; return false;
} }
} }
@ -214,9 +215,25 @@ public class DatabaseManager
public boolean setPlayerPronouns(String playerName, String mainPronoun, String secondaryPronoun) { 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 mainPronounId = getPronounId(mainPronoun);
int secondaryPronounId = getPronounId(secondaryPronoun); int secondaryPronounId = getPronounId(secondaryPronoun);
// update player values in the table
String query = "UPDATE players " + String query = "UPDATE players " +
"SET (main_pronoun_id, secondary_pronoun_id) = (?,?) " + "SET (main_pronoun_id, secondary_pronoun_id) = (?,?) " +
"WHERE player = ?;"; "WHERE player = ?;";
@ -228,27 +245,47 @@ public class DatabaseManager
pStatement.executeUpdate(); pStatement.executeUpdate();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
} }
return false; return false;
} }
public String getPronounFormat(int pronounId) { public String getPronounFormat(int pronounId) {
String query = "SELECT format FROM pronouns" + String query = "SELECT format FROM pronouns " +
"WHERE id = " + pronounId + " " + "WHERE id = " + pronounId + " " +
"LIMIT 1;"; "LIMIT 1;";
try (Statement stmt = dbConnection.createStatement()) try (Statement stmt = dbConnection.createStatement())
{ {
ResultSet rSet = stmt.executeQuery(query); ResultSet rSet = stmt.executeQuery(query);
return rSet.getString("id"); return rSet.getString("format");
} catch (SQLException e) { } catch (SQLException e) {
logger.err(e.getMessage()); e.printStackTrace();
} }
return null; 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;
}
} }