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

Commit

Permalink
Allow custom SQL queries
Browse files Browse the repository at this point in the history
Fix broken where string handling
  • Loading branch information
roscopeco committed Jul 1, 2013
1 parent 4647049 commit edbc81c
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions src/com/roscopeco/ormdroid/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ private static StringBuilder joinStrings(StringBuilder sb, String... strings) {
}

private final Class<T> mClass;
private final EntityMapping mEntityMapping;
private String sqlCache, sqlCache1;
private final EntityMapping mEntityMapping;
private String customSql;
private String sqlCache, sqlCache1, whereCache;
private SQLExpression whereExpr;
private String[] orderByColumns;
private int limit = -1;
Expand Down Expand Up @@ -154,38 +155,84 @@ public static SQLExpression or(SQLExpression... operands) {
return new LogicalExpr("OR", operands);
}

/**
* Set this query to execute the given custom SQL. This SQL must have proper
* syntax, and will be appended after the "SELECT * FROM sometable " generated
* by the Query itself.
*
* Note that when using this method, you will no longer be able to use the
* other methods to set query parameters (e.g. {@link #where(SQLExpression)}
* and {@link #limit(int)}).
*/
public Query<T> sql(String sql) {
sqlCache = null;
sqlCache1 = null;
whereCache = null;
whereExpr = null;
orderByColumns = null;
limit = -1;
customSql = sql;
return this;
}

public Query<T> where(SQLExpression expr) {
if (customSql != null) {
throw new IllegalStateException("Cannot change query parameters on custom SQL Query");
}

sqlCache = null;
sqlCache1 = null;
whereCache = null;
whereExpr = expr;
return this;
}

public Query<T> where(String sql) {
sqlCache = sql;
sqlCache1 = sql;
if (customSql != null) {
throw new IllegalStateException("Cannot change query parameters on custom SQL Query");
}

sqlCache = null;
sqlCache1 = null;
whereCache = sql;
whereExpr = null;
return this;
}

public Query<T> orderBy(String... columns) {
if (customSql != null) {
throw new IllegalStateException("Cannot change query parameters on custom SQL Query");
}

sqlCache = null;
sqlCache1 = null;
orderByColumns = columns;
return this;
}

public Query<T> limit(int limit) {
if (customSql != null) {
throw new IllegalStateException("Cannot change query parameters on custom SQL Query");
}

sqlCache = null;
sqlCache1 = null;
this.limit = limit;
return this;
}

private String generate(int limit) {
if (customSql != null) {
return customSql;
}

StringBuilder sb = new StringBuilder().append("SELECT * FROM ").append(mEntityMapping.mTableName);
if (whereExpr != null) {
sb.append(" WHERE ").append(whereExpr.generate());
if (whereCache != null) {
sb.append(" WHERE ").append(whereCache);
} else {
if (whereExpr != null) {
sb.append(" WHERE ").append(whereCache = whereExpr.generate());
}
}
if (orderByColumns != null && orderByColumns.length > 0) {
joinStrings(sb.append(" ORDER BY "), orderByColumns);
Expand Down

0 comments on commit edbc81c

Please sign in to comment.