KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > util > SQLTypeUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.util;
21
22 import java.util.ResourceBundle JavaDoc;
23 import java.sql.Types JavaDoc;
24
25 public class SQLTypeUtil extends Object JavaDoc {
26
27     // ===================== i18n utilities ===========================
28

29     /** Computes the localized string for the key.
30      * @param key The key of the string.
31      * @return the localized string.
32      */

33     public static String JavaDoc getString (String JavaDoc key) {
34         return ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString(key);
35     }
36
37     // ===================== sql type utilities ===========================
38

39     /** Convert sql types to String for display
40      * @param sqlType the type number from java.sql.Types
41      * @return the type name
42      */

43     static public String JavaDoc getSqlTypeString (int sqlType) {
44         switch(sqlType) {
45             case Types.BIGINT:
46                 return getString("SQL_BIGINT"); //NOI18N
47
case Types.BINARY:
48                 return getString("SQL_BINARY"); //NOI18N
49
case Types.BIT:
50                 return getString("SQL_BIT"); //NOI18N
51
case Types.CHAR:
52                 return getString("SQL_CHAR"); //NOI18N
53
case Types.DATE:
54                 return getString("SQL_DATE"); //NOI18N
55
case Types.DECIMAL:
56                 return getString("SQL_DECIMAL"); //NOI18N
57
case Types.DOUBLE:
58                 return getString("SQL_DOUBLE"); //NOI18N
59
case Types.FLOAT:
60                 return getString("SQL_FLOAT"); //NOI18N
61
case Types.INTEGER:
62                 return getString("SQL_INTEGER"); //NOI18N
63
case Types.LONGVARBINARY:
64                 return getString("SQL_LONGVARBINARY"); //NOI18N
65
case Types.LONGVARCHAR:
66                 return getString("SQL_LONGVARCHAR"); //NOI18N
67
case Types.NULL:
68                 return getString("SQL_NULL"); //NOI18N
69
case Types.NUMERIC:
70                 return getString("SQL_NUMERIC"); //NOI18N
71
case Types.OTHER:
72                 return getString("SQL_OTHER"); //NOI18N
73
case Types.REAL:
74                 return getString("SQL_REAL"); //NOI18N
75
case Types.SMALLINT:
76                 return getString("SQL_SMALLINT"); //NOI18N
77
case Types.TIME:
78                 return getString("SQL_TIME"); //NOI18N
79
case Types.TIMESTAMP:
80                 return getString("SQL_TIMESTAMP"); //NOI18N
81
case Types.TINYINT:
82                 return getString("SQL_TINYINT"); //NOI18N
83
case Types.VARBINARY:
84                 return getString("SQL_VARBINARY"); //NOI18N
85
case Types.VARCHAR:
86                 return getString("SQL_VARCHAR"); //NOI18N
87
case Types.JAVA_OBJECT:
88                 return getString("SQL_JAVA_OBJECT"); //NOI18N
89
case Types.DISTINCT:
90                 return getString("SQL_DISTINCT"); //NOI18N
91
case Types.STRUCT:
92                 return getString("SQL_STRUCT"); //NOI18N
93
case Types.ARRAY:
94                 return getString("SQL_ARRAY"); //NOI18N
95
case Types.BLOB:
96                 return getString("SQL_BLOB"); //NOI18N
97
case Types.CLOB:
98                 return getString("SQL_CLOB"); //NOI18N
99
case Types.REF:
100                 return getString("SQL_REF"); //NOI18N
101
default:
102                 return getString("SQL_UNKNOWN"); //NOI18N
103
}
104     }
105
106     /** Returns if the given data type is numeric type or not.
107      * @param type the type from java.sql.Types
108      * @return true if the given type is numeric type; false otherwise
109      */

110     static public boolean isNumeric (int type) {
111         switch (type) {
112             case Types.BIGINT:
113             case Types.BIT:
114             case Types.DECIMAL:
115             case Types.DOUBLE:
116             case Types.FLOAT:
117             case Types.INTEGER:
118             case Types.NUMERIC:
119             case Types.REAL:
120             case Types.SMALLINT:
121             case Types.TINYINT:
122                 return true;
123         }
124
125         return false;
126     }
127
128     /** Returns if the given data type is character type or not.
129      * @param type the type from java.sql.Types
130      * @return true if the given type is character type; false otherwise
131      */

132     static public boolean isCharacter (int type) {
133         switch (type) {
134             case Types.BINARY:
135             case Types.CHAR:
136             case Types.LONGVARCHAR:
137             case Types.VARCHAR:
138             case Types.VARBINARY:
139                 return true;
140         }
141
142         return false;
143     }
144         
145     /** Return if a given data type is blob type or not.
146      * Note: CLOB should really not be in this list, use isLob method for that.
147      * @param type the type from java.sql.Types
148      * return true if the give type is blob type; false otherwise
149      */

150        static public boolean isBlob (int type) {
151            switch (type) {
152                case Types.BLOB:
153                case Types.CLOB:
154                case Types.BINARY:
155                case Types.VARBINARY:
156                case Types.LONGVARBINARY:
157                case Types.OTHER:
158                    return true;
159            }
160
161            return false;
162        }
163
164     /** Return if a given data type is LOB (large object) type or not.
165      * Note: Implementation of this method uses isBlob method but also
166      * duplicates the check of CLOB because CLOB should really not return
167      * true from isBlob. However, there might be other non-IDE callers of
168      * the isBlob method (like appserver) so the CLOB check is left in both
169      * places for now.
170      * @param type the type from java.sql.Types
171      * return true if the give type is lob type; false otherwise
172      */

173        static public boolean isLob (int type) {
174            return (isBlob(type) || (Types.CLOB == type) ||
175                    (Types.LONGVARCHAR == type));
176        }
177
178     /** Returns if two data types are compatible or not.
179      * @param type1 first type to compare
180      * @param type2 second type to compare
181      * @return true if the types are compatible; false otherwise
182      */

183     static public boolean isCompatibleType (int type1, int type2) {
184         return ((type1 == type2)
185                      || (isCharacter(type1) && isCharacter(type2))
186                      || (isNumeric(type1) && isNumeric(type2))
187                      || (isBlob(type1) && isBlob(type2))
188                 );
189     }
190 }
191
Popular Tags