1
1
/*
2
- * Copyright 2019-2021 the original author or authors.
2
+ * Copyright 2019-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
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 ;
34
28
import org .springframework .data .relational .core .query .Criteria ;
35
29
import org .springframework .data .relational .core .query .CriteriaDefinition ;
36
30
import org .springframework .data .relational .core .sql .Expression ;
31
+ import org .springframework .data .relational .core .sql .LockMode ;
37
32
import org .springframework .data .relational .core .sql .SqlIdentifier ;
38
33
import org .springframework .data .relational .core .sql .Table ;
39
34
import org .springframework .data .relational .core .sql .render .RenderContext ;
54
49
* @author Mark Paluch
55
50
* @author Roman Chigvintsev
56
51
* @author Mingyuan Wu
52
+ * @author Diego Krupitza
57
53
*/
58
54
public interface StatementMapper {
59
55
@@ -228,9 +224,10 @@ class SelectSpec {
228
224
private final long offset ;
229
225
private final int limit ;
230
226
private final boolean distinct ;
227
+ private final LockMode lockMode ;
231
228
232
229
protected SelectSpec (Table table , List <String > projectedFields , List <Expression > selectList ,
233
- @ Nullable CriteriaDefinition criteria , Sort sort , int limit , long offset , boolean distinct ) {
230
+ @ Nullable CriteriaDefinition criteria , Sort sort , int limit , long offset , boolean distinct , LockMode lockMode ) {
234
231
this .table = table ;
235
232
this .projectedFields = projectedFields ;
236
233
this .selectList = selectList ;
@@ -239,6 +236,7 @@ protected SelectSpec(Table table, List<String> projectedFields, List<Expression>
239
236
this .offset = offset ;
240
237
this .limit = limit ;
241
238
this .distinct = distinct ;
239
+ this .lockMode = lockMode ;
242
240
}
243
241
244
242
/**
@@ -263,7 +261,7 @@ public static SelectSpec create(SqlIdentifier table) {
263
261
List <String > projectedFields = Collections .emptyList ();
264
262
List <Expression > selectList = Collections .emptyList ();
265
263
return new SelectSpec (Table .create (table ), projectedFields , selectList , Criteria .empty (), Sort .unsorted (), -1 , -1 ,
266
- false );
264
+ false , null );
267
265
}
268
266
269
267
public SelectSpec doWithTable (BiFunction <Table , SelectSpec , SelectSpec > function ) {
@@ -305,7 +303,7 @@ public SelectSpec withProjection(Expression... expressions) {
305
303
selectList .addAll (Arrays .asList (expressions ));
306
304
307
305
return new SelectSpec (this .table , projectedFields , selectList , this .criteria , this .sort , this .limit , this .offset ,
308
- this .distinct );
306
+ this .distinct , this . lockMode );
309
307
}
310
308
311
309
/**
@@ -321,7 +319,7 @@ public SelectSpec withProjection(Collection<Expression> projectedFields) {
321
319
selectList .addAll (projectedFields );
322
320
323
321
return new SelectSpec (this .table , this .projectedFields , selectList , this .criteria , this .sort , this .limit ,
324
- this .offset , this .distinct );
322
+ this .offset , this .distinct , this . lockMode );
325
323
}
326
324
327
325
/**
@@ -332,7 +330,7 @@ public SelectSpec withProjection(Collection<Expression> projectedFields) {
332
330
*/
333
331
public SelectSpec withCriteria (CriteriaDefinition criteria ) {
334
332
return new SelectSpec (this .table , this .projectedFields , this .selectList , criteria , this .sort , this .limit ,
335
- this .offset , this .distinct );
333
+ this .offset , this .distinct , this . lockMode );
336
334
}
337
335
338
336
/**
@@ -345,11 +343,11 @@ public SelectSpec withSort(Sort sort) {
345
343
346
344
if (sort .isSorted ()) {
347
345
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , sort , this .limit ,
348
- this .offset , this .distinct );
346
+ this .offset , this .distinct , this . lockMode );
349
347
}
350
348
351
349
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , this .limit ,
352
- this .offset , this .distinct );
350
+ this .offset , this .distinct , this . lockMode );
353
351
}
354
352
355
353
/**
@@ -365,11 +363,11 @@ public SelectSpec withPage(Pageable page) {
365
363
Sort sort = page .getSort ();
366
364
367
365
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria ,
368
- sort .isSorted () ? sort : this .sort , page .getPageSize (), page .getOffset (), this .distinct );
366
+ sort .isSorted () ? sort : this .sort , page .getPageSize (), page .getOffset (), this .distinct , this . lockMode );
369
367
}
370
368
371
369
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , this .limit ,
372
- this .offset , this .distinct );
370
+ this .offset , this .distinct , this . lockMode );
373
371
}
374
372
375
373
/**
@@ -380,7 +378,7 @@ public SelectSpec withPage(Pageable page) {
380
378
*/
381
379
public SelectSpec offset (long offset ) {
382
380
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , this .limit ,
383
- offset , this .distinct );
381
+ offset , this .distinct , this . lockMode );
384
382
}
385
383
386
384
/**
@@ -391,7 +389,7 @@ public SelectSpec offset(long offset) {
391
389
*/
392
390
public SelectSpec limit (int limit ) {
393
391
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , limit ,
394
- this .offset , this .distinct );
392
+ this .offset , this .distinct , this . lockMode );
395
393
}
396
394
397
395
/**
@@ -401,7 +399,28 @@ public SelectSpec limit(int limit) {
401
399
*/
402
400
public SelectSpec distinct () {
403
401
return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , limit ,
404
- this .offset , true );
402
+ this .offset , true , this .lockMode );
403
+ }
404
+
405
+ /**
406
+ * Associate a lock mode with the select and create a new {@link SelectSpec}.
407
+ *
408
+ * @param lockMode the {@link LockMode} we want to use. This might be null
409
+ * @return the {@link SelectSpec}.
410
+ */
411
+ public SelectSpec lock (LockMode lockMode ) {
412
+ return new SelectSpec (this .table , this .projectedFields , this .selectList , this .criteria , this .sort , limit ,
413
+ this .offset , this .distinct , lockMode );
414
+ }
415
+
416
+ /**
417
+ * The used lockmode
418
+ *
419
+ * @return might be null if no lockmode defined.
420
+ */
421
+ @ Nullable
422
+ public LockMode getLock () {
423
+ return this .lockMode ;
405
424
}
406
425
407
426
public Table getTable () {
@@ -441,6 +460,7 @@ public int getLimit() {
441
460
public boolean isDistinct () {
442
461
return this .distinct ;
443
462
}
463
+
444
464
}
445
465
446
466
/**
0 commit comments