forked from debezium/debezium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostgresType.java
306 lines (273 loc) · 8.55 KB
/
PostgresType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.connector.postgresql;
import java.util.List;
import java.util.Objects;
import org.postgresql.core.Oid;
import org.postgresql.core.TypeInfo;
/**
* A class that binds together a PostgresSQL OID, JDBC type id and the string name of the type.
* The array types contain link to their element type.
*
* @author Jiri Pechanec
*
*/
public class PostgresType {
public static final PostgresType UNKNOWN = new PostgresType("unknown", -1, Integer.MIN_VALUE, null, null, null, null);
private final String name;
private final int oid;
private final int jdbcId;
private final PostgresType parentType;
private final PostgresType elementType;
private final TypeInfo typeInfo;
private final int modifiers;
private final List<String> enumValues;
private PostgresType(String name, int oid, int jdbcId, TypeInfo typeInfo, List<String> enumValues, PostgresType parentType, PostgresType elementType) {
this(name, oid, jdbcId, TypeRegistry.NO_TYPE_MODIFIER, typeInfo, enumValues, parentType, elementType);
}
private PostgresType(String name, int oid, int jdbcId, int modifiers, TypeInfo typeInfo, List<String> enumValues, PostgresType parentType, PostgresType elementType) {
Objects.requireNonNull(name);
this.name = name;
this.oid = oid;
this.jdbcId = jdbcId;
this.typeInfo = typeInfo;
this.parentType = parentType;
this.elementType = elementType;
this.modifiers = modifiers;
this.enumValues = enumValues;
}
/**
* @return true if this type is an array
*/
public boolean isArrayType() {
return elementType != null;
}
/**
* The type system allows for the creation of user defined types (UDTs) which can be based
* on any existing type. When a type does not extend another type, it is considered to be
* a base or root type in the type hierarchy.
*
* @return true if this type is a base/root type
*/
public boolean isRootType() {
return parentType == null;
}
/**
* @return true if this type is an enum type
*/
public boolean isEnumType() {
return enumValues != null;
}
/**
*
* @return symbolic name of the type
*/
public String getName() {
return name;
}
/**
*
* @return PostgreSQL OID of this type
*/
public int getOid() {
return oid;
}
/**
*
* @return JDBC id of the type as reported by JDBC metadata
*/
public int getJdbcId() {
return jdbcId;
}
/**
*
* @return the type of element in arrays or null for primitive types
*/
public PostgresType getElementType() {
return elementType;
}
/**
*
* @return the parent postgres type this type is based upon
*/
public PostgresType getParentType() {
return parentType;
}
/**
*
* @return the postgres type at the top/root level for this type's hierarchy
*/
public PostgresType getRootType() {
PostgresType rootType = this;
while (!rootType.isRootType()) {
rootType = rootType.getParentType();
}
return rootType;
}
public List<String> getEnumValues() {
return enumValues;
}
/**
*
* @return the default length of the type
*/
public int getDefaultLength() {
if (typeInfo == null) {
return TypeRegistry.UNKNOWN_LENGTH;
}
if (parentType != null) {
if (modifiers == TypeRegistry.NO_TYPE_MODIFIER) {
return parentType.getDefaultLength();
}
else {
int size = typeInfo.getPrecision(parentType.getOid(), modifiers);
if (size == 0) {
size = typeInfo.getDisplaySize(parentType.getOid(), modifiers);
}
if (size != 0 && size != Integer.MAX_VALUE) {
return size;
}
}
}
int size = typeInfo.getPrecision(oid, modifiers);
if (size == 0) {
size = typeInfo.getDisplaySize(oid, modifiers);
}
return size;
}
/**
*
* @return the default scale of the type
*/
public int getDefaultScale() {
if (typeInfo == null) {
return TypeRegistry.UNKNOWN_LENGTH;
}
if (parentType != null) {
if (modifiers == TypeRegistry.NO_TYPE_MODIFIER) {
return parentType.getDefaultScale();
}
else {
return typeInfo.getScale(parentType.getOid(), modifiers);
}
}
return typeInfo.getScale(oid, modifiers);
}
/**
* @param modifier - type modifier coming from decoder
* @return length of the type based on the modifier
*/
public int length(int modifier) {
if (typeInfo == null) {
return TypeRegistry.UNKNOWN_LENGTH;
}
switch (oid) {
case Oid.TIMESTAMP:
case Oid.TIMESTAMPTZ:
case Oid.TIME:
case Oid.TIMETZ:
case Oid.INTERVAL:
return typeInfo.getPrecision(oid, modifier);
}
return modifier;
}
/**
* Get the underlying postgres type information object
* @return the type information object; may be null
*/
public TypeInfo getTypeInfo() {
return typeInfo;
}
/**
* @param modifier - type modifier coming from decoder
* @return scale of the type based on the modifier
*/
public int scale(int modifier) {
if (typeInfo == null) {
return TypeRegistry.UNKNOWN_LENGTH;
}
switch (oid) {
case Oid.TIMESTAMP:
case Oid.TIMESTAMPTZ:
case Oid.TIME:
case Oid.TIMETZ:
case Oid.INTERVAL:
return typeInfo.getScale(oid, modifier);
}
return getDefaultScale();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + oid;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PostgresType other = (PostgresType) obj;
if (oid != other.oid) {
return false;
}
return true;
}
@Override
public String toString() {
return "PostgresType [name=" + name + ", oid=" + oid + ", jdbcId=" + jdbcId + ", modifiers=" + modifiers + ", defaultLength=" + getDefaultLength()
+ ", defaultScale=" + getDefaultScale() + ", parentType=" + parentType + ", elementType=" + elementType + "]";
}
public static class Builder {
private final TypeRegistry typeRegistry;
private final String name;
private final int oid;
private final int jdbcId;
private final int modifiers;
private final TypeInfo typeInfo;
private int parentTypeOid;
private int elementTypeOid;
private List<String> enumValues;
public Builder(TypeRegistry typeRegistry, String name, int oid, int jdbcId, int modifiers, TypeInfo typeInfo) {
this.typeRegistry = typeRegistry;
this.name = name;
this.oid = oid;
this.jdbcId = jdbcId;
this.modifiers = modifiers;
this.typeInfo = typeInfo;
}
public Builder parentType(int parentTypeOid) {
this.parentTypeOid = parentTypeOid;
return this;
}
public Builder elementType(int elementTypeOid) {
this.elementTypeOid = elementTypeOid;
return this;
}
public Builder enumValues(List<String> enumValues) {
this.enumValues = enumValues;
return this;
}
public PostgresType build() {
PostgresType parentType = null;
if (parentTypeOid != 0) {
parentType = typeRegistry.get(parentTypeOid);
}
PostgresType elementType = null;
if (elementTypeOid != 0) {
elementType = typeRegistry.get(elementTypeOid);
}
return new PostgresType(name, oid, jdbcId, modifiers, typeInfo, enumValues, parentType, elementType);
}
}
}