@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // here's the switch statement that, given a URI, will determine what kind of request it is, // and query the database accordingly. Cursor retCursor; switch (sUriMatcher.match(uri)) { // "user/*/*" case USER_WITH_MOVIEID_AND_FAVORITE: { retCursor = getUserByMovieIDAndFavorite(uri, projection, sortOrder); break; } // "user/*" case USER_WITH_MOVIEID: { retCursor = getUserByMovieID(uri, projection, sortOrder); break; } // "user" case USER: retCursor = sSuperUserBuilder.query( mOpenHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); /* retCursor = mOpenHelper.getReadableDatabase().query( //MovieContract.UserEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder );*/ break; case MOVIEID: retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.MovieEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } retCursor.setNotificationUri(getContext().getContentResolver(), uri); return retCursor; }
@Override public int bulkInsert(Uri uri, ContentValues[] values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case USER: db.beginTransaction(); int returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MovieContract.UserEntry.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } getContext().getContentResolver().notifyChange(uri, null); return returnCount; default: return super.bulkInsert(uri, values); } }
/* Student: Add the ability to insert Locations to the implementation of this function. */ @Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); Uri returnUri; switch (match) { case USER: { long _id = db.insert(MovieContract.UserEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = MovieContract.UserEntry.buildUserUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case MOVIEID: { long _id = db.insert(MovieContract.MovieEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = MovieContract.MovieEntry.buildMovieUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return returnUri; }
private Cursor getUserByMovieIDAndFavorite(Uri uri, String[] projection, String sortOrder) { String movieID = MovieContract.UserEntry.getMovieIDFromUri(uri); boolean favorite = MovieContract.UserEntry.getFavoriteFromUri(uri); return sUserByMovieIDQueryBuilder.query( mOpenHelper.getReadableDatabase(), projection, sMovieIDWithFavoriteSelection, new String[] {movieID, Boolean.toString(favorite).toUpperCase()}, null, null, sortOrder); }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); int rowsUpdated; switch (match) { case USER: rowsUpdated = db.update(MovieContract.UserEntry.TABLE_NAME, values, selection, selectionArgs); break; case MOVIEID: rowsUpdated = db.update(MovieContract.MovieEntry.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 int delete(Uri uri, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); int rowsDeleted; // this makes delete all rows return the number of rows deleted if (null == selection) selection = "1"; switch (match) { case USER: rowsDeleted = db.delete(MovieContract.UserEntry.TABLE_NAME, selection, selectionArgs); break; case MOVIEID: rowsDeleted = db.delete(MovieContract.MovieEntry.TABLE_NAME, selection, selectionArgs); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } // Because a null deletes all rows if (rowsDeleted != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsDeleted; }
private Cursor getUserByMovieID(Uri uri, String[] projection, String sortOrder) { String movieID = MovieContract.UserEntry.getMovieIDFromUri(uri); boolean favorite = MovieContract.UserEntry.getFavoriteFromUri(uri); String[] selectionArgs; String selection; if (favorite == false) { selection = sMovieIDSelection; selectionArgs = new String[] {movieID}; } else { selectionArgs = new String[] {movieID, Boolean.toString(favorite).toUpperCase()}; selection = sMovieIDWithFavoriteSelection; } return sUserByMovieIDQueryBuilder.query( mOpenHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); }
// You do not need to call this method. This is a method specifically to assist the testing // framework in running smoothly. You can read more at: // http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() @Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); }