Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Allow logical expressions more than one operand
Browse files Browse the repository at this point in the history
Reinstate where(String)
  • Loading branch information
roscopeco committed Jun 30, 2013
1 parent b068469 commit 4647049
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/com/roscopeco/ormdroid/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,27 @@ public String generate() {

static class LogicalExpr implements SQLExpression {
final String op;
final SQLExpression lhs, rhs;
final SQLExpression[] operands;

public LogicalExpr(String op, SQLExpression lhs, SQLExpression rhs) {
public LogicalExpr(String op, SQLExpression... operands) {
this.op = op;
this.lhs = lhs;
this.rhs = rhs;
if (operands.length < 2) {
throw new IllegalArgumentException("Cannot create logical expression with < 2 operands");
}
this.operands = operands;
}

public String generate() {
StringBuilder sb = new StringBuilder();
sb.append("(").append(lhs.generate()).append(" ").append(op).append(" ").append(rhs.generate()).append(")");

sb.append("(");

for (int i = 0; i < operands.length - 1; i++) {
sb.append(operands[i].generate()).append(" ").append(op).append(" ");
}

sb.append(operands[operands.length - 1].generate()).append(")");

return sb.toString();
}
}
Expand Down Expand Up @@ -136,12 +146,12 @@ public static SQLExpression geq(String column, Object value) {
return new BinExpr(BinExpr.GEQ, column, TypeMapper.encodeValue(null, value));
}

public static SQLExpression and(SQLExpression lhs, SQLExpression rhs) {
return new LogicalExpr("AND", lhs, rhs);
public static SQLExpression and(SQLExpression... operands) {
return new LogicalExpr("AND", operands);
}

public static SQLExpression or(SQLExpression lhs, SQLExpression rhs) {
return new LogicalExpr("OR", lhs, rhs);
public static SQLExpression or(SQLExpression... operands) {
return new LogicalExpr("OR", operands);
}

public Query<T> where(SQLExpression expr) {
Expand All @@ -151,6 +161,13 @@ public Query<T> where(SQLExpression expr) {
return this;
}

public Query<T> where(String sql) {
sqlCache = sql;
sqlCache1 = sql;
whereExpr = null;
return this;
}

public Query<T> orderBy(String... columns) {
sqlCache = null;
sqlCache1 = null;
Expand Down

0 comments on commit 4647049

Please sign in to comment.