01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.repository.sail;
07:
08: import java.util.ArrayList;
09:
10: import info.aduna.iteration.CloseableIteration;
11:
12: import org.openrdf.query.BindingSet;
13: import org.openrdf.query.QueryEvaluationException;
14: import org.openrdf.query.QueryResultUtil;
15: import org.openrdf.query.TupleQuery;
16: import org.openrdf.query.TupleQueryResult;
17: import org.openrdf.query.TupleQueryResultHandler;
18: import org.openrdf.query.TupleQueryResultHandlerException;
19: import org.openrdf.query.algebra.TupleExpr;
20: import org.openrdf.query.impl.TupleQueryResultImpl;
21: import org.openrdf.query.parser.ParsedTupleQuery;
22: import org.openrdf.sail.SailConnection;
23: import org.openrdf.sail.SailException;
24:
25: /**
26: * @author Arjohn Kampman
27: */
28: public class SailTupleQuery extends SailQuery implements TupleQuery {
29:
30: protected SailTupleQuery(ParsedTupleQuery tupleQuery,
31: SailRepositoryConnection sailConnection) {
32: super (tupleQuery, sailConnection);
33: }
34:
35: @Override
36: public ParsedTupleQuery getParsedQuery() {
37: return (ParsedTupleQuery) super .getParsedQuery();
38: }
39:
40: public TupleQueryResult evaluate() throws QueryEvaluationException {
41: TupleExpr tupleExpr = getParsedQuery().getTupleExpr();
42:
43: try {
44: SailConnection sailCon = getConnection()
45: .getSailConnection();
46: CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter = sailCon
47: .evaluate(tupleExpr, getActiveDataset(),
48: getBindings(), getIncludeInferred());
49:
50: return new TupleQueryResultImpl(new ArrayList<String>(
51: tupleExpr.getBindingNames()), bindingsIter);
52: } catch (SailException e) {
53: throw new QueryEvaluationException(e.getMessage(), e);
54: }
55: }
56:
57: public void evaluate(TupleQueryResultHandler handler)
58: throws QueryEvaluationException,
59: TupleQueryResultHandlerException {
60: TupleQueryResult queryResult = evaluate();
61: QueryResultUtil.report(queryResult, handler);
62: }
63: }
|