18
18
19
19
import io .r2dbc .postgresql .api .PostgresqlConnection ;
20
20
import io .r2dbc .postgresql .util .Assert ;
21
+ import io .r2dbc .spi .Row ;
22
+ import io .r2dbc .spi .RowMetadata ;
21
23
import io .r2dbc .spi .Type ;
22
24
import reactor .core .publisher .Flux ;
23
25
import reactor .core .publisher .Mono ;
35
37
*/
36
38
public class PostgresTypes {
37
39
40
+ public final static int NO_SUCH_TYPE = -1 ;
41
+
38
42
// parameterized with %s for the comparator (=, IN), %s for the actual criteria value and %s for a potential LIMIT 1 statement
39
43
private static final String SELECT_PG_TYPE = "SELECT pg_type.oid, typarray, typname, typcategory "
40
44
+ " FROM pg_catalog.pg_type "
@@ -74,13 +78,7 @@ public Mono<PostgresType> lookupType(String typeName) {
74
78
}
75
79
76
80
return this .connection .createStatement (String .format (SELECT_PG_TYPE , "=" , "'" + typeName + "'" , "LIMIT 1" )).execute ()
77
- .flatMap (it -> it .map ((row , rowMetadata ) -> {
78
-
79
- Long oid = row .get ("oid" , Long .class );
80
- Long typarrayOid = row .get ("typarray" , Long .class );
81
- return new PostgresType (PostgresqlObjectId .toInt (oid ), oid .longValue (), PostgresqlObjectId .toInt (typarrayOid ), typarrayOid , row .get ("typname" , String .class ), row .get ("typcategory" ,
82
- String .class ));
83
- })).singleOrEmpty ();
81
+ .flatMap (it -> it .map (PostgresTypes ::createType )).singleOrEmpty ();
84
82
}
85
83
86
84
public Flux <PostgresType > lookupTypes (Iterable <String > typeNames ) {
@@ -103,13 +101,18 @@ public Flux<PostgresType> lookupTypes(Iterable<String> typeNames) {
103
101
}
104
102
105
103
return this .connection .createStatement (String .format (SELECT_PG_TYPE , "IN" , joiner , "" )).execute ()
106
- .flatMap (it -> it .map ((row , rowMetadata ) -> {
104
+ .flatMap (it -> it .map (PostgresTypes ::createType ));
105
+ }
106
+
107
+ private static PostgresType createType (Row row , RowMetadata rowMetadata ) {
108
+ Long oid = row .get ("oid" , Long .class );
109
+ String typname = row .get ("typname" , String .class );
110
+ String typcategory = row .get ("typcategory" , String .class );
111
+ Long typarrayOid = rowMetadata .contains ("typarray" ) ? row .get ("typarray" , Long .class ) : null ;
107
112
108
- Long oid = row .get ("oid" , Long .class );
109
- Long typarrayOid = row .get ("typarray" , Long .class );
110
- return new PostgresType (PostgresqlObjectId .toInt (oid ), oid .longValue (), PostgresqlObjectId .toInt (typarrayOid ), typarrayOid , row .get ("typname" , String .class ), row .get ("typcategory" ,
111
- String .class ));
112
- }));
113
+ long unsignedTyparray = typarrayOid != null ? typarrayOid : NO_SUCH_TYPE ;
114
+ int typarray = typarrayOid != null ? PostgresqlObjectId .toInt (typarrayOid ) : NO_SUCH_TYPE ;
115
+ return new PostgresType (PostgresqlObjectId .toInt (oid ), oid , typarray , unsignedTyparray , typname , typcategory );
113
116
}
114
117
115
118
public static class PostgresType implements Type , PostgresTypeIdentifier {
0 commit comments