Skip to content
This repository was archived by the owner on Feb 11, 2022. It is now read-only.

Commit 1f21945

Browse files
authored
Merge pull request #62 from novoda/fix_update_in_transaction
Fix update behaviour in content provider implementation
2 parents fd40798 + 344a624 commit 1f21945

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

core/src/androidTest/java/novoda/lib/sqliteprovider/ContentProviderTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11

22
package novoda.lib.sqliteprovider;
33

4+
import android.content.ContentValues;
45
import android.database.Cursor;
6+
import android.database.sqlite.SQLiteDatabase;
57
import android.net.Uri;
68
import android.test.AndroidTestCase;
79

10+
import java.io.IOException;
11+
12+
import novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper;
13+
814
public class ContentProviderTest extends AndroidTestCase {
915

16+
private static final String TABLE_NAME = "test";
17+
private static final String COLUMN_PRIMARY_KEY = "column_primary_key";
18+
private static final String ANY_COLUMN = "any_column";
19+
1020
public ContentProviderTest() {
1121
super();
1222
}
@@ -18,4 +28,45 @@ public void testMapping() throws Exception {
1828
assertTrue(cursor.getColumnIndexOrThrow("childs__id") > -1);
1929
assertTrue(cursor.getColumnIndexOrThrow("parents_name") > -1);
2030
}
31+
32+
public void test_GivenATableWithData_When_UpdatingNonexistentRow_Then_NumberOfAffectedRowsShouldBeZero() throws Exception {
33+
givenATableWithData();
34+
35+
ContentValues values = new ContentValues(1);
36+
values.put(ANY_COLUMN, 1);
37+
38+
int numRows = new ExtendedSQLiteOpenHelper(getContext())
39+
.getWritableDatabase()
40+
.update(TABLE_NAME, values, COLUMN_PRIMARY_KEY + "=?", new String[]{"2"});
41+
42+
assertEquals(0, numRows);
43+
44+
numRows = getContext().getContentResolver().update(
45+
Uri.parse("content://novoda.lib.sqliteprovider.test/" + TABLE_NAME),
46+
values,
47+
COLUMN_PRIMARY_KEY + "=?",
48+
new String[]{"2"});
49+
50+
assertEquals(0, numRows);
51+
}
52+
53+
private void givenATableWithData() throws IOException {
54+
ExtendedSQLiteOpenHelper helper = new ExtendedSQLiteOpenHelper(getContext());
55+
SQLiteDatabase db = helper.getWritableDatabase();
56+
db.execSQL("CREATE TABLE IF NOT EXISTS `" + TABLE_NAME + "` (" +
57+
"`" + COLUMN_PRIMARY_KEY + "` INTEGER PRIMARY KEY," +
58+
"`" + ANY_COLUMN + "` INTEGER UNIQUE)");
59+
60+
ContentValues values = new ContentValues(2);
61+
values.put(COLUMN_PRIMARY_KEY, 1);
62+
values.put(ANY_COLUMN, 1);
63+
helper.getWritableDatabase().insert(TABLE_NAME, null, values);
64+
}
65+
66+
@Override
67+
protected void tearDown() throws Exception {
68+
ExtendedSQLiteOpenHelper helper = new ExtendedSQLiteOpenHelper(getContext());
69+
SQLiteDatabase db = helper.getWritableDatabase();
70+
db.execSQL("DROP TABLE IF EXISTS `" + TABLE_NAME + "`");
71+
}
2172
}

core/src/androidTest/java/novoda/lib/sqliteprovider/sqlite/InsertHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private void onQueryOf(String tableName, CursorOperations cursorOperations) {
143143
}
144144
}
145145

146-
private static interface CursorOperations {
146+
private interface CursorOperations {
147147
void doOperationsOn(Cursor cursor);
148148
}
149149
}

core/src/main/java/novoda/lib/sqliteprovider/provider/SQLiteContentProviderImpl.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.content.Context;
66
import android.database.Cursor;
77
import android.database.DatabaseUtils;
8-
import android.database.SQLException;
98
import android.database.sqlite.SQLiteDatabase;
109
import android.database.sqlite.SQLiteOpenHelper;
1110
import android.net.Uri;
@@ -96,14 +95,12 @@ private Uri insertSilently(Uri uri, ContentValues values) {
9695
protected int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
9796
ContentValues insertValues = (values != null) ? new ContentValues(values) : new ContentValues();
9897

99-
int rowId = getWritableDatabase().update(UriUtils.getItemDirID(uri), insertValues, selection, selectionArgs);
98+
int rowsAffected = getWritableDatabase().update(UriUtils.getItemDirID(uri), insertValues, selection, selectionArgs);
10099

101-
if (rowId > 0) {
102-
Uri insertUri = ContentUris.withAppendedId(uri, rowId);
103-
notifyUriChange(insertUri);
104-
return rowId;
100+
if (rowsAffected > 0) {
101+
notifyUriChange(uri);
105102
}
106-
throw new SQLException("Failed to update row into " + uri + " because it does not exists.");
103+
return rowsAffected;
107104
}
108105

109106
@Override

0 commit comments

Comments
 (0)