6
6
*/
7
7
package org .hibernate .orm .test .query .sql ;
8
8
9
+ import java .util .HashSet ;
10
+ import java .util .Set ;
11
+
9
12
import org .hibernate .engine .query .ParameterRecognitionException ;
10
13
import org .hibernate .engine .query .internal .NativeQueryInterpreterStandardImpl ;
11
- import org .hibernate .engine .query .spi .ParamLocationRecognizer ;
12
14
import org .hibernate .query .sql .internal .ParameterParser ;
13
15
import org .hibernate .query .sql .spi .ParameterRecognizer ;
16
+ import org .hibernate .testing .orm .junit .JiraKey ;
14
17
15
- import org .hibernate .testing .TestForIssue ;
16
18
import org .junit .jupiter .api .Test ;
17
19
18
20
import static org .hibernate .engine .query .internal .NativeQueryInterpreterStandardImpl .NATIVE_QUERY_INTERPRETER ;
30
32
public class ParameterParserTest {
31
33
@ Test
32
34
public void testFunctionAsNativeQuery () {
33
- ParamLocationRecognizer recognizer = createRecognizer ();
35
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
34
36
35
37
try {
36
38
NATIVE_QUERY_INTERPRETER .recognizeParameters (
@@ -62,7 +64,7 @@ public void testFunctionAsNativeQuery() {
62
64
63
65
@ Test
64
66
public void testQuotedTextInComment () {
65
- ParamLocationRecognizer recognizer = createRecognizer ();
67
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
66
68
67
69
NATIVE_QUERY_INTERPRETER .recognizeParameters (
68
70
"-- 'This' should not fail the test.\n " + "SELECT column FROM Table WHERE column <> :param" ,
@@ -71,16 +73,12 @@ public void testQuotedTextInComment() {
71
73
72
74
recognizer .validate ();
73
75
74
- assertTrue (recognizer .getNamedParameterDescriptionMap ().containsKey ("param" ));
75
- }
76
-
77
- private ParamLocationRecognizer createRecognizer () {
78
- return new ParamLocationRecognizer ( 1 );
76
+ assertTrue (recognizer .getNamedParameters ().contains ("param" ));
79
77
}
80
78
81
79
@ Test
82
80
public void testContractionInComment () {
83
- ParamLocationRecognizer recognizer = createRecognizer ();
81
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
84
82
85
83
NATIVE_QUERY_INTERPRETER .recognizeParameters (
86
84
"-- This shouldn't fail the test.\n " + "SELECT column FROM Table WHERE column <> :param" ,
@@ -90,12 +88,12 @@ public void testContractionInComment() {
90
88
recognizer .complete ();
91
89
recognizer .validate ();
92
90
93
- assertTrue ( recognizer .getNamedParameterDescriptionMap ().containsKey ("param" ));
91
+ assertTrue ( recognizer .getNamedParameters ().contains ("param" ));
94
92
}
95
93
96
94
@ Test
97
95
public void testDoubleDashInCharLiteral () {
98
- ParamLocationRecognizer recognizer = createRecognizer ();
96
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
99
97
100
98
NATIVE_QUERY_INTERPRETER .recognizeParameters (
101
99
"select coalesce(i.name, '--NONE--') as itname from Item i where i.intVal=? " ,
@@ -105,12 +103,12 @@ public void testDoubleDashInCharLiteral() {
105
103
recognizer .complete ();
106
104
recognizer .validate ();
107
105
108
- assertEquals ( 1 , recognizer .getOrdinalParameterDescriptionMap (). size () );
106
+ assertEquals ( 1 , recognizer .getOrdinalParameterCount () );
109
107
}
110
108
111
109
@ Test
112
110
public void testSlashStarInCharLiteral () {
113
- ParamLocationRecognizer recognizer = createRecognizer ();
111
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
114
112
115
113
NATIVE_QUERY_INTERPRETER .recognizeParameters (
116
114
"select coalesce(i.name, '/*NONE') as itname from Item i where i.intVal=? " ,
@@ -120,12 +118,12 @@ public void testSlashStarInCharLiteral() {
120
118
recognizer .complete ();
121
119
recognizer .validate ();
122
120
123
- assertEquals ( 1 , recognizer .getOrdinalParameterDescriptionMap (). size () );
121
+ assertEquals ( 1 , recognizer .getOrdinalParameterCount () );
124
122
}
125
123
126
124
@ Test
127
125
public void testApostropheInOracleAlias () {
128
- ParamLocationRecognizer recognizer = createRecognizer ();
126
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
129
127
130
128
NATIVE_QUERY_INTERPRETER .recognizeParameters (
131
129
"SELECT column as \" Table's column\" FROM Table WHERE column <> :param" ,
@@ -135,11 +133,11 @@ public void testApostropheInOracleAlias() {
135
133
recognizer .complete ();
136
134
recognizer .validate ();
137
135
138
- assertTrue (recognizer .getNamedParameterDescriptionMap ().containsKey ("param" ));
136
+ assertTrue (recognizer .getNamedParameters ().contains ("param" ));
139
137
}
140
138
141
139
@ Test
142
- @ TestForIssue ( jiraKey = "HHH-1237" )
140
+ @ JiraKey ( value = "HHH-1237" )
143
141
public void testParseColonCharacterEscaped () {
144
142
final StringBuilder captured = new StringBuilder ();
145
143
ParameterRecognizer recognizer = new ParameterRecognizer () {
@@ -174,35 +172,36 @@ public void complete() {
174
172
175
173
@ Test
176
174
public void testParseNamedParameter () {
177
- ParamLocationRecognizer recognizer = createRecognizer ();
175
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
178
176
NATIVE_QUERY_INTERPRETER .recognizeParameters ("from Stock s where s.stockCode = :stockCode and s.xyz = :pxyz" , recognizer );
179
177
recognizer .complete ();
180
178
recognizer .validate ();
181
179
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 () );
185
183
}
186
184
187
185
@ Test
188
186
public void testParseJPAPositionalParameter () {
189
- ParamLocationRecognizer recognizer = createRecognizer ();
187
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
190
188
NATIVE_QUERY_INTERPRETER .recognizeParameters ("from Stock s where s.stockCode = ?1 and s.xyz = ?1" , recognizer );
191
189
recognizer .complete ();
192
190
recognizer .validate ();
193
191
194
- assertEquals ( 1 , recognizer .getOrdinalParameterDescriptionMap ().size () );
195
-
192
+ assertEquals ( 1 , recognizer .getJpaPositionalParameterCount () );
193
+
194
+ recognizer = createRecognizer ();
196
195
ParameterParser .parse ("from Stock s where s.stockCode = ?1 and s.xyz = ?2" , recognizer );
197
196
recognizer .complete ();
198
197
recognizer .validate ();
199
198
200
- assertEquals ( 2 , recognizer .getOrdinalParameterDescriptionMap (). size () );
199
+ assertEquals ( 2 , recognizer .getJpaPositionalParameterCount () );
201
200
}
202
201
203
202
@ Test
204
203
public void testJdbcParameterScanningEnabled () {
205
- ParamLocationRecognizer recognizer = createRecognizer ();
204
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
206
205
207
206
assertThrows (
208
207
ParameterRecognitionException .class ,
@@ -219,15 +218,15 @@ public void testJdbcParameterScanningEnabled() {
219
218
220
219
@ Test
221
220
public void testJdbcParameterScanningDisabled () {
222
- ParamLocationRecognizer recognizer = createRecognizer ();
221
+ ExtendedParameterRecognizer recognizer = createRecognizer ();
223
222
224
223
// Should recognize the jpa style ordinal parameters
225
224
new NativeQueryInterpreterStandardImpl ( true ).recognizeParameters (
226
225
"SELECT column FROM Table WHERE column.id = ?1 and column.name = ?2" ,
227
226
recognizer
228
227
);
229
228
recognizer .validate ();
230
- assertEquals ( 2 , recognizer .getOrdinalParameterDescriptionMap (). size () );
229
+ assertEquals ( 2 , recognizer .getJpaPositionalParameterCount () );
231
230
232
231
recognizer = createRecognizer ();
233
232
// Should ignore the '?'
@@ -236,8 +235,75 @@ public void testJdbcParameterScanningDisabled() {
236
235
recognizer
237
236
);
238
237
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
+ }
241
268
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
+ }
242
308
}
243
309
}
0 commit comments