@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); Uri returnUri; switch (sUriMatcher.match(uri)) { case MOVIES_LIST: long _id = db.insert(MoviesContract.MoviesEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = MoviesContract.MoviesEntry.buildMovieUri(_id); } else { throw new SQLException("Failed to insert row into " + uri); } break; case TRAILERS: _id = db.insert(MoviesContract.TrailersEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = MoviesContract.TrailersEntry.buildMovieUri(_id); } else { throw new SQLException("Failed to insert row into " + uri); } break; case REVIEWS: _id = db.insert(MoviesContract.ReviewsEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = MoviesContract.ReviewsEntry.buildMovieUri(_id); } else { throw new SQLException("Failed to insert row into " + uri); } break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } getContext().getContentResolver().notifyChange(uri, null); return returnUri; }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); int rowsUpdated; if (selection == null) { selection = "1"; } switch (sUriMatcher.match(uri)) { case MOVIES_LIST: rowsUpdated = db.update(MoviesContract.MoviesEntry.TABLE_NAME, values, selection, selectionArgs); break; case TRAILERS: rowsUpdated = db.update(MoviesContract.TrailersEntry.TABLE_NAME, values, selection, selectionArgs); break; case REVIEWS: rowsUpdated = db.update(MoviesContract.ReviewsEntry.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 update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final SQLiteDatabase db = moviesDbHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); int count; switch (match) { case MOVIE: { count = db.update(MoviesContract.MovieEntry.TABLE_NAME, values, selection, selectionArgs); break; } case REVIEW: { count = db.update(MoviesContract.ReviewEntry.TABLE_NAME, values, selection, selectionArgs); break; } case TRAILER: { count = db.update(MoviesContract.TrailerEntry.TABLE_NAME, values, selection, selectionArgs); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (count != 0) getContext().getContentResolver().notifyChange(uri, null); return count; }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { final SQLiteDatabase db = moviesDbHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); int deletedRows; if (null == selection) { selection = "1"; } switch (match) { case MOVIE: { deletedRows = db.delete(MoviesContract.MovieEntry.TABLE_NAME, selection, selectionArgs); break; } case REVIEW: { deletedRows = db.delete(MoviesContract.ReviewEntry.TABLE_NAME, selection, selectionArgs); break; } case TRAILER: { deletedRows = db.delete(MoviesContract.TrailerEntry.TABLE_NAME, selection, selectionArgs); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } return deletedRows; }
private Cursor getReviewsByMovieId(Uri uri, String[] projection, String sortOrder) { String movieId = MoviesContract.ReviewEntry.getMovieIdFromUri(uri); String[] selectionArgs = new String[] {movieId}; String selection = sMovieSelection; return sReviewByMovieIdQueryBuilder.query( moviesDbHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor; SQLiteDatabase db = mOpenHelper.getReadableDatabase(); switch (sUriMatcher.match(uri)) { case MOVIES_LIST: cursor = db.query( MoviesContract.MoviesEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; case TRAILERS: cursor = db.query( MoviesContract.TrailersEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null); break; case REVIEWS: cursor = db.query( MoviesContract.ReviewsEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
@Override public int bulkInsert(Uri uri, ContentValues[] values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); switch (sUriMatcher.match(uri)) { case MOVIES_LIST: db.beginTransaction(); int returnCount = 0; try { for (ContentValues value : values) { // Log.e(MoviesProvider.class.getName(), value.toString()); // long _id = // db.insertWithOnConflict(MoviesContract.MoviesEntry.TABLE_NAME, null, value, // SQLiteDatabase.CONFLICT_ABORT); // if(_id == -1) { // _id = db.update(MoviesContract.MoviesEntry.TABLE_NAME, // value, null, null); // } // long _id; // try { // _id = db.update(MoviesContract.MoviesEntry.TABLE_NAME, // value, null, null); // } // catch(SQLiteConstraintException e) { // _id = db.insert(MoviesContract.MoviesEntry.TABLE_NAME, // null, value); // } // long _id = db.update(MoviesContract.MoviesEntry.TABLE_NAME, // value, null, null); // db.insertWithOnConflict(MoviesContract.MoviesEntry.TABLE_NAME, // null, value, SQLiteDatabase.CONFLICT_IGNORE); long _id = db.insert(MoviesContract.MoviesEntry.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } return returnCount; case TRAILERS: db.beginTransaction(); returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MoviesContract.TrailersEntry.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } return returnCount; case REVIEWS: db.beginTransaction(); returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MoviesContract.ReviewsEntry.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } return returnCount; default: return super.bulkInsert(uri, values); } }
@Nullable @Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor retCursor; switch (sUriMatcher.match(uri)) { case MOVIE: { retCursor = moviesDbHelper .getReadableDatabase() .query( MoviesContract.MovieEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } case MOVIE_WITH_ID: { String movieId = MoviesContract.ReviewEntry.getMovieIdFromUri(uri); selectionArgs = new String[] {movieId}; selection = sMovieSelection; retCursor = moviesDbHelper .getReadableDatabase() .query( MoviesContract.MovieEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } case TRAILER: { retCursor = getTrailersByMovieId(uri, projection, sortOrder); break; } case REVIEW: { retCursor = getReviewsByMovieId(uri, projection, sortOrder); break; } case REVIEW_WITH_ID: { String movieId = MoviesContract.ReviewEntry.getMovieIdFromUri(uri); retCursor = sReviewByMovieIdQueryBuilder.query( moviesDbHelper.getReadableDatabase(), projection, sMovieSelection, new String[] {movieId}, null, null, sortOrder); break; } case TRAILER_WITH_ID: { String movieId = MoviesContract.TrailerEntry.getMovieIdFromUri(uri); retCursor = sTrailerByMovieIdQueryBuilder.query( moviesDbHelper.getReadableDatabase(), projection, sMovieSelection, new String[] {movieId}, null, null, sortOrder); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } retCursor.setNotificationUri(getContext().getContentResolver(), uri); return retCursor; }