7
7
import java .util .Locale ;
8
8
import java .util .Set ;
9
9
10
+ import org .hibernate .AssertionFailure ;
10
11
import org .hibernate .grammars .hql .HqlParser ;
11
12
import org .hibernate .jpa .spi .JpaCompliance ;
12
- import org .hibernate .query .hql .spi .SqmCreationProcessingState ;
13
- import org .hibernate .query .hql .spi .SqmPathRegistry ;
14
13
import org .hibernate .query .sqm .SqmTreeCreationLogger ;
15
14
import org .hibernate .query .sqm .StrictJpaComplianceViolation ;
16
15
import org .hibernate .query .sqm .tree .SqmJoinType ;
@@ -136,8 +135,7 @@ public static <E> void handleRootAsCrossJoin(
136
135
HqlParser .EntityWithJoinsContext entityWithJoinsContext ,
137
136
SqmRoot <E > sqmPrimaryRoot ,
138
137
SemanticQueryBuilder <?> sqmBuilder ) {
139
- final HqlParser .RootEntityContext fromRootContext =
140
- (HqlParser .RootEntityContext ) entityWithJoinsContext .fromRoot ();
138
+ final var fromRootContext = (HqlParser .RootEntityContext ) entityWithJoinsContext .fromRoot ();
141
139
142
140
//noinspection unchecked
143
141
final SqmRoot <E > sqmRoot = (SqmRoot <E >) fromRootContext .accept ( sqmBuilder );
@@ -150,9 +148,8 @@ public static <E> void handleRootAsCrossJoin(
150
148
);
151
149
sqmPrimaryRoot .addSqmJoin ( pseudoCrossJoin );
152
150
153
- final SqmCreationProcessingState processingState = sqmBuilder .getProcessingStateStack ().getCurrent ();
154
- final SqmPathRegistry pathRegistry = processingState .getPathRegistry ();
155
- pathRegistry .replace ( pseudoCrossJoin , sqmRoot );
151
+ sqmBuilder .getProcessingStateStack ().getCurrent ().getPathRegistry ()
152
+ .replace ( pseudoCrossJoin , sqmRoot );
156
153
157
154
final int size = entityWithJoinsContext .getChildCount ();
158
155
for ( int i = 1 ; i < size ; i ++ ) {
@@ -196,61 +193,49 @@ public static String extractVariable(HqlParser.VariableContext ctx, SemanticQuer
196
193
if ( ctx == null ) {
197
194
return null ;
198
195
}
199
-
200
- final ParseTree lastChild = ctx .getChild ( ctx .getChildCount () - 1 );
201
- if ( lastChild instanceof HqlParser .IdentifierContext identifierContext ) {
202
- // in this branch, the alias could be a reserved word ("keyword as identifier")
203
- // which JPA disallows...
204
- if ( sqmBuilder .getCreationOptions ().useStrictJpaCompliance () ) {
205
- final Token identificationVariableToken = identifierContext .getStart ();
206
- if ( RESERVED_WORDS .contains ( identificationVariableToken .getText ().toLowerCase ( Locale .ENGLISH ) ) ) {
207
- throw new StrictJpaComplianceViolation (
208
- String .format (
209
- Locale .ROOT ,
210
- "Strict JPQL compliance was violated : %s [%s]" ,
211
- StrictJpaComplianceViolation .Type .RESERVED_WORD_USED_AS_ALIAS .description (),
212
- identificationVariableToken .getText ()
213
- ),
214
- StrictJpaComplianceViolation .Type .RESERVED_WORD_USED_AS_ALIAS
215
- );
216
- }
196
+ else {
197
+ final ParseTree lastChild = ctx .getChild ( ctx .getChildCount () - 1 );
198
+ if ( lastChild instanceof HqlParser .IdentifierContext identifierContext ) {
199
+ // in this branch, the alias could be a reserved word ("keyword as identifier")
200
+ // which JPA disallows...
201
+ checkJpaCompliance ( sqmBuilder , identifierContext .getStart () );
202
+ return sqmBuilder .visitIdentifier ( identifierContext );
203
+ }
204
+ else if ( lastChild instanceof HqlParser .NakedIdentifierContext identifierContext ) {
205
+ // in this branch, the alias could be a reserved word ("keyword as identifier")
206
+ // which JPA disallows...
207
+ checkJpaCompliance ( sqmBuilder , identifierContext .getStart () );
208
+ return sqmBuilder .visitNakedIdentifier ( identifierContext );
209
+ }
210
+ else {
211
+ throw new AssertionFailure ( "Unexpected type parse of tree" );
217
212
}
218
- return sqmBuilder .visitIdentifier ( identifierContext );
219
213
}
220
- else {
221
- final HqlParser .NakedIdentifierContext identifierContext = (HqlParser .NakedIdentifierContext ) lastChild ;
222
- // in this branch, the alias could be a reserved word ("keyword as identifier")
223
- // which JPA disallows...
224
- if ( sqmBuilder .getCreationOptions ().useStrictJpaCompliance () ) {
225
- final Token identificationVariableToken = identifierContext .getStart ();
226
- if ( RESERVED_WORDS .contains ( identificationVariableToken .getText ().toLowerCase ( Locale .ENGLISH ) ) ) {
227
- throw new StrictJpaComplianceViolation (
228
- String .format (
229
- Locale .ROOT ,
230
- "Strict JPQL compliance was violated : %s [%s]" ,
231
- StrictJpaComplianceViolation .Type .RESERVED_WORD_USED_AS_ALIAS .description (),
232
- identificationVariableToken .getText ()
233
- ),
234
- StrictJpaComplianceViolation .Type .RESERVED_WORD_USED_AS_ALIAS
235
- );
236
- }
214
+ }
215
+
216
+ private static void checkJpaCompliance (SemanticQueryBuilder <?> sqmBuilder , Token identificationVariable ) {
217
+ if ( sqmBuilder .getCreationOptions ().useStrictJpaCompliance () ) {
218
+ if ( RESERVED_WORDS .contains ( identificationVariable .getText ().toLowerCase ( Locale .ENGLISH ) ) ) {
219
+ throw new StrictJpaComplianceViolation (
220
+ String .format (
221
+ Locale .ROOT ,
222
+ "Strict JPQL compliance was violated : %s [%s]" ,
223
+ StrictJpaComplianceViolation .Type .RESERVED_WORD_USED_AS_ALIAS .description (),
224
+ identificationVariable .getText ()
225
+ ),
226
+ StrictJpaComplianceViolation .Type .RESERVED_WORD_USED_AS_ALIAS
227
+ );
237
228
}
238
- return sqmBuilder .visitNakedIdentifier ( identifierContext );
239
229
}
240
230
}
241
231
242
232
/**
243
233
* Handle JPA requirement that variables (aliases) be case-insensitive
244
234
*/
245
235
public static String applyJpaCompliance (String text , SemanticQueryBuilder <?> sqmBuilder ) {
246
- if ( text == null ) {
247
- return null ;
248
- }
249
-
250
- if ( sqmBuilder .getCreationOptions ().useStrictJpaCompliance () ) {
251
- return text .toLowerCase ( Locale .getDefault () );
252
- }
253
-
254
- return text ;
236
+ return text != null
237
+ && sqmBuilder .getCreationOptions ().useStrictJpaCompliance ()
238
+ ? text .toLowerCase ( Locale .getDefault () )
239
+ : text ;
255
240
}
256
241
}
0 commit comments