/** * This method returns a simple count of the number of distinct objects which match the tags provided. * * @param editingContext the editing context to fetch into * @param tags the tags to search (String to tokenize, NSArray<String>, etc) * @param inclusion find matches for ANY tags or ALL tags provided * @return the count of distinct objects for the given tags */ public int countUniqueTaggedWith(EOEditingContext editingContext, ERTag.Inclusion inclusion, Object tags) { NSArray<String> tagNames = splitTagNames(tags); if (tagNames.count() == 0) { throw new IllegalArgumentException("No tags were passed in."); } EOQualifier qualifier = new ERXKey<ERTag>(_tagsRelationship.name()).append(ERTag.NAME).in(tagNames); EOFetchSpecification fetchSpec = new EOFetchSpecification(_entity.name(), qualifier, null); fetchSpec.setUsesDistinct(true); ERXSQLHelper sqlHelper = ERXSQLHelper.newSQLHelper(_entity.model()); EOSQLExpression sqlExpression = sqlHelper.sqlExpressionForFetchSpecification(editingContext, fetchSpec, 0, -1); sqlHelper.addGroupByClauseToExpression(editingContext, fetchSpec, sqlExpression); if (inclusion == ERTag.Inclusion.ALL) { sqlHelper.addHavingCountClauseToExpression(EOQualifier.QualifierOperatorEqual, tagNames.count(), sqlExpression); } int count = sqlHelper.rowCountForFetchSpecification(editingContext, fetchSpec); return count; }
EOQualifier qualifier = new ERXKey<ERTag>(_tagsRelationship.name()).append(ERTag.NAME).in(tagNames); if (additionalQualifier != null) { qualifier = ERXQ.and(qualifier, additionalQualifier);
EOQualifier tagNameQualifier = new ERXKey<ERTag>(_tagsRelationship.name()).append(ERTag.NAME).in(tagNames); EOFetchSpecification fetchSpec = new EOFetchSpecification(_entity.name(), tagNameQualifier, null); EOSQLExpression sqlExpression = sqlHelper.sqlExpressionForFetchSpecification(editingContext, fetchSpec, 0, -1, fetchAttributes); tagsFetchAttributes.addObject(countAttribute); EOQualifier idQualifier = new ERXKey<Object>("id").in(itemPrimaryKeys); EOFetchSpecification tagsFetchSpec = new EOFetchSpecification(_entity.name(), idQualifier, null); EOSQLExpression tagsSqlExpression = sqlHelper.sqlExpressionForFetchSpecification(editingContext, tagsFetchSpec, 0, -1, tagsFetchAttributes);