diff --git a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java index 8fdb1e65a5..2ab22d4679 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java @@ -232,15 +232,31 @@ final class MetaImpl extends AbstractMeta { return function.apply(c, s); } - private final R meta(ThrowingFunction function) { + private final R meta( + Supplier errorMessage, + ThrowingFunction function + ) { if (databaseMetaData == null) - return dsl().connectionResult(connection -> function.apply(connection.getMetaData())); + return dsl().connectionResult(connection -> meta0(errorMessage, function, connection.getMetaData())); + else + return meta0(errorMessage, function, databaseMetaData); + } + private static final R meta0( + Supplier errorMessage, + ThrowingFunction function, + DatabaseMetaData databaseMetaData + ) { try { return function.apply(databaseMetaData); } catch (SQLException e) { - throw new DataAccessException("Error while running MetaFunction", e); + String m = errorMessage.get(); + + if (StringUtils.isEmpty(m)) + throw new DataAccessException("Error querying DatabaseMetaData", e); + else + throw new DataAccessException("Error querying DatabaseMetaData: " + m, e); } } @@ -296,7 +312,7 @@ final class MetaImpl extends AbstractMeta { // [#2760] MySQL JDBC confuses "catalog" and "schema" if (!inverseSchemaCatalog) { - Result catalogs = meta(meta -> dsl().fetch( + Result catalogs = meta(() -> "Error while fetching catalogs", meta -> dsl().fetch( meta.getCatalogs(), SQLDataType.VARCHAR // TABLE_CATALOG )); @@ -378,7 +394,8 @@ final class MetaImpl extends AbstractMeta { if (!inverseSchemaCatalog) { - Schemas schemas = meta(meta -> { + Schemas schemas = meta(() -> "Error while fetching schemas for catalog: " + this, + meta -> { @@ -416,7 +433,7 @@ final class MetaImpl extends AbstractMeta { // [#2760] MySQL JDBC confuses "catalog" and "schema" else { - Result schemas = meta(meta -> dsl().fetch( + Result schemas = meta(() -> "Error while fetching catalogs", meta -> dsl().fetch( meta.getCatalogs(), SQLDataType.VARCHAR // TABLE_CATALOG )); @@ -455,7 +472,7 @@ final class MetaImpl extends AbstractMeta { if (empty) return emptyList(); - Result tables = meta(meta -> { + Result tables = meta(() -> "Error while fetching tables for schema: " + this, meta -> { String[] types; switch (family()) { @@ -604,7 +621,7 @@ final class MetaImpl extends AbstractMeta { String sql = sqlF.apply(dialect()); if (sql != null) { - Result result = meta(meta -> + Result result = meta(() -> "", meta -> withCatalog(DSL.catalog(catalog), ctx(meta), ctx -> ctx.resultQuery( sql, @@ -774,7 +791,7 @@ final class MetaImpl extends AbstractMeta { } private final Result getColumns0(final String catalog, final String schema, final String table) { - return meta(meta -> { + return meta(() -> "Error while fetching columns for table " + table + " in schema " + this, meta -> { try (ResultSet rs = catalogSchema(catalog, schema, (c, s) -> meta.getColumns(c, s, table, "%"))) { // Work around a bug in the SQL Server JDBC driver by // coercing data types to the expected types @@ -827,7 +844,10 @@ final class MetaImpl extends AbstractMeta { : M_SEQUENCES(dialect()); if (sql != null) { - Result result = meta(meta -> ctx(meta).resultQuery(sql, MetaSchema.this.getName()).fetch()); + Result result = meta( + () -> "Error while fetching sequences for schema: " + this, + meta -> ctx(meta).resultQuery(sql, MetaSchema.this.getName()).fetch() + ); // TODO Support catalogs as well Map> groups = result.intoGroups(new Field[] { result.field(0), result.field(1) }); @@ -850,7 +870,7 @@ final class MetaImpl extends AbstractMeta { String sql = M_SOURCES(dialect()); if (sql != null) { - Result result = meta(meta -> + Result result = meta(() -> "Error while fetching sources for schema " + this, meta -> withCatalog(getCatalog(), ctx(meta), ctx -> ctx.resultQuery(patchSchema(sql), MetaSchema.this.getName()).fetch() ) @@ -887,7 +907,7 @@ final class MetaImpl extends AbstractMeta { String sql = M_COMMENTS(dialect()); if (sql != null) { - Result result = meta(meta -> + Result result = meta(() -> "Error while fetching comments for schema: " + this, meta -> withCatalog(getCatalog(), ctx(meta), ctx -> ctx.resultQuery(sql, MetaSchema.this.getName()).fetch() ) @@ -1037,7 +1057,7 @@ final class MetaImpl extends AbstractMeta { // See https://github.com/h2database/h2database/issues/3236 return Tools., RuntimeException>ignoreNPE( () -> { - Result result = removeSystemIndexes(meta(meta -> { + Result result = removeSystemIndexes(meta(() -> "Error while fetching indexes for table: " + this, meta -> { String tableName; @@ -1174,7 +1194,7 @@ final class MetaImpl extends AbstractMeta { - Result result = meta(meta -> { + Result result = meta(() -> "Error while fetching primary key for table: " + this, meta -> { try (ResultSet rs = catalogSchema(getCatalog(), getSchema(), (c, s) -> meta.getPrimaryKeys(c, s, getName()))) { return dsl().fetch( rs, @@ -1196,7 +1216,7 @@ final class MetaImpl extends AbstractMeta { @Override @SuppressWarnings("unchecked") public final List> getReferences() { - Result result = meta(meta -> { + Result result = meta(() -> "Error while fetching references for table: " + this, meta -> { try (ResultSet rs = catalogSchema(getCatalog(), getSchema(), (c, s) -> meta.getImportedKeys(c, s, getName()))) { return dsl().fetch( rs, @@ -1641,7 +1661,7 @@ final class MetaImpl extends AbstractMeta { @Override @SuppressWarnings("unchecked") public final List> getReferences() { - Result result = meta(meta -> { + Result result = meta(() -> "Error while fetching references for unique key: " + this, meta -> { try (ResultSet rs = catalogSchema( getTable().getCatalog(), getTable().getSchema(),