77import  java .util .Locale ;
88import  java .util .Set ;
99
10+ import  org .hibernate .AssertionFailure ;
1011import  org .hibernate .grammars .hql .HqlParser ;
1112import  org .hibernate .jpa .spi .JpaCompliance ;
12- import  org .hibernate .query .hql .spi .SqmCreationProcessingState ;
13- import  org .hibernate .query .hql .spi .SqmPathRegistry ;
1413import  org .hibernate .query .sqm .SqmTreeCreationLogger ;
1514import  org .hibernate .query .sqm .StrictJpaComplianceViolation ;
1615import  org .hibernate .query .sqm .tree .SqmJoinType ;
@@ -136,8 +135,7 @@ public static <E> void handleRootAsCrossJoin(
136135			HqlParser .EntityWithJoinsContext  entityWithJoinsContext ,
137136			SqmRoot <E > sqmPrimaryRoot ,
138137			SemanticQueryBuilder <?> sqmBuilder ) {
139- 		final  HqlParser .RootEntityContext  fromRootContext  =
140- 				(HqlParser .RootEntityContext ) entityWithJoinsContext .fromRoot ();
138+ 		final  var  fromRootContext  = (HqlParser .RootEntityContext ) entityWithJoinsContext .fromRoot ();
141139
142140		//noinspection unchecked 
143141		final  SqmRoot <E > sqmRoot  = (SqmRoot <E >) fromRootContext .accept ( sqmBuilder  );
@@ -150,9 +148,8 @@ public static <E> void handleRootAsCrossJoin(
150148		);
151149		sqmPrimaryRoot .addSqmJoin ( pseudoCrossJoin  );
152150
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  );
156153
157154		final  int  size  = entityWithJoinsContext .getChildCount ();
158155		for  ( int  i  = 1 ; i  < size ; i ++ ) {
@@ -196,61 +193,49 @@ public static String extractVariable(HqlParser.VariableContext ctx, SemanticQuer
196193		if  ( ctx  == null  ) {
197194			return  null ;
198195		}
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"  );
217212			}
218- 			return  sqmBuilder .visitIdentifier ( identifierContext  );
219213		}
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+ 				);
237228			}
238- 			return  sqmBuilder .visitNakedIdentifier ( identifierContext  );
239229		}
240230	}
241231
242232	/** 
243233	 * Handle JPA requirement that variables (aliases) be case-insensitive 
244234	 */ 
245235	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 ;
255240	}
256241}
0 commit comments