21
21
22
22
import io .r2dbc .spi .Parameters ;
23
23
import io .r2dbc .spi .R2dbcType ;
24
+ import io .r2dbc .spi .Row ;
24
25
import io .r2dbc .spi .test .MockColumnMetadata ;
25
26
import io .r2dbc .spi .test .MockResult ;
26
27
import io .r2dbc .spi .test .MockRow ;
62
63
import org .springframework .data .relational .core .query .Query ;
63
64
import org .springframework .data .relational .core .query .Update ;
64
65
import org .springframework .data .relational .core .sql .SqlIdentifier ;
66
+ import org .springframework .data .relational .domain .RowDocument ;
65
67
import org .springframework .lang .Nullable ;
66
68
import org .springframework .r2dbc .core .DatabaseClient ;
67
69
import org .springframework .r2dbc .core .Parameter ;
@@ -88,7 +90,8 @@ void before() {
88
90
client = DatabaseClient .builder ().connectionFactory (recorder )
89
91
.bindMarkers (PostgresDialect .INSTANCE .getBindMarkersFactory ()).build ();
90
92
91
- R2dbcCustomConversions conversions = R2dbcCustomConversions .of (PostgresDialect .INSTANCE , new MoneyConverter ());
93
+ R2dbcCustomConversions conversions = R2dbcCustomConversions .of (PostgresDialect .INSTANCE , new MoneyConverter (),
94
+ new RowConverter (), new RowDocumentConverter ());
92
95
93
96
entityTemplate = new R2dbcEntityTemplate (client , PostgresDialect .INSTANCE ,
94
97
new MappingR2dbcConverter (new R2dbcMappingContext (), conversions ));
@@ -611,6 +614,42 @@ void shouldConsiderParameterConverter() {
611
614
Parameter .from (Parameters .in (R2dbcType .VARCHAR , "$$$" )));
612
615
}
613
616
617
+ @ Test // GH-1696
618
+ void shouldConsiderRowConverter () {
619
+
620
+ MockRowMetadata metadata = MockRowMetadata .builder ()
621
+ .columnMetadata (MockColumnMetadata .builder ().name ("foo" ).type (R2dbcType .INTEGER ).build ())
622
+ .columnMetadata (MockColumnMetadata .builder ().name ("bar" ).type (R2dbcType .VARCHAR ).build ()).build ();
623
+ MockResult result = MockResult .builder ().row (MockRow .builder ().identified ("foo" , Object .class , 42 )
624
+ .identified ("bar" , String .class , "the-bar" ).metadata (metadata ).build ()).build ();
625
+
626
+ recorder .addStubbing (s -> s .startsWith ("SELECT" ), result );
627
+
628
+ entityTemplate .select (MyRowToEntityType .class ).all ().as (StepVerifier ::create ) //
629
+ .assertNext (actual -> {
630
+ assertThat (actual .foo ).isEqualTo (1 ); // converter-fixed value
631
+ assertThat (actual .bar ).isEqualTo ("the-bar" ); // converted value
632
+ }).verifyComplete ();
633
+ }
634
+
635
+ @ Test // GH-1696
636
+ void shouldConsiderRowDocumentConverter () {
637
+
638
+ MockRowMetadata metadata = MockRowMetadata .builder ()
639
+ .columnMetadata (MockColumnMetadata .builder ().name ("foo" ).type (R2dbcType .INTEGER ).build ())
640
+ .columnMetadata (MockColumnMetadata .builder ().name ("bar" ).type (R2dbcType .VARCHAR ).build ()).build ();
641
+ MockResult result = MockResult .builder ().row (MockRow .builder ().identified ("foo" , Object .class , 42 )
642
+ .identified ("bar" , Object .class , "the-bar" ).metadata (metadata ).build ()).build ();
643
+
644
+ recorder .addStubbing (s -> s .startsWith ("SELECT" ), result );
645
+
646
+ entityTemplate .select (MyRowDocumentToEntityType .class ).all ().as (StepVerifier ::create ) //
647
+ .assertNext (actual -> {
648
+ assertThat (actual .foo ).isEqualTo (1 ); // converter-fixed value
649
+ assertThat (actual .bar ).isEqualTo ("the-bar" ); // converted value
650
+ }).verifyComplete ();
651
+ }
652
+
614
653
record WithoutId (String name ) {
615
654
}
616
655
@@ -826,4 +865,30 @@ public io.r2dbc.spi.Parameter convert(Money source) {
826
865
}
827
866
828
867
}
868
+
869
+ record MyRowToEntityType (int foo , String bar ) {
870
+
871
+ }
872
+
873
+ static class RowConverter implements Converter <Row , MyRowToEntityType > {
874
+
875
+ @ Override
876
+ public MyRowToEntityType convert (Row source ) {
877
+ return new MyRowToEntityType (1 , source .get ("bar" , String .class ));
878
+ }
879
+
880
+ }
881
+
882
+ record MyRowDocumentToEntityType (int foo , String bar ) {
883
+
884
+ }
885
+
886
+ static class RowDocumentConverter implements Converter <RowDocument , MyRowDocumentToEntityType > {
887
+
888
+ @ Override
889
+ public MyRowDocumentToEntityType convert (RowDocument source ) {
890
+ return new MyRowDocumentToEntityType (1 , (String ) source .get ("bar" ));
891
+ }
892
+
893
+ }
829
894
}
0 commit comments