Skip to content
Browse files

fixing a bug w/ how we're doing the setting of limit & offset directl…

…y in a query model
  • Loading branch information...
1 parent 0633994 commit 4e42cc9375d3d9b86a4ab1ed94f7d9f573a5a12f Michael Grove committed Feb 20, 2012
Showing with 82 additions and 8 deletions.
  1. +82 −8 core/src/com/clarkparsia/openrdf/query/SesameQueryUtils.java
View
90 core/src/com/clarkparsia/openrdf/query/SesameQueryUtils.java
@@ -59,6 +59,9 @@
import org.openrdf.query.algebra.ProjectionElem;
import org.openrdf.query.algebra.Projection;
import org.openrdf.query.algebra.MultiProjection;
+import org.openrdf.query.algebra.Reduced;
+import org.openrdf.query.algebra.QueryModelVisitor;
+import org.openrdf.query.algebra.QueryModelNode;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.GraphQueryResult;
import org.openrdf.query.QueryEvaluationException;
@@ -293,7 +296,7 @@ public static String escape(String theString) {
*/
public static void setLimit(final ParsedQuery theQuery, final int theLimit) {
try {
- SliceMutator aLimitSetter = new SliceMutator(theLimit);
+ SliceMutator aLimitSetter = SliceMutator.changeLimit(theLimit);
theQuery.getTupleExpr().visit(aLimitSetter);
if (!aLimitSetter.limitWasSet()) {
@@ -317,14 +320,13 @@ public static void setLimit(final ParsedQuery theQuery, final int theLimit) {
*/
public static void setOffset(final ParsedQuery theQuery, final int theOffset) {
try {
- SliceMutator aLimitSetter = new SliceMutator(theOffset);
+ SliceMutator aLimitSetter = SliceMutator.changeOffset(theOffset);
theQuery.getTupleExpr().visit(aLimitSetter);
if (!aLimitSetter.offsetWasSet()) {
- Slice aSlice = new Slice();
+ Slice aSlice = new Slice(theQuery.getTupleExpr());
aSlice.setOffset(theOffset);
- aSlice.setArg(theQuery.getTupleExpr());
theQuery.setTupleExpr(aSlice);
}
@@ -334,6 +336,18 @@ public static void setOffset(final ParsedQuery theQuery, final int theOffset) {
}
}
+// public static void setDistinct(final ParsedQuery theQuery, final boolean theDistinct) throws Exception {
+// }
+//
+// public static void setReduced(final ParsedQuery theQuery, final boolean theReduced) throws Exception {
+// if (!theReduced) {
+// theQuery.getTupleExpr().visit(new RemoveUnaryVisitor(Reduced.class));
+// }
+// else if (!contains(theQuery, Reduced.class)) {
+//
+// }
+// }
+
/**
* Close the iteration, ignoring any thrown exception and instead just logging it.
* @param theResults the iteration to close
@@ -350,6 +364,52 @@ public static void closeQuietly(final CloseableIteration<?, ? extends Exception>
}
}
+ private static class RemoveUnaryVisitor extends QueryModelVisitorBase<Exception> {
+ private final Class<? extends UnaryTupleOperator> mClass;
+
+ public RemoveUnaryVisitor(final Class<? extends UnaryTupleOperator> theClass) {
+ mClass = theClass;
+ }
+
+ @Override
+ protected void meetUnaryTupleOperator(final UnaryTupleOperator theUnaryTupleOperator) throws Exception {
+ if (mClass.equals(theUnaryTupleOperator.getClass())) {
+ theUnaryTupleOperator.replaceWith(theUnaryTupleOperator.getArg());
+ }
+ else {
+ super.meetUnaryTupleOperator(theUnaryTupleOperator);
+ }
+ }
+ }
+
+ private static class ContainsVisitor extends QueryModelVisitorBase<Exception> {
+ private boolean mContains = false;
+
+ private final Class<? extends QueryModelNode> mClass;
+
+ private ContainsVisitor(final Class<? extends QueryModelNode> theClass) {
+ mClass = theClass;
+ }
+
+ public boolean isContains() {
+ return mContains;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ @Override
+ protected void meetNode(final QueryModelNode theQueryModelNode) throws Exception {
+ if (mClass.equals(theQueryModelNode.getClass())) {
+ mContains = true;
+ }
+ else {
+ super.meetNode(theQueryModelNode);
+ }
+ }
+ }
+
+
/**
* Implementation of a {@link org.openrdf.query.algebra.QueryModelVisitor} which will set the limit or offset of a query
* object to the provided value. If there is no slice operator specified, {@link #limitWasSet} and {@link #offsetWasSet} will return false.
@@ -368,21 +428,35 @@ public static void closeQuietly(final CloseableIteration<?, ? extends Exception>
/**
* The new limit for the query
*/
- private int mNewLimit;
+ private final int mNewLimit;
/**
* The new offset for the query
*/
- private int mNewOffset;
+ private final int mNewOffset;
/**
* Create a new SetLimit object
- * @param theNewLimit the new limit to use for the query
+ * @param theNewLimit the new limit to use for the query, or -1 to not set
+ * @param theNewOffset the new offset to use for the query, or -1 to not set
*/
- private SliceMutator(final int theNewLimit) {
+ private SliceMutator(final int theNewLimit, final int theNewOffset) {
mNewLimit = theNewLimit;
+ mNewOffset = theNewOffset;
}
+ static SliceMutator changeLimit(final int theNewLimit) {
+ return new SliceMutator(theNewLimit, -1);
+ }
+
+ static SliceMutator changeOffset(final int theNewOffset) {
+ return new SliceMutator(-1, theNewOffset);
+ }
+
+ static SliceMutator changeLimitAndOffset(final int theNewLimit, final int theNewOffset) {
+ return new SliceMutator(theNewLimit, theNewOffset);
+ }
+
/**
* Resets the state of this visitor so it can be re-used.
*/

0 comments on commit 4e42cc9

Please sign in to comment.
Something went wrong with that request. Please try again.