You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2006/05/02 02:13:03 UTC
svn commit: r398747 [1/2] - in
/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker:
accesslayer/ metadata/
Author: arminw
Date: Mon May 1 17:13:01 2006
New Revision: 398747
URL: http://svn.apache.org/viewcvs?rev=398747&view=rev
Log:
refactoring, improvement of jdbc- and field types handling
Added:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldTypes.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/JdbcTypes.java
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldType.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/JdbcTypesHelper.java
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java?rev=398747&r1=398746&r2=398747&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java Mon May 1 17:13:01 2006
@@ -22,6 +22,7 @@
import org.apache.ojb.broker.core.PersistenceBrokerImpl;
import org.apache.ojb.broker.metadata.FieldDescriptor;
import org.apache.ojb.broker.metadata.JdbcTypesHelper;
+import org.apache.ojb.broker.metadata.JdbcType;
import org.apache.ojb.broker.query.ReportQuery;
/**
@@ -73,7 +74,9 @@
else
{
int jdbcType = extractJdbcType(i);
- val =JdbcTypesHelper.getObjectFromColumn(getRsAndStmt().m_rs, new Integer(jdbcType), i + 1);
+ JdbcType type = JdbcTypesHelper.getJdbcTypeByTypesIndex(new Integer(jdbcType));
+ val = type.getObjectFromColumn(getRsAndStmt().m_rs, i + 1);
+ //val =JdbcTypesHelper.getObjectFromColumn(getRsAndStmt().m_rs, new Integer(jdbcType), i + 1);
}
result[i] = val;
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java?rev=398747&r1=398746&r2=398747&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java Mon May 1 17:13:01 2006
@@ -21,6 +21,7 @@
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.core.PersistenceBrokerImpl;
import org.apache.ojb.broker.metadata.JdbcTypesHelper;
+import org.apache.ojb.broker.metadata.JdbcType;
/**
* ReporQueryRsIterator based on SQL-Statement
@@ -65,8 +66,9 @@
try
{
int jdbcType = rsMetaData.getColumnType(i + 1);
- Object item = JdbcTypesHelper.getObjectFromColumn(getRsAndStmt().m_rs, new Integer(jdbcType), i + 1);
- result[i] = item;
+ JdbcType type = JdbcTypesHelper.getJdbcTypeByTypesIndex(new Integer(jdbcType));
+ //Object item = JdbcTypesHelper.getObjectFromColumn(getRsAndStmt().m_rs, new Integer(jdbcType), i + 1);
+ result[i] = type.getObjectFromColumn(getRsAndStmt().m_rs, i + 1);
}
catch (SQLException e)
{
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java?rev=398747&r1=398746&r2=398747&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java Mon May 1 17:13:01 2006
@@ -16,6 +16,7 @@
*/
import java.io.Serializable;
+import java.sql.Types;
import java.util.Comparator;
import org.apache.commons.lang.SerializationUtils;
@@ -23,9 +24,8 @@
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.ojb.broker.OJBRuntimeException;
-import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentField;
import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
-import org.apache.ojb.broker.accesslayer.conversions.FieldConversionDefaultImpl;
+import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentField;
import org.apache.ojb.broker.util.ClassHelper;
/**
@@ -46,6 +46,8 @@
public static final String ACCESS_READONLY = RepositoryElements.TAG_ACCESS_READONLY;
public static final String ACCESS_READWRITE = RepositoryElements.TAG_ACCESS_READWRITE;
+ protected static final String JDBC_TYPE_CLASS_PROPERTY = "jdbcTypeClass";
+
private int m_ColNo;
private String m_ColumnName;
private String m_ColumnType;
@@ -113,7 +115,9 @@
}
/**
+ * Return the name of the mapped database column.
*
+ * @see #getFullColumnName()
*/
public String getColumnName()
{
@@ -121,12 +125,14 @@
}
/**
- * Answer the qualified ColumnName<br>
+ * Answer the qualified ColumnName
+ * <br>
* ie: myTab.name
*
- * @return
+ * @return The full column name
+ * @see #getColumnName()
*/
- public String getFullColumnName() // BRJ
+ public String getFullColumnName()
{
return getClassDescriptor().getFullTableName() + "." + getColumnName();
}
@@ -177,34 +183,63 @@
*/
private JdbcType lookupJdbcType()
{
- JdbcType result = null;
- String columnType = getColumnType();
- // if sql type was not set in metadata we use reflection
- // to determine sql type by reflection
- if (columnType == null)
- {
- try
+ JdbcType result;
+ /*
+ we allow to define specific {@link JdbcType} implementations
+ via custom attribute
+ */
+ String jdbcTypeClass = getAttribute(JDBC_TYPE_CLASS_PROPERTY);
+ if(jdbcTypeClass == null || jdbcTypeClass.length() == 0)
+ {
+ String columnType = getColumnType();
+ // if sql type was not set in metadata we use reflection
+ // to determine sql type by reflection
+ if (columnType == null || columnType.length() == 0)
{
- result = JdbcTypesHelper.getJdbcTypeByReflection(m_PersistentField.getType().getName());
+ try
+ {
+ result = JdbcTypesHelper.getJdbcTypeByReflection(m_PersistentField);
+ }
+ catch(Exception e)
+ {
+ String eol = SystemUtils.LINE_SEPARATOR;
+ throw new OJBRuntimeException("Can't automatically assign a jdbc field-type for field: "
+ + eol + this.toXML() + eol + "in class: " + eol + getClassDescriptor(), e);
+ }
}
- catch(Exception e)
+ else
{
- String eol = SystemUtils.LINE_SEPARATOR;
- throw new OJBRuntimeException("Can't automatically assign a jdbc field-type for field: "
- + eol + this.toXML() + eol + "in class: " + eol + getClassDescriptor(), e);
+ try
+ {
+ result = JdbcTypesHelper.getJdbcTypeByName(columnType);
+ // TODO: find better solution, workaround to handle Blob/Clob columns with byte-array/String fields
+ if(result.getType() == Types.BLOB && (new byte[]{}).getClass().isAssignableFrom(getPersistentField().getType()))
+ {
+ result = new JdbcTypes.T_BlobToByteArray();
+ }
+ else if(result.getType() == Types.CLOB && String.class.isAssignableFrom(getPersistentField().getType()))
+ {
+ result = new JdbcTypes.T_ClobToString();
+ }
+ }
+ catch(Exception e)
+ {
+ String eol = SystemUtils.LINE_SEPARATOR;
+ throw new OJBRuntimeException("Can't assign the specified jdbc field-type '"+columnType+"' for field: "
+ + eol + this.toXML() + eol + "in class: " + eol + getClassDescriptor(), e);
+ }
}
}
else
{
try
{
- result = JdbcTypesHelper.getJdbcTypeByName(columnType);
+ result = (JdbcType) ClassHelper.newInstance(jdbcTypeClass);
}
catch(Exception e)
{
- String eol = SystemUtils.LINE_SEPARATOR;
- throw new OJBRuntimeException("Can't assign the specified jdbc field-type '"+columnType+"' for field: "
- + eol + this.toXML() + eol + "in class: " + eol + getClassDescriptor(), e);
+ throw new MetadataException("Can't create specified " + JdbcType.class.getName()
+ + " implementation: " + jdbcTypeClass, e);
}
}
return result;
@@ -238,7 +273,7 @@
// if no conversion is specified use the default conversion
if (fieldConversion == null)
{
- fieldConversion = new FieldConversionDefaultImpl();
+ fieldConversion = FieldConversion.DEFAULT;
}
return fieldConversion;
}
@@ -463,7 +498,16 @@
*/
public boolean isAnonymous()
{
- return AnonymousPersistentField.class.isAssignableFrom(getPersistentField().getClass()) ? true : false;
+ return AnonymousPersistentField.class.isAssignableFrom(getPersistentField().getClass());
+ }
+
+ /**
+ * Returns <em>true</em> if the field is of type {@link java.sql.Blob}
+ * or {@link java.sql.Clob}.
+ */
+ public boolean isLobFieldType()
+ {
+ return getJdbcType().getFieldType().isLobField();
}
/*
@@ -548,7 +592,7 @@
// default-fetch not yet implemented
// conversion
- if( this.getFieldConversion().getClass() != FieldConversionDefaultImpl.class )
+ if( this.getFieldConversion().getClass() != FieldConversion.DEFAULT.getClass())
{
result.append( " " );
result.append( tags.getAttribute( FIELD_CONVERSION, getFieldConversion().getClass().getName() ) );
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldType.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldType.java?rev=398747&r1=398746&r2=398747&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldType.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldType.java Mon May 1 17:13:01 2006
@@ -48,21 +48,26 @@
*/
public boolean equals(Object firstValue, Object secondValue);
- /**
- * Returns the sql {@link java.sql.Types} of this field.
- */
- public int getSqlType();
-
- /**
- * Dets the associated sql field type of this field.
- * @param jdbcType The associated {@link org.apache.ojb.broker.metadata.JdbcType}.
- */
- public void setSqlType(JdbcType jdbcType);
+// /**
+// * Returns the sql {@link java.sql.Types} of this field.
+// */
+// public int getSqlType();
+//
+// /**
+// * Dets the associated sql field type of this field.
+// * @param jdbcType The associated {@link org.apache.ojb.broker.metadata.JdbcType}.
+// */
+// public void setSqlType(JdbcType jdbcType);
/**
* Returns <em>true</em> if the field type is mutable, e.g. a jdbc BLOB field or
* jdbc TIMESTAMP field.
- * @return
*/
public boolean isMutable();
+
+ /**
+ * Returns <em>true</em> if the field is of type {@link java.sql.Blob}
+ * or {@link java.sql.Clob}.
+ */
+ public boolean isLobField();
}
Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldTypes.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldTypes.java?rev=398747&view=auto
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldTypes.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/FieldTypes.java Mon May 1 17:13:01 2006
@@ -0,0 +1,338 @@
+package org.apache.ojb.broker.metadata;
+
+/* Copyright 2002-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Arrays;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.SerializationUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.ojb.broker.util.logging.LoggerFactory;
+
+/**
+ * Encapsulates {@link org.apache.ojb.broker.metadata.FieldType} implementations as inner classes.
+ *
+ * @version $Id: FieldTypes.java 365232 2005-12-21 23:36:07 +0100 (Mi, 21 Dez 2005) tomdz $
+ */
+public class FieldTypes
+{
+ private FieldTypes()
+ {
+ }
+
+ /** Base class for all fields. */
+ abstract static class BaseFieldType implements FieldType
+ {
+ /**
+ * Helper method to copy an object if possible.
+ *
+ * @param toCopy The object to copy.
+ * @return The copy of the object or <em>null</em> clone is not supported.
+ */
+ private Object copyIfCloneable(Object toCopy)
+ {
+ Object result = null;
+ if(toCopy instanceof Cloneable)
+ {
+ try
+ {
+ Method m = toCopy.getClass().getMethod("clone", ArrayUtils.EMPTY_CLASS_ARRAY);
+ /*
+ arminw:
+ By definition the overrided object.clone() method has to be public
+ so we don't need to make it accessible
+ */
+ //m.setAccessible(true);
+ result = m.invoke(toCopy, null);
+ }
+ catch(Exception e)
+ {
+ LoggerFactory.getDefaultLogger().error("[" + this.getClass().getName() + "]"
+ + "Can't invoke method 'clone' on Cloneable object: " + toCopy, e);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Helper method to copy an object if possible.
+ *
+ * @param toCopy The object to copy.
+ * @return The copy of the object or <em>null</em> if serialization is not supported.
+ */
+ private Object copyIfSerializeable(Object toCopy)
+ {
+ Object result = null;
+ if(toCopy instanceof Serializable)
+ {
+ result = SerializationUtils.clone((Serializable) toCopy);
+ }
+ return result;
+ }
+
+ /**
+ * This method copy the specified object if the object
+ * is {@link Cloneable} or {@link java.io.Serializable}, else
+ * <code>NULL</code> is returned.
+ *
+ * @param toCopy The object to copy.
+ * @return The copy result.
+ */
+ Object copyIfPossible(Object toCopy)
+ {
+ Object copy = copyIfCloneable(toCopy);
+ if(copy == null)
+ {
+ copy = copyIfSerializeable(toCopy);
+ }
+ return copy;
+ }
+
+ public String toString()
+ {
+ return new ToStringBuilder(this)
+ .append("isMutable", isMutable()).toString();
+ }
+
+ public boolean isLobField()
+ {
+ return false;
+ }
+ }
+
+ /** Base class for all <em>immutable</em> types, like Number fields, Strings, ... */
+ abstract static class ImmutableFieldType extends BaseFieldType
+ {
+ public boolean isMutable()
+ {
+ return false;
+ }
+
+ public Object copy(Object source)
+ {
+ return source;
+ }
+
+ public boolean equals(Object firstValue, Object secondValue)
+ {
+ return ObjectUtils.equals(firstValue, secondValue);
+ }
+ }
+
+ /** Base class for all <em>mutable</em> fields. */
+ abstract static class MutableFieldType extends BaseFieldType
+ {
+ public boolean isMutable()
+ {
+ return true;
+ }
+
+ public boolean equals(Object firstValue, Object secondValue)
+ {
+ return ObjectUtils.equals(firstValue, secondValue);
+ }
+ }
+
+ /**
+ * Clob (like other LOB's) fields are logical pointer to DB, so
+ * for OJB it's immutable.
+ *
+ * @see BlobFieldType
+ */
+ public static class ClobFieldType extends ImmutableFieldType
+ {
+ public boolean isLobField()
+ {
+ return true;
+ }
+ }
+
+ /**
+ * Blob fields are logical pointer to DB, so for OJB it's immutable.
+ * Snip of JDBC specification:
+ * "An application does not deal directly with the LOCATOR(blob) and
+ * LOCATOR(clob) types that are defined in SQL. By default, a JDBC
+ * driver should implement the Blob and Clob interfaces using the
+ * appropriate locator type. Also by default, Blob and Clob objects
+ * remain valid only during the transaction in which they are created."
+ */
+ public static class BlobFieldType extends ImmutableFieldType
+ {
+ public boolean isLobField()
+ {
+ return true;
+ }
+ }
+
+ /**
+ * Array fields are logical pointer to DB, so for OJB it's immutable.
+ * Snip of JDBC specification:
+ * "The Array object returned to an application by the ResultSet.getArray and
+ * CallableStatement.getArray methods is a logical pointer to the SQL ARRAY
+ * value in the database; it does not contain the contents of the SQL ARRAY value."
+ */
+ public static class ArrayFieldType extends ImmutableFieldType
+ {
+ }
+
+ /**
+ * Ref fields are logical pointer to DB, so for OJB it's immutable.
+ * Snip of JDBC specification:
+ * "An SQL REF value is a pointer; therefore, a Ref object, which is the mapping of a
+ * REF value, is likewise a pointer and does not contain the data of the structured type
+ * instance to which it refers."
+ */
+ public static class RefFieldType extends ImmutableFieldType
+ {
+ }
+
+ /**
+ * When using SQL UDT's it's possible that the jdbc-driver returns
+ * full materialized java objects defined by the user.
+ */
+ public static class StructFieldType extends MutableFieldType
+ {
+ // TODO: does this make sense?? or Struct instances always Locator objects?
+ public Object copy(Object fieldValue)
+ {
+ if(fieldValue == null) return null;
+
+ Object copy = copyIfPossible(fieldValue);
+ return copy == null ? fieldValue : copy;
+ }
+ }
+
+ /**
+ * If a user-defined object is used, we can check if object is
+ * {@link Cloneable} or {@link java.io.Serializable} to copy the object.
+ * If not possible return the specified object instance.
+ */
+ public static class JavaObjectFieldType extends MutableFieldType
+ {
+ public Object copy(Object fieldValue)
+ {
+ if(fieldValue == null) return null;
+
+ Object copy = copyIfPossible(fieldValue);
+ return copy == null ? fieldValue : copy;
+ }
+ }
+
+ public static class ByteArrayFieldType extends MutableFieldType
+ {
+ public Object copy(Object fieldValue)
+ {
+ byte[] result = null;
+ if(fieldValue != null)
+ {
+ byte[] source = (byte[]) fieldValue;
+ int length = source.length;
+ result = new byte[length];
+ System.arraycopy(fieldValue, 0, result, 0, length);
+ }
+ return result;
+ }
+
+ public boolean equals(Object firstValue, Object secondValue)
+ {
+ return Arrays.equals((byte[]) firstValue, (byte[]) secondValue);
+ }
+ }
+
+ public static class DateFieldType extends MutableFieldType
+ {
+ public Object copy(Object fieldValue)
+ {
+ Date source = (Date) fieldValue;
+ return source != null ? new Date(source.getTime()) : null;
+ }
+ }
+
+ public static class TimeFieldType extends MutableFieldType
+ {
+ public Object copy(Object fieldValue)
+ {
+ Time source = (Time) fieldValue;
+ return source != null ? new Time(source.getTime()) : null;
+ }
+ }
+
+ public static class TimestampFieldType extends MutableFieldType
+ {
+ public Object copy(Object fieldValue)
+ {
+ Timestamp result = null;
+ if(fieldValue != null)
+ {
+ Timestamp source = (Timestamp) fieldValue;
+ result = (Timestamp) source.clone();
+ }
+ return result;
+ }
+ }
+
+ public static class StringFieldType extends ImmutableFieldType
+ {
+ }
+
+ public static class BigDecimalFieldType extends ImmutableFieldType
+ {
+ }
+
+ public static class BooleanFieldType extends ImmutableFieldType
+ {
+ }
+
+ public static class ByteFieldType extends ImmutableFieldType
+ {
+ }
+
+ public static class ShortFieldType extends ImmutableFieldType
+ {
+
+ }
+
+ public static class IntegerFieldType extends ImmutableFieldType
+ {
+
+ }
+
+ public static class LongFieldType extends ImmutableFieldType
+ {
+
+ }
+
+ public static class FloatFieldType extends ImmutableFieldType
+ {
+
+ }
+
+ public static class DoubleFieldType extends ImmutableFieldType
+ {
+
+ }
+
+ public static class URLFieldType extends ImmutableFieldType
+ {
+
+ }
+}
Added: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/JdbcTypes.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/JdbcTypes.java?rev=398747&view=auto
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/JdbcTypes.java (added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/metadata/JdbcTypes.java Mon May 1 17:13:01 2006
@@ -0,0 +1,1271 @@
+package org.apache.ojb.broker.metadata;
+
+/* Copyright 2002-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
+import java.sql.Blob;
+import java.sql.CallableStatement;
+import java.sql.Clob;
+import java.sql.Date;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.sql.Types;
+
+import org.apache.commons.lang.BooleanUtils;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.ojb.broker.util.SqlHelper;
+import org.apache.ojb.broker.util.sequence.SequenceManagerException;
+
+/**
+ * Encapsulates {@link org.apache.ojb.broker.metadata.JdbcType} implementations as inner classes.
+ *
+ * @version $Id: JdbcTypes.java 382468 2006-03-02 20:00:37 +0100 (Do, 02 Mrz 2006) arminw $
+ * @see JdbcType
+ */
+public class JdbcTypes
+{
+ private JdbcTypes()
+ {
+ // default constructor
+ }
+
+ //======================================================================================
+ // inner classes implementing JdbcType interface
+ //======================================================================================
+
+ public abstract static class BaseType implements JdbcType
+ {
+ private FieldType fieldType;
+
+ protected BaseType()
+ {
+ fieldType = lookupFieldType();
+ }
+
+ /**
+ * This method is responsible to associate the {@link FieldType}
+ * of this {@link JdbcType}.
+ */
+ FieldType lookupFieldType()
+ {
+ return JdbcTypesHelper.newFieldType(this);
+ }
+
+ abstract Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException;
+
+ abstract Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException;
+
+ abstract Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException;
+ /*
+ only supported by jdk >= 1.4x, maybe useful in further versions
+ */
+ // abstract Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException;
+
+ public boolean equals(Object obj)
+ {
+ if(this == obj) return true;
+ boolean result = false;
+ if(obj instanceof JdbcType)
+ {
+ result = this.getType() == ((JdbcType) obj).getType();
+ }
+ return result;
+ }
+
+ public int hashCode()
+ {
+ return getType();
+ }
+
+ public FieldType getFieldType()
+ {
+ return fieldType;
+ }
+
+ public Object getObjectFromColumn(CallableStatement stmt, int columnId) throws SQLException
+ {
+ return getObjectFromColumn(null, stmt, null, columnId);
+ }
+
+ public Object getObjectFromColumn(ResultSet rs, String columnName) throws SQLException
+ {
+ return getObjectFromColumn(rs, null, columnName, MIN_INT);
+ }
+
+ public Object getObjectFromColumn(ResultSet rs, int columnId) throws SQLException
+ {
+ return getObjectFromColumn(rs, null, null, columnId);
+ }
+
+ public Object getObjectFromColumn(final ResultSet rs, final CallableStatement stmt,
+ final String columnName, int columnIndex) throws SQLException
+ {
+ if(stmt != null)
+ {
+ if(columnIndex == MIN_INT)
+ {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+ else
+ {
+ return readValueFromStatement(stmt, columnIndex);
+ }
+ }
+ else
+ {
+ return columnIndex == MIN_INT ?
+ readValueFromResultSet(rs, SqlHelper.stripOjbQuotes(columnName))
+ : readValueFromResultSet(rs, columnIndex);
+ }
+ }
+
+ public String toString()
+ {
+ return new ToStringBuilder(this)
+ .append("jdbcType", getType())
+ .append("jdbcTypeString", JdbcTypesHelper.getSqlTypeAsString(getType()))
+ .append("associatedFieldType", getFieldType())
+ .toString();
+ }
+
+// // not used in code, but maybe useful in further versions
+// public Object getObjectFromColumn(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return getObjectFromColumn(null, stmt, columnName, MIN_INT);
+// }
+//
+// public Object getObjectFromColumn(ResultSet rs, int columnId) throws SQLException
+// {
+// return getObjectFromColumn(rs, null, null, columnId);
+// }
+
+ }
+
+
+ public static final class T_Char extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier.toString();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getString(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getString(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getString(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getString(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.CHAR;
+ }
+ }
+
+ public static final class T_Varchar extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier.toString();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getString(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getString(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getString(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getString(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.VARCHAR;
+ }
+ }
+
+ public static final class T_LongVarChar extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier.toString();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getString(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getString(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getString(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getString(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.LONGVARCHAR;
+ }
+ }
+
+ public static final class T_Numeric extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new BigDecimal(identifier.longValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getBigDecimal(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getBigDecimal(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getBigDecimal(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getBigDecimal(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.NUMERIC;
+ }
+ }
+
+ public static final class T_Decimal extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new BigDecimal(identifier.longValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getBigDecimal(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getBigDecimal(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getBigDecimal(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getBigDecimal(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.DECIMAL;
+ }
+ }
+
+ public static final class T_Bit extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'BIT'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// boolean temp = stmt.getBoolean(columnName);
+// return (stmt.wasNull() ? null : new Boolean(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ boolean temp = stmt.getBoolean(columnIndex);
+ return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ boolean temp = rs.getBoolean(columnName);
+ return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ boolean temp = rs.getBoolean(columnIndex);
+ return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+ }
+
+ public int getType()
+ {
+ return Types.BIT;
+ }
+ }
+
+//#ifdef JDBC30
+
+ public static final class T_Boolean extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(final Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'BOOLEAN'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// boolean temp = stmt.getBoolean(columnName);
+// return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ boolean temp = stmt.getBoolean(columnIndex);
+ return (stmt.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ boolean temp = rs.getBoolean(columnName);
+ return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ boolean temp = rs.getBoolean(columnIndex);
+ return (rs.wasNull() ? null : BooleanUtils.toBooleanObject(temp));
+ }
+
+ public int getType()
+ {
+ return Types.BOOLEAN;
+ }
+ }
+//#endif
+
+ public static final class T_TinyInt extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(final Long identifier)
+ {
+ return new Byte(identifier.byteValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// byte temp = stmt.getByte(columnName);
+// return (stmt.wasNull() ? null : new Byte(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ byte temp = stmt.getByte(columnIndex);
+ return (stmt.wasNull() ? null : new Byte(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ byte temp = rs.getByte(columnName);
+ return (rs.wasNull() ? null : new Byte(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ byte temp = rs.getByte(columnIndex);
+ return (rs.wasNull() ? null : new Byte(temp));
+ }
+
+ public int getType()
+ {
+ return Types.TINYINT;
+ }
+ }
+
+ public static final class T_SmallInt extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Short(identifier.shortValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// short temp = stmt.getShort(columnName);
+// return (stmt.wasNull() ? null : new Short(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ short temp = stmt.getShort(columnIndex);
+ return (stmt.wasNull() ? null : new Short(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ short temp = rs.getShort(columnName);
+ return (rs.wasNull() ? null : new Short(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ short temp = rs.getShort(columnIndex);
+ return (rs.wasNull() ? null : new Short(temp));
+ }
+
+ public int getType()
+ {
+ return Types.SMALLINT;
+ }
+ }
+
+ public static final class T_Integer extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Integer(identifier.intValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// int temp = stmt.getInt(columnName);
+// return (stmt.wasNull() ? null : new Integer(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ int temp = stmt.getInt(columnIndex);
+ return (stmt.wasNull() ? null : new Integer(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ int temp = rs.getInt(columnName);
+ return (rs.wasNull() ? null : new Integer(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ int temp = rs.getInt(columnIndex);
+ return (rs.wasNull() ? null : new Integer(temp));
+ }
+
+ public int getType()
+ {
+ return Types.INTEGER;
+ }
+ }
+
+ public static final class T_BigInt extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier;
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// long temp = stmt.getLong(columnName);
+// return (stmt.wasNull() ? null : new Long(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ long temp = stmt.getLong(columnIndex);
+ return (stmt.wasNull() ? null : new Long(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ long temp = rs.getLong(columnName);
+ return (rs.wasNull() ? null : new Long(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ long temp = rs.getLong(columnIndex);
+ return (rs.wasNull() ? null : new Long(temp));
+ }
+
+ public int getType()
+ {
+ return Types.BIGINT;
+ }
+ }
+
+ public static final class T_Real extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Float(identifier.floatValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// float temp = stmt.getFloat(columnName);
+// return (stmt.wasNull() ? null : new Float(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ float temp = stmt.getFloat(columnIndex);
+ return (stmt.wasNull() ? null : new Float(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ float temp = rs.getFloat(columnName);
+ return (rs.wasNull() ? null : new Float(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ float temp = rs.getFloat(columnIndex);
+ return (rs.wasNull() ? null : new Float(temp));
+ }
+
+ public int getType()
+ {
+ return Types.REAL;
+ }
+ }
+
+ public static final class T_Float extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Double(identifier.doubleValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// double temp = stmt.getDouble(columnName);
+// return (stmt.wasNull() ? null : new Double(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ double temp = stmt.getDouble(columnIndex);
+ return (stmt.wasNull() ? null : new Double(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ double temp = rs.getDouble(columnName);
+ return (rs.wasNull() ? null : new Double(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ double temp = rs.getDouble(columnIndex);
+ return (rs.wasNull() ? null : new Double(temp));
+ }
+
+ public int getType()
+ {
+ return Types.FLOAT;
+ }
+ }
+
+ public static final class T_Double extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Double(identifier.doubleValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// double temp = stmt.getDouble(columnName);
+// return (stmt.wasNull() ? null : new Double(temp));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ double temp = stmt.getDouble(columnIndex);
+ return (stmt.wasNull() ? null : new Double(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ double temp = rs.getDouble(columnName);
+ return (rs.wasNull() ? null : new Double(temp));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ double temp = rs.getDouble(columnIndex);
+ return (rs.wasNull() ? null : new Double(temp));
+ }
+
+ public int getType()
+ {
+ return Types.DOUBLE;
+ }
+ }
+
+ public static final class T_Binary extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier.toString().getBytes();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getBytes(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getBytes(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getBytes(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getBytes(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.BINARY;
+ }
+ }
+
+ public static final class T_VarBinary extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier.toString().getBytes();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getBytes(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getBytes(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getBytes(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getBytes(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.VARBINARY;
+ }
+ }
+
+ public static final class T_LongVarBinary extends JdbcTypes.BaseType
+ {
+ protected static final int BUFSZ = 2048;
+
+ /**
+ * Retrieve LONGVARBINARY InputStream data and pack into a byte array.
+ *
+ * @param is the input stream to be retrieved
+ * @return a string containing the clob data
+ * @throws java.sql.SQLException if conversion fails or the clob cannot be read
+ */
+ protected static byte[] retrieveStreamDataFromRs(InputStream is) throws SQLException
+ {
+ if(is == null)
+ {
+ return null;
+ }
+ byte[] bytes = null;
+ ByteArrayOutputStream bos = null;
+ try
+ {
+ bos = new ByteArrayOutputStream();
+ int numRead;
+ byte[] buf = new byte[JdbcTypes.T_LongVarBinary.BUFSZ];
+ while((numRead = is.read(buf, 0, buf.length)) > 0)
+ {
+ bos.write(buf, 0, numRead);
+ }
+ bytes = bos.toByteArray();
+ }
+ catch(IOException e)
+ {
+ throw new SQLException("I/O exception retrieving LONGVARBINARY: " + e.getLocalizedMessage());
+ }
+ finally
+ {
+ if(bos != null)
+ {
+ try
+ {
+ bos.close();
+ }
+ catch(Exception ignored)
+ {
+ //ignore
+ }
+ }
+ }
+ return bytes;
+ }
+
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return identifier.toString().getBytes();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getBytes(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getBytes(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return JdbcTypes.T_LongVarBinary.retrieveStreamDataFromRs(rs.getBinaryStream(columnName));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return JdbcTypes.T_LongVarBinary.retrieveStreamDataFromRs(rs.getBinaryStream(columnIndex));
+ }
+
+ public int getType()
+ {
+ return Types.LONGVARBINARY;
+ }
+ }
+
+ public static final class T_Date extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Date(identifier.longValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getDate(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getDate(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getDate(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getDate(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.DATE;
+ }
+ }
+
+ public static final class T_Time extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Time(identifier.longValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getTime(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getTime(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getTime(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getTime(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.TIME;
+ }
+ }
+
+ public static final class T_Timestamp extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier)
+ {
+ return new Timestamp(identifier.longValue());
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getTimestamp(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getTimestamp(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getTimestamp(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getTimestamp(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.TIMESTAMP;
+ }
+ }
+
+ public static final class T_Clob extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'CLOB'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// Clob aClob = stmt.getClob(columnName);
+// return (stmt.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ Clob aClob = stmt.getClob(columnIndex);
+ return stmt.wasNull() ? null : aClob;
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ Clob aClob = rs.getClob(columnName);
+ return rs.wasNull() ? null : aClob;
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ Clob aClob = rs.getClob(columnIndex);
+ return rs.wasNull() ? null : aClob;
+ }
+
+ public int getType()
+ {
+ return Types.CLOB;
+ }
+ }
+
+ public static final class T_ClobToString extends JdbcTypes.BaseType
+ {
+ protected static final int BUFSZ = 32768;
+
+ /**
+ * Convert CLOB to String. Safe for very large objects.
+ *
+ * @param aClob clob with character data
+ * @return a string containing the clob data
+ * @throws java.sql.SQLException if conversion fails or the clob cannot be read
+ */
+ protected static String safeClobToString(Clob aClob) throws SQLException
+ {
+ long length = aClob.length();
+ if(length == 0)
+ {
+ return "";
+ }
+ StringBuffer sb = new StringBuffer();
+ char[] buf = new char[JdbcTypes.T_ClobToString.BUFSZ];
+ java.io.Reader stream = aClob.getCharacterStream();
+ try
+ {
+ int numRead;
+ while((numRead = stream.read(buf)) != -1)
+ {
+ sb.append(buf, 0, numRead);
+ }
+ stream.close();
+ }
+ catch(IOException e)
+ {
+ throw new SQLException(e.getLocalizedMessage());
+ }
+ return sb.toString();
+ }
+
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'CLOB'");
+ }
+
+ FieldType lookupFieldType()
+ {
+ return new FieldTypes.StringFieldType();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// Clob aClob = stmt.getClob(columnName);
+// return (stmt.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ Clob aClob = stmt.getClob(columnIndex);
+ return (stmt.wasNull() ? null : JdbcTypes.T_ClobToString.safeClobToString(aClob));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ Clob aClob = rs.getClob(columnName);
+ return (rs.wasNull() ? null : JdbcTypes.T_ClobToString.safeClobToString(aClob));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ Clob aClob = rs.getClob(columnIndex);
+ return (rs.wasNull() ? null : JdbcTypes.T_ClobToString.safeClobToString(aClob));
+ }
+
+ public int getType()
+ {
+ return Types.CLOB;
+ }
+ }
+
+ public static final class T_Blob extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'BLOB'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// Blob aBlob = stmt.getBlob(columnName);
+// return (stmt.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ Blob aBlob = stmt.getBlob(columnIndex);
+ return stmt.wasNull() ? null : aBlob;
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ Blob aBlob = rs.getBlob(columnName);
+ return rs.wasNull() ? null : aBlob;
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ Blob aBlob = rs.getBlob(columnIndex);
+ return rs.wasNull() ? null : aBlob;
+ }
+
+ public int getType()
+ {
+ return Types.BLOB;
+ }
+ }
+
+ public static final class T_BlobToByteArray extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'BLOB'");
+ }
+
+ FieldType lookupFieldType()
+ {
+ return new FieldTypes.ByteArrayFieldType();
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// Blob aBlob = stmt.getBlob(columnName);
+// return (stmt.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ Blob aBlob = stmt.getBlob(columnIndex);
+ return (stmt.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ Blob aBlob = rs.getBlob(columnName);
+ return (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ Blob aBlob = rs.getBlob(columnIndex);
+ return (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
+ }
+
+ public int getType()
+ {
+ return Types.BLOB;
+ }
+ }
+
+ public static final class T_Array extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'ARRAY'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getArray(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getArray(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getArray(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getArray(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.ARRAY;
+ }
+ }
+
+ public static final class T_Struct extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'STRUCT'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getObject(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getObject(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getObject(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getObject(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.STRUCT;
+ }
+ }
+
+ public static final class T_Ref extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'REF'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getRef(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getRef(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getRef(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getRef(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.REF;
+ }
+ }
+
+ public static final class T_JavaObject extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'JAVA_OBJECT'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getRef(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getObject(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getObject(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getObject(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.JAVA_OBJECT;
+ }
+ }
+
+//#ifdef JDBC30
+
+ public static final class T_Datalink extends JdbcTypes.BaseType
+ {
+ public Object sequenceKeyConversion(Long identifier) throws SequenceManagerException
+ {
+ throw new SequenceManagerException("Not supported sequence key type 'DATALINK'");
+ }
+
+// Object readValueFromStatement(CallableStatement stmt, String columnName) throws SQLException
+// {
+// return stmt.getURL(columnName);
+// }
+
+ Object readValueFromStatement(CallableStatement stmt, int columnIndex) throws SQLException
+ {
+ return stmt.getURL(columnIndex);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, String columnName) throws SQLException
+ {
+ return rs.getURL(columnName);
+ }
+
+ Object readValueFromResultSet(ResultSet rs, int columnIndex) throws SQLException
+ {
+ return rs.getURL(columnIndex);
+ }
+
+ public int getType()
+ {
+ return Types.DATALINK;
+ }
+ }
+//#endif
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org