@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); Uri returnUri; switch (match) { case MOVIE: { long _id = db.insert(MovieContract.Movies.TABLE_NAME, null, values); if (_id > 0) returnUri = MovieContract.Movies.buildMoviesUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case FAVOURITES: { long _id = db.insert(MovieContract.Favourites.TABLE_NAME, null, values); if (_id > 0) returnUri = MovieContract.Favourites.buildMoviesUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case TRAILERS: { long _id = db.insert(MovieContract.Trailers.TABLE_NAME, null, values); if (_id > 0) returnUri = MovieContract.Trailers.buildMoviesUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case REVIEWS: { long _id = db.insert(MovieContract.Reviews.TABLE_NAME, null, values); if (_id > 0) returnUri = MovieContract.Reviews.buildMoviesUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } // case GENRES: { // long _id = db.insert(MovieContract.Genres.TABLE_NAME, null, values); // if ( _id > 0 ) // returnUri = MovieContract.Genres.buildMoviesUri(_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; }
@Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); }
@Override public int bulkInsert(Uri uri, ContentValues[] values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case MOVIE: db.beginTransaction(); int returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MovieContract.Movies.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } getContext().getContentResolver().notifyChange(uri, null); return returnCount; case FAVOURITES: db.beginTransaction(); returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MovieContract.Favourites.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } getContext().getContentResolver().notifyChange(uri, null); return returnCount; case TRAILERS: db.beginTransaction(); returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MovieContract.Trailers.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } getContext().getContentResolver().notifyChange(uri, null); return returnCount; case REVIEWS: db.beginTransaction(); returnCount = 0; try { for (ContentValues value : values) { long _id = db.insert(MovieContract.Reviews.TABLE_NAME, null, value); if (_id != -1) { returnCount++; } } db.setTransactionSuccessful(); } finally { db.endTransaction(); } getContext().getContentResolver().notifyChange(uri, null); return returnCount; // case GENRES: // db.beginTransaction(); // returnCount = 0; // try { // for (ContentValues value : values) { // // long _id = db.insert(MovieContract.Genres.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); } }
@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 MOVIE: rowsUpdated = db.update(MovieContract.Movies.TABLE_NAME, values, selection, selectionArgs); break; case MOVIE_WITH_ID: { rowsUpdated = db.update( MovieContract.Movies.TABLE_NAME, values, MovieContract.Movies.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; } case FAVOURITES: rowsUpdated = db.update(MovieContract.Favourites.TABLE_NAME, values, selection, selectionArgs); break; case FAVOURITES_WITH_ID: { rowsUpdated = db.update( MovieContract.Favourites.TABLE_NAME, values, MovieContract.Favourites.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; } case TRAILERS: rowsUpdated = db.update(MovieContract.Trailers.TABLE_NAME, values, selection, selectionArgs); break; case TRAILERS_WITH_ID: { rowsUpdated = db.update( MovieContract.Trailers.TABLE_NAME, values, MovieContract.Trailers.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; } case REVIEWS: rowsUpdated = db.update(MovieContract.Reviews.TABLE_NAME, values, selection, selectionArgs); break; case REVIEWS_WITH_ID: { rowsUpdated = db.update( MovieContract.Reviews.TABLE_NAME, values, MovieContract.Reviews.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; } // case GENRES: // rowsUpdated = db.update(MovieContract.Genres.TABLE_NAME, // values, selection, // selectionArgs); // break; // case GENRES_WITH_ID: { // rowsUpdated = db.update(MovieContract.Genres.TABLE_NAME, // values, // MovieContract.Genres.MOVIE_ID + " = ?", // new // String[]{String.valueOf(ContentUris.parseId(uri))}); // 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 MOVIE: rowsDeleted = db.delete(MovieContract.Movies.TABLE_NAME, selection, selectionArgs); break; case MOVIE_WITH_ID: rowsDeleted = db.delete( MovieContract.Movies.TABLE_NAME, MovieContract.Movies.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; case FAVOURITES: rowsDeleted = db.delete(MovieContract.Favourites.TABLE_NAME, selection, selectionArgs); break; case FAVOURITES_WITH_ID: rowsDeleted = db.delete( MovieContract.Favourites.TABLE_NAME, MovieContract.Favourites.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; case TRAILERS: rowsDeleted = db.delete(MovieContract.Trailers.TABLE_NAME, selection, selectionArgs); break; case TRAILERS_WITH_ID: rowsDeleted = db.delete( MovieContract.Trailers.TABLE_NAME, MovieContract.Trailers.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; case REVIEWS: rowsDeleted = db.delete(MovieContract.Reviews.TABLE_NAME, selection, selectionArgs); break; case REVIEWS_WITH_ID: rowsDeleted = db.delete( MovieContract.Reviews.TABLE_NAME, MovieContract.Reviews.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; // case GENRES: // rowsDeleted = db.delete( // MovieContract.Genres.TABLE_NAME, selection, selectionArgs); // break; // case GENRES_WITH_ID: // rowsDeleted = db.delete(MovieContract.Genres.TABLE_NAME, // MovieContract.Genres.MOVIE_ID + " = ?", // new String[]{String.valueOf(ContentUris.parseId(uri))}); // break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (rowsDeleted != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsDeleted; }
@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)) { case MOVIE: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Movies.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } case MOVIE_WITH_ID: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Movies.TABLE_NAME, projection, MovieContract.Movies.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}, null, null, sortOrder); break; } case FAVOURITES: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Favourites.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } case FAVOURITES_WITH_ID: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Favourites.TABLE_NAME, projection, MovieContract.Favourites.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}, null, null, sortOrder); break; } case TRAILERS: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Trailers.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } case TRAILERS_WITH_ID: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Trailers.TABLE_NAME, projection, MovieContract.Trailers.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}, null, null, sortOrder); break; } case REVIEWS: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Reviews.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } case REVIEWS_WITH_ID: { retCursor = mOpenHelper .getReadableDatabase() .query( MovieContract.Reviews.TABLE_NAME, projection, MovieContract.Reviews.MOVIE_ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}, null, null, sortOrder); break; } // case GENRES: { // retCursor = mOpenHelper.getReadableDatabase().query( // MovieContract.Genres.TABLE_NAME, // projection, // selection, // selectionArgs, // null, // null, // sortOrder // ); // break; // } // case GENRES_WITH_ID:{ // retCursor = mOpenHelper.getReadableDatabase().query( // MovieContract.Genres.TABLE_NAME, // projection, // MovieContract.Genres.MOVIE_ID+" = ?", // new String[] {String.valueOf(ContentUris.parseId(uri))}, // null, // null, // sortOrder); // break; // } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } retCursor.setNotificationUri(getContext().getContentResolver(), uri); return retCursor; }
@Override protected SQLiteOpenHelper createSqLiteOpenHelper() { return MovieDBHelper.getInstance(getContext()); }