Fixed bug where an empty string would be stored in the database isntead of a NULL value

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-27 22:59:02 +01:00
parent 0fc84bd2a3
commit 4ba3d48d3c

View File

@ -29,7 +29,7 @@ public final class LanguageManager {
}
public static void importLanguageIfNeeded(Connection connection) {
String storedHash = "";
String storedHash = null;
try {
logger.debug("Checking if we need to import languages again.");
storedHash = getPropertyValue(HASH_KEY);
@ -37,12 +37,12 @@ public final class LanguageManager {
String actualHash = getActualHash();
logger.debug("Actual hash is '{}'.", actualHash);
logger.info("Importing new language file.");
if (!storedHash.equals(actualHash)) importLanguageFile(connection);
if (storedHash == null || !storedHash.equals(actualHash)) importLanguageFile(connection);
logger.debug("Saving new hash in database.");
saveProperty(HASH_KEY, actualHash);
} catch (SQLException | IOException | NoSuchAlgorithmException e) {
logger.error("Cannot update the languages right now. Reason: {}", e.getMessage());
if (storedHash.isEmpty()) System.exit(7);
if (storedHash == null || storedHash.isEmpty()) System.exit(7);
logger.warn("Using language already in database. Please contact this bot's administrator to fix this issue.");
}
}
@ -103,8 +103,8 @@ public final class LanguageManager {
String insert = "INSERT INTO languages(alpha3_b, alpha3_t, alpha2, english, french) VALUES(?, ?, ?, ?, ?);";
try (PreparedStatement stmt = connection.prepareStatement(insert)) {
stmt.setString(1, alpha3b);
stmt.setString(2, alpha3t);
stmt.setString(3, alpha2b);
stmt.setString(2, alpha3t.isBlank() ? null : alpha3t);
stmt.setString(3, alpha2b.isBlank() ? null : alpha2b);
stmt.setString(4, english);
stmt.setString(5, french);
stmt.executeUpdate();