Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy projection generation + fixed "do all possible joins by default" problem #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
.idea
.DS_Store
.DS_Store
*.iml
21 changes: 0 additions & 21 deletions android_contentprovider_generator.iml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class AbstractSelection<T extends AbstractSelection<?>> {
private final List<String> mSelectionArgs = new ArrayList<String>(5);

private final StringBuilder mOrderBy = new StringBuilder();
private final List<String> mProjection = new ArrayList<String>(5);

Boolean mNotify;
String mGroupBy;
Expand Down Expand Up @@ -286,6 +287,13 @@ public abstract class AbstractSelection<T extends AbstractSelection<?>> {
return mOrderBy.length() > 0 ? mOrderBy.toString() : null;
}

/**
* Returns the projection produced by this object.
*/
public String[] proj() {
return mProjection.isEmpty() ? null : mProjection.toArray(new String[mProjection.size()]);
}

/**
* Returns the {@code uri} argument to pass to the {@code ContentResolver} methods.
*/
Expand Down Expand Up @@ -364,6 +372,14 @@ public abstract class AbstractSelection<T extends AbstractSelection<?>> {
return (T) this;
}

@SuppressWarnings("unchecked")
public T forColumn(String... columns) {
for (String column : columns) {
mProjection.add(column);
}
return (T) this;
}

public int count(ContentResolver resolver) {
Cursor cursor = resolver.query(uri(), new String[] { COUNT }, sel(), args(), null);
if (cursor == null) return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,39 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity
* Query the given content resolver using this selection.
*
* @param contentResolver The content resolver to query.
* @param projection A list of which columns to return. Passing null will return all columns, which is inefficient.
* @return A {@code ${entity.nameCamelCase}Cursor} object, which is positioned before the first entry, or null.
*/
public ${entity.nameCamelCase}Cursor query(ContentResolver contentResolver, String[] projection) {
Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order());
public ${entity.nameCamelCase}Cursor query(ContentResolver contentResolver) {
Cursor cursor = contentResolver.query(uri(), proj(), sel(), args(), order());
if (cursor == null) return null;
return new ${entity.nameCamelCase}Cursor(cursor);
}

/**
* Equivalent of calling {@code query(contentResolver, null)}.
*/
public ${entity.nameCamelCase}Cursor query(ContentResolver contentResolver) {
return query(contentResolver, null);
}

/**
* Query the given content resolver using this selection.
*
* @param context The context to use for the query.
* @param projection A list of which columns to return. Passing null will return all columns, which is inefficient.
* @return A {@code ${entity.nameCamelCase}Cursor} object, which is positioned before the first entry, or null.
*/
public ${entity.nameCamelCase}Cursor query(Context context, String[] projection) {
Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order());
public ${entity.nameCamelCase}Cursor query(Context context) {
Cursor cursor = context.getContentResolver().query(uri(), proj(), sel(), args(), order());
if (cursor == null) return null;
return new ${entity.nameCamelCase}Cursor(cursor);
}

/**
* Equivalent of calling {@code query(context, null)}.
*/
public ${entity.nameCamelCase}Cursor query(Context context) {
return query(context, null);
@Override
public String[] proj() {
String[] projection = super.proj();
return projection == null ? ${entity.nameCamelCase}Columns.ALL_COLUMNS : projection;
}

public ${entity.nameCamelCase}Selection forAll${entity.nameCamelCase}Columns() {
return forColumn(${entity.nameCamelCase}Columns.ALL_COLUMNS);
}

public ${entity.nameCamelCase}Selection forId() {
return forColumn(${entity.nameCamelCase}Columns._ID);
}

public ${entity.nameCamelCase}Selection id(long... value) {
addEquals("${entity.nameLowerCase}." + ${entity.nameCamelCase}Columns._ID, toObjectArray(value));
Expand All @@ -76,8 +73,7 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity
}

public ${entity.nameCamelCase}Selection orderById(boolean desc) {
orderBy("${entity.nameLowerCase}." + ${entity.nameCamelCase}Columns._ID, desc);
return this;
return orderBy("${entity.nameLowerCase}." + ${entity.nameCamelCase}Columns._ID, desc);
}

public ${entity.nameCamelCase}Selection orderById() {
Expand Down Expand Up @@ -240,14 +236,16 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity
<#break>
</#switch>

public ${entity.nameCamelCase}Selection for<#if field.isForeign>${field.path}${field.nameCamelCase}<#else>${field.nameCamelCase}</#if>() {
return forColumn(${field.entity.nameCamelCase}Columns.${field.nameUpperCase});
}

public ${entity.nameCamelCase}Selection orderBy<#if field.isForeign>${field.path}${field.nameCamelCase}<#else>${field.nameCamelCase}</#if>(boolean desc) {
orderBy(${field.entity.nameCamelCase}Columns.${field.nameUpperCase}, desc);
return this;
return orderBy(${field.entity.nameCamelCase}Columns.${field.nameUpperCase}, desc);
}

public ${entity.nameCamelCase}Selection orderBy<#if field.isForeign>${field.path}${field.nameCamelCase}<#else>${field.nameCamelCase}</#if>() {
orderBy(${field.entity.nameCamelCase}Columns.${field.nameUpperCase}, false);
return this;
return orderBy(${field.entity.nameCamelCase}Columns.${field.nameUpperCase}, false);
}
</#if>
</#list>
Expand Down