001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema.util;
043:
044: import java.util.ResourceBundle;
045: import java.sql.Types;
046:
047: public class SQLTypeUtil extends Object {
048:
049: // ===================== i18n utilities ===========================
050:
051: /** Computes the localized string for the key.
052: * @param key The key of the string.
053: * @return the localized string.
054: */
055: public static String getString(String key) {
056: return ResourceBundle.getBundle(
057: "org.netbeans.modules.dbschema.resources.Bundle")
058: .getString(key);
059: }
060:
061: // ===================== sql type utilities ===========================
062:
063: /** Convert sql types to String for display
064: * @param sqlType the type number from java.sql.Types
065: * @return the type name
066: */
067: static public String getSqlTypeString(int sqlType) {
068: switch (sqlType) {
069: case Types.BIGINT:
070: return getString("SQL_BIGINT"); //NOI18N
071: case Types.BINARY:
072: return getString("SQL_BINARY"); //NOI18N
073: case Types.BIT:
074: return getString("SQL_BIT"); //NOI18N
075: case Types.CHAR:
076: return getString("SQL_CHAR"); //NOI18N
077: case Types.DATE:
078: return getString("SQL_DATE"); //NOI18N
079: case Types.DECIMAL:
080: return getString("SQL_DECIMAL"); //NOI18N
081: case Types.DOUBLE:
082: return getString("SQL_DOUBLE"); //NOI18N
083: case Types.FLOAT:
084: return getString("SQL_FLOAT"); //NOI18N
085: case Types.INTEGER:
086: return getString("SQL_INTEGER"); //NOI18N
087: case Types.LONGVARBINARY:
088: return getString("SQL_LONGVARBINARY"); //NOI18N
089: case Types.LONGVARCHAR:
090: return getString("SQL_LONGVARCHAR"); //NOI18N
091: case Types.NULL:
092: return getString("SQL_NULL"); //NOI18N
093: case Types.NUMERIC:
094: return getString("SQL_NUMERIC"); //NOI18N
095: case Types.OTHER:
096: return getString("SQL_OTHER"); //NOI18N
097: case Types.REAL:
098: return getString("SQL_REAL"); //NOI18N
099: case Types.SMALLINT:
100: return getString("SQL_SMALLINT"); //NOI18N
101: case Types.TIME:
102: return getString("SQL_TIME"); //NOI18N
103: case Types.TIMESTAMP:
104: return getString("SQL_TIMESTAMP"); //NOI18N
105: case Types.TINYINT:
106: return getString("SQL_TINYINT"); //NOI18N
107: case Types.VARBINARY:
108: return getString("SQL_VARBINARY"); //NOI18N
109: case Types.VARCHAR:
110: return getString("SQL_VARCHAR"); //NOI18N
111: case Types.JAVA_OBJECT:
112: return getString("SQL_JAVA_OBJECT"); //NOI18N
113: case Types.DISTINCT:
114: return getString("SQL_DISTINCT"); //NOI18N
115: case Types.STRUCT:
116: return getString("SQL_STRUCT"); //NOI18N
117: case Types.ARRAY:
118: return getString("SQL_ARRAY"); //NOI18N
119: case Types.BLOB:
120: return getString("SQL_BLOB"); //NOI18N
121: case Types.CLOB:
122: return getString("SQL_CLOB"); //NOI18N
123: case Types.REF:
124: return getString("SQL_REF"); //NOI18N
125: default:
126: return getString("SQL_UNKNOWN"); //NOI18N
127: }
128: }
129:
130: /** Returns if the given data type is numeric type or not.
131: * @param type the type from java.sql.Types
132: * @return true if the given type is numeric type; false otherwise
133: */
134: static public boolean isNumeric(int type) {
135: switch (type) {
136: case Types.BIGINT:
137: case Types.BIT:
138: case Types.DECIMAL:
139: case Types.DOUBLE:
140: case Types.FLOAT:
141: case Types.INTEGER:
142: case Types.NUMERIC:
143: case Types.REAL:
144: case Types.SMALLINT:
145: case Types.TINYINT:
146: return true;
147: }
148:
149: return false;
150: }
151:
152: /** Returns if the given data type is character type or not.
153: * @param type the type from java.sql.Types
154: * @return true if the given type is character type; false otherwise
155: */
156: static public boolean isCharacter(int type) {
157: switch (type) {
158: case Types.BINARY:
159: case Types.CHAR:
160: case Types.LONGVARCHAR:
161: case Types.VARCHAR:
162: case Types.VARBINARY:
163: return true;
164: }
165:
166: return false;
167: }
168:
169: /** Return if a given data type is blob type or not.
170: * Note: CLOB should really not be in this list, use isLob method for that.
171: * @param type the type from java.sql.Types
172: * return true if the give type is blob type; false otherwise
173: */
174: static public boolean isBlob(int type) {
175: switch (type) {
176: case Types.BLOB:
177: case Types.CLOB:
178: case Types.BINARY:
179: case Types.VARBINARY:
180: case Types.LONGVARBINARY:
181: case Types.OTHER:
182: return true;
183: }
184:
185: return false;
186: }
187:
188: /** Return if a given data type is LOB (large object) type or not.
189: * Note: Implementation of this method uses isBlob method but also
190: * duplicates the check of CLOB because CLOB should really not return
191: * true from isBlob. However, there might be other non-IDE callers of
192: * the isBlob method (like appserver) so the CLOB check is left in both
193: * places for now.
194: * @param type the type from java.sql.Types
195: * return true if the give type is lob type; false otherwise
196: */
197: static public boolean isLob(int type) {
198: return (isBlob(type) || (Types.CLOB == type) || (Types.LONGVARCHAR == type));
199: }
200:
201: /** Returns if two data types are compatible or not.
202: * @param type1 first type to compare
203: * @param type2 second type to compare
204: * @return true if the types are compatible; false otherwise
205: */
206: static public boolean isCompatibleType(int type1, int type2) {
207: return ((type1 == type2)
208: || (isCharacter(type1) && isCharacter(type2))
209: || (isNumeric(type1) && isNumeric(type2)) || (isBlob(type1) && isBlob(type2)));
210: }
211: }
|