0001: package org.apache.ojb.broker.metadata;
0002:
0003: /* Copyright 2003-2005 The Apache Software Foundation
0004: *
0005: * Licensed under the Apache License, Version 2.0 (the "License");
0006: * you may not use this file except in compliance with the License.
0007: * You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: import java.io.ByteArrayOutputStream;
0019: import java.io.IOException;
0020: import java.io.InputStream;
0021: import java.lang.reflect.Field;
0022: import java.math.BigDecimal;
0023: import java.net.URL;
0024: import java.sql.Blob;
0025: import java.sql.CallableStatement;
0026: import java.sql.Clob;
0027: import java.sql.Date;
0028: import java.sql.Ref;
0029: import java.sql.ResultSet;
0030: import java.sql.SQLException;
0031: import java.sql.Struct;
0032: import java.sql.Time;
0033: import java.sql.Timestamp;
0034: import java.sql.Types;
0035: import java.util.HashMap;
0036: import java.util.Map;
0037:
0038: import org.apache.commons.lang.BooleanUtils;
0039: import org.apache.commons.lang.builder.ToStringBuilder;
0040: import org.apache.ojb.broker.OJBRuntimeException;
0041: import org.apache.ojb.broker.util.sequence.SequenceManagerException;
0042:
0043: /**
0044: * Helper class which provide all supported {@link JdbcType} classes
0045: * (based on the {@link java.sql.Types}) as inner classes.
0046: *
0047: * @see JdbcType
0048: * @version $Id: JdbcTypesHelper.java,v 1.1.2.3 2005/12/21 22:26:10 tomdz Exp $
0049: */
0050: public class JdbcTypesHelper {
0051: private static Map jdbcObjectTypesFromType = new HashMap();
0052: private static Map jdbcObjectTypesFromName = new HashMap();
0053:
0054: /**
0055: * Hold out all JdbcType in a static maps
0056: */
0057: static {
0058: setJdbcType("array", Types.ARRAY, new T_Array());
0059: setJdbcType("bigint", Types.BIGINT, new T_BigInt());
0060: setJdbcType("binary", Types.BINARY, new T_Binary());
0061: setJdbcType("bit", Types.BIT, new T_Bit());
0062: setJdbcType("blob", Types.BLOB, new T_Blob());
0063: setJdbcType("char", Types.CHAR, new T_Char());
0064: setJdbcType("clob", Types.CLOB, new T_Clob());
0065: setJdbcType("date", Types.DATE, new T_Date());
0066: setJdbcType("decimal", Types.DECIMAL, new T_Decimal());
0067: setJdbcType("double", Types.DOUBLE, new T_Double());
0068: setJdbcType("float", Types.FLOAT, new T_Float());
0069: setJdbcType("integer", Types.INTEGER, new T_Integer());
0070: setJdbcType("longvarbinary", Types.LONGVARBINARY,
0071: new T_LongVarBinary());
0072: setJdbcType("longvarchar", Types.LONGVARCHAR,
0073: new T_LongVarChar());
0074: setJdbcType("numeric", Types.NUMERIC, new T_Numeric());
0075: setJdbcType("real", Types.REAL, new T_Real());
0076: setJdbcType("ref", Types.REF, new T_Ref());
0077: setJdbcType("smallint", Types.SMALLINT, new T_SmallInt());
0078: setJdbcType("struct", Types.STRUCT, new T_Struct());
0079: setJdbcType("time", Types.TIME, new T_Time());
0080: setJdbcType("timestamp", Types.TIMESTAMP, new T_Timestamp());
0081: setJdbcType("tinyint", Types.TINYINT, new T_TinyInt());
0082: setJdbcType("varbinary", Types.VARBINARY, new T_VarBinary());
0083: setJdbcType("varchar", Types.VARCHAR, new T_Varchar());
0084:
0085: //#ifdef JDBC30
0086: setJdbcType("boolean", Types.BOOLEAN, new T_Boolean());
0087: setJdbcType("datalink", Types.DATALINK, new T_Datalink());
0088: //#endif
0089:
0090: }
0091:
0092: public JdbcTypesHelper() {
0093: // default constructor
0094: }
0095:
0096: /**
0097: * Set the {@link JdbcType} by name and index.
0098: * @param typeName Name of the type
0099: * @param typeIndex index of the type
0100: * @param type the type
0101: */
0102: public static void setJdbcType(String typeName, int typeIndex,
0103: JdbcType type) {
0104: setJdbcTypeByName(typeName, type);
0105: setJdbcTypeByTypesIndex(typeIndex, type);
0106: }
0107:
0108: /**
0109: * Return the {@link JdbcType} for the given jdbc {@link java.sql.Types type}.
0110: */
0111: public static JdbcType getJdbcTypeByTypesIndex(Integer type) {
0112: return (JdbcType) jdbcObjectTypesFromType.get(type);
0113: }
0114:
0115: /**
0116: * Set the {@link JdbcType} by index.
0117: * @param typeIndex index of the type
0118: * @param type the type
0119: */
0120: public static void setJdbcTypeByTypesIndex(int typeIndex,
0121: JdbcType type) {
0122: jdbcObjectTypesFromType.put(new Integer(typeIndex), type);
0123: }
0124:
0125: /**
0126: * Lookup the {@link JdbcType} by name. If name was not found an exception
0127: * is thrown.
0128: */
0129: public static JdbcType getJdbcTypeByName(String typeName) {
0130: JdbcType result = null;
0131: result = (JdbcType) jdbcObjectTypesFromName.get(typeName
0132: .toLowerCase());
0133: if (result == null) {
0134: throw new OJBRuntimeException(
0135: "The type "
0136: + typeName
0137: + " can not be handled by OJB."
0138: + " Please specify only types as defined by java.sql.Types.");
0139: }
0140: return result;
0141: }
0142:
0143: /**
0144: * Set the {@link JdbcType} by name.
0145: * @param typeName Name of the type
0146: * @param type the type
0147: */
0148: public static void setJdbcTypeByName(String typeName, JdbcType type) {
0149: jdbcObjectTypesFromName.put(typeName, type);
0150: }
0151:
0152: /**
0153: * Try to automatically assign a jdbc type for the given
0154: * java type name. This method is used if e.g. in metadata a
0155: * column type was not set.
0156: *
0157: * @see FieldDescriptor#getJdbcType
0158: */
0159: public static JdbcType getJdbcTypeByReflection(String fieldType) {
0160: JdbcType result;
0161:
0162: if (fieldType.equalsIgnoreCase(Character.class.getName())
0163: || fieldType.equalsIgnoreCase("char"))
0164: result = getJdbcTypeByName("char");
0165: else if (fieldType.equalsIgnoreCase(Short.class.getName())
0166: || fieldType.equalsIgnoreCase("short"))
0167: result = getJdbcTypeByName("smallint");
0168: else if (fieldType.equalsIgnoreCase(Integer.class.getName())
0169: || fieldType.equalsIgnoreCase("int"))
0170: result = getJdbcTypeByName("integer");
0171: else if (fieldType.equalsIgnoreCase(Long.class.getName())
0172: || fieldType.equalsIgnoreCase("long"))
0173: result = getJdbcTypeByName("bigint");
0174: else if (fieldType.equalsIgnoreCase(Byte.class.getName())
0175: || fieldType.equalsIgnoreCase("byte"))
0176: result = getJdbcTypeByName("tinyint");
0177: else if (fieldType.equalsIgnoreCase(Float.class.getName())
0178: || fieldType.equalsIgnoreCase("float"))
0179: result = getJdbcTypeByName("real");
0180: else if (fieldType.equalsIgnoreCase(Double.class.getName())
0181: || fieldType.equalsIgnoreCase("double"))
0182: result = getJdbcTypeByName("float");
0183: else if (fieldType.equalsIgnoreCase(String.class.getName()))
0184: result = getJdbcTypeByName("varchar");
0185: /*
0186: TODO: arminw: useful? This only will work in conjunction with a FieldConversion
0187: */
0188: else if (fieldType.equalsIgnoreCase(java.util.Date.class
0189: .getName()))
0190: result = getJdbcTypeByName("date");
0191: else if (fieldType.equalsIgnoreCase(Date.class.getName()))
0192: result = getJdbcTypeByName("date");
0193: else if (fieldType.equalsIgnoreCase(Time.class.getName()))
0194: result = getJdbcTypeByName("time");
0195: else if (fieldType.equalsIgnoreCase(Timestamp.class.getName()))
0196: result = getJdbcTypeByName("timestamp");
0197: else if (fieldType.equalsIgnoreCase(BigDecimal.class.getName()))
0198: result = getJdbcTypeByName("decimal");
0199: else if (fieldType.equalsIgnoreCase(Ref.class.getName()))
0200: result = getJdbcTypeByName("ref");
0201: else if (fieldType.equalsIgnoreCase(Struct.class.getName()))
0202: result = getJdbcTypeByName("struct");
0203: else if (fieldType.equalsIgnoreCase(Boolean.class.getName())
0204: || fieldType.equalsIgnoreCase("boolean"))
0205: result = getJdbcTypeByName("bit");
0206: //#ifdef JDBC30
0207: else if (fieldType.equalsIgnoreCase(URL.class.getName()))
0208: result = getJdbcTypeByName("datalink");
0209: //#endif
0210: else
0211: throw new OJBRuntimeException(
0212: "The type "
0213: + fieldType
0214: + " can not be handled by OJB automatically."
0215: + " Please specify a type as defined by java.sql.Types in your field-descriptor");
0216: return result;
0217: }
0218:
0219: /**
0220: * Returns an java object read from the specified ResultSet column.
0221: */
0222: public static Object getObjectFromColumn(ResultSet rs,
0223: Integer jdbcType, int columnId) throws SQLException {
0224: return getObjectFromColumn(rs, null, jdbcType, null, columnId);
0225: }
0226:
0227: /**
0228: * Returns an java object for the given jdbcType by extract from the given
0229: * CallableStatement or ResultSet.
0230: * NOTE: Exactly one of the arguments of type CallableStatement or ResultSet
0231: * have to be non-null.
0232: * If the 'columnId' argument is equals {@link JdbcType#MIN_INT}, then the given 'columnName'
0233: * argument is used to lookup column. Else the given 'columnId' is used as column index.
0234: */
0235: private static Object getObjectFromColumn(ResultSet rs,
0236: CallableStatement stmt, Integer jdbcType,
0237: String columnName, int columnId) throws SQLException {
0238: return getJdbcTypeByTypesIndex(jdbcType).getObjectFromColumn(
0239: rs, stmt, columnName, columnId);
0240: }
0241:
0242: /**
0243: * Returns a string representation of the given {@link java.sql.Types} value.
0244: */
0245: public static String getSqlTypeAsString(int jdbcType) {
0246: String statusName = "*can't find String representation for sql type '"
0247: + jdbcType + "'*";
0248: try {
0249: Field[] fields = Types.class.getDeclaredFields();
0250: for (int i = 0; i < fields.length; i++) {
0251: if (fields[i].getInt(null) == jdbcType) {
0252: statusName = fields[i].getName();
0253: break;
0254: }
0255: }
0256: } catch (Exception ignore) {
0257: // ignore it
0258: }
0259: return statusName;
0260: }
0261:
0262: //======================================================================================
0263: // inner classes implementing JdbcType interface
0264: //======================================================================================
0265:
0266: public abstract static class BaseType implements JdbcType {
0267: private FieldType fieldType;
0268:
0269: protected BaseType() {
0270: fieldType = FieldTypeClasses.newFieldType(this );
0271: }
0272:
0273: abstract Object readValueFromResultSet(ResultSet rs,
0274: String columnName) throws SQLException;
0275:
0276: abstract Object readValueFromResultSet(ResultSet rs,
0277: int columnIndex) throws SQLException;
0278:
0279: abstract Object readValueFromStatement(CallableStatement stmt,
0280: int columnIndex) throws SQLException;
0281:
0282: /*
0283: only supported by jdk >= 1.4x, maybe useful in further versions
0284: */
0285: // abstract Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException;
0286: public boolean equals(Object obj) {
0287: if (this == obj)
0288: return true;
0289: boolean result = false;
0290: if (obj instanceof JdbcType) {
0291: result = this .getType() == ((JdbcType) obj).getType();
0292: }
0293: return result;
0294: }
0295:
0296: public int hashCode() {
0297: return getType();
0298: }
0299:
0300: public FieldType getFieldType() {
0301: return fieldType;
0302: }
0303:
0304: public Object getObjectFromColumn(CallableStatement stmt,
0305: int columnId) throws SQLException {
0306: return getObjectFromColumn(null, stmt, null, columnId);
0307: }
0308:
0309: public Object getObjectFromColumn(ResultSet rs,
0310: String columnName) throws SQLException {
0311: return getObjectFromColumn(rs, null, columnName, MIN_INT);
0312: }
0313:
0314: public Object getObjectFromColumn(final ResultSet rs,
0315: final CallableStatement stmt, final String columnName,
0316: int columnIndex) throws SQLException {
0317: if (stmt != null) {
0318: // return columnIndex == MIN_INT
0319: // ? readValueFromStatement(stmt, columnName) : readValueFromStatement(stmt, columnIndex);
0320: if (columnIndex == MIN_INT) {
0321: throw new UnsupportedOperationException(
0322: "Not implemented yet");
0323: } else {
0324: return readValueFromStatement(stmt, columnIndex);
0325: }
0326: } else {
0327: return columnIndex == MIN_INT ? readValueFromResultSet(
0328: rs, columnName) : readValueFromResultSet(rs,
0329: columnIndex);
0330: }
0331: }
0332:
0333: public String toString() {
0334: return new ToStringBuilder(this ).append("jdbcType",
0335: getType()).append("jdbcTypeString",
0336: getSqlTypeAsString(getType())).append(
0337: "associatedFieldType", getFieldType()).toString();
0338: }
0339:
0340: // // not used in code, but maybe useful in further versions
0341: // public Object getObjectFromColumn(CallableStatement stmt, String columnName) throws SQLException
0342: // {
0343: // return getObjectFromColumn(null, stmt, columnName, MIN_INT);
0344: // }
0345: //
0346: // public Object getObjectFromColumn(ResultSet rs, int columnId) throws SQLException
0347: // {
0348: // return getObjectFromColumn(rs, null, null, columnId);
0349: // }
0350:
0351: }
0352:
0353: public static final class T_Char extends BaseType {
0354: public Object sequenceKeyConversion(Long identifier) {
0355: return identifier.toString();
0356: }
0357:
0358: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0359: // {
0360: // return stmt.getString(columnName);
0361: // }
0362:
0363: Object readValueFromStatement(CallableStatement stmt,
0364: int columnIndex) throws SQLException {
0365: return stmt.getString(columnIndex);
0366: }
0367:
0368: Object readValueFromResultSet(ResultSet rs, String columnName)
0369: throws SQLException {
0370: return rs.getString(columnName);
0371: }
0372:
0373: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0374: throws SQLException {
0375: return rs.getString(columnIndex);
0376: }
0377:
0378: public int getType() {
0379: return Types.CHAR;
0380: }
0381: }
0382:
0383: public static final class T_Varchar extends BaseType {
0384: public Object sequenceKeyConversion(Long identifier) {
0385: return identifier.toString();
0386: }
0387:
0388: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0389: // {
0390: // return stmt.getString(columnName);
0391: // }
0392:
0393: Object readValueFromStatement(CallableStatement stmt,
0394: int columnIndex) throws SQLException {
0395: return stmt.getString(columnIndex);
0396: }
0397:
0398: Object readValueFromResultSet(ResultSet rs, String columnName)
0399: throws SQLException {
0400: return rs.getString(columnName);
0401: }
0402:
0403: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0404: throws SQLException {
0405: return rs.getString(columnIndex);
0406: }
0407:
0408: public int getType() {
0409: return Types.VARCHAR;
0410: }
0411: }
0412:
0413: public static final class T_LongVarChar extends BaseType {
0414: public Object sequenceKeyConversion(Long identifier) {
0415: return identifier.toString();
0416: }
0417:
0418: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0419: // {
0420: // return stmt.getString(columnName);
0421: // }
0422:
0423: Object readValueFromStatement(CallableStatement stmt,
0424: int columnIndex) throws SQLException {
0425: return stmt.getString(columnIndex);
0426: }
0427:
0428: Object readValueFromResultSet(ResultSet rs, String columnName)
0429: throws SQLException {
0430: return rs.getString(columnName);
0431: }
0432:
0433: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0434: throws SQLException {
0435: return rs.getString(columnIndex);
0436: }
0437:
0438: public int getType() {
0439: return Types.LONGVARCHAR;
0440: }
0441: }
0442:
0443: public static final class T_Numeric extends BaseType {
0444: public Object sequenceKeyConversion(Long identifier) {
0445: return new BigDecimal(identifier.longValue());
0446: }
0447:
0448: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0449: // {
0450: // return stmt.getBigDecimal(columnName);
0451: // }
0452:
0453: Object readValueFromStatement(CallableStatement stmt,
0454: int columnIndex) throws SQLException {
0455: return stmt.getBigDecimal(columnIndex);
0456: }
0457:
0458: Object readValueFromResultSet(ResultSet rs, String columnName)
0459: throws SQLException {
0460: return rs.getBigDecimal(columnName);
0461: }
0462:
0463: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0464: throws SQLException {
0465: return rs.getBigDecimal(columnIndex);
0466: }
0467:
0468: public int getType() {
0469: return Types.NUMERIC;
0470: }
0471: }
0472:
0473: public static final class T_Decimal extends BaseType {
0474: public Object sequenceKeyConversion(Long identifier) {
0475: return new BigDecimal(identifier.longValue());
0476: }
0477:
0478: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0479: // {
0480: // return stmt.getBigDecimal(columnName);
0481: // }
0482:
0483: Object readValueFromStatement(CallableStatement stmt,
0484: int columnIndex) throws SQLException {
0485: return stmt.getBigDecimal(columnIndex);
0486: }
0487:
0488: Object readValueFromResultSet(ResultSet rs, String columnName)
0489: throws SQLException {
0490: return rs.getBigDecimal(columnName);
0491: }
0492:
0493: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0494: throws SQLException {
0495: return rs.getBigDecimal(columnIndex);
0496: }
0497:
0498: public int getType() {
0499: return Types.DECIMAL;
0500: }
0501: }
0502:
0503: public static final class T_Bit extends BaseType {
0504: public Object sequenceKeyConversion(Long identifier)
0505: throws SequenceManagerException {
0506: throw new SequenceManagerException(
0507: "Not supported sequence key type 'BIT'");
0508: }
0509:
0510: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0511: // {
0512: // boolean temp = stmt.getBoolean(columnName);
0513: // return (stmt.wasNull() ? null : new Boolean(temp));
0514: // }
0515:
0516: Object readValueFromStatement(CallableStatement stmt,
0517: int columnIndex) throws SQLException {
0518: boolean temp = stmt.getBoolean(columnIndex);
0519: return (stmt.wasNull() ? null : BooleanUtils
0520: .toBooleanObject(temp));
0521: }
0522:
0523: Object readValueFromResultSet(ResultSet rs, String columnName)
0524: throws SQLException {
0525: boolean temp = rs.getBoolean(columnName);
0526: return (rs.wasNull() ? null : BooleanUtils
0527: .toBooleanObject(temp));
0528: }
0529:
0530: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0531: throws SQLException {
0532: boolean temp = rs.getBoolean(columnIndex);
0533: return (rs.wasNull() ? null : BooleanUtils
0534: .toBooleanObject(temp));
0535: }
0536:
0537: public int getType() {
0538: return Types.BIT;
0539: }
0540: }
0541:
0542: //#ifdef JDBC30
0543: public static final class T_Boolean extends BaseType {
0544: public Object sequenceKeyConversion(final Long identifier)
0545: throws SequenceManagerException {
0546: throw new SequenceManagerException(
0547: "Not supported sequence key type 'BOOLEAN'");
0548: }
0549:
0550: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0551: // {
0552: // boolean temp = stmt.getBoolean(columnName);
0553: // return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
0554: // }
0555:
0556: Object readValueFromStatement(CallableStatement stmt,
0557: int columnIndex) throws SQLException {
0558: boolean temp = stmt.getBoolean(columnIndex);
0559: return (stmt.wasNull() ? null : BooleanUtils
0560: .toBooleanObject(temp));
0561: }
0562:
0563: Object readValueFromResultSet(ResultSet rs, String columnName)
0564: throws SQLException {
0565: boolean temp = rs.getBoolean(columnName);
0566: return (rs.wasNull() ? null : BooleanUtils
0567: .toBooleanObject(temp));
0568: }
0569:
0570: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0571: throws SQLException {
0572: boolean temp = rs.getBoolean(columnIndex);
0573: return (rs.wasNull() ? null : BooleanUtils
0574: .toBooleanObject(temp));
0575: }
0576:
0577: public int getType() {
0578: return Types.BOOLEAN;
0579: }
0580: }
0581:
0582: //#endif
0583:
0584: public static final class T_TinyInt extends BaseType {
0585: public Object sequenceKeyConversion(final Long identifier) {
0586: return new Byte(identifier.byteValue());
0587: }
0588:
0589: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0590: // {
0591: // byte temp = stmt.getByte(columnName);
0592: // return (stmt.wasNull() ? null : new Byte(temp));
0593: // }
0594:
0595: Object readValueFromStatement(CallableStatement stmt,
0596: int columnIndex) throws SQLException {
0597: byte temp = stmt.getByte(columnIndex);
0598: return (stmt.wasNull() ? null : new Byte(temp));
0599: }
0600:
0601: Object readValueFromResultSet(ResultSet rs, String columnName)
0602: throws SQLException {
0603: byte temp = rs.getByte(columnName);
0604: return (rs.wasNull() ? null : new Byte(temp));
0605: }
0606:
0607: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0608: throws SQLException {
0609: byte temp = rs.getByte(columnIndex);
0610: return (rs.wasNull() ? null : new Byte(temp));
0611: }
0612:
0613: public int getType() {
0614: return Types.TINYINT;
0615: }
0616: }
0617:
0618: public static final class T_SmallInt extends BaseType {
0619: public Object sequenceKeyConversion(Long identifier) {
0620: return new Short(identifier.shortValue());
0621: }
0622:
0623: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0624: // {
0625: // short temp = stmt.getShort(columnName);
0626: // return (stmt.wasNull() ? null : new Short(temp));
0627: // }
0628:
0629: Object readValueFromStatement(CallableStatement stmt,
0630: int columnIndex) throws SQLException {
0631: short temp = stmt.getShort(columnIndex);
0632: return (stmt.wasNull() ? null : new Short(temp));
0633: }
0634:
0635: Object readValueFromResultSet(ResultSet rs, String columnName)
0636: throws SQLException {
0637: short temp = rs.getShort(columnName);
0638: return (rs.wasNull() ? null : new Short(temp));
0639: }
0640:
0641: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0642: throws SQLException {
0643: short temp = rs.getShort(columnIndex);
0644: return (rs.wasNull() ? null : new Short(temp));
0645: }
0646:
0647: public int getType() {
0648: return Types.SMALLINT;
0649: }
0650: }
0651:
0652: public static final class T_Integer extends BaseType {
0653: public Object sequenceKeyConversion(Long identifier) {
0654: return new Integer(identifier.intValue());
0655: }
0656:
0657: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0658: // {
0659: // int temp = stmt.getInt(columnName);
0660: // return (stmt.wasNull() ? null : new Integer(temp));
0661: // }
0662:
0663: Object readValueFromStatement(CallableStatement stmt,
0664: int columnIndex) throws SQLException {
0665: int temp = stmt.getInt(columnIndex);
0666: return (stmt.wasNull() ? null : new Integer(temp));
0667: }
0668:
0669: Object readValueFromResultSet(ResultSet rs, String columnName)
0670: throws SQLException {
0671: int temp = rs.getInt(columnName);
0672: return (rs.wasNull() ? null : new Integer(temp));
0673: }
0674:
0675: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0676: throws SQLException {
0677: int temp = rs.getInt(columnIndex);
0678: return (rs.wasNull() ? null : new Integer(temp));
0679: }
0680:
0681: public int getType() {
0682: return Types.INTEGER;
0683: }
0684: }
0685:
0686: public static final class T_BigInt extends BaseType {
0687: public Object sequenceKeyConversion(Long identifier) {
0688: return identifier;
0689: }
0690:
0691: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0692: // {
0693: // long temp = stmt.getLong(columnName);
0694: // return (stmt.wasNull() ? null : new Long(temp));
0695: // }
0696:
0697: Object readValueFromStatement(CallableStatement stmt,
0698: int columnIndex) throws SQLException {
0699: long temp = stmt.getLong(columnIndex);
0700: return (stmt.wasNull() ? null : new Long(temp));
0701: }
0702:
0703: Object readValueFromResultSet(ResultSet rs, String columnName)
0704: throws SQLException {
0705: long temp = rs.getLong(columnName);
0706: return (rs.wasNull() ? null : new Long(temp));
0707: }
0708:
0709: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0710: throws SQLException {
0711: long temp = rs.getLong(columnIndex);
0712: return (rs.wasNull() ? null : new Long(temp));
0713: }
0714:
0715: public int getType() {
0716: return Types.BIGINT;
0717: }
0718: }
0719:
0720: public static final class T_Real extends BaseType {
0721: public Object sequenceKeyConversion(Long identifier) {
0722: return new Float(identifier.floatValue());
0723: }
0724:
0725: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0726: // {
0727: // float temp = stmt.getFloat(columnName);
0728: // return (stmt.wasNull() ? null : new Float(temp));
0729: // }
0730:
0731: Object readValueFromStatement(CallableStatement stmt,
0732: int columnIndex) throws SQLException {
0733: float temp = stmt.getFloat(columnIndex);
0734: return (stmt.wasNull() ? null : new Float(temp));
0735: }
0736:
0737: Object readValueFromResultSet(ResultSet rs, String columnName)
0738: throws SQLException {
0739: float temp = rs.getFloat(columnName);
0740: return (rs.wasNull() ? null : new Float(temp));
0741: }
0742:
0743: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0744: throws SQLException {
0745: float temp = rs.getFloat(columnIndex);
0746: return (rs.wasNull() ? null : new Float(temp));
0747: }
0748:
0749: public int getType() {
0750: return Types.REAL;
0751: }
0752: }
0753:
0754: public static final class T_Float extends BaseType {
0755: public Object sequenceKeyConversion(Long identifier) {
0756: return new Double(identifier.doubleValue());
0757: }
0758:
0759: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0760: // {
0761: // double temp = stmt.getDouble(columnName);
0762: // return (stmt.wasNull() ? null : new Double(temp));
0763: // }
0764:
0765: Object readValueFromStatement(CallableStatement stmt,
0766: int columnIndex) throws SQLException {
0767: double temp = stmt.getDouble(columnIndex);
0768: return (stmt.wasNull() ? null : new Double(temp));
0769: }
0770:
0771: Object readValueFromResultSet(ResultSet rs, String columnName)
0772: throws SQLException {
0773: double temp = rs.getDouble(columnName);
0774: return (rs.wasNull() ? null : new Double(temp));
0775: }
0776:
0777: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0778: throws SQLException {
0779: double temp = rs.getDouble(columnIndex);
0780: return (rs.wasNull() ? null : new Double(temp));
0781: }
0782:
0783: public int getType() {
0784: return Types.FLOAT;
0785: }
0786: }
0787:
0788: public static final class T_Double extends BaseType {
0789: public Object sequenceKeyConversion(Long identifier) {
0790: return new Double(identifier.doubleValue());
0791: }
0792:
0793: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0794: // {
0795: // double temp = stmt.getDouble(columnName);
0796: // return (stmt.wasNull() ? null : new Double(temp));
0797: // }
0798:
0799: Object readValueFromStatement(CallableStatement stmt,
0800: int columnIndex) throws SQLException {
0801: double temp = stmt.getDouble(columnIndex);
0802: return (stmt.wasNull() ? null : new Double(temp));
0803: }
0804:
0805: Object readValueFromResultSet(ResultSet rs, String columnName)
0806: throws SQLException {
0807: double temp = rs.getDouble(columnName);
0808: return (rs.wasNull() ? null : new Double(temp));
0809: }
0810:
0811: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0812: throws SQLException {
0813: double temp = rs.getDouble(columnIndex);
0814: return (rs.wasNull() ? null : new Double(temp));
0815: }
0816:
0817: public int getType() {
0818: return Types.DOUBLE;
0819: }
0820: }
0821:
0822: public static final class T_Binary extends BaseType {
0823: public Object sequenceKeyConversion(Long identifier) {
0824: return identifier.toString().getBytes();
0825: }
0826:
0827: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0828: // {
0829: // return stmt.getBytes(columnName);
0830: // }
0831:
0832: Object readValueFromStatement(CallableStatement stmt,
0833: int columnIndex) throws SQLException {
0834: return stmt.getBytes(columnIndex);
0835: }
0836:
0837: Object readValueFromResultSet(ResultSet rs, String columnName)
0838: throws SQLException {
0839: return rs.getBytes(columnName);
0840: }
0841:
0842: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0843: throws SQLException {
0844: return rs.getBytes(columnIndex);
0845: }
0846:
0847: public int getType() {
0848: return Types.BINARY;
0849: }
0850: }
0851:
0852: public static final class T_VarBinary extends BaseType {
0853: public Object sequenceKeyConversion(Long identifier) {
0854: return identifier.toString().getBytes();
0855: }
0856:
0857: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0858: // {
0859: // return stmt.getBytes(columnName);
0860: // }
0861:
0862: Object readValueFromStatement(CallableStatement stmt,
0863: int columnIndex) throws SQLException {
0864: return stmt.getBytes(columnIndex);
0865: }
0866:
0867: Object readValueFromResultSet(ResultSet rs, String columnName)
0868: throws SQLException {
0869: return rs.getBytes(columnName);
0870: }
0871:
0872: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0873: throws SQLException {
0874: return rs.getBytes(columnIndex);
0875: }
0876:
0877: public int getType() {
0878: return Types.VARBINARY;
0879: }
0880: }
0881:
0882: public static final class T_LongVarBinary extends BaseType {
0883: protected static final int BUFSZ = 2048;
0884:
0885: /**
0886: * Retrieve LONGVARBINARY InputStream data and pack into a byte array.
0887: *
0888: * @param is the input stream to be retrieved
0889: * @return a string containing the clob data
0890: * @throws java.sql.SQLException if conversion fails or the clob cannot be read
0891: */
0892: protected static byte[] retrieveStreamDataFromRs(InputStream is)
0893: throws SQLException {
0894: if (is == null) {
0895: return null;
0896: }
0897: byte[] bytes = null;
0898: ByteArrayOutputStream bos = null;
0899: try {
0900: bos = new ByteArrayOutputStream();
0901: int numRead;
0902: byte[] buf = new byte[BUFSZ];
0903: while ((numRead = is.read(buf, 0, buf.length)) > 0) {
0904: bos.write(buf, 0, numRead);
0905: }
0906: bytes = bos.toByteArray();
0907: } catch (IOException e) {
0908: throw new SQLException(
0909: "I/O exception retrieving LONGVARBINARY: "
0910: + e.getLocalizedMessage());
0911: } finally {
0912: if (bos != null) {
0913: try {
0914: bos.close();
0915: } catch (Exception ignored) {
0916: //ignore
0917: }
0918: }
0919: }
0920: return bytes;
0921: }
0922:
0923: public Object sequenceKeyConversion(Long identifier) {
0924: return identifier.toString().getBytes();
0925: }
0926:
0927: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0928: // {
0929: // return stmt.getBytes(columnName);
0930: // }
0931:
0932: Object readValueFromStatement(CallableStatement stmt,
0933: int columnIndex) throws SQLException {
0934: return stmt.getBytes(columnIndex);
0935: }
0936:
0937: Object readValueFromResultSet(ResultSet rs, String columnName)
0938: throws SQLException {
0939: return retrieveStreamDataFromRs(rs
0940: .getBinaryStream(columnName));
0941: }
0942:
0943: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0944: throws SQLException {
0945: return retrieveStreamDataFromRs(rs
0946: .getBinaryStream(columnIndex));
0947: }
0948:
0949: public int getType() {
0950: return Types.LONGVARBINARY;
0951: }
0952: }
0953:
0954: public static final class T_Date extends BaseType {
0955: public Object sequenceKeyConversion(Long identifier) {
0956: return new Date(identifier.longValue());
0957: }
0958:
0959: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0960: // {
0961: // return stmt.getDate(columnName);
0962: // }
0963:
0964: Object readValueFromStatement(CallableStatement stmt,
0965: int columnIndex) throws SQLException {
0966: return stmt.getDate(columnIndex);
0967: }
0968:
0969: Object readValueFromResultSet(ResultSet rs, String columnName)
0970: throws SQLException {
0971: return rs.getDate(columnName);
0972: }
0973:
0974: Object readValueFromResultSet(ResultSet rs, int columnIndex)
0975: throws SQLException {
0976: return rs.getDate(columnIndex);
0977: }
0978:
0979: public int getType() {
0980: return Types.DATE;
0981: }
0982: }
0983:
0984: public static final class T_Time extends BaseType {
0985: public Object sequenceKeyConversion(Long identifier) {
0986: return new Time(identifier.longValue());
0987: }
0988:
0989: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
0990: // {
0991: // return stmt.getTime(columnName);
0992: // }
0993:
0994: Object readValueFromStatement(CallableStatement stmt,
0995: int columnIndex) throws SQLException {
0996: return stmt.getTime(columnIndex);
0997: }
0998:
0999: Object readValueFromResultSet(ResultSet rs, String columnName)
1000: throws SQLException {
1001: return rs.getTime(columnName);
1002: }
1003:
1004: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1005: throws SQLException {
1006: return rs.getTime(columnIndex);
1007: }
1008:
1009: public int getType() {
1010: return Types.TIME;
1011: }
1012: }
1013:
1014: public static final class T_Timestamp extends BaseType {
1015: public Object sequenceKeyConversion(Long identifier) {
1016: return new Timestamp(identifier.longValue());
1017: }
1018:
1019: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1020: // {
1021: // return stmt.getTimestamp(columnName);
1022: // }
1023:
1024: Object readValueFromStatement(CallableStatement stmt,
1025: int columnIndex) throws SQLException {
1026: return stmt.getTimestamp(columnIndex);
1027: }
1028:
1029: Object readValueFromResultSet(ResultSet rs, String columnName)
1030: throws SQLException {
1031: return rs.getTimestamp(columnName);
1032: }
1033:
1034: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1035: throws SQLException {
1036: return rs.getTimestamp(columnIndex);
1037: }
1038:
1039: public int getType() {
1040: return Types.TIMESTAMP;
1041: }
1042: }
1043:
1044: public static final class T_Clob extends BaseType {
1045: protected static final int BUFSZ = 32768;
1046:
1047: /**
1048: * Convert CLOB to String. Safe for very large objects.
1049: *
1050: * @param aClob clob with character data
1051: * @return a string containing the clob data
1052: * @throws java.sql.SQLException if conversion fails or the clob cannot be read
1053: */
1054: protected static String safeClobToString(Clob aClob)
1055: throws SQLException {
1056: long length = aClob.length();
1057: if (length == 0) {
1058: return "";
1059: }
1060: StringBuffer sb = new StringBuffer();
1061: char[] buf = new char[BUFSZ];
1062: java.io.Reader stream = aClob.getCharacterStream();
1063: try {
1064: int numRead;
1065: while ((numRead = stream.read(buf)) != -1) {
1066: sb.append(buf, 0, numRead);
1067: }
1068: stream.close();
1069: } catch (IOException e) {
1070: throw new SQLException(e.getLocalizedMessage());
1071: }
1072: return sb.toString();
1073: }
1074:
1075: public Object sequenceKeyConversion(Long identifier)
1076: throws SequenceManagerException {
1077: throw new SequenceManagerException(
1078: "Not supported sequence key type 'CLOB'");
1079: }
1080:
1081: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1082: // {
1083: // Clob aClob = stmt.getClob(columnName);
1084: // return (stmt.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()));
1085: // }
1086:
1087: Object readValueFromStatement(CallableStatement stmt,
1088: int columnIndex) throws SQLException {
1089: Clob aClob = stmt.getClob(columnIndex);
1090: return (stmt.wasNull() ? null : safeClobToString(aClob));
1091: }
1092:
1093: Object readValueFromResultSet(ResultSet rs, String columnName)
1094: throws SQLException {
1095: Clob aClob = rs.getClob(columnName);
1096: return (rs.wasNull() ? null : safeClobToString(aClob));
1097: }
1098:
1099: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1100: throws SQLException {
1101: Clob aClob = rs.getClob(columnIndex);
1102: return (rs.wasNull() ? null : safeClobToString(aClob));
1103: }
1104:
1105: public int getType() {
1106: return Types.CLOB;
1107: }
1108: }
1109:
1110: public static final class T_Blob extends BaseType {
1111: public Object sequenceKeyConversion(Long identifier)
1112: throws SequenceManagerException {
1113: throw new SequenceManagerException(
1114: "Not supported sequence key type 'BLOB'");
1115: }
1116:
1117: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1118: // {
1119: // Blob aBlob = stmt.getBlob(columnName);
1120: // return (stmt.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
1121: // }
1122:
1123: Object readValueFromStatement(CallableStatement stmt,
1124: int columnIndex) throws SQLException {
1125: Blob aBlob = stmt.getBlob(columnIndex);
1126: return (stmt.wasNull() ? null : aBlob.getBytes(1L,
1127: (int) aBlob.length()));
1128: }
1129:
1130: Object readValueFromResultSet(ResultSet rs, String columnName)
1131: throws SQLException {
1132: Blob aBlob = rs.getBlob(columnName);
1133: return (rs.wasNull() ? null : aBlob.getBytes(1L,
1134: (int) aBlob.length()));
1135: }
1136:
1137: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1138: throws SQLException {
1139: Blob aBlob = rs.getBlob(columnIndex);
1140: return (rs.wasNull() ? null : aBlob.getBytes(1L,
1141: (int) aBlob.length()));
1142: }
1143:
1144: public int getType() {
1145: return Types.BLOB;
1146: }
1147: }
1148:
1149: public static final class T_Array extends BaseType {
1150: public Object sequenceKeyConversion(Long identifier)
1151: throws SequenceManagerException {
1152: throw new SequenceManagerException(
1153: "Not supported sequence key type 'ARRAY'");
1154: }
1155:
1156: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1157: // {
1158: // return stmt.getArray(columnName);
1159: // }
1160:
1161: Object readValueFromStatement(CallableStatement stmt,
1162: int columnIndex) throws SQLException {
1163: return stmt.getArray(columnIndex);
1164: }
1165:
1166: Object readValueFromResultSet(ResultSet rs, String columnName)
1167: throws SQLException {
1168: return rs.getArray(columnName);
1169: }
1170:
1171: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1172: throws SQLException {
1173: return rs.getArray(columnIndex);
1174: }
1175:
1176: public int getType() {
1177: return Types.ARRAY;
1178: }
1179: }
1180:
1181: public static final class T_Struct extends BaseType {
1182: public Object sequenceKeyConversion(Long identifier)
1183: throws SequenceManagerException {
1184: throw new SequenceManagerException(
1185: "Not supported sequence key type 'STRUCT'");
1186: }
1187:
1188: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1189: // {
1190: // return stmt.getObject(columnName);
1191: // }
1192:
1193: Object readValueFromStatement(CallableStatement stmt,
1194: int columnIndex) throws SQLException {
1195: return stmt.getObject(columnIndex);
1196: }
1197:
1198: Object readValueFromResultSet(ResultSet rs, String columnName)
1199: throws SQLException {
1200: return rs.getObject(columnName);
1201: }
1202:
1203: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1204: throws SQLException {
1205: return rs.getObject(columnIndex);
1206: }
1207:
1208: public int getType() {
1209: return Types.STRUCT;
1210: }
1211: }
1212:
1213: public static final class T_Ref extends BaseType {
1214: public Object sequenceKeyConversion(Long identifier)
1215: throws SequenceManagerException {
1216: throw new SequenceManagerException(
1217: "Not supported sequence key type 'REF'");
1218: }
1219:
1220: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1221: // {
1222: // return stmt.getRef(columnName);
1223: // }
1224:
1225: Object readValueFromStatement(CallableStatement stmt,
1226: int columnIndex) throws SQLException {
1227: return stmt.getRef(columnIndex);
1228: }
1229:
1230: Object readValueFromResultSet(ResultSet rs, String columnName)
1231: throws SQLException {
1232: return rs.getRef(columnName);
1233: }
1234:
1235: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1236: throws SQLException {
1237: return rs.getRef(columnIndex);
1238: }
1239:
1240: public int getType() {
1241: return Types.REF;
1242: }
1243: }
1244:
1245: //#ifdef JDBC30
1246: public static final class T_Datalink extends BaseType {
1247: public Object sequenceKeyConversion(Long identifier)
1248: throws SequenceManagerException {
1249: throw new SequenceManagerException(
1250: "Not supported sequence key type 'DATALINK'");
1251: }
1252:
1253: // Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
1254: // {
1255: // return stmt.getURL(columnName);
1256: // }
1257:
1258: Object readValueFromStatement(CallableStatement stmt,
1259: int columnIndex) throws SQLException {
1260: return stmt.getURL(columnIndex);
1261: }
1262:
1263: Object readValueFromResultSet(ResultSet rs, String columnName)
1264: throws SQLException {
1265: return rs.getURL(columnName);
1266: }
1267:
1268: Object readValueFromResultSet(ResultSet rs, int columnIndex)
1269: throws SQLException {
1270: return rs.getURL(columnIndex);
1271: }
1272:
1273: public int getType() {
1274: return Types.DATALINK;
1275: }
1276: }
1277: //#endif
1278: }
|