/** * Method to compile the ordering clause of the query */ protected void compileOrdering() { if (compilation.getExprOrdering() != null) { compileComponent = CompilationComponent.ORDERING; orderingObject = new BasicDBObject(); for (Expression expr : compilation.getExprOrdering()) { OrderExpression orderExpr = (OrderExpression)expr; MongoFieldExpression orderMongoExpr = (MongoFieldExpression) orderExpr.getLeft().evaluate(this); if(orderMongoExpr == null) continue; String orderDir = orderExpr.getSortOrder(); int direction = ((orderDir == null || orderDir.equals("ascending")) ? 1 : -1); orderingObject.put(orderMongoExpr.getPropertyName(), direction); } compileComponent = null; } }
/** * @param qd The QueryData * @param orderExpr The OrderExpression * @return The name of the sort property that was added to the primary datastore query. */ String getSortProperty(QueryData qd, OrderExpression orderExpr) { PrimaryExpression left = (PrimaryExpression) orderExpr.getLeft(); AbstractClassMetaData acmd = qd.acmd; List<String> tuples = getTuples(left, qd.compilation.getCandidateAlias()); if (isJoin(left.getLeft(), tuples)) { // Change the class meta data to the meta-data for the joined class acmd = getJoinClassMetaData(left.getLeft(), tuples, qd); } AbstractMemberMetaData ammd = getMemberMetaDataForTuples(acmd, tuples); if (ammd == null) { throw noMetaDataException(left.getId(), acmd.getFullClassName()); } if (MetaDataUtils.isParentPKField(ammd)) { throw new UnsupportedDatastoreFeatureException("Cannot sort by parent."); } else { String sortProp; if (ammd.isPrimaryKey()) { sortProp = Entity.KEY_RESERVED_PROPERTY; } else { sortProp = determinePropertyName(ammd); } return sortProp; } }