From 270813c6ff4d9aa5237d4508ef99bfecc4d92161 Mon Sep 17 00:00:00 2001 From: Igor Perepelitsa Date: Thu, 16 Jul 2015 02:01:08 +1000 Subject: [PATCH 1/2] added method for counting https://github.com/BoD/android-contentprovider-generator/issues/83 --- .../abstractselection.ftl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl index 258d4a2..3c3d5f5 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl @@ -9,6 +9,7 @@ import java.util.List; import android.content.Context; import android.content.ContentResolver; +import android.database.Cursor; import android.net.Uri; public abstract class AbstractSelection> { @@ -31,6 +32,7 @@ public abstract class AbstractSelection> { private static final String CONTAINS = " LIKE '%' || ? || '%'"; private static final String STARTS = " LIKE ? || '%'"; private static final String ENDS = " LIKE '%' || ?"; + private static final String COUNT = "COUNT(*)"; private final StringBuilder mSelection = new StringBuilder(); private final List mSelectionArgs = new ArrayList(5); @@ -332,4 +334,15 @@ public abstract class AbstractSelection> { mLimit = limit; return (T) this; } + + public int count(ContentResolver resolver) { + final Cursor cursor = resolver.query(uri(), new String[]{COUNT}, sel(), args(), null); + if (cursor == null) + return 0; + try { + return cursor.moveToFirst() ? cursor.getInt(0) : 0; + } finally { + cursor.close(); + } + } } From 8817904bede85fd839163bf218a13b61f0599654 Mon Sep 17 00:00:00 2001 From: Igor Perepelitsa Date: Sat, 18 Jul 2015 18:11:30 +1000 Subject: [PATCH 2/2] Added order string generation --- .../abstractselection.ftl | 24 +++++++++++++++++-- .../selection.ftl | 24 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl index 3c3d5f5..02631cb 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl @@ -33,10 +33,13 @@ public abstract class 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 mSelectionArgs = new ArrayList(5); + private final StringBuilder mOrderBy = new StringBuilder(); + Boolean mNotify; String mGroupBy; String mHaving; @@ -276,6 +279,12 @@ public abstract class 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. @@ -335,10 +344,21 @@ public abstract class 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 { diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl index dce1218..b093b62 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl @@ -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()> @@ -243,6 +257,16 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity } <#break> + + public ${entity.nameCamelCase}Selection orderBy<#if field.isForeign>${field.path}${field.nameCamelCase}<#else>${field.nameCamelCase}(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}() { + orderBy(${field.entity.nameCamelCase}Columns.${field.nameUpperCase}, false); + return this; + } }