[jOOQ/jOOQ#17333] MetaImpl exceptions should indicate table and column

if applicable, which caused the exception
This commit is contained in:
Lukas Eder 2024-09-24 08:59:06 +02:00
parent 59a7bb1658
commit 9ce2aad3f3

View File

@ -232,15 +232,31 @@ final class MetaImpl extends AbstractMeta {
return function.apply(c, s);
}
private final <R> R meta(ThrowingFunction<DatabaseMetaData, R, SQLException> function) {
private final <R> R meta(
Supplier<String> errorMessage,
ThrowingFunction<DatabaseMetaData, R, SQLException> 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> R meta0(
Supplier<String> errorMessage,
ThrowingFunction<DatabaseMetaData, R, SQLException> 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<Record> catalogs = meta(meta -> dsl().fetch(
Result<Record> 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<Record> schemas = meta(meta -> dsl().fetch(
Result<Record> 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<Record> tables = meta(meta -> {
Result<Record> 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<Record> result = meta(meta ->
Result<Record> 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<Record> 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<Record> result = meta(meta -> ctx(meta).resultQuery(sql, MetaSchema.this.getName()).fetch());
Result<Record> result = meta(
() -> "Error while fetching sequences for schema: " + this,
meta -> ctx(meta).resultQuery(sql, MetaSchema.this.getName()).fetch()
);
// TODO Support catalogs as well
Map<Record, Result<Record>> 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<Record> result = meta(meta ->
Result<Record> 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<Record> result = meta(meta ->
Result<Record> 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.<List<Index>, RuntimeException>ignoreNPE(
() -> {
Result<Record> result = removeSystemIndexes(meta(meta -> {
Result<Record> result = removeSystemIndexes(meta(() -> "Error while fetching indexes for table: " + this, meta -> {
String tableName;
@ -1174,7 +1194,7 @@ final class MetaImpl extends AbstractMeta {
Result<Record> result = meta(meta -> {
Result<Record> 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<ForeignKey<Record, ?>> getReferences() {
Result<Record> result = meta(meta -> {
Result<Record> 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<ForeignKey<?, Record>> getReferences() {
Result<Record> result = meta(meta -> {
Result<Record> result = meta(() -> "Error while fetching references for unique key: " + this, meta -> {
try (ResultSet rs = catalogSchema(
getTable().getCatalog(),
getTable().getSchema(),