db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Carsten Ziegeler" <cziege...@s-und-n.de>
Subject [Patch] Get jdbc-type using reflection
Date Wed, 06 Aug 2003 07:43:58 GMT
Hi

Attached is a patch that uses reflection to resolve the column type
of a property. With the patch, you don't need to specify the
jdbc-type attribute in the mapping for most types. The value is
determined by looking at the return type of the bound property.
However, you can still use the attribute and "override" this
default.

I think this is a very valuable patch that makes writing mapping
files much easier. And it's a feature other mapping frameworks have.

The mapping between java types and sql types might have some
mistakes.

What do you think?

Carsten


Index: FieldDescriptor.java
===================================================================
RCS file:
/home/cvspublic/db-ojb/src/java/org/apache/ojb/broker/metadata/FieldDescript
or.java,v
retrieving revision 1.27
diff -u -r1.27 FieldDescriptor.java
--- FieldDescriptor.java	11 Jul 2003 07:42:05 -0000	1.27
+++ FieldDescriptor.java	5 Aug 2003 16:30:26 -0000
@@ -64,6 +64,8 @@
 import org.apache.ojb.broker.util.ClassHelper;

 import java.io.Serializable;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
 import java.sql.Types;
 import java.util.Comparator;

@@ -207,65 +209,106 @@
     private int getJdbcType()
     {
         int result;
-        String type = getColumnType().toLowerCase();
-
-        if (type.equals("bit"))
-            result = Types.BIT;
-        else if (type.equals("tinyint"))
-            result = Types.TINYINT;
-        else if (type.equals("smallint"))
-            result = Types.SMALLINT;
-        else if (type.equals("integer"))
-            result = Types.INTEGER;
-        else if (type.equals("bigint"))
-            result = Types.BIGINT;
-
-        else if (type.equals("float"))
-            result = Types.FLOAT;
-        else if (type.equals("real"))
-            result = Types.REAL;
-        else if (type.equals("double"))
-            result = Types.DOUBLE;
-
-        else if (type.equals("numeric"))
-            result = Types.NUMERIC;
-        else if (type.equals("decimal"))
-            result = Types.DECIMAL;
-
-        else if (type.equals("char"))
-            result = Types.CHAR;
-        else if (type.equals("varchar"))
-            result = Types.VARCHAR;
-        else if (type.equals("longvarchar"))
-            result = Types.LONGVARCHAR;
-
-        else if (type.equals("date"))
-            result = Types.DATE;
-        else if (type.equals("time"))
-            result = Types.TIME;
-        else if (type.equals("timestamp"))
-            result = Types.TIMESTAMP;
-
-        else if (type.equals("binary"))
-            result = Types.BINARY;
-        else if (type.equals("varbinary"))
-            result = Types.VARBINARY;
-        else if (type.equals("longvarbinary"))
-            result = Types.LONGVARBINARY;
-
-        else if (type.equals("clob"))
-            result = Types.CLOB;
-        else if (type.equals("blob"))
-            result = Types.BLOB;
-        else if (type.equals("struct"))
-            result = Types.STRUCT;
-        else
-            throw new OJBRuntimeException(
-                    "The type "
-                    + getColumnType()
-                    + " for attribute "
+        String columnType = this.getColumnType();
+        if ( columnType == null)
+        {
+            // use reflection
+            columnType = this.m_PersistentField.getType().getName();
+            if ( columnType.equals(Boolean.class.getName()) ||
columnType.equals("boolean"))
+                result = Types.BOOLEAN;
+            else if ( columnType.equals(Character.class.getName()) ||
columnType.equals("char"))
+                result = Types.CHAR;
+            else if ( columnType.equals(Short.class.getName()) ||
columnType.equals("short"))
+                result = Types.SMALLINT;
+            else if ( columnType.equals(Integer.class.getName()) ||
columnType.equals("int"))
+                result = Types.INTEGER;
+            else if ( columnType.equals(Long.class.getName()) ||
columnType.equals("long"))
+                result = Types.BIGINT;
+            else if ( columnType.equals(Byte.class.getName()) ||
columnType.equals("byte"))
+                result = Types.CHAR;
+            else if ( columnType.equals(Float.class.getName()) ||
columnType.equals("float"))
+                result = Types.FLOAT;
+            else if ( columnType.equals(Double.class.getName()) ||
columnType.equals("double"))
+                result = Types.DOUBLE;
+            else if ( columnType.equals(String.class.getName()) )
+                result = Types.VARCHAR;
+            else if ( columnType.equals(java.util.Date.class.getName()) )
+                result = Types.DATE;
+            else if ( columnType.equals(java.sql.Time.class.getName()) )
+                result = Types.TIME;
+            else if ( columnType.equals(Timestamp.class.getName()) )
+                result = Types.TIMESTAMP;
+            else if ( columnType.equals(java.sql.Date.class.getName()) )
+                result = Types.DATE;
+            else if ( columnType.equals(BigDecimal.class.getName()) )
+                result = Types.DECIMAL;
+            else
+                throw new OJBRuntimeException(
+                    "The type for attribute "
                     + getColumnName()
                     + " can not be handled by OJB. Please specify only
types as defined by java.sql.Types.");
+
+        } else {
+            String type = columnType.toLowerCase();
+
+            if (type.equals("bit"))
+                result = Types.BIT;
+            else if (type.equals("tinyint"))
+                result = Types.TINYINT;
+            else if (type.equals("smallint"))
+                result = Types.SMALLINT;
+            else if (type.equals("integer"))
+                result = Types.INTEGER;
+            else if (type.equals("bigint"))
+                result = Types.BIGINT;
+
+            else if (type.equals("float"))
+                result = Types.FLOAT;
+            else if (type.equals("real"))
+                result = Types.REAL;
+            else if (type.equals("double"))
+                result = Types.DOUBLE;
+
+            else if (type.equals("numeric"))
+                result = Types.NUMERIC;
+            else if (type.equals("decimal"))
+                result = Types.DECIMAL;
+
+            else if (type.equals("char"))
+                result = Types.CHAR;
+            else if (type.equals("varchar"))
+                result = Types.VARCHAR;
+            else if (type.equals("longvarchar"))
+                result = Types.LONGVARCHAR;
+
+            else if (type.equals("date"))
+                result = Types.DATE;
+            else if (type.equals("time"))
+                result = Types.TIME;
+            else if (type.equals("timestamp"))
+                result = Types.TIMESTAMP;
+
+            else if (type.equals("binary"))
+                result = Types.BINARY;
+            else if (type.equals("varbinary"))
+                result = Types.VARBINARY;
+            else if (type.equals("longvarbinary"))
+                result = Types.LONGVARBINARY;
+
+            else if (type.equals("clob"))
+                result = Types.CLOB;
+            else if (type.equals("blob"))
+                result = Types.BLOB;
+            else if (type.equals("struct"))
+                result = Types.STRUCT;
+            else
+                throw new OJBRuntimeException(
+                        "The type "
+                        + getColumnType()
+                        + " for attribute "
+                        + getColumnName()
+                        + " can not be handled by OJB. Please specify only
types as defined by java.sql.Types.");
+        }
         return result;
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message