Skip to content

Commit 3a28e8b

Browse files
committed
HHH-17314 - Deprecate ParamLocationRecognizer
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 26600ae commit 3a28e8b

File tree

2 files changed

+99
-31
lines changed

2 files changed

+99
-31
lines changed

hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParamLocationRecognizer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
* of journaling parameter locations.
2222
*
2323
* @author Steve Ebersole
24+
* @deprecated No longer used
2425
*/
26+
@Deprecated(since = "6.4", forRemoval = true)
2527
public class ParamLocationRecognizer implements ParameterRecognizer {
2628

2729
private Map<String, NamedParameterDescriptor<?>> namedParameterDescriptors;

hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/ParameterParserTest.java

Lines changed: 97 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
*/
77
package org.hibernate.orm.test.query.sql;
88

9+
import java.util.HashSet;
10+
import java.util.Set;
11+
912
import org.hibernate.engine.query.ParameterRecognitionException;
1013
import org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl;
11-
import org.hibernate.engine.query.spi.ParamLocationRecognizer;
1214
import org.hibernate.query.sql.internal.ParameterParser;
1315
import org.hibernate.query.sql.spi.ParameterRecognizer;
16+
import org.hibernate.testing.orm.junit.JiraKey;
1417

15-
import org.hibernate.testing.TestForIssue;
1618
import org.junit.jupiter.api.Test;
1719

1820
import static org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.NATIVE_QUERY_INTERPRETER;
@@ -30,7 +32,7 @@
3032
public class ParameterParserTest {
3133
@Test
3234
public void testFunctionAsNativeQuery() {
33-
ParamLocationRecognizer recognizer = createRecognizer();
35+
ExtendedParameterRecognizer recognizer = createRecognizer();
3436

3537
try {
3638
NATIVE_QUERY_INTERPRETER.recognizeParameters(
@@ -62,7 +64,7 @@ public void testFunctionAsNativeQuery() {
6264

6365
@Test
6466
public void testQuotedTextInComment() {
65-
ParamLocationRecognizer recognizer = createRecognizer();
67+
ExtendedParameterRecognizer recognizer = createRecognizer();
6668

6769
NATIVE_QUERY_INTERPRETER.recognizeParameters(
6870
"-- 'This' should not fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
@@ -71,16 +73,12 @@ public void testQuotedTextInComment() {
7173

7274
recognizer.validate();
7375

74-
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
75-
}
76-
77-
private ParamLocationRecognizer createRecognizer() {
78-
return new ParamLocationRecognizer( 1 );
76+
assertTrue(recognizer.getNamedParameters().contains("param"));
7977
}
8078

8179
@Test
8280
public void testContractionInComment() {
83-
ParamLocationRecognizer recognizer = createRecognizer();
81+
ExtendedParameterRecognizer recognizer = createRecognizer();
8482

8583
NATIVE_QUERY_INTERPRETER.recognizeParameters(
8684
"-- This shouldn't fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
@@ -90,12 +88,12 @@ public void testContractionInComment() {
9088
recognizer.complete();
9189
recognizer.validate();
9290

93-
assertTrue( recognizer.getNamedParameterDescriptionMap().containsKey("param"));
91+
assertTrue( recognizer.getNamedParameters().contains("param"));
9492
}
9593

9694
@Test
9795
public void testDoubleDashInCharLiteral() {
98-
ParamLocationRecognizer recognizer = createRecognizer();
96+
ExtendedParameterRecognizer recognizer = createRecognizer();
9997

10098
NATIVE_QUERY_INTERPRETER.recognizeParameters(
10199
"select coalesce(i.name, '--NONE--') as itname from Item i where i.intVal=? ",
@@ -105,12 +103,12 @@ public void testDoubleDashInCharLiteral() {
105103
recognizer.complete();
106104
recognizer.validate();
107105

108-
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() );
106+
assertEquals( 1, recognizer.getOrdinalParameterCount() );
109107
}
110108

111109
@Test
112110
public void testSlashStarInCharLiteral() {
113-
ParamLocationRecognizer recognizer = createRecognizer();
111+
ExtendedParameterRecognizer recognizer = createRecognizer();
114112

115113
NATIVE_QUERY_INTERPRETER.recognizeParameters(
116114
"select coalesce(i.name, '/*NONE') as itname from Item i where i.intVal=? ",
@@ -120,12 +118,12 @@ public void testSlashStarInCharLiteral() {
120118
recognizer.complete();
121119
recognizer.validate();
122120

123-
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() );
121+
assertEquals( 1, recognizer.getOrdinalParameterCount() );
124122
}
125123

126124
@Test
127125
public void testApostropheInOracleAlias() {
128-
ParamLocationRecognizer recognizer = createRecognizer();
126+
ExtendedParameterRecognizer recognizer = createRecognizer();
129127

130128
NATIVE_QUERY_INTERPRETER.recognizeParameters(
131129
"SELECT column as \"Table's column\" FROM Table WHERE column <> :param",
@@ -135,11 +133,11 @@ public void testApostropheInOracleAlias() {
135133
recognizer.complete();
136134
recognizer.validate();
137135

138-
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
136+
assertTrue(recognizer.getNamedParameters().contains("param"));
139137
}
140138

141139
@Test
142-
@TestForIssue( jiraKey = "HHH-1237")
140+
@JiraKey( value = "HHH-1237")
143141
public void testParseColonCharacterEscaped() {
144142
final StringBuilder captured = new StringBuilder();
145143
ParameterRecognizer recognizer = new ParameterRecognizer() {
@@ -174,35 +172,36 @@ public void complete() {
174172

175173
@Test
176174
public void testParseNamedParameter() {
177-
ParamLocationRecognizer recognizer = createRecognizer();
175+
ExtendedParameterRecognizer recognizer = createRecognizer();
178176
NATIVE_QUERY_INTERPRETER.recognizeParameters("from Stock s where s.stockCode = :stockCode and s.xyz = :pxyz", recognizer);
179177
recognizer.complete();
180178
recognizer.validate();
181179

182-
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("stockCode"));
183-
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("pxyz"));
184-
assertEquals( 2, recognizer.getNamedParameterDescriptionMap().size() );
180+
assertTrue(recognizer.getNamedParameters().contains("stockCode"));
181+
assertTrue(recognizer.getNamedParameters().contains("pxyz"));
182+
assertEquals( 2, recognizer.getNamedParameters().size() );
185183
}
186184

187185
@Test
188186
public void testParseJPAPositionalParameter() {
189-
ParamLocationRecognizer recognizer = createRecognizer();
187+
ExtendedParameterRecognizer recognizer = createRecognizer();
190188
NATIVE_QUERY_INTERPRETER.recognizeParameters("from Stock s where s.stockCode = ?1 and s.xyz = ?1", recognizer);
191189
recognizer.complete();
192190
recognizer.validate();
193191

194-
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() );
195-
192+
assertEquals( 1, recognizer.getJpaPositionalParameterCount() );
193+
194+
recognizer = createRecognizer();
196195
ParameterParser.parse("from Stock s where s.stockCode = ?1 and s.xyz = ?2", recognizer);
197196
recognizer.complete();
198197
recognizer.validate();
199198

200-
assertEquals( 2, recognizer.getOrdinalParameterDescriptionMap().size() );
199+
assertEquals( 2, recognizer.getJpaPositionalParameterCount() );
201200
}
202201

203202
@Test
204203
public void testJdbcParameterScanningEnabled() {
205-
ParamLocationRecognizer recognizer = createRecognizer();
204+
ExtendedParameterRecognizer recognizer = createRecognizer();
206205

207206
assertThrows(
208207
ParameterRecognitionException.class,
@@ -219,15 +218,15 @@ public void testJdbcParameterScanningEnabled() {
219218

220219
@Test
221220
public void testJdbcParameterScanningDisabled() {
222-
ParamLocationRecognizer recognizer = createRecognizer();
221+
ExtendedParameterRecognizer recognizer = createRecognizer();
223222

224223
// Should recognize the jpa style ordinal parameters
225224
new NativeQueryInterpreterStandardImpl( true ).recognizeParameters(
226225
"SELECT column FROM Table WHERE column.id = ?1 and column.name = ?2",
227226
recognizer
228227
);
229228
recognizer.validate();
230-
assertEquals( 2, recognizer.getOrdinalParameterDescriptionMap().size() );
229+
assertEquals( 2, recognizer.getJpaPositionalParameterCount() );
231230

232231
recognizer = createRecognizer();
233232
// Should ignore the '?'
@@ -236,8 +235,75 @@ public void testJdbcParameterScanningDisabled() {
236235
recognizer
237236
);
238237
recognizer.validate();
239-
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("id"));
240-
assertEquals( 0, recognizer.getOrdinalParameterDescriptionMap().size() );
238+
assertTrue(recognizer.getNamedParameters().contains("id"));
239+
assertEquals( 0, recognizer.getOrdinalParameterCount() );
240+
241+
}
242+
243+
private ExtendedParameterRecognizer createRecognizer() {
244+
return new TestParameterRecognizer();
245+
}
246+
247+
private interface ExtendedParameterRecognizer extends org.hibernate.query.sql.spi.ParameterRecognizer {
248+
void validate();
249+
int getOrdinalParameterCount();
250+
int getJpaPositionalParameterCount();
251+
Set<String> getNamedParameters();
252+
}
253+
254+
private final static class TestParameterRecognizer implements ExtendedParameterRecognizer {
255+
private int ordinalParameterCount = 0;
256+
private final Set<Integer> jpaPositionalParameters = new HashSet<>(2);
257+
private final Set<String> namedParameters = new HashSet<>(2);
258+
259+
@Override
260+
public void ordinalParameter(int sourcePosition) {
261+
ordinalParameterCount++;
262+
}
263+
264+
@Override
265+
public void namedParameter(String name, int sourcePosition) {
266+
namedParameters.add( name );
267+
}
241268

269+
@Override
270+
public void jpaPositionalParameter(int label, int sourcePosition) {
271+
jpaPositionalParameters.add( label );
272+
}
273+
274+
@Override
275+
public void other(char character) {
276+
// Don't care
277+
}
278+
279+
@Override
280+
public void validate() {
281+
if ( namedParameters.size() > 0 && ( ordinalParameterCount > 0 || jpaPositionalParameters.size() > 0 ) ) {
282+
throw mixedParamStrategy();
283+
}
284+
285+
if ( ordinalParameterCount > 0 && jpaPositionalParameters.size() > 0 ) {
286+
throw mixedParamStrategy();
287+
}
288+
}
289+
290+
@Override
291+
public int getOrdinalParameterCount() {
292+
return ordinalParameterCount;
293+
}
294+
295+
@Override
296+
public int getJpaPositionalParameterCount() {
297+
return jpaPositionalParameters.size();
298+
}
299+
300+
@Override
301+
public Set<String> getNamedParameters() {
302+
return namedParameters;
303+
}
304+
305+
private ParameterRecognitionException mixedParamStrategy() {
306+
throw new ParameterRecognitionException( "Mixed parameter strategies - use just one of named, positional or JPA-ordinal strategy" );
307+
}
242308
}
243309
}

0 commit comments

Comments
 (0)