Skip to content

Commit

Permalink
Merge branch '1.8.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Nov 11, 2014
2 parents 08afc40 + c2e2420 commit bce13b4
Show file tree
Hide file tree
Showing 33 changed files with 1,133 additions and 551 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It takes a set of entity (a.k.a "table") definitions as the input, and generates
- a `ContentProvider` class
- a `SQLiteOpenHelper` class
- a `SQLiteOpenHelperCallbacks` class
- one `BaseColumns` interface per entity
- one `BaseColumns` class per entity
- one `Cursor` class per entity
- one `ContentValues` class per entity
- one `Selection` class per entity
Expand Down Expand Up @@ -194,9 +194,9 @@ In this example, the field `main_team_id` is a foreign key referencing the prima
- Of course if `team` has foreign keys they will also be handled (and recursively).

#### Limitations
- Foreign keys always reference the `_id` column (the implicit primary key of all tables) and thus must always be of type `Long` - by design.
- **Only one foreign key to a particular table is allowed per table.** In the example above only one column in `person` can point to `team`.
- **Loops** (i.e. A has a foreign key to B and B has a foreign key to A) **aren't detected.** The generator will infinitely loop if they exist.
- Foreign keys always reference the `_id` column (the implicit primary key of all tables) and thus must always be of type `Long` - by design.


Building
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.app.Activity;
import android.content.ContentUris;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -22,6 +23,10 @@
import org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonContentValues;
import org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonCursor;
import org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonSelection;
import org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamColumns;
import org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamContentValues;
import org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamCursor;
import org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamSelection;
import org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamColumns;
import org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamContentValues;
import org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamCursor;
Expand All @@ -32,6 +37,7 @@ public class SampleActivity extends Activity {

private static final String[] FIRST_NAMES = { "James", "John", "Robert", "Michael", "William", "David", "Richard", "Charles", "Joseph", "Thomas", };
private static final String[] LAST_NAMES = { "SMITH", "JOHNSON", "WILLIAMS", "BROWN", "JONES", "MILLER", "DAVIS", "GARCIA", "RODRIGUEZ", "WILSON", };
private static final String[] COUNTRY_CODES = { "fr", "uk", "us", "de", "be", };
private static long YEAR_IN_MS = 365 * 24 * 60 * 60 * 1000 * 1000L;
private static final Random sRandom = new Random();

Expand Down Expand Up @@ -110,24 +116,27 @@ private void queryPeople() {
}

private void queryPeopleWithTeam() {
PersonSelection personSelection = new PersonSelection();
personSelection.firstName("James", "John");
String[] projection = { PersonColumns._ID, PersonColumns.FIRST_NAME, PersonColumns.LAST_NAME, PersonColumns.AGE, TeamColumns.NAME };
PersonCursor c = personSelection.query(getContentResolver(), projection);
PersonTeamSelection personTeamSelection = new PersonTeamSelection();
personTeamSelection.personFirstName("James", "John");
String[] projection = { PersonTeamColumns._ID, PersonColumns.FIRST_NAME, PersonColumns.LAST_NAME, PersonColumns.AGE, PersonColumns.COUNTRY_CODE,
TeamColumns.NAME, TeamColumns.COUNTRY_CODE };
PersonTeamCursor c = personTeamSelection.query(getContentResolver(), projection);
while (c.moveToNext()) {
Log.d(TAG, c.getFirstName() + " " + c.getLastName() + " (age: " + c.getAge() + ") - team: " + c.getTeamName());
Log.d(TAG, c.getPersonFirstName() + " " + c.getPersonLastName() + " (age: " + c.getPersonAge() + ", country code:" + c.getPersonCountryCode()
+ " ) - team: " + c.getTeamName() + " (country code: " + c.getTeamCountryCode() + ")");
}
c.close();
}

private void queryPeopleWithTeamAndCompany() {
PersonSelection personSelection = new PersonSelection();
personSelection.firstName("James", "John");
String[] projection = { PersonColumns._ID, PersonColumns.FIRST_NAME, PersonColumns.LAST_NAME, PersonColumns.AGE, TeamColumns.NAME, CompanyColumns.NAME,
TeamColumns._ID, CompanyColumns._ID };
PersonCursor c = personSelection.query(getContentResolver(), projection);
PersonTeamSelection personTeamSelection = new PersonTeamSelection();
personTeamSelection.personFirstName("James", "John");
String[] projection = { PersonTeamColumns._ID, PersonColumns.FIRST_NAME, PersonColumns.LAST_NAME, PersonColumns.AGE, PersonColumns.COUNTRY_CODE,
TeamColumns.NAME, TeamColumns.COUNTRY_CODE, CompanyColumns.NAME };
PersonTeamCursor c = personTeamSelection.query(getContentResolver(), projection);
while (c.moveToNext()) {
Log.d(TAG, c.getFirstName() + " " + c.getLastName() + " (age: " + c.getAge() + ") - team: " + c.getTeamName() + " - company: " + c.getCompanyName());
Log.d(TAG, c.getPersonFirstName() + " " + c.getPersonLastName() + " (age: " + c.getPersonAge() + ", country code:" + c.getPersonCountryCode()
+ " ) - team: " + c.getTeamName() + " (country code: " + c.getTeamCountryCode() + ") - company: " + c.getCompanyName());
}
c.close();
}
Expand All @@ -144,25 +153,38 @@ private void queryTeamsWithCompany() {
}

private void deleteBase() {
// Delete person-teams first
PersonTeamSelection personTeamSelection = new PersonTeamSelection();
personTeamSelection.delete(getContentResolver());

// Now delete companies (which should also delete teams because of the "on delete cascade" constraint
CompanySelection companySelection = new CompanySelection();
companySelection.delete(getContentResolver());

// Now delete persons
PersonSelection personSelection = new PersonSelection();
personSelection.delete(getContentResolver());
}

private void populateBase() {
// Insert companies
long google = insertCompany("Google", "1600 Amphitheatre Pkwy, Mountain View, CA 94043");
long microsoft = insertCompany("Microsoft", "One Microsoft Way Redmond, WA 98052-7329 USA");
long apple = insertCompany("Apple", "1 Infinite Loop, Cupertino, CA 95014");

// Insert teams
ArrayList<Long> teams = new ArrayList<>();
teams.add(insertTeam(google, "Nunchuk Geckos"));
teams.add(insertTeam(google, "Dancing Dingoes"));
teams.add(insertTeam(google, "American Dragons"));
teams.add(insertTeam(microsoft, "New York Lightning"));
teams.add(insertTeam(microsoft, "Red Legends"));
teams.add(insertTeam(microsoft, "Awesome Predators"));
teams.add(insertTeam(apple, "Cyborg Chasers"));
teams.add(insertTeam(apple, "Kamikaze Falcons"));
teams.add(insertTeam(google, "Nunchuk Geckos", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(google, "Dancing Dingoes", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(google, "American Dragons", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(microsoft, "New York Lightning", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(microsoft, "Red Legends", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(microsoft, "Awesome Predators", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(apple, "Cyborg Chasers", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));
teams.add(insertTeam(apple, "Kamikaze Falcons", COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)]));

// Insert persons
ArrayList<Long> persons = new ArrayList<>();
for (int i = 0; i < 40; i++) {
long mainTeamId = teams.get(sRandom.nextInt(teams.size()));
long secondaryTeamId = teams.get(sRandom.nextInt(teams.size()));
Expand All @@ -173,11 +195,27 @@ private void populateBase() {
boolean hasBlueEyes = sRandom.nextBoolean();
Float height = sRandom.nextFloat() + 1f;
Gender gender = Gender.values()[sRandom.nextInt(Gender.values().length)];
insertPerson(mainTeamId, secondaryTeamId, firstName, lastName, age, birthDate, hasBlueEyes, height, gender);
String countryCode = COUNTRY_CODES[sRandom.nextInt(COUNTRY_CODES.length)];
persons.add(insertPerson(mainTeamId, secondaryTeamId, firstName, lastName, age, birthDate, hasBlueEyes, height, gender, countryCode));
}

// Insert persons-teams
for (int i = 0; i < 150; i++) {
try {
insertPersonTeam(persons.get(sRandom.nextInt(persons.size())), teams.get(sRandom.nextInt(teams.size())));
} catch (SQLException e) {
// This can happen because of the unique constraint on person - some ids in persons can sometimes no longer exist
Log.w(TAG, "populateBase Trying to join with a person id that does not exist: this can happen, and should be expected", e);
}
}

}

/**
* Insert a company.
*
* @return the id of the created company.
*/
private long insertCompany(String name, String address) {
CompanyContentValues values = new CompanyContentValues();
values.putName(name);
Expand All @@ -186,26 +224,50 @@ private long insertCompany(String name, String address) {
return ContentUris.parseId(uri);
}

private long insertTeam(long companyId, String name) {
/**
* Insert a team.
*
* @return the id of the created team.
*/
private long insertTeam(long companyId, String name, String countryCode) {
TeamContentValues values = new TeamContentValues();
values.putCompanyId(companyId);
values.putName(name);
values.putCountryCode(countryCode);
Uri uri = values.insert(getContentResolver());
return ContentUris.parseId(uri);
}

/**
* Insert a person.
*
* @return the id of the created person.
*/
private long insertPerson(long mainTeamId, long secondaryTeamId, String firstName, String lastName, int age, Date birthDate, boolean hasBlueEyes,
Float height, Gender gender) {
Float height, Gender gender, String countryCode) {
PersonContentValues values = new PersonContentValues();
values.putMainTeamId(mainTeamId);
values.putFirstName(firstName);
values.putLastName(lastName);
values.putAge(age);
values.putBirthDate(birthDate);
values.putHasBlueEyes(hasBlueEyes);
values.putHeight(height);
values.putGender(gender);
values.putCountryCode(countryCode);

Uri uri = values.insert(getContentResolver());
return ContentUris.parseId(uri);
}

/**
* Insert a person-team.
*
* @return the id of the created person-team.
*/
private long insertPersonTeam(long personId, long teamId) {
PersonTeamContentValues values = new PersonTeamContentValues();
values.putPersonId(personId);
values.putTeamId(teamId);
Uri uri = values.insert(getContentResolver());
return ContentUris.parseId(uri);
}
Expand Down
Loading

0 comments on commit bce13b4

Please sign in to comment.