11
11
import org .hibernate .query .spi .QueryEngine ;
12
12
import org .hibernate .query .sqm .UnknownPathException ;
13
13
import org .hibernate .query .sqm .function .SelfRenderingSqmFunction ;
14
+ import org .hibernate .query .sqm .function .SqmFunctionRegistry ;
14
15
import org .hibernate .query .sqm .tree .expression .SqmExpression ;
15
16
import org .hibernate .query .sqm .tree .from .SqmFrom ;
16
17
import org .hibernate .spi .NavigablePath ;
@@ -98,31 +99,43 @@ public SqmPath<?> resolveIndexedAccess(
98
99
SqmExpression <?> selector ,
99
100
boolean isTerminal ,
100
101
SqmCreationState creationState ) {
101
- final SqmPathRegistry pathRegistry = creationState .getCurrentProcessingState ().getPathRegistry ();
102
+ final SqmPathRegistry pathRegistry =
103
+ creationState .getCurrentProcessingState ().getPathRegistry ();
102
104
final String alias = selector .toHqlString ();
103
- final NavigablePath navigablePath = getNavigablePath ().getParent ().append (
104
- CollectionPart .Nature .ELEMENT .getName (),
105
- alias
106
- );
105
+ final NavigablePath navigablePath =
106
+ getNavigablePath ().getParent ()
107
+ .append ( CollectionPart .Nature .ELEMENT .getName (), alias );
107
108
final SqmFrom <?, ?> indexedPath = pathRegistry .findFromByPath ( navigablePath );
108
109
if ( indexedPath != null ) {
109
110
return indexedPath ;
110
111
}
111
- final DomainType <T > sqmPathType = getNodeType ().getPathType ();
112
- final QueryEngine queryEngine = creationState .getCreationContext ().getQueryEngine ();
113
- final SelfRenderingSqmFunction <?> result ;
112
+ else {
113
+ final SqmFunctionPath <Object > path =
114
+ new SqmFunctionPath <>(
115
+ getIndexFunction (
116
+ selector ,
117
+ getNodeType ().getPathType (),
118
+ creationState .getCreationContext ().getQueryEngine ()
119
+ )
120
+ );
121
+ pathRegistry .register ( path );
122
+ return path ;
123
+ }
124
+ }
125
+
126
+ private SelfRenderingSqmFunction <?> getIndexFunction (
127
+ SqmExpression <?> selector , DomainType <T > sqmPathType , QueryEngine queryEngine ) {
128
+ final SqmFunctionRegistry registry = queryEngine .getSqmFunctionRegistry ();
114
129
if ( sqmPathType instanceof BasicPluralType <?, ?> ) {
115
- result = queryEngine .getSqmFunctionRegistry ()
116
- .findFunctionDescriptor ( "array_get" )
130
+ return registry .findFunctionDescriptor ( "array_get" )
117
131
.generateSqmExpression (
118
132
asList ( this , selector ),
119
133
null ,
120
134
queryEngine
121
135
);
122
136
}
123
- else if ( sqmPathType .resolveExpressible ( nodeBuilder () ).getRelationalJavaType ().getJavaTypeClass () == String .class ) {
124
- result = queryEngine .getSqmFunctionRegistry ()
125
- .findFunctionDescriptor ( "substring" )
137
+ else if ( getJavaTypeClass ( sqmPathType ) == String .class ) {
138
+ return registry .findFunctionDescriptor ( "substring" )
126
139
.generateSqmExpression (
127
140
asList ( this , selector , nodeBuilder ().literal ( 1 ) ),
128
141
nodeBuilder ().getCharacterType (),
@@ -132,9 +145,12 @@ else if ( sqmPathType.resolveExpressible( nodeBuilder() ).getRelationalJavaType(
132
145
else {
133
146
throw new UnsupportedOperationException ( "Index access is only supported for basic plural and string types, but got: " + sqmPathType );
134
147
}
135
- final SqmFunctionPath <Object > path = new SqmFunctionPath <>( result );
136
- pathRegistry .register ( path );
137
- return path ;
148
+ }
149
+
150
+ private Class <?> getJavaTypeClass (DomainType <T > sqmPathType ) {
151
+ return sqmPathType .resolveExpressible ( nodeBuilder () )
152
+ .getRelationalJavaType ()
153
+ .getJavaTypeClass ();
138
154
}
139
155
140
156
0 commit comments