Search in sources :

Example 6 with OrderElem

use of org.eclipse.rdf4j.query.algebra.OrderElem in project rdf4j by eclipse.

the class SPARQLQueryRenderer method render.

/**
 * @inheritDoc
 */
public String render(final ParsedQuery theQuery) throws Exception {
    mRenderer.reset();
    StringBuffer aBody = new StringBuffer(mRenderer.render(theQuery.getTupleExpr()));
    boolean aFirst = true;
    StringBuffer aQuery = new StringBuffer();
    if (theQuery instanceof ParsedTupleQuery) {
        aQuery.append("select ");
    } else if (theQuery instanceof ParsedBooleanQuery) {
        aQuery.append("ask\n");
    } else {
        aQuery.append("construct ");
    }
    if (mRenderer.isDistinct()) {
        aQuery.append("distinct ");
    }
    if (mRenderer.isReduced() && theQuery instanceof ParsedTupleQuery) {
        aQuery.append("reduced ");
    }
    if (!mRenderer.getProjection().isEmpty() && !(theQuery instanceof ParsedBooleanQuery)) {
        aFirst = true;
        if (!(theQuery instanceof ParsedTupleQuery)) {
            aQuery.append(" {\n");
        }
        for (ProjectionElemList aList : mRenderer.getProjection()) {
            if (SparqlTupleExprRenderer.isSPOElemList(aList)) {
                if (!aFirst) {
                    aQuery.append("\n");
                } else {
                    aFirst = false;
                }
                aQuery.append("  ").append(mRenderer.renderPattern(mRenderer.toStatementPattern(aList)));
            } else {
                for (ProjectionElem aElem : aList.getElements()) {
                    if (!aFirst) {
                        aQuery.append(" ");
                    } else {
                        aFirst = false;
                    }
                    aQuery.append("?" + aElem.getSourceName());
                // SPARQL does not support this, its an artifact of copy and
                // paste from the serql stuff
                // aQuery.append(mRenderer.getExtensions().containsKey(aElem.getSourceName())
                // ?
                // mRenderer.renderValueExpr(mRenderer.getExtensions().get(aElem.getSourceName()))
                // : "?"+aElem.getSourceName());
                // 
                // if (!aElem.getSourceName().equals(aElem.getTargetName()) ||
                // (mRenderer.getExtensions().containsKey(aElem.getTargetName())
                // &&
                // !mRenderer.getExtensions().containsKey(aElem.getSourceName())))
                // {
                // aQuery.append(" as ").append(mRenderer.getExtensions().containsKey(aElem.getTargetName())
                // ?
                // mRenderer.renderValueExpr(mRenderer.getExtensions().get(aElem.getTargetName()))
                // : aElem.getTargetName());
                // }
                }
            }
        }
        if (!(theQuery instanceof ParsedTupleQuery)) {
            aQuery.append("}");
        }
        aQuery.append("\n");
    } else if (mRenderer.getProjection().isEmpty()) {
        if (theQuery instanceof ParsedGraphQuery) {
            aQuery.append("{ }\n");
        } else if (theQuery instanceof ParsedTupleQuery) {
            aQuery.append("*\n");
        }
    }
    if (theQuery.getDataset() != null) {
        for (IRI aURI : theQuery.getDataset().getDefaultGraphs()) {
            aQuery.append("from <").append(aURI).append(">\n");
        }
        for (IRI aURI : theQuery.getDataset().getNamedGraphs()) {
            aQuery.append("from named <").append(aURI).append(">\n");
        }
    }
    if (aBody.length() > 0) {
        // test cases to prove these things work would be lovely.
        if (aBody.toString().trim().lastIndexOf(",") == aBody.length() - 1) {
            aBody.setCharAt(aBody.lastIndexOf(","), ' ');
        }
        if (!(theQuery instanceof ParsedBooleanQuery)) {
            aQuery.append("where ");
        }
        aQuery.append("{\n");
        aQuery.append(aBody);
        aQuery.append("}");
    }
    if (!mRenderer.getOrdering().isEmpty()) {
        aQuery.append("\norder by ");
        aFirst = true;
        for (OrderElem aOrder : mRenderer.getOrdering()) {
            if (!aFirst) {
                aQuery.append(" ");
            } else {
                aFirst = false;
            }
            if (aOrder.isAscending()) {
                aQuery.append(mRenderer.renderValueExpr(aOrder.getExpr()));
            } else {
                aQuery.append("desc(");
                aQuery.append(mRenderer.renderValueExpr(aOrder.getExpr()));
                aQuery.append(")");
            }
        }
    }
    if (mRenderer.getLimit() != -1 && !(theQuery instanceof ParsedBooleanQuery)) {
        aQuery.append("\nlimit ").append(mRenderer.getLimit());
    }
    if (mRenderer.getOffset() != -1) {
        aQuery.append("\noffset ").append(mRenderer.getOffset());
    }
    return aQuery.toString();
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) IRI(org.eclipse.rdf4j.model.IRI) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ParsedBooleanQuery(org.eclipse.rdf4j.query.parser.ParsedBooleanQuery) OrderElem(org.eclipse.rdf4j.query.algebra.OrderElem) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Aggregations

OrderElem (org.eclipse.rdf4j.query.algebra.OrderElem)6 ProjectionElemList (org.eclipse.rdf4j.query.algebra.ProjectionElemList)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Order (org.eclipse.rdf4j.query.algebra.Order)3 ProjectionElem (org.eclipse.rdf4j.query.algebra.ProjectionElem)2 SingletonSet (org.eclipse.rdf4j.query.algebra.SingletonSet)2 Slice (org.eclipse.rdf4j.query.algebra.Slice)2 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)2 ASTInList (org.eclipse.rdf4j.query.parser.serql.ast.ASTInList)2 ASTLimit (org.eclipse.rdf4j.query.parser.serql.ast.ASTLimit)2 ASTOffset (org.eclipse.rdf4j.query.parser.serql.ast.ASTOffset)2 ASTOrderBy (org.eclipse.rdf4j.query.parser.serql.ast.ASTOrderBy)2 IRI (org.eclipse.rdf4j.model.IRI)1 AggregateOperator (org.eclipse.rdf4j.query.algebra.AggregateOperator)1 Extension (org.eclipse.rdf4j.query.algebra.Extension)1 ExtensionElem (org.eclipse.rdf4j.query.algebra.ExtensionElem)1 GroupElem (org.eclipse.rdf4j.query.algebra.GroupElem)1 Var (org.eclipse.rdf4j.query.algebra.Var)1 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)1