Skip to content

Commit

Permalink
Show person's relations in PersonActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
teinvdlugt committed Feb 21, 2016
1 parent e4053bc commit 21d261f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
17 changes: 16 additions & 1 deletion app/src/main/java/com/teinvdlugt/android/greekgods/DBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

public class DBUtils {
/**
* Format this String using one argument, being the id of the person
* Format this String using one argument, being the id
* of the person you want to know the parents of
*/
public static final String PARENTS_QUERY = "SELECT p.name\n" +
"FROM people p\n" +
Expand All @@ -40,4 +41,18 @@ public class DBUtils {
" ))\n" +
")";

/**
* Format this String using one argument, being the id of
* the person you want to know the relations of
*/
public static final String RELATIONS_QUERY = "SELECT\n" +
" p.personId,\n" +
" p.name,\n" +
" r.relatiod_id\n" +
"FROM people p, relations r\n" +
"WHERE (r.personId1 = %1$d AND p.personId = r.personId2)\n" +
" OR (r.personId2 = %1$d AND p.personId = r.personId1)\n" +
" OR (((r.personId1 IS NULL AND r.personId2 = %1$d)\n" +
" OR r.personId2 IS NULL AND r.personId1 = %1$d) AND p.personId = %1$d)";

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void refresh() {
new AsyncTask<Void, Void, Void>() {
private String name;
private List<String> parents;
private String relations;
private List<String> relations;

@Override
protected Void doInBackground(Void... params) {
Expand Down Expand Up @@ -99,6 +99,24 @@ protected Void doInBackground(Void... params) {
if (c != null) c.close();
}

// Relations
try {
String relationsQuery = String.format(DBUtils.RELATIONS_QUERY, personId);
c = db.rawQuery(relationsQuery, null);
int nameColumn = c.getColumnIndex("name");
int relationIdColumn = c.getColumnIndex("relatiod_id");
c.moveToFirst();
relations = new ArrayList<>();
do {
relations.add(c.getString(nameColumn));
} while (c.moveToNext());
} catch (SQLiteException e) {
e.printStackTrace();
} catch (CursorIndexOutOfBoundsException ignored) {
} finally {
if (c != null) c.close();
}

db.close();
return null;
}
Expand All @@ -118,6 +136,16 @@ protected void onPostExecute(Void aVoid) {
sb.delete(sb.length() - 2, sb.length());
parentsTextView.setText(sb);
}
if (relations == null || relations.isEmpty()) {
relationsTextView.setText(R.string.no_relations);
} else {
StringBuilder sb = new StringBuilder();
for (String relation : relations) {
sb.append(relation).append("\n");
}
sb.delete(sb.length() - 1, sb.length());
relationsTextView.setText(sb);
}
}
}.execute();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<string name="person">Persoon</string>
<string name="something_went_wrong">Er is iets mis gegaan</string>
<string name="no_parents">Geen ouders</string>
<string name="no_relations">Geen relaties</string>
</resources>
9 changes: 9 additions & 0 deletions sql/relationsofperson.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT
p.personId,
p.name,
r.relatiod_id
FROM kcv.people p, kcv.relations r
WHERE (r.personId1 = 64 AND p.personId = r.personId2)
OR (r.personId2 = 64 AND p.personId = r.personId1)
OR (((r.personId1 IS NULL AND r.personId2 = 64)
OR r.personId2 IS NULL AND r.personId1 = 64) AND p.personId = 64)

0 comments on commit 21d261f

Please sign in to comment.