db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/odmg ContractVersionEffectivenessOQLTest.java
Date Sun, 27 Jul 2003 00:22:51 GMT
olegnitz    2003/07/26 17:22:51

  Modified:    src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java
               src/test/org/apache/ojb/odmg
                        ContractVersionEffectivenessOQLTest.java
  Log:
  Fixed SQL generation: for "related.pk" use the correspondent foreign key, don't add outer
join
  
  Revision  Changes    Path
  1.46      +53 -26    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.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- SqlQueryStatement.java	18 Jul 2003 17:20:52 -0000	1.45
  +++ SqlQueryStatement.java	27 Jul 2003 00:22:51 -0000	1.46
  @@ -158,20 +158,22 @@
               }
               else
               {
  -                tableAlias = getTableAlias(colName, useOuterJoins, aUserAlias); 
  +                tableAlias = getTableAlias(colName, useOuterJoins, aUserAlias);
                   if (tableAlias == null)
                   {
                       tableAlias = getRoot();
  -                }          
  +                }
               }
   */
           }
           else
           {
               String pathName = colName.substring(0, sp);
  -            String fieldName = colName.substring(sp + 1);
  +            String[] fieldNameRef = {colName.substring(sp + 1)};
  +
  +            tableAlias = getTableAlias(pathName, useOuterJoins, aUserAlias,
  +                                       fieldNameRef);
   
  -            tableAlias = getTableAlias(pathName, useOuterJoins, aUserAlias);
               /**
                * if we have not found an alias by the pathName or
                * aliasName (if given), try again because pathName
  @@ -184,22 +186,22 @@
                   /**
                    * pathName might be an alias, so check this first
                    */
  -                tableAlias = getTableAlias(null, useOuterJoins, pathName);
  -            }        
  -            
  +                tableAlias = getTableAlias(null, useOuterJoins, pathName, null);
  +            }
  +
               if (tableAlias != null)
               {
                   // correct column name to match the alias
                   // productGroup.groupName -> groupName
  -                pathInfo.column = fieldName;
  +                pathInfo.column = fieldNameRef[0];
               }
           }
  -        
  +
           result.tableAlias = tableAlias;
           result.pathInfo = pathInfo;
           return result;
       }
  -   
  +
       /**
        * Answer the column name for alias and path info<br>
        * if translate try to convert attribute name into column name otherwise use attribute
name<br>
  @@ -337,7 +339,7 @@
       {
           AttributeInfo attrInfo = getAttributeInfo(attr, useOuterJoins, aUserAlias);
           TableAlias tableAlias = attrInfo.tableAlias;
  -            
  +
           return appendColName(tableAlias, attrInfo.pathInfo, true, buf);
       }
   
  @@ -723,8 +725,13 @@
        * @param path the path from the target table of the query to this TableAlias.
        * @param useOuterJoins use outer join to join this table with the previous
        * table in the path.
  +     * @param aUserAlias
  +     * @param fieldRef String[1] contains the field name. 
  +     * In the case of related table's primary key the "related.pk" attribute
  +     * must not add new join, but use the value of foreign key
        */
  -    private TableAlias getTableAlias(String aPath, boolean useOuterJoins, String aUserAlias)
  +    private TableAlias getTableAlias(String aPath, boolean useOuterJoins,
  +                                     String aUserAlias, String[] fieldRef)
       {
           TableAlias curr, prev, indirect;
           String attr, attrPath = null;
  @@ -735,19 +742,20 @@
           Object[] keys = null;
           ArrayList descriptors;
           boolean outer = useOuterJoins;
  +        int pathLength;
   
  +        curr = getTableAliasForPath(aPath, aUserAlias);
   
  -        curr = getTableAliasForPath(aPath, aUserAlias);      
  -        
           if(curr != null)
           {
               return curr;
           }
  -        
  +
           descriptors = getRoot().cld.getAttributeDescriptorsForPath(aPath, getQuery().getPathClasses());
           prev = getRoot();
   
  -        for (int i = 0; i < descriptors.size(); i++)
  +        pathLength = descriptors.size();
  +        for (int i = 0; i < pathLength; i++)
           {
               if (!(descriptors.get(i) instanceof ObjectReferenceDescriptor))
               {
  @@ -809,17 +817,35 @@
   
                   prevKeys = ord.getForeignKeyFieldDescriptors(prev.cld);
                   keys = cld.getPkFields();
  +
  +                // [olegnitz]
  +                // a special case: the last element of the path is
  +                // reference and the field is one of PK fields =>
  +                // use the correspondent foreign key field, don't add the join
  +                if ((fieldRef != null) && (i == (pathLength - 1)))
  +                {
  +                    FieldDescriptor[] pk = cld.getPkFields();
  +
  +                    for (int j = 0; j < pk.length; j++)
  +                    {
  +                        if (pk[j].getAttributeName().equals(fieldRef[0]))
  +                        {
  +                            fieldRef[0] = ((FieldDescriptor) prevKeys[j]).getAttributeName();
  +                            return prev;
  +                        }
  +                    }
  +                }
               }
   
               curr = getTableAliasForPath(attrPath, aUserAlias, aPath);
  -            
  +
               if (curr == null)
               {
                   List hintClasses = (List) getQuery().getPathClasses().get(aPath);
  -                curr = createTableAlias(cld, attrPath, aUserAlias, aPath,hintClasses);
            
  -            
  +                curr = createTableAlias(cld, attrPath, aUserAlias, aPath,hintClasses);
  +
                   outer = outer || (curr.cld == prev.cld) || curr.hasExtents() || useOuterJoins;
  -                addJoin(prev, prevKeys, curr, keys, outer, attr);   
  +                addJoin(prev, prevKeys, curr, keys, outer, attr);
               }
               prev = curr;
           }
  @@ -828,14 +854,14 @@
   
       /**
        * add a join between two aliases
  -     * 
  +     *
        * no extents
        * A1   -> A2
  -     * 
  +     *
        * extents on the right
  -     * A1   -> A2 
  +     * A1   -> A2
        * A1   -> A2E0
  -     * 
  +     *
        * extents on the left : copy alias on right, extents point to copies
        * A1   -> A2
        * A1E0 -> A2C0 
  @@ -1296,7 +1322,8 @@
   
           if (sepPos >= 0)
           {
  -            getTableAlias(pathName.substring(0, sepPos), useOuterJoin, aUserAlias); 
  +            getTableAlias(pathName.substring(0, sepPos), useOuterJoin, aUserAlias,
  +                          new String[] {pathName.substring(sepPos + 1)});
           }
   
       }
  
  
  
  1.11      +1 -73     db-ojb/src/test/org/apache/ojb/odmg/ContractVersionEffectivenessOQLTest.java
  
  Index: ContractVersionEffectivenessOQLTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/ContractVersionEffectivenessOQLTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ContractVersionEffectivenessOQLTest.java	22 Jul 2003 00:33:00 -0000	1.10
  +++ ContractVersionEffectivenessOQLTest.java	27 Jul 2003 00:22:51 -0000	1.11
  @@ -255,78 +255,6 @@
           db.close();
       }
   
  -	 public void testComplexOQL3()
  -            throws Exception
  -    {
  -        /**
  -         * 1. create the objects with specific values we'll search on later.
  -         */
  -        Implementation odmg = OJB.getInstance();
  -        Database db = odmg.newDatabase();
  -        db.open(databaseName, Database.OPEN_READ_WRITE);
  -        Transaction tx = odmg.newTransaction();
  -        tx.begin();
  -
  -        Contract contract = new Contract();
  -        contract.setPk("C" + System.currentTimeMillis());
  -        contract.setContractValue1("version.contract.contractValue1.testComplexOQL");
  -        contract.setContractValue2(1);
  -        contract.setContractValue3("contractvalue3");
  -        contract.setContractValue4(new Timestamp(System.currentTimeMillis()));
  -        db.makePersistent(contract);
  -
  -        Version version = new Version();
  -        version.setPk("V" + System.currentTimeMillis());
  -        version.setVersionValue1("versionvalue1");
  -        version.setVersionValue2(1);
  -        version.setVersionValue3(new Timestamp(System.currentTimeMillis()));
  -        version.setContract(contract);
  -        db.makePersistent(version);
  -
  -        Effectiveness eff = new Effectiveness();
  -        eff.setPk("E" + System.currentTimeMillis());
  -        eff.setEffValue1("effValue1.testComplexOQL");
  -        eff.setEffValue2(20);
  -        eff.setEffValue3(new Timestamp(System.currentTimeMillis()));
  -        eff.setVersion(version);
  -        db.makePersistent(eff);
  -
  -        tx.commit();
  -        /**
  -         * 2. define the complex OQL query to find the object we created
  -         */
  -        String oql = "select s from " + org.apache.ojb.broker.Effectiveness.class.getName()
+ " where " +
  -                " version.contract.contractValue1=$1 and effValue1 = $2 and " +
  -                " (effValue3 > $3 or is_undefined(effValue3)) and " +
  -                " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and
" +
  -                " is_undefined(version.contract.relatedToContract)";
  -
  -        OQLQuery query = odmg.newOQLQuery();
  -        query.create(oql);
  -        query.bind("version.contract.contractValue1.testComplexOQL"); //version.contract.contractValue1=$1
  -        query.bind("effValue1.testComplexOQL"); // effValue1 = $2
  -        query.bind(new Timestamp(System.currentTimeMillis() - 5000)); // a while ago (effValue3
> $3)
  -        query.bind(new Integer(20)); // effValue2 <= $4
  -        query.bind(new Timestamp(System.currentTimeMillis() + 5000)); // a while from now
(effValue3<$5)
  -
  -        ManageableCollection all = (ManageableCollection) query.execute();
  -        java.util.Iterator it = all.ojbIterator();
  -        /**
  -         * make sure we got
  -         */
  -        int i = 0;
  -        while (it.hasNext())
  -        {
  -            it.next();
  -            i++;
  -        }
  -        if (i != 1)
  -        {
  -            fail("Should have found just one object, instead found " + i);
  -        }
  -        db.close();
  -    }
  -
       public void testGetWithVersionCriteria()
               throws Exception
       {
  @@ -772,4 +700,4 @@
       }
   
   
  -}
  \ No newline at end of file
  +}
  
  
  

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