if(spec.fetchLimit() > 0 || spec.sortOrderings() != null) { spec = new EOFetchSpecification(spec.entityName(), spec.qualifier(), null); }
    public static int rowCountForFetchSpecification(EOEditingContext ec, EOFetchSpecification spec) {        int rowCount = -1;        EOEntity entity = ERXEOAccessUtilities.entityNamed(ec, spec.entityName());        EOModel model = entity.model();        EOSQLExpression sql = ERXEOAccessUtilities.sqlExpressionForFetchSpecification(ec, spec, 0, -1);        String statement = sql.statement();        int index = statement.toLowerCase().indexOf(" from ");        statement = "select count(*) " + statement.substring(index, statement.length());        sql.setStatement(statement);        NSArray result = ERXEOAccessUtilities.rawRowsForSQLExpression(ec, model.name(), sql);        if (result.count() > 0) {            NSDictionary dict = (NSDictionary) result.objectAtIndex(0);            NSArray values = dict.allValues();            if (values.count() > 0) {                Object value = values.objectAtIndex(0);                if (value instanceof Number) {                    return ((Number) value).intValue();                } else {                    try {                        int c = Integer.parseInt(value.toString());                        rowCount = c;                    } catch (NumberFormatException e) {                        throw new IllegalStateException("sql " + sql + " returned a wrong result, could not convert " + value                                + " into an int!");                    }                }            } else {                throw new IllegalStateException("sql " + sql + " returned no result!");            }        } else {            throw new IllegalStateException("sql " + sql + " returned no result!");        }        return rowCount;    }