You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
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:
/*
*
*
*/
package com.roscopeco.ormdroid;
import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.roscopeco.ormdroid.Entity.EntityMapping;
/**
Represents and assists with building a database query that will load an
object (or graph). Mostly this class will be used indirectly via the
{@link Entity#query} method.
Example usage:
{@link #where where}({@link #eql eql}("id", 1)).{@link #execute execute}()
{@link #where where}({@link #eql eql}("name", "Joe")).{@link #execute
execute}()
{@link #where where}({@link #eql eql}("city", "London")).
{@link #executeMulti() executeMulti}()
{@link #executeMulti executeMulti}()
{@link #where where}({@link #and and}({@link #eql eql}("name", "Joe"),
{@link #eql eql}("city", "London"))).{@link #execute execute}()
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 sql(String sql) {
sqlCache = null;
sqlCache1 = null;
whereCache = null;
whereExpr = null;
orderByColumns = null;
limit = -1;
customSql = sql;
return this;
}
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();
}
/**
*/
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);
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();
}
}
/**
*/
public Cursor executeMultiForCursor() {
SQLiteDatabase db = ORMDroidApplication.getDefaultDatabase();
try {
return executeMultiForCursor(db);
} finally {
db.close();
}
}
/**
*/
public List executeMulti() {
SQLiteDatabase db = ORMDroidApplication.getDefaultDatabase();
try {
return executeMulti(db);
} finally {
db.close();
}
}
/**
*/
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);
String sql = toSql();
Log.v(TAG, sql);
return map.loadAll(db, db.rawQuery(sql, null));
}
}
The text was updated successfully, but these errors were encountered: