HHH-8901 Some very minor string handling optimisations

This commit is contained in:
Sanne Grinovero 2019-12-04 16:37:34 +00:00
parent ab9ae43185
commit b9b15245b7
3 changed files with 32 additions and 23 deletions

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.TypedValue;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.type.CompositeType;
@ -41,22 +42,20 @@ public class InExpression implements Criterion {
@Override
public String toSqlString( Criteria criteria, CriteriaQuery criteriaQuery ) {
final String[] columns = criteriaQuery.findColumns( propertyName, criteria );
if ( criteriaQuery.getFactory().getDialect().supportsRowValueConstructorSyntaxInInList() || columns.length <= 1 ) {
Dialect dialect = criteriaQuery.getFactory().getDialect();
if ( dialect.supportsRowValueConstructorSyntaxInInList() || columns.length <= 1 ) {
String singleValueParam = StringHelper.repeat( "?, ", columns.length - 1 ) + "?";
if ( columns.length > 1 ) {
singleValueParam = '(' + singleValueParam + ')';
}
String params = values.length > 0
final String params = values.length > 0
? StringHelper.repeat( singleValueParam + ", ", values.length - 1 ) + singleValueParam
: "";
// Deal with HHH-8901 : not all databases like to have an empty list.
: dialect.supportsEmptyInList() ? "" : "null";
String cols = String.join( ", ", columns );
if ( columns.length > 1 ) {
cols = '(' + cols + ')';
}
// HHH-8901
if ( ! criteriaQuery.getFactory().getDialect().supportsEmptyInList() && params.isEmpty() ) {
params = "null";
}
return cols + " in (" + params + ')';
}
else {

View File

@ -181,24 +181,32 @@ public class InPredicate<T>
// subquery expressions are already wrapped in parenthesis, so we only need to
// render the parenthesis here if the values represent an explicit value list
boolean isInSubqueryPredicate = getValues().size() == 1
&& Subquery.class.isInstance( getValues().get( 0 ) );
List<Expression<? extends T>> values = getValues();
boolean isInSubqueryPredicate = values.size() == 1
&& Subquery.class.isInstance( values.get( 0 ) );
if ( isInSubqueryPredicate ) {
buffer.append( ( (Renderable) getValues().get(0) ).render( renderingContext ) );
buffer.append( ( (Renderable) values.get( 0 ) ).render( renderingContext ) );
}
else {
buffer.append( '(' );
String sep = "";
for ( Expression value : getValues() ) {
buffer.append( sep )
.append( ( (Renderable) value ).render( renderingContext ) );
sep = ", ";
if ( values.isEmpty() ) {
if ( renderingContext.getDialect().supportsEmptyInList() ) {
buffer.append( "()" );
}
else {
buffer.append( "(null)" );
}
}
// HHH-8901
if ( ! renderingContext.getDialect().supportsEmptyInList() && getValues().isEmpty() ) {
buffer.append( "null" );
else {
buffer.append( '(' );
String sep = "";
for ( Expression value : values) {
buffer.append( sep )
.append( ( (Renderable) value )
.render( renderingContext ) );
sep = ", ";
}
buffer.append( ')' );
}
buffer.append( ')' );
}
return buffer.toString();
}

View File

@ -637,16 +637,18 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
parameterBindingMap.put( syntheticParam, syntheticBinding );
}
String expansionListAsString = expansionList.toString();
// HHH-8901
if ( ! dialect.supportsEmptyInList() && expansionList.length() == 0 ) {
expansionList.append( "null" );
if ( ! dialect.supportsEmptyInList() && expansionListAsString.isEmpty() ) {
expansionListAsString = "null";
}
queryString = StringHelper.replace(
beforePlaceholder,
afterPlaceholder,
sourceToken,
expansionList.toString(),
expansionListAsString,
true,
true
);