KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > DataType


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.io.InputStream JavaDoc;
8 import java.io.Reader JavaDoc;
9 import java.math.BigDecimal JavaDoc;
10 import java.sql.Clob JavaDoc;
11 import java.sql.Date JavaDoc;
12 import java.sql.ResultSet JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.sql.Time JavaDoc;
15 import java.sql.Timestamp JavaDoc;
16 import java.sql.Types JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19 import org.h2.engine.Constants;
20 import org.h2.engine.SessionInterface;
21 import org.h2.jdbc.JdbcBlob;
22 import org.h2.jdbc.JdbcClob;
23 import org.h2.jdbc.JdbcConnection;
24 import org.h2.jdbc.JdbcSQLException;
25 import org.h2.message.Message;
26 import org.h2.util.ByteUtils;
27 import org.h2.util.ObjectArray;
28 import org.h2.util.StringUtils;
29
30 public class DataType {
31     private static ObjectArray types = new ObjectArray();
32     private static HashMap JavaDoc typesByName = new HashMap JavaDoc();
33     private static DataType[] typesByValueType = new DataType[Value.TYPE_COUNT];
34     public int type;
35     public String JavaDoc name;
36     public int sqlType;
37     public String JavaDoc jdbc;
38
39     // how closely the data type maps to the corresponding JDBC SQL type (low is best)
40
public int order;
41
42     public int maxPrecision;
43     public int minScale, maxScale;
44     public boolean decimal;
45     public String JavaDoc prefix, suffix;
46     public String JavaDoc params;
47     public boolean autoInc;
48     public boolean caseSensitive;
49     public boolean supportsPrecision, supportsScale;
50     public long defaultPrecision;
51     public int defaultScale;
52     public boolean hidden;
53     
54     // JDK 1.3 compatibility: Types.BOOLEAN
55
public static final int TYPE_BOOLEAN = 16;
56     // JDK 1.3 compatibility: Types.DATALINK
57
public static final int TYPE_DATALINK = 70;
58
59     static {
60 //#ifdef JDK14
61
if(TYPE_BOOLEAN != Types.BOOLEAN) {
62             new Exception JavaDoc("Types.BOOLEAN: " + Types.BOOLEAN).printStackTrace();
63         }
64         if(TYPE_DATALINK != Types.DATALINK) {
65             new Exception JavaDoc("Types.DATALINK: " + Types.DATALINK).printStackTrace();
66         }
67 //#endif
68
add(Value.NULL, Types.NULL, "Null",
69                 new DataType(),
70                 new String JavaDoc[]{"NULL"}
71         );
72         add(Value.BOOLEAN, DataType.TYPE_BOOLEAN, "Boolean",
73                 createDecimal(ValueBoolean.PRECISION, ValueBoolean.PRECISION, 0, false, false),
74                 new String JavaDoc[]{"BOOLEAN", "BIT", "BOOL"}
75         );
76         add(Value.BYTE, Types.TINYINT, "Byte",
77                 createDecimal(ValueByte.PRECISION, ValueByte.PRECISION, 0, false, false),
78                 new String JavaDoc[]{"TINYINT"}
79         );
80         add(Value.SHORT, Types.SMALLINT, "Short",
81                 createDecimal(ValueShort.PRECISION, ValueShort.PRECISION, 0, false, false),
82                 new String JavaDoc[]{"SMALLINT", "YEAR", "INT2"}
83         );
84         add(Value.INT, Types.INTEGER, "Int",
85                 createDecimal(ValueInt.PRECISION, ValueInt.PRECISION, 0, false, false),
86                 new String JavaDoc[]{"INTEGER", "INT", "MEDIUMINT", "INT4", "SIGNED"}
87         );
88         add(Value.LONG, Types.BIGINT, "Long",
89                 createDecimal(ValueLong.PRECISION, ValueLong.PRECISION, 0, false, false),
90                 new String JavaDoc[]{"BIGINT", "INT8"}
91         );
92         add(Value.LONG, Types.BIGINT, "Long",
93                 createDecimal(ValueLong.PRECISION, ValueLong.PRECISION, 0, false, true),
94                 new String JavaDoc[]{"IDENTITY"}
95         );
96         add(Value.DECIMAL, Types.DECIMAL, "BigDecimal",
97                 createDecimal(Integer.MAX_VALUE, ValueDecimal.DEFAULT_PRECISION, ValueDecimal.DEFAULT_SCALE, true, false),
98                 new String JavaDoc[]{"DECIMAL", "DEC"}
99                 // TODO value: are NaN, Inf, -Inf,... supported as well?
100
);
101         add(Value.DECIMAL, Types.NUMERIC, "BigDecimal",
102                 createDecimal(Integer.MAX_VALUE, ValueDecimal.DEFAULT_PRECISION, ValueDecimal.DEFAULT_SCALE, true, false),
103                 new String JavaDoc[]{"NUMERIC", "NUMBER"}
104                 // TODO value: are NaN, Inf, -Inf,... supported as well?
105
);
106         add(Value.DOUBLE, Types.DOUBLE, "Double",
107                 createDecimal(ValueDouble.PRECISION, ValueDouble.PRECISION, 0, false, false),
108                 new String JavaDoc[] { "DOUBLE", "DOUBLE PRECISION" }
109         );
110         add(Value.DOUBLE, Types.FLOAT, "Double",
111                 createDecimal(ValueDouble.PRECISION, ValueDouble.PRECISION, 0, false, false),
112                 new String JavaDoc[] {"FLOAT", "FLOAT8" }
113                 // TODO value: show min and max values, E format if supported
114
);
115         add(Value.FLOAT, Types.REAL, "Float",
116                 createDecimal(ValueFloat.PRECISION, ValueFloat.PRECISION, 0, false, false),
117                 new String JavaDoc[] {"REAL", "FLOAT4"}
118         );
119         add(Value.TIME, Types.TIME, "Time",
120                 createDate(ValueTime.PRECISION, "TIME", 0),
121                 new String JavaDoc[]{"TIME"}
122                 // TODO value: min / max for time
123
);
124         add(Value.DATE, Types.DATE, "Date",
125                 createDate(ValueDate.PRECISION, "DATE", 0),
126                 new String JavaDoc[]{"DATE"}
127                 // TODO value: min / max for date
128
);
129         add(Value.TIMESTAMP, Types.TIMESTAMP, "Timestamp",
130                 createDate(ValueTimestamp.PRECISION, "TIMESTAMP", ValueTimestamp.DEFAULT_SCALE),
131                 new String JavaDoc[]{"TIMESTAMP", "DATETIME", "SMALLDATETIME"}
132                 // TODO value: min / max for timestamp
133
);
134         add(Value.BYTES, Types.VARBINARY, "Bytes",
135                 createString(false),
136                 new String JavaDoc[]{"VARBINARY"}
137         );
138         add(Value.BYTES, Types.BINARY, "Bytes",
139                 createString(false),
140                 new String JavaDoc[]{"BINARY", "RAW", "BYTEA", "LONG RAW"}
141         );
142         add(Value.UUID, Types.BINARY, "Bytes",
143                 createString(false),
144                 new String JavaDoc[]{"UUID"}
145         );
146         add(Value.BYTES, Types.LONGVARBINARY, "Bytes",
147                 createString(false),
148                 new String JavaDoc[]{"LONGVARBINARY"}
149         );
150         add(Value.JAVA_OBJECT, Types.OTHER, "Object",
151                 createString(false),
152                 new String JavaDoc[]{"OTHER", "OBJECT", "JAVA_OBJECT"}
153         );
154         add(Value.STRING, Types.VARCHAR, "String",
155                 createString(true),
156                 new String JavaDoc[]{"VARCHAR", "VARCHAR2", "NVARCHAR", "NVARCHAR2", "VARCHAR_CASESENSITIVE"}
157         );
158         add(Value.STRING, Types.LONGVARCHAR, "String",
159                 createString(true),
160                 new String JavaDoc[]{"LONGVARCHAR"}
161         );
162         add(Value.STRING, Types.CHAR, "String",
163                 createString(true),
164                 new String JavaDoc[]{"CHAR", "CHARACTER", "NCHAR"}
165         );
166         add(Value.STRING_IGNORECASE, Types.VARCHAR, "String",
167                 createString(false),
168                 new String JavaDoc[]{"VARCHAR_IGNORECASE"}
169         );
170         add(Value.BLOB, Types.BLOB, "Bytes",
171                 createString(false),
172                 new String JavaDoc[]{"BLOB", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB", "IMAGE", "OID"}
173         );
174         add(Value.CLOB, Types.CLOB, "String",
175                 createString(true),
176                 new String JavaDoc[]{"CLOB", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT", "NTEXT", "NCLOB"}
177         );
178         add(Value.ARRAY, Types.ARRAY, "Array",
179                 createString(false),
180                 new String JavaDoc[]{"ARRAY"}
181         );
182         
183         // TODO data types: try to support other types as well (longvarchar for odbc/access,...) - maybe map them to regular types?
184
}
185
186     private static void add(int type, int sqlType, String JavaDoc jdbc, DataType dataType, String JavaDoc[] names) {
187         for(int i=0; i<names.length; i++) {
188             DataType dt = new DataType();
189             dt.type = type;
190             dt.sqlType = sqlType;
191             dt.jdbc = jdbc;
192             dt.name = names[i];
193             dt.autoInc = dataType.autoInc;
194             dt.decimal = dataType.decimal;
195             dt.maxPrecision = dataType.maxPrecision;
196             dt.maxScale = dataType.maxScale;
197             dt.minScale = dataType.minScale;
198             dt.params = dataType.params;
199             dt.prefix = dataType.prefix;
200             dt.suffix = dataType.suffix;
201             dt.supportsPrecision = dataType.supportsPrecision;
202             dt.supportsScale = dataType.supportsScale;
203             dt.defaultPrecision = dataType.defaultPrecision;
204             dt.defaultScale = dataType.defaultScale;
205             dt.caseSensitive = dataType.caseSensitive;
206             dt.hidden = i > 0;
207             for(int j=0; j<types.size(); j++) {
208                 DataType t2 = (DataType) types.get(j);
209                 if(t2.sqlType == dt.sqlType) {
210                     dt.order++;
211                 }
212             }
213             typesByName.put(dt.name, dt);
214             if(typesByValueType[type] == null) {
215                 typesByValueType[type] = dt;
216             }
217             types.add(dt);
218         }
219     }
220
221     public static String JavaDoc getJdbcString(int type) {
222         return typesByValueType[type].jdbc;
223     }
224
225     private static DataType createDecimal(int maxPrecision, int defaultPrecision, int defaultScale, boolean needsPrecisionAndScale, boolean autoInc) {
226         DataType dataType = new DataType();
227         dataType.maxPrecision = maxPrecision;
228         dataType.defaultPrecision = defaultPrecision;
229         dataType.defaultScale = defaultScale;
230         if(needsPrecisionAndScale) {
231             dataType.params = "PRECISION,SCALE";
232             dataType.supportsPrecision = true;
233             dataType.supportsScale = true;
234         }
235         dataType.decimal = true;
236         dataType.autoInc = autoInc;
237         return dataType;
238     }
239
240     private static DataType createDate(int precision, String JavaDoc prefix, int scale) {
241         DataType dataType = new DataType();
242         dataType.prefix = prefix + " '";
243         dataType.suffix = "'";
244         dataType.maxPrecision = precision;
245         dataType.supportsScale = scale != 0;
246         dataType.maxScale = scale;
247         dataType.defaultPrecision = precision;
248         dataType.defaultScale = scale;
249         return dataType;
250     }
251
252     private static DataType createString(boolean caseSensitive) {
253         DataType dataType = new DataType();
254         dataType.prefix = "'";
255         dataType.suffix = "'";
256         dataType.params = "LENGTH";
257         dataType.caseSensitive = caseSensitive;
258         dataType.supportsPrecision = true;
259         dataType.maxPrecision = Integer.MAX_VALUE;
260         dataType.defaultPrecision = Integer.MAX_VALUE;
261         return dataType;
262     }
263
264     public static ObjectArray getTypes() {
265         return types;
266     }
267
268     public static Value readValue(SessionInterface session, ResultSet JavaDoc rs, int columnIndex, int type) throws SQLException JavaDoc {
269         Value v;
270         switch(type) {
271         case Value.NULL: {
272             return ValueNull.INSTANCE;
273         }
274         case Value.BYTES: {
275             byte[] buff = rs.getBytes(columnIndex);
276             v = buff==null ? (Value)ValueNull.INSTANCE : ValueBytes.getNoCopy(buff);
277             break;
278         }
279         case Value.UUID: {
280             byte[] buff = rs.getBytes(columnIndex);
281             v = buff==null ? (Value)ValueNull.INSTANCE : ValueUuid.get(buff);
282             break;
283         }
284         case Value.BOOLEAN: {
285             boolean value = rs.getBoolean(columnIndex);
286             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueBoolean.get(value);
287             break;
288         }
289         case Value.BYTE: {
290             byte value = rs.getByte(columnIndex);
291             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueByte.get(value);
292             break;
293         }
294         case Value.DATE: {
295             Date JavaDoc value = rs.getDate(columnIndex);
296             v = value == null ? (Value)ValueNull.INSTANCE : ValueDate.get(value);
297             break;
298         }
299         case Value.TIME: {
300             Time JavaDoc value = rs.getTime(columnIndex);
301             v = value == null ? (Value)ValueNull.INSTANCE : ValueTime.get(value);
302             break;
303         }
304         case Value.TIMESTAMP: {
305             Timestamp JavaDoc value = rs.getTimestamp(columnIndex);
306             v = value == null ? (Value)ValueNull.INSTANCE : ValueTimestamp.get(value);
307             break;
308         }
309         case Value.DECIMAL: {
310             BigDecimal JavaDoc value = rs.getBigDecimal(columnIndex);
311             v = value == null ? (Value)ValueNull.INSTANCE : ValueDecimal.get(value);
312             break;
313         }
314         case Value.DOUBLE: {
315             double value = rs.getDouble(columnIndex);
316             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueDouble.get(value);
317             break;
318         }
319         case Value.FLOAT: {
320             float value = rs.getFloat(columnIndex);
321             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueFloat.get(value);
322             break;
323         }
324         case Value.INT: {
325             int value = rs.getInt(columnIndex);
326             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueInt.get(value);
327             break;
328         }
329         case Value.LONG: {
330             long value = rs.getLong(columnIndex);
331             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueLong.get(value);
332             break;
333         }
334         case Value.SHORT: {
335             short value = rs.getShort(columnIndex);
336             v = rs.wasNull() ? (Value)ValueNull.INSTANCE : ValueShort.get(value);
337             break;
338         }
339         case Value.STRING_IGNORECASE:
340         case Value.STRING: {
341             String JavaDoc s = rs.getString(columnIndex);
342             v = (s == null) ? (Value)ValueNull.INSTANCE : ValueString.get(s);
343             break;
344         }
345         case Value.CLOB: {
346             if(session == null) {
347                 v = ValueLob.createSmallLob(Value.CLOB, StringUtils.utf8Encode(rs.getString(columnIndex)));
348             } else {
349                 Reader JavaDoc in = rs.getCharacterStream(columnIndex);
350                 v = (in == null) ? (Value)ValueNull.INSTANCE : ValueLob.createClob(in, -1, session.getDataHandler());
351             }
352             break;
353         }
354         case Value.BLOB: {
355             if(session == null) {
356                 v = ValueLob.createSmallLob(Value.BLOB, rs.getBytes(columnIndex));
357             } else {
358                 InputStream JavaDoc in = rs.getBinaryStream(columnIndex);
359                 v = (in == null) ? (Value)ValueNull.INSTANCE : ValueLob.createBlob(in, -1, session.getDataHandler());
360             }
361             break;
362         }
363         case Value.JAVA_OBJECT: {
364             byte[] buff = rs.getBytes(columnIndex);
365             v = buff==null ? (Value)ValueNull.INSTANCE : ValueJavaObject.getNoCopy(buff);
366             break;
367         }
368         default:
369             throw Message.getInternalError("type="+type);
370         }
371         return v;
372     }
373
374     public static String JavaDoc getTypeClassName(int type) {
375         switch(type) {
376         case Value.BOOLEAN:
377             return Boolean JavaDoc.class.getName(); // "java.lang.Boolean";
378
case Value.BYTE:
379             return Byte JavaDoc.class.getName(); // "java.lang.Byte";
380
case Value.SHORT:
381             return Short JavaDoc.class.getName(); // "java.lang.Short";
382
case Value.INT:
383             return Integer JavaDoc.class.getName(); // "java.lang.Integer";
384
case Value.LONG:
385             return Long JavaDoc.class.getName(); // "java.lang.Long";
386
case Value.DECIMAL:
387             return BigDecimal JavaDoc.class.getName(); // "java.math.BigDecimal";
388
case Value.TIME:
389             return Time JavaDoc.class.getName(); // "java.sql.Time";
390
case Value.DATE:
391             return Date JavaDoc.class.getName(); // "java.sql.Date";
392
case Value.TIMESTAMP:
393             return Timestamp JavaDoc.class.getName(); // "java.sql.Timestamp";
394
case Value.BYTES:
395         case Value.UUID:
396             return byte[].class.getName(); // "[B", not "byte[]";
397
case Value.STRING:
398         case Value.STRING_IGNORECASE:
399             return String JavaDoc.class.getName(); // "java.lang.String";
400
case Value.BLOB:
401             return java.sql.Blob JavaDoc.class.getName(); // "java.sql.Blob";
402
case Value.CLOB:
403             return java.sql.Clob JavaDoc.class.getName(); // "java.sql.Clob";
404
case Value.DOUBLE:
405             return Double JavaDoc.class.getName(); // "java.lang.Double";
406
case Value.FLOAT:
407             return Float JavaDoc.class.getName(); // "java.lang.Float";
408
case Value.NULL:
409             return null;
410         case Value.JAVA_OBJECT:
411             return Object JavaDoc.class.getName(); // "java.lang.Object";
412
default:
413             throw Message.getInternalError("type="+type);
414         }
415     }
416
417     public static DataType getDataType(int type) {
418         DataType dt = typesByValueType[type];
419         if(dt == null) {
420             dt = typesByValueType[Value.NULL];
421         }
422         return dt;
423     }
424
425     public static int convertTypeToSQLType(int type) {
426         return getDataType(type).sqlType;
427     }
428
429     public static int convertSQLTypeToValueType(int sqlType) throws SQLException JavaDoc {
430         switch(sqlType) {
431         case Types.VARCHAR:
432         case Types.CHAR:
433         case Types.LONGVARCHAR:
434             return Value.STRING;
435         case Types.NUMERIC:
436         case Types.DECIMAL:
437             return Value.DECIMAL;
438         case Types.BIT:
439         case DataType.TYPE_BOOLEAN:
440             return Value.BOOLEAN;
441         case Types.INTEGER:
442             return Value.INT;
443         case Types.SMALLINT:
444             return Value.SHORT;
445         case Types.TINYINT:
446             return Value.BYTE;
447         case Types.BIGINT:
448             return Value.LONG;
449         case Types.REAL:
450             return Value.FLOAT;
451         case Types.DOUBLE:
452         case Types.FLOAT:
453             return Value.DOUBLE;
454         case Types.BINARY:
455         case Types.VARBINARY:
456         case Types.LONGVARBINARY:
457             return Value.BYTES;
458         case Types.OTHER:
459         case Types.JAVA_OBJECT:
460             return Value.JAVA_OBJECT;
461         case Types.DATE:
462             return Value.DATE;
463         case Types.TIME:
464             return Value.TIME;
465         case Types.TIMESTAMP:
466             return Value.TIMESTAMP;
467         case Types.BLOB:
468             return Value.BLOB;
469         case Types.CLOB:
470             return Value.CLOB;
471         case Types.NULL:
472             return Value.NULL;
473         default:
474             throw Message.getSQLException(Message.UNKNOWN_DATA_TYPE_1, ""+sqlType);
475         }
476     }
477
478     public static int getTypeFromClass(Class JavaDoc x) throws SQLException JavaDoc {
479         // TODO refactor: too many if/else in functions, can reduce!
480
if(x==null) {
481             return Value.NULL;
482         }
483         if(ResultSet JavaDoc.class.isAssignableFrom(x)) {
484             return Value.RESULT_SET;
485         } else if(String JavaDoc.class.isAssignableFrom(x)) {
486             return Value.STRING;
487         } else if(BigDecimal JavaDoc.class.isAssignableFrom(x)) {
488             return Value.DECIMAL;
489         } else if(Boolean JavaDoc.class.isAssignableFrom(x) || boolean.class.isAssignableFrom(x)) {
490             return Value.BOOLEAN;
491         } else if(Byte JavaDoc.class.isAssignableFrom(x) || byte.class.isAssignableFrom(x)) {
492             return Value.BYTE;
493         } else if(Short JavaDoc.class.isAssignableFrom(x) || short.class.isAssignableFrom(x)) {
494             return Value.SHORT;
495         } else if(Integer JavaDoc.class.isAssignableFrom(x) || int.class.isAssignableFrom(x)) {
496             return Value.INT;
497         } else if(Character JavaDoc.class.isAssignableFrom(x) || char.class.isAssignableFrom(x)) {
498             throw Message.getSQLException(Message.DATA_CONVERSION_ERROR_1, "char (not supported)");
499         } else if(Long JavaDoc.class.isAssignableFrom(x) || long.class.isAssignableFrom(x)) {
500             return Value.LONG;
501         } else if(Float JavaDoc.class.isAssignableFrom(x) || float.class.isAssignableFrom(x)) {
502             return Value.FLOAT;
503         } else if(Double JavaDoc.class.isAssignableFrom(x) || double.class.isAssignableFrom(x)) {
504             return Value.DOUBLE;
505         } else if(byte[].class.isAssignableFrom(x)) {
506             return Value.BYTES;
507         } else if(Date JavaDoc.class.isAssignableFrom(x)) {
508             return Value.DATE;
509         } else if(Time JavaDoc.class.isAssignableFrom(x)) {
510             return Value.TIME;
511         } else if(Timestamp JavaDoc.class.isAssignableFrom(x)) {
512             return Value.TIMESTAMP;
513         } else if(java.util.Date JavaDoc.class.isAssignableFrom(x)) {
514             return Value.DATE;
515         } else if(java.io.Reader JavaDoc.class.isAssignableFrom(x)) {
516             return Value.CLOB;
517         } else if(java.sql.Clob JavaDoc.class.isAssignableFrom(x)) {
518             return Value.CLOB;
519         } else if(java.io.InputStream JavaDoc.class.isAssignableFrom(x)) {
520             return Value.BLOB;
521         } else if(java.sql.Blob JavaDoc.class.isAssignableFrom(x)) {
522             return Value.BLOB;
523         } else if(Object JavaDoc[].class.isAssignableFrom(x)) {
524             return Value.ARRAY;
525         } else if(Void.TYPE==x) {
526             return Value.NULL;
527         } else {
528             return Value.JAVA_OBJECT;
529         }
530     }
531
532     public static Value convertToValue(SessionInterface session, Object JavaDoc x, int type) throws SQLException JavaDoc {
533         if(x==null) {
534             return ValueNull.INSTANCE;
535         }
536         if(type == Value.JAVA_OBJECT) {
537             // serialize JAVA_OBJECTs, even if the type is known
538
if(Constants.SERIALIZE_JAVA_OBJECTS) {
539                 return ValueJavaObject.getNoCopy(ByteUtils.serialize(x));
540             }
541         }
542         if(x instanceof String JavaDoc) {
543             return ValueString.get((String JavaDoc)x);
544         } else if(x instanceof BigDecimal JavaDoc) {
545             return ValueDecimal.get((BigDecimal JavaDoc)x);
546         } else if(x instanceof Boolean JavaDoc) {
547             return ValueBoolean.get(((Boolean JavaDoc)x).booleanValue());
548         } else if(x instanceof Byte JavaDoc) {
549             return ValueByte.get(((Byte JavaDoc)x).byteValue());
550         } else if(x instanceof Short JavaDoc) {
551             return ValueShort.get(((Short JavaDoc)x).shortValue());
552         } else if(x instanceof Integer JavaDoc) {
553             return ValueInt.get(((Integer JavaDoc)x).intValue());
554         } else if(x instanceof Long JavaDoc) {
555             return ValueLong.get(((Long JavaDoc)x).longValue());
556         } else if(x instanceof Float JavaDoc) {
557             return ValueFloat.get(((Float JavaDoc)x).floatValue());
558         } else if(x instanceof Double JavaDoc) {
559             return ValueDouble.get(((Double JavaDoc)x).doubleValue());
560         } else if(x instanceof byte[]) {
561             return ValueBytes.get((byte[])x);
562         } else if(x instanceof Date JavaDoc) {
563             return ValueDate.get((Date JavaDoc)x);
564         } else if(x instanceof Time JavaDoc) {
565             return ValueTime.get((Time JavaDoc)x);
566         } else if(x instanceof Timestamp JavaDoc) {
567             return ValueTimestamp.get((Timestamp JavaDoc)x);
568         } else if(x instanceof java.util.Date JavaDoc) {
569             return ValueDate.get(new Date JavaDoc(((java.util.Date JavaDoc)x).getTime()));
570         } else if(x instanceof java.io.Reader JavaDoc) {
571             return ValueLob.createClob((java.io.Reader JavaDoc)x, -1, session.getDataHandler());
572         } else if(x instanceof java.sql.Clob JavaDoc) {
573             return ValueLob.createClob(((java.sql.Clob JavaDoc)x).getCharacterStream(), -1, session.getDataHandler());
574         } else if(x instanceof java.io.InputStream JavaDoc) {
575             return ValueLob.createBlob((java.io.InputStream JavaDoc)x, -1, session.getDataHandler());
576         } else if(x instanceof java.sql.Blob JavaDoc) {
577             return ValueLob.createBlob(((java.sql.Blob JavaDoc)x).getBinaryStream(), -1, session.getDataHandler());
578         } else if(x instanceof ResultSet JavaDoc) {
579             return ValueResultSet.get((ResultSet JavaDoc)x);
580         } else if(x instanceof Object JavaDoc[]) {
581             // (a.getClass().isArray());
582
// (a.getClass().getComponentType().isPrimitive());
583
Object JavaDoc[] o = (Object JavaDoc[]) x;
584             int len = o.length;
585             Value[] v = new Value[len];
586             for(int i=0; i<len; i++) {
587                 v[i] = convertToValue(session, o[i], type);
588             }
589             return ValueArray.get(v);
590         } else {
591             if(Constants.SERIALIZE_JAVA_OBJECTS) {
592                 return ValueJavaObject.getNoCopy(ByteUtils.serialize(x));
593             } else {
594                 throw Message.getSQLException(Message.UNKNOWN_DATA_TYPE_1, x.getClass().getName());
595             }
596         }
597     }
598
599     public static DataType getTypeByName(String JavaDoc s) {
600         return (DataType) typesByName.get(s);
601     }
602
603     public static boolean isLargeObject(int type) {
604         if(type == Value.BLOB || type == Value.CLOB) {
605             return true;
606         }
607         return false;
608     }
609
610     public static boolean supportsAdd(int type) {
611         switch(type) {
612         case Value.BYTE:
613         case Value.DECIMAL:
614         case Value.DOUBLE:
615         case Value.FLOAT:
616         case Value.INT:
617         case Value.LONG:
618         case Value.SHORT:
619             return true;
620         }
621         return false;
622     }
623
624     public static Object JavaDoc getDefaultForPrimitiveType(Class JavaDoc clazz) {
625         if(clazz == Boolean.TYPE) {
626             return Boolean.FALSE;
627         } else if(clazz == Byte.TYPE) {
628             return new Byte JavaDoc((byte)0);
629         } else if(clazz == Character.TYPE) {
630             return new Character JavaDoc((char)0);
631         } else if(clazz == Short.TYPE) {
632             return new Short JavaDoc((short)0);
633         } else if(clazz == Integer.TYPE) {
634             return new Integer JavaDoc(0);
635         } else if(clazz == Long.TYPE) {
636             return new Long JavaDoc(0);
637         } else if(clazz == Float.TYPE) {
638             return new Float JavaDoc(0);
639         } else if(clazz == Double.TYPE) {
640             return new Double JavaDoc(0);
641         } else {
642             throw Message.getInternalError("primitive="+ clazz.toString());
643         }
644     }
645
646     public static Object JavaDoc convertTo(SessionInterface session, JdbcConnection conn, Value v, Class JavaDoc paramClass) throws JdbcSQLException {
647         if(paramClass == java.sql.Blob JavaDoc.class) {
648             return new JdbcBlob(session, conn, v, 0);
649         } else if(paramClass == Clob JavaDoc.class) {
650             return new JdbcClob(session, conn, v, 0);
651         } else {
652             throw Message.getUnsupportedException();
653         }
654     }
655
656 }
657
Popular Tags