15
15
*/
16
16
package org .springframework .data .r2dbc .core ;
17
17
18
- import java .util .ArrayList ;
19
- import java .util .Arrays ;
20
- import java .util .Collection ;
21
- import java .util .Collections ;
22
- import java .util .LinkedHashMap ;
23
- import java .util .List ;
24
- import java .util .Map ;
18
+ import java .util .*;
25
19
import java .util .function .BiFunction ;
26
20
import java .util .function .Supplier ;
27
21
import java .util .stream .Collectors ;
33
27
import org .springframework .data .relational .core .query .Criteria ;
34
28
import org .springframework .data .relational .core .query .CriteriaDefinition ;
35
29
import org .springframework .data .relational .core .sql .Expression ;
30
+ import org .springframework .data .relational .core .sql .LockMode ;
36
31
import org .springframework .data .relational .core .sql .SqlIdentifier ;
37
32
import org .springframework .data .relational .core .sql .Table ;
38
33
import org .springframework .data .relational .core .sql .render .RenderContext ;
53
48
* @author Mark Paluch
54
49
* @author Roman Chigvintsev
55
50
* @author Mingyuan Wu
51
+ * @author Diego Krupitza
56
52
*/
57
53
public interface StatementMapper {
58
54
@@ -227,9 +223,10 @@ class SelectSpec {
227
223
private final long offset ;
228
224
private final int limit ;
229
225
private final boolean distinct ;
226
+ private final LockMode lockMode ;
230
227
231
228
protected SelectSpec (Table table , List <String > projectedFields , List <Expression > selectList ,
232
- @ Nullable CriteriaDefinition criteria , Sort sort , int limit , long offset , boolean distinct ) {
229
+ @ Nullable CriteriaDefinition criteria , Sort sort , int limit , long offset , boolean distinct , LockMode lockMode ) {
233
230
this .table = table ;
234
231
this .projectedFields = projectedFields ;
235
232
this .selectList = selectList ;
@@ -238,6 +235,7 @@ protected SelectSpec(Table table, List<String> projectedFields, List<Expression>
238
235
this .offset = offset ;
239
236
this .limit = limit ;
240
237
this .distinct = distinct ;
238
+ this .lockMode = lockMode ;
241
239
}
242
240
243
241
/**
@@ -262,7 +260,7 @@ public static SelectSpec create(SqlIdentifier table) {
262
260
List <String > projectedFields = Collections .emptyList ();
263
261
List <Expression > selectList = Collections .emptyList ();
264
262
return new SelectSpec (Table .create (table ), projectedFields , selectList , Criteria .empty (), Sort .unsorted (), -1 , -1 ,
265
- false );
263
+ false , null );
266
264
}
267
265
268
266
public SelectSpec doWithTable (BiFunction <Table , SelectSpec , SelectSpec > function ) {
@@ -304,7 +302,7 @@ public SelectSpec withProjection(Expression... expressions) {
304
302
selectList .addAll (Arrays .asList (expressions ));
305
303
306
304
return new SelectSpec (this .table , projectedFields , selectList , this .criteria , this .sort , this .limit , this .offset ,
307
- this .distinct );
305
+ this .distinct , this . lockMode );
308
306
}
309
307
310
308
/**
@@ -320,7 +318,7 @@ public SelectSpec withProjection(Collection<Expression> projectedFields) {
320
318
selectList .addAll (projectedFields );
321
319
322
320
return new SelectSpec (this .table , this .projectedFields , selectList , this .criteria , this .sort , this .limit ,
323
- this .offset , this .distinct );
321
+ this .offset , this .distinct , this . lockMode );
324
322
}
325
323
326
324
/**
@@ -331,7 +329,7 @@ public SelectSpec withProjection(Collection<Expression> projectedFields) {
331
329
*/
332
330
public SelectSpec withCriteria (CriteriaDefinition criteria ) {
333
331
return new SelectSpec (this .table , this .projectedFields , this .selectList , criteria , this .sort , this .limit ,
334
- this .offset , this .distinct );
332
+ this .offset , this .distinct , this . lockMode );
335
333
}
336
334
337
335
/**
@@ -344,11 +342,11 @@ public SelectSpec withSort(Sort sort) {
344
342
345
343
if (sort .isSorted ()) {
346
344
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , sort , this .limit ,
347
- this .offset , this .distinct );
345
+ this .offset , this .distinct , this . lockMode );
348
346
}
349
347
350
348
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , this .limit ,
351
- this .offset , this .distinct );
349
+ this .offset , this .distinct , this . lockMode );
352
350
}
353
351
354
352
/**
@@ -364,11 +362,11 @@ public SelectSpec withPage(Pageable page) {
364
362
Sort sort = page .getSort ();
365
363
366
364
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria ,
367
- sort .isSorted () ? sort : this .sort , page .getPageSize (), page .getOffset (), this .distinct );
365
+ sort .isSorted () ? sort : this .sort , page .getPageSize (), page .getOffset (), this .distinct , this . lockMode );
368
366
}
369
367
370
368
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , this .limit ,
371
- this .offset , this .distinct );
369
+ this .offset , this .distinct , this . lockMode );
372
370
}
373
371
374
372
/**
@@ -379,7 +377,7 @@ public SelectSpec withPage(Pageable page) {
379
377
*/
380
378
public SelectSpec offset (long offset ) {
381
379
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , this .limit ,
382
- offset , this .distinct );
380
+ offset , this .distinct , this . lockMode );
383
381
}
384
382
385
383
/**
@@ -390,7 +388,7 @@ public SelectSpec offset(long offset) {
390
388
*/
391
389
public SelectSpec limit (int limit ) {
392
390
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , limit ,
393
- this .offset , this .distinct );
391
+ this .offset , this .distinct , this . lockMode );
394
392
}
395
393
396
394
/**
@@ -400,7 +398,28 @@ public SelectSpec limit(int limit) {
400
398
*/
401
399
public SelectSpec distinct () {
402
400
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , limit ,
403
- this .offset , true );
401
+ this .offset , true , this .lockMode );
402
+ }
403
+
404
+ /**
405
+ * Associate a lock mode with the select and create a new {@link SelectSpec}.
406
+ *
407
+ * @param lockMode the {@link LockMode} we want to use. This might be null
408
+ * @return the {@link SelectSpec}.
409
+ */
410
+ public SelectSpec lock (LockMode lockMode ) {
411
+ return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , limit ,
412
+ this .offset , this .distinct , lockMode );
413
+ }
414
+
415
+ /**
416
+ * The used lockmode
417
+ *
418
+ * @return might be null if no lockmode defined.
419
+ */
420
+ @ Nullable
421
+ public LockMode getLock () {
422
+ return this .lockMode ;
404
423
}
405
424
406
425
public Table getTable () {
@@ -440,6 +459,7 @@ public int getLimit() {
440
459
public boolean isDistinct () {
441
460
return this .distinct ;
442
461
}
462
+
443
463
}
444
464
445
465
/**
0 commit comments