Created
October 14, 2013 12:28
-
-
Save anonymous/6974860 to your computer and use it in GitHub Desktop.
A fluent API for ContentResolver#query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.ArrayList; | |
import android.content.ContentResolver; | |
import android.database.Cursor; | |
import android.net.Uri; | |
/** | |
* Just an incomplete POC .... MANY operators missing (IN,BETWEEN, ...) AND MANY MORE! | |
* | |
* @author b.quentin | |
* | |
*/ | |
public class Query { | |
public interface QueryBuilder { | |
public interface UnfinishedWhere { | |
public QueryBuilder.Where ne(String value); | |
public QueryBuilder.Where eq(String value); | |
public QueryBuilder.Where le(String value); | |
public QueryBuilder.Where l(String value); | |
public QueryBuilder.Where g(String value); | |
public QueryBuilder.Where ge(String value); | |
public QueryBuilder.Where like(String value); | |
} | |
public interface Where { | |
public QueryBuilder and(); | |
public QueryBuilder or(); | |
public QueryBuilder.FinishedQueryBuilder orderBy(String orderBy); | |
public Query build(); | |
} | |
public interface FinishedQueryBuilder { | |
public Query build(); | |
} | |
public QueryBuilder select(String... columns); | |
public QueryBuilder.UnfinishedWhere where(String column); | |
public Query build(); | |
public Where where(Where q); | |
} | |
private QueryBuilderImpl queryBuilderImpl; | |
private Query(QueryBuilderImpl queryBuilderImpl) { | |
this.queryBuilderImpl = queryBuilderImpl; | |
} | |
public static QueryBuilder create() { | |
return new QueryBuilderImpl(); | |
} | |
public Cursor execute(ContentResolver contentResolver, Uri contentUri) { | |
String[] projection = queryBuilderImpl.fields; | |
String selection = queryBuilderImpl.where.toString(); | |
String[] selectionArgs = queryBuilderImpl.params.toArray(new String[queryBuilderImpl.params.size()]); | |
String sortOrder = queryBuilderImpl.sortOrder; | |
return contentResolver.query(contentUri, projection, selection, selectionArgs, sortOrder); | |
} | |
public static class QueryBuilderImpl implements QueryBuilder, QueryBuilder.UnfinishedWhere, QueryBuilder.Where, QueryBuilder.FinishedQueryBuilder { | |
private String[] fields; | |
private String sortOrder; | |
private StringBuffer where = new StringBuffer(); | |
private ArrayList<String> params = new ArrayList<String>(); | |
@Override | |
public QueryBuilder select(String... columns) { | |
fields = columns; | |
return this; | |
} | |
@Override | |
public Where where(Where qb){ | |
QueryBuilderImpl w = (QueryBuilderImpl) qb; | |
where.append("("); | |
where.append(w.where.toString()); | |
where.append(")"); | |
params.addAll(w.params); | |
return this; | |
} | |
@Override | |
public UnfinishedWhere where(String column) { | |
where.append(" "); | |
where.append(column); | |
return this; | |
} | |
@Override | |
public QueryBuilder.FinishedQueryBuilder orderBy(String orderBy) { | |
this.sortOrder = orderBy; | |
return this; | |
} | |
@Override | |
public QueryBuilder and() { | |
where.append(" AND "); | |
return this; | |
} | |
@Override | |
public QueryBuilder or() { | |
where.append(" OR "); | |
return this; | |
} | |
@Override | |
public Where ne(String value) { | |
params.add(value); | |
where.append("<>?"); | |
return this; | |
} | |
@Override | |
public Where eq(String value) { | |
params.add(value); | |
where.append("=?"); | |
return this; | |
} | |
@Override | |
public Where le(String value) { | |
params.add(value); | |
where.append("<=?"); | |
return this; | |
} | |
@Override | |
public Where l(String value) { | |
params.add(value); | |
where.append("<?"); | |
return this; | |
} | |
@Override | |
public Where g(String value) { | |
params.add(value); | |
where.append(">?"); | |
return this; | |
} | |
@Override | |
public Where ge(String value) { | |
params.add(value); | |
where.append(">=?"); | |
return this; | |
} | |
@Override | |
public Where like(String value) { | |
params.add(value); | |
where.append("LIKE ? "); | |
return this; | |
} | |
@Override | |
public Query build() { | |
return new Query(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment