Skip to content

Commit

Permalink
Merge branch 'yargray-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Jul 18, 2015
2 parents f4a8bde + 8817904 commit 81a0c65
Show file tree
Hide file tree
Showing 22 changed files with 1,014 additions and 106 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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!).
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <input folder> -o <output folder>`
`java -jar android_contentprovider_generator-1.9.3-bundle.jar -i <input folder> -o <output folder>`
- Input folder: where to find `_config.json` and your entity json files
- Output folder: where the resulting files will be generated

Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")");
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends AbstractSelection<?>> {
Expand All @@ -51,10 +53,14 @@ public abstract class AbstractSelection<T extends 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<String> mSelectionArgs = new ArrayList<String>(5);

private final StringBuilder mOrderBy = new StringBuilder();

Boolean mNotify;
String mGroupBy;
String mHaving;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.Date;

import android.content.Context;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
Expand All @@ -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);
}


Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 81a0c65

Please sign in to comment.