From 8aef3ceb06402a1de2649b20823fbb273c18573d Mon Sep 17 00:00:00 2001 From: HaHaWTH Date: Tue, 30 Jan 2024 13:42:10 +0800 Subject: [PATCH] Patch H2 and Update H2 to 2.2.224 --- pom.xml | 9 ++----- .../java/fr/xephi/authme/datasource/H2.java | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 945b4552..60587cd2 100644 --- a/pom.xml +++ b/pom.xml @@ -294,7 +294,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.4.1 + 3.5.1 shaded-jar @@ -350,11 +350,6 @@ org.h2 fr.xephi.authme.libs.org.h2 - - - - - @@ -1133,7 +1128,7 @@ com.h2database h2 - 2.1.214 + 2.2.224 compile diff --git a/src/main/java/fr/xephi/authme/datasource/H2.java b/src/main/java/fr/xephi/authme/datasource/H2.java index 523c1559..88cda765 100644 --- a/src/main/java/fr/xephi/authme/datasource/H2.java +++ b/src/main/java/fr/xephi/authme/datasource/H2.java @@ -194,17 +194,26 @@ public class H2 extends AbstractSqlDataSource { } /** - * Checks if a column is missing. - * @deprecated Not working in H2 database(always true), replaced by statement "IF NOT EXISTS" - * @return true/false + * Checks if a column is missing in the specified table. + * @param columnName the name of the column to look for + * @return true if the column is missing, false if it exists + * @throws SQLException if an error occurs while executing SQL or accessing the result set + * @deprecated Not work in H2, it always returns true */ @Deprecated private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException { - try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName.toUpperCase())) { - return !rs.next(); - } + String query = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?"; +// try (PreparedStatement preparedStatement = con.prepareStatement(query)) { +// preparedStatement.setString(1, tableName); +// preparedStatement.setString(2, columnName.toUpperCase()); +// try (ResultSet rs = preparedStatement.executeQuery()) { +// return !rs.next(); +// } +// } + return true; } + @Override public void reload() { close(con); @@ -384,11 +393,9 @@ public class H2 extends AbstractSqlDataSource { * @param st Statement object to the database */ private void addRegistrationDateColumn(Statement st) throws SQLException { - int affectedRows = st.executeUpdate("ALTER TABLE " + tableName + int affect = 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 - if (affectedRows != 0) { + if (affect > 0) { long currentTimestamp = System.currentTimeMillis(); int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;", tableName, col.REGISTRATION_DATE, currentTimestamp));