Skip to content

Commit cca376e

Browse files
committed
fix typing issues related to QueryParameterBindings
+ more code cleanups
1 parent b8076d7 commit cca376e

File tree

10 files changed

+230
-253
lines changed

10 files changed

+230
-253
lines changed

hibernate-core/src/main/java/org/hibernate/internal/FilterImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.io.Serializable;
88
import java.util.Arrays;
99
import java.util.Collection;
10-
import java.util.Collections;
1110
import java.util.Map;
1211
import java.util.TreeMap;
1312
import java.util.function.Supplier;
@@ -20,6 +19,9 @@
2019

2120
import org.checkerframework.checker.nullness.qual.Nullable;
2221

22+
import static java.util.Collections.emptyMap;
23+
import static java.util.Collections.unmodifiableMap;
24+
2325
/**
2426
* Implementation of FilterImpl. FilterImpl implements the user's
2527
* view into enabled dynamic filters, allowing them to set filter parameter values.
@@ -88,7 +90,7 @@ public boolean isAppliedToLoadByKey() {
8890
}
8991

9092
public Map<String,?> getParameters() {
91-
return parameters == null ? Collections.emptyMap() : Collections.unmodifiableMap( parameters );
93+
return parameters == null ? emptyMap() : unmodifiableMap( parameters );
9294
}
9395

9496
/**

hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureParamBindings.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ public <P> ProcedureParameterBinding<P> getQueryParamerBinding(ProcedureParamete
7575

7676
@Override
7777
public <P> ProcedureParameterBinding<P> getBinding(String name) {
78-
final ProcedureParameterImplementor<P> parameter = (ProcedureParameterImplementor<P>) parameterMetadata
79-
.getQueryParameter( name );
78+
//noinspection unchecked
79+
final ProcedureParameterImplementor<P> parameter =
80+
(ProcedureParameterImplementor<P>) parameterMetadata.getQueryParameter( name );
8081
if ( parameter == null ) {
8182
throw new IllegalArgumentException( "Parameter does not exist: " + name );
8283
}
@@ -85,8 +86,9 @@ public <P> ProcedureParameterBinding<P> getBinding(String name) {
8586

8687
@Override
8788
public <P> ProcedureParameterBinding<P> getBinding(int position) {
88-
final ProcedureParameterImplementor<P> parameter = (ProcedureParameterImplementor<P>) parameterMetadata
89-
.getQueryParameter( position );
89+
//noinspection unchecked
90+
final ProcedureParameterImplementor<P> parameter =
91+
(ProcedureParameterImplementor<P>) parameterMetadata.getQueryParameter( position );
9092
if ( parameter == null ) {
9193
throw new IllegalArgumentException( "Parameter at position " + position + "does not exist" );
9294
}
@@ -95,28 +97,23 @@ public <P> ProcedureParameterBinding<P> getBinding(int position) {
9597

9698
@Override
9799
public void validate() {
98-
parameterMetadata.visitRegistrations(
99-
queryParameter -> {
100-
final ProcedureParameterImplementor procParam = (ProcedureParameterImplementor) queryParameter;
101-
if ( procParam.getMode() == ParameterMode.IN
102-
|| procParam.getMode() == ParameterMode.INOUT ) {
103-
if ( !getBinding( procParam ).isBound() ) {
104-
// depending on "pass nulls" this might be ok...
105-
// for now, just log a warning
106-
if ( procParam.getPosition() != null ) {
107-
LOG.debugf(
108-
"Procedure parameter at position %s is not bound",
109-
procParam.getPosition()
110-
);
111-
112-
}
113-
else {
114-
LOG.debugf( "Procedure parameter %s is not bound", procParam.getName() );
115-
}
116-
}
117-
}
100+
parameterMetadata.visitRegistrations( parameter -> validate( (ProcedureParameterImplementor<?>) parameter ) );
101+
}
102+
103+
private <T> void validate(ProcedureParameterImplementor<T> procParam) {
104+
final ParameterMode mode = procParam.getMode();
105+
if ( mode == ParameterMode.IN || mode == ParameterMode.INOUT ) {
106+
if ( !getBinding( procParam ).isBound() ) {
107+
// depending on "pass nulls" this might be OK - for now, just log a warning
108+
if ( procParam.getPosition() != null ) {
109+
LOG.debugf( "Procedure parameter at position %s is not bound", procParam.getPosition() );
110+
111+
}
112+
else {
113+
LOG.debugf( "Procedure parameter %s is not bound", procParam.getName() );
118114
}
119-
);
115+
}
116+
}
120117
}
121118

122119
@Override

hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureParameterMetadataImpl.java

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.procedure.internal;
66

77
import java.util.ArrayList;
8-
import java.util.Collections;
98
import java.util.HashSet;
109
import java.util.List;
1110
import java.util.Set;
@@ -17,6 +16,7 @@
1716
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1817
import org.hibernate.procedure.spi.NamedCallableQueryMemento;
1918
import org.hibernate.procedure.spi.ParameterStrategy;
19+
import org.hibernate.query.BindableType;
2020
import org.hibernate.query.QueryParameter;
2121
import org.hibernate.query.internal.QueryParameterBindingsImpl;
2222
import org.hibernate.query.procedure.ProcedureParameter;
@@ -25,6 +25,9 @@
2525
import org.hibernate.query.spi.QueryParameterBindings;
2626
import org.hibernate.query.spi.QueryParameterImplementor;
2727

28+
import static java.util.Collections.emptyList;
29+
import static java.util.Collections.emptySet;
30+
2831
/**
2932
* Specialized ParameterMetadataImplementor for callable queries implementing
3033
* expandable parameter registrations
@@ -39,29 +42,26 @@ public ProcedureParameterMetadataImpl() {
3942
}
4043

4144
public ProcedureParameterMetadataImpl(NamedCallableQueryMemento memento, SharedSessionContractImplementor session) {
42-
memento.getParameterMementos().forEach(
43-
parameterMemento -> registerParameter( parameterMemento.resolve( session ) )
44-
);
45+
memento.getParameterMementos()
46+
.forEach( parameterMemento -> registerParameter( parameterMemento.resolve( session ) ) );
4547
}
4648

47-
public void registerParameter(ProcedureParameterImplementor parameter) {
48-
if ( parameter.getName() != null ) {
49+
public void registerParameter(ProcedureParameterImplementor<?> parameter) {
50+
if ( parameter.isNamed() ) {
4951
if ( parameterStrategy == ParameterStrategy.POSITIONAL ) {
5052
throw new IllegalArgumentException( "Cannot mix named parameter with positional parameter registrations" );
5153
}
5254
parameterStrategy = ParameterStrategy.NAMED;
5355
}
54-
else if ( parameter.getPosition() != null ) {
56+
else if ( parameter.isOrdinal() ) {
5557
if ( parameterStrategy == ParameterStrategy.NAMED ) {
5658
throw new IllegalArgumentException( "Cannot mix positional parameter with named parameter registrations" );
5759
}
58-
this.parameterStrategy = ParameterStrategy.POSITIONAL;
60+
parameterStrategy = ParameterStrategy.POSITIONAL;
5961
}
6062
else {
6163
throw new IllegalArgumentException( "Unrecognized parameter type : " + parameter );
6264
}
63-
64-
6565
if ( parameters == null ) {
6666
parameters = new ArrayList<>();
6767
}
@@ -74,7 +74,7 @@ public QueryParameterBindings createBindings(SessionFactoryImplementor sessionFa
7474
}
7575

7676
@Override
77-
public void visitParameters(Consumer<QueryParameterImplementor<?>> consumer) {
77+
public void visitParameters(Consumer<QueryParameter<?>> consumer) {
7878
if ( parameters != null ) {
7979
parameters.forEach( consumer );
8080
}
@@ -93,11 +93,11 @@ public boolean hasPositionalParameters() {
9393
@Override
9494
public Set<String> getNamedParameterNames() {
9595
if ( !hasNamedParameters() ) {
96-
return Collections.emptySet();
96+
return emptySet();
9797
}
9898

9999
final Set<String> rtn = new HashSet<>();
100-
for ( ProcedureParameter parameter : parameters ) {
100+
for ( ProcedureParameter<?> parameter : parameters ) {
101101
if ( parameter.getName() != null ) {
102102
rtn.add( parameter.getName() );
103103
}
@@ -107,19 +107,12 @@ public Set<String> getNamedParameterNames() {
107107

108108
@Override
109109
public int getParameterCount() {
110-
if ( parameters == null ) {
111-
return 0;
112-
}
113-
return parameters.size();
110+
return parameters == null ? 0 : parameters.size();
114111
}
115112

116113
@Override
117-
@SuppressWarnings("SuspiciousMethodCalls")
118-
public boolean containsReference(QueryParameter parameter) {
119-
if ( parameters == null ) {
120-
return false;
121-
}
122-
return parameters.contains( parameter );
114+
public boolean containsReference(QueryParameter<?> parameter) {
115+
return parameters != null && parameters.contains( (ProcedureParameterImplementor<?>) parameter );
123116
}
124117

125118
public ParameterStrategy getParameterStrategy() {
@@ -131,14 +124,14 @@ public boolean hasAnyMatching(Predicate<QueryParameterImplementor<?>> filter) {
131124
if ( parameters.isEmpty() ) {
132125
return false;
133126
}
134-
135-
for ( ProcedureParameterImplementor<?> parameter : parameters ) {
136-
if ( filter.test( parameter ) ) {
137-
return true;
127+
else {
128+
for ( ProcedureParameterImplementor<?> parameter : parameters ) {
129+
if ( filter.test( parameter ) ) {
130+
return true;
131+
}
138132
}
133+
return false;
139134
}
140-
141-
return false;
142135
}
143136

144137
@Override
@@ -157,7 +150,6 @@ public ProcedureParameterImplementor<?> getQueryParameter(String name) {
157150
if ( parameter != null ) {
158151
return parameter;
159152
}
160-
161153
throw new IllegalArgumentException( "Named parameter [" + name + "] is not registered with this procedure call" );
162154
}
163155

@@ -177,57 +169,51 @@ public ProcedureParameterImplementor<?> getQueryParameter(int positionLabel) {
177169
if ( queryParameter != null ) {
178170
return queryParameter;
179171
}
180-
181172
throw new IllegalArgumentException( "Positional parameter [" + positionLabel + "] is not registered with this procedure call" );
182173
}
183174

184175
@Override
185-
public <P> ProcedureParameterImplementor<P> resolve(Parameter<P> param) {
186-
if ( param instanceof ProcedureParameterImplementor ) {
187-
for ( ProcedureParameterImplementor<?> p : parameters ) {
188-
if ( p == param ) {
189-
//noinspection unchecked
190-
return (ProcedureParameterImplementor<P>) p;
176+
public <P> ProcedureParameterImplementor<P> resolve(Parameter<P> parameter) {
177+
if ( parameter instanceof ProcedureParameterImplementor<P> procedureParameterImplementor ) {
178+
for ( ProcedureParameterImplementor<?> registered : parameters ) {
179+
if ( registered == parameter ) {
180+
return procedureParameterImplementor;
191181
}
192182
}
193183
}
194-
195184
return null;
196185
}
197186

198187
@Override
199188
public Set<? extends QueryParameter<?>> getRegistrations() {
200-
if ( parameters == null ) {
201-
return Collections.emptySet();
202-
}
203-
return new HashSet<>( parameters );
189+
return parameters == null ? emptySet() : new HashSet<>( parameters );
204190
}
205191

206192
@Override
207193
public List<? extends ProcedureParameterImplementor<?>> getRegistrationsAsList() {
208-
if ( parameters == null ) {
209-
return Collections.emptyList();
210-
}
211-
return parameters;
194+
return parameters == null ? emptyList() : parameters;
212195
}
213196

214197
@Override
215-
public void visitRegistrations(Consumer<? extends QueryParameter<?>> action) {
198+
public void visitRegistrations(Consumer<QueryParameter<?>> action) {
216199
if ( parameters != null ) {
217-
parameters.forEach( (Consumer) action );
200+
parameters.forEach( action );
218201
}
219202
}
220203

221204
@Override
222205
public Set<Integer> getOrdinalParameterLabels() {
223206
final HashSet<Integer> labels = new HashSet<>();
224-
visitRegistrations(
225-
p -> {
226-
if ( p.getPosition() != null ) {
227-
labels.add( p.getPosition() );
228-
}
229-
}
230-
);
207+
visitRegistrations( parameter -> {
208+
if ( parameter.getPosition() != null ) {
209+
labels.add( parameter.getPosition() );
210+
}
211+
} );
231212
return labels;
232213
}
214+
215+
@Override
216+
public <T> BindableType<T> getInferredParameterType(QueryParameter<T> parameter) {
217+
return null;
218+
}
233219
}

hibernate-core/src/main/java/org/hibernate/query/ParameterMetadata.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public interface ParameterMetadata {
7171
*/
7272
<P> QueryParameter<P> resolve(Parameter<P> param);
7373

74-
default <T> BindableType<T> getInferredParameterType(QueryParameter<T> parameter) {
75-
return null;
76-
}
74+
<T> BindableType<T> getInferredParameterType(QueryParameter<T> parameter);
7775

7876
/**
7977
* Is this parameter reference registered in this collection?
@@ -85,7 +83,7 @@ default <T> BindableType<T> getInferredParameterType(QueryParameter<T> parameter
8583
/**
8684
* General purpose visitation using functional
8785
*/
88-
void visitRegistrations(Consumer<? extends QueryParameter<?>> action);
86+
void visitRegistrations(Consumer<QueryParameter<?>> action);
8987

9088

9189
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

hibernate-core/src/main/java/org/hibernate/query/QueryParameter.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313
*/
1414
@Incubating
1515
public interface QueryParameter<T> extends jakarta.persistence.Parameter<T> {
16+
/**
17+
* Determine if this a named parameter or ordinal.
18+
*
19+
* @return {@code true} if it is a named parameter;
20+
* {@code false} if it is ordinal
21+
*
22+
* @since 7.0
23+
*/
24+
default boolean isNamed() {
25+
return getName() != null;
26+
}
27+
28+
/**
29+
* Determine if this a named parameter or ordinal.
30+
*
31+
* @return {@code true} if it is an ordinal parameter;
32+
* {@code false} if it is named
33+
*
34+
* @since 7.0
35+
*/
36+
default boolean isOrdinal() {
37+
return getPosition() != null;
38+
}
39+
1640
/**
1741
* Does this parameter allow multi-valued (collection, array, etc) binding?
1842
* <p>

0 commit comments

Comments
 (0)