private String generate(int limit) {
StringBuilder sb = new StringBuilder().append("SELECT * FROM ").append(
mEntityMapping.mTableName);
if (customSql != null) {
return sb.append(" ").append(customSql).toString();
}
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);
}
if (limit > -1) {
sb.append(" LIMIT ").append(limit);
}
return sb.toString();
}
public String toSql() {
if (sqlCache == null) {
return sqlCache = generate(this.limit);
} else {
return sqlCache;
}
}
public String toString() {
return toSql();
}
/**
Execute the query on the default database, returning only a single
result. If the query would return multiple results, only the first will
be returned by this method.
*/
public T execute() {
SQLiteDatabase db = ORMDroidApplication.getDefaultDatabase();
try {
return execute(db);
} finally {
db.close();
}
}
/**
Execute the query on the specified database, returning only a single
result. If the query would return multiple results, only the first will
be returned by this method.
*/
public T execute(SQLiteDatabase db) {
EntityMapping map = Entity.getEntityMappingEnsureSchema(db, mClass);
Executes the query against the default database, returning a high
performance cursor instead of the complete in-memory list of objects.
*/
public Cursor executeMultiForCursor() {
SQLiteDatabase db = ORMDroidApplication.getDefaultDatabase();
try {
return executeMultiForCursor(db);
} finally {
db.close();
}
}
/**
Execute the query on the default database, returning all results.
*/
public List executeMulti() {
SQLiteDatabase db = ORMDroidApplication.getDefaultDatabase();
try {
return executeMulti(db);
} finally {
db.close();
}
}
/**
Executes the query against the specified database, returning a high
performance cursor instead of the complete in-memory list of objects.
*/
public Cursor executeMultiForCursor(SQLiteDatabase db) {
String sql = toSql();
Log.v(TAG, sql);
return db.rawQuery(sql, null);
}
/**
Execute the query on the specified database, returning all results.
*/
public List executeMulti(SQLiteDatabase db) {
EntityMapping map = Entity.getEntityMappingEnsureSchema(db, mClass);
Hi
I love the framework, however I was missing 2 query operators, isnull, isnotnull.
Please find the enhancement below: /*
import java.util.List;
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log;
import com.roscopeco.ormdroid.Entity.EntityMapping;
/**
public static interface SQLExpression { String generate(); }
static class BinExpr implements SQLExpression { static final String EQ = " = "; static final String NE = " != "; static final String LT = " < "; static final String GT = " > "; static final String LEQ = " <= "; static final String GEQ = " >= "; static final String ISN = " IS NULL "; static final String ISNN = " IS NOT NULL ";
}
static class LogicalExpr implements SQLExpression { final String op; final SQLExpression[] operands;
}
private static StringBuilder joinStrings(StringBuilder sb, String... strings) { if (strings.length < 1) { return sb.append("*"); } else { sb.append(strings[0]); for (int i = 1; i < strings.length; i++) { sb.append(", ").append(strings[i]); } return sb; } }
private final Class mClass;
private final EntityMapping mEntityMapping;
private String customSql;
private String sqlCache, sqlCache1, whereCache;
private SQLExpression whereExpr;
private String[] orderByColumns;
private int limit = -1;
public Query(Class clz) {
mEntityMapping = Entity.getEntityMapping(mClass = clz);
}
public static Query query(Class clz) {
return new Query(clz);
}
public static SQLExpression eql(String column, Object value) { return new BinExpr(BinExpr.EQ, column, TypeMapper.encodeValue(null, value)); }
public static SQLExpression neq(String column, Object value) { return new BinExpr(BinExpr.NE, column, TypeMapper.encodeValue(null, value)); }
public static SQLExpression lt(String column, Object value) { return new BinExpr(BinExpr.LT, column, TypeMapper.encodeValue(null, value)); }
public static SQLExpression gt(String column, Object value) { return new BinExpr(BinExpr.GT, column, TypeMapper.encodeValue(null, value)); }
public static SQLExpression leq(String column, Object value) { return new BinExpr(BinExpr.LEQ, column, TypeMapper.encodeValue(null, value)); }
public static SQLExpression isnull(String column) { return new BinExpr(BinExpr.ISN, column); }
public static SQLExpression isnotnull(String column) { return new BinExpr(BinExpr.ISNN, column); }
public static SQLExpression geq(String column, Object value) { return new BinExpr(BinExpr.GEQ, column, TypeMapper.encodeValue(null, value)); }
public static SQLExpression and(SQLExpression... operands) { return new LogicalExpr("AND", operands); }
public static SQLExpression or(SQLExpression... operands) { return new LogicalExpr("OR", operands); }
/**
public Query where(SQLExpression expr) {
if (customSql != null) {
throw new IllegalStateException(
"Cannot change query parameters on custom SQL Query");
}
}
public Query where(String sql) {
if (customSql != null) {
throw new IllegalStateException(
"Cannot change query parameters on custom SQL Query");
}
}
public Query orderBy(String... columns) {
if (customSql != null) {
throw new IllegalStateException(
"Cannot change query parameters on custom SQL Query");
}
}
public Query limit(int limit) {
if (customSql != null) {
throw new IllegalStateException(
"Cannot change query parameters on custom SQL Query");
}
}
private String generate(int limit) { StringBuilder sb = new StringBuilder().append("SELECT * FROM ").append( mEntityMapping.mTableName);
}
public String toSql() { if (sqlCache == null) { return sqlCache = generate(this.limit); } else { return sqlCache; } }
public String toString() { return toSql(); }
/**
/**
if (sqlCache1 == null) { sqlCache1 = generate(1); } String sql = sqlCache1; Log.v(TAG, sql); Cursor c = db.rawQuery(sql, null); try { if (c.moveToFirst()) { return map. load(db, c);
} else {
return null;
}
} finally {
c.close();
}
}
/**
/**
/**
/**
String sql = toSql(); Log.v(TAG, sql); return map.loadAll(db, db.rawQuery(sql, null)); } }