/* This test uses the database directly to insert and then uses the ContentProvider to read out the data. Uncomment this test to see if your location queries are performing correctly. */ public void testBasicLocationQueries() { // insert our test records into the database WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext); // Test the basic content provider query Cursor locationCursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI, null, null, null, null); // Make sure we get the correct cursor out of the database TestUtilities.validateCursor( "testBasicLocationQueries, location query", locationCursor, testValues); // Has the NotificationUri been set correctly? --- we can only test this easily against API // level 19 or greater because getNotificationUri was added in API level 19. if (Build.VERSION.SDK_INT >= 19) { assertEquals( "Error: Location Query did not properly set NotificationUri", locationCursor.getNotificationUri(), LocationEntry.CONTENT_URI); } }
public void deleteAllRecordsFromDB() { WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase database = dbHelper.getWritableDatabase(); database.delete(WeatherContract.WeatherEntry.TABLE_NAME, null, null); database.delete(WeatherContract.LocationEntry.TABLE_NAME, null, null); database.close(); }
@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)) { // "weather/*/*" case WEATHER_WITH_LOCATION_AND_DATE: { retCursor = getWeatherByLocationSettingAndDate(uri, projection, sortOrder); break; } // "weather/*" case WEATHER_WITH_LOCATION: { retCursor = getWeatherByLocationSetting(uri, projection, sortOrder); break; } // "weather" case WEATHER: { retCursor = mOpenHelper .getReadableDatabase() .query( WeatherContract.WeatherEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } // "location" case LOCATION: { retCursor = mOpenHelper .getReadableDatabase() .query( WeatherContract.LocationEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } retCursor.setNotificationUri(getContext().getContentResolver(), uri); return retCursor; }
/* Students: You can uncomment this function once you have finished creating the LocationEntry part of the WeatherContract as well as the WeatherDbHelper. */ static long insertNorthPoleLocationValues(Context context) { // insert our test records into the database WeatherDbHelper dbHelper = new WeatherDbHelper(context); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); long locationRowId; locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues); // Verify we got a row back. assertTrue("Error: Failure to insert North Pole Location Values", locationRowId != -1); return locationRowId; }
@Override public int bulkInsert(Uri uri, ContentValues[] values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case WEATHER: db.beginTransaction(); int returnCount = 0; try { for (ContentValues value : values) { normalizeDate(value); long _id = db.insert(WeatherContract.WeatherEntry.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 WEATHER: { normalizeDate(values); long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = WeatherContract.WeatherEntry.buildWeatherUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case LOCATION: { long _id = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = WeatherContract.LocationEntry.buildLocationUri(_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 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // Student: This is a lot like the delete function. We return the number of rows impacted // by the update. SQLiteDatabase db = mOpenHelper.getWritableDatabase(); int match = sUriMatcher.match(uri); int rowUpdated; switch (match) { case WEATHER: rowUpdated = db.update(WeatherEntry.TABLE_NAME, values, selection, selectionArgs); break; case LOCATION: rowUpdated = db.update(LocationEntry.TABLE_NAME, values, selection, selectionArgs); break; default: throw new UnsupportedOperationException(); } Context context = getContext(); if (rowUpdated != 0 && context != null) { context.getContentResolver().notifyChange(uri, null); } return rowUpdated; }
@Override public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) { // Student: Start by getting a writable database SQLiteDatabase db = mOpenHelper.getWritableDatabase(); // Student: Use the uriMatcher to match the WEATHER and LOCATION URI's we are going to // handle. If it doesn't match these, throw an UnsupportedOperationException. int match = sUriMatcher.match(uri); int rowDeleted; if (selection == null) selection = "1"; switch (match) { case WEATHER: rowDeleted = db.delete(WeatherEntry.TABLE_NAME, selection, selectionArgs); break; case LOCATION: rowDeleted = db.delete(LocationEntry.TABLE_NAME, selection, selectionArgs); break; default: throw new UnsupportedOperationException(); } // Student: A null value deletes all rows. In my implementation of this, I only notified // the uri listeners (using the content resolver) if the rowsDeleted != 0 or the selection // is null. // Oh, and you should notify the listeners here. Context context = getContext(); if (rowDeleted != 0 && context != null) { context.getContentResolver().notifyChange(uri, null); } // Student: return the actual rows deleted return rowDeleted; }
/* Students: Here is where you will build code to test that we can insert and query the database. We've done a lot of work for you. You'll want to look in TestUtilities where you can use the "createWeatherValues" function. You can also make use of the validateCurrentRecord function from within TestUtilities. */ public void testWeatherTable() { // First insert the location, and then use the locationRowId to insert // the weather. Make sure to cover as many failure cases as you can. // Instead of rewriting all of the code we've already written in testLocationTable // we can move this code to insertLocation and then call insertLocation from both // tests. Why move it? We need the code to return the ID of the inserted location // and our testLocationTable can only return void because it's a test. long locationRowId = insertLocation(); assertFalse("Error: Location Not Inserted Correctly", locationRowId == -1L); // First step: Get reference to writable database WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createWeatherValues TestUtilities function if you wish) ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); // Insert ContentValues into database and get a row ID back long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); assertTrue(weatherRowId != -1); // Query the database and receive a Cursor back Cursor weatherCursor = db.query(WeatherContract.WeatherEntry.TABLE_NAME, null, null, null, null, null, null); // Move the cursor to a valid database row assertTrue("Error: No Records returned from location query", weatherCursor.moveToFirst()); // Validate data in resulting Cursor with the original ContentValues // (you can use the validateCurrentRecord function in TestUtilities to validate the // query if you like) TestUtilities.validateCurrentRecord( "testInsertReadDb weatherEntry failed to validate", weatherCursor, weatherValues); assertFalse( "Error: More thant one record returned from weather query", weatherCursor.moveToNext()); // Finally, close the cursor and database weatherCursor.close(); db.close(); }
private Cursor getWeatherByLocationSettingAndDate( Uri uri, String[] projection, String sortOrder) { String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri); long date = WeatherContract.WeatherEntry.getDateFromUri(uri); return sWeatherByLocationSettingQueryBuilder.query( mOpenHelper.getReadableDatabase(), projection, sLocationSettingAndDaySelection, new String[] {locationSetting, Long.toString(date)}, null, null, sortOrder); }
/* This test uses the database directly to insert and then uses the ContentProvider to read out the data. Uncomment this test to see if the basic weather query functionality given in the ContentProvider is working correctly. */ public void testBasicWeatherQuery() { // insert our test records into the database WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext); // Fantastic. Now that we have a location, add some weather! ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); long weatherRowId = db.insert(WeatherEntry.TABLE_NAME, null, weatherValues); assertTrue("Unable to Insert WeatherEntry into the Database", weatherRowId != -1); db.close(); // Test the basic content provider query Cursor weatherCursor = mContext.getContentResolver().query(WeatherEntry.CONTENT_URI, null, null, null, null); // Make sure we get the correct cursor out of the database TestUtilities.validateCursor("testBasicWeatherQuery", weatherCursor, weatherValues); }
/* Students: This is a helper method for the testWeatherTable quiz. You can move your code from testLocationTable to here so that you can call this code from both testWeatherTable and testLocationTable. */ public long insertLocation() { // First step: Get reference to writable database WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); // Insert ContentValues into database and get a row ID back long locationRowId; locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues); assertTrue(locationRowId != -1); // Query the database and receive a Cursor back Cursor cursor = db.query(WeatherContract.LocationEntry.TABLE_NAME, null, null, null, null, null, null); assertTrue("Error: No Records returned from location query", cursor.moveToFirst()); // Move the cursor to a valid database row TestUtilities.validateCurrentRecord( "Error: Location Query Validation Failed", cursor, testValues); // Validate data in resulting Cursor with the original ContentValues // (you can use the validateCurrentRecord function in TestUtilities to validate the // query if you like) assertFalse("Error: More than one record returned from location query", cursor.moveToNext()); // Finally, close the cursor and database cursor.close(); db.close(); return locationRowId; }
@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 WEATHER: rowsDeleted = db.delete(WeatherContract.WeatherEntry.TABLE_NAME, selection, selectionArgs); break; case LOCATION: rowsDeleted = db.delete(WeatherContract.LocationEntry.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 getWeatherByLocationSetting(Uri uri, String[] projection, String sortOrder) { String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri); long startDate = WeatherContract.WeatherEntry.getStartDateFromUri(uri); String[] selectionArgs; String selection; if (startDate == 0) { selection = sLocationSettingSelection; selectionArgs = new String[] {locationSetting}; } else { selectionArgs = new String[] {locationSetting, Long.toString(startDate)}; selection = sLocationSettingWithStartDateSelection; } return sWeatherByLocationSettingQueryBuilder.query( mOpenHelper.getReadableDatabase(), projection, selection, selectionArgs, 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 WEATHER: normalizeDate(values); rowsUpdated = db.update(WeatherContract.WeatherEntry.TABLE_NAME, values, selection, selectionArgs); break; case LOCATION: rowsUpdated = db.update(WeatherContract.LocationEntry.TABLE_NAME, values, selection, selectionArgs); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (rowsUpdated != 0) { getContext().getContentResolver().notifyChange(uri, null); } return rowsUpdated; }
@Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); }