Skip to content

Commit 89dab7d

Browse files
committed
code cleanups around native query stuff
1 parent 195d061 commit 89dab7d

File tree

5 files changed

+107
-129
lines changed

5 files changed

+107
-129
lines changed

hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java

Lines changed: 90 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -804,111 +804,114 @@ protected String expandParameterLists() {
804804
}
805805
// HHH-1123
806806
// Some DBs limit number of IN expressions. For now, warn...
807-
final SessionFactoryImplementor sessionFactory = getSessionFactory();
808-
final Dialect dialect = sessionFactory.getJdbcServices().getDialect();
809-
final boolean paddingEnabled = sessionFactory.getSessionFactoryOptions().inClauseParameterPaddingEnabled();
807+
final SessionFactoryImplementor factory = getSessionFactory();
808+
final Dialect dialect = factory.getJdbcServices().getDialect();
809+
final boolean paddingEnabled = factory.getSessionFactoryOptions().inClauseParameterPaddingEnabled();
810810
final int inExprLimit = dialect.getInExpressionCountLimit();
811811

812-
StringBuilder sb = null;
812+
StringBuilder sql = null;
813813

814814
// Handle parameter lists
815815
int offset = 0;
816816
for ( ParameterOccurrence occurrence : parameterOccurrences ) {
817-
final QueryParameterImplementor<?> queryParameter = occurrence.getParameter();
817+
final QueryParameterImplementor<?> queryParameter = occurrence.parameter();
818818
final QueryParameterBinding<?> binding = parameterBindings.getBinding( queryParameter );
819-
if ( !binding.isMultiValued() ) {
820-
continue;
821-
}
822-
final Collection<?> bindValues = binding.getBindValues();
823-
824-
final int bindValueCount = bindValues.size();
825-
final int bindValueMaxCount = determineBindValueMaxCount( paddingEnabled, inExprLimit, bindValueCount );
826-
827-
if ( inExprLimit > 0 && bindValueCount > inExprLimit ) {
828-
log.tooManyInExpressions(
829-
dialect.getClass().getName(),
830-
inExprLimit,
831-
queryParameter.getName() == null
832-
? queryParameter.getPosition().toString()
833-
: queryParameter.getName(),
834-
bindValueCount
835-
);
836-
}
837-
838-
final int sourcePosition = occurrence.getSourcePosition();
839-
if ( sourcePosition < 0 ) {
840-
continue;
841-
}
842-
843-
// check if placeholder is already immediately enclosed in parentheses
844-
// (ignoring whitespace)
845-
boolean isEnclosedInParens = true;
846-
for ( int i = sourcePosition - 1; i >= 0; i-- ) {
847-
final char ch = sqlString.charAt( i );
848-
if ( !isWhitespace( ch ) ) {
849-
isEnclosedInParens = ch == '(';
850-
break;
851-
}
852-
}
853-
if ( isEnclosedInParens ) {
854-
for ( int i = sourcePosition + 1; i < sqlString.length(); i++ ) {
855-
final char ch = sqlString.charAt( i );
856-
if ( !isWhitespace( ch ) ) {
857-
isEnclosedInParens = ch == ')';
858-
break;
819+
if ( binding.isMultiValued() ) {
820+
final int bindValueCount = binding.getBindValues().size();
821+
logTooManyExpressions( inExprLimit, bindValueCount, dialect, queryParameter );
822+
final int sourcePosition = occurrence.sourcePosition();
823+
if ( sourcePosition >= 0 ) {
824+
// check if placeholder is already immediately enclosed in parentheses
825+
// (ignoring whitespace)
826+
final boolean isEnclosedInParens = isEnclosedInParens( sourcePosition );
827+
// short-circuit for performance when only 1 value and the
828+
// placeholder is already enclosed in parentheses...
829+
if ( bindValueCount != 1 || !isEnclosedInParens ) {
830+
if ( sql == null ) {
831+
sql = new StringBuilder( sqlString.length() + 20 );
832+
sql.append( sqlString );
833+
}
834+
final int bindValueMaxCount =
835+
determineBindValueMaxCount( paddingEnabled, inExprLimit, bindValueCount );
836+
final String expansionListAsString =
837+
expandList( bindValueMaxCount, isEnclosedInParens );
838+
final int start = sourcePosition + offset;
839+
final int end = start + 1;
840+
sql.replace( start, end, expansionListAsString );
841+
offset += expansionListAsString.length() - 1;
859842
}
860843
}
861844
}
845+
}
846+
return sql == null ? sqlString : sql.toString();
847+
}
848+
849+
private static void logTooManyExpressions(
850+
int inExprLimit, int bindValueCount,
851+
Dialect dialect, QueryParameterImplementor<?> queryParameter) {
852+
if ( inExprLimit > 0 && bindValueCount > inExprLimit ) {
853+
log.tooManyInExpressions(
854+
dialect.getClass().getName(),
855+
inExprLimit,
856+
queryParameter.getName() == null
857+
? queryParameter.getPosition().toString()
858+
: queryParameter.getName(),
859+
bindValueCount
860+
);
861+
}
862+
}
862863

863-
if ( bindValueCount == 1 && isEnclosedInParens ) {
864-
// short-circuit for performance when only 1 value and the
865-
// placeholder is already enclosed in parentheses...
866-
continue;
864+
private static String expandList(int bindValueMaxCount, boolean isEnclosedInParens) {
865+
// HHH-8901
866+
if ( bindValueMaxCount == 0 ) {
867+
return isEnclosedInParens ? "null" : "(null)";
868+
}
869+
else {
870+
// Shift 1 bit instead of multiplication by 2
871+
final char[] chars;
872+
if ( isEnclosedInParens ) {
873+
chars = new char[(bindValueMaxCount << 1) - 1];
874+
chars[0] = '?';
875+
for ( int i = 1; i < bindValueMaxCount; i++ ) {
876+
final int index = i << 1;
877+
chars[index - 1] = ',';
878+
chars[index] = '?';
879+
}
867880
}
868-
869-
if ( sb == null ) {
870-
sb = new StringBuilder( sqlString.length() + 20 );
871-
sb.append( sqlString );
881+
else {
882+
chars = new char[(bindValueMaxCount << 1) + 1];
883+
chars[0] = '(';
884+
chars[1] = '?';
885+
for ( int i = 1; i < bindValueMaxCount; i++ ) {
886+
final int index = i << 1;
887+
chars[index] = ',';
888+
chars[index + 1] = '?';
889+
}
890+
chars[chars.length - 1] = ')';
872891
}
892+
return new String( chars );
893+
}
894+
}
873895

874-
final String expansionListAsString;
875-
// HHH-8901
876-
if ( bindValueMaxCount == 0 ) {
877-
expansionListAsString = isEnclosedInParens ? "null" : "(null)";
896+
private boolean isEnclosedInParens(int sourcePosition) {
897+
boolean isEnclosedInParens = true;
898+
for ( int i = sourcePosition - 1; i >= 0; i-- ) {
899+
final char ch = sqlString.charAt( i );
900+
if ( !isWhitespace( ch ) ) {
901+
isEnclosedInParens = ch == '(';
902+
break;
878903
}
879-
else {
880-
// Shift 1 bit instead of multiplication by 2
881-
final char[] chars;
882-
if ( isEnclosedInParens ) {
883-
chars = new char[( bindValueMaxCount << 1 ) - 1];
884-
chars[0] = '?';
885-
for ( int i = 1; i < bindValueMaxCount; i++ ) {
886-
final int index = i << 1;
887-
chars[index - 1] = ',';
888-
chars[index] = '?';
889-
}
890-
}
891-
else {
892-
chars = new char[( bindValueMaxCount << 1 ) + 1];
893-
chars[0] = '(';
894-
chars[1] = '?';
895-
for ( int i = 1; i < bindValueMaxCount; i++ ) {
896-
final int index = i << 1;
897-
chars[index] = ',';
898-
chars[index + 1] = '?';
899-
}
900-
chars[chars.length - 1] = ')';
904+
}
905+
if ( isEnclosedInParens ) {
906+
for ( int i = sourcePosition + 1; i < sqlString.length(); i++ ) {
907+
final char ch = sqlString.charAt( i );
908+
if ( !isWhitespace( ch ) ) {
909+
isEnclosedInParens = ch == ')';
910+
break;
901911
}
902-
903-
expansionListAsString = new String(chars);
904912
}
905-
906-
final int start = sourcePosition + offset;
907-
final int end = start + 1;
908-
sb.replace( start, end, expansionListAsString );
909-
offset += expansionListAsString.length() - 1;
910913
}
911-
return sb == null ? sqlString : sb.toString();
914+
return isEnclosedInParens;
912915
}
913916

914917
public static int determineBindValueMaxCount(boolean paddingEnabled, int inExprLimit, int bindValueCount) {

hibernate-core/src/main/java/org/hibernate/query/sql/spi/NonSelectInterpretationsKey.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package org.hibernate.query.sql.spi;
66

77
import java.util.Collection;
8-
import java.util.Collections;
98
import java.util.HashSet;
9+
import java.util.Objects;
1010

1111
import org.hibernate.query.spi.QueryInterpretationCache;
1212

13+
import static java.util.Collections.emptySet;
14+
1315
/**
1416
* QueryInterpretations key for non-select NativeQuery instances
1517
*
@@ -21,7 +23,7 @@ public class NonSelectInterpretationsKey implements QueryInterpretationCache.Key
2123

2224
public NonSelectInterpretationsKey(String sql, Collection<String> querySpaces) {
2325
this.sql = sql;
24-
this.querySpaces = querySpaces == null ? Collections.emptySet() : querySpaces;
26+
this.querySpaces = querySpaces == null ? emptySet() : querySpaces;
2527
}
2628

2729
@Override
@@ -31,33 +33,24 @@ public String getQueryString() {
3133

3234
@Override
3335
public QueryInterpretationCache.Key prepareForStore() {
34-
return new NonSelectInterpretationsKey(
35-
sql,
36-
querySpaces.isEmpty() ? Collections.emptySet() : new HashSet<>( querySpaces )
37-
);
36+
return new NonSelectInterpretationsKey( sql,
37+
querySpaces.isEmpty() ? emptySet() : new HashSet<>( querySpaces ) );
3838
}
3939

4040
@Override
4141
public boolean equals(Object o) {
4242
if ( this == o ) {
4343
return true;
4444
}
45-
if ( o == null || getClass() != o.getClass() ) {
46-
return false;
47-
}
48-
49-
NonSelectInterpretationsKey that = (NonSelectInterpretationsKey) o;
50-
51-
if ( !sql.equals( that.sql ) ) {
45+
if ( !(o instanceof NonSelectInterpretationsKey that) ) {
5246
return false;
5347
}
54-
return querySpaces.equals( that.querySpaces );
48+
return sql.equals( that.sql )
49+
&& querySpaces.equals( that.querySpaces );
5550
}
5651

5752
@Override
5853
public int hashCode() {
59-
int result = sql.hashCode();
60-
result = 31 * result + querySpaces.hashCode();
61-
return result;
54+
return Objects.hash( sql, querySpaces );
6255
}
6356
}

hibernate-core/src/main/java/org/hibernate/query/sql/spi/ParameterOccurrence.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,5 @@
99
/**
1010
* @author Christian Beikov
1111
*/
12-
public final class ParameterOccurrence {
13-
14-
private final QueryParameterImplementor<?> parameter;
15-
private final int sourcePosition;
16-
17-
public ParameterOccurrence(QueryParameterImplementor<?> parameter, int sourcePosition) {
18-
this.parameter = parameter;
19-
this.sourcePosition = sourcePosition;
20-
}
21-
22-
public QueryParameterImplementor<?> getParameter() {
23-
return parameter;
24-
}
25-
26-
public int getSourcePosition() {
27-
return sourcePosition;
28-
}
12+
public record ParameterOccurrence(QueryParameterImplementor<?> parameter, int sourcePosition) {
2913
}

hibernate-core/src/main/java/org/hibernate/query/sql/spi/SelectInterpretationsKey.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ public boolean equals(Object o) {
8484
if ( this == o ) {
8585
return true;
8686
}
87-
if ( o == null || getClass() != o.getClass() ) {
87+
if ( !(o instanceof SelectInterpretationsKey that) ) {
8888
return false;
8989
}
90-
91-
final SelectInterpretationsKey that = (SelectInterpretationsKey) o;
9290
return sql.equals( that.sql )
93-
&& Objects.equals( jdbcValuesMappingProducer, that.jdbcValuesMappingProducer )
94-
&& Objects.equals( querySpaces, that.querySpaces )
95-
&& Objects.equals( tupleTransformer, that.tupleTransformer )
96-
&& Objects.equals( resultListTransformer, that.resultListTransformer );
91+
&& Objects.equals( jdbcValuesMappingProducer, that.jdbcValuesMappingProducer )
92+
&& Objects.equals( querySpaces, that.querySpaces )
93+
&& Objects.equals( tupleTransformer, that.tupleTransformer )
94+
&& Objects.equals( resultListTransformer, that.resultListTransformer );
9795
}
9896
}

hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcParameterBindingsImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public JdbcParameterBindingsImpl(
5555
final int inExprLimit = dialect.getParameterCountLimit();
5656

5757
for ( ParameterOccurrence occurrence : parameterOccurrences ) {
58-
final QueryParameterImplementor<?> param = occurrence.getParameter();
58+
final QueryParameterImplementor<?> param = occurrence.parameter();
5959
final QueryParameterBinding<?> binding = queryParameterBindings.getBinding( param );
6060

6161
final JdbcMapping jdbcMapping;

0 commit comments

Comments
 (0)