35
35
import com .ing .data .cassandra .jdbc .types .DataTypeEnum ;
36
36
import com .ing .data .cassandra .jdbc .types .TypesMap ;
37
37
import com .ing .data .cassandra .jdbc .utils .ArrayImpl ;
38
+ import com .ing .data .cassandra .jdbc .utils .UdtUtil .WithFormattedContentsDefaultUdtValue ;
38
39
import org .apache .commons .collections4 .IteratorUtils ;
39
40
import org .apache .commons .io .IOUtils ;
40
41
import org .apache .commons .lang3 .StringUtils ;
109
110
import static com .ing .data .cassandra .jdbc .utils .ErrorConstants .VECTOR_ELEMENTS_NOT_NUMBERS ;
110
111
import static com .ing .data .cassandra .jdbc .utils .ErrorConstants .WAS_CLOSED_RS ;
111
112
import static com .ing .data .cassandra .jdbc .utils .JsonUtil .getObjectMapper ;
113
+ import static com .ing .data .cassandra .jdbc .utils .UdtUtil .udtValueUsingFormattedContents ;
114
+ import static com .ing .data .cassandra .jdbc .utils .UdtUtil .udtValuesUsingFormattedContents ;
112
115
113
116
/**
114
117
* Cassandra result set: implementation class for {@link java.sql.ResultSet}.
166
169
* and {@link java.sql.ResultSet#getArray(String)}.
167
170
* </p>
168
171
*
172
+ * @implNote User-defined types (UDT) values, including such values in lists, sets and maps, returned by the methods
173
+ * {@link #getObject(int)} and {@link #getObject(String)} are instances of {@link WithFormattedContentsDefaultUdtValue}.
174
+ * Be careful when using the method {@link WithFormattedContentsDefaultUdtValue#toString()} on these values to avoid
175
+ * data leaks (e.g. in application logs). Some borderline cases, especially complex types involving nested collections
176
+ * and nested UDT values may be not working correctly.
169
177
* @see ResultSet
170
178
*/
171
179
public class CassandraResultSet extends AbstractResultSet
@@ -913,8 +921,8 @@ public Object getObject(final int columnIndex) throws SQLException {
913
921
final Set <?> resultSet ;
914
922
915
923
if (elementsType instanceof UserDefinedType ) {
916
- resultSet = this .currentRow .getSet (columnIndex - 1 ,
917
- TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ());
924
+ resultSet = udtValuesUsingFormattedContents ( this .currentRow .getSet (columnIndex - 1 ,
925
+ TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ())) ;
918
926
} else if (elementsType instanceof TupleType ) {
919
927
resultSet = this .currentRow .getSet (columnIndex - 1 ,
920
928
TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ());
@@ -935,8 +943,8 @@ public Object getObject(final int columnIndex) throws SQLException {
935
943
final List <?> resultList ;
936
944
937
945
if (elementsType instanceof UserDefinedType ) {
938
- resultList = this .currentRow .getList (columnIndex - 1 ,
939
- TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ());
946
+ resultList = udtValuesUsingFormattedContents ( this .currentRow .getList (columnIndex - 1 ,
947
+ TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ())) ;
940
948
} else if (elementsType instanceof TupleType ) {
941
949
resultList = this .currentRow .getList (columnIndex - 1 ,
942
950
TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ());
@@ -960,17 +968,20 @@ public Object getObject(final int columnIndex) throws SQLException {
960
968
final MapType mapType = (MapType ) cqlDataType ;
961
969
final DataType keyType = mapType .getKeyType ();
962
970
final DataType valueType = mapType .getValueType ();
971
+ boolean containsUdtValues = false ;
963
972
964
973
Class <?> keyClass = TypesMap .getTypeForComparator (keyType .asCql (false , false )).getType ();
965
974
if (keyType instanceof UserDefinedType ) {
966
975
keyClass = TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ();
976
+ containsUdtValues = true ;
967
977
} else if (keyType instanceof TupleType ) {
968
978
keyClass = TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ();
969
979
}
970
980
971
981
Class <?> valueClass = TypesMap .getTypeForComparator (valueType .asCql (false , false )).getType ();
972
982
if (valueType instanceof UserDefinedType ) {
973
983
valueClass = TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ();
984
+ containsUdtValues = true ;
974
985
} else if (valueType instanceof TupleType ) {
975
986
valueClass = TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ();
976
987
}
@@ -979,6 +990,9 @@ public Object getObject(final int columnIndex) throws SQLException {
979
990
if (resultMap == null ) {
980
991
return null ;
981
992
}
993
+ if (containsUdtValues ) {
994
+ return udtValuesUsingFormattedContents (resultMap );
995
+ }
982
996
return new HashMap <>(resultMap );
983
997
}
984
998
} else {
@@ -1023,7 +1037,7 @@ public Object getObject(final int columnIndex) throws SQLException {
1023
1037
case TIMEUUID :
1024
1038
return this .currentRow .getUuid (columnIndex - 1 );
1025
1039
case UDT :
1026
- return this .currentRow .getUdtValue (columnIndex - 1 );
1040
+ return udtValueUsingFormattedContents ( this .currentRow .getUdtValue (columnIndex - 1 ) );
1027
1041
case TUPLE :
1028
1042
return this .currentRow .getTupleValue (columnIndex - 1 );
1029
1043
}
@@ -1051,8 +1065,8 @@ public Object getObject(final String columnLabel) throws SQLException {
1051
1065
final Set <?> resultSet ;
1052
1066
1053
1067
if (elementsType instanceof UserDefinedType ) {
1054
- resultSet = this .currentRow .getSet (columnLabel ,
1055
- TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ());
1068
+ resultSet = udtValuesUsingFormattedContents ( this .currentRow .getSet (columnLabel ,
1069
+ TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ())) ;
1056
1070
} else if (elementsType instanceof TupleType ) {
1057
1071
resultSet = this .currentRow .getSet (columnLabel ,
1058
1072
TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ());
@@ -1073,8 +1087,8 @@ public Object getObject(final String columnLabel) throws SQLException {
1073
1087
final List <?> resultList ;
1074
1088
1075
1089
if (elementsType instanceof UserDefinedType ) {
1076
- resultList = this .currentRow .getList (columnLabel ,
1077
- TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ());
1090
+ resultList = udtValuesUsingFormattedContents ( this .currentRow .getList (columnLabel ,
1091
+ TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ())) ;
1078
1092
} else if (elementsType instanceof TupleType ) {
1079
1093
resultList = this .currentRow .getList (columnLabel ,
1080
1094
TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ());
@@ -1098,17 +1112,20 @@ public Object getObject(final String columnLabel) throws SQLException {
1098
1112
final MapType mapType = (MapType ) cqlDataType ;
1099
1113
final DataType keyType = mapType .getKeyType ();
1100
1114
final DataType valueType = mapType .getValueType ();
1115
+ boolean containsUdtValues = false ;
1101
1116
1102
1117
Class <?> keyClass = TypesMap .getTypeForComparator (keyType .asCql (false , false )).getType ();
1103
1118
if (keyType instanceof UserDefinedType ) {
1104
1119
keyClass = TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ();
1120
+ containsUdtValues = true ;
1105
1121
} else if (keyType instanceof TupleType ) {
1106
1122
keyClass = TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ();
1107
1123
}
1108
1124
1109
1125
Class <?> valueClass = TypesMap .getTypeForComparator (valueType .asCql (false , false )).getType ();
1110
1126
if (valueType instanceof UserDefinedType ) {
1111
1127
valueClass = TypesMap .getTypeForComparator (DataTypeEnum .UDT .asLowercaseCql ()).getType ();
1128
+ containsUdtValues = true ;
1112
1129
} else if (valueType instanceof TupleType ) {
1113
1130
valueClass = TypesMap .getTypeForComparator (DataTypeEnum .TUPLE .asLowercaseCql ()).getType ();
1114
1131
}
@@ -1117,6 +1134,9 @@ public Object getObject(final String columnLabel) throws SQLException {
1117
1134
if (resultMap == null ) {
1118
1135
return null ;
1119
1136
}
1137
+ if (containsUdtValues ) {
1138
+ return udtValuesUsingFormattedContents (resultMap );
1139
+ }
1120
1140
return new HashMap <>(resultMap );
1121
1141
}
1122
1142
} else {
@@ -1161,7 +1181,7 @@ public Object getObject(final String columnLabel) throws SQLException {
1161
1181
case TIMEUUID :
1162
1182
return this .currentRow .getUuid (columnLabel );
1163
1183
case UDT :
1164
- return this .currentRow .getUdtValue (columnLabel );
1184
+ return udtValueUsingFormattedContents ( this .currentRow .getUdtValue (columnLabel ) );
1165
1185
case TUPLE :
1166
1186
return this .currentRow .getTupleValue (columnLabel );
1167
1187
}
0 commit comments