Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.exception;

import java.sql.SQLException;

import org.hibernate.JDBCException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.exception;

import java.sql.SQLException;

import org.hibernate.JDBCException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.exception;

import java.sql.SQLException;

import org.hibernate.JDBCException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.exception;

import java.sql.SQLException;

import org.hibernate.JDBCException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public StandardSQLExceptionConverter(SQLExceptionConversionDelegate... delegates

@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
for ( SQLExceptionConversionDelegate delegate : delegates ) {
final JDBCException jdbcException = delegate.convert( sqlException, message, sql );
for ( var delegate : delegates ) {
final var jdbcException = delegate.convert( sqlException, message, sql );
if ( jdbcException != null ) {
return jdbcException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import java.sql.SQLException;
import java.util.function.Function;

import org.hibernate.internal.util.NullnessUtil;

import org.checkerframework.checker.nullness.qual.Nullable;


/**
* Extracts a violated database constraint name from an error message
* by matching the error message against a template.
Expand All @@ -27,23 +27,18 @@ public TemplatedViolatedConstraintNameExtractor(Function<SQLException,String> ex
}

@Override
public @Nullable String extractConstraintName(SQLException sqle) {
public @Nullable String extractConstraintName(SQLException exception) {
try {
String constraintName = null;

// handle nested exceptions
do {
constraintName = extractConstraintName.apply(sqle);
if (sqle.getNextException() == null
|| sqle.getNextException() == sqle) {
break;
}
else {
sqle = NullnessUtil.castNonNull( sqle.getNextException() );
while (true) {
final String constraintName = extractConstraintName.apply( exception );
final SQLException chained = exception.getNextException();
if ( constraintName != null
|| chained == null
|| chained == exception ) {
return constraintName;
}
} while (constraintName == null);

return constraintName;
exception = chained;
}
}
catch (NumberFormatException nfe) {
return null;
Expand All @@ -60,18 +55,15 @@ public TemplatedViolatedConstraintNameExtractor(Function<SQLException,String> ex
* @return The found constraint name, or null.
*/
public static @Nullable String extractUsingTemplate(String templateStart, String templateEnd, String message) {
int templateStartPosition = message.indexOf( templateStart );
final int templateStartPosition = message.indexOf( templateStart );
if ( templateStartPosition < 0 ) {
return null;
}

int start = templateStartPosition + templateStart.length();
int end = templateEnd.equals("\n") ? -1 : message.indexOf( templateEnd, start );
if ( end < 0 ) {
end = message.length();
else {
final int start = templateStartPosition + templateStart.length();
final int end = templateEnd.equals( "\n" ) ? -1 : message.indexOf( templateEnd, start );
return end < 0 ? message.substring( start ) : message.substring( start, end );
}

return message.substring( start, end );
}

}