db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer RowReaderDefaultImpl.java
Date Sun, 12 Sep 2004 00:50:50 GMT
arminw      2004/09/11 17:50:50

  Modified:    src/java/org/apache/ojb/broker/accesslayer Tag:
                        OJB_1_0_RELEASE RowReaderDefaultImpl.java
  Log:
  apply a patch (+ some modification) by Luis Cruz
  
  <snip>
  As for the patch, when materializing a row from the database, we can
  just check the ojbConcreteClass whenever this is present and use it to
  determine the fields that need to be materialized. This helps
  performance whenever the ojbConcreteClass is present in the row of the
  resultSet and also avoids materializing every column of the table (which
  can lead to problems when field converters are used).
  </snip>
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.30.2.2  +65 -36    db-ojb/src/java/org/apache/ojb/broker/accesslayer/RowReaderDefaultImpl.java
  
  Index: RowReaderDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RowReaderDefaultImpl.java,v
  retrieving revision 1.30.2.1
  retrieving revision 1.30.2.2
  diff -u -r1.30.2.1 -r1.30.2.2
  --- RowReaderDefaultImpl.java	9 Aug 2004 07:51:26 -0000	1.30.2.1
  +++ RowReaderDefaultImpl.java	12 Sep 2004 00:50:49 -0000	1.30.2.2
  @@ -20,12 +20,10 @@
   import java.sql.SQLException;
   import java.util.Map;
   
  -import org.apache.ojb.broker.PBFactoryException;
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.util.ClassHelper;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
   
   /**
    * Default implementation of the {@link RowReader} interface.
  @@ -36,6 +34,10 @@
   public class RowReaderDefaultImpl implements RowReader
   {
       /**
  +     * Used as key in result set row map.
  +     */
  +    private static final String OJB_CONCRETE_CLASS_KEY = "ojbTemporaryNoneColumnKey";
  +    /**
        * represents a zero sized parameter array
        */
       private static final Object[] NO_ARGS = {};
  @@ -171,7 +173,28 @@
           }
           else
           {
  -            fields = m_cld.getRepository().getFieldDescriptorsForMultiMappedTable(m_cld);
  +            String ojbConcreteClass = extractOjbConcreteClass(m_cld, rs);
  +            /*
  +            arminw:
  +            if multiple classes were mapped to the same table, lookup the concrete
  +            class and use these fields, attach ojbConcreteClass in row map for later use
  +            */
  +            if(ojbConcreteClass != null)
  +            {
  +                row.put(OJB_CONCRETE_CLASS_KEY, ojbConcreteClass);
  +                ClassDescriptor cld = m_cld.getRepository().getDescriptorFor(ojbConcreteClass);
  +                fields = cld.getFieldDescriptions();
  +            }
  +            else
  +            {
  +                // fields = m_cld.getRepository().getFieldDescriptorsForMultiMappedTable(m_cld);
  +                /*
  +                arminw:
  +                should be valid to use fields of class-descriptor, because we handle ojbConcreteClass
  +                above, so a multi mapped table class-descriptor will never be used here
  +                */
  +                fields = m_cld.getFieldDescriptions();
  +            }
           }
           readValuesFrom(rs, row, fields);
       }
  @@ -210,47 +233,53 @@
           }
       }
   
  +    protected String extractOjbConcreteClass(ClassDescriptor cld, ResultSet rs)
  +    {
  +        FieldDescriptor fld = m_cld.getOjbConcreteClassField();
  +        if (fld == null)
  +        {
  +            return null;
  +        }
  +        try
  +        {
  +            String result = (String) fld.getJdbcType().getObjectFromColumn(rs, fld.getColumnName());
  +            if (result == null || result.trim().length() == 0)
  +            {
  +                throw new PersistenceBrokerException(
  +                        "ojbConcreteClass field for class " + cld.getClassNameOfObject()
  +                        + " returned null or 0-length string");
  +            }
  +            else
  +            {
  +                return result.trim();
  +            }
  +        }
  +        catch(SQLException e)
  +        {
  +            throw new PersistenceBrokerException("Unexpected error while try to read 'ojbConcretClass'"
+
  +                    " field from result set using column name " + fld.getColumnName() +
" main class" +
  +                    " was " + m_cld.getClassNameOfObject(), e);
  +        }
  +    }
  +
       /**
        * Check if there is an attribute which tells us which concrete class is to be instantiated.
        */
       protected ClassDescriptor selectClassDescriptor(Map row) throws PersistenceBrokerException
       {
  -        // check if there is an attribute which tells us which concrete class is to be
instantiated
  -        FieldDescriptor concreteClassFD = m_cld.getOjbConcreteClassField();
  -
  -        if (concreteClassFD == null)
  -            return m_cld;
  -        else
  +        ClassDescriptor result = m_cld;
  +        String ojbConcreteClass = (String) row.get(OJB_CONCRETE_CLASS_KEY);
  +        if(ojbConcreteClass != null)
           {
  -            try
  +            result = m_cld.getRepository().getDescriptorFor(ojbConcreteClass);
  +            // if we can't find class-descriptor for concrete class, something wrong with
mapping
  +            if (result == null)
               {
  -                String concreteClass = (String) row.get(concreteClassFD.getColumnName());
  -                if (concreteClass == null || concreteClass.trim().length() == 0)
  -                {
  -                    throw new PersistenceBrokerException(
  -                            "ojbConcreteClass field returned null or 0-length string");
  -                }
  -                else
  -                {
  -                    concreteClass = concreteClass.trim();
  -                }
  -                ClassDescriptor result = m_cld.getRepository().getDescriptorFor(concreteClass);
  -                if (result == null)
  -                {
  -                    LoggerFactory.getDefaultLogger().warn(
  -                            "[" + RowReaderDefaultImpl.class.getName()
  -                            + "] Can not find class-descriptor for ojbConcreteClass " +
concreteClass
  -                            + ", use given class-descriptor '" + m_cld.getClassNameOfObject()
  -                            + "' instead");
  -                    result = m_cld;
  -                }
  -                return result;
  -            }
  -            catch (PBFactoryException e)
  -            {
  -                throw new PersistenceBrokerException(e);
  +                throw new PersistenceBrokerException("Can't find class-descriptor for ojbConcreteClass
'"
  +                        + ojbConcreteClass + "', the main class was " + m_cld.getClassNameOfObject());
               }
           }
  +        return result;
       }
   
       public void setClassDescriptor(ClassDescriptor cld)
  
  
  

---------------------------------------------------------------------
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