@@ -49,7 +49,15 @@ public class SqmPathRegistryImpl implements SqmPathRegistry {
4949
5050 public SqmPathRegistryImpl (SqmCreationProcessingState associatedProcessingState ) {
5151 this .associatedProcessingState = associatedProcessingState ;
52- this .jpaCompliance = associatedProcessingState .getCreationState ().getCreationContext ().getNodeBuilder ().getJpaCompliance ();
52+ this .jpaCompliance =
53+ associatedProcessingState .getCreationState ().getCreationContext ()
54+ .getNodeBuilder ().getJpaCompliance ();
55+ }
56+
57+ private String handleAliasCaseSensitivity (String alias ) {
58+ return jpaCompliance .isJpaQueryComplianceEnabled ()
59+ ? alias .toLowerCase ( Locale .getDefault () )
60+ : alias ;
5361 }
5462
5563 @ Override
@@ -63,35 +71,33 @@ public void register(SqmPath<?> sqmPath) {
6371 // Regarding part #1 (add to the path-by-path map), it is ok for a SqmFrom to replace a
6472 // non-SqmFrom. This should equate to, e.g., an implicit join.
6573
66- if ( sqmPath instanceof SqmFrom <?, ?> sqmFrom ) {
74+ final var navigablePath = sqmPath . getNavigablePath ();
6775
76+ if ( sqmPath instanceof SqmFrom <?, ?> sqmFrom ) {
6877 registerByAliasOnly ( sqmFrom );
69-
70- final SqmFrom <?, ?> previousFromByPath = sqmFromByPath .put ( sqmPath .getNavigablePath (), sqmFrom );
71-
78+ final var previousFromByPath = sqmFromByPath .put ( navigablePath , sqmFrom );
7279 if ( previousFromByPath != null ) {
7380 // this should never happen
7481 throw new ParsingException (
7582 String .format (
7683 Locale .ROOT ,
7784 "Registration for SqmFrom [%s] overrode previous registration: %s -> %s" ,
78- sqmPath . getNavigablePath () ,
85+ navigablePath ,
7986 previousFromByPath ,
8087 sqmFrom
8188 )
8289 );
8390 }
8491 }
8592
86- final SqmPath <?> previousPath = sqmPathByPath .put ( sqmPath .getNavigablePath (), sqmPath );
87-
93+ final var previousPath = sqmPathByPath .put ( navigablePath , sqmPath );
8894 if ( previousPath instanceof SqmFrom ) {
8995 // this should never happen
9096 throw new ParsingException (
9197 String .format (
9298 Locale .ROOT ,
9399 "Registration for path [%s] overrode previous registration: %s -> %s" ,
94- sqmPath . getNavigablePath () ,
100+ navigablePath ,
95101 previousPath ,
96102 sqmPath
97103 )
@@ -101,8 +107,6 @@ public void register(SqmPath<?> sqmPath) {
101107
102108 private static String fromPath (SqmFrom <?, ?> sqmFrom , boolean first ) {
103109 //TODO: the qualified path, but not using getFullPath() which has cruft
104- final String path = sqmFrom .getNavigablePath ().getLocalName ();
105- final String alias = sqmFrom .getExplicitAlias ();
106110 final String keyword ;
107111 if ( sqmFrom instanceof SqmRoot && first ) {
108112 keyword = "from " ;
@@ -113,19 +117,16 @@ else if ( sqmFrom instanceof SqmJoin ) {
113117 else {
114118 keyword = first ? "" : ", " ;
115119 }
120+ final String path = sqmFrom .getNavigablePath ().getLocalName ();
121+ final String alias = sqmFrom .getExplicitAlias ();
116122 return keyword + (alias == null ? path : path + " as " + alias );
117123 }
118124
119125 @ Override
120126 public void registerByAliasOnly (SqmFrom <?, ?> sqmFrom ) {
121127 final String alias = sqmFrom .getExplicitAlias ();
122128 if ( alias != null ) {
123- final String aliasToUse = jpaCompliance .isJpaQueryComplianceEnabled ()
124- ? alias .toLowerCase ( Locale .getDefault () )
125- : alias ;
126-
127- final SqmFrom <?, ?> previousFrom = sqmFromByAlias .put ( aliasToUse , sqmFrom );
128-
129+ final var previousFrom = sqmFromByAlias .put ( handleAliasCaseSensitivity ( alias ), sqmFrom );
129130 if ( previousFrom != null ) {
130131 throw new AliasCollisionException (
131132 String .format (
@@ -144,11 +145,7 @@ public void registerByAliasOnly(SqmFrom<?, ?> sqmFrom) {
144145 public <E > void replace (SqmEntityJoin <?,E > sqmJoin , SqmRoot <E > sqmRoot ) {
145146 final String alias = sqmJoin .getExplicitAlias ();
146147 if ( alias != null ) {
147- final String aliasToUse = jpaCompliance .isJpaQueryComplianceEnabled ()
148- ? alias .toLowerCase ( Locale .getDefault () )
149- : alias ;
150-
151- final SqmFrom <?, ?> previousFrom = sqmFromByAlias .put ( aliasToUse , sqmJoin );
148+ final var previousFrom = sqmFromByAlias .put ( handleAliasCaseSensitivity ( alias ), sqmJoin );
152149 if ( previousFrom != null && !( previousFrom instanceof SqmRoot ) ) {
153150 throw new AliasCollisionException (
154151 String .format (
@@ -162,28 +159,30 @@ public <E> void replace(SqmEntityJoin<?,E> sqmJoin, SqmRoot<E> sqmRoot) {
162159 }
163160 }
164161
165- final SqmFrom <?, ?> previousFromByPath = sqmFromByPath .put ( sqmJoin .getNavigablePath (), sqmJoin );
162+ final var navigablePath = sqmJoin .getNavigablePath ();
163+
164+ final var previousFromByPath = sqmFromByPath .put ( navigablePath , sqmJoin );
166165 if ( previousFromByPath != null && !( previousFromByPath instanceof SqmRoot ) ) {
167166 // this should never happen
168167 throw new ParsingException (
169168 String .format (
170169 Locale .ROOT ,
171170 "Registration for SqmFrom [%s] overrode previous registration: %s -> %s" ,
172- sqmJoin . getNavigablePath () ,
171+ navigablePath ,
173172 previousFromByPath ,
174173 sqmJoin
175174 )
176175 );
177176 }
178177
179- final SqmPath <?> previousPath = sqmPathByPath .put ( sqmJoin . getNavigablePath () , sqmJoin );
178+ final var previousPath = sqmPathByPath .put ( navigablePath , sqmJoin );
180179 if ( previousPath instanceof SqmFrom && !( previousPath instanceof SqmRoot ) ) {
181180 // this should never happen
182181 throw new ParsingException (
183182 String .format (
184183 Locale .ROOT ,
185184 "Registration for path [%s] overrode previous registration: %s -> %s" ,
186- sqmJoin . getNavigablePath () ,
185+ navigablePath ,
187186 previousPath ,
188187 sqmJoin
189188 )
@@ -199,40 +198,27 @@ public <E> void replace(SqmEntityJoin<?,E> sqmJoin, SqmRoot<E> sqmRoot) {
199198
200199 @ Override
201200 public <X extends SqmFrom <?, ?>> X findFromByAlias (String alias , boolean searchParent ) {
202- final String localAlias = jpaCompliance .isJpaQueryComplianceEnabled ()
203- ? alias .toLowerCase ( Locale .getDefault () )
204- : alias ;
205-
206- final SqmFrom <?, ?> registered = sqmFromByAlias .get ( localAlias );
201+ final String localAlias = handleAliasCaseSensitivity ( alias );
207202
203+ final var registered = sqmFromByAlias .get ( localAlias );
208204 if ( registered != null ) {
209205 //noinspection unchecked
210206 return (X ) registered ;
211207 }
212208
213- SqmCreationProcessingState parentProcessingState = associatedProcessingState .getParentProcessingState ();
209+ var parentProcessingState = associatedProcessingState .getParentProcessingState ();
214210 if ( searchParent && parentProcessingState != null ) {
215211 X parentRegistered ;
216212 do {
217- parentRegistered = parentProcessingState .getPathRegistry ().findFromByAlias (
218- alias ,
219- false
220- );
213+ parentRegistered =
214+ parentProcessingState .getPathRegistry ()
215+ .findFromByAlias ( alias , false );
221216 parentProcessingState = parentProcessingState .getParentProcessingState ();
222- } while (parentProcessingState != null && parentRegistered == null );
217+ }
218+ while (parentProcessingState != null && parentRegistered == null );
223219 if ( parentRegistered != null ) {
224220 // If a parent query contains the alias, we need to create a correlation on the subquery
225- final SqmSubQuery <?> selectQuery = ( SqmSubQuery <?> ) associatedProcessingState .getProcessingQuery ();
226- final SqmFrom <?, ?> correlated ;
227- if ( parentRegistered instanceof Root <?> root ) {
228- correlated = selectQuery .correlate ( root );
229- }
230- else if ( parentRegistered instanceof Join <?, ?> join ) {
231- correlated = selectQuery .correlate ( join );
232- }
233- else {
234- throw new UnsupportedOperationException ( "Can't correlate from node: " + parentRegistered );
235- }
221+ final var correlated = correlate ( parentRegistered );
236222 register ( correlated );
237223 //noinspection unchecked
238224 return (X ) correlated ;
@@ -241,16 +227,28 @@ else if ( parentRegistered instanceof Join<?, ?> join ) {
241227
242228 final boolean onlyOneFrom = sqmFromByPath .size () == 1 ;
243229 if ( onlyOneFrom && localAlias .equalsIgnoreCase ( "this" ) ) {
244- final SqmRoot <?> root = (SqmRoot <?>) sqmFromByPath .entrySet ().iterator ().next ().getValue ();
230+ final var root = (SqmRoot <?>) sqmFromByPath .entrySet ().iterator ().next ().getValue ();
245231 if ( root .getAlias () == null ) {
246232 //noinspection unchecked
247233 return (X ) root ;
248234 }
249235 }
250-
251236 return null ;
252237 }
253238
239+ private <X extends SqmFrom <?, ?>> SqmFrom <?, ?> correlate (X parentRegistered ) {
240+ final var selectQuery = (SqmSubQuery <?>) associatedProcessingState .getProcessingQuery ();
241+ if ( parentRegistered instanceof Root <?> root ) {
242+ return selectQuery .correlate ( root );
243+ }
244+ else if ( parentRegistered instanceof Join <?, ?> join ) {
245+ return selectQuery .correlate ( join );
246+ }
247+ else {
248+ throw new UnsupportedOperationException ( "Can't correlate from node: " + parentRegistered );
249+ }
250+ }
251+
254252 @ Override
255253 public <X extends SqmFrom <?, ?>> X findFromExposing (String navigableName ) {
256254 // todo (6.0) : atm this checks every from-element every time, the idea being to make sure there
@@ -259,7 +257,7 @@ else if ( parentRegistered instanceof Join<?, ?> join ) {
259257
260258 SqmFrom <?, ?> found = null ;
261259 for ( var entry : sqmFromByPath .entrySet () ) {
262- final SqmFrom <?, ?> fromElement = entry .getValue ();
260+ final var fromElement = entry .getValue ();
263261 if ( definesAttribute ( fromElement .getReferencedPathSource (), navigableName ) ) {
264262 if ( found != null ) {
265263 throw new SemanticException ( "Ambiguous unqualified attribute reference '" + navigableName +
@@ -270,12 +268,13 @@ else if ( parentRegistered instanceof Join<?, ?> join ) {
270268 }
271269
272270 if ( found == null ) {
273- if ( associatedProcessingState .getParentProcessingState () != null ) {
271+ final var processingState = associatedProcessingState .getParentProcessingState ();
272+ if ( processingState != null ) {
274273// QUERY_LOGGER.tracef(
275274// "Unable to resolve unqualified attribute [%s] in local from-clause; checking parent ",
276275// navigableName
277276// );
278- found = associatedProcessingState . getParentProcessingState () .getPathRegistry ().findFromExposing ( navigableName );
277+ found = processingState .getPathRegistry ().findFromExposing ( navigableName );
279278 }
280279 }
281280
@@ -290,83 +289,76 @@ else if ( parentRegistered instanceof Join<?, ?> join ) {
290289
291290 @ Override
292291 public <X extends SqmFrom <?, ?>> X resolveFrom (NavigablePath navigablePath , Function <NavigablePath , SqmFrom <?, ?>> creator ) {
293- final SqmFrom <?, ?> existing = sqmFromByPath .get ( navigablePath );
292+ final var existing = sqmFromByPath .get ( navigablePath );
294293 if ( existing != null ) {
295294 //noinspection unchecked
296295 return (X ) existing ;
297296 }
298-
299- final SqmFrom <?, ?> sqmFrom = creator .apply ( navigablePath );
300- register ( sqmFrom );
301- //noinspection unchecked
302- return (X ) sqmFrom ;
297+ else {
298+ final var sqmFrom = creator .apply ( navigablePath );
299+ register ( sqmFrom );
300+ //noinspection unchecked
301+ return (X ) sqmFrom ;
302+ }
303303 }
304304
305305 @ Override
306306 public <X extends SqmFrom <?, ?>> X resolveFrom (SqmPath <?> path ) {
307- final SqmFrom <?, ?> existing = sqmFromByPath .get ( path .getNavigablePath () );
307+ final var navigablePath = path .getNavigablePath ();
308+ final var existing = sqmFromByPath .get ( navigablePath );
308309 if ( existing != null ) {
309310 //noinspection unchecked
310311 return (X ) existing ;
311312 }
312-
313- final SqmFrom <?, ?> sqmFrom = resolveFrom ( path .getLhs () ).join ( path .getNavigablePath ().getLocalName () );
314- register ( sqmFrom );
315- //noinspection unchecked
316- return (X ) sqmFrom ;
313+ else {
314+ final var sqmFrom =
315+ resolveFrom ( path .getLhs () )
316+ .join ( navigablePath .getLocalName () );
317+ register ( sqmFrom );
318+ //noinspection unchecked
319+ return (X ) sqmFrom ;
320+ }
317321 }
318322
319323 private boolean definesAttribute (SqmPathSource <?> containerType , String name ) {
320324 return !( containerType .getSqmType () instanceof BasicDomainType )
321- && containerType .findSubPathSource ( name , true ) != null ;
325+ && containerType .findSubPathSource ( name , true ) != null ;
322326 }
323327
324328 @ Override
325329 public SqmAliasedNode <?> findAliasedNodeByAlias (String alias ) {
326330 assert alias != null ;
327-
328- final String aliasToUse = jpaCompliance .isJpaQueryComplianceEnabled ()
329- ? alias .toLowerCase ( Locale .getDefault () )
330- : alias ;
331-
331+ final String aliasToUse = handleAliasCaseSensitivity ( alias );
332332 for ( int i = 0 ; i < simpleSelectionNodes .size (); i ++ ) {
333- final SqmAliasedNode <?> node = simpleSelectionNodes .get ( i );
333+ final var node = simpleSelectionNodes .get ( i );
334334 if ( aliasToUse .equals ( node .getAlias () ) ) {
335335 return node ;
336336 }
337337 }
338-
339338 return null ;
340339 }
341340
342341 @ Override
343342 public Integer findAliasedNodePosition (String alias ) {
344- if ( alias == null ) {
345- return null ;
346- }
347-
348- final String aliasToUse = jpaCompliance .isJpaQueryComplianceEnabled ()
349- ? alias .toLowerCase ( Locale .getDefault () )
350- : alias ;
351-
352- // NOTE : 1-based
353-
354- for ( int i = 0 ; i < simpleSelectionNodes .size (); i ++ ) {
355- final SqmAliasedNode <?> node = simpleSelectionNodes .get ( i );
356- if ( aliasToUse .equals ( node .getAlias () ) ) {
357- return i + 1 ;
343+ if ( alias != null ) {
344+ final String aliasToUse = handleAliasCaseSensitivity ( alias );
345+ // NOTE: 1-based
346+ for ( int i = 0 ; i < simpleSelectionNodes .size (); i ++ ) {
347+ final var node = simpleSelectionNodes .get ( i );
348+ if ( aliasToUse .equals ( node .getAlias () ) ) {
349+ return i + 1 ;
350+ }
358351 }
359352 }
360-
361353 return null ;
362354 }
363355
364356 @ Override
365357 public SqmAliasedNode <?> findAliasedNodeByPosition (int position ) {
366- // NOTE : 1-based
358+ // NOTE: 1-based
367359 return position > simpleSelectionNodes .size ()
368360 ? null
369- : simpleSelectionNodes .get (position - 1 );
361+ : simpleSelectionNodes .get ( position - 1 );
370362 }
371363
372364 @ Override
@@ -377,20 +369,18 @@ public void register(SqmAliasedNode<?> node) {
377369
378370 private void checkResultVariable (SqmAliasedNode <?> selection ) {
379371 final String alias = selection .getAlias ();
380- if ( alias == null ) {
381- return ;
382- }
383-
384- final Integer position = findAliasedNodePosition ( alias );
385- if ( position != null ) {
386- throw new AliasCollisionException (
387- String .format (
388- Locale .ENGLISH ,
389- "Duplicate alias '%s' at position %s in 'select' clause" ,
390- alias ,
391- position
392- )
393- );
372+ if ( alias != null ) {
373+ final Integer position = findAliasedNodePosition ( alias );
374+ if ( position != null ) {
375+ throw new AliasCollisionException (
376+ String .format (
377+ Locale .ENGLISH ,
378+ "Duplicate alias '%s' at position %s in 'select' clause" ,
379+ alias ,
380+ position
381+ )
382+ );
383+ }
394384 }
395385 }
396386}
0 commit comments