Skip to content

Commit

Permalink
Clean up ViolatedConstraintNameExtract-o-rs
Browse files Browse the repository at this point in the history
- Fix the spelling error in the name of this hierarchy
- Use delegation to an anonymous function instead of overriding in
  TemplateViolatedConstraintNameExtractor
  • Loading branch information
gavinking authored and sebersole committed Apr 20, 2020
1 parent 5687373 commit 5fc3598
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 319 deletions.
Expand Up @@ -18,8 +18,8 @@
import org.hibernate.dialect.sequence.SequenceSupport;
import org.hibernate.exception.internal.CacheSQLExceptionConversionDelegate;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.persister.entity.Lockable;
import org.hibernate.query.TemporalUnit;
import org.hibernate.query.spi.QueryEngine;
Expand All @@ -32,6 +32,8 @@
import java.sql.SQLException;
import java.sql.Types;

import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate;

/**
* Dialect for Intersystems Caché SQL 2007.1 and above.
*
Expand Down Expand Up @@ -320,19 +322,14 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
}

@Override
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER;
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
return EXTRACTOR;
}

/**
* The Cache ViolatedConstraintNameExtracter.
*/
private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
return extractUsingTemplate( "constraint (", ") violated", sqle.getMessage() );
}
};
private static final ViolatedConstraintNameExtractor EXTRACTOR =
new TemplatedViolatedConstraintNameExtractor( sqle ->
extractUsingTemplate( "constraint (", ") violated", sqle.getMessage() )
);


// Overridden informational metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
12 changes: 4 additions & 8 deletions hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java
Expand Up @@ -30,7 +30,7 @@
import org.hibernate.exception.spi.ConversionContext;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.SQLExceptionConverter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.util.ReflectHelper;
Expand Down Expand Up @@ -1803,14 +1803,10 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return null;
}

private static final ViolatedConstraintNameExtracter EXTRACTER = new ViolatedConstraintNameExtracter() {
public String extractConstraintName(SQLException sqle) {
return null;
}
};
private static final ViolatedConstraintNameExtractor EXTRACTOR = sqle -> null;

public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER;
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
return EXTRACTOR;
}


Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.LockTimeoutException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
Expand Down Expand Up @@ -511,33 +511,30 @@ public String translateDatetimeFormat(String format) {
}

@Override
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER;
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
return EXTRACTOR;
}

private static final ViolatedConstraintNameExtracter EXTRACTER = new ViolatedConstraintNameExtracter() {
private static final Pattern FOREIGN_UNIQUE_OR_PRIMARY_KEY_PATTERN =
Pattern.compile( "violation of .+? constraint \"([^\"]+)\"" );
private static final Pattern CHECK_CONSTRAINT_PATTERN =
Pattern.compile( "Operation violates CHECK constraint (.+?) on view or table" );

final Pattern foreignUniqueOrPrimaryKeyPattern = Pattern.compile( "violation of .+? constraint \"([^\"]+)\"" );
final Pattern checkConstraintPattern = Pattern.compile(
"Operation violates CHECK constraint (.+?) on view or table" );

@Override
public String extractConstraintName(SQLException sqle) {
String message = sqle.getMessage();
if ( message != null ) {
Matcher foreignUniqueOrPrimaryKeyMatcher =
foreignUniqueOrPrimaryKeyPattern.matcher( message );
if ( foreignUniqueOrPrimaryKeyMatcher.find() ) {
return foreignUniqueOrPrimaryKeyMatcher.group( 1 );
}
private static final ViolatedConstraintNameExtractor EXTRACTOR = sqle -> {
String message = sqle.getMessage();
if ( message != null ) {
Matcher foreignUniqueOrPrimaryKeyMatcher =
FOREIGN_UNIQUE_OR_PRIMARY_KEY_PATTERN.matcher( message );
if ( foreignUniqueOrPrimaryKeyMatcher.find() ) {
return foreignUniqueOrPrimaryKeyMatcher.group( 1 );
}

Matcher checkConstraintMatcher = checkConstraintPattern.matcher( message );
if ( checkConstraintMatcher.find() ) {
return checkConstraintMatcher.group( 1 );
}
Matcher checkConstraintMatcher = CHECK_CONSTRAINT_PATTERN.matcher( message );
if ( checkConstraintMatcher.find() ) {
return checkConstraintMatcher.group( 1 );
}
return null;
}
return null;
};

@Override
Expand Down
45 changes: 18 additions & 27 deletions hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java
Expand Up @@ -23,8 +23,8 @@
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.metamodel.mapping.EntityMappingType;
Expand Down Expand Up @@ -245,32 +245,23 @@ public SqmMultiTableMutationStrategy getFallbackSqmMutationStrategy(
}

@Override
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER;
}

private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
/**
* Extract the name of the violated constraint from the given SQLException.
*
* @param sqle The exception that was the result of the constraint violation.
* @return The extracted constraint name.
*/
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
String constraintName = null;
// 23000: Check constraint violation: {0}
// 23001: Unique index or primary key violation: {0}
if ( sqle.getSQLState().startsWith( "23" ) ) {
final String message = sqle.getMessage();
final int idx = message.indexOf( "violation: " );
if ( idx > 0 ) {
constraintName = message.substring( idx + "violation: ".length() );
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
return EXTRACTOR;
}

private static final ViolatedConstraintNameExtractor EXTRACTOR =
new TemplatedViolatedConstraintNameExtractor( sqle -> {
// 23000: Check constraint violation: {0}
// 23001: Unique index or primary key violation: {0}
if ( sqle.getSQLState().startsWith( "23" ) ) {
final String message = sqle.getMessage();
final int idx = message.indexOf( "violation: " );
if ( idx > 0 ) {
return message.substring( idx + "violation: ".length() );
}
}
}
return constraintName;
}
};
return null;
} );

@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
Expand Down
117 changes: 47 additions & 70 deletions hibernate-core/src/main/java/org/hibernate/dialect/HSQLDialect.java
Expand Up @@ -7,7 +7,6 @@
package org.hibernate.dialect;

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

import org.hibernate.JDBCException;
Expand All @@ -34,8 +33,8 @@
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.ReflectHelper;
Expand All @@ -56,6 +55,7 @@

import org.jboss.logging.Logger;

import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate;
import static org.hibernate.query.CastType.BOOLEAN;
import static org.hibernate.query.CastType.INTEGER;
import static org.hibernate.query.CastType.LONG;
Expand Down Expand Up @@ -327,78 +327,55 @@ public String getFromDual() {
}

@Override
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return version < 200 ? EXTRACTER_18 : EXTRACTER_20;
}

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

final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );

switch (errorCode) {
case -8:
return extractUsingTemplate(
"Integrity constraint violation ", " table:",
sqle.getMessage()
);
case -9:
return extractUsingTemplate(
"Violation of unique index: ", " in statement [",
sqle.getMessage()
);
case -104:
return extractUsingTemplate(
"Unique constraint violation: ", " in statement [",
sqle.getMessage()
);
case -177:
return extractUsingTemplate(
"Integrity constraint violation - no parent ", " table:",
sqle.getMessage()
);
}
return null;
}

};
public ViolatedConstraintNameExtractor getViolatedConstraintNameExtracter() {
return version < 200 ? EXTRACTOR_18 : EXTRACTOR_20;
}

private static final ViolatedConstraintNameExtractor EXTRACTOR_18 =
new TemplatedViolatedConstraintNameExtractor( sqle -> {
switch ( JdbcExceptionHelper.extractErrorCode( sqle ) ) {
case -8:
return extractUsingTemplate(
"Integrity constraint violation ", " table:",
sqle.getMessage()
);
case -9:
return extractUsingTemplate(
"Violation of unique index: ", " in statement [",
sqle.getMessage()
);
case -104:
return extractUsingTemplate(
"Unique constraint violation: ", " in statement [",
sqle.getMessage()
);
case -177:
return extractUsingTemplate(
"Integrity constraint violation - no parent ", " table:",
sqle.getMessage()
);
}
return null;
} );

/**
* HSQLDB 2.0 messages have changed
* messages may be localized - therefore use the common, non-locale element " table: "
*/
private static final ViolatedConstraintNameExtracter EXTRACTER_20 = new TemplatedViolatedConstraintNameExtracter() {
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {

final int errorCode = JdbcExceptionHelper.extractErrorCode( sqle );

switch (errorCode) {
case -8:
return extractUsingTemplate(
"; ", " table: ",
sqle.getMessage()
);
case -9:
return extractUsingTemplate(
"; ", " table: ",
sqle.getMessage()
);
case -104:
return extractUsingTemplate(
"; ", " table: ",
sqle.getMessage()
);
case -177:
return extractUsingTemplate(
"; ", " table: ",
sqle.getMessage()
);
}
return null;
}
};
private static final ViolatedConstraintNameExtractor EXTRACTOR_20 =
new TemplatedViolatedConstraintNameExtractor( sqle -> {
switch ( JdbcExceptionHelper.extractErrorCode( sqle ) ) {
case -8:
case -9:
case -104:
case -177:
return extractUsingTemplate(
"; ", " table: ",
sqle.getMessage()
);
}
return null;
} );

@Override
public String getSelectClauseNullString(int sqlType) {
Expand Down

0 comments on commit 5fc3598

Please sign in to comment.