Patch for H2

This commit is contained in:
HaHaWTH 2024-01-30 00:14:44 +08:00
parent 5f89964f49
commit 910ebdd5bf
2 changed files with 11 additions and 9 deletions

View File

@ -9,7 +9,7 @@ the generated config.yml file.
```yml ```yml
DataSource: DataSource:
# What type of database do you want to use? # What type of database do you want to use?
# Valid values: SQLITE, MARIADB, MYSQL, POSTGRESQL # Valid values: H2, SQLITE, MARIADB, MYSQL, POSTGRESQL
backend: SQLITE backend: SQLITE
# Enable the database caching system, should be disabled on bungeecord environments # Enable the database caching system, should be disabled on bungeecord environments
# or when a website integration is being used. # or when a website integration is being used.

View File

@ -200,7 +200,7 @@ public class H2 extends AbstractSqlDataSource {
*/ */
@Deprecated @Deprecated
private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException { 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(); return !rs.next();
} }
} }
@ -384,20 +384,22 @@ public class H2 extends AbstractSqlDataSource {
* @param st Statement object to the database * @param st Statement object to the database
*/ */
private void addRegistrationDateColumn(Statement st) throws SQLException { 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';"); + " 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 // Use the timestamp from Java to avoid timezone issues in case JVM and database are out of sync
long currentTimestamp = System.currentTimeMillis(); if (affectedRows != 0) {
int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;", long currentTimestamp = System.currentTimeMillis();
tableName, col.REGISTRATION_DATE, currentTimestamp)); int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;",
logger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, " tableName, col.REGISTRATION_DATE, currentTimestamp));
+ currentTimestamp + ", to all " + updatedRows + " rows"); logger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, "
+ currentTimestamp + ", to all " + updatedRows + " rows");
}
} }
@Override @Override
String getJdbcUrl(String dataPath, String ignored, String database) { 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) { private static void close(Connection con) {