Skip to content

Commit

Permalink
Hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Jun 7, 2023
1 parent 1669b81 commit 1dbe863
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.function.BiFunction;

import javax.sql.DataSource;

Expand Down Expand Up @@ -300,36 +301,54 @@ public static DatabaseDriver fromJdbcUrl(String url) {
}

/**
* Find a {@link DatabaseDriver} for the given product name.
* @param productName product name
* @return the database driver or {@link #UNKNOWN} if not found
* Find a {@link DatabaseDriver} for the given {@code DataSource}.
* @param dataSource data source to inspect
* @return the database driver of {@link #UNKNOWN} if not found
* @since 2.6.0
*/
public static DatabaseDriver fromProductName(String productName) {
if (StringUtils.hasLength(productName)) {
for (DatabaseDriver candidate : values()) {
if (candidate.matchProductName(productName)) {
return candidate;
}
}
}
return UNKNOWN;
public static DatabaseDriver fromDataSource(DataSource dataSource) {
return fromDataSource(dataSource, null);
}

/**
* Find a {@link DatabaseDriver} for the given {@code DataSource}.
* @param dataSource data source to inspect
* @param exceptionWrapper optional function used to create the wrapped exception to
* throw if not database can be detected
* @return the database driver of {@link #UNKNOWN} if not found
* @since 2.6.0
* @since 2.7.13
*/
public static DatabaseDriver fromDataSource(DataSource dataSource) {
public static DatabaseDriver fromDataSource(DataSource dataSource,
BiFunction<String, Exception, RuntimeException> exceptionWrapper) {
try {
String productName = JdbcUtils.commonDatabaseName(
JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName));
return DatabaseDriver.fromProductName(productName);
}
catch (Exception ex) {
RuntimeException wrappedException = (exceptionWrapper != null)
? exceptionWrapper.apply("Unable to detect database type", ex) : null;
if (wrappedException != null) {
throw wrappedException;
}
return DatabaseDriver.UNKNOWN;
}
}

/**
* Find a {@link DatabaseDriver} for the given product name.
* @param productName product name
* @return the database driver or {@link #UNKNOWN} if not found
*/
public static DatabaseDriver fromProductName(String productName) {
if (StringUtils.hasLength(productName)) {
for (DatabaseDriver candidate : values()) {
if (candidate.matchProductName(productName)) {
return candidate;
}
}
}
return UNKNOWN;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,8 @@ private List<String> resolveAll(Supplier<String> platformProvider, String... val
}

private String determinePlatform(DataSource dataSource) {
DatabaseDriver databaseDriver = getDatabaseDriver(dataSource);
Assert.state(databaseDriver != DatabaseDriver.UNKNOWN, "Unable to detect database type");
DatabaseDriver databaseDriver = DatabaseDriver.fromDataSource(dataSource, IllegalStateException::new);
return this.driverMappings.getOrDefault(databaseDriver, databaseDriver.getId());
}

DatabaseDriver getDatabaseDriver(DataSource dataSource) {
return DatabaseDriver.fromDataSource(dataSource);
}

}

0 comments on commit 1dbe863

Please sign in to comment.