|
2 | 2 |
|
3 | 3 | import com.datastax.oss.driver.api.core.data.TupleValue;
|
4 | 4 | import com.datastax.oss.driver.api.core.data.UdtValue;
|
5 |
| -import com.datastax.oss.driver.api.core.type.DataType; |
| 5 | +import com.datastax.oss.driver.api.core.type.*; |
6 | 6 | import lombok.Data;
|
7 | 7 |
|
8 | 8 | import java.math.BigDecimal;
|
|
15 | 15 |
|
16 | 16 | @Data
|
17 | 17 | public class TypeInfo {
|
18 |
| - private static Map<String, Class> typeMap = loadTypeMap(); |
| 18 | + private static Map<DataType, Class> cqlToJavaTypeMap = loadCqlToJavaTypeMap(); |
19 | 19 | private Class typeClass = Object.class;
|
20 | 20 | private List<Class> subTypes = new ArrayList<Class>();
|
21 | 21 | private boolean isCounter = false;
|
| 22 | + private boolean isFrozen = false; |
22 | 23 |
|
23 | 24 | public TypeInfo(DataType dataType) {
|
24 |
| - this(dataType.toString()); |
25 |
| - } |
26 |
| - |
27 |
| - public TypeInfo(String dataTypeStr) { |
28 |
| - int sIdx = dataTypeStr.indexOf('('); |
29 |
| - int eIdx = -1; |
30 |
| - if (sIdx != -1) { |
31 |
| - eIdx = dataTypeStr.substring(sIdx + 1).indexOf('('); |
32 |
| - if (eIdx == -1) { |
33 |
| - eIdx = dataTypeStr.substring(sIdx + 1).indexOf(','); |
34 |
| - } |
35 |
| - eIdx += sIdx + 1; |
36 |
| - } |
| 25 | + typeClass = getDataTypeToType(dataType); |
37 | 26 |
|
38 |
| - if (dataTypeStr.toLowerCase(Locale.ROOT).startsWith("list")) { |
39 |
| - typeClass = List.class; |
40 |
| - subTypes.add(typeMap.get(dataTypeStr.substring(sIdx + 1, eIdx).toLowerCase(Locale.ROOT))); |
41 |
| - } else if (dataTypeStr.toLowerCase(Locale.ROOT).startsWith("set")) { |
42 |
| - typeClass = Set.class; |
43 |
| - subTypes.add(typeMap.get(dataTypeStr.substring(sIdx + 1, eIdx).toLowerCase(Locale.ROOT))); |
44 |
| - } else if (dataTypeStr.toLowerCase(Locale.ROOT).startsWith("map")) { |
45 |
| - typeClass = Map.class; |
46 |
| - subTypes.add(typeMap.get(dataTypeStr.substring(sIdx + 1, dataTypeStr.indexOf("=>")).trim().toLowerCase(Locale.ROOT))); |
47 |
| - subTypes.add(typeMap.get(dataTypeStr.substring(dataTypeStr.indexOf("=>") + 2, dataTypeStr.indexOf(',')).trim().toLowerCase(Locale.ROOT))); |
48 |
| - } else if (dataTypeStr.toLowerCase(Locale.ROOT).startsWith("udt")) { |
49 |
| - typeClass = UdtValue.class; |
50 |
| - } else if (dataTypeStr.toLowerCase(Locale.ROOT).startsWith("tuple")) { |
51 |
| - typeClass = TupleValue.class; |
52 |
| - } else if (dataTypeStr.toLowerCase(Locale.ROOT).startsWith("counter")) { |
53 |
| - typeClass = Long.class; |
| 27 | + if (dataType instanceof UserDefinedType) { |
| 28 | + isFrozen = ((UserDefinedType) dataType).isFrozen(); |
| 29 | + } else if (dataType instanceof ListType) { |
| 30 | + subTypes.add(getDataTypeToType(((ListType) dataType).getElementType())); |
| 31 | + isFrozen = ((ListType) dataType).isFrozen(); |
| 32 | + } else if (dataType instanceof SetType) { |
| 33 | + subTypes.add(getDataTypeToType(((SetType) dataType).getElementType())); |
| 34 | + isFrozen = ((SetType) dataType).isFrozen(); |
| 35 | + } else if (dataType instanceof MapType) { |
| 36 | + subTypes.add(getDataTypeToType(((MapType) dataType).getKeyType())); |
| 37 | + subTypes.add(getDataTypeToType(((MapType) dataType).getValueType())); |
| 38 | + isFrozen = ((MapType) dataType).isFrozen(); |
| 39 | + } else if (DataTypes.COUNTER.equals(dataType)) { |
54 | 40 | isCounter = true;
|
55 |
| - } else { |
56 |
| - typeClass = typeMap.get(dataTypeStr.toLowerCase(Locale.ROOT)); |
57 | 41 | }
|
58 | 42 | }
|
59 | 43 |
|
60 |
| - private static Map loadTypeMap() { |
61 |
| - Map typeMap = new HashMap<>(); |
62 |
| - typeMap.put("ascii", String.class); |
63 |
| - typeMap.put("bigint", Long.class); |
64 |
| - typeMap.put("blob", ByteBuffer.class); |
65 |
| - typeMap.put("boolean", Boolean.class); |
66 |
| - typeMap.put("counter", Long.class); |
67 |
| - typeMap.put("date", LocalDate.class); |
68 |
| - typeMap.put("decimal", BigDecimal.class); |
69 |
| - typeMap.put("double", Double.class); |
70 |
| - typeMap.put("float", Float.class); |
71 |
| - typeMap.put("int", Integer.class); |
72 |
| - typeMap.put("inet", String.class); |
73 |
| - typeMap.put("smallint", Short.class); |
74 |
| - typeMap.put("text", String.class); |
75 |
| - typeMap.put("time", LocalTime.class); |
76 |
| - typeMap.put("timestamp", Instant.class); |
77 |
| - typeMap.put("timeuuid", UUID.class); |
78 |
| - typeMap.put("tinyint", Byte.class); |
79 |
| - typeMap.put("udt", UdtValue.class); |
80 |
| - typeMap.put("uuid", UUID.class); |
81 |
| - typeMap.put("varchar", String.class); |
82 |
| - typeMap.put("varint", BigInteger.class); |
| 44 | + private static Map loadCqlToJavaTypeMap() { |
| 45 | + Map<DataType, Class> typeMap = new HashMap<>(); |
| 46 | + typeMap.put(DataTypes.ASCII, String.class); |
| 47 | + typeMap.put(DataTypes.BIGINT, Long.class); |
| 48 | + typeMap.put(DataTypes.BLOB, ByteBuffer.class); |
| 49 | + typeMap.put(DataTypes.BOOLEAN, Boolean.class); |
| 50 | + typeMap.put(DataTypes.COUNTER, Long.class); |
| 51 | + typeMap.put(DataTypes.DATE, LocalDate.class); |
| 52 | + typeMap.put(DataTypes.DECIMAL, BigDecimal.class); |
| 53 | + typeMap.put(DataTypes.DOUBLE, Double.class); |
| 54 | + typeMap.put(DataTypes.FLOAT, Float.class); |
| 55 | + typeMap.put(DataTypes.INT, Integer.class); |
| 56 | + typeMap.put(DataTypes.INET, String.class); |
| 57 | + typeMap.put(DataTypes.SMALLINT, Short.class); |
| 58 | + typeMap.put(DataTypes.TEXT, String.class); |
| 59 | + typeMap.put(DataTypes.TIME, LocalTime.class); |
| 60 | + typeMap.put(DataTypes.TIMESTAMP, Instant.class); |
| 61 | + typeMap.put(DataTypes.TIMEUUID, UUID.class); |
| 62 | + typeMap.put(DataTypes.TINYINT, Byte.class); |
| 63 | + typeMap.put(DataTypes.UUID, UUID.class); |
| 64 | + typeMap.put(DataTypes.VARINT, BigInteger.class); |
83 | 65 |
|
84 | 66 | return typeMap;
|
85 | 67 | }
|
86 | 68 |
|
| 69 | + private Class getDataTypeToType(DataType dataType) { |
| 70 | + if (dataType instanceof UserDefinedType) return UdtValue.class; |
| 71 | + else if (dataType instanceof ListType) { |
| 72 | + return List.class; |
| 73 | + } else if (dataType instanceof SetType) { |
| 74 | + return Set.class; |
| 75 | + } else if (dataType instanceof MapType) { |
| 76 | + return Map.class; |
| 77 | + } else if (dataType instanceof TupleType) { |
| 78 | + return TupleValue.class; |
| 79 | + } |
| 80 | + |
| 81 | + return cqlToJavaTypeMap.get(dataType); |
| 82 | + } |
| 83 | + |
87 | 84 | public String toString() {
|
88 | 85 | return "Type: " + typeClass.toString() + " SubTypes: " + subTypes.toString();
|
89 | 86 | }
|
|
0 commit comments