Skip to content

Commit

Permalink
Add flattened relationship support for many to many in ERXExistsQuali…
Browse files Browse the repository at this point in the history
…fier.

Fix date typos in comments.
  • Loading branch information
spelletier committed May 20, 2016
1 parent 41229d4 commit 60152b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ public ERXKeyValueQualifier containsObject(Object obj) {
* represented by this ERXKey.
*
* @author Samuel Pelletier
* @since May 56, 2016
* @since May 16, 2016
*/
public ERXExistsQualifier containsAnyOfTheseObjects(NSArray<? extends ERXGenericRecord> valueArray) {
ERXExistsQualifier existsQualifier = new ERXExistsQualifier(ERXQ.isIn(valueArray), _key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public static <T> ERXKeyComparisonQualifier equals(ERXKey<T> key, ERXKey<T> valu
* @return an EOQualifier
*
* @author Samuel Pelletier
* @since May 56, 2016
* @since May 16, 2016
*/
public static EOQualifier isIn(NSArray<? extends ERXGenericRecord> valueArray) {
NSMutableArray<EOQualifier> qualifiers = new NSMutableArray<>();
Expand All @@ -391,7 +391,7 @@ public static EOQualifier isIn(NSArray<? extends ERXGenericRecord> valueArray) {
* @return an EOQualifier
*
* @author Samuel Pelletier
* @since May 56, 2016
* @since May 16, 2016
*/
public static EOQualifier is(ERXGenericRecord value) {
EOEntity entity = value.entity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSMutableSet;

import er.extensions.eof.ERXKey;
import er.extensions.foundation.ERXArrayUtilities;
import er.extensions.foundation.ERXStringUtilities;
import er.extensions.qualifiers.ERXPrefixQualifierTraversal;

/**
* A qualifier that qualifies using an EXISTS clause.
Expand All @@ -51,7 +53,7 @@
*
* <code>select t0.ID, t0.ATT_1, ... t0.ATT_N from FIRST_TABLE t0 where EXISTS (select t1.ID from ANOTHER_TABLE where t1.ATT_1 = ? and t1.FIRST_TABLE_ID = t0.ID)</code>
*
* @author Travis Cripps, Aaron Rosenzweig
* @author Travis Cripps, Aaron Rosenzweig, Samuel Pelletier
*/
public class ERXExistsQualifier extends EOQualifier implements Cloneable, NSCoding, EOKeyValueArchiving {
/**
Expand Down Expand Up @@ -229,6 +231,23 @@ public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression e
}
}
}

// If this is a flattened relationship, we need to insert the middle hidden entity and adjust key paths
if (relationship != null && relationship.definition() != null) {
if (relationship.componentRelationships().count() != 2) {
throw new IllegalArgumentException("ERXExistsQualifier only support many to many flattened relationship.");
}
EORelationship innerToManyRelationship = (EORelationship) relationship.componentRelationships().objectAtIndex(0);
EORelationship innerToOneRelationship = (EORelationship) relationship.componentRelationships().objectAtIndex(1);

if (baseKeyPath != null) {
baseKeyPath = baseKeyPath.replaceAll(relationship.name()+"$", innerToManyRelationship.relationshipPath());
}
relationship = innerToManyRelationship;

ERXKey<Object> prefixKey = new ERXKey<>(innerToOneRelationship.relationshipPath());
subqualifier = prefixKey.prefix(subqualifier);
}

EOEntity srcEntity = relationship != null ? relationship.entity() : baseEntity;
EOEntity destEntity = relationship != null ? relationship.destinationEntity() : baseEntity;
Expand Down Expand Up @@ -276,7 +295,7 @@ public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression e

EOSQLExpression subExpression = factory.expressionForEntity(destEntity);
subExpression.setUseAliases(true);
subExpression.prepareSelectExpressionWithAttributes(destEntity.primaryKeyAttributes(), false, fetchSpecification);
subExpression.prepareSelectExpressionWithAttributes(relationship.destinationAttributes(), false, fetchSpecification);

for (Enumeration bindEnumeration = subExpression.bindVariableDictionaries().objectEnumerator(); bindEnumeration.hasMoreElements();) {
expression.addBindVariableDictionary((NSDictionary)bindEnumeration.nextElement());
Expand Down

0 comments on commit 60152b3

Please sign in to comment.