/
TypeMap.java
317 lines (282 loc) · 15.3 KB
/
TypeMap.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
307
308
309
310
311
312
313
314
315
316
317
package org.jumpmind.db.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import static org.jumpmind.db.model.ColumnTypes.ORACLE_TIMESTAMPLTZ;
import static org.jumpmind.db.model.ColumnTypes.ORACLE_TIMESTAMPTZ;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.platform.PlatformUtils;
/**
* A class that maps SQL type names to their JDBC type ID found in
* {@link java.sql.Types} and vice versa.
*/
public abstract class TypeMap
{
/** The string representation of the {@link java.sql.Types#ARRAY} constant. */
public static final String ARRAY = "ARRAY";
/** The string representation of the {@link java.sql.Types#BIGINT} constant. */
public static final String BIGINT = "BIGINT";
/** The string representation of the {@link java.sql.Types#BINARY} constant. */
public static final String BINARY = "BINARY";
/** The string representation of the {@link java.sql.Types#BIT} constant. */
public static final String BIT = "BIT";
/** The string representation of the {@link java.sql.Types#BLOB} constant. */
public static final String BLOB = "BLOB";
/** The string representation of the {@link java.sql.Types#BOOLEAN} constant. */
public static final String BOOLEAN = "BOOLEAN";
/** The string representation of the {@link java.sql.Types#CHAR} constant. */
public static final String CHAR = "CHAR";
/** The string representation of the {@link java.sql.Types#CLOB} constant. */
public static final String CLOB = "CLOB";
/** The string representation of the {@link java.sql.Types#DATALINK} constant. */
public static final String DATALINK = "DATALINK";
/** The string representation of the {@link java.sql.Types#DATE} constant. */
public static final String DATE = "DATE";
/** The string representation of the {@link java.sql.Types#DECIMAL} constant. */
public static final String DECIMAL = "DECIMAL";
/** The string representation of the {@link java.sql.Types#DISTINCT} constant. */
public static final String DISTINCT = "DISTINCT";
/** The string representation of the {@link java.sql.Types#DOUBLE} constant. */
public static final String DOUBLE = "DOUBLE";
/** The string representation of the {@link java.sql.Types#FLOAT} constant. */
public static final String FLOAT = "FLOAT";
/** The string representation of the {@link java.sql.Types#INTEGER} constant. */
public static final String INTEGER = "INTEGER";
/** The string representation of the {@link java.sql.Types#JAVA_OBJECT} constant. */
public static final String JAVA_OBJECT = "JAVA_OBJECT";
/** The string representation of the {@link java.sql.Types#LONGVARBINARY} constant. */
public static final String LONGVARBINARY = "LONGVARBINARY";
/** The string representation of the {@link java.sql.Types#LONGVARCHAR} constant. */
public static final String LONGVARCHAR = "LONGVARCHAR";
/** The string representation of the {@link java.sql.Types#NULL} constant. */
public static final String NULL = "NULL";
/** The string representation of the {@link java.sql.Types#NUMERIC} constant. */
public static final String NUMERIC = "NUMERIC";
/** The string representation of the {@link java.sql.Types#OTHER} constant. */
public static final String OTHER = "OTHER";
/** The string representation of the {@link java.sql.Types#REAL} constant. */
public static final String REAL = "REAL";
/** The string representation of the {@link java.sql.Types#REF} constant. */
public static final String REF = "REF";
/** The string representation of the {@link java.sql.Types#SMALLINT} constant. */
public static final String SMALLINT = "SMALLINT";
/** The string representation of the {@link java.sql.Types#STRUCT} constant. */
public static final String STRUCT = "STRUCT";
/** The string representation of the {@link java.sql.Types#TIME} constant. */
public static final String TIME = "TIME";
/** The string representation of the {@link java.sql.Types#TIMESTAMP} constant. */
public static final String TIMESTAMP = "TIMESTAMP";
public static final String TIMESTAMPTZ = "TIMESTAMPTZ";
public static final String TIMESTAMPLTZ = "TIMESTAMPLTZ";
/** The string representation of the {@link java.sql.Types#TINYINT} constant. */
public static final String TINYINT = "TINYINT";
/** The string representation of the {@link java.sql.Types#VARBINARY} constant. */
public static final String VARBINARY = "VARBINARY";
/** The string representation of the {@link java.sql.Types#VARCHAR} constant. */
public static final String VARCHAR = "VARCHAR";
public static final String SQLXML = "SQLXML";
public static final String GEOMETRY = "GEOMETRY";
public static final String GEOGRAPHY = "GEOGRAPHY";
public static final String POINT = "POINT";
public static final String LINESTRING = "LINESTRING";
public static final String POLYGON = "POLYGON";
public static final String UUID = "UUID";
public static final String VARBIT = "VARBIT";
public static final String INTERVAL = "INTERVAL";
public static final String NCHAR = "NCHAR";
public static final String NVARCHAR = "NVARCHAR";
public static final String LONGNVARCHAR = "LONGNVARCHAR";
public static final String NCLOB = "NCLOB";
public static final String IMAGE = "IMAGE";
public static final String DATETIME2 = "DATETIME2";
public static final String TSVECTOR = "TSVECTOR";
/** Maps type names to the corresponding {@link java.sql.Types} constants. */
private static HashMap<String, Integer> _typeNameToTypeCode = new HashMap<String, Integer>();
/** Maps {@link java.sql.Types} type code constants to the corresponding type names. */
private static HashMap<Integer, String> _typeCodeToTypeName = new HashMap<Integer, String>();
/** Contains the types per category. */
private static HashMap<JdbcTypeCategoryEnum, Set<Integer>> _typesPerCategory = new HashMap<JdbcTypeCategoryEnum, Set<Integer>>();
static
{
registerJdbcType(Types.ARRAY, ARRAY, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.BIGINT, BIGINT, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.BINARY, BINARY, JdbcTypeCategoryEnum.BINARY);
registerJdbcType(Types.BIT, BIT, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.BLOB, BLOB, JdbcTypeCategoryEnum.BINARY);
registerJdbcType(Types.CHAR, CHAR, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(Types.CLOB, CLOB, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(Types.DATE, DATE, JdbcTypeCategoryEnum.DATETIME);
registerJdbcType(Types.DECIMAL, DECIMAL, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.DISTINCT, DISTINCT, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.DOUBLE, DOUBLE, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.FLOAT, FLOAT, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.INTEGER, INTEGER, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.JAVA_OBJECT, JAVA_OBJECT, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.LONGVARBINARY, LONGVARBINARY, JdbcTypeCategoryEnum.BINARY);
registerJdbcType(Types.LONGVARCHAR, LONGVARCHAR, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(Types.NULL, NULL, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.NUMERIC, NUMERIC, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.OTHER, OTHER, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.REAL, REAL, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.REF, REF, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.SMALLINT, SMALLINT, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.STRUCT, STRUCT, JdbcTypeCategoryEnum.SPECIAL);
registerJdbcType(Types.TIME, TIME, JdbcTypeCategoryEnum.DATETIME);
registerJdbcType(Types.TIMESTAMP, TIMESTAMP, JdbcTypeCategoryEnum.DATETIME);
registerJdbcType(Types.TINYINT, TINYINT, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(Types.VARBINARY, VARBINARY, JdbcTypeCategoryEnum.BINARY);
registerJdbcType(Types.VARCHAR, VARCHAR, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(ORACLE_TIMESTAMPTZ, TIMESTAMPTZ, JdbcTypeCategoryEnum.DATETIME);
registerJdbcType(ORACLE_TIMESTAMPLTZ, TIMESTAMPLTZ, JdbcTypeCategoryEnum.DATETIME);
// only available in JDK 1.4 and above:
if (PlatformUtils.supportsJava14JdbcTypes())
{
registerJdbcType(PlatformUtils.determineBooleanTypeCode(), BOOLEAN, JdbcTypeCategoryEnum.NUMERIC);
registerJdbcType(PlatformUtils.determineDatalinkTypeCode(), DATALINK, JdbcTypeCategoryEnum.SPECIAL);
}
// Torque/Turbine extensions which we only support when reading from an XML schema
_typeNameToTypeCode.put("BOOLEANINT", new Integer(Types.TINYINT));
_typeNameToTypeCode.put("BOOLEANCHAR", new Integer(Types.CHAR));
registerJdbcType(ColumnTypes.SQLXML, SQLXML, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(ColumnTypes.NCHAR, NCHAR, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(ColumnTypes.NCLOB, NCLOB, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(ColumnTypes.NVARCHAR, NVARCHAR, JdbcTypeCategoryEnum.TEXTUAL);
registerJdbcType(ColumnTypes.LONGNVARCHAR, LONGNVARCHAR, JdbcTypeCategoryEnum.TEXTUAL);
}
/**
* Returns the JDBC type code (one of the {@link java.sql.Types} constants) that
* corresponds to the given JDBC type name.
*
* @param typeName The JDBC type name (case is ignored)
* @return The type code or <code>null</code> if the type is unknown
*/
public static Integer getJdbcTypeCode(String typeName)
{
return (Integer)_typeNameToTypeCode.get(typeName.toUpperCase());
}
public static String getJdbcTypeDescriptions(int[] types) {
StringBuilder buff = new StringBuilder(32);
for (int type : types) {
buff.append(getJdbcTypeName(type)).append(", ");
}
if (buff.length() > 0) {
buff.setLength(buff.length()-2); // loose the last ", "
}
return buff.toString();
}
/**
* Returns the JDBC type name that corresponds to the given type code
* (one of the {@link java.sql.Types} constants).
*
* @param typeCode The type code
* @return The JDBC type name (one of the constants in this class) or
* <code>null</code> if the type is unknown
*/
public static String getJdbcTypeName(int typeCode)
{
String description = _typeCodeToTypeName.get(new Integer(typeCode));
if (StringUtils.isEmpty(description)) {
description = String.valueOf(typeCode);
}
return description;
}
/**
* Registers a jdbc type.
*
* @param typeCode The type code (one of the {@link java.sql.Types} constants)
* @param typeName The type name (case is ignored)
* @param category The type category
*/
protected static void registerJdbcType(int typeCode, String typeName, JdbcTypeCategoryEnum category)
{
Integer typeId = new Integer(typeCode);
_typeNameToTypeCode.put(typeName.toUpperCase(), typeId);
_typeCodeToTypeName.put(typeId, typeName.toUpperCase());
Set<Integer> typesInCategory = _typesPerCategory.get(category);
if (typesInCategory == null)
{
typesInCategory = new HashSet<Integer>();
_typesPerCategory.put(category, typesInCategory);
}
typesInCategory.add(typeId);
}
/**
* Determines whether the given jdbc type (one of the {@link java.sql.Types} constants)
* is a numeric type.
*
* @param jdbcTypeCode The type code
* @return <code>true</code> if the type is a numeric one
*/
public static boolean isNumericType(int jdbcTypeCode)
{
Set<Integer> typesInCategory = _typesPerCategory.get(JdbcTypeCategoryEnum.NUMERIC);
return typesInCategory == null ? false : typesInCategory.contains(new Integer(jdbcTypeCode));
}
/**
* Determines whether the given jdbc type (one of the {@link java.sql.Types} constants)
* is a date/time type.
*
* @param jdbcTypeCode The type code
* @return <code>true</code> if the type is a numeric one
*/
public static boolean isDateTimeType(int jdbcTypeCode)
{
Set<Integer> typesInCategory = _typesPerCategory.get(JdbcTypeCategoryEnum.DATETIME);
return typesInCategory == null ? false : typesInCategory.contains(new Integer(jdbcTypeCode));
}
/**
* Determines whether the given jdbc type (one of the {@link java.sql.Types} constants)
* is a text type.
*
* @param jdbcTypeCode The type code
* @return <code>true</code> if the type is a text one
*/
public static boolean isTextType(int jdbcTypeCode)
{
Set<Integer> typesInCategory = _typesPerCategory.get(JdbcTypeCategoryEnum.TEXTUAL);
return typesInCategory == null ? false : typesInCategory.contains(new Integer(jdbcTypeCode));
}
/**
* Determines whether the given jdbc type (one of the {@link java.sql.Types} constants)
* is a binary type.
*
* @param jdbcTypeCode The type code
* @return <code>true</code> if the type is a binary one
*/
public static boolean isBinaryType(int jdbcTypeCode)
{
Set<Integer> typesInCategory = _typesPerCategory.get(JdbcTypeCategoryEnum.BINARY);
return typesInCategory == null ? false : typesInCategory.contains(new Integer(jdbcTypeCode));
}
/**
* Determines whether the given sql type (one of the {@link java.sql.Types} constants)
* is a special type.
*
* @param jdbcTypeCode The type code
* @return <code>true</code> if the type is a special one
*/
public static boolean isSpecialType(int jdbcTypeCode)
{
Set<Integer> typesInCategory = _typesPerCategory.get(JdbcTypeCategoryEnum.SPECIAL);
return typesInCategory == null ? false : typesInCategory.contains(new Integer(jdbcTypeCode));
}
}