Skip to content

Commit

Permalink
HHH-9166 handle nested exceptions with TemplatedViolatedConstraintNam…
Browse files Browse the repository at this point in the history
…eExtracter
  • Loading branch information
brmeyer committed Jun 26, 2015
1 parent 1e54ee3 commit 11ae0f7
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 87 deletions.
Expand Up @@ -6,11 +6,6 @@
*/
package org.hibernate.dialect;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.LockMode;
import org.hibernate.MappingException;
import org.hibernate.cfg.Environment;
Expand Down Expand Up @@ -47,6 +42,11 @@
import org.hibernate.sql.JoinFragment;
import org.hibernate.type.StandardBasicTypes;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
* Caché 2007.1 dialect.
*
Expand Down Expand Up @@ -669,7 +669,7 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
*/
public static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
@Override
public String extractConstraintName(SQLException sqle) {
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
return extractUsingTemplate( "constraint (", ") violated", sqle.getMessage() );
}
};
Expand Down
Expand Up @@ -6,9 +6,6 @@
*/
package org.hibernate.dialect;

import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import org.hibernate.boot.TempTableDdlTransactionHandling;
Expand Down Expand Up @@ -37,9 +34,11 @@
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;

import org.jboss.logging.Logger;

import java.sql.SQLException;
import java.sql.Types;

/**
* A dialect compatible with the H2 database.
*
Expand Down Expand Up @@ -335,7 +334,8 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
* @param sqle The exception that was the result of the constraint violation.
* @return The extracted constraint name.
*/
public String extractConstraintName(SQLException sqle) {
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
String constraintName = null;
// 23000: Check constraint violation: {0}
// 23001: Unique index or primary key violation: {0}
Expand Down
Expand Up @@ -6,11 +6,6 @@
*/
package org.hibernate.dialect;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.MappingException;
Expand Down Expand Up @@ -46,9 +41,13 @@
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.type.StandardBasicTypes;

import org.jboss.logging.Logger;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

/**
* An SQL dialect compatible with HSQLDB (HyperSQL).
* <p/>
Expand Down Expand Up @@ -393,7 +392,7 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {

private static final ViolatedConstraintNameExtracter EXTRACTER_18 = new TemplatedViolatedConstraintNameExtracter() {
@Override
public String extractConstraintName(SQLException sqle) {
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
String constraintName = null;

final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );
Expand Down Expand Up @@ -430,7 +429,7 @@ else if ( errorCode == -177 ) {
*/
private static final ViolatedConstraintNameExtracter EXTRACTER_20 = new TemplatedViolatedConstraintNameExtracter() {
@Override
public String extractConstraintName(SQLException sqle) {
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
String constraintName = null;

final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );
Expand Down
Expand Up @@ -6,10 +6,6 @@
*/
package org.hibernate.dialect;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

import org.hibernate.MappingException;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.dialect.pagination.FirstLimitHandler;
Expand All @@ -26,6 +22,10 @@
import org.hibernate.internal.util.StringHelper;
import org.hibernate.type.StandardBasicTypes;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

/**
* Informix dialect.<br>
* <br>
Expand Down Expand Up @@ -223,7 +223,7 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {

private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
@Override
public String extractConstraintName(SQLException sqle) {
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
String constraintName = null;
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );

Expand Down
Expand Up @@ -6,13 +6,13 @@
*/
package org.hibernate.dialect;

import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.internal.util.JdbcExceptionHelper;

import java.sql.SQLException;
import java.sql.Types;

/**
* An SQL dialect for MySQL 5.x specific features.
*
Expand All @@ -38,17 +38,13 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {

private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {

public String extractConstraintName(SQLException sqle) {
try {
final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) ).intValue();
switch ( sqlState ) {
case 23000:
return extractUsingTemplate( " for key '", "'", sqle.getMessage() );
default:
return null;
}
}
catch ( NumberFormatException nfe ) {
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) ).intValue();
switch ( sqlState ) {
case 23000:
return extractUsingTemplate( " for key '", "'", sqle.getMessage() );
default:
return null;
}
}
Expand Down
Expand Up @@ -6,13 +6,6 @@
*/
package org.hibernate.dialect;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Locale;

import org.hibernate.JDBCException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.annotations.common.util.StringHelper;
Expand Down Expand Up @@ -47,6 +40,13 @@
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Locale;

/**
* A dialect for Oracle 8i.
*
Expand Down Expand Up @@ -469,7 +469,8 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
* @param sqle The exception that was the result of the constraint violation.
* @return The extracted constraint name.
*/
public String extractConstraintName(SQLException sqle) {
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );
if ( errorCode == 1 || errorCode == 2291 || errorCode == 2292 ) {
return extractUsingTemplate( "(", ")", sqle.getMessage() );
Expand Down
Expand Up @@ -6,12 +6,6 @@
*/
package org.hibernate.dialect;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

import org.hibernate.cfg.Environment;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.NvlFunction;
Expand All @@ -21,15 +15,20 @@
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.hql.spi.id.IdTableSupportStandardImpl;
import org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy;
import org.hibernate.hql.spi.id.local.AfterUseAction;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.type.StandardBasicTypes;

import org.jboss.logging.Logger;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;

/**
* An SQL dialect for Oracle 9 (uses ANSI-style syntax where possible).
*
Expand Down Expand Up @@ -299,7 +298,7 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {

private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
@Override
public String extractConstraintName(SQLException sqle) {
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );
if ( errorCode == 1 || errorCode == 2291 || errorCode == 2292 ) {
return extractUsingTemplate( "constraint (", ") violated", sqle.getMessage() );
Expand Down
Expand Up @@ -6,11 +6,6 @@
*/
package org.hibernate.dialect;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.JDBCException;
import org.hibernate.LockOptions;
import org.hibernate.PessimisticLockException;
Expand Down Expand Up @@ -41,6 +36,11 @@
import org.hibernate.type.descriptor.sql.ClobTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
* An SQL dialect for Postgres
* <p/>
Expand Down Expand Up @@ -406,26 +406,22 @@ public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
* Orginally contributed by Denny Bartelt.
*/
private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
public String extractConstraintName(SQLException sqle) {
try {
final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) );
switch (sqlState) {
// CHECK VIOLATION
case 23514: return extractUsingTemplate( "violates check constraint \"","\"", sqle.getMessage() );
// UNIQUE VIOLATION
case 23505: return extractUsingTemplate( "violates unique constraint \"","\"", sqle.getMessage() );
// FOREIGN KEY VIOLATION
case 23503: return extractUsingTemplate( "violates foreign key constraint \"","\"", sqle.getMessage() );
// NOT NULL VIOLATION
case 23502: return extractUsingTemplate( "null value in column \"","\" violates not-null constraint", sqle.getMessage() );
// TODO: RESTRICT VIOLATION
case 23001: return null;
// ALL OTHER
default: return null;
}
}
catch (NumberFormatException nfe) {
return null;
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
final int sqlState = Integer.valueOf( JdbcExceptionHelper.extractSqlState( sqle ) );
switch (sqlState) {
// CHECK VIOLATION
case 23514: return extractUsingTemplate( "violates check constraint \"","\"", sqle.getMessage() );
// UNIQUE VIOLATION
case 23505: return extractUsingTemplate( "violates unique constraint \"","\"", sqle.getMessage() );
// FOREIGN KEY VIOLATION
case 23503: return extractUsingTemplate( "violates foreign key constraint \"","\"", sqle.getMessage() );
// NOT NULL VIOLATION
case 23502: return extractUsingTemplate( "null value in column \"","\" violates not-null constraint", sqle.getMessage() );
// TODO: RESTRICT VIOLATION
case 23001: return null;
// ALL OTHER
default: return null;
}
}
};
Expand Down
Expand Up @@ -144,7 +144,7 @@ public ResultSet getResultSet(CallableStatement cs) throws SQLException {
* @return The extracted constraint name.
*/
@Override
public String extractConstraintName(SQLException sqle) {
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
String constraintName = null;

int errorCode = sqle.getErrorCode();
Expand Down
Expand Up @@ -6,14 +6,41 @@
*/
package org.hibernate.exception.spi;

import java.sql.SQLException;

/**
* Knows how to extract a violated constraint name from an error message based on the
* fact that the constraint name is templated within the message.
*
* @author Steve Ebersole
* @author Brett Meyer
*/
public abstract class TemplatedViolatedConstraintNameExtracter implements ViolatedConstraintNameExtracter {

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

// handle nested exceptions
do {
constraintName = doExtractConstraintName(sqle);
if (sqle.getNextException() == null
|| sqle.getNextException() == sqle) {
break;
} else {
sqle = sqle.getNextException();
}
} while (constraintName == null);

return constraintName;
} catch (NumberFormatException nfe) {
return null;
}
}

protected abstract String doExtractConstraintName(SQLException sqle) throws NumberFormatException;

/**
* Extracts the constraint name based on a template (i.e., <i>templateStart</i><b>constraintName</b><i>templateEnd</i>).
*
Expand Down

0 comments on commit 11ae0f7

Please sign in to comment.