|
19 | 19 | import static org.mockito.Mockito.*;
|
20 | 20 | import static org.springframework.data.relational.core.query.Criteria.*;
|
21 | 21 |
|
| 22 | +import io.r2dbc.spi.Parameters; |
22 | 23 | import io.r2dbc.spi.R2dbcType;
|
23 | 24 | import io.r2dbc.spi.test.MockColumnMetadata;
|
24 | 25 | import io.r2dbc.spi.test.MockResult;
|
|
35 | 36 | import org.junit.jupiter.api.BeforeEach;
|
36 | 37 | import org.junit.jupiter.api.Test;
|
37 | 38 | import org.springframework.beans.factory.ObjectFactory;
|
| 39 | +import org.springframework.core.convert.converter.Converter; |
38 | 40 | import org.springframework.data.annotation.CreatedDate;
|
39 | 41 | import org.springframework.data.annotation.Id;
|
40 | 42 | import org.springframework.data.annotation.LastModifiedDate;
|
|
43 | 45 | import org.springframework.data.domain.Sort;
|
44 | 46 | import org.springframework.data.mapping.callback.ReactiveEntityCallbacks;
|
45 | 47 | import org.springframework.data.mapping.context.PersistentEntities;
|
| 48 | +import org.springframework.data.r2dbc.convert.MappingR2dbcConverter; |
| 49 | +import org.springframework.data.r2dbc.convert.R2dbcCustomConversions; |
46 | 50 | import org.springframework.data.r2dbc.dialect.PostgresDialect;
|
47 | 51 | import org.springframework.data.r2dbc.mapping.OutboundRow;
|
| 52 | +import org.springframework.data.r2dbc.mapping.R2dbcMappingContext; |
48 | 53 | import org.springframework.data.r2dbc.mapping.event.AfterConvertCallback;
|
49 | 54 | import org.springframework.data.r2dbc.mapping.event.AfterSaveCallback;
|
50 | 55 | import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
|
@@ -82,7 +87,11 @@ void before() {
|
82 | 87 | recorder = StatementRecorder.newInstance();
|
83 | 88 | client = DatabaseClient.builder().connectionFactory(recorder)
|
84 | 89 | .bindMarkers(PostgresDialect.INSTANCE.getBindMarkersFactory()).build();
|
85 |
| - entityTemplate = new R2dbcEntityTemplate(client, PostgresDialect.INSTANCE); |
| 90 | + |
| 91 | + R2dbcCustomConversions conversions = R2dbcCustomConversions.of(PostgresDialect.INSTANCE, new MoneyConverter()); |
| 92 | + |
| 93 | + entityTemplate = new R2dbcEntityTemplate(client, PostgresDialect.INSTANCE, |
| 94 | + new MappingR2dbcConverter(new R2dbcMappingContext(), conversions)); |
86 | 95 | }
|
87 | 96 |
|
88 | 97 | @Test // gh-220
|
@@ -147,7 +156,6 @@ void shouldProjectEntityUsingInheritedInterface() {
|
147 | 156 | assertThat(actual).isInstanceOf(Person.class);
|
148 | 157 | }).verifyComplete();
|
149 | 158 |
|
150 |
| - |
151 | 159 | StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("SELECT"));
|
152 | 160 | assertThat(statement.getSql()).isEqualTo("SELECT foo.* FROM foo WHERE foo.THE_NAME = $1");
|
153 | 161 | }
|
@@ -584,10 +592,29 @@ void updateExcludesInsertOnlyColumns() {
|
584 | 592 | Parameter.from(23L));
|
585 | 593 | }
|
586 | 594 |
|
| 595 | + @Test // GH-1696 |
| 596 | + void shouldConsiderParameterConverter() { |
| 597 | + |
| 598 | + MockRowMetadata metadata = MockRowMetadata.builder().build(); |
| 599 | + MockResult result = MockResult.builder().rowMetadata(metadata).rowsUpdated(1).build(); |
| 600 | + |
| 601 | + recorder.addStubbing(s -> s.startsWith("INSERT"), result); |
| 602 | + |
| 603 | + entityTemplate.insert(new WithMoney(null, new Money((byte) 1))).as(StepVerifier::create) // |
| 604 | + .expectNextCount(1) // |
| 605 | + .verifyComplete(); |
| 606 | + |
| 607 | + StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("INSERT")); |
| 608 | + |
| 609 | + assertThat(statement.getSql()).isEqualTo("INSERT INTO with_money (money) VALUES ($1)"); |
| 610 | + assertThat(statement.getBindings()).hasSize(1).containsEntry(0, |
| 611 | + Parameter.from(Parameters.in(R2dbcType.VARCHAR, "$$$"))); |
| 612 | + } |
| 613 | + |
587 | 614 | record WithoutId(String name) {
|
588 | 615 | }
|
589 | 616 |
|
590 |
| - interface Named{ |
| 617 | + interface Named { |
591 | 618 | String getName();
|
592 | 619 | }
|
593 | 620 |
|
@@ -784,4 +811,19 @@ public Mono<Person> onAfterConvert(Person entity, SqlIdentifier table) {
|
784 | 811 | return Mono.just(person);
|
785 | 812 | }
|
786 | 813 | }
|
| 814 | + |
| 815 | + record WithMoney(@Id Integer id, Money money) { |
| 816 | + } |
| 817 | + |
| 818 | + record Money(byte amount) { |
| 819 | + } |
| 820 | + |
| 821 | + static class MoneyConverter implements Converter<Money, io.r2dbc.spi.Parameter> { |
| 822 | + |
| 823 | + @Override |
| 824 | + public io.r2dbc.spi.Parameter convert(Money source) { |
| 825 | + return Parameters.in(R2dbcType.VARCHAR, "$$$"); |
| 826 | + } |
| 827 | + |
| 828 | + } |
787 | 829 | }
|
0 commit comments