Skip to content

Commit a5e2a8e

Browse files
committed
#2 - Polishing.
Introduce customization hook methods. Rename DefaultTypedGenericExecuteSpec to DefaultTypedExecuteSpec and GenericExecuteSpecSupport to ExecuteSpecSupport as types are not tied to generic execution. Encapsulate fields in DatabaseClient builders.
1 parent 20eb2c1 commit a5e2a8e

File tree

3 files changed

+62
-41
lines changed

3 files changed

+62
-41
lines changed

src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClient.java

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,29 @@ protected DataAccessException translateException(String task, @Nullable String s
216216
return (dae != null ? dae : new UncategorizedR2dbcException(task, sql, ex));
217217
}
218218

219+
/**
220+
* Customization hook.
221+
*/
222+
protected <T> DefaultTypedExecuteSpec<T> createTypedExecuteSpec(Map<Integer, SettableValue> byIndex,
223+
Map<String, SettableValue> byName, Supplier<String> sqlSupplier, Class<T> typeToRead) {
224+
return new DefaultTypedExecuteSpec<>(byIndex, byName, sqlSupplier, typeToRead);
225+
}
226+
227+
/**
228+
* Customization hook.
229+
*/
230+
protected ExecuteSpecSupport createGenericExecuteSpec(Map<Integer, SettableValue> byIndex,
231+
Map<String, SettableValue> byName, Supplier<String> sqlSupplier) {
232+
return new DefaultGenericExecuteSpec(byIndex, byName, sqlSupplier);
233+
}
234+
235+
/**
236+
* Customization hook.
237+
*/
238+
protected DefaultGenericExecuteSpec createGenericExecuteSpec(Supplier<String> sqlSupplier) {
239+
return new DefaultGenericExecuteSpec(sqlSupplier);
240+
}
241+
219242
private static void doBind(Statement statement, Map<String, SettableValue> byName,
220243
Map<Integer, SettableValue> byIndex) {
221244

@@ -236,7 +259,6 @@ private static void doBind(Statement statement, Map<String, SettableValue> byNam
236259
statement.bindNull(name, o.getType());
237260
}
238261
});
239-
240262
}
241263

242264
/**
@@ -256,21 +278,21 @@ public GenericExecuteSpec sql(Supplier<String> sqlSupplier) {
256278

257279
Assert.notNull(sqlSupplier, "SQL Supplier must not be null!");
258280

259-
return new DefaultGenericExecuteSpec(sqlSupplier);
281+
return createGenericExecuteSpec(sqlSupplier);
260282
}
261283
}
262284

263285
/**
264286
* Base class for {@link DatabaseClient.GenericExecuteSpec} implementations.
265287
*/
266288
@RequiredArgsConstructor
267-
private class GenericExecuteSpecSupport {
289+
class ExecuteSpecSupport {
268290

269291
final Map<Integer, SettableValue> byIndex;
270292
final Map<String, SettableValue> byName;
271293
final Supplier<String> sqlSupplier;
272294

273-
GenericExecuteSpecSupport(Supplier<String> sqlSupplier) {
295+
ExecuteSpecSupport(Supplier<String> sqlSupplier) {
274296

275297
this.byIndex = Collections.emptyMap();
276298
this.byName = Collections.emptyMap();
@@ -284,7 +306,7 @@ protected String getSql() {
284306
return sql;
285307
}
286308

287-
protected <T> SqlResult<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
309+
<T> SqlResult<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFunction) {
288310

289311
Function<Connection, Statement> executeFunction = it -> {
290312

@@ -307,23 +329,23 @@ protected <T> SqlResult<T> exchange(String sql, BiFunction<Row, RowMetadata, T>
307329
mappingFunction);
308330
}
309331

310-
public GenericExecuteSpecSupport bind(int index, Object value) {
332+
public ExecuteSpecSupport bind(int index, Object value) {
311333

312334
Map<Integer, SettableValue> byIndex = new LinkedHashMap<>(this.byIndex);
313335
byIndex.put(index, new SettableValue(index, value, null));
314336

315337
return createInstance(byIndex, this.byName, this.sqlSupplier);
316338
}
317339

318-
public GenericExecuteSpecSupport bindNull(int index, Class<?> type) {
340+
public ExecuteSpecSupport bindNull(int index, Class<?> type) {
319341

320342
Map<Integer, SettableValue> byIndex = new LinkedHashMap<>(this.byIndex);
321343
byIndex.put(index, new SettableValue(index, null, type));
322344

323345
return createInstance(byIndex, this.byName, this.sqlSupplier);
324346
}
325347

326-
public GenericExecuteSpecSupport bind(String name, Object value) {
348+
public ExecuteSpecSupport bind(String name, Object value) {
327349

328350
Assert.hasText(name, "Parameter name must not be null or empty!");
329351

@@ -333,7 +355,7 @@ public GenericExecuteSpecSupport bind(String name, Object value) {
333355
return createInstance(this.byIndex, byName, this.sqlSupplier);
334356
}
335357

336-
public GenericExecuteSpecSupport bindNull(String name, Class<?> type) {
358+
public ExecuteSpecSupport bindNull(String name, Class<?> type) {
337359

338360
Assert.hasText(name, "Parameter name must not be null or empty!");
339361

@@ -343,12 +365,12 @@ public GenericExecuteSpecSupport bindNull(String name, Class<?> type) {
343365
return createInstance(this.byIndex, byName, this.sqlSupplier);
344366
}
345367

346-
protected GenericExecuteSpecSupport createInstance(Map<Integer, SettableValue> byIndex,
347-
Map<String, SettableValue> byName, Supplier<String> sqlSupplier) {
348-
return new GenericExecuteSpecSupport(byIndex, byName, sqlSupplier);
368+
protected ExecuteSpecSupport createInstance(Map<Integer, SettableValue> byIndex, Map<String, SettableValue> byName,
369+
Supplier<String> sqlSupplier) {
370+
return new ExecuteSpecSupport(byIndex, byName, sqlSupplier);
349371
}
350372

351-
public GenericExecuteSpecSupport bind(Object bean) {
373+
public ExecuteSpecSupport bind(Object bean) {
352374

353375
Assert.notNull(bean, "Bean must not be null!");
354376

@@ -359,7 +381,7 @@ public GenericExecuteSpecSupport bind(Object bean) {
359381
/**
360382
* Default {@link DatabaseClient.GenericExecuteSpec} implementation.
361383
*/
362-
private class DefaultGenericExecuteSpec extends GenericExecuteSpecSupport implements GenericExecuteSpec {
384+
protected class DefaultGenericExecuteSpec extends ExecuteSpecSupport implements GenericExecuteSpec {
363385

364386
DefaultGenericExecuteSpec(Map<Integer, SettableValue> byIndex, Map<String, SettableValue> byName,
365387
Supplier<String> sqlSupplier) {
@@ -375,7 +397,7 @@ public <R> TypedExecuteSpec<R> as(Class<R> resultType) {
375397

376398
Assert.notNull(resultType, "Result type must not be null!");
377399

378-
return new DefaultTypedGenericExecuteSpec<>(this.byIndex, this.byName, this.sqlSupplier, resultType);
400+
return createTypedExecuteSpec(this.byIndex, this.byName, this.sqlSupplier, resultType);
379401
}
380402

381403
@Override
@@ -414,22 +436,22 @@ public DefaultGenericExecuteSpec bind(Object bean) {
414436
}
415437

416438
@Override
417-
protected GenericExecuteSpecSupport createInstance(Map<Integer, SettableValue> byIndex,
418-
Map<String, SettableValue> byName, Supplier<String> sqlSupplier) {
419-
return new DefaultGenericExecuteSpec(byIndex, byName, sqlSupplier);
439+
protected ExecuteSpecSupport createInstance(Map<Integer, SettableValue> byIndex, Map<String, SettableValue> byName,
440+
Supplier<String> sqlSupplier) {
441+
return createGenericExecuteSpec(byIndex, byName, sqlSupplier);
420442
}
421443
}
422444

423445
/**
424446
* Default {@link DatabaseClient.GenericExecuteSpec} implementation.
425447
*/
426448
@SuppressWarnings("unchecked")
427-
private class DefaultTypedGenericExecuteSpec<T> extends GenericExecuteSpecSupport implements TypedExecuteSpec<T> {
449+
protected class DefaultTypedExecuteSpec<T> extends ExecuteSpecSupport implements TypedExecuteSpec<T> {
428450

429451
private final Class<T> typeToRead;
430452
private final BiFunction<Row, RowMetadata, T> mappingFunction;
431453

432-
DefaultTypedGenericExecuteSpec(Map<Integer, SettableValue> byIndex, Map<String, SettableValue> byName,
454+
DefaultTypedExecuteSpec(Map<Integer, SettableValue> byIndex, Map<String, SettableValue> byName,
433455
Supplier<String> sqlSupplier, Class<T> typeToRead) {
434456

435457
super(byIndex, byName, sqlSupplier);
@@ -443,7 +465,7 @@ public <R> TypedExecuteSpec<R> as(Class<R> resultType) {
443465

444466
Assert.notNull(resultType, "Result type must not be null!");
445467

446-
return new DefaultTypedGenericExecuteSpec<>(this.byIndex, this.byName, this.sqlSupplier, resultType);
468+
return createTypedExecuteSpec(this.byIndex, this.byName, this.sqlSupplier, resultType);
447469
}
448470

449471
@Override
@@ -457,34 +479,34 @@ public Mono<SqlResult<T>> exchange() {
457479
}
458480

459481
@Override
460-
public DefaultTypedGenericExecuteSpec<T> bind(int index, Object value) {
461-
return (DefaultTypedGenericExecuteSpec<T>) super.bind(index, value);
482+
public DefaultTypedExecuteSpec<T> bind(int index, Object value) {
483+
return (DefaultTypedExecuteSpec<T>) super.bind(index, value);
462484
}
463485

464486
@Override
465-
public DefaultTypedGenericExecuteSpec<T> bindNull(int index, Class<?> type) {
466-
return (DefaultTypedGenericExecuteSpec<T>) super.bindNull(index, type);
487+
public DefaultTypedExecuteSpec<T> bindNull(int index, Class<?> type) {
488+
return (DefaultTypedExecuteSpec<T>) super.bindNull(index, type);
467489
}
468490

469491
@Override
470-
public DefaultTypedGenericExecuteSpec<T> bind(String name, Object value) {
471-
return (DefaultTypedGenericExecuteSpec) super.bind(name, value);
492+
public DefaultTypedExecuteSpec<T> bind(String name, Object value) {
493+
return (DefaultTypedExecuteSpec) super.bind(name, value);
472494
}
473495

474496
@Override
475-
public DefaultTypedGenericExecuteSpec<T> bindNull(String name, Class<?> type) {
476-
return (DefaultTypedGenericExecuteSpec<T>) super.bindNull(name, type);
497+
public DefaultTypedExecuteSpec<T> bindNull(String name, Class<?> type) {
498+
return (DefaultTypedExecuteSpec<T>) super.bindNull(name, type);
477499
}
478500

479501
@Override
480-
public DefaultTypedGenericExecuteSpec<T> bind(Object bean) {
481-
return (DefaultTypedGenericExecuteSpec<T>) super.bind(bean);
502+
public DefaultTypedExecuteSpec<T> bind(Object bean) {
503+
return (DefaultTypedExecuteSpec<T>) super.bind(bean);
482504
}
483505

484506
@Override
485-
protected DefaultTypedGenericExecuteSpec<T> createInstance(Map<Integer, SettableValue> byIndex,
507+
protected DefaultTypedExecuteSpec<T> createInstance(Map<Integer, SettableValue> byIndex,
486508
Map<String, SettableValue> byName, Supplier<String> sqlSupplier) {
487-
return new DefaultTypedGenericExecuteSpec<>(byIndex, byName, sqlSupplier, typeToRead);
509+
return createTypedExecuteSpec(byIndex, byName, sqlSupplier, typeToRead);
488510
}
489511
}
490512

@@ -550,8 +572,8 @@ public DefaultSelectSpecSupport page(Pageable page) {
550572
}
551573

552574
StringBuilder getLimitOffset(Pageable pageable) {
553-
return new StringBuilder().append("LIMIT").append(' ').append(page.getPageSize()) //
554-
.append(' ').append("OFFSET").append(' ').append(page.getOffset());
575+
return new StringBuilder().append("LIMIT").append(' ').append(pageable.getPageSize()) //
576+
.append(' ').append("OFFSET").append(' ').append(pageable.getOffset());
555577
}
556578

557579
StringBuilder getSortClause(Sort sort) {

src/main/java/org/springframework/data/r2dbc/function/DefaultDatabaseClientBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
*/
3434
class DefaultDatabaseClientBuilder implements DatabaseClient.Builder {
3535

36-
@Nullable ConnectionFactory connector;
37-
@Nullable R2dbcExceptionTranslator exceptionTranslator;
38-
ReactiveDataAccessStrategy accessStrategy = new DefaultReactiveDataAccessStrategy();
36+
private @Nullable ConnectionFactory connector;
37+
private @Nullable R2dbcExceptionTranslator exceptionTranslator;
38+
private ReactiveDataAccessStrategy accessStrategy = new DefaultReactiveDataAccessStrategy();
3939

4040
DefaultDatabaseClientBuilder() {}
4141

@@ -45,6 +45,7 @@ class DefaultDatabaseClientBuilder implements DatabaseClient.Builder {
4545

4646
this.connector = other.connector;
4747
this.exceptionTranslator = other.exceptionTranslator;
48+
this.accessStrategy = other.accessStrategy;
4849
}
4950

5051
@Override

src/main/java/org/springframework/data/r2dbc/function/DefaultTransactionalDatabaseClientBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ class DefaultTransactionalDatabaseClientBuilder extends DefaultDatabaseClientBui
3333

3434
DefaultTransactionalDatabaseClientBuilder(DefaultDatabaseClientBuilder other) {
3535

36+
super(other);
3637
Assert.notNull(other, "DefaultDatabaseClientBuilder must not be null!");
37-
38-
this.connector = other.connector;
39-
this.exceptionTranslator = other.exceptionTranslator;
4038
}
4139

4240
@Override

0 commit comments

Comments
 (0)