Skip to content

Commit 7d3ee2d

Browse files
committed
rework handling of check constraint 'options' on SQL Server
1 parent 37d57cd commit 7d3ee2d

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacyDialect.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
5252
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
5353
import org.hibernate.internal.util.JdbcExceptionHelper;
54-
import org.hibernate.internal.util.StringHelper;
5554
import org.hibernate.mapping.AggregateColumn;
5655
import org.hibernate.mapping.CheckConstraint;
5756
import org.hibernate.mapping.Column;
@@ -98,6 +97,8 @@
9897
import jakarta.persistence.TemporalType;
9998

10099
import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate;
100+
import static org.hibernate.internal.util.StringHelper.isBlank;
101+
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
101102
import static org.hibernate.query.common.TemporalUnit.NANOSECOND;
102103
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.INTEGER;
103104
import static org.hibernate.type.SqlTypes.*;
@@ -1234,19 +1235,22 @@ public boolean supportsFromClauseInUpdate() {
12341235

12351236
@Override
12361237
public String getCheckConstraintString(CheckConstraint checkConstraint) {
1238+
// The only useful option is 'NOT FOR REPLICATION'
1239+
// and it comes before the constraint expression
12371240
final String constraintName = checkConstraint.getName();
1238-
return constraintName == null
1239-
?
1240-
" check " + getCheckConstraintOptions( checkConstraint ) + "(" + checkConstraint.getConstraint() + ")"
1241-
:
1242-
" constraint " + constraintName + " check " + getCheckConstraintOptions( checkConstraint ) + "(" + checkConstraint.getConstraint() + ")";
1241+
final String checkWithName =
1242+
isBlank( constraintName )
1243+
? " check"
1244+
: " constraint " + constraintName + " check";
1245+
return appendCheckConstraintOptions( checkConstraint, checkWithName )
1246+
+ " (" + checkConstraint.getConstraint() + ")";
12431247
}
12441248

1245-
private String getCheckConstraintOptions(CheckConstraint checkConstraint) {
1246-
if ( StringHelper.isNotEmpty( checkConstraint.getOptions() ) ) {
1247-
return checkConstraint.getOptions() + " ";
1248-
}
1249-
return "";
1249+
@Override
1250+
public String appendCheckConstraintOptions(CheckConstraint checkConstraint, String sqlCheckConstraint) {
1251+
return isNotEmpty( checkConstraint.getOptions() )
1252+
? sqlCheckConstraint + " " + checkConstraint.getOptions()
1253+
: sqlCheckConstraint;
12501254
}
12511255

12521256
@Override

hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.checkerframework.checker.nullness.qual.Nullable;
1111
import org.hibernate.HibernateException;
1212
import org.hibernate.Incubating;
13+
import org.hibernate.Internal;
1314
import org.hibernate.Length;
1415
import org.hibernate.LockMode;
1516
import org.hibernate.LockOptions;
@@ -6031,36 +6032,46 @@ public FunctionalDependencyAnalysisSupport getFunctionalDependencyAnalysisSuppor
60316032
*/
60326033
public String getCheckConstraintString(CheckConstraint checkConstraint) {
60336034
final String constraintName = checkConstraint.getName();
6034-
final String constraint = isBlank( constraintName )
6035-
? " check (" + checkConstraint.getConstraint() + ")"
6036-
: " constraint " + constraintName + " check (" + checkConstraint.getConstraint() + ")";
6035+
final String checkWithName =
6036+
isBlank( constraintName )
6037+
? " check"
6038+
: " constraint " + constraintName + " check";
6039+
final String constraint = checkWithName + " (" + checkConstraint.getConstraint() + ")";
60376040
return appendCheckConstraintOptions( checkConstraint, constraint );
60386041
}
60396042

60406043
/**
6041-
* Append the {@link CheckConstraint} options to SQL check sqlCheckConstraint
6044+
* Append the {@linkplain CheckConstraint#getOptions() options} to the given DDL
6045+
* string declaring a SQL {@code check} constraint.
60426046
*
60436047
* @param checkConstraint an instance of {@link CheckConstraint}
60446048
* @param sqlCheckConstraint the SQL to append the {@link CheckConstraint} options
60456049
*
60466050
* @return a SQL expression
6051+
*
6052+
* @since 7.0
60476053
*/
6054+
@Internal @Incubating
60486055
public String appendCheckConstraintOptions(CheckConstraint checkConstraint, String sqlCheckConstraint) {
60496056
return sqlCheckConstraint;
60506057
}
60516058

60526059
/**
6053-
* Does this dialect support appending table options SQL fragment at the end of the SQL Table creation statement?
6060+
* Does this dialect support appending table options SQL fragment at the end of the SQL table creation statement?
60546061
*
60556062
* @return {@code true} indicates it does; {@code false} indicates it does not;
6063+
*
6064+
* @since 7.0
60566065
*/
6066+
@Deprecated(since = "7.1", forRemoval = true)
60576067
public boolean supportsTableOptions() {
60586068
return false;
60596069
}
60606070

60616071
/**
60626072
* Does this dialect support binding {@link Types#NULL} for {@link PreparedStatement#setNull(int, int)}?
6063-
* if it does, then call of {@link PreparedStatement#getParameterMetaData()} could be eliminated for better performance.
6073+
* If it does, then the call to {@link PreparedStatement#getParameterMetaData()} may be skipped for
6074+
* better performance.
60646075
*
60656076
* @return {@code true} indicates it does; {@code false} indicates it does not;
60666077
* @see org.hibernate.type.descriptor.jdbc.ObjectNullResolvingJdbcType

hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,16 +1219,22 @@ public CallableStatementSupport getCallableStatementSupport() {
12191219

12201220
@Override
12211221
public String getCheckConstraintString(CheckConstraint checkConstraint) {
1222+
// The only useful option is 'NOT FOR REPLICATION'
1223+
// and it comes before the constraint expression
12221224
final String constraintName = checkConstraint.getName();
1223-
return isBlank( constraintName )
1224-
? " check " + getCheckConstraintOptions( checkConstraint )
1225-
+ "(" + checkConstraint.getConstraint() + ")"
1226-
: " constraint " + constraintName + " check " + getCheckConstraintOptions( checkConstraint )
1227-
+ "(" + checkConstraint.getConstraint() + ")";
1225+
final String checkWithName =
1226+
isBlank( constraintName )
1227+
? " check"
1228+
: " constraint " + constraintName + " check";
1229+
return appendCheckConstraintOptions( checkConstraint, checkWithName )
1230+
+ " (" + checkConstraint.getConstraint() + ")";
12281231
}
12291232

1230-
private String getCheckConstraintOptions(CheckConstraint checkConstraint) {
1231-
return isNotEmpty( checkConstraint.getOptions() ) ? checkConstraint.getOptions() + " " : "";
1233+
@Override
1234+
public String appendCheckConstraintOptions(CheckConstraint checkConstraint, String sqlCheckConstraint) {
1235+
return isNotEmpty( checkConstraint.getOptions() )
1236+
? sqlCheckConstraint + " " + checkConstraint.getOptions()
1237+
: sqlCheckConstraint;
12321238
}
12331239

12341240
@Override

0 commit comments

Comments
 (0)