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/test/org/apache/ojb/broker InheritanceMultipleTableTest.java
Date Sat, 28 May 2005 10:37:15 GMT
brj         2005/05/28 03:37:15

  Modified:    src/test/org/apache/ojb Tag: OJB_1_0_RELEASE
                        repository_junit_inheritance.xml
               src/java/org/apache/ojb/broker/accesslayer/sql Tag:
                        OJB_1_0_RELEASE SqlQueryStatement.java
               src/test/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        InheritanceMultipleTableTest.java
  Log:
  use outer join hint when building the super-join-tree
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.5   +13 -0     db-ojb/src/test/org/apache/ojb/Attic/repository_junit_inheritance.xml
  
  Index: repository_junit_inheritance.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/Attic/repository_junit_inheritance.xml,v
  retrieving revision 1.1.2.4
  retrieving revision 1.1.2.5
  diff -u -r1.1.2.4 -r1.1.2.5
  --- repository_junit_inheritance.xml	7 May 2005 16:02:57 -0000	1.1.2.4
  +++ repository_junit_inheritance.xml	28 May 2005 10:37:15 -0000	1.1.2.5
  @@ -68,6 +68,19 @@
           <!-- only for testing we use the second PK in base class Employee as FK  -->
           <inverse-foreignkey field-ref="id_2"/>
       </collection-descriptor>
  +
  +    <collection-descriptor
  +        name="executives"
  +        element-class-ref="org.apache.ojb.broker.InheritanceMultipleTableTest$Executive"
  +        proxy="false"
  +        auto-retrieve="true"
  +        auto-update="object"
  +        auto-delete="object"
  +    >
  +        <!-- only for testing we use the second PK in base class Manager as FK  -->
  +        <inverse-foreignkey field-ref="id_2"/>
  +    </collection-descriptor>
  +
   </class-descriptor>
   
   
  
  
  
  No                   revision
  No                   revision
  1.75.2.6  +34 -8     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.75.2.5
  retrieving revision 1.75.2.6
  diff -u -r1.75.2.5 -r1.75.2.6
  --- SqlQueryStatement.java	22 Apr 2005 17:08:13 -0000	1.75.2.5
  +++ SqlQueryStatement.java	28 May 2005 10:37:15 -0000	1.75.2.6
  @@ -134,7 +134,7 @@
           }
   
           // Walk the super reference-descriptor
  -        buildSuperJoinTree(m_root, m_baseCld, "");
  +        buildSuperJoinTree(m_root, m_baseCld, "" ,false);
   
           // In some cases it is necessary to split the query criteria
           // and then to generate UNION of several SELECTs
  @@ -1105,7 +1105,10 @@
   			    // BRJ : if ord is taken from 'super' we have to change prev accordingly
   				if (!prev.cld.equals(ord.getClassDescriptor()))
   				{
  -				    prev = getTableAliasForClassDescriptor(ord.getClassDescriptor());
  +					TableAlias ordAlias = getTableAliasForClassDescriptor(ord.getClassDescriptor());
  +					Join join = prev.getJoin(ordAlias);
  +					join.isOuter = join.isOuter || outer;
  +				    prev = ordAlias;
   				}	
   
   				prevKeys = ord.getForeignKeyFieldDescriptors(prev.cld);
  @@ -1141,7 +1144,7 @@
   				outer = outer || (curr.cld == prev.cld) || curr.hasExtents() || useOuterJoins;
   				addJoin(prev, prevKeys, curr, keys, outer, attr);
   
  -				buildSuperJoinTree(curr, cld, aPath);
  +				buildSuperJoinTree(curr, cld, aPath, outer);
   			}
   
   			prev = curr;
  @@ -1654,7 +1657,7 @@
        * @param cld
        * @param name
        */
  -    protected void buildSuperJoinTree(TableAlias left, ClassDescriptor cld, String name)
  +    protected void buildSuperJoinTree(TableAlias left, ClassDescriptor cld, String name,
boolean useOuterJoin)
       {
           Iterator objRefs = cld.getObjectReferenceDescriptors().iterator();
           while (objRefs.hasNext())
  @@ -1668,12 +1671,12 @@
                   TableAlias base_alias = getTableAliasForPath(name, null);
   
                   String aliasName = String.valueOf(getAliasChar()) + m_aliasCount++;
  -                TableAlias right = new TableAlias(refCld, aliasName, false, null);
  +                TableAlias right = new TableAlias(refCld, aliasName, useOuterJoin, null);
   
                   Join join1to1 = new Join(left, leftFields, right, refCld.getPkFields(),
false, "superClass");
                   base_alias.addJoin(join1to1);
   
  -                buildSuperJoinTree(right, refCld, name);
  +                buildSuperJoinTree(right, refCld, name, useOuterJoin);
               }
           }
       }
  @@ -1946,6 +1949,29 @@
               return (joins != null);
           }
   
  +        /**
  +         * Get the Join ponting to anAlias.
  +         * @param anAlias
  +         * @return
  +         */
  +        Join getJoin(TableAlias anAlias)
  +        {
  +        	Join result = null;
  +        	
  +        	Iterator iter = joins.iterator() ;
  +        	while (iter.hasNext())
  +        	{
  +        		Join join = (Join) iter.next();
  +        		if (join.right.equals(anAlias))
  +        		{
  +        			result = join;
  +        			break;
  +        		}
  +        	}
  +        	
  +        	return result;
  +        }
  +        
           public String toString()
           {
               StringBuffer sb = new StringBuffer(1024);
  @@ -2000,7 +2026,7 @@
           final String[] leftKeys;
           final TableAlias right;
           final String[] rightKeys;
  -        final boolean isOuter;
  +        boolean isOuter;
           /** This is the name of the field corresponding to this join */
           final String name;
   
  
  
  
  No                   revision
  No                   revision
  1.7.2.8   +95 -23    db-ojb/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java
  
  Index: InheritanceMultipleTableTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java,v
  retrieving revision 1.7.2.7
  retrieving revision 1.7.2.8
  diff -u -r1.7.2.7 -r1.7.2.8
  --- InheritanceMultipleTableTest.java	7 May 2005 16:02:57 -0000	1.7.2.7
  +++ InheritanceMultipleTableTest.java	28 May 2005 10:37:15 -0000	1.7.2.8
  @@ -3,6 +3,7 @@
   import java.io.Serializable;
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.Collections;
   import java.util.Iterator;
   import java.util.List;
   
  @@ -10,9 +11,11 @@
   import org.apache.commons.lang.builder.EqualsBuilder;
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
  +import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.junit.PBTestCase;
   
  @@ -151,26 +154,7 @@
           long timestamp = System.currentTimeMillis();
           Long id_2 = new Long(timestamp);
           String name = "testInheritancedObjectsInCollectionReferences_" + timestamp;
  -        Manager m_1 = new Manager(id_2, name + "_manager_1");
  -        Manager m_2 = new Manager(id_2, name + "_manager_2");
  -        Manager m_3 = new Manager(id_2, name + "_manager_3");
  -        m_3.setDepartment("none");
  -        Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null);
  -        Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null);
  -        Employee em = new Employee(id_2, name + "_employee");
  -
  -        ArrayList employees = new ArrayList();
  -        employees.add(m_1);
  -        employees.add(m_2);
  -        employees.add(m_3);
  -        employees.add(ex_1);
  -        employees.add(ex_2);
  -        employees.add(em);
  -
  -        Company company = new Company(id_2, name, employees);
  -        broker.beginTransaction();
  -        broker.store(company);
  -        broker.commitTransaction();
  +        Company company = prepareTestDataWithCompany(id_2, name);
   
           broker.clearCache();
           Criteria crit = new Criteria();
  @@ -181,7 +165,12 @@
           Company newCompany = (Company) result.iterator().next();
           List newEmployees = newCompany.getEmployees();
           assertNotNull(newEmployees);
  -        assertEquals(employees.size(), newEmployees.size());
  +        assertEquals(company.getEmployees().size(), newEmployees.size());
  +        
  +        List newExecutives = newCompany.getExecutives();
  +        assertNotNull(newExecutives);
  +        assertEquals(company.getExecutives().size(), newExecutives.size());
  +
           int countEmployee = 0;
           int countExecutive = 0;
           int countManager = 0;
  @@ -556,6 +545,34 @@
           broker.commitTransaction();
       }
   
  +	private Company prepareTestDataWithCompany(Long id_2, String name) {
  +		Manager m_1 = new Manager(id_2, name + "_manager_1");
  +        Manager m_2 = new Manager(id_2, name + "_manager_2");
  +        Manager m_3 = new Manager(id_2, name + "_manager_3");
  +        m_3.setDepartment("none");
  +        Executive ex_1 = new Executive(id_2, name + "_executive", "department_1", null);
  +        Executive ex_2 = new Executive(id_2, name + "_executive", "department_1", null);
  +        Employee em = new Employee(id_2, name + "_employee");
  +
  +        ArrayList employees = new ArrayList();
  +        employees.add(m_1);
  +        employees.add(m_2);
  +        employees.add(m_3);
  +        employees.add(ex_1);
  +        employees.add(ex_2);
  +        employees.add(em);
  +
  +        ArrayList executives = new ArrayList();
  +        executives.add(ex_1);
  +        executives.add(ex_2);
  +        
  +        Company company = new Company(id_2, name, employees, executives);
  +        broker.beginTransaction();
  +        broker.store(company);
  +        broker.commitTransaction();
  +		return company;
  +	}
  +    
       public void testQuery_InheritedObjects()
       {
           long timestamp = System.currentTimeMillis();
  @@ -693,6 +710,49 @@
           assertEquals(1, result.size());
       }
   
  +  public void testQuery_ReferenceOuterJoin()
  +    {
  +        long timestamp = System.currentTimeMillis();
  +        Long id_2 = new Long(timestamp);
  +        String name = "testQuery_ReferenceOuterJoin" + timestamp;
  +        Company comp = prepareTestDataWithCompany(id_2, name);
  +        
  +        // Store a dummy company
  +        Company dummyComp = new Company(new Long(System.currentTimeMillis()), name + "_dummy",

  +        		Collections.EMPTY_LIST, Collections.EMPTY_LIST);
  +        broker.beginTransaction();
  +        broker.store(dummyComp);
  +        broker.commitTransaction();
  +        
  +        broker.clearCache();
  +
  +        Criteria crit = new Criteria();
  +        crit.addLike("name", name + "%");
  +        QueryByCriteria query = QueryFactory.newQuery(Company.class, crit, true);
  +        Collection result = broker.getCollectionByQuery(query);
  +        assertEquals(2, result.size());								// retrieve both companies
  +        
  +        crit = new Criteria();
  +        crit.addLike("name", name + "%");
  +        
  +        Criteria nameCrit1 = new Criteria();
  +        nameCrit1.addLike("executives.name", name + "%");
  +        Criteria nameCrit2 = new Criteria();
  +        nameCrit2.addIsNull("executives.name");
  +        nameCrit1.addOrCriteria(nameCrit2);
  +        crit.addAndCriteria(nameCrit1);
  +        
  +        query = QueryFactory.newQuery(Company.class, crit, true);
  +        query.addOrderByAscending("id");
  +        query.setPathOuterJoin("executives");	
  +        
  +        SqlGenerator sqlg = broker.serviceSqlGenerator();
  +        String sql = sqlg.getPreparedSelectStatement(query, broker.getClassDescriptor(Company.class));
  +
  +        result = broker.getCollectionByQuery(query);
  +        assertEquals(2, result.size());								// should retrieve both companies
  +     }
  +    
       public void testInsertQuery()
       {
           long timestamp = System.currentTimeMillis();
  @@ -1126,16 +1186,18 @@
           private Long id;
           private String name;
           private List  employees;
  +        private List  executives;
   
           public Company()
           {
           }
   
  -        public Company(Long id, String name, List employees)
  +        public Company(Long id, String name, List employees,List executives)
           {
               this.id = id;
               this.name = name;
               this.employees = employees;
  +            this.executives = executives;
           }
   
           public Long getId()
  @@ -1167,6 +1229,16 @@
           {
               this.employees = employees;
           }
  +
  +		public List getExecutives()
  +		{
  +			return executives;
  +		}
  +
  +		public void setExecutives(List executives)
  +		{
  +			this.executives = executives;
  +		}
       }
   
       public static class Consortium
  
  
  

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