forked from debezium/debezium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostgresType.java
226 lines (204 loc) · 5.98 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
/*
* 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.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);
private final String name;
private final int oid;
private final int jdbcId;
private final PostgresType baseType;
private final PostgresType elementType;
private final TypeInfo typeInfo;
private final int modifiers;
public PostgresType(String name, int oid, int jdbcId, TypeInfo typeInfo, PostgresType baseType, PostgresType elementType) {
this(name, oid, jdbcId, TypeRegistry.NO_TYPE_MODIFIER, typeInfo, baseType, elementType);
}
public PostgresType(String name, int oid, int jdbcId, int modifiers, TypeInfo typeInfo, PostgresType baseType, PostgresType elementType) {
Objects.requireNonNull(name);
this.name = name;
this.oid = oid;
this.jdbcId = jdbcId;
this.typeInfo = typeInfo;
this.baseType = baseType;
this.elementType = elementType;
this.modifiers = modifiers;
}
/**
* @return true if this type is an array
*/
public boolean isArrayType() {
return elementType != null;
}
/**
* @return true if this type is a base type
*/
public boolean isBaseType() {
return baseType == 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 base postgres type this type is based upon
*/
public PostgresType getBaseType() {
return baseType;
}
/**
*
* @return the default length of the type
*/
public int getDefaultLength() {
if (typeInfo == null) {
return TypeRegistry.UNKNOWN_LENGTH;
}
if (baseType != null) {
if (modifiers == TypeRegistry.NO_TYPE_MODIFIER) {
return baseType.getDefaultLength();
}
else {
int size = typeInfo.getPrecision(baseType.getOid(), modifiers);
if (size == 0) {
size = typeInfo.getDisplaySize(baseType.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 (baseType != null) {
if (modifiers == TypeRegistry.NO_TYPE_MODIFIER) {
return baseType.getDefaultScale();
}
else {
return typeInfo.getScale(baseType.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() + ", baseType=" + baseType + ", elementType=" + elementType + "]";
}
}