Skip to content

Commit

Permalink
Refactor DatabaseProduct enum
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jul 20, 2023
1 parent 936bb0a commit e818942
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package net.javacrumbs.shedlock.provider.jdbctemplate;

import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.Configuration;
import net.javacrumbs.shedlock.support.annotation.NonNull;

import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Predicate;

public enum DatabaseProduct {

PostgresSQL("PostgreSQL"::equals),
SQLServer("Microsoft SQL Server"::equals),
Oracle("Oracle"::equals),
MySQL("MySQL"::equals),
MariaDB("MariaDB"::equals),
HQL("HSQL Database Engine"::equals),
H2("H2"::equals),
DB2(s -> s.startsWith("DB2")),
Unknown(s -> false);
POSTGRES_SQL("PostgreSQL"::equals, PostgresSqlServerTimeStatementsSource::new),
SQL_SERVER("Microsoft SQL Server"::equals, MsSqlServerTimeStatementsSource::new),
ORACLE("Oracle"::equals, OracleServerTimeStatementsSource::new),
MY_SQL("MySQL"::equals, MySqlServerTimeStatementsSource::new),
MARIA_DB("MariaDB"::equals, MySqlServerTimeStatementsSource::new),
HQL("HSQL Database Engine"::equals, HsqlServerTimeStatementsSource::new),
H2("H2"::equals, H2ServerTimeStatementsSource::new),
DB2(s -> s.startsWith("DB2"), Db2ServerTimeStatementsSource::new),
UNKNOWN(s -> false, configuration -> {
throw new UnsupportedOperationException("DB time is not supported for unknown database product");
});

private final Predicate<String> productMatcher;

DatabaseProduct(Predicate<String> productMatcher) {
private final Function<Configuration, SqlStatementsSource> serverTimeStatementsSource;

DatabaseProduct(Predicate<String> productMatcher, Function<Configuration, SqlStatementsSource> serverTimeStatementsSource) {
this.productMatcher = productMatcher;
this.serverTimeStatementsSource = serverTimeStatementsSource;
}

SqlStatementsSource getDbTimeStatementSource(Configuration configuration) {
return serverTimeStatementsSource.apply(configuration);
}

/**
Expand All @@ -27,10 +39,11 @@ public enum DatabaseProduct {
* @param productName Obtained from the JDBC connection. See java.sql.connection.getMetaData().getProductName().
* @return The matching ProductName enum
*/
public static DatabaseProduct matchProductName(final String productName) {
@NonNull
static DatabaseProduct matchProductName(final String productName) {
return Arrays.stream(DatabaseProduct.values())
.filter(databaseProduct -> databaseProduct.productMatcher.test(productName))
.findFirst().orElse(null);
.findFirst().orElse(UNKNOWN);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public JdbcTemplateLockProvider(@NonNull Configuration configuration) {

public static final class Configuration {
private final JdbcTemplate jdbcTemplate;
private final DatabaseProduct databaseProductName;
private final DatabaseProduct databaseProduct;
private final PlatformTransactionManager transactionManager;
private final String tableName;
private final TimeZone timeZone;
Expand All @@ -99,7 +99,7 @@ public static final class Configuration {

Configuration(
@NonNull JdbcTemplate jdbcTemplate,
@Nullable DatabaseProduct databaseProductName,
@Nullable DatabaseProduct databaseProduct,
@Nullable PlatformTransactionManager transactionManager,
@NonNull String tableName,
@Nullable TimeZone timeZone,
Expand All @@ -109,7 +109,7 @@ public static final class Configuration {
@Nullable Integer isolationLevel) {

this.jdbcTemplate = requireNonNull(jdbcTemplate, "jdbcTemplate can not be null");
this.databaseProductName = databaseProductName;
this.databaseProduct = databaseProduct;
this.transactionManager = transactionManager;
this.tableName = requireNonNull(tableName, "tableName can not be null");
this.timeZone = timeZone;
Expand All @@ -126,8 +126,8 @@ public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

public DatabaseProduct getDatabaseProductName() {
return databaseProductName;
public DatabaseProduct getDatabaseProduct() {
return databaseProduct;
}

public PlatformTransactionManager getTransactionManager() {
Expand Down Expand Up @@ -165,7 +165,7 @@ public static Configuration.Builder builder() {

public static final class Builder {
private JdbcTemplate jdbcTemplate;
private DatabaseProduct databaseProductName;
private DatabaseProduct databaseProduct;
private PlatformTransactionManager transactionManager;
private String tableName = DEFAULT_TABLE_NAME;
private TimeZone timeZone;
Expand Down Expand Up @@ -210,8 +210,8 @@ public Builder withDbUpperCase(final boolean dbUpperCase) {
* @param databaseProduct Database product
* @return ConfigurationBuilder
*/
public Builder withDatabaseProductName(final DatabaseProduct databaseProduct) {
this.databaseProductName = databaseProduct;
public Builder withDatabaseProduct(final DatabaseProduct databaseProduct) {
this.databaseProduct = databaseProduct;
return this;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ public Builder withIsolationLevel(int isolationLevel) {
public JdbcTemplateLockProvider.Configuration build() {
return new JdbcTemplateLockProvider.Configuration(
jdbcTemplate,
databaseProductName,
databaseProduct,
transactionManager,
dbUpperCase ? tableName.toUpperCase() : tableName,
timeZone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TimeZone;

class SqlStatementsSource {
Expand All @@ -42,48 +41,15 @@ class SqlStatementsSource {
}

static SqlStatementsSource create(Configuration configuration) {
DatabaseProduct databaseProduct = getDatabaseProductName(configuration);
DatabaseProduct databaseProduct = getDatabaseProduct(configuration);

if (configuration.getUseDbTime()) {
switch (databaseProduct) {
case PostgresSQL -> {
logger.debug("Using PostgresSqlServerTimeStatementsSource");
return new PostgresSqlServerTimeStatementsSource(configuration);
}
case SQLServer -> {
logger.debug("Using MsSqlServerTimeStatementsSource");
return new MsSqlServerTimeStatementsSource(configuration);
}
case Oracle -> {
logger.debug("Using OracleServerTimeStatementsSource");
return new OracleServerTimeStatementsSource(configuration);
}
case MySQL -> {
logger.debug("Using MySqlServerTimeStatementsSource");
return new MySqlServerTimeStatementsSource(configuration);
}
case MariaDB -> {
logger.debug("Using MySqlServerTimeStatementsSource (for MariaDB)");
return new MySqlServerTimeStatementsSource(configuration);
}
case HQL -> {
logger.debug("Using HsqlServerTimeStatementsSource");
return new HsqlServerTimeStatementsSource(configuration);
}
case H2 -> {
logger.debug("Using H2ServerTimeStatementsSource");
return new H2ServerTimeStatementsSource(configuration);
}
case DB2 -> {
logger.debug("Using Db2ServerTimeStatementsSource");
return new Db2ServerTimeStatementsSource(configuration);
}
default ->
throw new UnsupportedOperationException("DB time is not supported for '" + databaseProduct + "'");
}
var statementsSource = databaseProduct.getDbTimeStatementSource(configuration);
logger.debug("Using {}", statementsSource.getClass().getSimpleName());
return statementsSource;
} else {
if (Objects.equals(databaseProduct, DatabaseProduct.PostgresSQL)) {
logger.debug("Using PostgresSqlServerTimeStatementsSource");
if (Objects.equals(databaseProduct, DatabaseProduct.POSTGRES_SQL)) {
logger.debug("Using PostgresSqlStatementsSource");
return new PostgresSqlStatementsSource(configuration);
} else {
logger.debug("Using SqlStatementsSource");
Expand All @@ -92,16 +58,16 @@ static SqlStatementsSource create(Configuration configuration) {
}
}

private static DatabaseProduct getDatabaseProductName(final Configuration configuration) {
if (configuration.getDatabaseProductName() != null) {
return configuration.getDatabaseProductName();
private static DatabaseProduct getDatabaseProduct(final Configuration configuration) {
if (configuration.getDatabaseProduct() != null) {
return configuration.getDatabaseProduct();
}
try {
String jdbcProductName = configuration.getJdbcTemplate().execute((ConnectionCallback<String>) connection -> connection.getMetaData().getDatabaseProductName());
return Optional.ofNullable(DatabaseProduct.matchProductName(jdbcProductName)).orElse(DatabaseProduct.Unknown);
return DatabaseProduct.matchProductName(jdbcProductName);
} catch (Exception e) {
logger.debug("Can not determine database product name " + e.getMessage());
return DatabaseProduct.Unknown;
return DatabaseProduct.UNKNOWN;
}
}

Expand Down

0 comments on commit e818942

Please sign in to comment.