001 package org.apache.ojb.broker.metadata;
002
003 /* Copyright 2003-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.io.ByteArrayOutputStream;
019 import java.io.IOException;
020 import java.io.InputStream;
021 import java.lang.reflect.Field;
022 import java.math.BigDecimal;
023 import java.net.URL;
024 import java.sql.Blob;
025 import java.sql.CallableStatement;
026 import java.sql.Clob;
027 import java.sql.Date;
028 import java.sql.Ref;
029 import java.sql.ResultSet;
030 import java.sql.SQLException;
031 import java.sql.Struct;
032 import java.sql.Time;
033 import java.sql.Timestamp;
034 import java.sql.Types;
035 import java.util.HashMap;
036 import java.util.Map;
037
038 import org.apache.commons.lang.BooleanUtils;
039 import org.apache.commons.lang.builder.ToStringBuilder;
040 import org.apache.ojb.broker.OJBRuntimeException;
041 import org.apache.ojb.broker.util.sequence.SequenceManagerException;
042
043 /**
044 * Helper class which provide all supported {@link JdbcType} classes
045 * (based on the {@link java.sql.Types}) as inner classes.
046 *
047 * @see JdbcType
048 * @version $Id: JdbcTypesHelper.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
049 */
050 public class JdbcTypesHelper
051 {
052 private static Map jdbcObjectTypesFromType = new HashMap();
053 private static Map jdbcObjectTypesFromName = new HashMap();
054
055 /**
056 * Hold out all JdbcType in a static maps
057 */
058 static
059 {
060 setJdbcType("array", Types.ARRAY, new T_Array());
061 setJdbcType("bigint", Types.BIGINT, new T_BigInt());
062 setJdbcType("binary", Types.BINARY, new T_Binary());
063 setJdbcType("bit", Types.BIT, new T_Bit());
064 setJdbcType("blob", Types.BLOB, new T_Blob());
065 setJdbcType("char", Types.CHAR, new T_Char());
066 setJdbcType("clob", Types.CLOB, new T_Clob());
067 setJdbcType("date", Types.DATE, new T_Date());
068 setJdbcType("decimal", Types.DECIMAL, new T_Decimal());
069 setJdbcType("double", Types.DOUBLE, new T_Double());
070 setJdbcType("float", Types.FLOAT, new T_Float());
071 setJdbcType("integer", Types.INTEGER, new T_Integer());
072 setJdbcType("longvarbinary", Types.LONGVARBINARY, new T_LongVarBinary());
073 setJdbcType("longvarchar", Types.LONGVARCHAR, new T_LongVarChar());
074 setJdbcType("numeric", Types.NUMERIC, new T_Numeric());
075 setJdbcType("real", Types.REAL, new T_Real());
076 setJdbcType("ref", Types.REF, new T_Ref());
077 setJdbcType("smallint", Types.SMALLINT, new T_SmallInt());
078 setJdbcType("struct", Types.STRUCT, new T_Struct());
079 setJdbcType("time", Types.TIME, new T_Time());
080 setJdbcType("timestamp", Types.TIMESTAMP, new T_Timestamp());
081 setJdbcType("tinyint", Types.TINYINT, new T_TinyInt());
082 setJdbcType("varbinary", Types.VARBINARY, new T_VarBinary());
083 setJdbcType("varchar", Types.VARCHAR, new T_Varchar());
084
085 //#ifdef JDBC30
086 setJdbcType("boolean", Types.BOOLEAN, new T_Boolean());
087 setJdbcType("datalink", Types.DATALINK, new T_Datalink());
088 //#endif
089
090 }
091
092 public JdbcTypesHelper()
093 {
094 // default constructor
095 }
096
097 /**
098 * Set the {@link JdbcType} by name and index.
099 * @param typeName Name of the type
100 * @param typeIndex index of the type
101 * @param type the type
102 */
103 public static void setJdbcType(String typeName, int typeIndex, JdbcType type)
104 {
105 setJdbcTypeByName(typeName, type);
106 setJdbcTypeByTypesIndex(typeIndex, type);
107 }
108
109 /**
110 * Return the {@link JdbcType} for the given jdbc {@link java.sql.Types type}.
111 */
112 public static JdbcType getJdbcTypeByTypesIndex(Integer type)
113 {
114 return (JdbcType) jdbcObjectTypesFromType.get(type);
115 }
116
117 /**
118 * Set the {@link JdbcType} by index.
119 * @param typeIndex index of the type
120 * @param type the type
121 */
122 public static void setJdbcTypeByTypesIndex(int typeIndex, JdbcType type)
123 {
124 jdbcObjectTypesFromType.put(new Integer(typeIndex), type);
125 }
126
127 /**
128 * Lookup the {@link JdbcType} by name. If name was not found an exception
129 * is thrown.
130 */
131 public static JdbcType getJdbcTypeByName(String typeName)
132 {
133 JdbcType result = null;
134 result = (JdbcType) jdbcObjectTypesFromName.get(typeName.toLowerCase());
135 if (result == null)
136 {
137 throw new OJBRuntimeException("The type " + typeName + " can not be handled by OJB." +
138 " Please specify only types as defined by java.sql.Types.");
139 }
140 return result;
141 }
142
143 /**
144 * Set the {@link JdbcType} by name.
145 * @param typeName Name of the type
146 * @param type the type
147 */
148 public static void setJdbcTypeByName(String typeName, JdbcType type)
149 {
150 jdbcObjectTypesFromName.put(typeName, type);
151 }
152
153 /**
154 * Try to automatically assign a jdbc type for the given
155 * java type name. This method is used if e.g. in metadata a
156 * column type was not set.
157 *
158 * @see FieldDescriptor#getJdbcType
159 */
160 public static JdbcType getJdbcTypeByReflection(String fieldType)
161 {
162 JdbcType result;
163
164 if (fieldType.equalsIgnoreCase(Character.class.getName()) || fieldType.equalsIgnoreCase("char"))
165 result = getJdbcTypeByName("char");
166 else if (fieldType.equalsIgnoreCase(Short.class.getName()) || fieldType.equalsIgnoreCase("short"))
167 result = getJdbcTypeByName("smallint");
168 else if (fieldType.equalsIgnoreCase(Integer.class.getName()) || fieldType.equalsIgnoreCase("int"))
169 result = getJdbcTypeByName("integer");
170 else if (fieldType.equalsIgnoreCase(Long.class.getName()) || fieldType.equalsIgnoreCase("long"))
171 result = getJdbcTypeByName("bigint");
172 else if (fieldType.equalsIgnoreCase(Byte.class.getName()) || fieldType.equalsIgnoreCase("byte"))
173 result = getJdbcTypeByName("tinyint");
174 else if (fieldType.equalsIgnoreCase(Float.class.getName()) || fieldType.equalsIgnoreCase("float"))
175 result = getJdbcTypeByName("real");
176 else if (fieldType.equalsIgnoreCase(Double.class.getName()) || fieldType.equalsIgnoreCase("double"))
177 result = getJdbcTypeByName("float");
178 else if (fieldType.equalsIgnoreCase(String.class.getName()))
179 result = getJdbcTypeByName("varchar");
180 /*
181 TODO: arminw: useful? This only will work in conjunction with a FieldConversion
182 */
183 else if (fieldType.equalsIgnoreCase(java.util.Date.class.getName()))
184 result = getJdbcTypeByName("date");
185 else if (fieldType.equalsIgnoreCase(Date.class.getName()))
186 result = getJdbcTypeByName("date");
187 else if (fieldType.equalsIgnoreCase(Time.class.getName()))
188 result = getJdbcTypeByName("time");
189 else if (fieldType.equalsIgnoreCase(Timestamp.class.getName()))
190 result = getJdbcTypeByName("timestamp");
191 else if (fieldType.equalsIgnoreCase(BigDecimal.class.getName()))
192 result = getJdbcTypeByName("decimal");
193 else if (fieldType.equalsIgnoreCase(Ref.class.getName()))
194 result = getJdbcTypeByName("ref");
195 else if (fieldType.equalsIgnoreCase(Struct.class.getName()))
196 result = getJdbcTypeByName("struct");
197 else if (fieldType.equalsIgnoreCase(Boolean.class.getName()) || fieldType.equalsIgnoreCase("boolean"))
198 result = getJdbcTypeByName("bit");
199 //#ifdef JDBC30
200 else if (fieldType.equalsIgnoreCase(URL.class.getName()))
201 result = getJdbcTypeByName("datalink");
202 //#endif
203 else
204 throw new OJBRuntimeException("The type " + fieldType + " can not be handled by OJB automatically."
205 + " Please specify a type as defined by java.sql.Types in your field-descriptor");
206 return result;
207 }
208
209
210 /**
211 * Returns an java object read from the specified ResultSet column.
212 */
213 public static Object getObjectFromColumn(ResultSet rs, Integer jdbcType, int columnId)
214 throws SQLException
215 {
216 return getObjectFromColumn(rs, null, jdbcType, null, columnId);
217 }
218
219 /**
220 * Returns an java object for the given jdbcType by extract from the given
221 * CallableStatement or ResultSet.
222 * NOTE: Exactly one of the arguments of type CallableStatement or ResultSet
223 * have to be non-null.
224 * If the 'columnId' argument is equals {@link JdbcType#MIN_INT}, then the given 'columnName'
225 * argument is used to lookup column. Else the given 'columnId' is used as column index.
226 */
227 private static Object getObjectFromColumn(ResultSet rs, CallableStatement stmt, Integer jdbcType, String columnName, int columnId)
228 throws SQLException
229 {
230 return getJdbcTypeByTypesIndex(jdbcType).getObjectFromColumn(rs, stmt, columnName, columnId);
231 }
232
233 /**
234 * Returns a string representation of the given {@link java.sql.Types} value.
235 */
236 public static String getSqlTypeAsString(int jdbcType)
237 {
238 String statusName = "*can't find String representation for sql type '" + jdbcType + "'*";
239 try
240 {
241 Field[] fields = Types.class.getDeclaredFields();
242 for (int i = 0; i < fields.length; i++)
243 {
244 if (fields[i].getInt(null) == jdbcType)
245 {
246 statusName = fields[i].getName();
247 break;
248 }
249 }
250 }
251 catch (Exception ignore)
252 {
253 // ignore it
254 }
255 return statusName;
256 }
257
258
259 //======================================================================================
260 // inner classes implementing JdbcType interface
261 //======================================================================================
262
263 public abstract static class BaseType implements JdbcType
264 {
265 private FieldType fieldType;
266
267 protected BaseType()
268 {
269 fieldType = FieldTypeClasses.newFieldType(this);
270 }
271
272 abstract Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException;
273
274 abstract Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException;
275
276 abstract Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException;
277 /*
278 only supported by jdk >= 1.4x, maybe useful in further versions
279 */
280 // abstract Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException;
281
282 public boolean equals(Object obj)
283 {
284 if (this == obj) return true;
285 boolean result = false;
286 if (obj instanceof JdbcType)
287 {
288 result = this.getType() == ((JdbcType) obj).getType();
289 }
290 return result;
291 }
292
293 public int hashCode()
294 {
295 return getType();
296 }
297
298 public FieldType getFieldType()
299 {
300 return fieldType;
301 }
302
303 public Object getObjectFromColumn(CallableStatement stmt, int columnId) throws SQLException
304 {
305 return getObjectFromColumn(null, stmt, null, columnId);
306 }
307
308 public Object getObjectFromColumn(ResultSet rs, String columnName) throws SQLException
309 {
310 return getObjectFromColumn(rs, null, columnName, MIN_INT);
311 }
312
313 public Object getObjectFromColumn(final ResultSet rs, final CallableStatement stmt,
314 final String columnName, int columnIndex) throws SQLException
315 {
316 if (stmt != null)
317 {
318 // return columnIndex == MIN_INT
319 // ? readValueFromStatement(stmt, columnName) : readValueFromStatement(stmt, columnIndex);
320 if (columnIndex == MIN_INT)
321 {
322 throw new UnsupportedOperationException("Not implemented yet");
323 }
324 else
325 {
326 return readValueFromStatement(stmt, columnIndex);
327 }
328 }
329 else
330 {
331 return columnIndex == MIN_INT
332 ? readValueFromResultSet(rs, columnName) : readValueFromResultSet(rs, columnIndex);
333 }
334 }
335
336 public String toString()
337 {
338 return new ToStringBuilder(this)
339 .append("jdbcType", getType())
340 .append("jdbcTypeString", getSqlTypeAsString(getType()))
341 .append("associatedFieldType", getFieldType())
342 .toString();
343 }
344
345 // // not used in code, but maybe useful in further versions
346 // public Object getObjectFromColumn(CallableStatement stmt, String columnName) throws SQLException
347 // {
348 // return getObjectFromColumn(null, stmt, columnName, MIN_INT);
349 // }
350 //
351 // public Object getObjectFromColumn(ResultSet rs, int columnId) throws SQLException
352 // {
353 // return getObjectFromColumn(rs, null, null, columnId);
354 // }
355
356 }
357
358
359 public static final class T_Char extends BaseType
360 {
361 public Object sequenceKeyConversion(Long identifier)
362 {
363 return identifier.toString();
364 }
365
366 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
367 // {
368 // return stmt.getString(columnName);
369 // }
370
371 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
372 {
373 return stmt.getString(columnIndex);
374 }
375
376 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
377 {
378 return rs.getString(columnName);
379 }
380
381 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
382 {
383 return rs.getString(columnIndex);
384 }
385
386 public int getType()
387 {
388 return Types.CHAR;
389 }
390 }
391
392 public static final class T_Varchar extends BaseType
393 {
394 public Object sequenceKeyConversion(Long identifier)
395 {
396 return identifier.toString();
397 }
398
399 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
400 // {
401 // return stmt.getString(columnName);
402 // }
403
404 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
405 {
406 return stmt.getString(columnIndex);
407 }
408
409 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
410 {
411 return rs.getString(columnName);
412 }
413
414 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
415 {
416 return rs.getString(columnIndex);
417 }
418
419 public int getType()
420 {
421 return Types.VARCHAR;
422 }
423 }
424
425 public static final class T_LongVarChar extends BaseType
426 {
427 public Object sequenceKeyConversion(Long identifier)
428 {
429 return identifier.toString();
430 }
431
432 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
433 // {
434 // return stmt.getString(columnName);
435 // }
436
437 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
438 {
439 return stmt.getString(columnIndex);
440 }
441
442 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
443 {
444 return rs.getString(columnName);
445 }
446
447 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
448 {
449 return rs.getString(columnIndex);
450 }
451
452 public int getType()
453 {
454 return Types.LONGVARCHAR;
455 }
456 }
457
458 public static final class T_Numeric extends BaseType
459 {
460 public Object sequenceKeyConversion(Long identifier)
461 {
462 return new BigDecimal(identifier.longValue());
463 }
464
465 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
466 // {
467 // return stmt.getBigDecimal(columnName);
468 // }
469
470 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
471 {
472 return stmt.getBigDecimal(columnIndex);
473 }
474
475 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
476 {
477 return rs.getBigDecimal(columnName);
478 }
479
480 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
481 {
482 return rs.getBigDecimal(columnIndex);
483 }
484
485 public int getType()
486 {
487 return Types.NUMERIC;
488 }
489 }
490
491 public static final class T_Decimal extends BaseType
492 {
493 public Object sequenceKeyConversion(Long identifier)
494 {
495 return new BigDecimal(identifier.longValue());
496 }
497
498 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
499 // {
500 // return stmt.getBigDecimal(columnName);
501 // }
502
503 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
504 {
505 return stmt.getBigDecimal(columnIndex);
506 }
507
508 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
509 {
510 return rs.getBigDecimal(columnName);
511 }
512
513 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
514 {
515 return rs.getBigDecimal(columnIndex);
516 }
517
518 public int getType()
519 {
520 return Types.DECIMAL;
521 }
522 }
523
524 public static final class T_Bit extends BaseType
525 {
526 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
527 {
528 throw new SequenceManagerException("Not supported sequence key type 'BIT'");
529 }
530
531 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
532 // {
533 // boolean temp = stmt.getBoolean(columnName);
534 // return (stmt.wasNull() ? null : new Boolean(temp));
535 // }
536
537 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
538 {
539 boolean temp = stmt.getBoolean(columnIndex);
540 return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
541 }
542
543 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
544 {
545 boolean temp = rs.getBoolean(columnName);
546 return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
547 }
548
549 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
550 {
551 boolean temp = rs.getBoolean(columnIndex);
552 return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
553 }
554
555 public int getType()
556 {
557 return Types.BIT;
558 }
559 }
560
561 //#ifdef JDBC30
562 public static final class T_Boolean extends BaseType
563 {
564 public Object sequenceKeyConversion(final Long identifier) throws SequenceManagerException
565 {
566 throw new SequenceManagerException("Not supported sequence key type 'BOOLEAN'");
567 }
568
569 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
570 // {
571 // boolean temp = stmt.getBoolean(columnName);
572 // return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
573 // }
574
575 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
576 {
577 boolean temp = stmt.getBoolean(columnIndex);
578 return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
579 }
580
581 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
582 {
583 boolean temp = rs.getBoolean(columnName);
584 return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
585 }
586
587 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
588 {
589 boolean temp = rs.getBoolean(columnIndex);
590 return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
591 }
592
593 public int getType()
594 {
595 return Types.BOOLEAN;
596 }
597 }
598 //#endif
599
600 public static final class T_TinyInt extends BaseType
601 {
602 public Object sequenceKeyConversion(final Long identifier)
603 {
604 return new Byte(identifier.byteValue());
605 }
606
607 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
608 // {
609 // byte temp = stmt.getByte(columnName);
610 // return (stmt.wasNull() ? null : new Byte(temp));
611 // }
612
613 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
614 {
615 byte temp = stmt.getByte(columnIndex);
616 return (stmt.wasNull() ? null : new Byte(temp));
617 }
618
619 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
620 {
621 byte temp = rs.getByte(columnName);
622 return (rs.wasNull() ? null : new Byte(temp));
623 }
624
625 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
626 {
627 byte temp = rs.getByte(columnIndex);
628 return (rs.wasNull() ? null : new Byte(temp));
629 }
630
631 public int getType()
632 {
633 return Types.TINYINT;
634 }
635 }
636
637 public static final class T_SmallInt extends BaseType
638 {
639 public Object sequenceKeyConversion(Long identifier)
640 {
641 return new Short(identifier.shortValue());
642 }
643
644 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
645 // {
646 // short temp = stmt.getShort(columnName);
647 // return (stmt.wasNull() ? null : new Short(temp));
648 // }
649
650 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
651 {
652 short temp = stmt.getShort(columnIndex);
653 return (stmt.wasNull() ? null : new Short(temp));
654 }
655
656 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
657 {
658 short temp = rs.getShort(columnName);
659 return (rs.wasNull() ? null : new Short(temp));
660 }
661
662 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
663 {
664 short temp = rs.getShort(columnIndex);
665 return (rs.wasNull() ? null : new Short(temp));
666 }
667
668 public int getType()
669 {
670 return Types.SMALLINT;
671 }
672 }
673
674 public static final class T_Integer extends BaseType
675 {
676 public Object sequenceKeyConversion(Long identifier)
677 {
678 return new Integer(identifier.intValue());
679 }
680
681 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
682 // {
683 // int temp = stmt.getInt(columnName);
684 // return (stmt.wasNull() ? null : new Integer(temp));
685 // }
686
687 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
688 {
689 int temp = stmt.getInt(columnIndex);
690 return (stmt.wasNull() ? null : new Integer(temp));
691 }
692
693 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
694 {
695 int temp = rs.getInt(columnName);
696 return (rs.wasNull() ? null : new Integer(temp));
697 }
698
699 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
700 {
701 int temp = rs.getInt(columnIndex);
702 return (rs.wasNull() ? null : new Integer(temp));
703 }
704
705 public int getType()
706 {
707 return Types.INTEGER;
708 }
709 }
710
711 public static final class T_BigInt extends BaseType
712 {
713 public Object sequenceKeyConversion(Long identifier)
714 {
715 return identifier;
716 }
717
718 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
719 // {
720 // long temp = stmt.getLong(columnName);
721 // return (stmt.wasNull() ? null : new Long(temp));
722 // }
723
724 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
725 {
726 long temp = stmt.getLong(columnIndex);
727 return (stmt.wasNull() ? null : new Long(temp));
728 }
729
730 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
731 {
732 long temp = rs.getLong(columnName);
733 return (rs.wasNull() ? null : new Long(temp));
734 }
735
736 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
737 {
738 long temp = rs.getLong(columnIndex);
739 return (rs.wasNull() ? null : new Long(temp));
740 }
741
742 public int getType()
743 {
744 return Types.BIGINT;
745 }
746 }
747
748 public static final class T_Real extends BaseType
749 {
750 public Object sequenceKeyConversion(Long identifier)
751 {
752 return new Float(identifier.floatValue());
753 }
754
755 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
756 // {
757 // float temp = stmt.getFloat(columnName);
758 // return (stmt.wasNull() ? null : new Float(temp));
759 // }
760
761 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
762 {
763 float temp = stmt.getFloat(columnIndex);
764 return (stmt.wasNull() ? null : new Float(temp));
765 }
766
767 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
768 {
769 float temp = rs.getFloat(columnName);
770 return (rs.wasNull() ? null : new Float(temp));
771 }
772
773 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
774 {
775 float temp = rs.getFloat(columnIndex);
776 return (rs.wasNull() ? null : new Float(temp));
777 }
778
779 public int getType()
780 {
781 return Types.REAL;
782 }
783 }
784
785 public static final class T_Float extends BaseType
786 {
787 public Object sequenceKeyConversion(Long identifier)
788 {
789 return new Double(identifier.doubleValue());
790 }
791
792 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
793 // {
794 // double temp = stmt.getDouble(columnName);
795 // return (stmt.wasNull() ? null : new Double(temp));
796 // }
797
798 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
799 {
800 double temp = stmt.getDouble(columnIndex);
801 return (stmt.wasNull() ? null : new Double(temp));
802 }
803
804 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
805 {
806 double temp = rs.getDouble(columnName);
807 return (rs.wasNull() ? null : new Double(temp));
808 }
809
810 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
811 {
812 double temp = rs.getDouble(columnIndex);
813 return (rs.wasNull() ? null : new Double(temp));
814 }
815
816 public int getType()
817 {
818 return Types.FLOAT;
819 }
820 }
821
822 public static final class T_Double extends BaseType
823 {
824 public Object sequenceKeyConversion(Long identifier)
825 {
826 return new Double(identifier.doubleValue());
827 }
828
829 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
830 // {
831 // double temp = stmt.getDouble(columnName);
832 // return (stmt.wasNull() ? null : new Double(temp));
833 // }
834
835 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
836 {
837 double temp = stmt.getDouble(columnIndex);
838 return (stmt.wasNull() ? null : new Double(temp));
839 }
840
841 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
842 {
843 double temp = rs.getDouble(columnName);
844 return (rs.wasNull() ? null : new Double(temp));
845 }
846
847 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
848 {
849 double temp = rs.getDouble(columnIndex);
850 return (rs.wasNull() ? null : new Double(temp));
851 }
852
853 public int getType()
854 {
855 return Types.DOUBLE;
856 }
857 }
858
859 public static final class T_Binary extends BaseType
860 {
861 public Object sequenceKeyConversion(Long identifier)
862 {
863 return identifier.toString().getBytes();
864 }
865
866 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
867 // {
868 // return stmt.getBytes(columnName);
869 // }
870
871 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
872 {
873 return stmt.getBytes(columnIndex);
874 }
875
876 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
877 {
878 return rs.getBytes(columnName);
879 }
880
881 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
882 {
883 return rs.getBytes(columnIndex);
884 }
885
886 public int getType()
887 {
888 return Types.BINARY;
889 }
890 }
891
892 public static final class T_VarBinary extends BaseType
893 {
894 public Object sequenceKeyConversion(Long identifier)
895 {
896 return identifier.toString().getBytes();
897 }
898
899 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
900 // {
901 // return stmt.getBytes(columnName);
902 // }
903
904 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
905 {
906 return stmt.getBytes(columnIndex);
907 }
908
909 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
910 {
911 return rs.getBytes(columnName);
912 }
913
914 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
915 {
916 return rs.getBytes(columnIndex);
917 }
918
919 public int getType()
920 {
921 return Types.VARBINARY;
922 }
923 }
924
925 public static final class T_LongVarBinary extends BaseType
926 {
927 protected static final int BUFSZ = 2048;
928
929 /**
930 * Retrieve LONGVARBINARY InputStream data and pack into a byte array.
931 *
932 * @param is the input stream to be retrieved
933 * @return a string containing the clob data
934 * @throws java.sql.SQLException if conversion fails or the clob cannot be read
935 */
936 protected static byte[] retrieveStreamDataFromRs(InputStream is) throws SQLException
937 {
938 if (is == null)
939 {
940 return null;
941 }
942 byte[] bytes = null;
943 ByteArrayOutputStream bos = null;
944 try
945 {
946 bos = new ByteArrayOutputStream();
947 int numRead;
948 byte[] buf = new byte[BUFSZ];
949 while ((numRead = is.read(buf, 0, buf.length)) > 0)
950 {
951 bos.write(buf, 0, numRead);
952 }
953 bytes = bos.toByteArray();
954 }
955 catch (IOException e)
956 {
957 throw new SQLException("I/O exception retrieving LONGVARBINARY: " + e.getLocalizedMessage());
958 }
959 finally
960 {
961 if (bos != null)
962 {
963 try
964 {
965 bos.close();
966 }
967 catch (Exception ignored)
968 {
969 //ignore
970 }
971 }
972 }
973 return bytes;
974 }
975
976 public Object sequenceKeyConversion(Long identifier)
977 {
978 return identifier.toString().getBytes();
979 }
980
981 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
982 // {
983 // return stmt.getBytes(columnName);
984 // }
985
986 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
987 {
988 return stmt.getBytes(columnIndex);
989 }
990
991 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
992 {
993 return retrieveStreamDataFromRs(rs.getBinaryStream(columnName));
994 }
995
996 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
997 {
998 return retrieveStreamDataFromRs(rs.getBinaryStream(columnIndex));
999 }
1000
1001 public int getType()
1002 {
1003 return Types.LONGVARBINARY;
1004 }
1005 }
1006
1007 public static final class T_Date extends BaseType
1008 {
1009 public Object sequenceKeyConversion(Long identifier)
1010 {
1011 return new Date(identifier.longValue());
1012 }
1013
1014 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1015 // {
1016 // return stmt.getDate(columnName);
1017 // }
1018
1019 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1020 {
1021 return stmt.getDate(columnIndex);
1022 }
1023
1024 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1025 {
1026 return rs.getDate(columnName);
1027 }
1028
1029 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1030 {
1031 return rs.getDate(columnIndex);
1032 }
1033
1034 public int getType()
1035 {
1036 return Types.DATE;
1037 }
1038 }
1039
1040 public static final class T_Time extends BaseType
1041 {
1042 public Object sequenceKeyConversion(Long identifier)
1043 {
1044 return new Time(identifier.longValue());
1045 }
1046
1047 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1048 // {
1049 // return stmt.getTime(columnName);
1050 // }
1051
1052 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1053 {
1054 return stmt.getTime(columnIndex);
1055 }
1056
1057 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1058 {
1059 return rs.getTime(columnName);
1060 }
1061
1062 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1063 {
1064 return rs.getTime(columnIndex);
1065 }
1066
1067 public int getType()
1068 {
1069 return Types.TIME;
1070 }
1071 }
1072
1073 public static final class T_Timestamp extends BaseType
1074 {
1075 public Object sequenceKeyConversion(Long identifier)
1076 {
1077 return new Timestamp(identifier.longValue());
1078 }
1079
1080 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1081 // {
1082 // return stmt.getTimestamp(columnName);
1083 // }
1084
1085 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1086 {
1087 return stmt.getTimestamp(columnIndex);
1088 }
1089
1090 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1091 {
1092 return rs.getTimestamp(columnName);
1093 }
1094
1095 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1096 {
1097 return rs.getTimestamp(columnIndex);
1098 }
1099
1100 public int getType()
1101 {
1102 return Types.TIMESTAMP;
1103 }
1104 }
1105
1106 public static final class T_Clob extends BaseType
1107 {
1108 protected static final int BUFSZ = 32768;
1109
1110 /**
1111 * Convert CLOB to String. Safe for very large objects.
1112 *
1113 * @param aClob clob with character data
1114 * @return a string containing the clob data
1115 * @throws java.sql.SQLException if conversion fails or the clob cannot be read
1116 */
1117 protected static String safeClobToString(Clob aClob) throws SQLException
1118 {
1119 long length = aClob.length();
1120 if (length == 0)
1121 {
1122 return "";
1123 }
1124 StringBuffer sb = new StringBuffer();
1125 char[] buf = new char[BUFSZ];
1126 java.io.Reader stream = aClob.getCharacterStream();
1127 try
1128 {
1129 int numRead;
1130 while ((numRead = stream.read(buf)) != -1)
1131 {
1132 sb.append(buf, 0, numRead);
1133 }
1134 stream.close();
1135 }
1136 catch (IOException e)
1137 {
1138 throw new SQLException(e.getLocalizedMessage());
1139 }
1140 return sb.toString();
1141 }
1142
1143 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
1144 {
1145 throw new SequenceManagerException("Not supported sequence key type 'CLOB'");
1146 }
1147
1148 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1149 // {
1150 // Clob aClob = stmt.getClob(columnName);
1151 // return (stmt.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()));
1152 // }
1153
1154 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1155 {
1156 Clob aClob = stmt.getClob(columnIndex);
1157 return (stmt.wasNull() ? null : safeClobToString(aClob));
1158 }
1159
1160 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1161 {
1162 Clob aClob = rs.getClob(columnName);
1163 return (rs.wasNull() ? null : safeClobToString(aClob));
1164 }
1165
1166 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1167 {
1168 Clob aClob = rs.getClob(columnIndex);
1169 return (rs.wasNull() ? null : safeClobToString(aClob));
1170 }
1171
1172 public int getType()
1173 {
1174 return Types.CLOB;
1175 }
1176 }
1177
1178 public static final class T_Blob extends BaseType
1179 {
1180 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
1181 {
1182 throw new SequenceManagerException("Not supported sequence key type 'BLOB'");
1183 }
1184
1185 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1186 // {
1187 // Blob aBlob = stmt.getBlob(columnName);
1188 // return (stmt.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
1189 // }
1190
1191 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1192 {
1193 Blob aBlob = stmt.getBlob(columnIndex);
1194 return (stmt.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
1195 }
1196
1197 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1198 {
1199 Blob aBlob = rs.getBlob(columnName);
1200 return (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
1201 }
1202
1203 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1204 {
1205 Blob aBlob = rs.getBlob(columnIndex);
1206 return (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
1207 }
1208
1209 public int getType()
1210 {
1211 return Types.BLOB;
1212 }
1213 }
1214
1215 public static final class T_Array extends BaseType
1216 {
1217 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
1218 {
1219 throw new SequenceManagerException("Not supported sequence key type 'ARRAY'");
1220 }
1221
1222 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1223 // {
1224 // return stmt.getArray(columnName);
1225 // }
1226
1227 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1228 {
1229 return stmt.getArray(columnIndex);
1230 }
1231
1232 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1233 {
1234 return rs.getArray(columnName);
1235 }
1236
1237 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1238 {
1239 return rs.getArray(columnIndex);
1240 }
1241
1242 public int getType()
1243 {
1244 return Types.ARRAY;
1245 }
1246 }
1247
1248 public static final class T_Struct extends BaseType
1249 {
1250 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
1251 {
1252 throw new SequenceManagerException("Not supported sequence key type 'STRUCT'");
1253 }
1254
1255 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1256 // {
1257 // return stmt.getObject(columnName);
1258 // }
1259
1260 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1261 {
1262 return stmt.getObject(columnIndex);
1263 }
1264
1265 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1266 {
1267 return rs.getObject(columnName);
1268 }
1269
1270 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1271 {
1272 return rs.getObject(columnIndex);
1273 }
1274
1275 public int getType()
1276 {
1277 return Types.STRUCT;
1278 }
1279 }
1280
1281 public static final class T_Ref extends BaseType
1282 {
1283 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
1284 {
1285 throw new SequenceManagerException("Not supported sequence key type 'REF'");
1286 }
1287
1288 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1289 // {
1290 // return stmt.getRef(columnName);
1291 // }
1292
1293 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1294 {
1295 return stmt.getRef(columnIndex);
1296 }
1297
1298 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1299 {
1300 return rs.getRef(columnName);
1301 }
1302
1303 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1304 {
1305 return rs.getRef(columnIndex);
1306 }
1307
1308 public int getType()
1309 {
1310 return Types.REF;
1311 }
1312 }
1313
1314 //#ifdef JDBC30
1315 public static final class T_Datalink extends BaseType
1316 {
1317 public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
1318 {
1319 throw new SequenceManagerException("Not supported sequence key type 'DATALINK'");
1320 }
1321
1322 // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1323 // {
1324 // return stmt.getURL(columnName);
1325 // }
1326
1327 Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
1328 {
1329 return stmt.getURL(columnIndex);
1330 }
1331
1332 Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
1333 {
1334 return rs.getURL(columnName);
1335 }
1336
1337 Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
1338 {
1339 return rs.getURL(columnIndex);
1340 }
1341
1342 public int getType()
1343 {
1344 return Types.DATALINK;
1345 }
1346 }
1347 //#endif
1348 }