Skip to content

Commit

Permalink
Add negation operator support to expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoleman committed May 5, 2011
1 parent 97d9fd4 commit 7f57ac9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
13 changes: 12 additions & 1 deletion features/arithmetic.feature
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,15 @@ Feature: Arithmetic queries
Then I should get the following results:
| x |
| 1 |


Scenario: Negation (expression and value)
Given I have created the following "NumberDomain" instances:
| x |
| -1 |
| 0 |
| 1 |
When I execute the code "NumberDomain.where { (- property('x')) gte: 0 }.all()"
Then I should get the following results:
| x |
| -1 |
| 0 |
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ class ExpressionBase {
div(expr)
}

ExpressionBase negative() {
return new ArithmeticExpression(null, this, '-', builder)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
StringWriter sqlWriter = new StringWriter();
sqlWriter.append('(');
if (rhs == null) { sqlWriter.append('('); }
appendSqlStringForExpression(lhs, sqlWriter, criteria, criteriaQuery);
if (rhs == null) { sqlWriter.append(')'); }
sqlWriter.append(' ');
if (lhs != null) {
if (rhs == null) { sqlWriter.append('('); }
appendSqlStringForExpression(lhs, sqlWriter, criteria, criteriaQuery);
if (rhs == null) { sqlWriter.append(')'); }
sqlWriter.append(' ');
}
sqlWriter.append(operator);
if (rhs != null) {
sqlWriter.append(' ');
Expand Down

0 comments on commit 7f57ac9

Please sign in to comment.