* @return the number of rows
*/
public int rowCountForFetchSpecification(EOEditingContext ec, EOFetchSpecification spec) {
int rowCount = -1;
EOEntity entity = ERXEOAccessUtilities.entityNamed(ec, spec.entityName());
EOModel model = entity.model();
NSArray result = null;
String sql;
if (spec.hints() == null || spec.hints().isEmpty() || spec.hints().valueForKey(EODatabaseContext.CustomQueryExpressionHintKey) == null) {
// no hints
if (spec.fetchLimit() > 0 || spec.sortOrderings() != null) {
boolean usesDistinct = spec.usesDistinct();
spec = new EOFetchSpecification(spec.entityName(), spec.qualifier(), null);
spec.setUsesDistinct(usesDistinct);
}
EOSQLExpression sqlExpression = sqlExpressionForFetchSpecification(ec, spec, 0, -1);
String statement = sqlExpression.statement();
String listString = sqlExpression.listString();
String countExpression;
if (spec.usesDistinct()) {
countExpression = sqlForCountDistinct(entity);
} else {
countExpression = "count(*) ";
}
statement = statement.replace(listString, countExpression);
sqlExpression.setStatement(statement);
sql = statement;
result = ERXEOAccessUtilities.rawRowsForSQLExpression(ec, model.name(), sqlExpression);
}
else {
// we have hints
Object hint = spec.hints().valueForKey(EODatabaseContext.CustomQueryExpressionHintKey);
sql = ERXSQLHelper.newSQLHelper(model).customQueryExpressionHintAsString(hint);
// MS: This looks super sketchy ...
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1);
}
sql = "select count(*) from " + sqlForSubquery(sql, "result_count_temp_table");
result = EOUtilities.rawRowsForSQL(ec, model.name(), sql, null);
}
if (result.count() > 0) {
NSDictionary dict = (NSDictionary) result.objectAtIndex(0);
NSArray values = dict.allValues();