merge backout
authorMarco Bonardo <mbonardo@mozilla.com>
Sat, 16 Jan 2010 06:48:14 -0800
changeset 37258 24813cf3a03a3ea0e7ef98a9935ebc267314985a
parent 37256 c4e11789ff88fe5a3fd940bd1634721b915c8cb3 (current diff)
parent 37257 a337ba00409df59670c2ff15cc447db93e4263c9 (diff)
child 37260 d8007cc7175274c9b99818b4c65817a64614fcec
push idunknown
push userunknown
push dateunknown
milestone1.9.3a1pre
merge backout
toolkit/components/places/src/nsNavBookmarks.cpp
--- a/toolkit/components/places/src/nsNavBookmarks.cpp
+++ b/toolkit/components/places/src/nsNavBookmarks.cpp
@@ -132,47 +132,51 @@ NS_IMPL_ISUPPORTS3(nsNavBookmarks,
 
 
 nsresult
 nsNavBookmarks::Init()
 {
   nsNavHistory* history = nsNavHistory::GetHistoryService();
   NS_ENSURE_TRUE(history, NS_ERROR_OUT_OF_MEMORY);
   mDBConn = history->GetStorageConnection();
-  NS_ENSURE_STATE(mDBConn);
-
-  // TODO: we could consider roots changes as schema changes, and init them
-  // only if the database has been created/updated, history.databaseStatus
-  // can tell us that.
-  nsresult rv = InitRoots();
+  mozStorageTransaction transaction(mDBConn, PR_FALSE);
+
+  nsresult rv = InitStatements();
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = InitRoots();
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = transaction.Commit();
   NS_ENSURE_SUCCESS(rv, rv);
 
   mCanNotify = true;
 
-  // Observe annotations.
+  // Add observers
   nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService();
   NS_ENSURE_TRUE(annosvc, NS_ERROR_OUT_OF_MEMORY);
   annosvc->AddObserver(this);
 
-  // Allows us to notify on title changes. MUST BE LAST so it is impossible
+  // allows us to notify on title changes. MUST BE LAST so it is impossible
   // to fail after this call, or the history service will have a reference to
   // us and we won't go away.
   history->AddObserver(this, PR_FALSE);
 
   // DO NOT PUT STUFF HERE that can fail. See observer comment above.
 
   return NS_OK;
 }
 
 
-/**
- * All commands that initialize the database schema should be here.
- * This is called from history init after database connection has been
- * established.
- */
+// nsNavBookmarks::InitTables
+//
+//    All commands that initialize the schema of the DB go in here. This is
+//    called from history init before the dummy DB connection is started that
+//    will prevent us from modifying the schema.
+
 nsresult // static
 nsNavBookmarks::InitTables(mozIStorageConnection* aDBConn)
 {
   PRBool exists;
   nsresult rv = aDBConn->TableExists(NS_LITERAL_CSTRING("moz_bookmarks"), &exists);
   NS_ENSURE_SUCCESS(rv, rv);
   if (! exists) {
     rv = aDBConn->ExecuteSimpleSQL(CREATE_MOZ_BOOKMARKS);
@@ -214,16 +218,258 @@ nsNavBookmarks::InitTables(mozIStorageCo
     rv = aDBConn->ExecuteSimpleSQL(CREATE_KEYWORD_VALIDITY_TRIGGER);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
 
+// nsNavBookmarks::InitStatements
+//
+//    Create common statements to query the database
+nsresult
+nsNavBookmarks::InitStatements()
+{
+  // mDBFindURIBookmarks
+  // Double ordering covers possible lastModified ties, that could happen when
+  // importing, syncing or due to extensions.
+  nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT b.id "
+      "FROM moz_bookmarks b "
+      "WHERE b.type = ?2 AND b.fk = ( "
+        "SELECT id FROM moz_places_temp "
+        "WHERE url = ?1 "
+        "UNION "
+        "SELECT id FROM moz_places "
+        "WHERE url = ?1 "
+        "LIMIT 1 "
+      ") "
+      "ORDER BY b.lastModified DESC, b.id DESC "),
+    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.
+  // Results are kGetInfoIndex_*
+
+  // mDBGetChildren: select all children of a given folder, sorted by position
+  // This is a LEFT OUTER JOIN with moz_places since folders does not have
+  // a reference into that table.
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT IFNULL(h_t.id, h.id), IFNULL(h_t.url, h.url), "
+             "COALESCE(b.title, h_t.title, h.title), "
+             "IFNULL(h_t.rev_host, h.rev_host), "
+             "IFNULL(h_t.visit_count, h.visit_count), "
+             "IFNULL(h_t.last_visit_date, h.last_visit_date), "
+             "f.url, null, b.id, b.dateAdded, b.lastModified, b.parent, null, "
+             "b.position, b.type, b.fk, b.folder_type "
+      "FROM moz_bookmarks b "
+      "LEFT JOIN moz_places_temp h_t ON b.fk = h_t.id "
+      "LEFT JOIN moz_places h ON b.fk = h.id "
+      "LEFT JOIN moz_favicons f ON h.favicon_id = f.id "
+      "WHERE b.parent = ?1 "
+      "ORDER BY b.position ASC"),
+    getter_AddRefs(mDBGetChildren));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBFolderCount: count all of the children of a given folder and checks
+  // that it exists.
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT COUNT(*), "
+      "(SELECT id FROM moz_bookmarks WHERE id = ?1) "
+      "FROM moz_bookmarks WHERE parent = ?1"),
+    getter_AddRefs(mDBFolderCount));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT position FROM moz_bookmarks WHERE id = ?1"),
+    getter_AddRefs(mDBGetItemIndex));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT id, fk, type FROM moz_bookmarks WHERE parent = ?1 AND position = ?2"),
+    getter_AddRefs(mDBGetChildAt));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // get bookmark/folder/separator properties
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT b.id, "
+             "IFNULL((SELECT url FROM moz_places_temp WHERE id = b.fk), "
+                    "(SELECT url FROM moz_places WHERE id = b.fk)), "
+             "b.title, b.position, b.fk, b.parent, b.type, b.folder_type, "
+             "b.dateAdded, b.lastModified "
+      "FROM moz_bookmarks b "
+      "WHERE b.id = ?1"),
+    getter_AddRefs(mDBGetItemProperties));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT item_id FROM moz_items_annos "
+      "WHERE content = ?1 "
+      "LIMIT 1"),
+    getter_AddRefs(mDBGetItemIdForGUID));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBGetRedirectDestinations
+  // input = page ID, time threshold; output = unique ID input has redirected to
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT DISTINCT dest_v.place_id "
+      "FROM moz_historyvisits_temp source_v "
+      "JOIN moz_historyvisits_temp dest_v ON dest_v.from_visit = source_v.id "
+      "WHERE source_v.place_id = ?1 "
+        "AND source_v.visit_date >= ?2 "
+        "AND dest_v.visit_type IN (") +
+        nsPrintfCString("%d,%d",
+                        nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
+                        nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
+        NS_LITERAL_CSTRING(") "
+      "UNION "
+      "SELECT DISTINCT dest_v.place_id "
+      "FROM moz_historyvisits_temp source_v "
+      "JOIN moz_historyvisits dest_v ON dest_v.from_visit = source_v.id "
+      "WHERE source_v.place_id = ?1 "
+        "AND source_v.visit_date >= ?2 "
+        "AND dest_v.visit_type IN (") +
+        nsPrintfCString("%d,%d",
+                        nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
+                        nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
+        NS_LITERAL_CSTRING(") "
+      "UNION "
+      "SELECT DISTINCT dest_v.place_id "
+      "FROM moz_historyvisits source_v "
+      "JOIN moz_historyvisits_temp dest_v ON dest_v.from_visit = source_v.id "
+      "WHERE source_v.place_id = ?1 "
+        "AND source_v.visit_date >= ?2 "
+        "AND dest_v.visit_type IN (") +
+        nsPrintfCString("%d,%d",
+                        nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
+                        nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
+        NS_LITERAL_CSTRING(") "
+      "UNION "      
+      "SELECT DISTINCT dest_v.place_id "
+      "FROM moz_historyvisits source_v "
+      "JOIN moz_historyvisits dest_v ON dest_v.from_visit = source_v.id "
+      "WHERE source_v.place_id = ?1 "
+        "AND source_v.visit_date >= ?2 "
+        "AND dest_v.visit_type IN (") +
+        nsPrintfCString("%d,%d",
+                        nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
+                        nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
+        NS_LITERAL_CSTRING(") "),
+    getter_AddRefs(mDBGetRedirectDestinations));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBInsertBookmark
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "INSERT INTO moz_bookmarks "
+        "(id, fk, type, parent, position, title, folder_type, dateAdded, lastModified) "
+      "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)"),
+    getter_AddRefs(mDBInsertBookmark));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBIsBookmarkedInDatabase
+  // Just select position since it's just an int32 and may be faster.
+  // We don't actually care about the data, just whether there is any.
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT position FROM moz_bookmarks WHERE fk = ?1 AND type = ?2"),
+    getter_AddRefs(mDBIsBookmarkedInDatabase));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBIsRealBookmark
+  // Checks to make sure a place_id is a bookmark, and isn't a livemark.
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT id "
+      "FROM moz_bookmarks "
+      "WHERE fk = ?1 "
+        "AND type = ?2 "
+        "AND parent NOT IN ("
+          "SELECT a.item_id "
+          "FROM moz_items_annos a "
+          "JOIN moz_anno_attributes n ON a.anno_attribute_id = n.id "
+          "WHERE n.name = ?3"
+        ") "
+      "LIMIT 1"),
+    getter_AddRefs(mDBIsRealBookmark));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBGetLastBookmarkID
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT id "
+      "FROM moz_bookmarks "
+      "ORDER BY ROWID DESC "
+      "LIMIT 1"),
+    getter_AddRefs(mDBGetLastBookmarkID));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBSetItemDateAdded
+  // lastModified is set to the same value as dateAdded.  We do this for
+  // performance reasons, since it will allow us to use an index to sort items
+  // by date.
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "UPDATE moz_bookmarks SET dateAdded = ?1, lastModified = ?1 "
+      "WHERE id = ?2"),
+    getter_AddRefs(mDBSetItemDateAdded));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBSetItemLastModified
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "UPDATE moz_bookmarks SET lastModified = ?1 WHERE id = ?2"),
+    getter_AddRefs(mDBSetItemLastModified));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // mDBSetItemIndex
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "UPDATE moz_bookmarks SET position = ?2 WHERE id = ?1"),
+    getter_AddRefs(mDBSetItemIndex));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // get keyword text for bookmark id
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT k.keyword FROM moz_bookmarks b "
+      "JOIN moz_keywords k ON k.id = b.keyword_id "
+      "WHERE b.id = ?1"),
+    getter_AddRefs(mDBGetKeywordForBookmark));
+  NS_ENSURE_SUCCESS(rv, rv);
+  // get keyword text for URI (must be a bookmarked URI)
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT k.keyword "
+      "FROM ( "
+        "SELECT id FROM moz_places_temp "
+        "WHERE url = ?1 "
+        "UNION ALL "
+        "SELECT id FROM moz_places "
+        "WHERE url = ?1 "
+        "LIMIT 1 "
+      ") AS h "
+      "JOIN moz_bookmarks b ON b.fk = h.id "
+      "JOIN moz_keywords k ON k.id = b.keyword_id"),
+    getter_AddRefs(mDBGetKeywordForURI));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // get URI for keyword
+  rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
+      "SELECT url FROM moz_keywords k "
+      "JOIN moz_bookmarks b ON b.keyword_id = k.id "
+      "JOIN moz_places_temp h ON b.fk = h.id "
+      "WHERE k.keyword = ?1 "
+      "UNION ALL "
+      "SELECT url FROM moz_keywords k "
+      "JOIN moz_bookmarks b ON b.keyword_id = k.id "
+      "JOIN moz_places h ON b.fk = h.id "
+      "WHERE k.keyword = ?1 "
+      "LIMIT 1"),
+    getter_AddRefs(mDBGetURIForKeyword));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  return NS_OK;
+}
+
+
 mozIStorageStatement*
 nsNavBookmarks::GetStatement(const nsCOMPtr<mozIStorageStatement>& aStmt)
 {
 #define RETURN_IF_STMT(_stmt, _sql)                                        \
   PR_BEGIN_MACRO                                                           \
   if (address_of(_stmt) == address_of(aStmt)) {                            \
     if (!_stmt) {                                                          \
       nsresult rv = mDBConn->CreateStatement(_sql, getter_AddRefs(_stmt)); \
@@ -231,203 +477,16 @@ nsNavBookmarks::GetStatement(const nsCOM
     }                                                                      \
     return _stmt.get();                                                    \
   }                                                                        \
   PR_END_MACRO
 
   if (mShuttingDown)
     return nsnull;
 
-  // Double ordering covers possible lastModified ties, that could happen when
-  // importing, syncing or due to extensions.
-  RETURN_IF_STMT(mDBFindURIBookmarks, NS_LITERAL_CSTRING(
-    "SELECT b.id "
-    "FROM moz_bookmarks b "
-    "WHERE b.type = ?2 AND b.fk = ( "
-      "SELECT id FROM moz_places_temp "
-      "WHERE url = ?1 "
-      "UNION "
-      "SELECT id FROM moz_places "
-      "WHERE url = ?1 "
-      "LIMIT 1 "
-    ") "
-    "ORDER BY b.lastModified DESC, b.id DESC "));
-
-  // Select all children of a given folder, sorted by position.
-  // This is a LEFT OUTER JOIN with moz_places since folders does not have
-  // a reference into that table.
-  // We 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.
-  RETURN_IF_STMT(mDBGetChildren, NS_LITERAL_CSTRING(
-    "SELECT IFNULL(h_t.id, h.id), IFNULL(h_t.url, h.url), "
-           "COALESCE(b.title, h_t.title, h.title), "
-           "IFNULL(h_t.rev_host, h.rev_host), "
-           "IFNULL(h_t.visit_count, h.visit_count), "
-           "IFNULL(h_t.last_visit_date, h.last_visit_date), "
-           "f.url, null, b.id, b.dateAdded, b.lastModified, b.parent, null, "
-           "b.position, b.type, b.fk, b.folder_type "
-    "FROM moz_bookmarks b "
-    "LEFT JOIN moz_places_temp h_t ON b.fk = h_t.id "
-    "LEFT JOIN moz_places h ON b.fk = h.id "
-    "LEFT JOIN moz_favicons f ON h.favicon_id = f.id "
-    "WHERE b.parent = ?1 "
-    "ORDER BY b.position ASC"));
-
-  // Count all of the children of a given folder and checks that it exists.
-  RETURN_IF_STMT(mDBFolderCount, NS_LITERAL_CSTRING(
-    "SELECT COUNT(*), "
-    "(SELECT id FROM moz_bookmarks WHERE id = ?1) "
-    "FROM moz_bookmarks WHERE parent = ?1"));
-
-  RETURN_IF_STMT(mDBGetItemIndex, NS_LITERAL_CSTRING(
-    "SELECT position FROM moz_bookmarks WHERE id = ?1"));
-
-  RETURN_IF_STMT(mDBGetChildAt, NS_LITERAL_CSTRING(
-    "SELECT id, fk, type FROM moz_bookmarks "
-    "WHERE parent = ?1 AND position = ?2"));
-
-  // Get bookmark/folder/separator properties.
-  RETURN_IF_STMT(mDBGetItemProperties, NS_LITERAL_CSTRING(
-    "SELECT b.id, "
-           "IFNULL((SELECT url FROM moz_places_temp WHERE id = b.fk), "
-                  "(SELECT url FROM moz_places WHERE id = b.fk)), "
-           "b.title, b.position, b.fk, b.parent, b.type, b.folder_type, "
-           "b.dateAdded, b.lastModified "
-    "FROM moz_bookmarks b "
-    "WHERE b.id = ?1"));
-
-  RETURN_IF_STMT(mDBGetItemIdForGUID, NS_LITERAL_CSTRING(
-    "SELECT item_id FROM moz_items_annos "
-    "WHERE content = ?1 "
-    "LIMIT 1"));
-
-  // input = page ID, time threshold; output = unique ID input has redirected to
-  RETURN_IF_STMT(mDBGetRedirectDestinations, NS_LITERAL_CSTRING(
-    "SELECT DISTINCT dest_v.place_id "
-    "FROM moz_historyvisits_temp source_v "
-    "JOIN moz_historyvisits_temp dest_v ON dest_v.from_visit = source_v.id "
-    "WHERE source_v.place_id = ?1 "
-      "AND source_v.visit_date >= ?2 "
-      "AND dest_v.visit_type IN (") +
-      nsPrintfCString("%d,%d",
-                      nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
-                      nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
-      NS_LITERAL_CSTRING(") "
-    "UNION "
-    "SELECT DISTINCT dest_v.place_id "
-    "FROM moz_historyvisits_temp source_v "
-    "JOIN moz_historyvisits dest_v ON dest_v.from_visit = source_v.id "
-    "WHERE source_v.place_id = ?1 "
-      "AND source_v.visit_date >= ?2 "
-      "AND dest_v.visit_type IN (") +
-      nsPrintfCString("%d,%d",
-                      nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
-                      nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
-      NS_LITERAL_CSTRING(") "
-    "UNION "
-    "SELECT DISTINCT dest_v.place_id "
-    "FROM moz_historyvisits source_v "
-    "JOIN moz_historyvisits_temp dest_v ON dest_v.from_visit = source_v.id "
-    "WHERE source_v.place_id = ?1 "
-      "AND source_v.visit_date >= ?2 "
-      "AND dest_v.visit_type IN (") +
-      nsPrintfCString("%d,%d",
-                      nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
-                      nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
-      NS_LITERAL_CSTRING(") "
-    "UNION "
-    "SELECT DISTINCT dest_v.place_id "
-    "FROM moz_historyvisits source_v "
-    "JOIN moz_historyvisits dest_v ON dest_v.from_visit = source_v.id "
-    "WHERE source_v.place_id = ?1 "
-      "AND source_v.visit_date >= ?2 "
-      "AND dest_v.visit_type IN (") +
-      nsPrintfCString("%d,%d",
-                      nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
-                      nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY) +
-      NS_LITERAL_CSTRING(") "));
-
-  RETURN_IF_STMT(mDBInsertBookmark, NS_LITERAL_CSTRING(
-    "INSERT INTO moz_bookmarks "
-      "(id, fk, type, parent, position, title, folder_type, "
-       "dateAdded, lastModified) "
-    "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)"));
-
-  // Just select position since it's just an int32 and may be faster.
-  // We don't actually care about the data, just whether there is any.
-  RETURN_IF_STMT(mDBIsBookmarkedInDatabase, NS_LITERAL_CSTRING(
-    "SELECT position FROM moz_bookmarks WHERE fk = ?1 AND type = ?2"));
-
-  // Checks to make sure a place_id is a bookmark, and isn't a livemark.
-  RETURN_IF_STMT(mDBIsRealBookmark, NS_LITERAL_CSTRING(
-    "SELECT id "
-    "FROM moz_bookmarks "
-    "WHERE fk = ?1 "
-      "AND type = ?2 "
-      "AND parent NOT IN ("
-        "SELECT a.item_id "
-        "FROM moz_items_annos a "
-        "JOIN moz_anno_attributes n ON a.anno_attribute_id = n.id "
-        "WHERE n.name = ?3"
-      ") "
-    "LIMIT 1"));
-
-  RETURN_IF_STMT(mDBGetLastBookmarkID, NS_LITERAL_CSTRING(
-    "SELECT id "
-    "FROM moz_bookmarks "
-    "ORDER BY ROWID DESC "
-    "LIMIT 1"));
-
-  // lastModified is set to the same value as dateAdded.  We do this for
-  // performance reasons, since it will allow us to use an index to sort items
-  // by date.
-  RETURN_IF_STMT(mDBSetItemDateAdded, NS_LITERAL_CSTRING(
-    "UPDATE moz_bookmarks SET dateAdded = ?1, lastModified = ?1 "
-    "WHERE id = ?2"));
-
-  RETURN_IF_STMT(mDBSetItemLastModified, NS_LITERAL_CSTRING(
-    "UPDATE moz_bookmarks SET lastModified = ?1 WHERE id = ?2"));
-
-  RETURN_IF_STMT(mDBSetItemIndex, NS_LITERAL_CSTRING(
-    "UPDATE moz_bookmarks SET position = ?2 WHERE id = ?1"));
-
-  // Get keyword text for bookmark id.
-  RETURN_IF_STMT(mDBGetKeywordForBookmark, NS_LITERAL_CSTRING(
-    "SELECT k.keyword FROM moz_bookmarks b "
-    "JOIN moz_keywords k ON k.id = b.keyword_id "
-    "WHERE b.id = ?1"));
-
-  // Get keyword text for bookmarked URI.
-  RETURN_IF_STMT(mDBGetKeywordForURI, NS_LITERAL_CSTRING(
-    "SELECT k.keyword "
-    "FROM ( "
-      "SELECT id FROM moz_places_temp "
-      "WHERE url = ?1 "
-      "UNION ALL "
-      "SELECT id FROM moz_places "
-      "WHERE url = ?1 "
-      "LIMIT 1 "
-    ") AS h "
-    "JOIN moz_bookmarks b ON b.fk = h.id "
-    "JOIN moz_keywords k ON k.id = b.keyword_id"));
-
-  // Get URI for keyword.
-  RETURN_IF_STMT(mDBGetURIForKeyword, NS_LITERAL_CSTRING(
-    "SELECT url FROM moz_keywords k "
-    "JOIN moz_bookmarks b ON b.keyword_id = k.id "
-    "JOIN moz_places_temp h ON b.fk = h.id "
-    "WHERE k.keyword = ?1 "
-    "UNION ALL "
-    "SELECT url FROM moz_keywords k "
-    "JOIN moz_bookmarks b ON b.keyword_id = k.id "
-    "JOIN moz_places h ON b.fk = h.id "
-    "WHERE k.keyword = ?1 "
-    "LIMIT 1"));
-
   RETURN_IF_STMT(mDBAdjustPosition, NS_LITERAL_CSTRING(
     "UPDATE moz_bookmarks SET position = position + ?1 "
     "WHERE parent = ?2 AND position >= ?3 AND position <= ?4"));
 
   RETURN_IF_STMT(mDBRemoveItem, NS_LITERAL_CSTRING(
     "DELETE FROM moz_bookmarks WHERE id = ?1"));
 
   RETURN_IF_STMT(mDBGetLastChildId, NS_LITERAL_CSTRING(
@@ -507,18 +566,16 @@ nsNavBookmarks::FinalizeStatements() {
 //    defined point in the hierarchy and set its attributes from this.
 //
 //    This should be called as the last part of the init process so that
 //    all of the statements are set up and the service is ready to use.
 
 nsresult
 nsNavBookmarks::InitRoots()
 {
-  mozStorageTransaction transaction(mDBConn, PR_FALSE);
-
   nsCOMPtr<mozIStorageStatement> getRootStatement;
   nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
       "SELECT folder_id FROM moz_bookmarks_roots WHERE root_name = ?1"),
     getter_AddRefs(getRootStatement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool createdPlacesRoot = PR_FALSE;
   rv = CreateRoot(getRootStatement, NS_LITERAL_CSTRING("places"),
@@ -576,19 +633,16 @@ nsNavBookmarks::InitRoots()
   NS_ENSURE_TRUE(history, NS_ERROR_OUT_OF_MEMORY);
   rv = history->GetDatabaseStatus(&databaseStatus);
   if (NS_FAILED(rv) ||
       databaseStatus != nsINavHistoryService::DATABASE_STATUS_OK) {
     rv = InitDefaults();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  rv = transaction.Commit();
-  NS_ENSURE_SUCCESS(rv, rv);
-
   return NS_OK;
 }
 
 
 // nsNavBookmarks::InitDefaults
 //
 // Initializes default bookmarks and containers.
 // Pulls from places.propertes for l10n.
@@ -650,17 +704,16 @@ nsNavBookmarks::InitDefaults()
 //    have to have a global for this. Creation is less optimized because it
 //    happens rarely.
 
 nsresult
 nsNavBookmarks::CreateRoot(mozIStorageStatement* aGetRootStatement,
                            const nsCString& name, PRInt64* aID,
                            PRInt64 aParentID, PRBool* aWasCreated)
 {
-  NS_ENSURE_STATE(aGetRootStatement);
   mozStorageStatementScoper scoper(aGetRootStatement);
   PRBool hasResult = PR_FALSE;
   nsresult rv = aGetRootStatement->BindUTF8StringParameter(0, name);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = aGetRootStatement->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, rv);
   if (hasResult) {
     if (aWasCreated)
@@ -856,29 +909,29 @@ nsNavBookmarks::RecursiveAddBookmarkHash
 {
   nsresult rv;
   nsTArray<PRInt64> found;
 
   // scope for the DB statement. The statement must be reset by the time we
   // recursively call ourselves again, because our recursive call will use the
   // same statement.
   {
-    mozIStorageStatement* stmt = GetStatement(mDBGetRedirectDestinations);
-    NS_ENSURE_STATE(stmt);
-    mozStorageStatementScoper scoper(stmt);
-    rv = stmt->BindInt64Parameter(0, aCurrentSource);
+    mozStorageStatementScoper scoper(mDBGetRedirectDestinations);
+    rv = mDBGetRedirectDestinations->BindInt64Parameter(0, aCurrentSource);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = stmt->BindInt64Parameter(1, aMinTime);
+    rv = mDBGetRedirectDestinations->BindInt64Parameter(1, aMinTime);
     NS_ENSURE_SUCCESS(rv, rv);
 
     PRBool hasMore;
-    while (NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)) && hasMore) {
+    while (NS_SUCCEEDED(mDBGetRedirectDestinations->ExecuteStep(&hasMore)) &&
+           hasMore) {
+
       // add this newly found redirect destination to the hashtable
       PRInt64 curID;
-      rv = stmt->GetInt64(0, &curID);
+      rv = mDBGetRedirectDestinations->GetInt64(0, &curID);
       NS_ENSURE_SUCCESS(rv, rv);
 
       // It is very important we ignore anything already in our hashtable. It
       // is actually pretty common to get loops of redirects. For example,
       // a restricted page will redirect you to a login page, which will
       // redirect you to the restricted page again with the proper cookie.
       PRInt64 alreadyExistingOne;
       if (GetBookmarksHash()->Get(curID, &alreadyExistingOne))
@@ -941,30 +994,29 @@ nsNavBookmarks::UpdateBookmarkHashOnRemo
 PRBool
 nsNavBookmarks::IsRealBookmark(PRInt64 aPlaceId)
 {
   // Fast path is to check the hash table first.  If it is in the hash table,
   // then verify that it is a real bookmark.
   PRInt64 bookmarkId;
   PRBool isBookmark = GetBookmarksHash()->Get(aPlaceId, &bookmarkId);
   if (isBookmark) {
-    mozIStorageStatement* stmt = GetStatement(mDBIsRealBookmark);
-    NS_ENSURE_STATE(stmt);
-    mozStorageStatementScoper scoper(stmt);
-
-    nsresult rv = stmt->BindInt64Parameter(0, aPlaceId);
+    mozStorageStatementScoper scope(mDBIsRealBookmark);
+
+    nsresult rv = mDBIsRealBookmark->BindInt64Parameter(0, aPlaceId);
     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Binding failed");
-    rv = stmt->BindInt32Parameter(1, TYPE_BOOKMARK);
+    rv = mDBIsRealBookmark->BindInt32Parameter(1, TYPE_BOOKMARK);
     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Binding failed");
-    rv = stmt->BindUTF8StringParameter(2, NS_LITERAL_CSTRING(LMANNO_FEEDURI));
+    rv = mDBIsRealBookmark->BindUTF8StringParameter(
+      2, NS_LITERAL_CSTRING(LMANNO_FEEDURI));
     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Binding failed");
 
     // If we get any rows, then there exists at least one bookmark corresponding
     // to aPlaceId that is not a livemark item.
-    rv = stmt->ExecuteStep(&isBookmark);
+    rv = mDBIsRealBookmark->ExecuteStep(&isBookmark);
     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "ExecuteStep failed");
     if (NS_SUCCEEDED(rv))
       return isBookmark;
   }
 
   return PR_FALSE;
 }
 
@@ -973,24 +1025,22 @@ nsNavBookmarks::IsRealBookmark(PRInt64 a
 //
 //    This checks to see if the specified place_id is actually bookmarked.
 //    This does not check for redirects in the hashtable.
 
 nsresult
 nsNavBookmarks::IsBookmarkedInDatabase(PRInt64 aPlaceId,
                                        PRBool* aIsBookmarked)
 {
-  mozIStorageStatement* stmt = GetStatement(mDBIsBookmarkedInDatabase);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aPlaceId);
+  mozStorageStatementScoper scope(mDBIsBookmarkedInDatabase);
+  nsresult rv = mDBIsBookmarkedInDatabase->BindInt64Parameter(0, aPlaceId);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->BindInt32Parameter(1, TYPE_BOOKMARK);
+  rv = mDBIsBookmarkedInDatabase->BindInt32Parameter(1, TYPE_BOOKMARK);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->ExecuteStep(aIsBookmarked);
+  rv = mDBIsBookmarkedInDatabase->ExecuteStep(aIsBookmarked);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 
 nsresult
 nsNavBookmarks::AdjustIndices(PRInt64 aFolderId,
                               PRInt32 aStartIndex,
@@ -1069,86 +1119,81 @@ nsNavBookmarks::InsertBookmarkInDB(PRInt
                                    const nsACString& aTitle,
                                    PRTime aDateAdded,
                                    PRTime aLastModified,
                                    const nsAString& aServiceContractId,
                                    PRInt64* _newItemId)
 {
   NS_ASSERTION(_newItemId, "Null pointer passed to InsertBookmarkInDB!");
 
-  mozIStorageStatement* stmt = GetStatement(mDBInsertBookmark);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
+  mozStorageStatementScoper scope(mDBInsertBookmark);
 
   nsresult rv;
   if (aItemId && aItemId != -1)
-    rv = stmt->BindInt64Parameter(kInsertBookmarkIndex_Id, aItemId);
+    rv = mDBInsertBookmark->BindInt64Parameter(kInsertBookmarkIndex_Id, aItemId);
   else
-    rv = stmt->BindNullParameter(kInsertBookmarkIndex_Id);
+    rv = mDBInsertBookmark->BindNullParameter(kInsertBookmarkIndex_Id);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (aPlaceId && aPlaceId != -1)
-    rv = stmt->BindInt64Parameter(kInsertBookmarkIndex_PlaceId, aPlaceId);
+    rv = mDBInsertBookmark->BindInt64Parameter(kInsertBookmarkIndex_PlaceId, aPlaceId);
   else
-    rv = stmt->BindNullParameter(kInsertBookmarkIndex_PlaceId);
+    rv = mDBInsertBookmark->BindNullParameter(kInsertBookmarkIndex_PlaceId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = stmt->BindInt32Parameter(kInsertBookmarkIndex_Type, aItemType);
+  rv = mDBInsertBookmark->BindInt32Parameter(kInsertBookmarkIndex_Type, aItemType);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->BindInt64Parameter(kInsertBookmarkIndex_Parent, aParentId);
+  rv = mDBInsertBookmark->BindInt64Parameter(kInsertBookmarkIndex_Parent, aParentId);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->BindInt32Parameter(kInsertBookmarkIndex_Position, aIndex);
+  rv = mDBInsertBookmark->BindInt32Parameter(kInsertBookmarkIndex_Position, aIndex);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Support NULL titles.
   if (aTitle.IsVoid())
-    rv = stmt->BindNullParameter(kInsertBookmarkIndex_Title);
+    rv = mDBInsertBookmark->BindNullParameter(kInsertBookmarkIndex_Title);
   else
-    rv = stmt->BindUTF8StringParameter(kInsertBookmarkIndex_Title, aTitle);
+    rv = mDBInsertBookmark->BindUTF8StringParameter(kInsertBookmarkIndex_Title, aTitle);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (aServiceContractId.IsEmpty())
-    rv = stmt->BindNullParameter(kInsertBookmarkIndex_ServiceContractId);
+    rv = mDBInsertBookmark->BindNullParameter(kInsertBookmarkIndex_ServiceContractId);
   else
-    rv = stmt->BindStringParameter(kInsertBookmarkIndex_ServiceContractId, aServiceContractId);
+    rv = mDBInsertBookmark->BindStringParameter(kInsertBookmarkIndex_ServiceContractId, aServiceContractId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = stmt->BindInt64Parameter(kInsertBookmarkIndex_DateAdded, aDateAdded);
+  rv = mDBInsertBookmark->BindInt64Parameter(kInsertBookmarkIndex_DateAdded, aDateAdded);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (aLastModified)
-    rv = stmt->BindInt64Parameter(kInsertBookmarkIndex_LastModified, aLastModified);
+    rv = mDBInsertBookmark->BindInt64Parameter(kInsertBookmarkIndex_LastModified, aLastModified);
   else
-    rv = stmt->BindInt64Parameter(kInsertBookmarkIndex_LastModified, aDateAdded);
+    rv = mDBInsertBookmark->BindInt64Parameter(kInsertBookmarkIndex_LastModified, aDateAdded);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = stmt->Execute();
+  rv = mDBInsertBookmark->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (!aItemId || aItemId == -1) {
     // Get the new inserted item id.
-    mozIStorageStatement* lastInsertIdStmt = GetStatement(mDBGetLastBookmarkID);
-    NS_ENSURE_STATE(lastInsertIdStmt);
-    mozStorageStatementScoper scoper(lastInsertIdStmt);
+    mozStorageStatementScoper scoper(mDBGetLastBookmarkID);
     PRBool hasResult;
-    rv = lastInsertIdStmt->ExecuteStep(&hasResult);
+    rv = mDBGetLastBookmarkID->ExecuteStep(&hasResult);
     NS_ENSURE_SUCCESS(rv, rv);
     NS_ENSURE_TRUE(hasResult, NS_ERROR_UNEXPECTED);
-    rv = lastInsertIdStmt->GetInt64(0, _newItemId);
+    rv = mDBGetLastBookmarkID->GetInt64(0, _newItemId);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   else {
     *_newItemId = aItemId;
   }
 
   // Update last modified date of the parent folder.
   // XXX TODO: This should be done recursively for all ancestors, that would
   //           be slow without a nested tree though.  See bug 408991.
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           aParentId, aDateAdded);
+  rv = SetItemDateInternal(mDBSetItemLastModified, aParentId, aDateAdded);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::InsertBookmark(PRInt64 aFolder,
@@ -1274,40 +1319,41 @@ nsNavBookmarks::RemoveItem(PRInt64 aItem
 
   nsresult rv;
   PRInt32 childIndex;
   PRInt64 placeId, folderId;
   PRUint16 itemType;
   nsCAutoString buffer;
   nsCAutoString spec;
 
-  {
-    mozIStorageStatement* getInfoStmt = GetStatement(mDBGetItemProperties);
-    NS_ENSURE_STATE(getInfoStmt);
-    mozStorageStatementScoper scoper(getInfoStmt);
-
-    rv = getInfoStmt->BindInt64Parameter(0, aItemId);
-    NS_ENSURE_SUCCESS(rv, rv);
-    PRBool hasResult;
-    rv = getInfoStmt->ExecuteStep(&hasResult);
+  { // scoping to ensure the statement gets reset
+    mozStorageStatementScoper scope(mDBGetItemProperties);
+    mDBGetItemProperties->BindInt64Parameter(0, aItemId);
+
+    PRBool results;
+    rv = mDBGetItemProperties->ExecuteStep(&results);
     NS_ENSURE_SUCCESS(rv, rv);
-    if (!hasResult)
+
+    if (!results)
       return NS_ERROR_INVALID_ARG; // invalid bookmark id
 
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Position, &childIndex);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Position,
+                                        &childIndex);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_PlaceID, &placeId);
+    rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_PlaceID,
+                                        &placeId);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_Parent, &folderId);
+    rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent,
+                                        &folderId);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Type,
-                               (PRInt32*)&itemType);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                        (PRInt32*)&itemType);
     NS_ENSURE_SUCCESS(rv, rv);
     if (itemType == TYPE_BOOKMARK) {
-      rv = getInfoStmt->GetUTF8String(kGetItemPropertiesIndex_URI, spec);
+      rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_URI, spec);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
 
   if (itemType == TYPE_FOLDER) {
     rv = RemoveFolder(aItemId);
     NS_ENSURE_SUCCESS(rv, rv);
     return NS_OK;
@@ -1334,18 +1380,17 @@ nsNavBookmarks::RemoveItem(PRInt64 aItem
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   if (childIndex != -1) {
     rv = AdjustIndices(folderId, childIndex + 1, PR_INT32_MAX, -1);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           folderId, PR_Now());
+  rv = SetItemDateInternal(mDBSetItemLastModified, folderId, PR_Now());
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = transaction.Commit();
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = UpdateBookmarkHashOnRemove(placeId);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1595,56 +1640,52 @@ nsNavBookmarks::GetIdForItemAt(PRInt64 a
   nsresult rv;
   if (aIndex == nsINavBookmarksService::DEFAULT_INDEX) {
     // Get last item within aFolder.
     rv = GetLastChildId(aFolder, aItemId);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   else {
     // Get the item in aFolder with position aIndex.
-    mozIStorageStatement* stmt = GetStatement(mDBGetChildAt);
-    NS_ENSURE_STATE(stmt);
-    mozStorageStatementScoper scoper(stmt);
-
-    rv = stmt->BindInt64Parameter(0, aFolder);
+    mozStorageStatementScoper scope(mDBGetChildAt);
+
+    rv = mDBGetChildAt->BindInt64Parameter(0, aFolder);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = stmt->BindInt32Parameter(1, aIndex);
+    rv = mDBGetChildAt->BindInt32Parameter(1, aIndex);
     NS_ENSURE_SUCCESS(rv, rv);
 
     PRBool found;
-    rv = stmt->ExecuteStep(&found);
+    rv = mDBGetChildAt->ExecuteStep(&found);
     NS_ENSURE_SUCCESS(rv, rv);
     if (found) {
-      rv = stmt->GetInt64(0, aItemId);
+      rv = mDBGetChildAt->GetInt64(0, aItemId);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
   return NS_OK;
 }
 
 
 nsresult 
 nsNavBookmarks::GetParentAndIndexOfFolder(PRInt64 aFolderId,
                                           PRInt64* _aParent,
                                           PRInt32* _aIndex)
 {
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aFolderId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aFolderId);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  rv = mDBGetItemProperties->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(hasResult, NS_ERROR_INVALID_ARG);
 
-  rv = stmt->GetInt64(kGetItemPropertiesIndex_Parent, _aParent);
+  rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent, _aParent);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->GetInt32(kGetItemPropertiesIndex_Position, _aIndex);
+  rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Position, _aIndex);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 nsresult
 nsNavBookmarks::RemoveFolder(PRInt64 aFolderId)
@@ -1656,37 +1697,38 @@ nsNavBookmarks::RemoveFolder(PRInt64 aFo
 
   mozStorageTransaction transaction(mDBConn, PR_FALSE);
 
   nsresult rv;
   PRInt64 parent;
   PRInt32 index, type;
   nsCAutoString folderType;
   {
-    mozIStorageStatement* getInfoStmt = GetStatement(mDBGetItemProperties);
-    NS_ENSURE_STATE(getInfoStmt);
-    mozStorageStatementScoper scoper(getInfoStmt);
-    rv = getInfoStmt->BindInt64Parameter(0, aFolderId);
+    mozStorageStatementScoper scope(mDBGetItemProperties);
+    rv = mDBGetItemProperties->BindInt64Parameter(0, aFolderId);
     NS_ENSURE_SUCCESS(rv, rv);
 
     PRBool hasResult;
-    rv = getInfoStmt->ExecuteStep(&hasResult);
+    rv = mDBGetItemProperties->ExecuteStep(&hasResult);
     NS_ENSURE_SUCCESS(rv, rv);
     if (!hasResult) {
       return NS_ERROR_INVALID_ARG; // folder is not in the hierarchy
     }
 
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Type, (PRInt32*)&type);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                        (PRInt32*)&type);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_Parent, &parent);
+    rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent,
+                                        &parent);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Position, &index);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Position,
+                                        &index);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId,
-                                    folderType);
+    rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId,
+                                             folderType);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // Ensure this is really a folder.
   NS_ENSURE_TRUE(type == TYPE_FOLDER, NS_ERROR_INVALID_ARG);
 
   // First, remove item annotations
   nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService();
@@ -1718,18 +1760,17 @@ nsNavBookmarks::RemoveFolder(PRInt64 aFo
     NS_ENSURE_SUCCESS(rv, rv);
     rv = stmt->Execute();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   rv = AdjustIndices(parent, index + 1, PR_INT32_MAX, -1);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           parent, PR_Now());
+  rv = SetItemDateInternal(mDBSetItemLastModified, parent, PR_Now());
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = transaction.Commit();
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (aFolderId == mToolbarFolder) {
     mToolbarFolder = 0;
   }
@@ -1766,46 +1807,47 @@ nsresult
 nsNavBookmarks::GetDescendantChildren(PRInt64 aFolderId,
                                       PRInt64 aGrandParentId,
                                       nsTArray<folderChildrenInfo>& aFolderChildrenArray) {
   // New children will be added from this index on.
   PRUint32 startIndex = aFolderChildrenArray.Length();
   nsresult rv;
   {
     // Collect children informations.
-    mozIStorageStatement* stmt = GetStatement(mDBGetChildren);
-    NS_ENSURE_STATE(stmt);
-    mozStorageStatementScoper scoper(stmt);
-    rv = stmt->BindInt64Parameter(0, aFolderId);
+    mozStorageStatementScoper scope(mDBGetChildren);
+    rv = mDBGetChildren->BindInt64Parameter(0, aFolderId);
     NS_ENSURE_SUCCESS(rv, rv);
 
     PRBool hasMore;
-    while (NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)) && hasMore) {
+    while (NS_SUCCEEDED(mDBGetChildren->ExecuteStep(&hasMore)) && hasMore) {
       folderChildrenInfo child;
-      rv = stmt->GetInt64(nsNavHistory::kGetInfoIndex_ItemId, &child.itemId);
+      rv = mDBGetChildren->GetInt64(nsNavHistory::kGetInfoIndex_ItemId,
+                                    &child.itemId);
       NS_ENSURE_SUCCESS(rv, rv);
       child.parentId = aFolderId;
       child.grandParentId = aGrandParentId;
-      rv = stmt->GetInt32(kGetChildrenIndex_Type, (PRInt32*)&child.itemType);
+      rv = mDBGetChildren->GetInt32(kGetChildrenIndex_Type,
+                                    (PRInt32*)&child.itemType);
       NS_ENSURE_SUCCESS(rv, rv);
-      rv = stmt->GetInt64(kGetChildrenIndex_PlaceID, &child.placeId);
+      rv = mDBGetChildren->GetInt64(kGetChildrenIndex_PlaceID, &child.placeId);
       NS_ENSURE_SUCCESS(rv, rv);
-      rv = stmt->GetInt32(kGetChildrenIndex_Position, &child.index);
+      rv = mDBGetChildren->GetInt32(kGetChildrenIndex_Position, &child.index);
       NS_ENSURE_SUCCESS(rv, rv);
 
       if (child.itemType == TYPE_BOOKMARK) {
         nsCAutoString URIString;
-        rv = stmt->GetUTF8String(nsNavHistory::kGetInfoIndex_URL, URIString);
+        rv = mDBGetChildren->GetUTF8String(nsNavHistory::kGetInfoIndex_URL,
+                                           URIString);
         NS_ENSURE_SUCCESS(rv, rv);
         child.url = URIString;
       }
       else if (child.itemType == TYPE_FOLDER) {
         nsCAutoString folderType;
-        rv = stmt->GetUTF8String(kGetChildrenIndex_ServiceContractId,
-                                 folderType);
+        rv = mDBGetChildren->GetUTF8String(kGetChildrenIndex_ServiceContractId,
+                                           folderType);
         NS_ENSURE_SUCCESS(rv, rv);
         child.folderType = folderType;
       }
       // Append item to children's array.
       aFolderChildrenArray.AppendElement(child);
     }
   }
 
@@ -1829,38 +1871,37 @@ NS_IMETHODIMP
 nsNavBookmarks::RemoveFolderChildren(PRInt64 aFolderId)
 {
   NS_ENSURE_ARG_MIN(aFolderId, 1);
 
   nsresult rv;
   PRUint16 itemType;
   PRInt64 grandParentId;
   {
-    mozIStorageStatement* getInfoStmt = GetStatement(mDBGetItemProperties);
-    NS_ENSURE_STATE(getInfoStmt);
-    mozStorageStatementScoper scoper(getInfoStmt);
-    rv = getInfoStmt->BindInt64Parameter(0, aFolderId);
+    mozStorageStatementScoper scope(mDBGetItemProperties);
+    rv = mDBGetItemProperties->BindInt64Parameter(0, aFolderId);
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Sanity check: ensure that item exists.
     PRBool folderExists;
-    if (NS_FAILED(getInfoStmt->ExecuteStep(&folderExists)) || !folderExists)
+    if (NS_FAILED(mDBGetItemProperties->ExecuteStep(&folderExists)) || !folderExists)
       return NS_ERROR_INVALID_ARG;
 
     // Sanity check: ensure that this is a folder.
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Type,
-                               (PRInt32*)&itemType);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                        (PRInt32*)&itemType);
     NS_ENSURE_SUCCESS(rv, rv);
     if (itemType != TYPE_FOLDER)
       return NS_ERROR_INVALID_ARG;
 
     // Get the grandParent.
     // We have to do this only once since recursion will give us other
     // grandParents without the need of additional queries.
-    rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_Parent, &grandParentId);
+    rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent,
+                                        &grandParentId);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // Fill folder children array recursively.
   nsTArray<folderChildrenInfo> folderChildrenArray;
   rv = GetDescendantChildren(aFolderId, grandParentId, folderChildrenArray);
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1914,18 +1955,17 @@ nsNavBookmarks::RemoveFolderChildren(PRI
       "DELETE FROM moz_items_annos "
       "WHERE id IN ("
         "SELECT a.id from moz_items_annos a "
         "LEFT JOIN moz_bookmarks b ON a.item_id = b.id "
         "WHERE b.id ISNULL)"));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Set the lastModified date.
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           aFolderId, PR_Now());
+  rv = SetItemDateInternal(mDBSetItemLastModified, aFolderId, PR_Now());
   NS_ENSURE_SUCCESS(rv, rv);
 
   for (PRUint32 i = 0; i < folderChildrenArray.Length(); i++) {
     folderChildrenInfo child = folderChildrenArray[i];
     if (child.itemType == TYPE_BOOKMARK) {
       PRInt64 placeId = child.placeId;
       UpdateBookmarkHashOnRemove(placeId);
 
@@ -2000,67 +2040,66 @@ nsNavBookmarks::MoveItem(PRInt64 aItemId
 
   // get item properties
   nsresult rv;
   PRInt64 oldParent;
   PRInt32 oldIndex;
   PRUint16 itemType;
   nsCAutoString folderType;
   {
-    mozIStorageStatement* getInfoStmt = GetStatement(mDBGetItemProperties);
-    NS_ENSURE_STATE(getInfoStmt);
-    mozStorageStatementScoper scoper(getInfoStmt);
-    rv = getInfoStmt->BindInt64Parameter(0, aItemId);
+    mozStorageStatementScoper scope(mDBGetItemProperties);
+    rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
     NS_ENSURE_SUCCESS(rv, rv);
 
-    PRBool hasResult;
-    rv = getInfoStmt->ExecuteStep(&hasResult);
+    PRBool results;
+    rv = mDBGetItemProperties->ExecuteStep(&results);
     NS_ENSURE_SUCCESS(rv, rv);
-    if (!hasResult) {
+    if (!results) {
       return NS_ERROR_INVALID_ARG; // folder is not in the hierarchy
     }
 
-    rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_Parent, &oldParent);
+    rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent,
+                                        &oldParent);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Position, &oldIndex);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Position,
+                                        &oldIndex);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Type,
-                               (PRInt32*)&itemType);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                        (PRInt32*)&itemType);
     NS_ENSURE_SUCCESS(rv, rv);
     if (itemType == TYPE_FOLDER) {
-      rv = getInfoStmt->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId,
-                                      folderType);
+      rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId,
+                                               folderType);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
 
   // if parent and index are the same, nothing to do
   if (oldParent == aNewParent && oldIndex == aIndex)
     return NS_OK;
 
   // Make sure aNewParent is not aFolder or a subfolder of aFolder
   if (itemType == TYPE_FOLDER) {
     PRInt64 ancestorId = aNewParent;
 
     while (ancestorId) {
+      mozStorageStatementScoper scope(mDBGetItemProperties);
       if (ancestorId == aItemId) {
         return NS_ERROR_INVALID_ARG;
       }
 
-      mozIStorageStatement* getInfoStmt = GetStatement(mDBGetItemProperties);
-      NS_ENSURE_STATE(getInfoStmt);
-      mozStorageStatementScoper scoper(getInfoStmt);
-      rv = getInfoStmt->BindInt64Parameter(0, ancestorId);
+      rv = mDBGetItemProperties->BindInt64Parameter(0, ancestorId);
       NS_ENSURE_SUCCESS(rv, rv);
 
       PRBool hasResult;
-      rv = getInfoStmt->ExecuteStep(&hasResult);
+      rv = mDBGetItemProperties->ExecuteStep(&hasResult);
       NS_ENSURE_SUCCESS(rv, rv);
       if (hasResult) {
-        rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_Parent, &ancestorId);
+        rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent,
+                                            &ancestorId);
         NS_ENSURE_SUCCESS(rv, rv);
       }
       else {
         break;
       }
     }
   }
 
@@ -2132,21 +2171,19 @@ nsNavBookmarks::MoveItem(PRInt64 aItemId
     NS_ENSURE_SUCCESS(rv, rv);
     rv = stmt->BindInt64Parameter(2, aItemId);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = stmt->Execute();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   PRTime now = PR_Now();
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           oldParent, now);
+  rv = SetItemDateInternal(mDBSetItemLastModified, oldParent, now);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           aNewParent, now);
+  rv = SetItemDateInternal(mDBSetItemLastModified, aNewParent, now);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = transaction.Commit();
   NS_ENSURE_SUCCESS(rv, rv);
 
   // notify bookmark observers
   ENUMERATE_OBSERVERS(mCanNotify, mCacheObservers, mObservers, nsINavBookmarkObserver,
                       OnItemMoved(aItemId, oldParent, oldIndex, aNewParent,
@@ -2165,18 +2202,17 @@ nsNavBookmarks::MoveItem(PRInt64 aItemId
 }
 
 
 nsresult
 nsNavBookmarks::SetItemDateInternal(mozIStorageStatement* aStatement,
                                     PRInt64 aItemId,
                                     PRTime aValue)
 {
-  NS_ENSURE_STATE(aStatement);
-  mozStorageStatementScoper scoper(aStatement);
+  mozStorageStatementScoper scope(aStatement);
   nsresult rv = aStatement->BindInt64Parameter(0, aValue);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = aStatement->BindInt64Parameter(1, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = aStatement->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -2190,18 +2226,17 @@ nsNavBookmarks::SetItemDateInternal(mozI
 NS_IMETHODIMP
 nsNavBookmarks::SetItemDateAdded(PRInt64 aItemId, PRTime aDateAdded)
 {
   // GetItemType also ensures that aItemId points to a valid item.
   PRUint16 itemType;
   nsresult rv = GetItemType(aItemId, &itemType);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = SetItemDateInternal(GetStatement(mDBSetItemDateAdded),
-                           aItemId, aDateAdded);
+  rv = SetItemDateInternal(mDBSetItemDateAdded, aItemId, aDateAdded);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Note: mDBSetItemDateAdded also sets lastModified to aDateAdded.
   ENUMERATE_OBSERVERS(mCanNotify, mCacheObservers, mObservers, nsINavBookmarkObserver,
                       OnItemChanged(aItemId, NS_LITERAL_CSTRING("dateAdded"),
                                     PR_FALSE,
                                     nsPrintfCString(16, "%lld", aDateAdded),
                                     aDateAdded,
@@ -2211,44 +2246,42 @@ nsNavBookmarks::SetItemDateAdded(PRInt64
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetItemDateAdded(PRInt64 aItemId, PRTime* _dateAdded)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
   NS_ENSURE_ARG_POINTER(_dateAdded);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  rv = mDBGetItemProperties->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(hasResult, NS_ERROR_INVALID_ARG); // Invalid itemId.
 
-  rv = stmt->GetInt64(kGetItemPropertiesIndex_DateAdded, _dateAdded);
+  rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_DateAdded,
+                                      _dateAdded);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::SetItemLastModified(PRInt64 aItemId, PRTime aLastModified)
 {
   // GetItemType also ensures that aItemId points to a valid item.
   PRUint16 itemType;
   nsresult rv = GetItemType(aItemId, &itemType);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           aItemId, aLastModified);
+  rv = SetItemDateInternal(mDBSetItemLastModified, aItemId, aLastModified);
   NS_ENSURE_SUCCESS(rv, rv);
 
   ENUMERATE_OBSERVERS(mCanNotify, mCacheObservers, mObservers, nsINavBookmarkObserver,
                       OnItemChanged(aItemId,
                                     NS_LITERAL_CSTRING("lastModified"),
                                     PR_FALSE,
                                     nsPrintfCString(16, "%lld", aLastModified),
                                     aLastModified,
@@ -2258,32 +2291,28 @@ nsNavBookmarks::SetItemLastModified(PRIn
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetItemLastModified(PRInt64 aItemId, PRTime* aLastModified)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
   NS_ENSURE_ARG_POINTER(aLastModified);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  PRBool results;
+  rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!hasResult)
+  if (!results)
     return NS_ERROR_INVALID_ARG; // invalid item id
 
-  rv = stmt->GetInt64(kGetItemPropertiesIndex_LastModified, aLastModified);
-  NS_ENSURE_SUCCESS(rv, rv);
-  return NS_OK;
+  return mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_LastModified, aLastModified);
 }
 
 
 nsresult
 nsNavBookmarks::GetGUIDBase(nsAString &aGUIDBase)
 {
   if (!mGUIDBase.IsEmpty()) {
     aGUIDBase = mGUIDBase;
@@ -2350,31 +2379,29 @@ nsNavBookmarks::SetItemGUID(PRInt64 aIte
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetItemIdForGUID(const nsAString& aGUID, PRInt64* aItemId)
 {
   NS_ENSURE_ARG_POINTER(aItemId);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemIdForGUID);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindStringParameter(0, aGUID);
+  mozStorageStatementScoper scoper(mDBGetItemIdForGUID);
+  nsresult rv = mDBGetItemIdForGUID->BindStringParameter(0, aGUID);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasMore = PR_FALSE;
-  rv = stmt->ExecuteStep(&hasMore);
+  rv = mDBGetItemIdForGUID->ExecuteStep(&hasMore);
   if (NS_FAILED(rv) || ! hasMore) {
     *aItemId = -1;
     return NS_OK; // not found: return -1
   }
 
   // found, get the itemId
-  rv = stmt->GetInt64(0, aItemId);
+  rv = mDBGetItemIdForGUID->GetInt64(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::SetItemTitle(PRInt64 aItemId, const nsACString& aTitle)
 {
@@ -2413,213 +2440,205 @@ nsNavBookmarks::SetItemTitle(PRInt64 aIt
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetItemTitle(PRInt64 aItemId, nsACString& aTitle)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  PRBool results;
+  rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!hasResult)
+  if (!results)
     return NS_ERROR_INVALID_ARG; // invalid bookmark id
 
-  rv = stmt->GetUTF8String(kGetItemPropertiesIndex_Title, aTitle);
+  rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_Title,
+                                           aTitle);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetBookmarkURI(PRInt64 aItemId, nsIURI** aURI)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
   NS_ENSURE_ARG_POINTER(aURI);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  PRBool results;
+  rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!hasResult)
+  if (!results)
     return NS_ERROR_INVALID_ARG; // invalid bookmark id
 
   PRInt32 type;
-  rv = stmt->GetInt32(kGetItemPropertiesIndex_Type, (PRInt32*)&type);
+  rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                      (PRInt32*)&type);
   NS_ENSURE_SUCCESS(rv, rv);
   // Ensure this is a bookmark.
   NS_ENSURE_TRUE(type == TYPE_BOOKMARK, NS_ERROR_INVALID_ARG);
 
   nsCAutoString spec;
-  rv = stmt->GetUTF8String(kGetItemPropertiesIndex_URI, spec);
+  rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_URI, spec);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = NS_NewURI(aURI, spec);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetItemType(PRInt64 aItemId, PRUint16* _type)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
   NS_ENSURE_ARG_POINTER(_type);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  PRBool results;
+  rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!hasResult) {
+  if (!results) {
     return NS_ERROR_INVALID_ARG; // invalid bookmark id
   }
 
-  rv = stmt->GetInt32(kGetItemPropertiesIndex_Type, (PRInt32*)_type);
+  rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                      (PRInt32*)_type);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 nsresult
 nsNavBookmarks::GetFolderType(PRInt64 aFolder, nsACString& aType)
 {
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aFolder);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aFolder);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  PRBool results;
+  rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!hasResult) {
+  if (!results) {
     return NS_ERROR_INVALID_ARG;
   }
 
-  return stmt->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId, aType);
+  return mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId, aType);
 }
 
 
 nsresult
 nsNavBookmarks::ResultNodeForContainer(PRInt64 aID,
                                        nsNavHistoryQueryOptions* aOptions,
                                        nsNavHistoryResultNode** aNode)
 {
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aID);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  mDBGetItemProperties->BindInt64Parameter(0, aID);
+
+  PRBool results;
+  nsresult rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
-
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
-  NS_ENSURE_SUCCESS(rv, rv);
-  NS_ENSURE_TRUE(hasResult, NS_ERROR_INVALID_ARG);
+  NS_ASSERTION(results, "ResultNodeForContainer expects a valid item id");
 
   nsCAutoString title;
-  rv = stmt->GetUTF8String(kGetItemPropertiesIndex_Title, title);
+  rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_Title,
+                                           title);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRUint16 itemType;
-  rv = stmt->GetInt32(kGetItemPropertiesIndex_Type, (PRInt32*)&itemType);
+  rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                      (PRInt32*)&itemType);
   NS_ENSURE_SUCCESS(rv, rv);
   if (itemType == TYPE_DYNAMIC_CONTAINER) {
     // contract id
     nsCAutoString contractId;
-    rv = stmt->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId,
-                             contractId);
+    rv = mDBGetItemProperties->GetUTF8String(kGetItemPropertiesIndex_ServiceContractId,
+                                             contractId);
     NS_ENSURE_SUCCESS(rv, rv);
-    *aNode = new nsNavHistoryContainerResultNode(EmptyCString(),
-                                                 title,
-                                                 EmptyCString(),
+    *aNode = new nsNavHistoryContainerResultNode(EmptyCString(), title, EmptyCString(),
                                                  nsINavHistoryResultNode::RESULT_TYPE_DYNAMIC_CONTAINER,
                                                  PR_TRUE,
                                                  contractId,
                                                  aOptions);
     (*aNode)->mItemId = aID;
   }
   else { // TYPE_FOLDER
     *aNode = new nsNavHistoryFolderResultNode(title, aOptions, aID, EmptyCString());
   }
   if (!*aNode)
     return NS_ERROR_OUT_OF_MEMORY;
 
-  rv = stmt->GetInt64(kGetItemPropertiesIndex_DateAdded,
-                      &(*aNode)->mDateAdded);
+  rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_DateAdded,
+                                      &(*aNode)->mDateAdded);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->GetInt64(kGetItemPropertiesIndex_LastModified,
-                      &(*aNode)->mLastModified);
+  rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_LastModified,
+                                      &(*aNode)->mLastModified);
   NS_ENSURE_SUCCESS(rv, rv);
 
   NS_ADDREF(*aNode);
   return NS_OK;
 }
 
 
 nsresult
 nsNavBookmarks::QueryFolderChildren(PRInt64 aFolderId,
                                     nsNavHistoryQueryOptions* aOptions,
                                     nsCOMArray<nsNavHistoryResultNode>* aChildren)
 {
-  mozIStorageStatement* stmt = GetStatement(mDBGetChildren);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-
-  nsresult rv = stmt->BindInt64Parameter(0, aFolderId);
+  mozStorageStatementScoper scope(mDBGetChildren);
+
+  nsresult rv = mDBGetChildren->BindInt64Parameter(0, aFolderId);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  PRBool results;
+
   nsCOMPtr<nsNavHistoryQueryOptions> options = do_QueryInterface(aOptions, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRInt32 index = -1;
-  PRBool hasResult;
-  while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
+  while (NS_SUCCEEDED(mDBGetChildren->ExecuteStep(&results)) && results) {
 
     // The results will be in order of index. Even if we don't add a node
     // because it was excluded, we need to count its index, so do that
     // before doing anything else. Index was initialized to -1 above, so
     // it will start counting at 0 the first time through the loop.
     index ++;
 
     PRUint16 itemType;
-    rv = stmt->GetInt32(kGetChildrenIndex_Type, (PRInt32*)&itemType);
+    rv = mDBGetChildren->GetInt32(kGetChildrenIndex_Type,
+                                  (PRInt32*)&itemType);
     NS_ENSURE_SUCCESS(rv, rv);
     PRInt64 id;
-    rv = stmt->GetInt64(nsNavHistory::kGetInfoIndex_ItemId, &id);
+    rv = mDBGetChildren->GetInt64(nsNavHistory::kGetInfoIndex_ItemId, &id);
     NS_ENSURE_SUCCESS(rv, rv);
     nsRefPtr<nsNavHistoryResultNode> node;
     if (itemType == TYPE_BOOKMARK) {
       nsNavHistory* history = nsNavHistory::GetHistoryService();
       NS_ENSURE_TRUE(history, NS_ERROR_OUT_OF_MEMORY);
-      rv = history->RowToResult(stmt, options, getter_AddRefs(node));
+      rv = history->RowToResult(mDBGetChildren, options, getter_AddRefs(node));
       NS_ENSURE_SUCCESS(rv, rv);
 
       PRUint32 nodeType;
       node->GetType(&nodeType);
       if ((nodeType == nsINavHistoryResultNode::RESULT_TYPE_QUERY &&
            aOptions->ExcludeQueries()) ||
           (nodeType != nsINavHistoryResultNode::RESULT_TYPE_QUERY &&
            nodeType != nsINavHistoryResultNode::RESULT_TYPE_FOLDER_SHORTCUT &&
@@ -2643,25 +2662,25 @@ nsNavBookmarks::QueryFolderChildren(PRIn
       if (aOptions->ExcludeItems()) {
         continue;
       }
       node = new nsNavHistorySeparatorResultNode();
       NS_ENSURE_TRUE(node, NS_ERROR_OUT_OF_MEMORY);
 
       // add the item identifier (RowToResult does so for bookmark items in
       // the next else block);
-      rv = stmt->GetInt64(nsNavHistory::kGetInfoIndex_ItemId,
+      rv = mDBGetChildren->GetInt64(nsNavHistory::kGetInfoIndex_ItemId,
                                     &node->mItemId);
       NS_ENSURE_SUCCESS(rv, rv);
       // date-added and last-modified
-      rv = stmt->GetInt64(nsNavHistory::kGetInfoIndex_ItemDateAdded,
-                          &node->mDateAdded);
+      rv = mDBGetChildren->GetInt64(nsNavHistory::kGetInfoIndex_ItemDateAdded,
+                                    &node->mDateAdded);
       NS_ENSURE_SUCCESS(rv, rv);
-      rv = stmt->GetInt64(nsNavHistory::kGetInfoIndex_ItemLastModified,
-                          &node->mLastModified);
+      rv = mDBGetChildren->GetInt64(nsNavHistory::kGetInfoIndex_ItemLastModified,
+                                    &node->mLastModified);
       NS_ENSURE_SUCCESS(rv, rv);
     }
 
     // this method fills all bookmark queries, so we store the index of the
     // item in its parent
     node->mBookmarkIndex = index;
 
     NS_ENSURE_TRUE(aChildren->AppendObject(node), NS_ERROR_OUT_OF_MEMORY);
@@ -2669,35 +2688,33 @@ nsNavBookmarks::QueryFolderChildren(PRIn
   return NS_OK;
 }
 
 
 nsresult
 nsNavBookmarks::FolderCount(PRInt64 aFolderId, PRInt32* _folderCount)
 {
   *_folderCount = 0;
-  mozIStorageStatement* stmt = GetStatement(mDBFolderCount);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-
-  nsresult rv = stmt->BindInt64Parameter(0, aFolderId);
+  mozStorageStatementScoper scope(mDBFolderCount);
+
+  nsresult rv = mDBFolderCount->BindInt64Parameter(0, aFolderId);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  rv = mDBFolderCount->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(hasResult, NS_ERROR_UNEXPECTED);
 
   // Ensure that the folder we are looking for exists.
   PRInt64 confirmFolderId;
-  rv = stmt->GetInt64(1, &confirmFolderId);
+  rv = mDBFolderCount->GetInt64(1, &confirmFolderId);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(confirmFolderId == aFolderId, NS_ERROR_INVALID_ARG);
 
-  rv = stmt->GetInt32(0, _folderCount);
+  rv = mDBFolderCount->GetInt32(0, _folderCount);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::IsBookmarked(nsIURI* aURI, PRBool* aBookmarked)
@@ -2862,57 +2879,53 @@ nsNavBookmarks::ChangeBookmarkURI(PRInt6
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetFolderIdForItem(PRInt64 aItemId, PRInt64* aFolderId)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
   NS_ENSURE_ARG_POINTER(aFolderId);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  PRBool results;
+  rv = mDBGetItemProperties->ExecuteStep(&results);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!hasResult)
+  if (!results)
     return NS_ERROR_INVALID_ARG; // invalid item id
 
-  rv = stmt->GetInt64(kGetItemPropertiesIndex_Parent, aFolderId);
+  rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent, aFolderId);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // this should not happen, but see bug #400448 for details
   NS_ENSURE_TRUE(aItemId != *aFolderId, NS_ERROR_UNEXPECTED);
   return NS_OK;
 }
 
 
 nsresult
 nsNavBookmarks::GetBookmarkIdsForURITArray(nsIURI* aURI,
                                            nsTArray<PRInt64>& aResult)
 {
   NS_ENSURE_ARG(aURI);
 
-  mozIStorageStatement* stmt = GetStatement(mDBFindURIBookmarks);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-
-  nsresult rv = BindStatementURI(stmt, 0, aURI);
+  mozStorageStatementScoper scope(mDBFindURIBookmarks);
+
+  nsresult rv = BindStatementURI(mDBFindURIBookmarks, 0, aURI);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->BindInt32Parameter(1, TYPE_BOOKMARK);
+  rv = mDBFindURIBookmarks->BindInt32Parameter(1, TYPE_BOOKMARK);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool more;
-  while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&more))) && more) {
+  while (NS_SUCCEEDED((rv = mDBFindURIBookmarks->ExecuteStep(&more))) && more) {
     PRInt64 bookmarkId;
-    rv = stmt->GetInt64(kFindBookmarksIndex_ID, &bookmarkId);
+    rv = mDBFindURIBookmarks->GetInt64(kFindBookmarksIndex_ID, &bookmarkId);
     NS_ENSURE_SUCCESS(rv, rv);
     NS_ENSURE_TRUE(aResult.AppendElement(bookmarkId), NS_ERROR_OUT_OF_MEMORY);
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
@@ -2951,28 +2964,25 @@ nsNavBookmarks::GetBookmarkIdsForURI(nsI
 NS_IMETHODIMP
 nsNavBookmarks::GetItemIndex(PRInt64 aItemId, PRInt32* _index)
 {
   NS_ENSURE_ARG_MIN(aItemId, 1);
   NS_ENSURE_ARG_POINTER(_index);
 
   *_index = -1;
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemIndex);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
-  NS_ENSURE_SUCCESS(rv, rv);
+  mozStorageStatementScoper scope(mDBGetItemIndex);
+  mDBGetItemIndex->BindInt64Parameter(0, aItemId);
   PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  nsresult rv = mDBGetItemIndex->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, rv);
   if (!hasResult)
     return NS_OK;
 
-  rv = stmt->GetInt32(0, _index);
+  rv = mDBGetItemIndex->GetInt32(0, _index);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::SetItemIndex(PRInt64 aItemId, PRInt32 aNewIndex)
@@ -2981,52 +2991,50 @@ nsNavBookmarks::SetItemIndex(PRInt64 aIt
   NS_ENSURE_ARG_MIN(aNewIndex, 0);
 
   nsresult rv;
   PRInt32 oldIndex = 0;
   PRInt64 parent = 0;
   PRUint16 itemType;
 
   {
-    mozIStorageStatement* getInfoStmt(mDBGetItemProperties);
-    NS_ENSURE_STATE(getInfoStmt);
-    mozStorageStatementScoper scoper(getInfoStmt);
-    rv = getInfoStmt->BindInt64Parameter(0, aItemId);
+    mozStorageStatementScoper scopeGet(mDBGetItemProperties);
+    rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
     NS_ENSURE_SUCCESS(rv, rv);
 
-    PRBool hasResult;
-    rv = getInfoStmt->ExecuteStep(&hasResult);
+    PRBool results;
+    rv = mDBGetItemProperties->ExecuteStep(&results);
     NS_ENSURE_SUCCESS(rv, rv);
-    if (!hasResult)
+    if (!results)
       return NS_OK;
 
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Position, &oldIndex);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Position,
+                                        &oldIndex);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt32(kGetItemPropertiesIndex_Type,
-                               (PRInt32*)&itemType);
+    rv = mDBGetItemProperties->GetInt32(kGetItemPropertiesIndex_Type,
+                                        (PRInt32*)&itemType);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = getInfoStmt->GetInt64(kGetItemPropertiesIndex_Parent, &parent);
+    rv = mDBGetItemProperties->GetInt64(kGetItemPropertiesIndex_Parent,
+                                        &parent);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // Ensure we are not going out of range.
   PRInt32 folderCount;
   rv = FolderCount(parent, &folderCount);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(aNewIndex < folderCount, NS_ERROR_INVALID_ARG);
 
-  mozIStorageStatement* stmt = GetStatement(mDBSetItemIndex);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scoper(mDBSetItemIndex);
+  rv = mDBSetItemIndex->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = stmt->BindInt32Parameter(1, aNewIndex);
+  rv = mDBSetItemIndex->BindInt32Parameter(1, aNewIndex);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = stmt->Execute();
+  rv = mDBSetItemIndex->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
 
   ENUMERATE_OBSERVERS(mCanNotify, mCacheObservers, mObservers, nsINavBookmarkObserver,
                       OnItemMoved(aItemId, parent, oldIndex, parent,
                                   aNewIndex, itemType));
 
   return NS_OK;
 }
@@ -3125,87 +3133,79 @@ nsNavBookmarks::SetKeywordForBookmark(PR
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetKeywordForURI(nsIURI* aURI, nsAString& aKeyword)
 {
   NS_ENSURE_ARG(aURI);
   aKeyword.Truncate(0);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetKeywordForURI);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = BindStatementURI(stmt, 0, aURI);
+  mozStorageStatementScoper scoper(mDBGetKeywordForURI);
+  nsresult rv = BindStatementURI(mDBGetKeywordForURI, 0, aURI);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasMore = PR_FALSE;
-  rv = stmt->ExecuteStep(&hasMore);
+  rv = mDBGetKeywordForURI->ExecuteStep(&hasMore);
   if (NS_FAILED(rv) || !hasMore) {
     aKeyword.SetIsVoid(PR_TRUE);
     return NS_OK; // not found: return void keyword string
   }
 
   // found, get the keyword
-  rv = stmt->GetString(0, aKeyword);
+  rv = mDBGetKeywordForURI->GetString(0, aKeyword);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetKeywordForBookmark(PRInt64 aBookmarkId, nsAString& aKeyword)
 {
   NS_ENSURE_ARG_MIN(aBookmarkId, 1);
   aKeyword.Truncate(0);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetKeywordForBookmark);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aBookmarkId);
+  mozStorageStatementScoper scoper(mDBGetKeywordForBookmark);
+  nsresult rv = mDBGetKeywordForBookmark->BindInt64Parameter(0, aBookmarkId);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasMore = PR_FALSE;
-  rv = stmt->ExecuteStep(&hasMore);
+  rv = mDBGetKeywordForBookmark->ExecuteStep(&hasMore);
   if (NS_FAILED(rv) || ! hasMore) {
     aKeyword.SetIsVoid(PR_TRUE);
     return NS_OK; // not found: return void keyword string
   }
 
   // found, get the keyword
-  rv = stmt->GetString(0, aKeyword);
-  NS_ENSURE_SUCCESS(rv, rv);
-  return NS_OK;
+  return mDBGetKeywordForBookmark->GetString(0, aKeyword);
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::GetURIForKeyword(const nsAString& aKeyword, nsIURI** aURI)
 {
   NS_ENSURE_ARG_POINTER(aURI);
   NS_ENSURE_TRUE(!aKeyword.IsEmpty(), NS_ERROR_INVALID_ARG);
   *aURI = nsnull;
 
   // Shortcuts are always lowercased internally.
   nsAutoString kwd(aKeyword);
   ToLowerCase(kwd);
 
-  mozIStorageStatement* stmt = GetStatement(mDBGetURIForKeyword);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindStringParameter(0, kwd);
+  mozStorageStatementScoper scoper(mDBGetURIForKeyword);
+  nsresult rv = mDBGetURIForKeyword->BindStringParameter(0, kwd);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRBool hasMore = PR_FALSE;
-  rv = stmt->ExecuteStep(&hasMore);
+  rv = mDBGetURIForKeyword->ExecuteStep(&hasMore);
   if (NS_FAILED(rv) || ! hasMore)
     return NS_OK; // not found: leave URI null
 
   // found, get the URI
   nsCAutoString spec;
-  rv = stmt->GetUTF8String(0, spec);
+  rv = mDBGetURIForKeyword->GetUTF8String(0, spec);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = NS_NewURI(aURI, spec);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 
 // See RunInBatchMode
@@ -3442,18 +3442,17 @@ NS_IMETHODIMP
 nsNavBookmarks::OnItemAnnotationSet(PRInt64 aItemId, const nsACString& aName)
 {
   // GetItemType also ensures that aItemId points to a valid item.
   PRUint16 itemType;
   nsresult rv = GetItemType(aItemId, &itemType);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRTime lastModified = PR_Now();
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           aItemId, lastModified);
+  rv = SetItemDateInternal(mDBSetItemLastModified, aItemId, lastModified);
   NS_ENSURE_SUCCESS(rv, rv);
 
   ENUMERATE_OBSERVERS(mCanNotify, mCacheObservers, mObservers,
                       nsINavBookmarkObserver,
                       OnItemChanged(aItemId, aName, PR_TRUE, EmptyCString(),
                                     lastModified, itemType));
 
   return NS_OK;
@@ -3471,35 +3470,32 @@ NS_IMETHODIMP
 nsNavBookmarks::OnItemAnnotationRemoved(PRInt64 aItemId, const nsACString& aName)
 {
   // GetItemType also ensures that aItemId points to a valid item.
   PRUint16 itemType;
   nsresult rv = GetItemType(aItemId, &itemType);
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRTime lastModified = PR_Now();
-  rv = SetItemDateInternal(GetStatement(mDBSetItemLastModified),
-                           aItemId, lastModified);
+  rv = SetItemDateInternal(mDBSetItemLastModified, aItemId, lastModified);
   NS_ENSURE_SUCCESS(rv, rv);
 
   ENUMERATE_OBSERVERS(mCanNotify, mCacheObservers, mObservers,
                       nsINavBookmarkObserver,
                       OnItemChanged(aItemId, aName, PR_TRUE, EmptyCString(),
                                     lastModified, itemType));
 
   return NS_OK;
 }
 
 
 PRBool
 nsNavBookmarks::ItemExists(PRInt64 aItemId) {
-  mozIStorageStatement* stmt = GetStatement(mDBGetItemProperties);
-  NS_ENSURE_STATE(stmt);
-  mozStorageStatementScoper scoper(stmt);
-  nsresult rv = stmt->BindInt64Parameter(0, aItemId);
+  mozStorageStatementScoper scope(mDBGetItemProperties);
+  nsresult rv = mDBGetItemProperties->BindInt64Parameter(0, aItemId);
   NS_ENSURE_SUCCESS(rv, PR_FALSE);
 
   PRBool hasResult;
-  rv = stmt->ExecuteStep(&hasResult);
+  rv = mDBGetItemProperties->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, PR_FALSE);
 
   return hasResult;
 }
--- a/toolkit/components/places/src/nsNavBookmarks.h
+++ b/toolkit/components/places/src/nsNavBookmarks.h
@@ -131,16 +131,17 @@ public:
 
 private:
   static nsNavBookmarks* gBookmarksService;
 
   ~nsNavBookmarks();
 
   nsresult InitRoots();
   nsresult InitDefaults();
+  nsresult InitStatements();
   nsresult CreateRoot(mozIStorageStatement* aGetRootStatement,
                       const nsCString& name,
                       PRInt64* aID,
                       PRInt64 aParentID,
                       PRBool* aWasCreated);
 
   nsresult AdjustIndices(PRInt64 aFolder,
                          PRInt32 aStartIndex,
@@ -276,75 +277,80 @@ private:
   /**
    * TArray version of getBookmarksIdForURI for ease of use in C++ code.
    * Pass in a reference to a TArray; it will get filled with the
    * resulting list of bookmark IDs.
    */
   nsresult GetBookmarkIdsForURITArray(nsIURI* aURI,
                                       nsTArray<PRInt64>& aResult);
 
-
-  /**
-   *  You should always use this getter and never use directly the nsCOMPtr.
-   */
-  mozIStorageStatement* GetStatement(const nsCOMPtr<mozIStorageStatement>& aStmt);
-
+  // kGetInfoIndex_* results + kGetChildrenIndex_* results
   nsCOMPtr<mozIStorageStatement> mDBGetChildren;
-  // kGetInfoIndex_* results + kGetChildrenIndex_* results
   static const PRInt32 kGetChildrenIndex_Position;
   static const PRInt32 kGetChildrenIndex_Type;
   static const PRInt32 kGetChildrenIndex_PlaceID;
   static const PRInt32 kGetChildrenIndex_FolderTitle;
   static const PRInt32 kGetChildrenIndex_ServiceContractId;
 
-  nsCOMPtr<mozIStorageStatement> mDBFindURIBookmarks;
+  nsCOMPtr<mozIStorageStatement> mDBFindURIBookmarks;  // kFindBookmarksIndex_* results
   static const PRInt32 kFindBookmarksIndex_ID;
   static const PRInt32 kFindBookmarksIndex_Type;
   static const PRInt32 kFindBookmarksIndex_PlaceID;
   static const PRInt32 kFindBookmarksIndex_Parent;
   static const PRInt32 kFindBookmarksIndex_Position;
   static const PRInt32 kFindBookmarksIndex_Title;
 
-  nsCOMPtr<mozIStorageStatement> mDBGetItemProperties;
+  nsCOMPtr<mozIStorageStatement> mDBFolderCount;
+
+  nsCOMPtr<mozIStorageStatement> mDBGetItemIndex;
+  nsCOMPtr<mozIStorageStatement> mDBGetChildAt;
+
+  nsCOMPtr<mozIStorageStatement> mDBGetItemProperties; // kGetItemPropertiesIndex_*
   static const PRInt32 kGetItemPropertiesIndex_ID;
-  static const PRInt32 kGetItemPropertiesIndex_URI;
+  static const PRInt32 kGetItemPropertiesIndex_URI; // null for folders and separators
   static const PRInt32 kGetItemPropertiesIndex_Title;
   static const PRInt32 kGetItemPropertiesIndex_Position;
   static const PRInt32 kGetItemPropertiesIndex_PlaceID;
   static const PRInt32 kGetItemPropertiesIndex_Parent;
   static const PRInt32 kGetItemPropertiesIndex_Type;
   static const PRInt32 kGetItemPropertiesIndex_ServiceContractId;
   static const PRInt32 kGetItemPropertiesIndex_DateAdded;
   static const PRInt32 kGetItemPropertiesIndex_LastModified;
 
+  nsCOMPtr<mozIStorageStatement> mDBGetItemIdForGUID;
+  nsCOMPtr<mozIStorageStatement> mDBGetRedirectDestinations;
+
   nsCOMPtr<mozIStorageStatement> mDBInsertBookmark;
   static const PRInt32 kInsertBookmarkIndex_Id;
   static const PRInt32 kInsertBookmarkIndex_PlaceId;
   static const PRInt32 kInsertBookmarkIndex_Type;
   static const PRInt32 kInsertBookmarkIndex_Parent;
   static const PRInt32 kInsertBookmarkIndex_Position;
   static const PRInt32 kInsertBookmarkIndex_Title;
   static const PRInt32 kInsertBookmarkIndex_ServiceContractId;
   static const PRInt32 kInsertBookmarkIndex_DateAdded;
   static const PRInt32 kInsertBookmarkIndex_LastModified;
 
-  nsCOMPtr<mozIStorageStatement> mDBFolderCount;
-  nsCOMPtr<mozIStorageStatement> mDBGetItemIndex;
-  nsCOMPtr<mozIStorageStatement> mDBGetChildAt;
-  nsCOMPtr<mozIStorageStatement> mDBGetItemIdForGUID;
-  nsCOMPtr<mozIStorageStatement> mDBGetRedirectDestinations;
   nsCOMPtr<mozIStorageStatement> mDBIsBookmarkedInDatabase;
   nsCOMPtr<mozIStorageStatement> mDBIsRealBookmark;
   nsCOMPtr<mozIStorageStatement> mDBGetLastBookmarkID;
   nsCOMPtr<mozIStorageStatement> mDBSetItemDateAdded;
   nsCOMPtr<mozIStorageStatement> mDBSetItemLastModified;
   nsCOMPtr<mozIStorageStatement> mDBSetItemIndex;
+
+  // keywords
   nsCOMPtr<mozIStorageStatement> mDBGetKeywordForURI;
   nsCOMPtr<mozIStorageStatement> mDBGetKeywordForBookmark;
   nsCOMPtr<mozIStorageStatement> mDBGetURIForKeyword;
+
+  /**
+   *  For the next statements you should always use this getter and never use
+   *  directly the statement nsCOMPtr.
+   */
+  mozIStorageStatement* GetStatement(const nsCOMPtr<mozIStorageStatement>& aStmt);
   nsCOMPtr<mozIStorageStatement> mDBAdjustPosition;
   nsCOMPtr<mozIStorageStatement> mDBRemoveItem;
   nsCOMPtr<mozIStorageStatement> mDBGetLastChildId;
   nsCOMPtr<mozIStorageStatement> mDBMoveItem;
   nsCOMPtr<mozIStorageStatement> mDBSetItemTitle;
   nsCOMPtr<mozIStorageStatement> mDBChangeBookmarkURI;
 
   class RemoveFolderTransaction : public nsITransaction {