Bug 380505 - Simplify and optimize mDBGetChildren. r=sspitzer.
authormozilla.mano@sent.com
Mon, 14 May 2007 14:42:57 -0700
changeset 1428 90916f8df38c3d404b9874ffbe1c8e88f7e3ff34
parent 1427 3952e2a8864311582cf3fafdce8525dfa9d0c5f0
child 1429 b61578bd5ce38e8b7c2a31fa2a7e3e8ed044e224
push id1
push usersledru@mozilla.com
push dateThu, 04 Dec 2014 17:57:20 +0000
reviewerssspitzer
bugs380505
milestone1.9a5pre
Bug 380505 - Simplify and optimize mDBGetChildren. r=sspitzer.
toolkit/components/places/src/nsNavBookmarks.cpp
toolkit/components/places/tests/bookmarks/test_bookmarks.js
--- a/toolkit/components/places/src/nsNavBookmarks.cpp
+++ b/toolkit/components/places/src/nsNavBookmarks.cpp
@@ -54,17 +54,16 @@ const PRInt32 nsNavBookmarks::kFindBookm
 const PRInt32 nsNavBookmarks::kFindBookmarksIndex_Parent = 3;
 const PRInt32 nsNavBookmarks::kFindBookmarksIndex_Position = 4;
 const PRInt32 nsNavBookmarks::kFindBookmarksIndex_Title = 5;
 
 // These columns sit to the right of the kGetInfoIndex_* columns.
 const PRInt32 nsNavBookmarks::kGetChildrenIndex_Position = 9;
 const PRInt32 nsNavBookmarks::kGetChildrenIndex_Type = 10;
 const PRInt32 nsNavBookmarks::kGetChildrenIndex_ForeignKey = 11;
-const PRInt32 nsNavBookmarks::kGetChildrenIndex_FolderTitle = 12;
 
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_ID = 0;
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_URI = 1;
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_Title = 2;
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_Position = 3;
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_PlaceID = 4;
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_Parent = 5;
 const PRInt32 nsNavBookmarks::kGetItemPropertiesIndex_Type = 6;
@@ -134,66 +133,31 @@ nsNavBookmarks::Init()
       "SELECT a.* "
       "FROM moz_bookmarks a, moz_places h "
       "WHERE h.url = ?1 AND a.fk = h.id and a.type = ?2"),
     getter_AddRefs(mDBFindURIBookmarks));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Construct a result where the first columns exactly match those returned by
   // mDBGetURLPageInfo, and additionally contains columns for position,
-  // item_child, and folder_child from moz_bookmarks.  This selects only
-  // _item_ children which are in moz_places.
+  // item_child, and folder_child from moz_bookmarks.
   // Results are kGetInfoIndex_*
-  nsCAutoString selectItemChildren =
+  nsCAutoString selectChildren(
     NS_LITERAL_CSTRING("SELECT h.id, h.url, a.title, "
       "h.rev_host, h.visit_count, "
       "(SELECT MAX(visit_date) FROM moz_historyvisits WHERE place_id = h.id), "
-      "f.url, null, a.id, a.position, a.type, a.fk, null "
-    "FROM moz_bookmarks a "
-    "JOIN moz_places h ON a.fk = h.id "
-    "LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
-    "WHERE a.parent = ?1 AND a.type = ") +
-      nsPrintfCString("%d", TYPE_BOOKMARK) +
-      NS_LITERAL_CSTRING(" AND a.position >= ?2 AND a.position <= ?3");
-
-  // Construct a result where the first columns are padded out to the width
-  // of mDBGetVisitPageInfo, containing additional columns for position,
-  // item_child, and folder_child and name from
-  // moz_bookmarks.  This selects only _folder_ children.
-  // Results are kGetInfoIndex_* kGetChildrenIndex_*
-  nsCAutoString selectFolderChildren = 
-    NS_LITERAL_CSTRING("SELECT null, null, null, null, null, null, null, "
-      "null, a.id, a.position, a.type, a.fk, a.title "
-    "FROM moz_bookmarks a "
-    "WHERE a.parent = ?1 AND a.type = ") +
-      nsPrintfCString("%d", TYPE_FOLDER) +
-      NS_LITERAL_CSTRING(" AND a.position >= ?2 AND a.position <= ?3");
-
-  // Construct a result where the first columns are padded out to the width
-  // of mDBGetVisitPageInfo, containing additional columns for position,
-  // item_child, and folder_child from moz_bookmarks.  This selects only
-  // _separator_ children which are in moz_bookmarks.  Results are
-  // kGetInfoIndex_* kGetChildrenIndex_*.  item_child and folder_child will
-  // be NULL for separators.
-  nsCAutoString selectSeparatorChildren =
-  NS_LITERAL_CSTRING("SELECT null, null, null, null, null, null, null, "
-      "null, a.id, a.position, a.type, null, null "
-    "FROM moz_bookmarks a "
-    "WHERE a.type = ") +
-      nsPrintfCString("%d", TYPE_SEPARATOR) +
-      NS_LITERAL_CSTRING(" AND a.parent = ?1 AND a.position >= ?2 AND a.position <= ?3");
-
-  NS_NAMED_LITERAL_CSTRING(orderByPosition, " ORDER BY a.position");
+      "f.url, null, a.id, a.position, a.type, a.fk "
+     "FROM moz_bookmarks a "
+     "LEFT JOIN moz_places h ON a.fk = h.id "
+     "LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
+     "WHERE a.parent = ?1 AND a.position >= ?2 AND a.position <= ?3"
+     " ORDER BY a.position"));
 
   // mDBGetChildren: select all children of a given folder, sorted by position
-  rv = dbConn->CreateStatement(selectItemChildren +
-                               NS_LITERAL_CSTRING(" UNION ALL ") +
-                               selectFolderChildren +
-                               NS_LITERAL_CSTRING(" UNION ALL ") +
-                               selectSeparatorChildren + orderByPosition,
+  rv = dbConn->CreateStatement(selectChildren,
                                getter_AddRefs(mDBGetChildren));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // mDBFolderCount: count all of the children of a given folder
   rv = dbConn->CreateStatement(NS_LITERAL_CSTRING("SELECT COUNT(*) FROM moz_bookmarks WHERE parent = ?1"),
                                getter_AddRefs(mDBFolderCount));
   NS_ENSURE_SUCCESS(rv, rv);
 
--- a/toolkit/components/places/tests/bookmarks/test_bookmarks.js
+++ b/toolkit/components/places/tests/bookmarks/test_bookmarks.js
@@ -490,16 +490,46 @@ function run_test() {
   do_check_true(annosvc.itemHasAnnotation(newId3, "test-annotation"));
   bmsvc.removeItem(newId3);
   do_check_false(annosvc.itemHasAnnotation(newId3, "test-annotation"));
 
   // bug 378820
   var uri1 = uri("http://foo.tld/a");
   bmsvc.insertItem(testRoot, uri1, bmsvc.DEFAULT_INDEX);
   histsvc.addVisit(uri1, Date.now(), 0, histsvc.TRANSITION_TYPED, false, 0);
+
+  testSimpleFolderResult();
+}
+
+function testSimpleFolderResult() {
+  var parent = bmsvc.createFolder(root, "test", bmsvc.DEFAULT_INDEX);
+  var sep = bmsvc.insertSeparator(parent, bmsvc.DEFAULT_INDEX);
+  var item = bmsvc.insertItem(parent, uri("about:blank"), bmsvc.DEFAULT_INDEX);
+  bmsvc.setItemTitle(item, "test bookmark");
+  var folder = bmsvc.createFolder(parent, "test folder", bmsvc.DEFAULT_INDEX);
+
+  var options = histsvc.getNewQueryOptions();
+  var query = histsvc.getNewQuery();
+  query.setFolders([parent], 1);
+  var result = histsvc.executeQuery(query, options);
+  var rootNode = result.root;
+  rootNode.containerOpen = true;
+  do_check_eq(rootNode.childCount, 3);
+
+  var node = rootNode.getChild(0);
+  do_check_eq(node.itemId, sep);
+  do_check_eq(node.title, "");
+  node = rootNode.getChild(1);
+  do_check_eq(node.itemId, item);
+  do_check_eq(node.title, "test bookmark");
+  node = rootNode.getChild(2);
+  do_check_eq(node.itemId, folder);
+  do_check_eq(node.title, "test folder");
+
+  rootNode.containerOpen = false;
 }
 
 function getChildCount(aFolderId) {
   var cc = -1;
   try {
     var options = histsvc.getNewQueryOptions();
     var query = histsvc.getNewQuery();
     query.setFolders([aFolderId], 1);