001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2002 David MacLean
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.sql.DatabaseMetaData;
021: import java.sql.Types;
022: import java.util.ArrayList;
023:
024: public class JDBCTypeMapper {
025:
026: public static String[] getJdbcTypeList() {
027: ArrayList<String> result = new ArrayList<String>();
028: result.add(getJdbcTypeName(Types.ARRAY));
029: result.add(getJdbcTypeName(Types.BOOLEAN));
030: result.add(getJdbcTypeName(Types.BIGINT));
031: result.add(getJdbcTypeName(Types.BINARY));
032: result.add(getJdbcTypeName(Types.BIT));
033: result.add(getJdbcTypeName(Types.BLOB));
034: result.add(getJdbcTypeName(Types.CHAR));
035: result.add(getJdbcTypeName(Types.CLOB));
036: result.add(getJdbcTypeName(Types.DATALINK));
037: result.add(getJdbcTypeName(Types.DATE));
038: result.add(getJdbcTypeName(Types.DECIMAL));
039: result.add(getJdbcTypeName(Types.DISTINCT));
040: result.add(getJdbcTypeName(Types.DOUBLE));
041: result.add(getJdbcTypeName(Types.FLOAT));
042: result.add(getJdbcTypeName(Types.INTEGER));
043: result.add(getJdbcTypeName(Types.JAVA_OBJECT));
044: result.add(getJdbcTypeName(Types.LONGVARBINARY));
045: result.add(getJdbcTypeName(Types.LONGVARCHAR));
046: result.add(getJdbcTypeName(Types.NUMERIC));
047: result.add(getJdbcTypeName(Types.NULL));
048: result.add(getJdbcTypeName(Types.OTHER));
049: result.add(getJdbcTypeName(Types.REAL));
050: result.add(getJdbcTypeName(Types.REF));
051: result.add(getJdbcTypeName(Types.SMALLINT));
052: result.add(getJdbcTypeName(Types.STRUCT));
053: result.add(getJdbcTypeName(Types.TIME));
054: result.add(getJdbcTypeName(Types.TIMESTAMP));
055: result.add(getJdbcTypeName(Types.TINYINT));
056: result.add(getJdbcTypeName(Types.VARBINARY));
057: result.add(getJdbcTypeName(Types.VARCHAR));
058: return result.toArray(new String[result.size()]);
059: }
060:
061: public static String getJdbcTypeName(int jdbcType) {
062: String typeName = "";
063: switch (jdbcType) {
064: case Types.ARRAY:
065: typeName = "ARRAY";
066: break;
067: case Types.BOOLEAN:
068: typeName = "BOOLEAN";
069: break;
070: case Types.BIGINT:
071: typeName = "BIGINT";
072: break;
073: case Types.BINARY:
074: typeName = "BINARY";
075: break;
076: case Types.BIT:
077: typeName = "BIT";
078: break;
079: case Types.BLOB:
080: typeName = "BLOB";
081: break;
082: case Types.CHAR:
083: typeName = "CHAR";
084: break;
085: case Types.CLOB:
086: typeName = "CLOB";
087: break;
088: case Types.DATALINK:
089: typeName = "DATALINK";
090: break;
091: case Types.DATE:
092: typeName = "DATE";
093: break;
094: case Types.DECIMAL:
095: typeName = "DECIMAL";
096: break;
097: case Types.DISTINCT:
098: typeName = "DISTINCT";
099: break;
100: case Types.DOUBLE:
101: typeName = "DOUBLE";
102: break;
103: case Types.FLOAT:
104: typeName = "FLOAT";
105: break;
106: case Types.INTEGER:
107: typeName = "INTEGER";
108: break;
109: case Types.JAVA_OBJECT:
110: typeName = "JAVA_OBJECT";
111: break;
112: case Types.LONGVARBINARY:
113: typeName = "LONGVARBINARY";
114: break;
115: case Types.LONGVARCHAR:
116: typeName = "LONGVARCHAR";
117: break;
118: case Types.NULL:
119: typeName = "NULL";
120: break;
121: case Types.NUMERIC:
122: typeName = "NUMERIC";
123: break;
124: case Types.OTHER:
125: typeName = "OTHER";
126: break;
127: case Types.REAL:
128: typeName = "REAL";
129: break;
130: case Types.REF:
131: typeName = "REF";
132: break;
133: case Types.SMALLINT:
134: typeName = "SMALLINT";
135: break;
136: case Types.STRUCT:
137: typeName = "STRUCT";
138: break;
139: case Types.TIME:
140: typeName = "TIME";
141: break;
142: case Types.TIMESTAMP:
143: typeName = "TIMESTAMP";
144: break;
145: case Types.TINYINT:
146: typeName = "TINYINT";
147: break;
148: case Types.VARBINARY:
149: typeName = "VARBINARY";
150: break;
151: case Types.VARCHAR:
152: typeName = "VARCHAR";
153: break;
154: }
155: return typeName;
156: }
157:
158: public static int getJdbcType(String jdbcTypeName) {
159: int type = Types.NULL;
160: if (jdbcTypeName == null) {
161: return Types.NULL;
162: }
163: if ("ARRAY".equals(jdbcTypeName)) {
164: type = Types.ARRAY;
165: } else if ("BIGINT".equals(jdbcTypeName)) {
166: type = Types.BIGINT;
167: } else if ("BINARY".equals(jdbcTypeName)) {
168: type = Types.BINARY;
169: } else if ("BIT".equals(jdbcTypeName)) {
170: type = Types.BIT;
171: } else if ("BLOB".equals(jdbcTypeName)) {
172: type = Types.BLOB;
173: } else if ("BOOLEAN".equals(jdbcTypeName)) {
174: type = Types.BOOLEAN;
175: } else if ("CHAR".equals(jdbcTypeName)) {
176: type = Types.CHAR;
177: } else if ("CLOB".equals(jdbcTypeName)) {
178: type = Types.CLOB;
179: } else if ("DATE".equals(jdbcTypeName)) {
180: type = Types.DATE;
181: } else if ("DATALINK".equals(jdbcTypeName)) {
182: type = Types.DATALINK;
183: } else if ("DECIMAL".equals(jdbcTypeName)) {
184: type = Types.DECIMAL;
185: } else if ("DISTINCT".equals(jdbcTypeName)) {
186: type = Types.DISTINCT;
187: } else if ("DOUBLE".equals(jdbcTypeName)) {
188: type = Types.DOUBLE;
189: } else if ("FLOAT".equals(jdbcTypeName)) {
190: type = Types.FLOAT;
191: } else if ("INTEGER".equals(jdbcTypeName)) {
192: type = Types.INTEGER;
193: } else if ("JAVA_OBJECT".equals(jdbcTypeName)) {
194: type = Types.JAVA_OBJECT;
195: } else if ("LONGVARBINARY".equals(jdbcTypeName)) {
196: type = Types.LONGVARBINARY;
197: } else if ("LONGVARCHAR".equals(jdbcTypeName)) {
198: type = Types.LONGVARCHAR;
199: } else if ("NULL".equals(jdbcTypeName)) {
200: type = Types.NULL;
201: } else if ("NUMERIC".equals(jdbcTypeName)) {
202: type = Types.NUMERIC;
203: } else if ("OTHER".equals(jdbcTypeName)) {
204: type = Types.OTHER;
205: } else if ("REAL".equals(jdbcTypeName)) {
206: type = Types.REAL;
207: } else if ("REF".equals(jdbcTypeName)) {
208: type = Types.REF;
209: } else if ("SMALLINT".equals(jdbcTypeName)) {
210: type = Types.SMALLINT;
211: } else if ("STRUCT".equals(jdbcTypeName)) {
212: type = Types.STRUCT;
213: } else if ("TIME".equals(jdbcTypeName)) {
214: type = Types.TIME;
215: } else if ("TIMESTAMP".equals(jdbcTypeName)) {
216: type = Types.TIMESTAMP;
217: } else if ("TINYINT".equals(jdbcTypeName)) {
218: type = Types.TINYINT;
219: } else if ("VARBINARY".equals(jdbcTypeName)) {
220: type = Types.VARBINARY;
221: } else if ("VARCHAR".equals(jdbcTypeName)) {
222: type = Types.VARCHAR;
223: } else if (jdbcTypeName.startsWith("NVARCHAR")) {
224: type = Types.VARCHAR;
225: }
226: return type;
227: }
228:
229: public static boolean isNumberType(int jdbcType) {
230: boolean result = false;
231: switch (jdbcType) {
232: case Types.BIGINT:
233: case Types.DECIMAL:
234: case Types.DOUBLE:
235: case Types.FLOAT:
236: case Types.INTEGER:
237: case Types.NUMERIC:
238: result = true;
239: break;
240: default:
241: result = false;
242: }
243: return result;
244: }
245:
246: public static boolean isDateType(int jdbcType) {
247: boolean result = false;
248: switch (jdbcType) {
249: case Types.DATE:
250: case Types.TIME:
251: case Types.TIMESTAMP:
252: result = true;
253: break;
254: default:
255: result = false;
256: }
257: return result;
258: }
259:
260: public static boolean isLongType(int jdbcType) {
261: boolean result = false;
262: switch (jdbcType) {
263: case Types.LONGVARBINARY:
264: case Types.LONGVARCHAR:
265: case Types.BLOB:
266: case Types.CLOB:
267: result = true;
268: break;
269: default:
270: result = false;
271: }
272: return result;
273: }
274:
275: public static IndexInfo.IndexType getIndexType(short indexType) {
276: IndexInfo.IndexType result = null;
277: switch (indexType) {
278: case DatabaseMetaData.tableIndexStatistic:
279: result = IndexInfo.IndexType.STATISTIC;
280: break;
281: case DatabaseMetaData.tableIndexClustered:
282: result = IndexInfo.IndexType.CLUSTERED;
283: break;
284: case DatabaseMetaData.tableIndexHashed:
285: result = IndexInfo.IndexType.HASHED;
286: break;
287: case DatabaseMetaData.tableIndexOther:
288: result = IndexInfo.IndexType.OTHER;
289: break;
290: default:
291: throw new IllegalArgumentException("Unknown index type: "
292: + indexType);
293: }
294: return result;
295: }
296:
297: public static IndexInfo.SortOrder getIndexSortOrder(String sortOrder) {
298: if (sortOrder == null) {
299: return IndexInfo.SortOrder.NONE;
300: }
301: if (sortOrder.equalsIgnoreCase("A")) {
302: return IndexInfo.SortOrder.ASC;
303: }
304: if (sortOrder.equalsIgnoreCase("D")) {
305: return IndexInfo.SortOrder.DESC;
306: }
307:
308: throw new IllegalArgumentException("Unknown index sort order: "
309: + sortOrder);
310: }
311: }
|