55package org .hibernate .sql .exec .internal ;
66
77import org .checkerframework .checker .nullness .qual .Nullable ;
8+ import org .hibernate .LockOptions ;
9+ import org .hibernate .Locking ;
10+ import org .hibernate .dialect .lock .spi .LockTimeoutType ;
11+ import org .hibernate .dialect .lock .spi .LockingSupport ;
812import org .hibernate .internal .util .collections .CollectionHelper ;
913import org .hibernate .query .spi .QueryOptions ;
14+ import org .hibernate .sql .ast .spi .LockingClauseStrategy ;
1015import org .hibernate .sql .ast .tree .expression .JdbcParameter ;
16+ import org .hibernate .sql .ast .tree .select .QuerySpec ;
17+ import org .hibernate .sql .exec .internal .lock .CollectionLockingAction ;
18+ import org .hibernate .sql .exec .internal .lock .FollowOnLockingAction ;
1119import org .hibernate .sql .exec .spi .ExecutionContext ;
1220import org .hibernate .sql .exec .spi .JdbcLockStrategy ;
1321import org .hibernate .sql .exec .spi .JdbcOperationQuery ;
1422import org .hibernate .sql .exec .spi .JdbcParameterBinder ;
1523import org .hibernate .sql .exec .spi .JdbcParameterBinding ;
1624import org .hibernate .sql .exec .spi .JdbcParameterBindings ;
1725import org .hibernate .sql .exec .spi .JdbcSelect ;
26+ import org .hibernate .sql .exec .spi .JdbcSelectWithActionsBuilder ;
1827import org .hibernate .sql .exec .spi .LoadedValuesCollector ;
1928import org .hibernate .sql .exec .spi .PostAction ;
2029import org .hibernate .sql .exec .spi .PreAction ;
3544public class JdbcSelectWithActions implements JdbcOperationQuery , JdbcSelect {
3645 private final JdbcOperationQuerySelect primaryOperation ;
3746
38- private final LoadedValuesCollector loadedValuesCollector ;
39- private final PreAction [] preActions ;
40- private final PostAction [] postActions ;
47+ // Used by Hibernate Reactive
48+ protected final LoadedValuesCollector loadedValuesCollector ;
49+ // Used by Hibernate Reactive
50+ protected final PreAction [] preActions ;
51+ // Used by Hibernate Reactive
52+ protected final PostAction [] postActions ;
4153
4254 public JdbcSelectWithActions (
4355 JdbcOperationQuerySelect primaryOperation ,
@@ -149,24 +161,84 @@ public boolean isCompatibleWith(JdbcParameterBindings jdbcParameterBindings, Que
149161 return primaryOperation .isCompatibleWith ( jdbcParameterBindings , queryOptions );
150162 }
151163
152- public static class Builder {
153- private final JdbcOperationQuerySelect primaryAction ;
154-
164+ public static class Builder implements JdbcSelectWithActionsBuilder {
165+ private JdbcOperationQuerySelect primaryAction ;
155166 private LoadedValuesCollector loadedValuesCollector ;
156167 protected List <PreAction > preActions ;
157168 protected List <PostAction > postActions ;
158-
159- public Builder (JdbcOperationQuerySelect primaryAction ) {
160- this .primaryAction = primaryAction ;
169+ protected LockTimeoutType lockTimeoutType ;
170+ protected LockingSupport lockingSupport ;
171+ protected LockOptions lockOptions ;
172+ protected QuerySpec lockingTarget ;
173+ protected LockingClauseStrategy lockingClauseStrategy ;
174+ boolean isFollonOnLockStrategy ;
175+
176+ @ Override
177+ public Builder setPrimaryAction (JdbcSelect primaryAction ){
178+ assert primaryAction instanceof JdbcOperationQuerySelect ;
179+ this .primaryAction = (JdbcOperationQuerySelect ) primaryAction ;
180+ return this ;
161181 }
162182
163183 @ SuppressWarnings ("UnusedReturnValue" )
184+ @ Override
164185 public Builder setLoadedValuesCollector (LoadedValuesCollector loadedValuesCollector ) {
165186 this .loadedValuesCollector = loadedValuesCollector ;
166187 return this ;
167188 }
168189
190+ @ Override
191+ public Builder setLockTimeoutType (LockTimeoutType lockTimeoutType ){
192+ this .lockTimeoutType = lockTimeoutType ;
193+ return this ;
194+ }
195+
196+ @ Override
197+ public Builder setLockingSupport (LockingSupport lockingSupport ){
198+ this .lockingSupport = lockingSupport ;
199+ return this ;
200+ }
201+
202+ @ Override
203+ public Builder setLockOptions (LockOptions lockOptions ){
204+ this .lockOptions = lockOptions ;
205+ return this ;
206+ }
207+
208+ @ Override
209+ public Builder setLockingTarget (QuerySpec lockingTarget ){
210+ this .lockingTarget = lockingTarget ;
211+ return this ;
212+ }
213+
214+ @ Override
215+ public Builder setLockingClauseStrategy (LockingClauseStrategy lockingClauseStrategy ){
216+ this .lockingClauseStrategy = lockingClauseStrategy ;
217+ return this ;
218+ }
219+
220+ @ Override
221+ public Builder setIsFollowOnLockStrategy (boolean isFollonOnLockStrategy ){
222+ this .isFollonOnLockStrategy = isFollonOnLockStrategy ;
223+ return this ;
224+ }
225+
226+ @ Override
169227 public JdbcSelect build () {
228+ if ( lockTimeoutType == LockTimeoutType .CONNECTION ) {
229+ addSecondaryActionPair (
230+ new LockTimeoutHandler (
231+ lockOptions .getTimeout (),
232+ lockingSupport .getConnectionLockTimeoutStrategy ()
233+ )
234+ );
235+ }
236+ if ( isFollonOnLockStrategy ) {
237+ FollowOnLockingAction .apply ( lockOptions , lockingTarget , lockingClauseStrategy , this );
238+ }
239+ else if ( lockOptions .getScope () == Locking .Scope .INCLUDE_COLLECTIONS ) {
240+ CollectionLockingAction .apply ( lockOptions , lockingTarget , this );
241+ }
170242 if ( preActions == null && postActions == null ) {
171243 assert loadedValuesCollector == null ;
172244 return primaryAction ;
@@ -182,6 +254,7 @@ public JdbcSelect build() {
182254 *
183255 * @return {@code this}, for method chaining.
184256 */
257+ @ Override
185258 public Builder appendPreAction (PreAction ... actions ) {
186259 if ( preActions == null ) {
187260 preActions = new ArrayList <>();
@@ -195,6 +268,7 @@ public Builder appendPreAction(PreAction... actions) {
195268 *
196269 * @return {@code this}, for method chaining.
197270 */
271+ @ Override
198272 public Builder prependPreAction (PreAction ... actions ) {
199273 if ( preActions == null ) {
200274 preActions = new ArrayList <>();
@@ -209,6 +283,7 @@ public Builder prependPreAction(PreAction... actions) {
209283 *
210284 * @return {@code this}, for method chaining.
211285 */
286+ @ Override
212287 public Builder appendPostAction (PostAction ... actions ) {
213288 if ( postActions == null ) {
214289 postActions = new ArrayList <>();
@@ -222,6 +297,7 @@ public Builder appendPostAction(PostAction... actions) {
222297 *
223298 * @return {@code this}, for method chaining.
224299 */
300+ @ Override
225301 public Builder prependPostAction (PostAction ... actions ) {
226302 if ( postActions == null ) {
227303 postActions = new ArrayList <>();
@@ -243,6 +319,7 @@ public Builder prependPostAction(PostAction... actions) {
243319 *
244320 * @return {@code this}, for method chaining.
245321 */
322+ @ Override
246323 public Builder addSecondaryActionPair (SecondaryAction action ) {
247324 return addSecondaryActionPair ( (PreAction ) action , (PostAction ) action );
248325 }
@@ -255,24 +332,27 @@ public Builder addSecondaryActionPair(SecondaryAction action) {
255332 *
256333 * @return {@code this}, for method chaining.
257334 */
335+ @ Override
258336 public Builder addSecondaryActionPair (PreAction preAction , PostAction postAction ) {
259337 prependPreAction ( preAction );
260338 appendPostAction ( postAction );
261339 return this ;
262340 }
263341
264- private static PreAction [] toPreActionArray (List <PreAction > actions ) {
265- if ( CollectionHelper .isEmpty ( actions ) ) {
266- return null ;
267- }
268- return actions .toArray ( new PreAction [0 ] );
342+ // Used by Hibernate Reactive
343+ static PreAction [] toPreActionArray (List <PreAction > actions ) {
344+ if ( CollectionHelper .isEmpty ( actions ) ) {
345+ return null ;
269346 }
270-
271- private static PostAction [] toPostActionArray ( List < PostAction > actions ) {
272- if ( CollectionHelper . isEmpty ( actions ) ) {
273- return null ;
274- }
275- return actions . toArray ( new PostAction [ 0 ] ) ;
347+ return actions . toArray ( new PreAction [ 0 ] );
348+ }
349+ // Used by Hibernate Reactive
350+ static PostAction [] toPostActionArray ( List < PostAction > actions ) {
351+ if ( CollectionHelper . isEmpty ( actions ) ) {
352+ return null ;
276353 }
354+ return actions .toArray ( new PostAction [0 ] );
355+ }
356+
277357 }
278358}
0 commit comments