Return-Path: Delivered-To: apmail-db-ojb-dev-archive@www.apache.org Received: (qmail 35791 invoked from network); 4 Sep 2003 13:53:03 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 4 Sep 2003 13:53:03 -0000 Received: (qmail 29777 invoked by uid 500); 4 Sep 2003 13:52:48 -0000 Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 29725 invoked by uid 500); 4 Sep 2003 13:52:47 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 29693 invoked by uid 500); 4 Sep 2003 13:52:47 -0000 Received: (qmail 29652 invoked from network); 4 Sep 2003 13:52:46 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 4 Sep 2003 13:52:46 -0000 Received: (qmail 35495 invoked by uid 1513); 4 Sep 2003 13:52:33 -0000 Date: 4 Sep 2003 13:52:33 -0000 Message-ID: <20030904135233.35494.qmail@minotaur.apache.org> From: brj@apache.org To: db-ojb-cvs@apache.org Subject: cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql SqlQueryStatement.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N brj 2003/09/04 06:52:33 Modified: src/java/org/apache/ojb/broker/accesslayer/sql SqlQueryStatement.java Log: also look for FieldDescriptor in super class (vertical inheritance) new solution by kurt hoehn Revision Changes Path 1.52 +76 -56 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.51 retrieving revision 1.52 diff -u -r1.51 -r1.52 --- SqlQueryStatement.java 29 Aug 2003 20:51:39 -0000 1.51 +++ SqlQueryStatement.java 4 Sep 2003 13:52:33 -0000 1.52 @@ -54,13 +54,7 @@ * . */ -import java.util.ArrayList; -import java.util.Collection; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import org.apache.ojb.broker.PersistenceBrokerSQLException; import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes; @@ -102,7 +96,7 @@ /** maps trees of joins to criteria */ private HashMap m_joinTreeToCriteria = new HashMap(); - private Platform pf; + private Platform m_platform; private ClassDescriptor m_baseCld; private ClassDescriptor m_searchCld; @@ -125,7 +119,7 @@ this.m_parentStatement = parent; this.m_query = (QueryByCriteria) query; this.m_columns = columns; - this.pf = pf; + this.m_platform = pf; this.m_searchCld = cld; if ((m_query == null) || (m_query.getBaseClass() == m_query.getSearchClass())) @@ -150,7 +144,7 @@ // Walk the super reference-descriptor - getReferenceCriteria( m_root, m_baseCld ); + buildSuperJoinTree( m_root, m_baseCld, "" ); // In some cases it is necessary to split the query criteria // and then to generate UNION of several SELECTs @@ -162,34 +156,6 @@ } - /** - * 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() { @@ -245,7 +211,9 @@ String pathName = colName.substring(0, sp); String[] fieldNameRef = { colName.substring(sp + 1)}; + // BRJ: // check if we refer to an attribute in the parent query + // this prefix is temporary ! if (pathName.equals("parentQuery") && m_parentStatement != null) { return m_parentStatement.getAttributeInfo(fieldNameRef[0], useOuterJoins, aUserAlias); @@ -334,7 +302,7 @@ } else { - // throw new IllegalArgumentException("Now Field found for : " + aPathInfo.column); + // throw new IllegalArgumentException("No Field found for : " + aPathInfo.column); result = aPathInfo.column; } } @@ -878,7 +846,7 @@ { columns = ((ReportQuery) subQuery).getColumns(); } - sql = new SqlSelectStatement(this ,pf, cld, columns, subQuery, m_logger).getStatement(); + sql = new SqlSelectStatement(this ,m_platform, cld, columns, subQuery, m_logger).getStatement(); } buf.append(" ("); @@ -886,6 +854,7 @@ buf.append(") "); } + /** * Get TableAlias by the path from the target table of the query. * @param path the path from the target table of the query to this TableAlias. @@ -920,6 +889,22 @@ descriptors = getRoot().cld.getAttributeDescriptorsForPath(aPath, getQuery().getPathClasses()); prev = getRoot(); + if( descriptors == null || descriptors.size() == 0 ) + { + if( prev.hasJoins() ) + { + for( Iterator itr = prev.iterateJoins(); itr.hasNext(); ) + { + prev = ((Join)itr.next()).left; + descriptors = prev.cld.getAttributeDescriptorsForPath(aPath, getQuery().getPathClasses()); + if( descriptors.size() > 0 ) + { + break; + } + } + } + } + pathLength = descriptors.size(); for (int i = 0; i < pathLength; i++) { @@ -956,24 +941,24 @@ } else { - String mnAttrPath = attrPath + "*"; + String mnAttrPath = attrPath + "*"; String mnPath = aPath + "*"; String mnUserAlias = (aUserAlias == null ? null : aUserAlias + "*"); - indirect = getTableAliasForPath(mnAttrPath, mnUserAlias, mnPath); - if (indirect == null) - { - indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath, mnUserAlias, mnPath); - - // we need two Joins for m:n - // 1.) prev class to indirectionTable - prevKeys = prev.cld.getPkFields(); + indirect = getTableAliasForPath(mnAttrPath, mnUserAlias, mnPath); + if (indirect == null) + { + indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath, mnUserAlias, mnPath); + + // we need two Joins for m:n + // 1.) prev class to indirectionTable + prevKeys = prev.cld.getPkFields(); keys = cod.getFksToThisClass(); - addJoin(prev, prevKeys, indirect, keys, outer, attr + "*"); - } + addJoin(prev, prevKeys, indirect, keys, outer, attr + "*"); + } // 2.) indirectionTable to the current Class - prev = indirect; - prevKeys = cod.getFksToItemClass(); - keys = cld.getPkFields(); + prev = indirect; + prevKeys = cod.getFksToItemClass(); + keys = cld.getPkFields(); } } else @@ -1012,9 +997,13 @@ outer = outer || (curr.cld == prev.cld) || curr.hasExtents() || useOuterJoins; addJoin(prev, prevKeys, curr, keys, outer, attr); + + buildSuperJoinTree( curr, cld, aPath ); } + prev = curr; } + return curr; } @@ -1510,6 +1499,37 @@ } /** + * build the Join-Information if a super reference exists + * + * @param left + * @param cld + * @param name + */ + protected void buildSuperJoinTree( TableAlias left, ClassDescriptor cld, String name ) + { + Iterator objRefs = cld.getObjectReferenceDescriptors().iterator(); + while( objRefs.hasNext() ) + { + ObjectReferenceDescriptor objRef = (ObjectReferenceDescriptor) objRefs.next(); + FieldDescriptor[] leftFields = objRef.getForeignKeyFieldDescriptors(cld); + + ClassDescriptor refCld = cld.getRepository().getDescriptorFor(objRef.getItemClassName()); + if (objRef.getPersistentField() instanceof AnonymousPersistentFieldForInheritance) + { + TableAlias base_alias = getTableAliasForPath( name, null ); + + String aliasName = String.valueOf(getAliasChar()) + m_aliasCount++; + TableAlias right = new TableAlias( refCld, aliasName, false, null ); + + Join join1to1 = new Join(left, leftFields, right, refCld.getPkFields(), false, "superClass"); + base_alias.addJoin( join1to1 ); + + buildSuperJoinTree( right, refCld, name); + } + } + } + + /** * First reduce the Criteria to the normal disjunctive form, then * calculate the necessary tree of joined tables for each item, then group * items with the same tree of joined tables. @@ -1600,7 +1620,7 @@ */ protected byte getJoinSyntaxType() { - return pf.getJoinSyntaxType(); + return m_platform.getJoinSyntaxType(); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org For additional commands, e-mail: ojb-dev-help@db.apache.org