@@ -59,46 +76,52 @@ public final class DatabaseProvider implements Supplier, Closeable { * * A database pool will be created using jdbc-connection-pool.
*/ - public static DatabaseProviderBuilder builder(Config config) - throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - return builder(createDataSource(config), getFlavor(config)); - } - - /** - * Use an externally configured DataSource and a Flavor. - */ - public static DatabaseProviderBuilder builder(DataSource ds, Flavor flavor) { - return new DatabaseProviderBuilderImpl(ds, () -> { - try { - return ds.getConnection(); - } catch (Exception e) { - throw new DatabaseException("unable to obtain a connection from the DataSource", e); + public static DatabaseProviderBuilder builder(Config config) { + String flavorString = config.getString("database.flavor"); + if (flavorString == null) { + String url = config.getString("database.url"); + if (url == null) { + throw new DatabaseException("You must provide database.url"); } - }, new OptionsDefault(flavor)); + flavorString = Flavor.fromJdbcUrl(url).getName(); + } + return builder(createDataSource(config), flavorString); } /** * Builder method to create and initialize an instance of this class using * the JDBC standard DriverManager method. The url parameter will be inspected - * to determine the Flavor for this database. + * to determine the Flavor for this database. Short version for + * anonymous access without user name or password. */ - public static DatabaseProviderBuilder builder(ClassLoader classLoader, String url) { - return builder(classLoader, url, Flavor.fromJdbcUrl(url), null, null, null); + public static DatabaseProviderBuilder builder(ClassLoader classLoader, + String url) { + return builder(classLoader, url, Flavor.fromJdbcUrl(url).driverClass(), null, null, null); } - private static DatabaseProviderBuilder builder(ClassLoader classLoader, - String url, - Flavor flavor, - Properties info, - String user, - String password) { - Options options = new OptionsDefault(flavor); + /** + * Builder method to create and initialize an instance of this class using + * the JDBC standard DriverManager method. + * @param classLoader the classloader + * @param url the database URL + * @param driverClass the driver class + * @param properties the driver properties or null + * @param user user name or null + * @param password user password or null + * @return a database provider builder + */ + public static DatabaseProviderBuilder builder(ClassLoader classLoader, + String url, + String driverClass, + Properties properties, + String user, + String password) { try { DriverManager.getDriver(url); } catch (SQLException e) { - if (flavor.driverClass() != null) { + if (driverClass != null) { try { - Class.forName(flavor.driverClass(), true, classLoader); + Class.forName(driverClass, true, classLoader); } catch (ClassNotFoundException e1) { throw new DatabaseException("couldn't locate JDBC driver", e1); } @@ -106,8 +129,8 @@ public final class DatabaseProvider implements Supplier, Closeable { } return new DatabaseProviderBuilderImpl(null, () -> { try { - if (info != null) { - return DriverManager.getConnection(url, info); + if (properties != null) { + return DriverManager.getConnection(url, properties); } else if (user != null) { return DriverManager.getConnection(url, user, password); } @@ -115,12 +138,10 @@ public final class DatabaseProvider implements Supplier , Closeable { } catch (Exception e) { throw new DatabaseException("unable to obtain a connection from DriverManager", e); } - }, options); + }, new OptionsDefault(Flavor.fromJdbcUrl(url))); } - private static DataSource createDataSource(Config config) - throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, - IllegalAccessException { + private static DataSource createDataSource(Config config) { String url = config.getString("database.url"); if (url == null) { throw new DatabaseException("You must provide database.url"); @@ -137,22 +158,12 @@ public final class DatabaseProvider implements Supplier , Closeable { poolConfig.setPassword(config.getString("database.password")); poolConfig.setMaximumPoolSize(config.getInteger("database.pool.size", 8)); poolConfig.setAutoCommit(false); - return new PoolDataSource(poolConfig); - } - - private static Flavor getFlavor(Config config) { - String url = config.getString("database.url"); - if (url == null) { - throw new DatabaseException("You must provide database.url"); + try { + return new PoolDataSource(poolConfig); + } catch (Exception e) { + logger.log(Level.SEVERE, e.getMessage(), e); + return null; } - Flavor flavor; - String flavorString = config.getString("database.flavor"); - if (flavorString != null) { - flavor = Flavor.valueOf(flavorString); - } else { - flavor = Flavor.fromJdbcUrl(url); - } - return flavor; } public void transact(final DbCode code) { diff --git a/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java b/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java index cf7b3fc..244cb7b 100644 --- a/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java +++ b/jdbc-query/src/main/java/org/xbib/jdbc/query/Flavor.java @@ -90,7 +90,7 @@ public interface Flavor { String failInQueue(String table); - public static Flavor fromJdbcUrl(String url) { + static Flavor fromJdbcUrl(String url) { if (url == null) { throw new DatabaseException("url must not be null"); } @@ -103,7 +103,10 @@ public interface Flavor { throw new DatabaseException("Cannot determine database flavor from url"); } - public static Flavor valueOf(String name) { + static Flavor valueOf(String name) { + if (name == null) { + throw new DatabaseException("name must not be null"); + } ServiceLoader serviceLoader = ServiceLoader.load(Flavor.class); for (Flavor flavor : serviceLoader) { if (flavor.getName().equals(name)) { diff --git a/settings.gradle b/settings.gradle index 613d067..7f394fd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,9 +1,9 @@ dependencyResolutionManagement { versionCatalogs { libs { - version('gradle', '7.5.1') - version('junit', '5.9.1') - version('testcontainers', '1.17.5') + version('gradle', '8.1.1') + version('junit', '5.9.3') + version('testcontainers', '1.18.0') library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit') library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit') library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit') @@ -12,10 +12,10 @@ dependencyResolutionManagement { library('derby', 'org.apache.derby', 'derby').version('10.16.1.1') library('hsqldb', 'org.hsqldb', 'hsqldb').version('2.7.1') library('h2', 'com.h2database', 'h2').version('2.1.214') - library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.0.8') - library('oracle', 'com.oracle.database.jdbc','ojdbc11').version('21.7.0.0') - library('postgresql', 'org.postgresql', 'postgresql').version('42.5.0') - library('mockito-core', 'org.mockito', 'mockito-core').version('4.8.1') + library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.1.3') + library('oracle', 'com.oracle.database.jdbc','ojdbc11').version('23.2.0.0') + library('postgresql', 'org.postgresql', 'postgresql').version('42.6.0') + library('mockito-core', 'org.mockito', 'mockito-core').version('5.3.1') library('testcontainers', 'org.testcontainers', 'testcontainers').versionRef('testcontainers') library('testcontainers-junit-jupiter', 'org.testcontainers', 'junit-jupiter').versionRef('testcontainers') library('testcontainers-mariadb', 'org.testcontainers', 'mariadb').versionRef('testcontainers')