Bug 1252610 - Don't insert suggested sites into topsites table unless they actually exist r=rnewman draft
authorAndrzej Hunt <ahunt@mozilla.com>
Tue, 01 Mar 2016 13:54:24 -0800
changeset 335889 8a7786a7f46f72a6491cae1ae006131fc29dfbca
parent 335875 4f18b8f8a55e3d1e561508b98612c25a4c9df7be
child 335890 e9ece842484ea554ef91e3d0f947d5070979e7cb
push id11916
push userahunt@mozilla.com
push dateTue, 01 Mar 2016 21:57:47 +0000
reviewersrnewman
bugs1252610
milestone47.0a1
Bug 1252610 - Don't insert suggested sites into topsites table unless they actually exist r=rnewman MozReview-Commit-ID: 8MPLvxQ0FWu
mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java
--- a/mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/BrowserProvider.java
@@ -758,28 +758,28 @@ public class BrowserProvider extends Sha
         // We could access the underlying data here, however SuggestedSites also performs filtering on the suggested
         // sites list, which means we'd need to process the lists within SuggestedSites in any case. If we're doing
         // that processing, there is little real between us using a MatrixCursor, or a Map (or List) instead of the
         // MatrixCursor.
         final Cursor suggestedSitesCursor = suggestedSites.get(Integer.parseInt(suggestedGridLimit));
 
         String[] suggestedSiteArgs = new String[0];
 
-        boolean firstClause = true;
+        boolean hasProcessedAnySuggestedSites = true;
 
         final int idColumnIndex = suggestedSitesCursor.getColumnIndexOrThrow(Bookmarks._ID);
         final int urlColumnIndex = suggestedSitesCursor.getColumnIndexOrThrow(Bookmarks.URL);
         final int titleColumnIndex = suggestedSitesCursor.getColumnIndexOrThrow(Bookmarks.TITLE);
 
         while (suggestedSitesCursor.moveToNext()) {
             // We'll be using this as a subquery, hence we need to avoid the preceding UNION ALL
-            if (!firstClause) {
+            if (!hasProcessedAnySuggestedSites) {
                 suggestedSitesBuilder.append(" UNION ALL");
             } else {
-                firstClause = false;
+                hasProcessedAnySuggestedSites = false;
             }
             suggestedSitesBuilder.append(" SELECT" +
                                          " ? AS " + Bookmarks._ID + "," +
                                          " ? AS " + Bookmarks.URL + "," +
                                          " ? AS " + Bookmarks.TITLE);
 
             suggestedSiteArgs = DBUtils.appendSelectionArgs(suggestedSiteArgs,
                                                             new String[] {
@@ -813,38 +813,40 @@ public class BrowserProvider extends Sha
                        " FROM " + Combined.VIEW_NAME +
                        " WHERE " + ignoreForTopSitesWhereClause +
                        " ORDER BY " + BrowserContract.getFrecencySortOrder(true, false) +
                        " LIMIT ?",
 
                        DBUtils.appendSelectionArgs(ignoreForTopSitesArgs,
                                                    totalLimitArgs));
 
-            db.execSQL("INSERT INTO " + TABLE_TOPSITES +
-                       // We need to LIMIT _after_ selecting the relevant suggested sites, which requires us to
-                       // use an additional internal subquery, since we cannot LIMIT a subquery that is part of UNION ALL.
-                       // Hence the weird SELECT * FROM (SELECT ...relevant suggested sites... LIMIT ?)
-                       " SELECT * FROM (SELECT " +
-                       Bookmarks._ID + ", " +
-                       Bookmarks._ID + " AS " + Combined.BOOKMARK_ID + ", " +
-                       " -1 AS " + Combined.HISTORY_ID + ", " +
-                       Bookmarks.URL + ", " +
-                       Bookmarks.TITLE + ", " +
-                       "NULL AS " + Combined.HISTORY_ID + ", " +
-                       TopSites.TYPE_SUGGESTED + " as " + TopSites.TYPE +
-                       " FROM ( " + suggestedSitesBuilder.toString() + " )" +
-                       " WHERE " +
-                       Bookmarks.URL + " NOT IN (SELECT url FROM " + TABLE_TOPSITES + ")" +
-                       " AND " +
-                       Bookmarks.URL + " NOT IN (SELECT url " + pinnedSitesFromClause + ")" +
-                       suggestedLimitClause + " )",
+            if (!hasProcessedAnySuggestedSites) {
+                db.execSQL("INSERT INTO " + TABLE_TOPSITES +
+                           // We need to LIMIT _after_ selecting the relevant suggested sites, which requires us to
+                           // use an additional internal subquery, since we cannot LIMIT a subquery that is part of UNION ALL.
+                           // Hence the weird SELECT * FROM (SELECT ...relevant suggested sites... LIMIT ?)
+                           " SELECT * FROM (SELECT " +
+                           Bookmarks._ID + ", " +
+                           Bookmarks._ID + " AS " + Combined.BOOKMARK_ID + ", " +
+                           " -1 AS " + Combined.HISTORY_ID + ", " +
+                           Bookmarks.URL + ", " +
+                           Bookmarks.TITLE + ", " +
+                           "NULL AS " + Combined.HISTORY_ID + ", " +
+                           TopSites.TYPE_SUGGESTED + " as " + TopSites.TYPE +
+                           " FROM ( " + suggestedSitesBuilder.toString() + " )" +
+                           " WHERE " +
+                           Bookmarks.URL + " NOT IN (SELECT url FROM " + TABLE_TOPSITES + ")" +
+                           " AND " +
+                           Bookmarks.URL + " NOT IN (SELECT url " + pinnedSitesFromClause + ")" +
+                           suggestedLimitClause + " )",
 
-                       DBUtils.concatenateSelectionArgs(suggestedSiteArgs,
-                                                        pinnedSitesArgs,
-                                                        suggestedLimitArgs));
+                           DBUtils.concatenateSelectionArgs(suggestedSiteArgs,
+                                                            pinnedSitesArgs,
+                                                            suggestedLimitArgs));
+            }
 
             final String generatePositionFromPositionAndRowid =
                     "COALESCE(" + Bookmarks.POSITION + ", " +
                                   DBUtils.qualifyColumn(TABLE_TOPSITES, "rowid") + " + (SELECT COUNT(*) " + pinnedSitesFromClause + ")" +
                     ")";
 
             final SQLiteCursor c = (SQLiteCursor) db.rawQuery(
                         "SELECT " +