User: dsundstrom
  Date: 02/01/29 09:56:15

  Modified:    src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql
                        IdentifierManager.java SQLGenerator.java
  Log:
  Fixed bug where the alias for relation tables was not incorrectly stored
  in hashmap, so each time it was requested a new one was generated.
  Fixed bug that incorrectly generated the join for a relation table mapped
  relationship.
  
  Revision  Changes    Path
  1.3       +99 -27    
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/IdentifierManager.java
  
  Index: IdentifierManager.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/IdentifierManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IdentifierManager.java    2001/09/01 22:03:17     1.2
  +++ IdentifierManager.java    2002/01/29 17:56:15     1.3
  @@ -8,6 +8,9 @@
   import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCTypeMappingMetaData;
   
   public class IdentifierManager {
  +   /**
  +    * PathElement objects by dotted path string.
  +    */
      private final Map pathElements = new Hashtable();
      private final Map identifiersByPathElement = new Hashtable();
      private final Map tableAliases = new Hashtable();
  @@ -35,6 +38,11 @@
         aliasMaxLength = target.aliasMaxLength;
      }
   
  +   /**
  +    * Gets the abstract schema that has been assigned the specified identifier.
  +    * @param identifier the identifier that is used to search for the schema
  +    * @return the abstract schema if found; null otherwise
  +    */
      public AbstractSchema getAbstractSchema(String identifier) {
         PathElement pathElement = getPathElement(identifier);
         if(pathElement instanceof AbstractSchema) {
  @@ -43,17 +51,32 @@
         return null;
      }
   
  +   /**
  +    * Gets the existing abstract schema that has been assigned the 
  +    * specified identifier. If the schema is not found this method will throw
  +    * an IllegalArgumentException.
  +    * @param identifier the identifier that is used to search for the schema
  +    * @return the abstract schema
  +    * @throws IllegalArgumentException if the schema is not found
  +    */
      public AbstractSchema getExistingAbstractSchema(String identifier) {
         PathElement pathElement = getExistingPathElement(identifier);
         if(pathElement instanceof AbstractSchema) {
            return (AbstractSchema)pathElement;
         } else {
  -         throw new IllegalArgumentException("Path element with identifier is not an 
instance of AbstractSchema: " +
  -            " identifier=" + identifier + 
  -            ", pathElement=" + pathElement);
  +         throw new IllegalArgumentException("Path element with identifier " +
  +               "is not an instance of AbstractSchema: " +
  +               "identifier=" + identifier + 
  +               ", pathElement=" + pathElement);
         }
      }
   
  +   /**
  +    * Gets the cmr field identified by the specified path. If the path does 
  +    * not identify a cmr field, this function will return null.
  +    * @param path the path that is used to search for the cmr field
  +    * @return the cmr field if found; null otherwise
  +    */
      public CMRField getCMRField(String path) {
         PathElement pathElement = getPathElement(path);
         if(pathElement instanceof CMRField) {
  @@ -67,12 +90,19 @@
         if(pathElement instanceof CMRField) {
            return (CMRField)pathElement;
         } else {
  -         throw new IllegalArgumentException("Path element at path is not an 
instance of CMRField: " +
  -            " path=" + path + 
  -            ", pathElement=" + pathElement);
  +         throw new IllegalArgumentException("Path element at path is not " +
  +               "an instance of CMRField: " +
  +               "path=" + path + 
  +               ", pathElement=" + pathElement);
         }
      }
      
  +   /**
  +    * Gets the entity identified by the specified path. If the path does 
  +    * not identify an entity, this function will return null.
  +    * @param path the path that is used to search for the entity
  +    * @return the entity if found; null otherwise
  +    */
      public EntityPathElement getEntityPathElement(String path) {
         PathElement pathElement = getPathElement(path);
         if(pathElement instanceof EntityPathElement) {
  @@ -86,12 +116,19 @@
         if(pathElement instanceof EntityPathElement) {
            return (EntityPathElement)pathElement;
         } else {
  -         throw new IllegalArgumentException("Path element at path is not an 
instance of EntityPathElement: " +
  -            " path=" + path + 
  -            ", pathElement=" + pathElement);
  +         throw new IllegalArgumentException("Path element at path is not " +
  +               "an instance of EntityPathElement: " +
  +               "path=" + path + 
  +               ", pathElement=" + pathElement);
         }
      }
      
  +   /**
  +    * Gets the cmp field identified by the specified path. If the path does 
  +    * not identify a cmp field, this function will return null.
  +    * @param path the path that is used to search for the cmp field
  +    * @return the cmp field if found; null otherwise
  +    */
      public CMPField getCMPField(String path) {
         PathElement pathElement = getPathElement(path);
         if(pathElement instanceof CMPField) {
  @@ -105,12 +142,19 @@
         if(pathElement instanceof CMPField) {
            return (CMPField)pathElement;
         } else {
  -         throw new IllegalArgumentException("Path element at path is not an 
instance of CMPField: " +
  -            " path=" + path + 
  -            ", pathElement=" + pathElement);
  +         throw new IllegalArgumentException("Path element at path is not " +
  +               "an instance of CMPField: " +
  +               "path=" + path + 
  +               ", pathElement=" + pathElement);
         }
      }
      
  +   /**
  +    * Gets the path element identified by  the specified path. If the path is
  +    * unknown, this function will return null.
  +    * @param path the path that is used to search for the path element
  +    * @return the path element if found; null otherwise
  +    */
      public PathElement getPathElement(String path) {
         return (PathElement)pathElements.get(path);
      }
  @@ -123,33 +167,50 @@
         throw new IllegalArgumentException("Unknown path: "+path);
      }
      
  +   /**
  +    * Is the specified path known (i.e., has a path element been assigned to 
  +    * the path)?
  +    * @param path the path that is checked for an existing path element
  +    * @return true if a path element has been assigned to the 
  +    * path; flase otherwise
  +    */
      public boolean isKnownPath(String path) {
         return pathElements.containsKey(path);
      }
      
      public boolean isIdentifierRegistered(String identifier) {
         if(identifier.indexOf(".")>=0) {
  -         throw new IllegalArgumentException("identifier is a path not an 
identifier: " + identifier);
  +         throw new IllegalArgumentException("identifier is a path not an " +
  +               "identifier: " + identifier);
         }
         return pathElements.containsKey(identifier);
      }
      
  -   public void registerIdentifier(CMRField collectionValuedCMRField, String 
identifier) {
  +   public void registerIdentifier(
  +         CMRField collectionValuedCMRField, 
  +         String identifier) {
  +
         if(identifier.indexOf(".")>=0) {
  -         throw new IllegalArgumentException("identifier is a path not an 
identifier: " + identifier);
  +         throw new IllegalArgumentException("identifier is a path not an " +
  +               "identifier: " + identifier);
         }      
         if(collectionValuedCMRField == null) {
            throw new IllegalArgumentException("collectionValuedCMRField is null");
         }
         if(!collectionValuedCMRField.isCollectionValued()) {
  -         throw new IllegalArgumentException("collectionValuedCMRField is single 
valued");
  +         throw new IllegalArgumentException("collectionValuedCMRField is " +
  +               "single valued");
         }
         addMapping(identifier, collectionValuedCMRField);
      }
  +
  +   public void registerIdentifier(
  +         AbstractSchema abstractSchema,
  +         String identifier) {
   
  -   public void registerIdentifier(AbstractSchema abstractSchema, String identifier) 
{
         if(identifier.indexOf(".")>=0) {
  -         throw new IllegalArgumentException("identifier is a path not an 
identifier: " + identifier);
  +         throw new IllegalArgumentException("identifier is a path not an " +
  +               "identifier: " + identifier);
         }
         if(abstractSchema == null) {
            throw new IllegalArgumentException("abstractSchema is null");
  @@ -159,7 +220,8 @@
   
      public void registerPath(PathElement pathElement, String path) {
         if(path.indexOf(".")<0) {
  -         throw new IllegalArgumentException("path is an identifier not a path: " + 
path);
  +         throw new IllegalArgumentException("path is an identifier not " +
  +               "a path: " + path);
         }
         if(pathElement == null) {
            throw new IllegalArgumentException("pathElement is null");
  @@ -190,14 +252,16 @@
               buf.insert(0, getNextAliasHeader());
               // add the alias to the map
               String alias = buf.toString();
  -            alias = alias.substring(0, Math.min(aliasMaxLength, alias.length()));
  +            alias = alias.substring(0, 
  +                  Math.min(aliasMaxLength, alias.length()));
               tableAliases.put(pathElement, alias);
               
               return alias;
            } else if(pathElement instanceof AbstractSchema) {
               // when path element is an instance of abstract schema
               // we should get an identifier in the first iteration
  -            throw new IllegalStateException("No registered identifier for 
AbstractSchema: "+pathElement);
  +            throw new IllegalStateException("No registered identifier for " +
  +                  "AbstractSchema: "+pathElement);
            }
            
            // prepend the parent's name
  @@ -207,23 +271,31 @@
            parent = parent.getParent();
         }
         // should never happen...
  -      throw new IllegalStateException("No root identifier for path element: 
"+pathElement); 
  +      throw new IllegalStateException("No root identifier for path " +
  +            "element: "+pathElement); 
      }
      
      public String getRelationTableAlias(CMRField cmrField) {
         if(relationTableAliases.containsKey(cmrField)) {
  -         return (String)tableAliases.get(cmrField);
  +         return (String)relationTableAliases.get(cmrField);
         }
         
  -      String alias = getNextAliasHeader() + getTableAlias(cmrField.getParent()) + 
"_to_" + cmrField.getName();
  +      String alias = getNextAliasHeader() + 
  +            cmrField.getCMRFieldBridge().getRelationMetaData().getTableName();
  +            //getTableAlias(cmrField.getParent()) + "_to_" + cmrField.getName();
         alias = alias.substring(0, Math.min(aliasMaxLength, alias.length()));
  -      relationTableAliases.put(alias, cmrField);
  +      relationTableAliases.put(cmrField, alias);
         return alias;
      }
      
      private String getNextAliasHeader() {
  -      StringBuffer buf = new StringBuffer(aliasHeaderPrefix.length() + 2 + 
aliasHeaderSuffix.length());
  -      buf.append(aliasHeaderPrefix).append(aliasCount++).append(aliasHeaderSuffix);
  +      StringBuffer buf = new StringBuffer(
  +            aliasHeaderPrefix.length() + 2 + aliasHeaderSuffix.length());
  +
  +      buf.append(aliasHeaderPrefix);
  +      buf.append(aliasCount++);
  +      buf.append(aliasHeaderSuffix);
  +
         return buf.toString();
      }
      
  
  
  
  1.6       +28 -19    
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/SQLGenerator.java
  
  Index: SQLGenerator.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/SQLGenerator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SQLGenerator.java 2002/01/16 21:10:59     1.5
  +++ SQLGenerator.java 2002/01/29 17:56:15     1.6
  @@ -31,7 +31,8 @@
         String fromClause = getFromClause();
         String whereClause = getWhereClause(userWhereClause);
         
  -      StringBuffer buf = new 
StringBuffer(selectClause.length()+fromClause.length()+whereClause.length()+2);
  +      StringBuffer buf = new StringBuffer(
  +            selectClause.length()+fromClause.length()+whereClause.length()+2);
         buf.append(selectClause);
         buf.append(" ");
         buf.append(fromClause);
  @@ -54,7 +55,8 @@
            buf.append("DISTINCT ");
         }
         
  -      PathElement selectPathElement = idManager.getExistingPathElement(selectPath);
  +      PathElement selectPathElement = 
  +            idManager.getExistingPathElement(selectPath);
         if(selectPathElement instanceof AbstractSchema) {
            AbstractSchema schema = (AbstractSchema)selectPathElement;
            buf.append(getSelectClause(schema, readAhead));
  @@ -66,8 +68,9 @@
            buf.append(getSelectClause(cmpField));
         } else {
            // should never happen
  -         throw new IllegalStateException("Path element is instance of unknown type: 
" +
  -               "selectPath=" + selectPath + " selectPathElement=" + 
selectPathElement);
  +         throw new IllegalStateException("Path element is instance of " +
  +               "unknown type: selectPath=" + selectPath + 
  +               " selectPathElement=" + selectPathElement);
         }      
         return buf.toString();
      }
  @@ -111,7 +114,8 @@
   
      private String getSelectClause(CMPField cmpField) {
         String identifier = idManager.getTableAlias(cmpField.getParent());
  -      return SQLUtil.getColumnNamesClause(cmpField.getCMPFieldBridge(), identifier);
  +      return SQLUtil.getColumnNamesClause(
  +            cmpField.getCMPFieldBridge(), identifier);
      }
   
   
  @@ -120,10 +124,11 @@
   
         buf.append("FROM ");
   
  -      for(Iterator i = idManager.getUniqueEntityPathElements().iterator(); 
i.hasNext(); ) {
  -         EntityPathElement pathElement = (EntityPathElement)i.next();
  +      for(Iterator iter = idManager.getUniqueEntityPathElements().iterator(); 
  +            iter.hasNext(); ) {
  +         EntityPathElement pathElement = (EntityPathElement)iter.next();
            buf.append(getTableDeclarations(pathElement));
  -         if(i.hasNext()) {
  +         if(iter.hasNext()) {
               buf.append(", ");
            }
         }
  @@ -140,7 +145,8 @@
   
         if(pathElement instanceof CMRField) {
            CMRField cmrField = (CMRField)pathElement;
  -         JDBCRelationMetaData relationMetaData = 
cmrField.getCMRFieldBridge().getMetaData().getRelationMetaData();
  +         JDBCRelationMetaData relationMetaData = 
  +               cmrField.getCMRFieldBridge().getMetaData().getRelationMetaData();
            if(relationMetaData.isTableMappingStyle()) {
               buf.append(", ");
               buf.append(relationMetaData.getTableName());
  @@ -239,18 +245,20 @@
         } else {
            String relationAlias = idManager.getRelationTableAlias(cmrField);
   
  -         JDBCCMPFieldBridge fkField;
  -         JDBCCMPFieldBridge pkField;
  +         JDBCCMPFieldBridge parentField;
  +         JDBCCMPFieldBridge relationField;
  +         JDBCCMPFieldBridge childField;
   
  -         // parent has the foreign keys
  +         // parent to relation table join
            List parentFields = cmrFieldBridge.getTableKeyFields();
            for(Iterator iter = parentFields.iterator(); iter.hasNext(); ) {
   
  -            fkField = (JDBCCMPFieldBridge)iter.next();
  -            pkField = parent.getCMPFieldBridge(fkField.getFieldName());
  +            relationField = (JDBCCMPFieldBridge)iter.next();
  +            parentField = parent.getCMPFieldBridge(
  +                  relationField.getFieldName());
   
               buf.append(SQLUtil.getJoinClause(
  -                     pkField, parentAlias, fkField, relationAlias));
  +                     parentField, parentAlias, relationField, relationAlias));
   
               if(iter.hasNext()) {
                  buf.append(" AND ");
  @@ -260,14 +268,15 @@
            buf.append(" AND ");
   
            // parent has the foreign keys
  -         List childFields = cmrFieldBridge.getTableKeyFields();
  +         List childFields = relatedCMRFieldBridge.getTableKeyFields();
            for(Iterator iter = childFields.iterator(); iter.hasNext(); ) {
   
  -            fkField = (JDBCCMPFieldBridge)iter.next();
  -            pkField = relatedEntity.getCMPFieldByName(fkField.getFieldName());
  +            relationField = (JDBCCMPFieldBridge)iter.next();
  +            childField = relatedEntity.getCMPFieldByName(
  +                  relationField.getFieldName());
   
               buf.append(SQLUtil.getJoinClause(
  -                     pkField, childAlias, fkField, relationAlias));
  +                     childField, childAlias, relationField, relationAlias));
   
               if(iter.hasNext()) {
                  buf.append(" AND ");
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to