@Test void translateDependentObjectsStillExistNoDoubleQuotes() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("2BP01"); when(serverErrorMessage.getDetail()) .thenReturn( "constraint my_foreign_key_constraint on table myTable depends on table myDependentTable"); EntityTypeReferencedException e = postgreSqlExceptionTranslator.translateDependentObjectsStillExist( mock(Throwable.class), new PSQLException(serverErrorMessage)); assertEquals("dependencies:myRefEntity=myEntity", e.getMessage()); }
EntityTypeReferencedException translateDependentObjectsStillExist( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String detail = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile( "constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?") .matcher(detail); Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>(); while (matcher.find()) { String tableName = matcher.group(2); String dependentTableName = matcher.group(3); String entityTypeName = tryGetEntityTypeName(tableName).orElse(null); String dependentEntityTypeName = tryGetEntityTypeName(dependentTableName).orElse(null); Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent( dependentEntityTypeName, k -> new LinkedHashSet<>()); dependentTableNames.add(entityTypeName); } if (entityTypeDependencyMap.isEmpty()) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } return new EntityTypeReferencedException(entityTypeDependencyMap, sourceThrowable); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { EntityTypeReferencedException translateDependentObjectsStillExist( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String detail = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile( "constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?") .matcher(detail); Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>(); while (matcher.find()) { String tableName = matcher.group(2); String dependentTableName = matcher.group(3); String entityTypeName = tryGetEntityTypeName(tableName).orElse(null); String dependentEntityTypeName = tryGetEntityTypeName(dependentTableName).orElse(null); Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent( dependentEntityTypeName, k -> new LinkedHashSet<>()); dependentTableNames.add(entityTypeName); } if (entityTypeDependencyMap.isEmpty()) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } return new EntityTypeReferencedException(entityTypeDependencyMap, sourceThrowable); } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { EntityTypeReferencedException translateDependentObjectsStillExist( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String detail = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile( "constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?") .matcher(detail); Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>(); while (matcher.find()) { String tableName = matcher.group(2); String dependentTableName = matcher.group(3); String entityTypeName = tryGetEntityTypeName(tableName).orElse(null); String dependentEntityTypeName = tryGetEntityTypeName(dependentTableName).orElse(null); Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent( dependentEntityTypeName, k -> new LinkedHashSet<>()); dependentTableNames.add(entityTypeName); } if (entityTypeDependencyMap.isEmpty()) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } return new EntityTypeReferencedException(entityTypeDependencyMap, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { EntityTypeReferencedException translateDependentObjectsStillExist( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String detail = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile( "constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?") .matcher(detail); Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>(); while (matcher.find()) { String tableName = matcher.group(2); String dependentTableName = matcher.group(3); String entityTypeName = tryGetEntityTypeName(tableName).orElse(null); String dependentEntityTypeName = tryGetEntityTypeName(dependentTableName).orElse(null); Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent( dependentEntityTypeName, k -> new LinkedHashSet<>()); dependentTableNames.add(entityTypeName); } if (entityTypeDependencyMap.isEmpty()) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } return new EntityTypeReferencedException(entityTypeDependencyMap, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { EntityTypeReferencedException translateDependentObjectsStillExist( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String detail = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile( "constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?") .matcher(detail); Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>(); while (matcher.find()) { String tableName = matcher.group(2); String dependentTableName = matcher.group(3); String entityTypeName = tryGetEntityTypeName(tableName).orElse(null); String dependentEntityTypeName = tryGetEntityTypeName(dependentTableName).orElse(null); Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent( dependentEntityTypeName, k -> new LinkedHashSet<>()); dependentTableNames.add(entityTypeName); } if (entityTypeDependencyMap.isEmpty()) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } return new EntityTypeReferencedException(entityTypeDependencyMap, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException);  }