@Override public int delete(Uri uri, String selection, String[] selectionArgs) { final SQLiteDatabase db = dbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); int rowsDeleted; switch (match) { case BOOK: rowsDeleted = db.delete( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry.TABLE_NAME, selection, selectionArgs); break; case AUTHOR: rowsDeleted = db.delete( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.AuthorEntry.TABLE_NAME, selection, selectionArgs); break; case CATEGORY: rowsDeleted = db.delete( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.CategoryEntry .TABLE_NAME, selection, selectionArgs); break; case BOOK_ID: rowsDeleted = db.delete( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry.TABLE_NAME, com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry._ID + " = '" + ContentUris.parseId(uri) + "'", selectionArgs); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } // Because a null deletes all rows if (selection == null || rowsDeleted != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsDeleted; }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final SQLiteDatabase db = dbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); int rowsUpdated; switch (match) { case BOOK: rowsUpdated = db.update( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry.TABLE_NAME, values, selection, selectionArgs); break; case AUTHOR: rowsUpdated = db.update( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.AuthorEntry.TABLE_NAME, values, selection, selectionArgs); break; case CATEGORY: rowsUpdated = db.update( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.CategoryEntry .TABLE_NAME, values, selection, selectionArgs); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (rowsUpdated != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsUpdated; }
@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = dbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); Uri returnUri; switch (match) { case BOOK: { long _id = db.insert( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry.buildBookUri( _id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } getContext() .getContentResolver() .notifyChange( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.BookEntry .buildFullBookUri(_id), null); break; } case AUTHOR: { long _id = db.insert( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.AuthorEntry .TABLE_NAME, null, values); if (_id > 0) returnUri = com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.AuthorEntry .buildAuthorUri(values.getAsLong("_id")); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case CATEGORY: { long _id = db.insert( com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.CategoryEntry .TABLE_NAME, null, values); if (_id > 0) returnUri = com.awesome.byunghwa.app.alexandria.data.AlexandriaContract.CategoryEntry .buildCategoryUri(values.getAsLong("_id")); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } return returnUri; }