/*
* Copyright 2009 DuraSpace.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mulgara.query;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.mulgara.connection.Connection;
import org.mulgara.connection.Connection.SessionOp;
import org.mulgara.server.Session;
/**
* A query type similar to SELECT, though it may only contain results with multiples of
* 3 columns, with the following rules:
* First column: contains URIs or BlankNodes
* Second column: contains URIs
* Third column: contains URIs, BlankNodes, or Literals
*
* @created Jun 26, 2008
* @author Paula Gearon
* @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
* @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
*/
public class ConstructQuery extends Query {
/** Required serialization ID */
private static final long serialVersionUID = -6024259961466362580L;
private final Set<Variable> bNodes;
public ConstructQuery(List<? extends SelectElement> variableList, GraphExpression graphExpression,
ConstraintExpression constraintExpression,
List<Order> orderList, Integer limit, int offset) {
super(variableList, graphExpression, constraintExpression,
null, // no having
orderList,
limit,
offset,
true,
new UnconstrainedAnswer());
Set<Variable> bn = new HashSet<Variable>();
for (SelectElement e: variableList) {
if (e instanceof Variable && ((Variable)e).isBnodeVar()) bn.add((Variable)e);
}
bNodes = Collections.unmodifiableSet(bn);
}
/**
* Gets the set of variables that represent bnodes being generated by this query.
* @return An unmodifiable set of variables that are bNode generators.
*/
public Set<Variable> getBnodeVars() {
return bNodes;
}
/**
* Executes this query on a connection.
* @param conn The connection to a database session to execute the query against.
* @return The answer to this query. This must be closed by the calling code.
*/
public GraphAnswer execute(Connection conn) throws QueryException, TuplesException {
// pipe all the query types through the one Session method
GraphAnswer answer = conn.execute(new SessionOp<GraphAnswer,QueryException>(){
public GraphAnswer fn(Session session) throws QueryException {
return session.query(ConstructQuery.this);
}
});
if (answer == null) throw new QueryException("Invalid answer received");
// move to the first row
answer.beforeFirst();
return answer;
}
}