1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db;
22
23 import java.io.Serializable;
24 import java.sql.Types;
25
26 import org.enableit.db.beans.Column;
27
28
29 /***
30 * <p>This class has a primary purpose of representing a null value to be
31 * inserted into some SQL call handled by the proxy objects</p>
32 *
33 * <p>This is necessary to allow the proxies to receive a null object
34 * yet still provide the JDBC driver with a known object type
35 * as required by <CODE>PreparedStatement.setNull</CODE></p>
36 *
37 * <p>The integer used in the constructor should be one of the static
38 * final members of <CODE>Types</CODE></p>
39 *
40 * @version __VERSION__
41 *
42 * @author tim@thestephensons.org
43 *
44 * @see java.sql.Types java.sql.Types <br/>
45 * @see org.enableit.db.DatabaseProxy DatabaseProxy <br/>
46 * @see org.enableit.db.GenericDBProxy GenericDBProxy <br/>
47 */
48 public class SqlType implements Serializable {
49 /***
50 * Represents a Boolean
51 */
52 public static final int BOOLEAN = Types.BIT;
53
54 /***
55 * Represents a Character
56 */
57 public static final int CHARACTER = Types.CHAR;
58
59 /***
60 * Represents a Double
61 */
62 public static final int DOUBLE = Types.DOUBLE;
63
64 /***
65 * Represents a Float
66 */
67 public static final int FLOAT = Types.FLOAT;
68
69 /***
70 * Represents an Integer
71 * @deprecated
72 */
73 public static final int INTEGER = Types.INTEGER;
74
75 /***
76 * Represents a Long
77 */
78 public static final int LONG = Types.NUMERIC;
79
80 /***
81 * Represents a Short
82 */
83 public static final int SHORT = Types.SMALLINT;
84
85 /***
86 * Represents a String
87 */
88 public static final int STRING = Types.VARCHAR;
89
90 /***
91 * Represents a BigDecimal
92 */
93 public static final int BIGDECIMAL = Types.NUMERIC;
94
95 /***
96 * Represents a java.util.Date
97 */
98 public static final int DATE = Types.DATE;
99
100 /***
101 * Represents a Byte[]
102 */
103 public static final int BYTEARRAY = Types.BLOB;
104
105 /***
106 * Represents a SQL Date
107 */
108 public static final int SQLDATE = Types.DATE;
109
110 /***
111 * Represents a SQL Time
112 */
113 public static final int SQLTIME = Types.TIME;
114
115 /*
116 * Represents a SQL Timestamp.
117 * @deprecated
118 */
119 public static final int SQLTIMESTAMP = Types.TIMESTAMP;
120
121 /***
122 * Information on the exact CVS version accessible after compilation
123 */
124 public static final String ABOUT = "$Revision: 1.13 $";
125
126 /*** the internal storage for this dataType. */
127 private int dataType = 0;
128
129 /*** the internal storage for this data type's scale */
130 private int scale = 0;
131
132 /*** the internal storage for this data type's precision */
133 private int precision = 0;
134
135 /***
136 * Constructs a new instance of this class initialised with the integer constant of its type.
137 * @param dataType the int that represents the datatype that this class is being created for.
138 */
139 public SqlType(int dataType) {
140 this.dataType = dataType;
141 }
142
143 /***
144 * Constructs a new instance of this class initialised with the integer constant of its type.
145 * @param dataType the int that represents the datatype that this class is being created for.
146 * @param scale The scale of this data type.
147 * @param precision The precision of this data type.
148 */
149 public SqlType(int dataType, int scale, int precision) {
150 this.dataType = dataType;
151 this.scale = scale;
152 this.precision = precision;
153 }
154
155 /***
156 * Constructs a new instance of this class initialised with the datatype
157 * that matches the string in the <code>Column</code> instance most closely.
158 * @param column Encapsulates data ABOUT a db column.
159 */
160 public SqlType(Column column) {
161 String colType = column.getColType();
162
163 if (colType.startsWith("numeric")) {
164 this.dataType = Types.NUMERIC;
165 this.scale = Integer.parseInt(colType.substring(colType.indexOf("(")
166 + 1, colType.indexOf(",")));
167 this.precision = Integer.parseInt(colType.substring(colType.indexOf(
168 ",") + 1, colType.indexOf(")")));
169 } else if (colType.startsWith("char")) {
170 this.dataType = Types.CHAR;
171 } else if (colType.equals("clob")) {
172 //TODO: This is necessary to work with ASA,
173 //should encapsulate within the DDL Factory
174 this.dataType = Types.LONGVARCHAR;
175 } else if (colType.equals("longvarchar")) {
176 this.dataType = Types.LONGVARCHAR;
177 } else if (colType.startsWith("varchar")) {
178 this.dataType = Types.VARCHAR;
179 } else if (colType.equals("float")) {
180 this.dataType = Types.FLOAT;
181 } else if (colType.startsWith("int")) {
182 this.dataType = Types.INTEGER;
183 } else if (colType.equals("date")) {
184 this.dataType = Types.DATE;
185 } else if (colType.equals("time")) {
186 this.dataType = Types.TIME;
187 } else if (colType.equals("datetime")) {
188 this.dataType = Types.TIMESTAMP;
189 }
190 }
191
192 /***
193 * Returns a string name of the datatype represented by this object
194 */
195 public String toString() {
196 String dataTypeName;
197
198 switch (dataType) {
199 case Types.BINARY:
200 case Types.BLOB:
201 dataTypeName = "blob";
202
203 break;
204 case Types.LONGVARCHAR:
205 case Types.CLOB:
206 dataTypeName = "clob";
207
208 break;
209 case Types.BIT:
210 case Types.CHAR:
211 dataTypeName = "char";
212 dataTypeName += "(";
213 dataTypeName += String.valueOf(scale);
214 dataTypeName += ")";
215
216 break;
217 case Types.DATE:
218 dataTypeName = "date";
219
220 break;
221 case Types.DECIMAL:
222 dataTypeName = "decimal";
223 dataTypeName += "(";
224 dataTypeName += String.valueOf(scale);
225 dataTypeName += ",";
226 dataTypeName += String.valueOf(precision);
227 dataTypeName += ")";
228
229 break;
230 case Types.DOUBLE:
231 dataTypeName = "double";
232
233 break;
234 case Types.FLOAT:
235 dataTypeName = "float";
236
237 break;
238 case Types.INTEGER:
239 dataTypeName = "int";
240
241 break;
242 case Types.JAVA_OBJECT:
243 dataTypeName = "java.lang.Object";
244
245 break;
246 case Types.LONGVARBINARY:
247 dataTypeName = "long varbinary";
248
249 break;
250 case Types.NUMERIC:
251 dataTypeName = "numeric";
252 dataTypeName += "(";
253 dataTypeName += String.valueOf(scale);
254 dataTypeName += ",";
255 dataTypeName += String.valueOf(precision);
256 dataTypeName += ")";
257
258 break;
259 case Types.OTHER:
260 dataTypeName = "java.lang.Object";
261
262 break;
263 case Types.REAL:
264 dataTypeName = "real";
265
266 break;
267 case Types.SMALLINT:
268 dataTypeName = "smallint";
269
270 break;
271 case Types.TINYINT:
272 dataTypeName = "tinyint";
273
274 break;
275 case Types.TIME:
276 dataTypeName = "time";
277
278 break;
279 case Types.TIMESTAMP:
280 dataTypeName = "datetime";
281
282 break;
283 case Types.VARCHAR:
284 dataTypeName = "varchar";
285 dataTypeName += "(";
286 dataTypeName += String.valueOf(scale);
287 dataTypeName += ")";
288
289 break;
290 default:
291 dataTypeName = "NotImplemented, enumerated type=" + dataType;
292 }
293
294 return dataTypeName;
295 }
296
297 /***
298 * Returns the datatype that this class represents
299 * @returns int the datatype that this class is representative of.
300 */
301 public int getDataType() {
302 return dataType;
303 }
304
305 /***
306 * Returns the appropriate Java datatype that this class represents
307 * @returns The Java datatype that this class is representative of.
308 */
309 public String getJavaType() {
310 String javaType = null;
311
312 switch (dataType) {
313 case Types.BINARY:
314 case Types.BLOB:
315 javaType = "java.lang.Object";
316
317 break;
318 case Types.LONGVARCHAR:
319 case Types.CLOB:
320 case Types.BIT:
321 case Types.CHAR:
322 javaType = "java.lang.String";
323
324 break;
325 case Types.DATE:
326 javaType = "java.sql.Date";
327
328 break;
329 case Types.DECIMAL:
330 javaType = "java.math.BigDecimal";
331
332 break;
333 case Types.DOUBLE:
334 javaType = "java.lang.Double";
335
336 break;
337 case Types.FLOAT:
338 javaType = "java.lang.Float";
339
340 break;
341 case Types.INTEGER:
342 javaType = "java.lang.Integer";
343
344 break;
345 case Types.JAVA_OBJECT:
346 javaType = "java.lang.Object";
347
348 break;
349 case Types.LONGVARBINARY:
350 javaType = "java.lang.Object";
351
352 break;
353 case Types.NUMERIC:
354 javaType = "java.math.BigDecimal";
355
356 break;
357 case Types.OTHER:
358 javaType = "java.lang.Object";
359
360 break;
361 case Types.REAL:
362 javaType = "java.lang.Float";
363
364 break;
365 case Types.SMALLINT:
366 javaType = "java.lang.Integer";
367
368 break;
369 case Types.TINYINT:
370 javaType = "java.lang.Integer";
371
372 break;
373 case Types.TIME:
374 javaType = "java.sql.Time";
375
376 break;
377 case Types.TIMESTAMP:
378 javaType = "java.sql.Timestamp";
379
380 break;
381 case Types.VARCHAR:
382 javaType = "java.lang.String";
383
384 break;
385 default:
386 javaType = "NotImplemented, enumerated type=" + dataType;
387 }
388
389 return javaType;
390 }
391 }
This page was automatically generated by Maven