diff --git a/CHANGELOG.md b/CHANGELOG.md index e1847d3..4e30e42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ Android ContentProvider Generator Changelog =========================================== -v1.9.3 (not yet released) +v1.9.3 (2015-07-18) ------ - Updated content provider template to support ${applicationId} variable that is supported in the new manifest merger build tool (thanks almilli!). diff --git a/README.md b/README.md index 4f1fdff..8ef5350 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ https://github.com/BoD/android-contentprovider-generator/releases/latest ### Run the tool -`java -jar android_contentprovider_generator-1.9.2-bundle.jar -i -o ` +`java -jar android_contentprovider_generator-1.9.3-bundle.jar -i -o ` - Input folder: where to find `_config.json` and your entity json files - Output folder: where the resulting files will be generated @@ -150,7 +150,7 @@ Long age = person.getAge(); ```java PersonSelection where = new PersonSelection(); -where.firstName("John").or().age(42); +where.firstName("John").or().age(42).orderByFirstName(); PersonCursor person = where.query(getContentResolver()); person.moveToNext(); String lastName = person.getLastName(); @@ -224,7 +224,7 @@ You need maven to build this tool. `mvn package` -This will produce `android_contentprovider_generator-1.9.2-bundle.jar` in the `target` folder. +This will produce `android_contentprovider_generator-1.9.3-bundle.jar` in the `target` folder. Similar tools diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/app/SampleActivity.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/app/SampleActivity.java index c9417a3..1e204d9 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/app/SampleActivity.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/app/SampleActivity.java @@ -127,10 +127,10 @@ private void queryPeople() { } c.close(); - // Like / startsWitdh / contains / endsWith + // Like / startsWitdh / contains / endsWith + order by Log.d(TAG, "---"); personSelection = new PersonSelection(); - personSelection.lastNameEndsWith("SON").or().firstNameContains("ar", "ae"); + personSelection.lastNameEndsWith("SON").or().firstNameContains("ar", "ae").orderByLastName(); c = personSelection.query(getContentResolver(), projection); while (c.moveToNext()) { Log.d(TAG, c.getId() + " - " + c.getFirstName() + " " + c.getLastName() + " (age: " + c.getAge() + ")"); @@ -191,7 +191,7 @@ private void queryPeopleWithTeamAndCompany() { private void queryTeamsWithCompany() { TeamSelection teamSelection = new TeamSelection(); - teamSelection.name("Red Legends"); + teamSelection.name("Red Legends").orderByCompanySerialNumberId(true); String[] projection = { TeamColumns._ID, TeamColumns.NAME, CompanyColumns.NAME, }; TeamCursor c = teamSelection.query(getContentResolver(), projection); while (c.moveToNext()) { diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractContentValues.java index ac5598c..3c7ce45 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractContentValues.java @@ -24,6 +24,7 @@ */ package org.jraf.androidcontentprovidergenerator.sample.provider.base; +import android.content.Context; import android.content.ContentResolver; import android.content.ContentValues; import android.net.Uri; @@ -45,10 +46,19 @@ public ContentValues values() { /** * Inserts a row into a table using the values stored by this object. - * + * * @param contentResolver The content resolver to use. */ public Uri insert(ContentResolver contentResolver) { return contentResolver.insert(uri(), values()); } + + /** + * Inserts a row into a table using the values stored by this object. + * + * @param context The context to use. + */ + public Uri insert(Context context) { + return context.getContentResolver().insert(uri(), values()); + } } \ No newline at end of file diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractSelection.java index 2d50bf0..8e2ea0a 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/base/AbstractSelection.java @@ -28,7 +28,9 @@ import java.util.Date; import java.util.List; +import android.content.Context; import android.content.ContentResolver; +import android.database.Cursor; import android.net.Uri; public abstract class AbstractSelection> { @@ -51,10 +53,14 @@ 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 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; @@ -294,6 +300,12 @@ public String[] args() { 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. @@ -319,6 +331,16 @@ public int delete(ContentResolver contentResolver) { return contentResolver.delete(uri(), sel(), args()); } + /** + * Deletes row(s) specified by this selection. + * + * @param context The context to use. + * @return The number of rows deleted. + */ + public int delete(Context context) { + return context.getContentResolver().delete(uri(), sel(), args()); + } + @SuppressWarnings("unchecked") public T notify(boolean notify) { mNotify = notify; @@ -342,4 +364,34 @@ public T limit(int limit) { mLimit = limit; 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); + } + + @SuppressWarnings("unchecked") + public T orderBy(String... orders) { + for (String order : orders) { + orderBy(order, false); + } + return (T) this; + } + + public int count(ContentResolver resolver) { + 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(); + } + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyContentValues.java index f489d8d..d04e8fa 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable CompanySelection wh return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable CompanySelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + /** * The commercial name of this company. */ diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanySelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanySelection.java index f9b938d..dce55c2 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanySelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanySelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -47,28 +48,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code CompanyCursor} object, which is positioned before the first entry, or null. */ - public CompanyCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public CompanyCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new CompanyCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public CompanyCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public CompanyCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 CompanyCursor} object, which is positioned before the first entry, or null. */ - public CompanyCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public CompanyCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new CompanyCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public CompanyCursor query(Context context) { + return query(context, null); } @@ -77,6 +89,20 @@ public CompanySelection id(long... value) { return this; } + public CompanySelection idNot(long... value) { + addNotEquals("company." + CompanyColumns._ID, toObjectArray(value)); + return this; + } + + public CompanySelection orderById(boolean desc) { + orderBy("company." + CompanyColumns._ID, desc); + return this; + } + + public CompanySelection orderById() { + return orderById(false); + } + public CompanySelection name(String... value) { addEquals(CompanyColumns.NAME, value); return this; @@ -107,6 +133,16 @@ public CompanySelection nameEndsWith(String... value) { return this; } + public CompanySelection orderByName(boolean desc) { + orderBy(CompanyColumns.NAME, desc); + return this; + } + + public CompanySelection orderByName() { + orderBy(CompanyColumns.NAME, false); + return this; + } + public CompanySelection address(String... value) { addEquals(CompanyColumns.ADDRESS, value); return this; @@ -137,6 +173,16 @@ public CompanySelection addressEndsWith(String... value) { return this; } + public CompanySelection orderByAddress(boolean desc) { + orderBy(CompanyColumns.ADDRESS, desc); + return this; + } + + public CompanySelection orderByAddress() { + orderBy(CompanyColumns.ADDRESS, false); + return this; + } + public CompanySelection serialNumberId(long... value) { addEquals(CompanyColumns.SERIAL_NUMBER_ID, toObjectArray(value)); return this; @@ -167,6 +213,16 @@ public CompanySelection serialNumberIdLtEq(long value) { return this; } + public CompanySelection orderBySerialNumberId(boolean desc) { + orderBy(CompanyColumns.SERIAL_NUMBER_ID, desc); + return this; + } + + public CompanySelection orderBySerialNumberId() { + orderBy(CompanyColumns.SERIAL_NUMBER_ID, false); + return this; + } + public CompanySelection serialNumberPart0(String... value) { addEquals(SerialNumberColumns.PART0, value); return this; @@ -197,6 +253,16 @@ public CompanySelection serialNumberPart0EndsWith(String... value) { return this; } + public CompanySelection orderBySerialNumberPart0(boolean desc) { + orderBy(SerialNumberColumns.PART0, desc); + return this; + } + + public CompanySelection orderBySerialNumberPart0() { + orderBy(SerialNumberColumns.PART0, false); + return this; + } + public CompanySelection serialNumberPart1(String... value) { addEquals(SerialNumberColumns.PART1, value); return this; @@ -226,4 +292,14 @@ public CompanySelection serialNumberPart1EndsWith(String... value) { addEndsWith(SerialNumberColumns.PART1, value); return this; } + + public CompanySelection orderBySerialNumberPart1(boolean desc) { + orderBy(SerialNumberColumns.PART1, desc); + return this; + } + + public CompanySelection orderBySerialNumberPart1() { + orderBy(SerialNumberColumns.PART1, false); + return this; + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualContentValues.java index 883f739..ac3cf0f 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable ManualSelection whe return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable ManualSelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + public ManualContentValues putTitle(@NonNull String value) { if (value == null) throw new IllegalArgumentException("title must not be null"); mContentValues.put(ManualColumns.TITLE, value); diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualSelection.java index d449596..28831f9 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualSelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -46,28 +47,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code ManualCursor} object, which is positioned before the first entry, or null. */ - public ManualCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public ManualCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new ManualCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public ManualCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public ManualCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 ManualCursor} object, which is positioned before the first entry, or null. */ - public ManualCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public ManualCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new ManualCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public ManualCursor query(Context context) { + return query(context, null); } @@ -76,6 +88,20 @@ public ManualSelection id(long... value) { return this; } + public ManualSelection idNot(long... value) { + addNotEquals("manual." + ManualColumns._ID, toObjectArray(value)); + return this; + } + + public ManualSelection orderById(boolean desc) { + orderBy("manual." + ManualColumns._ID, desc); + return this; + } + + public ManualSelection orderById() { + return orderById(false); + } + public ManualSelection title(String... value) { addEquals(ManualColumns.TITLE, value); return this; @@ -106,6 +132,16 @@ public ManualSelection titleEndsWith(String... value) { return this; } + public ManualSelection orderByTitle(boolean desc) { + orderBy(ManualColumns.TITLE, desc); + return this; + } + + public ManualSelection orderByTitle() { + orderBy(ManualColumns.TITLE, false); + return this; + } + public ManualSelection isbn(String... value) { addEquals(ManualColumns.ISBN, value); return this; @@ -135,4 +171,14 @@ public ManualSelection isbnEndsWith(String... value) { addEndsWith(ManualColumns.ISBN, value); return this; } + + public ManualSelection orderByIsbn(boolean desc) { + orderBy(ManualColumns.ISBN, desc); + return this; + } + + public ManualSelection orderByIsbn() { + orderBy(ManualColumns.ISBN, false); + return this; + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonContentValues.java index 91aa97d..de112ef 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable PersonSelection whe return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable PersonSelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + /** * First name of this person. For instance, John. */ diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonSelection.java index 7e388fe..4f5f1c8 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonSelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -46,28 +47,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code PersonCursor} object, which is positioned before the first entry, or null. */ - public PersonCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public PersonCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new PersonCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public PersonCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public PersonCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 PersonCursor} object, which is positioned before the first entry, or null. */ - public PersonCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public PersonCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new PersonCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public PersonCursor query(Context context) { + return query(context, null); } @@ -76,6 +88,20 @@ public PersonSelection id(long... value) { return this; } + public PersonSelection idNot(long... value) { + addNotEquals("person." + PersonColumns._ID, toObjectArray(value)); + return this; + } + + public PersonSelection orderById(boolean desc) { + orderBy("person." + PersonColumns._ID, desc); + return this; + } + + public PersonSelection orderById() { + return orderById(false); + } + public PersonSelection firstName(String... value) { addEquals(PersonColumns.FIRST_NAME, value); return this; @@ -106,6 +132,16 @@ public PersonSelection firstNameEndsWith(String... value) { return this; } + public PersonSelection orderByFirstName(boolean desc) { + orderBy(PersonColumns.FIRST_NAME, desc); + return this; + } + + public PersonSelection orderByFirstName() { + orderBy(PersonColumns.FIRST_NAME, false); + return this; + } + public PersonSelection lastName(String... value) { addEquals(PersonColumns.LAST_NAME, value); return this; @@ -136,6 +172,16 @@ public PersonSelection lastNameEndsWith(String... value) { return this; } + public PersonSelection orderByLastName(boolean desc) { + orderBy(PersonColumns.LAST_NAME, desc); + return this; + } + + public PersonSelection orderByLastName() { + orderBy(PersonColumns.LAST_NAME, false); + return this; + } + public PersonSelection age(int... value) { addEquals(PersonColumns.AGE, toObjectArray(value)); return this; @@ -166,6 +212,16 @@ public PersonSelection ageLtEq(int value) { return this; } + public PersonSelection orderByAge(boolean desc) { + orderBy(PersonColumns.AGE, desc); + return this; + } + + public PersonSelection orderByAge() { + orderBy(PersonColumns.AGE, false); + return this; + } + public PersonSelection birthDate(Date... value) { addEquals(PersonColumns.BIRTH_DATE, value); return this; @@ -201,11 +257,31 @@ public PersonSelection birthDateBeforeEq(Date value) { return this; } + public PersonSelection orderByBirthDate(boolean desc) { + orderBy(PersonColumns.BIRTH_DATE, desc); + return this; + } + + public PersonSelection orderByBirthDate() { + orderBy(PersonColumns.BIRTH_DATE, false); + return this; + } + public PersonSelection hasBlueEyes(boolean value) { addEquals(PersonColumns.HAS_BLUE_EYES, toObjectArray(value)); return this; } + public PersonSelection orderByHasBlueEyes(boolean desc) { + orderBy(PersonColumns.HAS_BLUE_EYES, desc); + return this; + } + + public PersonSelection orderByHasBlueEyes() { + orderBy(PersonColumns.HAS_BLUE_EYES, false); + return this; + } + public PersonSelection height(Float... value) { addEquals(PersonColumns.HEIGHT, value); return this; @@ -236,6 +312,16 @@ public PersonSelection heightLtEq(float value) { return this; } + public PersonSelection orderByHeight(boolean desc) { + orderBy(PersonColumns.HEIGHT, desc); + return this; + } + + public PersonSelection orderByHeight() { + orderBy(PersonColumns.HEIGHT, false); + return this; + } + public PersonSelection gender(Gender... value) { addEquals(PersonColumns.GENDER, value); return this; @@ -247,6 +333,16 @@ public PersonSelection genderNot(Gender... value) { } + public PersonSelection orderByGender(boolean desc) { + orderBy(PersonColumns.GENDER, desc); + return this; + } + + public PersonSelection orderByGender() { + orderBy(PersonColumns.GENDER, false); + return this; + } + public PersonSelection countryCode(String... value) { addEquals(PersonColumns.COUNTRY_CODE, value); return this; @@ -276,4 +372,14 @@ public PersonSelection countryCodeEndsWith(String... value) { addEndsWith(PersonColumns.COUNTRY_CODE, value); return this; } + + public PersonSelection orderByCountryCode(boolean desc) { + orderBy(PersonColumns.COUNTRY_CODE, desc); + return this; + } + + public PersonSelection orderByCountryCode() { + orderBy(PersonColumns.COUNTRY_CODE, false); + return this; + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamContentValues.java index d644f3f..1570da6 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable PersonTeamSelection return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable PersonTeamSelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + public PersonTeamContentValues putPersonId(long value) { mContentValues.put(PersonTeamColumns.PERSON_ID, value); return this; diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamSelection.java index 4a4192a..c57d18b 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamSelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -51,28 +52,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code PersonTeamCursor} object, which is positioned before the first entry, or null. */ - public PersonTeamCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public PersonTeamCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new PersonTeamCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public PersonTeamCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public PersonTeamCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 PersonTeamCursor} object, which is positioned before the first entry, or null. */ - public PersonTeamCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public PersonTeamCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new PersonTeamCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public PersonTeamCursor query(Context context) { + return query(context, null); } @@ -81,6 +93,20 @@ public PersonTeamSelection id(long... value) { return this; } + public PersonTeamSelection idNot(long... value) { + addNotEquals("person_team." + PersonTeamColumns._ID, toObjectArray(value)); + return this; + } + + public PersonTeamSelection orderById(boolean desc) { + orderBy("person_team." + PersonTeamColumns._ID, desc); + return this; + } + + public PersonTeamSelection orderById() { + return orderById(false); + } + public PersonTeamSelection personId(long... value) { addEquals(PersonTeamColumns.PERSON_ID, toObjectArray(value)); return this; @@ -111,6 +137,16 @@ public PersonTeamSelection personIdLtEq(long value) { return this; } + public PersonTeamSelection orderByPersonId(boolean desc) { + orderBy(PersonTeamColumns.PERSON_ID, desc); + return this; + } + + public PersonTeamSelection orderByPersonId() { + orderBy(PersonTeamColumns.PERSON_ID, false); + return this; + } + public PersonTeamSelection personFirstName(String... value) { addEquals(PersonColumns.FIRST_NAME, value); return this; @@ -141,6 +177,16 @@ public PersonTeamSelection personFirstNameEndsWith(String... value) { return this; } + public PersonTeamSelection orderByPersonFirstName(boolean desc) { + orderBy(PersonColumns.FIRST_NAME, desc); + return this; + } + + public PersonTeamSelection orderByPersonFirstName() { + orderBy(PersonColumns.FIRST_NAME, false); + return this; + } + public PersonTeamSelection personLastName(String... value) { addEquals(PersonColumns.LAST_NAME, value); return this; @@ -171,6 +217,16 @@ public PersonTeamSelection personLastNameEndsWith(String... value) { return this; } + public PersonTeamSelection orderByPersonLastName(boolean desc) { + orderBy(PersonColumns.LAST_NAME, desc); + return this; + } + + public PersonTeamSelection orderByPersonLastName() { + orderBy(PersonColumns.LAST_NAME, false); + return this; + } + public PersonTeamSelection personAge(int... value) { addEquals(PersonColumns.AGE, toObjectArray(value)); return this; @@ -201,6 +257,16 @@ public PersonTeamSelection personAgeLtEq(int value) { return this; } + public PersonTeamSelection orderByPersonAge(boolean desc) { + orderBy(PersonColumns.AGE, desc); + return this; + } + + public PersonTeamSelection orderByPersonAge() { + orderBy(PersonColumns.AGE, false); + return this; + } + public PersonTeamSelection personBirthDate(Date... value) { addEquals(PersonColumns.BIRTH_DATE, value); return this; @@ -236,11 +302,31 @@ public PersonTeamSelection personBirthDateBeforeEq(Date value) { return this; } + public PersonTeamSelection orderByPersonBirthDate(boolean desc) { + orderBy(PersonColumns.BIRTH_DATE, desc); + return this; + } + + public PersonTeamSelection orderByPersonBirthDate() { + orderBy(PersonColumns.BIRTH_DATE, false); + return this; + } + public PersonTeamSelection personHasBlueEyes(boolean value) { addEquals(PersonColumns.HAS_BLUE_EYES, toObjectArray(value)); return this; } + public PersonTeamSelection orderByPersonHasBlueEyes(boolean desc) { + orderBy(PersonColumns.HAS_BLUE_EYES, desc); + return this; + } + + public PersonTeamSelection orderByPersonHasBlueEyes() { + orderBy(PersonColumns.HAS_BLUE_EYES, false); + return this; + } + public PersonTeamSelection personHeight(Float... value) { addEquals(PersonColumns.HEIGHT, value); return this; @@ -271,6 +357,16 @@ public PersonTeamSelection personHeightLtEq(float value) { return this; } + public PersonTeamSelection orderByPersonHeight(boolean desc) { + orderBy(PersonColumns.HEIGHT, desc); + return this; + } + + public PersonTeamSelection orderByPersonHeight() { + orderBy(PersonColumns.HEIGHT, false); + return this; + } + public PersonTeamSelection personGender(Gender... value) { addEquals(PersonColumns.GENDER, value); return this; @@ -282,6 +378,16 @@ public PersonTeamSelection personGenderNot(Gender... value) { } + public PersonTeamSelection orderByPersonGender(boolean desc) { + orderBy(PersonColumns.GENDER, desc); + return this; + } + + public PersonTeamSelection orderByPersonGender() { + orderBy(PersonColumns.GENDER, false); + return this; + } + public PersonTeamSelection personCountryCode(String... value) { addEquals(PersonColumns.COUNTRY_CODE, value); return this; @@ -312,6 +418,16 @@ public PersonTeamSelection personCountryCodeEndsWith(String... value) { return this; } + public PersonTeamSelection orderByPersonCountryCode(boolean desc) { + orderBy(PersonColumns.COUNTRY_CODE, desc); + return this; + } + + public PersonTeamSelection orderByPersonCountryCode() { + orderBy(PersonColumns.COUNTRY_CODE, false); + return this; + } + public PersonTeamSelection teamId(long... value) { addEquals(PersonTeamColumns.TEAM_ID, toObjectArray(value)); return this; @@ -342,6 +458,16 @@ public PersonTeamSelection teamIdLtEq(long value) { return this; } + public PersonTeamSelection orderByTeamId(boolean desc) { + orderBy(PersonTeamColumns.TEAM_ID, desc); + return this; + } + + public PersonTeamSelection orderByTeamId() { + orderBy(PersonTeamColumns.TEAM_ID, false); + return this; + } + public PersonTeamSelection teamCompanyId(long... value) { addEquals(TeamColumns.COMPANY_ID, toObjectArray(value)); return this; @@ -372,6 +498,16 @@ public PersonTeamSelection teamCompanyIdLtEq(long value) { return this; } + public PersonTeamSelection orderByTeamCompanyId(boolean desc) { + orderBy(TeamColumns.COMPANY_ID, desc); + return this; + } + + public PersonTeamSelection orderByTeamCompanyId() { + orderBy(TeamColumns.COMPANY_ID, false); + return this; + } + public PersonTeamSelection teamCompanyName(String... value) { addEquals(CompanyColumns.NAME, value); return this; @@ -402,6 +538,16 @@ public PersonTeamSelection teamCompanyNameEndsWith(String... value) { return this; } + public PersonTeamSelection orderByTeamCompanyName(boolean desc) { + orderBy(CompanyColumns.NAME, desc); + return this; + } + + public PersonTeamSelection orderByTeamCompanyName() { + orderBy(CompanyColumns.NAME, false); + return this; + } + public PersonTeamSelection teamCompanyAddress(String... value) { addEquals(CompanyColumns.ADDRESS, value); return this; @@ -432,6 +578,16 @@ public PersonTeamSelection teamCompanyAddressEndsWith(String... value) { return this; } + public PersonTeamSelection orderByTeamCompanyAddress(boolean desc) { + orderBy(CompanyColumns.ADDRESS, desc); + return this; + } + + public PersonTeamSelection orderByTeamCompanyAddress() { + orderBy(CompanyColumns.ADDRESS, false); + return this; + } + public PersonTeamSelection teamCompanySerialNumberId(long... value) { addEquals(CompanyColumns.SERIAL_NUMBER_ID, toObjectArray(value)); return this; @@ -462,6 +618,16 @@ public PersonTeamSelection teamCompanySerialNumberIdLtEq(long value) { return this; } + public PersonTeamSelection orderByTeamCompanySerialNumberId(boolean desc) { + orderBy(CompanyColumns.SERIAL_NUMBER_ID, desc); + return this; + } + + public PersonTeamSelection orderByTeamCompanySerialNumberId() { + orderBy(CompanyColumns.SERIAL_NUMBER_ID, false); + return this; + } + public PersonTeamSelection teamCompanySerialNumberPart0(String... value) { addEquals(SerialNumberColumns.PART0, value); return this; @@ -492,6 +658,16 @@ public PersonTeamSelection teamCompanySerialNumberPart0EndsWith(String... value) return this; } + public PersonTeamSelection orderByTeamCompanySerialNumberPart0(boolean desc) { + orderBy(SerialNumberColumns.PART0, desc); + return this; + } + + public PersonTeamSelection orderByTeamCompanySerialNumberPart0() { + orderBy(SerialNumberColumns.PART0, false); + return this; + } + public PersonTeamSelection teamCompanySerialNumberPart1(String... value) { addEquals(SerialNumberColumns.PART1, value); return this; @@ -522,6 +698,16 @@ public PersonTeamSelection teamCompanySerialNumberPart1EndsWith(String... value) return this; } + public PersonTeamSelection orderByTeamCompanySerialNumberPart1(boolean desc) { + orderBy(SerialNumberColumns.PART1, desc); + return this; + } + + public PersonTeamSelection orderByTeamCompanySerialNumberPart1() { + orderBy(SerialNumberColumns.PART1, false); + return this; + } + public PersonTeamSelection teamName(String... value) { addEquals(TeamColumns.NAME, value); return this; @@ -552,6 +738,16 @@ public PersonTeamSelection teamNameEndsWith(String... value) { return this; } + public PersonTeamSelection orderByTeamName(boolean desc) { + orderBy(TeamColumns.NAME, desc); + return this; + } + + public PersonTeamSelection orderByTeamName() { + orderBy(TeamColumns.NAME, false); + return this; + } + public PersonTeamSelection teamCountryCode(String... value) { addEquals(TeamColumns.COUNTRY_CODE, value); return this; @@ -582,6 +778,16 @@ public PersonTeamSelection teamCountryCodeEndsWith(String... value) { return this; } + public PersonTeamSelection orderByTeamCountryCode(boolean desc) { + orderBy(TeamColumns.COUNTRY_CODE, desc); + return this; + } + + public PersonTeamSelection orderByTeamCountryCode() { + orderBy(TeamColumns.COUNTRY_CODE, false); + return this; + } + public PersonTeamSelection teamSerialNumberId(long... value) { addEquals(TeamColumns.SERIAL_NUMBER_ID, toObjectArray(value)); return this; @@ -612,6 +818,16 @@ public PersonTeamSelection teamSerialNumberIdLtEq(long value) { return this; } + public PersonTeamSelection orderByTeamSerialNumberId(boolean desc) { + orderBy(TeamColumns.SERIAL_NUMBER_ID, desc); + return this; + } + + public PersonTeamSelection orderByTeamSerialNumberId() { + orderBy(TeamColumns.SERIAL_NUMBER_ID, false); + return this; + } + public PersonTeamSelection teamSerialNumberPart0(String... value) { addEquals(SerialNumberColumns.PART0, value); return this; @@ -642,6 +858,16 @@ public PersonTeamSelection teamSerialNumberPart0EndsWith(String... value) { return this; } + public PersonTeamSelection orderByTeamSerialNumberPart0(boolean desc) { + orderBy(SerialNumberColumns.PART0, desc); + return this; + } + + public PersonTeamSelection orderByTeamSerialNumberPart0() { + orderBy(SerialNumberColumns.PART0, false); + return this; + } + public PersonTeamSelection teamSerialNumberPart1(String... value) { addEquals(SerialNumberColumns.PART1, value); return this; @@ -671,4 +897,14 @@ public PersonTeamSelection teamSerialNumberPart1EndsWith(String... value) { addEndsWith(SerialNumberColumns.PART1, value); return this; } + + public PersonTeamSelection orderByTeamSerialNumberPart1(boolean desc) { + orderBy(SerialNumberColumns.PART1, desc); + return this; + } + + public PersonTeamSelection orderByTeamSerialNumberPart1() { + orderBy(SerialNumberColumns.PART1, false); + return this; + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductContentValues.java index cf227a6..790aa04 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable ProductSelection wh return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable ProductSelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + public ProductContentValues putProductId(long value) { mContentValues.put(ProductColumns.PRODUCT_ID, value); return this; diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductSelection.java index 1cd8991..8a05f36 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductSelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -47,28 +48,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code ProductCursor} object, which is positioned before the first entry, or null. */ - public ProductCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public ProductCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new ProductCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public ProductCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public ProductCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 ProductCursor} object, which is positioned before the first entry, or null. */ - public ProductCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public ProductCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new ProductCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public ProductCursor query(Context context) { + return query(context, null); } @@ -77,6 +89,20 @@ public ProductSelection id(long... value) { return this; } + public ProductSelection idNot(long... value) { + addNotEquals("product." + ProductColumns._ID, toObjectArray(value)); + return this; + } + + public ProductSelection orderById(boolean desc) { + orderBy("product." + ProductColumns._ID, desc); + return this; + } + + public ProductSelection orderById() { + return orderById(false); + } + public ProductSelection productId(long... value) { addEquals(ProductColumns.PRODUCT_ID, toObjectArray(value)); return this; @@ -107,6 +133,16 @@ public ProductSelection productIdLtEq(long value) { return this; } + public ProductSelection orderByProductId(boolean desc) { + orderBy(ProductColumns.PRODUCT_ID, desc); + return this; + } + + public ProductSelection orderByProductId() { + orderBy(ProductColumns.PRODUCT_ID, false); + return this; + } + public ProductSelection name(String... value) { addEquals(ProductColumns.NAME, value); return this; @@ -137,6 +173,16 @@ public ProductSelection nameEndsWith(String... value) { return this; } + public ProductSelection orderByName(boolean desc) { + orderBy(ProductColumns.NAME, desc); + return this; + } + + public ProductSelection orderByName() { + orderBy(ProductColumns.NAME, false); + return this; + } + public ProductSelection manualId(Long... value) { addEquals(ProductColumns.MANUAL_ID, value); return this; @@ -167,6 +213,16 @@ public ProductSelection manualIdLtEq(long value) { return this; } + public ProductSelection orderByManualId(boolean desc) { + orderBy(ProductColumns.MANUAL_ID, desc); + return this; + } + + public ProductSelection orderByManualId() { + orderBy(ProductColumns.MANUAL_ID, false); + return this; + } + public ProductSelection manualTitle(String... value) { addEquals(ManualColumns.TITLE, value); return this; @@ -197,6 +253,16 @@ public ProductSelection manualTitleEndsWith(String... value) { return this; } + public ProductSelection orderByManualTitle(boolean desc) { + orderBy(ManualColumns.TITLE, desc); + return this; + } + + public ProductSelection orderByManualTitle() { + orderBy(ManualColumns.TITLE, false); + return this; + } + public ProductSelection manualIsbn(String... value) { addEquals(ManualColumns.ISBN, value); return this; @@ -226,4 +292,14 @@ public ProductSelection manualIsbnEndsWith(String... value) { addEndsWith(ManualColumns.ISBN, value); return this; } + + public ProductSelection orderByManualIsbn(boolean desc) { + orderBy(ManualColumns.ISBN, desc); + return this; + } + + public ProductSelection orderByManualIsbn() { + orderBy(ManualColumns.ISBN, false); + return this; + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberContentValues.java index a7a025f..3437174 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable SerialNumberSelecti return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable SerialNumberSelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + /** * Unique id, first part. */ diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberSelection.java index 216935b..bde890b 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberSelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -46,28 +47,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code SerialNumberCursor} object, which is positioned before the first entry, or null. */ - public SerialNumberCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public SerialNumberCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new SerialNumberCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public SerialNumberCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public SerialNumberCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 SerialNumberCursor} object, which is positioned before the first entry, or null. */ - public SerialNumberCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public SerialNumberCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new SerialNumberCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public SerialNumberCursor query(Context context) { + return query(context, null); } @@ -76,6 +88,20 @@ public SerialNumberSelection id(long... value) { return this; } + public SerialNumberSelection idNot(long... value) { + addNotEquals("serial_number." + SerialNumberColumns._ID, toObjectArray(value)); + return this; + } + + public SerialNumberSelection orderById(boolean desc) { + orderBy("serial_number." + SerialNumberColumns._ID, desc); + return this; + } + + public SerialNumberSelection orderById() { + return orderById(false); + } + public SerialNumberSelection part0(String... value) { addEquals(SerialNumberColumns.PART0, value); return this; @@ -106,6 +132,16 @@ public SerialNumberSelection part0EndsWith(String... value) { return this; } + public SerialNumberSelection orderByPart0(boolean desc) { + orderBy(SerialNumberColumns.PART0, desc); + return this; + } + + public SerialNumberSelection orderByPart0() { + orderBy(SerialNumberColumns.PART0, false); + return this; + } + public SerialNumberSelection part1(String... value) { addEquals(SerialNumberColumns.PART1, value); return this; @@ -135,4 +171,14 @@ public SerialNumberSelection part1EndsWith(String... value) { addEndsWith(SerialNumberColumns.PART1, value); return this; } + + public SerialNumberSelection orderByPart1(boolean desc) { + orderBy(SerialNumberColumns.PART1, desc); + return this; + } + + public SerialNumberSelection orderByPart1() { + orderBy(SerialNumberColumns.PART1, false); + return this; + } } diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamContentValues.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamContentValues.java index d737912..86af8c6 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamContentValues.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamContentValues.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.net.Uri; import android.support.annotation.NonNull; @@ -52,6 +53,16 @@ public int update(ContentResolver contentResolver, @Nullable TeamSelection where return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); } + /** + * Update row(s) using the values stored by this object and the given selection. + * + * @param contentResolver The content resolver to use. + * @param where The selection to use (can be {@code null}). + */ + public int update(Context context, @Nullable TeamSelection where) { + return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args()); + } + public TeamContentValues putCompanyId(long value) { mContentValues.put(TeamColumns.COMPANY_ID, value); return this; diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamSelection.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamSelection.java index 8daac86..dc98ec3 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamSelection.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamSelection.java @@ -26,6 +26,7 @@ import java.util.Date; +import android.content.Context; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; @@ -49,28 +50,39 @@ protected Uri baseUri() { * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @return A {@code TeamCursor} object, which is positioned before the first entry, or null. */ - public TeamCursor query(ContentResolver contentResolver, String[] projection, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public TeamCursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new TeamCursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ - public TeamCursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); + public TeamCursor query(ContentResolver contentResolver) { + return query(contentResolver, null); } /** - * Equivalent of calling {@code query(contentResolver, projection, null, 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 TeamCursor} object, which is positioned before the first entry, or null. */ - public TeamCursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + public TeamCursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); + if (cursor == null) return null; + return new TeamCursor(cursor); + } + + /** + * Equivalent of calling {@code query(context, null)}. + */ + public TeamCursor query(Context context) { + return query(context, null); } @@ -79,6 +91,20 @@ public TeamSelection id(long... value) { return this; } + public TeamSelection idNot(long... value) { + addNotEquals("team." + TeamColumns._ID, toObjectArray(value)); + return this; + } + + public TeamSelection orderById(boolean desc) { + orderBy("team." + TeamColumns._ID, desc); + return this; + } + + public TeamSelection orderById() { + return orderById(false); + } + public TeamSelection companyId(long... value) { addEquals(TeamColumns.COMPANY_ID, toObjectArray(value)); return this; @@ -109,6 +135,16 @@ public TeamSelection companyIdLtEq(long value) { return this; } + public TeamSelection orderByCompanyId(boolean desc) { + orderBy(TeamColumns.COMPANY_ID, desc); + return this; + } + + public TeamSelection orderByCompanyId() { + orderBy(TeamColumns.COMPANY_ID, false); + return this; + } + public TeamSelection companyName(String... value) { addEquals(CompanyColumns.NAME, value); return this; @@ -139,6 +175,16 @@ public TeamSelection companyNameEndsWith(String... value) { return this; } + public TeamSelection orderByCompanyName(boolean desc) { + orderBy(CompanyColumns.NAME, desc); + return this; + } + + public TeamSelection orderByCompanyName() { + orderBy(CompanyColumns.NAME, false); + return this; + } + public TeamSelection companyAddress(String... value) { addEquals(CompanyColumns.ADDRESS, value); return this; @@ -169,6 +215,16 @@ public TeamSelection companyAddressEndsWith(String... value) { return this; } + public TeamSelection orderByCompanyAddress(boolean desc) { + orderBy(CompanyColumns.ADDRESS, desc); + return this; + } + + public TeamSelection orderByCompanyAddress() { + orderBy(CompanyColumns.ADDRESS, false); + return this; + } + public TeamSelection companySerialNumberId(long... value) { addEquals(CompanyColumns.SERIAL_NUMBER_ID, toObjectArray(value)); return this; @@ -199,6 +255,16 @@ public TeamSelection companySerialNumberIdLtEq(long value) { return this; } + public TeamSelection orderByCompanySerialNumberId(boolean desc) { + orderBy(CompanyColumns.SERIAL_NUMBER_ID, desc); + return this; + } + + public TeamSelection orderByCompanySerialNumberId() { + orderBy(CompanyColumns.SERIAL_NUMBER_ID, false); + return this; + } + public TeamSelection companySerialNumberPart0(String... value) { addEquals(SerialNumberColumns.PART0, value); return this; @@ -229,6 +295,16 @@ public TeamSelection companySerialNumberPart0EndsWith(String... value) { return this; } + public TeamSelection orderByCompanySerialNumberPart0(boolean desc) { + orderBy(SerialNumberColumns.PART0, desc); + return this; + } + + public TeamSelection orderByCompanySerialNumberPart0() { + orderBy(SerialNumberColumns.PART0, false); + return this; + } + public TeamSelection companySerialNumberPart1(String... value) { addEquals(SerialNumberColumns.PART1, value); return this; @@ -259,6 +335,16 @@ public TeamSelection companySerialNumberPart1EndsWith(String... value) { return this; } + public TeamSelection orderByCompanySerialNumberPart1(boolean desc) { + orderBy(SerialNumberColumns.PART1, desc); + return this; + } + + public TeamSelection orderByCompanySerialNumberPart1() { + orderBy(SerialNumberColumns.PART1, false); + return this; + } + public TeamSelection name(String... value) { addEquals(TeamColumns.NAME, value); return this; @@ -289,6 +375,16 @@ public TeamSelection nameEndsWith(String... value) { return this; } + public TeamSelection orderByName(boolean desc) { + orderBy(TeamColumns.NAME, desc); + return this; + } + + public TeamSelection orderByName() { + orderBy(TeamColumns.NAME, false); + return this; + } + public TeamSelection countryCode(String... value) { addEquals(TeamColumns.COUNTRY_CODE, value); return this; @@ -319,6 +415,16 @@ public TeamSelection countryCodeEndsWith(String... value) { return this; } + public TeamSelection orderByCountryCode(boolean desc) { + orderBy(TeamColumns.COUNTRY_CODE, desc); + return this; + } + + public TeamSelection orderByCountryCode() { + orderBy(TeamColumns.COUNTRY_CODE, false); + return this; + } + public TeamSelection serialNumberId(long... value) { addEquals(TeamColumns.SERIAL_NUMBER_ID, toObjectArray(value)); return this; @@ -349,6 +455,16 @@ public TeamSelection serialNumberIdLtEq(long value) { return this; } + public TeamSelection orderBySerialNumberId(boolean desc) { + orderBy(TeamColumns.SERIAL_NUMBER_ID, desc); + return this; + } + + public TeamSelection orderBySerialNumberId() { + orderBy(TeamColumns.SERIAL_NUMBER_ID, false); + return this; + } + public TeamSelection serialNumberPart0(String... value) { addEquals(SerialNumberColumns.PART0, value); return this; @@ -379,6 +495,16 @@ public TeamSelection serialNumberPart0EndsWith(String... value) { return this; } + public TeamSelection orderBySerialNumberPart0(boolean desc) { + orderBy(SerialNumberColumns.PART0, desc); + return this; + } + + public TeamSelection orderBySerialNumberPart0() { + orderBy(SerialNumberColumns.PART0, false); + return this; + } + public TeamSelection serialNumberPart1(String... value) { addEquals(SerialNumberColumns.PART1, value); return this; @@ -408,4 +534,14 @@ public TeamSelection serialNumberPart1EndsWith(String... value) { addEndsWith(SerialNumberColumns.PART1, value); return this; } + + public TeamSelection orderBySerialNumberPart1(boolean desc) { + orderBy(SerialNumberColumns.PART1, desc); + return this; + } + + public TeamSelection orderBySerialNumberPart1() { + orderBy(SerialNumberColumns.PART1, false); + return this; + } } diff --git a/pom.xml b/pom.xml index 2d9d46e..b74da6e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jraf android_contentprovider_generator - 1.9.2 + 1.9.3 jar GenerateAndroidProvider diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl index 258d4a2..098075f 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,10 +32,14 @@ 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 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; @@ -274,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. @@ -302,7 +313,7 @@ public abstract class AbstractSelection> { /** * Deletes row(s) specified by this selection. * - * @param Context the context to use. + * @param context The context to use. * @return The number of rows deleted. */ public int delete(Context context) { @@ -332,4 +343,34 @@ public abstract class AbstractSelection> { mLimit = limit; 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); + } + + @SuppressWarnings("unchecked") + public T orderBy(String... orders) { + for (String order : orders) { + orderBy(order, false); + } + return (T) this; + } + + public int count(ContentResolver resolver) { + 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(); + } + } } diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl index dce1218..47179e7 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl @@ -29,28 +29,19 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @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, String sortOrder) { - Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), sortOrder); + public ${entity.nameCamelCase}Cursor query(ContentResolver contentResolver, String[] projection) { + Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new ${entity.nameCamelCase}Cursor(cursor); } /** - * Equivalent of calling {@code query(contentResolver, projection, null)}. - */ - public ${entity.nameCamelCase}Cursor query(ContentResolver contentResolver, String[] projection) { - return query(contentResolver, projection, null); - } - - /** - * Equivalent of calling {@code query(contentResolver, projection, null, null)}. + * Equivalent of calling {@code query(contentResolver, null)}. */ public ${entity.nameCamelCase}Cursor query(ContentResolver contentResolver) { - return query(contentResolver, null, null); + return query(contentResolver, null); } /** @@ -58,28 +49,19 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity * * @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. - * @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort - * order, which may be unordered. * @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, String sortOrder) { - Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), sortOrder); + public ${entity.nameCamelCase}Cursor query(Context context, String[] projection) { + Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order()); if (cursor == null) return null; return new ${entity.nameCamelCase}Cursor(cursor); } /** - * Equivalent of calling {@code query(context, projection, null)}. - */ - public ${entity.nameCamelCase}Cursor query(Context context, String[] projection) { - return query(context, projection, null); - } - - /** - * Equivalent of calling {@code query(context, projection, null, null)}. + * Equivalent of calling {@code query(context, null)}. */ public ${entity.nameCamelCase}Cursor query(Context context) { - return query(context, null, null); + return query(context, null); } @@ -87,6 +69,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 +239,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; + } }