From 910ebdd5bf035634a5981a458e06e13bafde0964 Mon Sep 17 00:00:00 2001 From: HaHaWTH Date: Tue, 30 Jan 2024 00:14:44 +0800 Subject: [PATCH] Patch for H2 --- docs/config.md | 2 +- .../java/fr/xephi/authme/datasource/H2.java | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/config.md b/docs/config.md index b83ed584..d5f46275 100644 --- a/docs/config.md +++ b/docs/config.md @@ -9,7 +9,7 @@ the generated config.yml file. ```yml DataSource: # What type of database do you want to use? - # Valid values: SQLITE, MARIADB, MYSQL, POSTGRESQL + # Valid values: H2, SQLITE, MARIADB, MYSQL, POSTGRESQL backend: SQLITE # Enable the database caching system, should be disabled on bungeecord environments # or when a website integration is being used. diff --git a/src/main/java/fr/xephi/authme/datasource/H2.java b/src/main/java/fr/xephi/authme/datasource/H2.java index 17a55118..523c1559 100644 --- a/src/main/java/fr/xephi/authme/datasource/H2.java +++ b/src/main/java/fr/xephi/authme/datasource/H2.java @@ -200,7 +200,7 @@ public class H2 extends AbstractSqlDataSource { */ @Deprecated private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException { - try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) { + try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName.toUpperCase())) { return !rs.next(); } } @@ -384,20 +384,22 @@ public class H2 extends AbstractSqlDataSource { * @param st Statement object to the database */ private void addRegistrationDateColumn(Statement st) throws SQLException { - st.executeUpdate("ALTER TABLE " + tableName + int affectedRows = st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN IF NOT EXISTS " + col.REGISTRATION_DATE + " BIGINT NOT NULL DEFAULT '0';"); // Use the timestamp from Java to avoid timezone issues in case JVM and database are out of sync - long currentTimestamp = System.currentTimeMillis(); - int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;", - tableName, col.REGISTRATION_DATE, currentTimestamp)); - logger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, " - + currentTimestamp + ", to all " + updatedRows + " rows"); + if (affectedRows != 0) { + long currentTimestamp = System.currentTimeMillis(); + int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;", + tableName, col.REGISTRATION_DATE, currentTimestamp)); + logger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, " + + currentTimestamp + ", to all " + updatedRows + " rows"); + } } @Override String getJdbcUrl(String dataPath, String ignored, String database) { - return "jdbc:h2:" + dataPath + File.separator + database + ".db"; + return "jdbc:h2:" + dataPath + File.separator + database; } private static void close(Connection con) {