Skip to content

Commit

Permalink
JCBC-960: Expression method to wrap in parenthesis
Browse files Browse the repository at this point in the history
Motivation
----------
Often, N1QL statements will need to explicitly define evaluation order
by using parenthesis. There is currently no method to do that and one
as to fallback to string concatenation inside of an x() call.

Modifications
-------------
Added the `par()` method that infixes an Expression inside parenthesis.

Result
------
Better looking wrapping of Expression in parenthesis.

Change-Id: Ia35ac8266e54a3ecc4e439c2f0a809a83312283d
Reviewed-on: http://review.couchbase.org/64210
Tested-by: Simon Baslé <simon@couchbase.com>
Reviewed-by: Michael Nitschinger <michael@nitschinger.at>
  • Loading branch information
simonbasle committed May 19, 2016
1 parent 9800c35 commit e30ab99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/couchbase/client/java/query/dsl/Expression.java
Expand Up @@ -173,6 +173,16 @@ public static Expression sub(final Statement statement) {
return x("(" + statement.toString() + ")");
}

/**
* Puts an {@link Expression} in parenthesis.
*
* @param expression the expression to wrap in parenthesis.
* @return the expression, wrapped in parenthesis.
*/
public static Expression par(final Expression expression) {
return infix(expression.toString(), "(", ")");
}

/**
* Construct a path ("a.b.c") from Expressions or values. Strings are considered identifiers
* (so they won't be quoted).
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;

import static com.couchbase.client.java.query.dsl.Expression.i;
import static com.couchbase.client.java.query.dsl.Expression.par;
import static com.couchbase.client.java.query.dsl.Expression.s;
import static com.couchbase.client.java.query.dsl.Expression.x;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -197,4 +198,11 @@ public void shouldAcceptArithmeticOperationsForAllKindOfNumbers() {
assertEquals("1 / user.id / user.age / 1 / 100 / 2.3 / 4.5", divide.toString());
}

@Test
public void shouldWrapExpressionInParenthesis() {
Expression base = x("foo").eq(x("bar"));

assertEquals("( foo = bar )", par(base).toString());
}

}

0 comments on commit e30ab99

Please sign in to comment.