db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql SqlQueryStatement.java
Date Fri, 29 Aug 2003 20:51:39 GMT
brj         2003/08/29 13:51:39

  Modified:    src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java
  Log:
  also look for FieldDescriptor in super class (vertical inheritance)
  solution by kurt hoehn
  
  Revision  Changes    Path
  1.51      +143 -28   db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java
  
  Index: SqlQueryStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- SqlQueryStatement.java	17 Aug 2003 08:49:31 -0000	1.50
  +++ SqlQueryStatement.java	29 Aug 2003 20:51:39 -0000	1.51
  @@ -70,6 +70,7 @@
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.FieldHelper;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  +import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentFieldForInheritance;
   import org.apache.ojb.broker.platforms.Platform;
   import org.apache.ojb.broker.query.*;
   import org.apache.ojb.broker.util.SqlHelper;
  @@ -105,6 +106,8 @@
       private ClassDescriptor m_baseCld;
       private ClassDescriptor m_searchCld;
   
  +    private int m_aliasCount = 0;
  +
   
       /**
        * Constructor for SqlCriteriaStatement.
  @@ -124,6 +127,7 @@
           this.m_columns = columns;
           this.pf = pf;
           this.m_searchCld = cld;
  +
           if ((m_query == null) || (m_query.getBaseClass() == m_query.getSearchClass()))
           {
               this.m_baseCld = m_searchCld;
  @@ -132,7 +136,9 @@
           {
               this.m_baseCld = cld.getRepository().getDescriptorFor(query.getBaseClass());
           }
  +
           this.m_root = createTableAlias(m_baseCld, null, "");
  +
           if (m_searchCld == m_baseCld)
           {
               this.m_search = m_root;
  @@ -142,6 +148,10 @@
               this.m_search = getTableAlias(m_query.getObjectProjectionAttribute(), false,
null, null);
           }
   
  +
  +        // Walk the super reference-descriptor
  +        getReferenceCriteria( m_root, m_baseCld );
  +
           // In some cases it is necessary to split the query criteria
           // and then to generate UNION of several SELECTs
           // We build the joinTreeToCriteria mapping,
  @@ -151,6 +161,36 @@
           }
       }
   
  +
  +    /**
  +     * Walks the class hierarchy based on the super reference-descriptor in the class-descriptor
  +     *
  +     * @param left
  +     * @param cld
  +     */
  +    protected void getReferenceCriteria(TableAlias left, ClassDescriptor cld)
  +    {
  +        Iterator objRefs = cld.getObjectReferenceDescriptors().iterator();
  +
  +        while (objRefs.hasNext())
  +        {
  +            ObjectReferenceDescriptor superRef = (ObjectReferenceDescriptor) objRefs.next();
  +
  +//            if( superRef.getPersistentField().getName() == null )
  +            if (superRef.getPersistentField() instanceof AnonymousPersistentFieldForInheritance)
  +            {
  +                ClassDescriptor superCld = cld.getRepository().getDescriptorFor(superRef.getItemClassName());
  +                FieldDescriptor[] leftFields = superRef.getForeignKeyFieldDescriptors(cld);
  +
  +                TableAlias right = createTableAlias(superCld, null, superRef.getItemClassName());
  +                Join join1to1 = new Join(left, leftFields, right, superCld.getPkFields(),
false, "superClass");
  +                left.addJoin(join1to1);
  +
  +                getReferenceCriteria(right, superCld);
  +            }
  +        }
  +    }
  +
       protected ClassDescriptor getBaseClassDescriptor()
       {
           return m_baseCld;
  @@ -264,7 +304,29 @@
   
   			if (fld != null)
   			{
  -				result = aTableAlias.alias + "." + fld.getColumnName();
  +                // added to suport the super reference descriptor
  +                if( fld.getClassDescriptor().getFullTableName() != aTableAlias.table )
  +                {
  +                    Iterator itr = aTableAlias.joins.iterator();
  +                    while( itr.hasNext() )
  +                    {
  +                        Join join = (Join) itr.next();
  +                        if( join.right.table == fld.getClassDescriptor().getFullTableName()
)
  +                        {
  +                            result = join.right.alias + "." + fld.getColumnName();
  +                            break;
  +                        }
  +                    }
  +
  +                    if( result == null )
  +                    {
  +                        result = aPathInfo.column;
  +                    }
  +                }
  +                else
  +                {
  +				    result = aTableAlias.alias + "." + fld.getColumnName();
  +                }
   			}
               else if ("*".equals(aPathInfo.column))
               {
  @@ -335,36 +397,88 @@
   				ObjectReferenceDescriptor ord = aTableAlias.cld.getObjectReferenceDescriptorByName(colName);
   				if (ord != null)
   				{
  -					if (aTableAlias == getRoot())
  -					{
  -						// no path expression
  -						FieldDescriptor[] fk = ord.getForeignKeyFieldDescriptors(aTableAlias.cld);
  -						if (fk.length > 0)
  -						{
  -							fld = fk[0];
  -						}
  -					}
  -					else
  -					{
  -						// attribute with path expression
  -						/**
  -						 * MBAIRD
  -						 * potentially people are referring to objects, not to the object's primary key,
and then we need to take the
  -						 * primary key attribute of the referenced object to help them out.
  -						 */
  -						ClassDescriptor cld = aTableAlias.cld.getRepository().getDescriptorFor(ord.getItemClass());
  -						if (cld != null)
  -						{
  -							fld = aTableAlias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
  -						}
  -					}
  +					fld = getFldFromReference(aTableAlias, ord);
   				}
  +                else
  +                {
  +                    fld = getFldFromJoin(aTableAlias, colName);
  +                }
   			}
   		}
   
   		return fld;
   	}
  -    
  +
  +    /**
  +     * get FieldDescriptor from joined superclass
  +     * @param aTableAlias
  +     * @param aColName
  +     * @return
  +     */
  +    private FieldDescriptor getFldFromJoin(TableAlias aTableAlias,  String aColName)
  +    {
  +        FieldDescriptor fld = null;
  +        
  +        // Search Join Structure for attribute
  +        if( aTableAlias.joins != null )
  +        {
  +            Iterator itr = aTableAlias.joins.iterator();
  +            while( itr.hasNext() )
  +            {
  +                Join join = (Join) itr.next();
  +                ClassDescriptor cld = join.right.cld;
  +
  +                if (cld != null)
  +                {
  +                    fld = cld.getFieldDescriptorByName( aColName );
  +                    if( fld != null )
  +                    {
  +                        break;
  +                    }
  +                    
  +                }        
  +            }
  +        }
  +        return fld;
  +    }
  +
  +    /**
  +     * Get FieldDescriptor from Reference
  +     * @param aTableAlias
  +     * @param anOrd
  +     * @return
  +     */
  +    private FieldDescriptor getFldFromReference(TableAlias aTableAlias, ObjectReferenceDescriptor
anOrd)
  +    {
  +        FieldDescriptor fld = null;
  +        
  +        if (aTableAlias == getRoot())
  +        {
  +        	// no path expression
  +        	FieldDescriptor[] fk = anOrd.getForeignKeyFieldDescriptors(aTableAlias.cld);
  +        	if (fk.length > 0)
  +        	{
  +        		fld = fk[0];
  +        	}
  +        }
  +        else
  +        {
  +        	// attribute with path expression
  +        	/**
  +        	 * MBAIRD
  +        	 * potentially people are referring to objects, not to the object's primary key,
and then we need to take the
  +        	 * primary key attribute of the referenced object to help them out.
  +        	 */
  +        	ClassDescriptor cld = aTableAlias.cld.getRepository().getDescriptorFor(anOrd.getItemClass());
  +        	if (cld != null)
  +        	{
  +        		fld = aTableAlias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
  +        	}
  +        }
  +        
  +        return fld;
  +    }
  +
       /**
        * Answer the appropriate ColumnName <br>
        * if a FIELDDESCRIPTOR is found for the Criteria the colName is taken from
  @@ -1019,9 +1133,10 @@
               lookForExtents = true;
           } 
           
  -        String aliasName = String.valueOf(getAliasChar()) + m_pathToAlias.size();
  +        String aliasName = String.valueOf(getAliasChar()) + m_aliasCount++; // m_pathToAlias.size();
           alias = new TableAlias(cld, aliasName, lookForExtents,hints);
  -        m_pathToAlias.put(path, alias);
  +
  +        m_pathToAlias.put(path, alias );
           return alias;
       }
   
  @@ -1059,7 +1174,7 @@
               getLogger().warn("Creating TableAlias without table for path: " + path);
           }
   
  -        String aliasName = String.valueOf(getAliasChar()) + m_pathToAlias.size();
  +        String aliasName = String.valueOf(getAliasChar()) + m_aliasCount++;  // + m_pathToAlias.size();
           alias = new TableAlias(table, aliasName);
           m_pathToAlias.put(path, alias);
   
  
  
  

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