Search in sources :

Example 1 with HandlerBlock

use of org.apache.jena.arq.querybuilder.handlers.HandlerBlock in project jena by apache.

the class AbstractQueryBuilder method build.

/**
	 * Build the query. Performs the var replacements as specified by
	 * setVar(var,node) calls.
	 * 
	 * @return The query.
	 */
public final Query build() {
    Query q = new Query();
    // set the query type
    switch(query.getQueryType()) {
        case Query.QueryTypeAsk:
            q.setQueryAskType();
            break;
        case Query.QueryTypeConstruct:
            q.setQueryConstructType();
            break;
        case Query.QueryTypeDescribe:
            q.setQueryDescribeType();
            break;
        case Query.QueryTypeSelect:
            q.setQuerySelectType();
            break;
        default:
            throw new IllegalStateException("Internal query is not a known type: " + q.getQueryType());
    }
    // use the HandlerBlock implementation to copy the data.
    HandlerBlock handlerBlock = new HandlerBlock(q);
    handlerBlock.addAll(getHandlerBlock());
    // set the vars
    handlerBlock.setVars(values);
    //  make sure we have a query pattern before we start building.
    if (q.getQueryPattern() == null) {
        q.setQueryPattern(new ElementGroup());
    }
    handlerBlock.build();
    return q;
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) HandlerBlock(org.apache.jena.arq.querybuilder.handlers.HandlerBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 2 with HandlerBlock

use of org.apache.jena.arq.querybuilder.handlers.HandlerBlock in project jena by apache.

the class AbstractQueryBuilder method clone.

/**
	 * Close the query.
	 * 
	 * This can be used when the query would not normally parse as is required
	 * by the Query.clone() method.
	 * 
	 * @param q2
	 *            The query to clone
	 * @return A clone of the q2 param.
	 */
public static Query clone(Query q2) {
    Query retval = new Query();
    // set the query type
    if (q2.isSelectType()) {
        retval.setQuerySelectType();
    } else if (q2.isAskType()) {
        retval.setQueryAskType();
    } else if (q2.isDescribeType()) {
        retval.setQueryDescribeType();
    } else if (q2.isConstructType()) {
        retval.setQueryConstructType();
    }
    // use the handler block to clone the data
    HandlerBlock hb = new HandlerBlock(retval);
    HandlerBlock hb2 = new HandlerBlock(q2);
    hb.addAll(hb2);
    return retval;
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) HandlerBlock(org.apache.jena.arq.querybuilder.handlers.HandlerBlock)

Example 3 with HandlerBlock

use of org.apache.jena.arq.querybuilder.handlers.HandlerBlock in project jena by apache.

the class AbstractQueryBuilder method rewrite.

/**
	 * Rewrite a query replacing variables as specified in the values map.
	 * 
	 * @param q2
	 *            The query to rewrite
	 * @param values
	 *            a Mapping of var to node for replacement.
	 * @return The new query with the specified vars replaced.
	 */
public static Query rewrite(Query q2, Map<Var, Node> values) {
    HandlerBlock hb = new HandlerBlock(q2);
    hb.setVars(values);
    return q2;
}
Also used : HandlerBlock(org.apache.jena.arq.querybuilder.handlers.HandlerBlock)

Aggregations

HandlerBlock (org.apache.jena.arq.querybuilder.handlers.HandlerBlock)3 Query (org.apache.jena.query.Query)2 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)2 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)1