Change searches to do an upsert on insert().

Change-Id: Ib1d2f3ec678b37620f0907981c3d39cee634d9d7
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.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 @@
             }
 
             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 @@
         }
     }
 
+    /**
+     * 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 @@
                 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);