diff options
author | Jeff Hamilton <jham@android.com> | 2010-08-23 10:14:06 -0500 |
---|---|---|
committer | Jeff Hamilton <jham@android.com> | 2010-09-02 10:04:41 -0500 |
commit | 7669ba4b28f0bdf40e18b3be7a0cb242c62daa03 (patch) | |
tree | 071badbfc11a2c26d4970f666fc63efc9fdde542 /src/com/android | |
parent | 3d6df16f1c9a64cf2dc5b41a2078f4c49b0c296a (diff) | |
download | packages_apps_Browser-7669ba4b28f0bdf40e18b3be7a0cb242c62daa03.zip packages_apps_Browser-7669ba4b28f0bdf40e18b3be7a0cb242c62daa03.tar.gz packages_apps_Browser-7669ba4b28f0bdf40e18b3be7a0cb242c62daa03.tar.bz2 |
Change searches to do an upsert on insert().
Change-Id: Ib1d2f3ec678b37620f0907981c3d39cee634d9d7
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/browser/provider/BrowserProvider2.java | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/com/android/browser/provider/BrowserProvider2.java b/src/com/android/browser/provider/BrowserProvider2.java index 5825525..d93a039 100644 --- a/src/com/android/browser/provider/BrowserProvider2.java +++ b/src/com/android/browser/provider/BrowserProvider2.java @@ -31,13 +31,13 @@ import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.provider.BrowserContract; +import android.provider.SyncStateContract; import android.provider.BrowserContract.Bookmarks; import android.provider.BrowserContract.ChromeSyncColumns; import android.provider.BrowserContract.History; import android.provider.BrowserContract.Searches; import android.provider.BrowserContract.SyncState; import android.provider.ContactsContract.RawContacts; -import android.provider.SyncStateContract; import android.text.TextUtils; import java.util.HashMap; @@ -615,12 +615,12 @@ public class BrowserProvider2 extends SQLiteContentProvider { } case SEARCHES: { - id = db.insertOrThrow(TABLE_SEARCHES, Searches.SEARCH, values); + id = insertSearchesInTransaction(db, values); break; } case SYNCSTATE: { - id = mSyncHelper.insert(mDb, values); + id = mSyncHelper.insert(db, values); break; } @@ -636,6 +636,31 @@ public class BrowserProvider2 extends SQLiteContentProvider { } } + /** + * Searches are unique, so perform an UPSERT manually since SQLite doesn't support them. + */ + private long insertSearchesInTransaction(SQLiteDatabase db, ContentValues values) { + String search = values.getAsString(Searches.SEARCH); + if (TextUtils.isEmpty(search)) { + throw new IllegalArgumentException("Must include the SEARCH field"); + } + Cursor cursor = null; + try { + cursor = db.query(TABLE_SEARCHES, new String[] { Searches._ID }, + Searches.SEARCH + "=?", new String[] { search }, null, null, null); + if (cursor.moveToNext()) { + long id = cursor.getLong(0); + db.update(TABLE_SEARCHES, values, Searches._ID + "=?", + new String[] { Long.toString(id) }); + return id; + } else { + return db.insertOrThrow(TABLE_SEARCHES, Searches.SEARCH, values); + } + } finally { + if (cursor != null) cursor.close(); + } + } + @Override public int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs, boolean callerIsSyncAdapter) { @@ -664,16 +689,6 @@ public class BrowserProvider2 extends SQLiteContentProvider { return db.update(TABLE_HISTORY, values, selection, selectionArgs); } - case SEARCHES_ID: { - selection = DatabaseUtils.concatenateWhere(selection, TABLE_SEARCHES + "._id=?"); - selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs, - new String[] { Long.toString(ContentUris.parseId(uri)) }); - // fall through - } - case SEARCHES: { - return db.update(TABLE_SEARCHES, values, selection, selectionArgs); - } - case SYNCSTATE: { return mSyncHelper.update(mDb, values, appendAccountToSelection(uri, selection), selectionArgs); |