NSArray attributesFromEntity = entity.attributesToFetch();   EOSQLExpression ex = sqlFactory.expressionForEntity(entity); ex.setUseAliases(true); ex.setUseBindVariables(true); ex.prepareSelectExpressionWithAttributes(attributesFromEntity, false, spec); System.out.println(" ex.statement():" +  ex.statement());      EOSQLExpression sqlExpr = sqlFactory.selectStatementForAttributes(attributesFromEntity, false, spec, entity);
NSArray attributes = spec.rawRowKeyPaths(); if(attributes == null || attributes.count() == 0) attributes = entity.attributesToFetch(); EOSQLExpression sqlExpr = sqlFactory.selectStatementForAttributes(attributes, false, spec, entity);
    public static EOSQLExpression sqlExpressionForFetchSpecificationAndEditingContext(EOFetchSpecification spec, EOEditingContext ec, long start, long end) {        EOModel model = modelForFetchSpecificationAndEditingContext(spec, ec);        EOEntity entity = model.entityNamed(spec.entityName());        EOAdaptor adaptor = EOAdaptor.adaptorWithModel(model);        EODatabase db = new EODatabase(adaptor);        EOSQLExpressionFactory sqlFactory = adaptor.expressionFactory();        //NSArray attributes = spec.rawRowKeyPaths();        NSArray attributesFromEntity = entity.attributesToFetch();        /*EOQualifier qualifier = spec.qualifier();        if (qualifier != null)            qualifier = EOQualifierSQLGeneration.Support._schemaBasedQualifierWithRootEntity(qualifier, entity.rootEntity());        if (qualifier != spec.qualifier()) {            spec = (EOFetchSpecification) spec.clone();            spec.setQualifier(qualifier);        }*/        EOSQLExpression ex = sqlFactory.expressionForEntity(entity);        ex.setUseAliases(true);        ex.setUseBindVariables(true);        ex.prepareSelectExpressionWithAttributes(attributesFromEntity, false, spec);        System.out.println(" ex.statement():" +  ex.statement());        //EOSQLExpression expression=factory.selectStatementForAttributes(entity.primaryKeyAttributes(),        //                                   false,        //                                   fs,        //                                   entity);        EOSQLExpression sqlExpr = sqlFactory.selectStatementForAttributes(attributesFromEntity, false, spec, entity);        String sql = sqlExpr.statement();        if(end >= 0) {            String url = (String)model.connectionDictionary().objectForKey("URL");            if(url != null) {                if(url.toLowerCase().indexOf("frontbase") != -1) {                    //add TOP(start, (end - start)) after the SELECT word                    int index = sql.indexOf("select");                    if (index == -1) {                        index = sql.indexOf("SELECT");                    }                    index += 6;                    //FIXME: this works for frontbase, might need to be adjusted for other db servers!                    StringBuffer buf = new StringBuffer();                    buf.append(sql.substring(0, index)).append(" TOP(").append(start).append(",").append(end - start).append(") ").append(sql.substring(index + 1, sql.length()));                    sql = buf.toString();                } else if(url.toLowerCase().indexOf("mysql") != -1) {                    sql += " LIMIT " + start + ", " + (end - start);                }            }            sqlExpr.setStatement(sql);        }        return sqlExpr;    }