KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > DbType


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.jdbc.typing;
25
26 import java.sql.Types JavaDoc;
27
28
29     /**
30     private String [] type_names ={"CHAR","VARCHAR", "LONGVARCHAR",
31     "NUMERIC","DECIMAL","BIT","TINYINT","SMALLINT","INTEGER","BIGINT","REAL",
32     "FLOAT","DOUBLE",
33     "BINARY","VARBINARY","LONGVARBINARY",
34     "DATE","TIME","TIMESTAMP"};
35     @return java.lang.Object
36     @roseuid 3B2798BF00B5
37      */

38
39 public class DbType implements Cloneable JavaDoc
40 {
41
42     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
43     private static final String JavaDoc RCSName = "$Name: $";
44
45     public static final int ORACLE_ROWID = 111;
46     public static final int BOOLEAN = 16; // because java.sql.Types.BOOLEAN does not exist in JDK 1.3 !!!
47

48
49     /** @todo */
50     private int jdbcType;
51     
52     /** The size of this column */
53     private long length = 0;
54
55     /** Number of digit after decimal point */
56     private int scale = 0;
57     
58     /** The native name of this column's data type */
59     private String JavaDoc nativeType;
60     
61     /** Whether this column is a 'full text' type */
62     private boolean isLongType = false;
63     
64     /** Whether this column is a LOB type */
65     private boolean isLOB = false;
66
67     public DbType() {}
68     
69     public DbType(int jdbcType) {
70         this.jdbcType = jdbcType;
71     }
72     
73     public DbType(TypeMap.SQLType sqlType) {
74         this(sqlType.getJDBCType());
75         setNativeType(sqlType.getNativeTypeName());
76     }
77     
78     /**
79      * @param jdbcType
80      * @param length
81      * @param scale
82      */

83     public DbType(int jdbcType, int length, int scale) {
84         this(jdbcType, length);
85         this.scale = scale;
86     }
87     
88     public DbType(int jdbcType, int size) {
89         this.jdbcType = jdbcType;
90         this.length = size;
91     }
92
93     public DbType(int length, int scale, TypeMap.SQLType sqlType) {
94         this(sqlType);
95         // if no length is pecified take the maximum for the type
96
if (length == 0)
97             this.length = sqlType.getPrecision();
98         else
99             this.length = length;
100         this.scale = scale;
101     }
102
103     public Object JavaDoc clone () throws CloneNotSupportedException JavaDoc
104     {
105         return super.clone();
106     };
107
108     public boolean isCompatibleTo(DbType type)
109     {
110         return true;
111     }
112
113     /**
114     Access method for the _name property.
115     
116     @return the current value of the _name property
117      */

118     public String JavaDoc getName() {
119         return getNameFromCode(jdbcType);
120     }
121
122     public int getPrimitiveTypeCode() {
123         return jdbcType;
124     }
125
126     public boolean isPrimitive() {
127         return true;
128     }
129
130     public boolean isUdt() {
131         return false;
132     }
133
134     /**
135     Access method for the _code property.
136     
137     @return the current value of the _code property
138      */

139     public int getJDBCType() {
140         return jdbcType;
141     }
142
143     /**
144     Sets the value of the _code property.
145     
146     @param aCode the new value of the _code property
147      */

148     public void setJDBCType(int aCode) {
149         jdbcType = aCode;
150     }
151
152     public String JavaDoc name() {
153         return getNameFromCode(jdbcType);
154     }
155
156     /**
157     @param code
158     @return String
159     @roseuid 3BD14FB5018A
160      */

161     protected static String JavaDoc getNameFromCode(int code) {
162         String JavaDoc retVal = null;
163         switch (code) {
164             case Types.ARRAY:
165                 retVal = "ARRAY";
166                 break;
167             case Types.BIGINT:
168                 retVal = "BIGINT";
169                 break;
170             case Types.BINARY:
171                 retVal = "BINARY";
172                 break;
173             case Types.BIT:
174                 retVal = "BIT";
175                 break;
176             case Types.BLOB:
177                 retVal = "BLOB";
178                 break;
179             case Types.CHAR:
180                 retVal = "CHAR";
181                 break;
182             case Types.CLOB:
183                 retVal = "CLOB";
184                 break;
185             case Types.DATE:
186                 retVal = "DATE";
187                 break;
188             case Types.DECIMAL:
189                 retVal = "DECIMAL";
190                 break;
191             case Types.DISTINCT:
192                 retVal = "DISTINCT";
193                 break;
194             case Types.DOUBLE:
195                 retVal = "DOUBLE";
196                 break;
197             case Types.FLOAT:
198                 retVal = "FLOAT";
199                 break;
200             case Types.INTEGER:
201                 retVal = "INTEGER";
202                 break;
203             case Types.JAVA_OBJECT:
204                 retVal = "JAVA_OBJECT";
205                 break;
206             case Types.LONGVARBINARY:
207                 retVal = "LONGVARBINARY";
208                 break;
209             case Types.LONGVARCHAR:
210                 retVal = "LONGVARCHAR";
211                 break;
212             case Types.NULL:
213                 retVal = "NULL";
214                 break;
215             case Types.NUMERIC:
216                 retVal = "NUMERIC";
217                 break;
218             case Types.REAL:
219                 retVal = "REAL";
220                 break;
221             case Types.SMALLINT:
222                 retVal = "SMALLINT";
223                 break;
224             case Types.STRUCT:
225                 retVal = "STRUCT";
226                 break;
227             case Types.TIME:
228                 retVal = "TIME";
229                 break;
230             case Types.TIMESTAMP:
231                 retVal = "TIMESTAMP";
232                 break;
233             case Types.TINYINT:
234                 retVal = "TINYINT";
235                 break;
236             case Types.VARBINARY:
237                 retVal = "VARBINARY";
238                 break;
239             case Types.VARCHAR:
240                 retVal = "VARCHAR";
241                 break;
242             case ORACLE_ROWID:
243                 retVal = "ORACLE_ROWID";
244                 break;
245             default:
246                 break;
247         }
248     
249         return retVal;
250     }
251
252     public boolean isNumeric() {
253         return Types.INTEGER == jdbcType ||
254                 Types.DECIMAL == jdbcType ||
255                 Types.DOUBLE == jdbcType ||
256                 Types.FLOAT == jdbcType ||
257                 Types.REAL == jdbcType ||
258                 Types.NUMERIC == jdbcType ||
259                 Types.SMALLINT == jdbcType ||
260                 Types.BIGINT == jdbcType ||
261                 Types.TINYINT == jdbcType ;
262     }
263
264     public boolean isDecimal() {
265         return Types.DECIMAL == jdbcType;
266     }
267
268     public boolean isInteger() {
269         return Types.INTEGER == jdbcType ||
270                 Types.SMALLINT== jdbcType ||
271                 Types.BIGINT== jdbcType ||
272                 Types.TINYINT == jdbcType ;
273     }
274
275     public boolean isString() {
276         return Types.CHAR == jdbcType ||
277                 Types.CLOB == jdbcType ||
278                 Types.VARCHAR == jdbcType ||
279                 Types.LONGVARCHAR == jdbcType ;
280     }
281
282     public boolean isDataTime() {
283         return Types.DATE == jdbcType ||
284                 Types.TIMESTAMP == jdbcType ||
285                 Types.TIME == jdbcType ;
286     }
287
288     public boolean isNull() {
289         return Types.NULL == jdbcType;
290     }
291
292     public String JavaDoc pprint() {
293         return DbType.getNameFromCode(jdbcType) + "(" + Integer.toString(jdbcType) + ")";
294     }
295     public long getLength() {
296         return length;
297     }
298
299     public void setLength(long length) {
300         this.length = length;
301     }
302
303     public int getScale() {
304         return scale;
305     }
306
307     public void setScale(int scale) {
308         this.scale = scale;
309     }
310
311     public boolean isLOB() {
312         return isLOB;
313     }
314
315     public void setLOB(boolean isLOB) {
316         this.isLOB = isLOB;
317     }
318
319     public boolean isLongType() {
320         return isLongType;
321     }
322
323     public void setLongType(boolean isLongType) {
324         this.isLongType = isLongType;
325     }
326
327     public String JavaDoc getNativeType() {
328         return nativeType;
329     }
330
331     public void setNativeType(String JavaDoc nativeType) {
332         this.nativeType = nativeType;
333     }
334
335 }
336
Popular Tags