Return-Path: Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 25613 invoked by uid 500); 25 May 2003 00:00:22 -0000 Received: (qmail 25610 invoked from network); 25 May 2003 00:00:22 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 25 May 2003 00:00:22 -0000 Received: (qmail 51993 invoked by uid 1510); 25 May 2003 00:00:21 -0000 Date: 25 May 2003 00:00:21 -0000 Message-ID: <20030525000021.51992.qmail@icarus.apache.org> From: arminw@apache.org To: db-ojb-cvs@apache.org Subject: cvs commit: db-ojb/src/java/org/apache/ojb/broker/util SqlHelper.java ProxyHelper.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N arminw 2003/05/24 17:00:20 Modified: src/java/org/apache/ojb/broker/util SqlHelper.java ProxyHelper.java Log: please review: improvements by Lance Eason Revision Changes Path 1.13 +171 -146 db-ojb/src/java/org/apache/ojb/broker/util/SqlHelper.java Index: SqlHelper.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/SqlHelper.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- SqlHelper.java 6 May 2003 19:53:27 -0000 1.12 +++ SqlHelper.java 25 May 2003 00:00:20 -0000 1.13 @@ -1,8 +1,8 @@ package org.apache.ojb.broker.util; -import org.apache.ojb.broker.metadata.FieldDescriptor; -import org.apache.ojb.broker.metadata.ClassDescriptor; import org.apache.ojb.broker.OJBRuntimeException; +import org.apache.ojb.broker.metadata.ClassDescriptor; +import org.apache.ojb.broker.metadata.FieldDescriptor; import java.sql.ResultSet; import java.sql.SQLException; @@ -85,7 +85,7 @@ public String prefix; public String suffix; - PathInfo(String aPrefix, String aColumn, String aSuffix) + PathInfo(String aPrefix, String aColumn, String aSuffix) { column = aColumn; prefix = aPrefix; @@ -102,19 +102,47 @@ */ public static String cleanPath(String aPath) { - return splitPath(aPath).column; + int braceBegin; + int braceEnd; + int colBegin; + String betweenBraces; + String colName = aPath; + + if (aPath == null) return null; + + braceBegin = aPath.indexOf("("); + if (braceBegin >= 0) + { + braceEnd = aPath.lastIndexOf(")"); + if (braceEnd >= 0) + { + betweenBraces = aPath.substring(braceBegin + 1, braceEnd).trim(); + // look for ie 'distinct name' + colBegin = betweenBraces.lastIndexOf(" "); + if (colBegin >= 0) + { + colName = betweenBraces.substring(colBegin + 1).trim(); + } + else + { + colName = betweenBraces; + } + } + } + + return colName; } /** - * Split a path into column , prefix and suffix, the prefix contains all info - * up to the column
+ * Split a path into column , prefix and suffix, the prefix contains all info + * up to the column
* ie: avg(amount) -> amount , avg( , )
* ie: sum (accounts.amount) as theSum -> accounts.amount , sum( , ) as theSum
* ie: count( distinct id ) as bla -> id , count(distinct , ) as bla
- * @param aPath - * @return PathInfo - */ + * @param aPath + * @return PathInfo + */ public static PathInfo splitPath(String aPath) { int braceBegin; @@ -123,7 +151,7 @@ String betweenBraces; String prefix = null; String suffix = null; - String colName; + String colName = aPath; if (aPath == null) { @@ -131,35 +159,32 @@ } braceBegin = aPath.indexOf("("); - braceEnd = aPath.lastIndexOf(")"); - if (braceBegin >= 0 && braceEnd >= 0) + if (braceBegin >= 0) { - betweenBraces = aPath.substring(braceBegin + 1, braceEnd).trim(); - // look for ie 'distinct name' - colBegin = betweenBraces.lastIndexOf(" "); - if (colBegin >= 0) + braceEnd = aPath.lastIndexOf(")"); + if (braceEnd >= 0) { - prefix = aPath.substring(0, braceBegin + 1) + betweenBraces.substring(0, colBegin + 1); - colName = betweenBraces.substring(colBegin + 1).trim(); - } - else - { - prefix = aPath.substring(0, braceBegin + 1); - colName = betweenBraces; + betweenBraces = aPath.substring(braceBegin + 1, braceEnd).trim(); + // look for ie 'distinct name' + colBegin = betweenBraces.lastIndexOf(" "); + if (colBegin >= 0) + { + prefix = aPath.substring(0, braceBegin + 1) + betweenBraces.substring(0, colBegin + 1); + colName = betweenBraces.substring(colBegin + 1).trim(); + } + else + { + prefix = aPath.substring(0, braceBegin + 1); + colName = betweenBraces; + } + suffix = aPath.substring(braceEnd); } - suffix = aPath.substring(braceEnd); - } - else - { - colName = aPath; } return new PathInfo(prefix, colName, suffix); } - - /** * retrieves an Object from a ResultSet column. * @param rs the ResultSet to be read from. @@ -188,130 +213,130 @@ switch (jdbcType) { - case Types.BIT : + case Types.BIT: { - boolVal = rs.getBoolean( columnId ); - result = ( rs.wasNull() ? null : new Boolean( boolVal ) ); + boolVal = rs.getBoolean(columnId); + result = (rs.wasNull() ? null : new Boolean(boolVal)); break; } - case Types.TINYINT : + case Types.TINYINT: { - byteVal = rs.getByte( columnId ); - result = ( rs.wasNull() ? null : new Byte( byteVal ) ); + byteVal = rs.getByte(columnId); + result = (rs.wasNull() ? null : new Byte(byteVal)); break; } - case Types.SMALLINT : + case Types.SMALLINT: { - shortVal = rs.getShort( columnId ); - result = ( rs.wasNull() ? null : new Short( shortVal ) ); + shortVal = rs.getShort(columnId); + result = (rs.wasNull() ? null : new Short(shortVal)); break; } - case Types.INTEGER : + case Types.INTEGER: { - intVal = rs.getInt( columnId ); - result = ( rs.wasNull() ? null : new Integer( intVal ) ); + intVal = rs.getInt(columnId); + result = (rs.wasNull() ? null : new Integer(intVal)); break; } - case Types.BIGINT : + case Types.BIGINT: { - longVal = rs.getLong( columnId ); - result = ( rs.wasNull() ? null : new Long( longVal ) ); + longVal = rs.getLong(columnId); + result = (rs.wasNull() ? null : new Long(longVal)); break; } - case Types.DOUBLE : - case Types.FLOAT : + case Types.DOUBLE: + case Types.FLOAT: { - doubleVal = rs.getDouble( columnId ); - result = ( rs.wasNull() ? null : new Double( doubleVal ) ); + doubleVal = rs.getDouble(columnId); + result = (rs.wasNull() ? null : new Double(doubleVal)); break; } - case Types.REAL : + case Types.REAL: { - floatVal = rs.getFloat( columnId ); - result = ( rs.wasNull() ? null : new Float( floatVal ) ); + floatVal = rs.getFloat(columnId); + result = (rs.wasNull() ? null : new Float(floatVal)); break; } - case Types.NUMERIC : + case Types.NUMERIC: { result = rs.getBigDecimal(columnId); break; } - case Types.DECIMAL : + case Types.DECIMAL: { result = rs.getBigDecimal(columnId); break; } - case Types.CHAR : + case Types.CHAR: { result = rs.getString(columnId); break; } - case Types.VARCHAR : + case Types.VARCHAR: { result = rs.getString(columnId); break; } - case Types.LONGVARCHAR : + case Types.LONGVARCHAR: { result = rs.getString(columnId); break; } - case Types.DATE : + case Types.DATE: { result = rs.getDate(columnId); break; } - case Types.TIME : + case Types.TIME: { result = rs.getTime(columnId); break; } - case Types.TIMESTAMP : + case Types.TIMESTAMP: { result = rs.getTimestamp(columnId); break; } - case Types.BINARY : + case Types.BINARY: { result = rs.getBytes(columnId); break; } - case Types.VARBINARY : + case Types.VARBINARY: { result = rs.getBytes(columnId); break; } - case Types.LONGVARBINARY : + case Types.LONGVARBINARY: { result = rs.getBytes(columnId); break; } - case Types.CLOB : + case Types.CLOB: { java.sql.Clob aClob = rs.getClob(columnId); - result = ( rs.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()) ); + result = (rs.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length())); break; } - case Types.BLOB : + case Types.BLOB: { java.sql.Blob aBlob = rs.getBlob(columnId); - result = ( rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()) ); + result = (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length())); + break; + } + case Types.STRUCT: + { + java.sql.Struct aStruct = (java.sql.Struct) rs.getObject(columnId); + result = (rs.wasNull() ? null : aStruct); break; } - case Types.STRUCT: - { - java.sql.Struct aStruct = (java.sql.Struct) rs.getObject(columnId); - result = ( rs.wasNull() ? null : aStruct ); - break; - } default : { throw new OJBRuntimeException( - "The type " + "The type " + jdbcType + " for attribute " + columnId @@ -337,129 +362,129 @@ switch (jdbcType) { - case Types.BIT : + case Types.BIT: { - boolVal = rs.getBoolean( columnId ); - result = ( rs.wasNull() ? null : new Boolean( boolVal ) ); + boolVal = rs.getBoolean(columnId); + result = (rs.wasNull() ? null : new Boolean(boolVal)); break; } - case Types.TINYINT : + case Types.TINYINT: { - byteVal = rs.getByte( columnId ); - result = ( rs.wasNull() ? null : new Byte( byteVal ) ); + byteVal = rs.getByte(columnId); + result = (rs.wasNull() ? null : new Byte(byteVal)); break; } - case Types.SMALLINT : + case Types.SMALLINT: { - shortVal = rs.getShort( columnId ); - result = ( rs.wasNull() ? null : new Short( shortVal ) ); + shortVal = rs.getShort(columnId); + result = (rs.wasNull() ? null : new Short(shortVal)); break; } - case Types.INTEGER : + case Types.INTEGER: { - intVal = rs.getInt( columnId ); - result = ( rs.wasNull() ? null : new Integer( intVal ) ); + intVal = rs.getInt(columnId); + result = (rs.wasNull() ? null : new Integer(intVal)); break; } - case Types.BIGINT : + case Types.BIGINT: { - longVal = rs.getLong( columnId ); - result = ( rs.wasNull() ? null : new Long( longVal ) ); + longVal = rs.getLong(columnId); + result = (rs.wasNull() ? null : new Long(longVal)); break; } - case Types.DOUBLE : - case Types.FLOAT : + case Types.DOUBLE: + case Types.FLOAT: { - doubleVal = rs.getDouble( columnId ); - result = ( rs.wasNull() ? null : new Double( doubleVal ) ); + doubleVal = rs.getDouble(columnId); + result = (rs.wasNull() ? null : new Double(doubleVal)); break; } - case Types.REAL : + case Types.REAL: { - floatVal = rs.getFloat( columnId ); - result = ( rs.wasNull() ? null : new Float( floatVal ) ); + floatVal = rs.getFloat(columnId); + result = (rs.wasNull() ? null : new Float(floatVal)); break; } - case Types.NUMERIC : + case Types.NUMERIC: { result = rs.getBigDecimal(columnId); break; } - case Types.DECIMAL : + case Types.DECIMAL: { result = rs.getBigDecimal(columnId); break; } - case Types.CHAR : + case Types.CHAR: { result = rs.getString(columnId); break; } - case Types.VARCHAR : + case Types.VARCHAR: { result = rs.getString(columnId); break; } - case Types.LONGVARCHAR : + case Types.LONGVARCHAR: { result = rs.getString(columnId); break; } - case Types.DATE : + case Types.DATE: { result = rs.getDate(columnId); break; } - case Types.TIME : + case Types.TIME: { result = rs.getTime(columnId); break; } - case Types.TIMESTAMP : + case Types.TIMESTAMP: { result = rs.getTimestamp(columnId); break; } - case Types.BINARY : + case Types.BINARY: { result = rs.getBytes(columnId); break; } - case Types.VARBINARY : + case Types.VARBINARY: { result = rs.getBytes(columnId); break; } - case Types.LONGVARBINARY : + case Types.LONGVARBINARY: { result = rs.getBytes(columnId); break; } - case Types.CLOB : + case Types.CLOB: { java.sql.Clob aClob = rs.getClob(columnId); - result = ( rs.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()) ); + result = (rs.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length())); break; } - case Types.BLOB : + case Types.BLOB: { java.sql.Blob aBlob = rs.getBlob(columnId); - result = ( rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()) ); + result = (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length())); + break; + } + case Types.STRUCT: + { + java.sql.Struct aStruct = (java.sql.Struct) rs.getObject(columnId); + result = (rs.wasNull() ? null : aStruct); break; } - case Types.STRUCT: - { - java.sql.Struct aStruct = (java.sql.Struct) rs.getObject(columnId); - result = ( rs.wasNull() ? null : aStruct ); - break; - } default : { throw new OJBRuntimeException( - "The type " + "The type " + jdbcType + " for attribute " + columnId @@ -531,8 +556,8 @@ /** * @return SQL type for the given value, Types.OTHER if not found. */ - public static int getSqlTypeByValue(Object value) - { + public static int getSqlTypeByValue(Object value) + { if (value instanceof Integer) { return Types.INTEGER; @@ -593,107 +618,107 @@ { return Types.OTHER; } - } + } public static Object getJdbcTypeAsString(int jdbcType) { switch (jdbcType) { - case Types.BIT : + case Types.BIT: { return "Types.BIT"; } - case Types.TINYINT : + case Types.TINYINT: { return "Types.TINYINT"; } - case Types.SMALLINT : + case Types.SMALLINT: { return "Types.SMALLINT"; } - case Types.INTEGER : + case Types.INTEGER: { return "Types.INTEGER"; } - case Types.BIGINT : + case Types.BIGINT: { return "Types.BIGINT"; } - case Types.DOUBLE : + case Types.DOUBLE: { return "Types.DOUBLE"; } - case Types.FLOAT : + case Types.FLOAT: { return "Types.FLOAT"; } - case Types.REAL : + case Types.REAL: { return "Types.REAL"; } - case Types.NUMERIC : + case Types.NUMERIC: { return "Types.NUMERIC"; } - case Types.DECIMAL : + case Types.DECIMAL: { return "Types.DECIMAL"; } - case Types.CHAR : + case Types.CHAR: { return "Types.CHAR"; } - case Types.VARCHAR : + case Types.VARCHAR: { return "Types.VARCHAR"; } - case Types.LONGVARCHAR : + case Types.LONGVARCHAR: { return "Types.LONGVARCHAR"; } - case Types.DATE : + case Types.DATE: { return "Types.DATE"; } - case Types.TIME : + case Types.TIME: { return "Types.TIME"; } - case Types.TIMESTAMP : + case Types.TIMESTAMP: { return "Types.TIMESTAMP"; } - case Types.BINARY : + case Types.BINARY: { return "Types.BINARY"; } - case Types.VARBINARY : + case Types.VARBINARY: { return "Types.VARBINARY"; } - case Types.LONGVARBINARY : + case Types.LONGVARBINARY: { return "Types.LONGVARBINARY"; } - case Types.CLOB : + case Types.CLOB: { return "Types.CLOB"; } - case Types.BLOB : + case Types.BLOB: { return "Types.BLOB"; } - case Types.STRUCT : + case Types.STRUCT: { return "Types.STRUCT"; - } + } default : { return "UNKOWN Type"; } } } -} +} \ No newline at end of file 1.10 +52 -11 db-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java Index: ProxyHelper.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/ProxyHelper.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- ProxyHelper.java 4 May 2003 11:06:03 -0000 1.9 +++ ProxyHelper.java 25 May 2003 00:00:20 -0000 1.10 @@ -61,6 +61,7 @@ import com.develop.java.lang.reflect.Proxy; */ //#endif +import java.lang.reflect.Constructor; import java.util.List; import java.util.Set; @@ -74,8 +75,11 @@ import org.apache.ojb.broker.accesslayer.SetProxy; import org.apache.ojb.broker.query.Query; import org.apache.ojb.broker.core.PersistenceBrokerImpl; +import org.apache.ojb.broker.core.PersistenceBrokerConfiguration; import org.apache.ojb.broker.util.logging.Logger; import org.apache.ojb.broker.util.logging.LoggerFactory; +import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator; +import org.apache.ojb.broker.util.configuration.ConfigurationException; /** @@ -88,6 +92,44 @@ { private static Logger log = LoggerFactory.getLogger(ProxyHelper.class); + private static final Constructor listProxyConstructor; + private static final Constructor setProxyConstructor; + private static final Constructor collectionProxyConstructor; + private static Constructor collectionProxyConstructorUserDefined; + + static + { + Class paramType[] = {PBKey.class, Class.class, Query.class}; + try + { + listProxyConstructor = ListProxy.class.getConstructor(paramType); + setProxyConstructor = ListProxy.class.getConstructor(paramType); + collectionProxyConstructor = CollectionProxy.class.getConstructor(paramType); + } + catch (Throwable t) + { + throw new PersistenceBrokerException("Error accessing constructor for collection proxy class.", t); + } + + Class collectionProxyClass = null; + try + { + PersistenceBrokerConfiguration config = + (PersistenceBrokerConfiguration) OjbConfigurator.getInstance().getConfigurationFor(null); + + collectionProxyClass = config.getCollectionProxyClass(); + if(collectionProxyClass != null) + { + collectionProxyConstructorUserDefined = collectionProxyClass.getConstructor(paramType); + } + } + catch (Exception e) + { + throw new PersistenceBrokerException("Could not build constructor for collectionProxy class ["+ + collectionProxyClass+"] defined in configuration", e); + } + } + /** * Get the real Object * @@ -251,18 +293,17 @@ */ public static final ManageableCollection createCollectionProxy(PersistenceBrokerImpl broker, Query query, Class collectionClass) { - Class paramType[] = { PBKey.class, Class.class, Query.class }; Object param[] = { broker.getPBKey(), collectionClass, query }; - Class proxyClass = broker.getCollectionProxyClass(); - if (proxyClass == null) + Constructor proxyConstructor = collectionProxyConstructorUserDefined; + if (proxyConstructor == null) { - proxyClass = getCollectionProxyClass(collectionClass); + proxyConstructor = getCollectionProxyConstructor(collectionClass); } try { - return (ManageableCollection) proxyClass.getConstructor(paramType).newInstance(param); + return (ManageableCollection) proxyConstructor.newInstance(param); } catch (Throwable e) { @@ -275,19 +316,19 @@ * @param collectionClass * @return Class */ - private static Class getCollectionProxyClass(Class collectionClass) + private static Constructor getCollectionProxyConstructor(Class collectionClass) { if (List.class.isAssignableFrom(collectionClass)) { - return ListProxy.class; + return listProxyConstructor; } if (Set.class.isAssignableFrom(collectionClass)) { - return SetProxy.class; + return setProxyConstructor; } - return CollectionProxy.class; + return collectionProxyConstructor; } -} +} \ No newline at end of file