Skip to content

Commit

Permalink
Added order string generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Perepelitsa committed Jul 18, 2015
1 parent 270813c commit 8817904
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public abstract class AbstractSelection<T extends AbstractSelection<?>> {
private static final String STARTS = " LIKE ? || '%'";
private static final String ENDS = " LIKE '%' || ?";
private static final String COUNT = "COUNT(*)";
private static final String DESC = " DESC";

private final StringBuilder mSelection = new StringBuilder();
private final List<String> mSelectionArgs = new ArrayList<String>(5);

private final StringBuilder mOrderBy = new StringBuilder();

Boolean mNotify;
String mGroupBy;
String mHaving;
Expand Down Expand Up @@ -276,6 +279,12 @@ public abstract class AbstractSelection<T extends AbstractSelection<?>> {
return mSelectionArgs.toArray(new String[size]);
}

/**
* Returns the order string produced by this object.
*/
public String order() {
return mOrderBy.length() > 0 ? mOrderBy.toString() : null;
}

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

@SuppressWarnings("unchecked")
public T orderBy(String order, boolean desc) {
if (mOrderBy.length() > 0) mOrderBy.append(COMMA);
mOrderBy.append(order);
if (desc) mOrderBy.append(DESC);
return (T) this;
}

public T orderBy(String order) {
return orderBy(order, false);
}

public int count(ContentResolver resolver) {
final Cursor cursor = resolver.query(uri(), new String[]{COUNT}, sel(), args(), null);
if (cursor == null)
return 0;
if (cursor == null) return 0;
try {
return cursor.moveToFirst() ? cursor.getInt(0) : 0;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity
addEquals("${entity.nameLowerCase}." + ${entity.nameCamelCase}Columns._ID, toObjectArray(value));
return this;
}

public ${entity.nameCamelCase}Selection idNot(long... value) {
addNotEquals("${entity.nameLowerCase}." + ${entity.nameCamelCase}Columns._ID, toObjectArray(value));
return this;
}

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

public ${entity.nameCamelCase}Selection orderById() {
return orderById(false);
}
<#list entity.getFieldsIncludingJoins() as field>
<#if field.nameLowerCase != "_id">
<#switch field.type.name()>
Expand Down Expand Up @@ -243,6 +257,16 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity
}
<#break>
</#switch>

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;
}

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;
}
</#if>
</#list>
}

0 comments on commit 8817904

Please sign in to comment.