fixing and extending content providers update and delete

This commit is contained in:
Dominik
2012-10-31 17:33:48 +01:00
parent 3466892010
commit 1e96cf8fe9

View File

@@ -569,29 +569,34 @@ public class ApgProvider extends ContentProvider {
int count; int count;
final int match = sUriMatcher.match(uri); final int match = sUriMatcher.match(uri);
String defaultSelection = null;
switch (match) { switch (match) {
case PUBLIC_KEY_RING_BY_ROW_ID: case PUBLIC_KEY_RING_BY_ROW_ID:
// corresponding keys and userIds are deleted by ON DELETE CASCADE
count = db.delete(Tables.KEY_RINGS,
buildDefaultKeyRingsSelection(uri, KeyTypes.PUBLIC, selection), selectionArgs);
break;
case SECRET_KEY_RING_BY_ROW_ID: case SECRET_KEY_RING_BY_ROW_ID:
defaultSelection = BaseColumns._ID + "=" + uri.getLastPathSegment();
// corresponding keys and userIds are deleted by ON DELETE CASCADE // corresponding keys and userIds are deleted by ON DELETE CASCADE
count = db.delete(Tables.KEY_RINGS, count = db.delete(Tables.KEY_RINGS,
buildDefaultKeyRingsSelection(uri, KeyTypes.SECRET, selection), selectionArgs); buildDefaultKeyRingsSelection(defaultSelection, getKeyType(match), selection),
selectionArgs);
break;
case PUBLIC_KEY_RING_BY_MASTER_KEY_ID:
case SECRET_KEY_RING_BY_MASTER_KEY_ID:
defaultSelection = KeyRings.MASTER_KEY_ID + "=" + uri.getLastPathSegment();
count = db.delete(Tables.KEY_RINGS,
buildDefaultKeyRingsSelection(defaultSelection, getKeyType(match), selection),
selectionArgs);
break; break;
case PUBLIC_KEY_RING_KEY_BY_ROW_ID: case PUBLIC_KEY_RING_KEY_BY_ROW_ID:
count = db.delete(Tables.KEYS,
buildDefaultKeysSelection(uri, KeyTypes.PUBLIC, selection), selectionArgs);
case SECRET_KEY_RING_KEY_BY_ROW_ID: case SECRET_KEY_RING_KEY_BY_ROW_ID:
count = db.delete(Tables.KEYS, count = db.delete(Tables.KEYS,
buildDefaultKeysSelection(uri, KeyTypes.SECRET, selection), selectionArgs); buildDefaultKeysSelection(uri, getKeyType(match), selection), selectionArgs);
break;
case PUBLIC_KEY_RING_USER_ID_BY_ROW_ID: case PUBLIC_KEY_RING_USER_ID_BY_ROW_ID:
count = db.delete(Tables.KEYS, buildDefaultUserIdsSelection(uri, selection),
selectionArgs);
case SECRET_KEY_RING_USER_ID_BY_ROW_ID: case SECRET_KEY_RING_USER_ID_BY_ROW_ID:
count = db.delete(Tables.KEYS, buildDefaultUserIdsSelection(uri, selection), count = db.delete(Tables.KEYS, buildDefaultUserIdsSelection(uri, selection),
selectionArgs); selectionArgs);
break;
default: default:
throw new UnsupportedOperationException("Unknown uri: " + uri); throw new UnsupportedOperationException("Unknown uri: " + uri);
} }
@@ -609,32 +614,29 @@ public class ApgProvider extends ContentProvider {
final SQLiteDatabase db = mApgDatabase.getWritableDatabase(); final SQLiteDatabase db = mApgDatabase.getWritableDatabase();
String defaultSelection = null;
int count = 0; int count = 0;
try { try {
final int match = sUriMatcher.match(uri); final int match = sUriMatcher.match(uri);
switch (match) { switch (match) {
case PUBLIC_KEY_RING_BY_ROW_ID: case PUBLIC_KEY_RING_BY_ROW_ID:
count = db.update(Tables.KEY_RINGS, values,
buildDefaultKeyRingsSelection(uri, KeyTypes.PUBLIC, selection),
selectionArgs);
break;
case SECRET_KEY_RING_BY_ROW_ID: case SECRET_KEY_RING_BY_ROW_ID:
count = db.update(Tables.KEY_RINGS, values, defaultSelection = BaseColumns._ID + "=" + uri.getLastPathSegment();
buildDefaultKeyRingsSelection(uri, KeyTypes.SECRET, selection),
selectionArgs); count = db.update(
Tables.KEY_RINGS,
values,
buildDefaultKeyRingsSelection(defaultSelection, getKeyType(match),
selection), selectionArgs);
break; break;
case PUBLIC_KEY_RING_KEY_BY_ROW_ID: case PUBLIC_KEY_RING_KEY_BY_ROW_ID:
count = db.update(Tables.KEYS, values,
buildDefaultKeysSelection(uri, KeyTypes.PUBLIC, selection), selectionArgs);
break;
case SECRET_KEY_RING_KEY_BY_ROW_ID: case SECRET_KEY_RING_KEY_BY_ROW_ID:
count = db.update(Tables.KEYS, values, count = db
buildDefaultKeysSelection(uri, KeyTypes.SECRET, selection), selectionArgs); .update(Tables.KEYS, values,
buildDefaultKeysSelection(uri, getKeyType(match), selection),
selectionArgs);
break; break;
case PUBLIC_KEY_RING_USER_ID_BY_ROW_ID: case PUBLIC_KEY_RING_USER_ID_BY_ROW_ID:
count = db.update(Tables.USER_IDS, values,
buildDefaultUserIdsSelection(uri, selection), selectionArgs);
break;
case SECRET_KEY_RING_USER_ID_BY_ROW_ID: case SECRET_KEY_RING_USER_ID_BY_ROW_ID:
count = db.update(Tables.USER_IDS, values, count = db.update(Tables.USER_IDS, values,
buildDefaultUserIdsSelection(uri, selection), selectionArgs); buildDefaultUserIdsSelection(uri, selection), selectionArgs);
@@ -660,9 +662,8 @@ public class ApgProvider extends ContentProvider {
* @param selection * @param selection
* @return * @return
*/ */
private String buildDefaultKeyRingsSelection(Uri uri, Integer keyType, String selection) { private String buildDefaultKeyRingsSelection(String defaultSelection, Integer keyType,
String rowId = uri.getLastPathSegment(); String selection) {
String andType = ""; String andType = "";
if (keyType != null) { if (keyType != null) {
andType = " AND " + KeyRingsColumns.TYPE + "=" + keyType; andType = " AND " + KeyRingsColumns.TYPE + "=" + keyType;
@@ -673,7 +674,7 @@ public class ApgProvider extends ContentProvider {
andSelection = " AND (" + selection + ")"; andSelection = " AND (" + selection + ")";
} }
return BaseColumns._ID + "=" + rowId + andType + andSelection; return defaultSelection + andType + andSelection;
} }
/** /**