B2G/gecko/toolkit/components/places/tests/queries/head_queries.js
/**
 * Generalized function to pull in an array of objects of data and push it into
 * the database. It does NOT do any checking to see that the input is
 * appropriate.
 */
Generalized function to pull in an array of objects of data and push it into
the database. It does NOT do any checking to see that the input is
appropriate.
Generalized function to pull in an array of objects of data and push it into the database . It does NOT do any checking to see that the input is appropriate .
function populateDB(aArray) {
  PlacesUtils.history.runInBatchMode({
    runBatched: function (aUserData)
    {
      aArray.forEach(function (data)
      {
        try {
          var qdata = new queryData(data);
          if (qdata.isVisit) {
            var referrer = qdata.referrer ? uri(qdata.referrer) : null;
            var visitId = PlacesUtils.history.addVisit(uri(qdata.uri), qdata.lastVisit,
                                                       referrer, qdata.transType,
                                                       qdata.isRedirect, qdata.sessionID);
            if (qdata.title && !qdata.isDetails) {
              PlacesUtils.history.setPageTitle(uri(qdata.uri), qdata.title);
            }
            if (qdata.visitCount && !qdata.isDetails) {
              let stmt = DBConn().createStatement(
                "UPDATE moz_places SET visit_count = :vc WHERE url = :url");
              stmt.params.vc = qdata.visitCount;
              stmt.params.url = qdata.uri;
              try {
                stmt.execute();
              }
              catch (ex) {
                print("Error while setting visit_count.");
              }
              finally {
                stmt.finalize();
              }
            }
          }
          if (qdata.isRedirect) {
            let stmt = DBConn().createAsyncStatement(
              "UPDATE moz_places SET hidden = 1 WHERE url = :url");
            stmt.params.url = qdata.uri;
            try {
              stmt.executeAsync();
            }
            catch (ex) {
              print("Error while setting hidden.");
            }
            finally {
              stmt.finalize();
            }
          }
          if (qdata.isDetails) {
            PlacesUtils.history.addVisit(uri(qdata.uri),
                                         qdata.lastVisit,
                                         null,
                                         TRANSITION_LINK,
                                         false,
                                         0);
            PlacesUtils.ghistory2.setPageTitle(uri(qdata.uri),
                                               qdata.title);
          }
          if (qdata.markPageAsTyped){
            PlacesUtils.bhistory.markPageAsTyped(uri(qdata.uri));
          }
          if (qdata.isPageAnnotation) {
            if (qdata.removeAnnotation) 
              PlacesUtils.annotations.removePageAnnotation(uri(qdata.uri),
                                                           qdata.annoName);
            else {
              PlacesUtils.annotations.setPageAnnotation(uri(qdata.uri),
                                                        qdata.annoName,
                                                        qdata.annoVal,
                                                        qdata.annoFlags,
                                                        qdata.annoExpiration);
            }
          }
          if (qdata.isItemAnnotation) {
            if (qdata.removeAnnotation)
              PlacesUtils.annotations.removeItemAnnotation(qdata.itemId,
                                                           qdata.annoName);
            else {
              PlacesUtils.annotations.setItemAnnotation(qdata.itemId,
                                                        qdata.annoName,
                                                        qdata.annoVal,
                                                        qdata.annoFlags,
                                                        qdata.annoExpiration);
            }
          }
          if (qdata.isPageBinaryAnnotation) {
            if (qdata.removeAnnotation)
              PlacesUtils.annotations.removePageAnnotation(uri(qdata.uri),
                                                           qdata.annoName);
            else {
              PlacesUtils.annotations.setPageAnnotationBinary(uri(qdata.uri),
                                                              qdata.annoName,
                                                              qdata.binarydata,
                                                              qdata.binaryDataLength,
                                                              qdata.annoMimeType,
                                                              qdata.annoFlags,
                                                              qdata.annoExpiration);
            }
          }
          if (qdata.isItemBinaryAnnotation) {
            if (qdata.removeAnnotation)
              PlacesUtils.annotations.removeItemAnnotation(qdata.itemId,
                                                           qdata.annoName);
            else {
              PlacesUtils.annotations.setItemAnnotationBinary(qdata.itemId,
                                                              qdata.annoName,
                                                              qdata.binaryData,
                                                              qdata.binaryDataLength,
                                                              qdata.annoMimeType,
                                                              qdata.annoFlags,
                                                              qdata.annoExpiration);
            }
          }
          if (qdata.isFolder) {
            let folderId = PlacesUtils.bookmarks.createFolder(qdata.parentFolder,
                                                              qdata.title,
                                                              qdata.index);
            if (qdata.readOnly)
              PlacesUtils.bookmarks.setFolderReadonly(folderId, true);
          }
          if (qdata.isLivemark) {
            PlacesUtils.livemarks.addLivemark({ title: qdata.title
                                              , parentId: qdata.parentFolder
                                              , index: qdata.index
                                              , feedURI: uri(qdata.feedURI)
                                              , siteURI: uri(qdata.uri)
                                              });
          }
          if (qdata.isBookmark) {
            let itemId = PlacesUtils.bookmarks.insertBookmark(qdata.parentFolder,
                                                              uri(qdata.uri),
                                                              qdata.index,
                                                              qdata.title);
            if (qdata.keyword)
              PlacesUtils.bookmarks.setKeywordForBookmark(itemId, qdata.keyword);
            if (qdata.dateAdded)
              PlacesUtils.bookmarks.setItemDateAdded(itemId, qdata.dateAdded);
            if (qdata.lastModified)
              PlacesUtils.bookmarks.setItemLastModified(itemId, qdata.lastModified);
          }
          if (qdata.isTag) {
            PlacesUtils.tagging.tagURI(uri(qdata.uri), qdata.tagArray);
          }
          if (qdata.isDynContainer) {
            PlacesUtils.bookmarks.createDynamicContainer(qdata.parentFolder,
                                                         qdata.title,
                                                         qdata.contractId,
                                                         qdata.index);
          }
          if (qdata.isSeparator)
            PlacesUtils.bookmarks.insertSeparator(qdata.parentFolder, qdata.index);
        } catch (ex) {
          LOG("Problem with this URI: " + data.uri);
          do_throw("Error creating database: " + ex + "\n");
        }
      }); 
    }
  }, null);
}
function populateDB ( aArray ) { PlacesUtils . history . runInBatchMode ( { runBatched : function ( aUserData ) { aArray . forEach ( function ( data ) { try { var qdata = new queryData ( data ) ; if ( qdata . isVisit ) { var referrer = qdata . referrer ? uri ( qdata . referrer ) : null ; var visitId = PlacesUtils . history . addVisit ( uri ( qdata . uri ) , qdata . lastVisit , referrer , qdata . transType , qdata . isRedirect , qdata . sessionID ) ; if ( qdata . title && ! qdata . isDetails ) { PlacesUtils . history . setPageTitle ( uri ( qdata . uri ) , qdata . title ) ; } if ( qdata . visitCount && ! qdata . isDetails ) { let stmt = DBConn ( ) . createStatement ( "UPDATE moz_places SET visit_count = :vc WHERE url = :url" ) ; stmt . params . vc = qdata . visitCount ; stmt . params . url = qdata . uri ; try { stmt . execute ( ) ; } catch ( ex ) { print ( "Error while setting visit_count." ) ; } finally { stmt . finalize ( ) ; } } } if ( qdata . isRedirect ) { let stmt = DBConn ( ) . createAsyncStatement ( "UPDATE moz_places SET hidden = 1 WHERE url = :url" ) ; stmt . params . url = qdata . uri ; try { stmt . executeAsync ( ) ; } catch ( ex ) { print ( "Error while setting hidden." ) ; } finally { stmt . finalize ( ) ; } } if ( qdata . isDetails ) { PlacesUtils . history . addVisit ( uri ( qdata . uri ) , qdata . lastVisit , null , TRANSITION_LINK , false , 0 ) ; PlacesUtils . ghistory2 . setPageTitle ( uri ( qdata . uri ) , qdata . title ) ; } if ( qdata . markPageAsTyped ) { PlacesUtils . bhistory . markPageAsTyped ( uri ( qdata . uri ) ) ; } if ( qdata . isPageAnnotation ) { if ( qdata . removeAnnotation ) PlacesUtils . annotations . removePageAnnotation ( uri ( qdata . uri ) , qdata . annoName ) ; else { PlacesUtils . annotations . setPageAnnotation ( uri ( qdata . uri ) , qdata . annoName , qdata . annoVal , qdata . annoFlags , qdata . annoExpiration ) ; } } if ( qdata . isItemAnnotation ) { if ( qdata . removeAnnotation ) PlacesUtils . annotations . removeItemAnnotation ( qdata . itemId , qdata . annoName ) ; else { PlacesUtils . annotations . setItemAnnotation ( qdata . itemId , qdata . annoName , qdata . annoVal , qdata . annoFlags , qdata . annoExpiration ) ; } } if ( qdata . isPageBinaryAnnotation ) { if ( qdata . removeAnnotation ) PlacesUtils . annotations . removePageAnnotation ( uri ( qdata . uri ) , qdata . annoName ) ; else { PlacesUtils . annotations . setPageAnnotationBinary ( uri ( qdata . uri ) , qdata . annoName , qdata . binarydata , qdata . binaryDataLength , qdata . annoMimeType , qdata . annoFlags , qdata . annoExpiration ) ; } } if ( qdata . isItemBinaryAnnotation ) { if ( qdata . removeAnnotation ) PlacesUtils . annotations . removeItemAnnotation ( qdata . itemId , qdata . annoName ) ; else { PlacesUtils . annotations . setItemAnnotationBinary ( qdata . itemId , qdata . annoName , qdata . binaryData , qdata . binaryDataLength , qdata . annoMimeType , qdata . annoFlags , qdata . annoExpiration ) ; } } if ( qdata . isFolder ) { let folderId = PlacesUtils . bookmarks . createFolder ( qdata . parentFolder , qdata . title , qdata . index ) ; if ( qdata . readOnly ) PlacesUtils . bookmarks . setFolderReadonly ( folderId , true ) ; } if ( qdata . isLivemark ) { PlacesUtils . livemarks . addLivemark ( { title : qdata . title , parentId : qdata . parentFolder , index : qdata . index , feedURI : uri ( qdata . feedURI ) , siteURI : uri ( qdata . uri ) } ) ; } if ( qdata . isBookmark ) { let itemId = PlacesUtils . bookmarks . insertBookmark ( qdata . parentFolder , uri ( qdata . uri ) , qdata . index , qdata . title ) ; if ( qdata . keyword ) PlacesUtils . bookmarks . setKeywordForBookmark ( itemId , qdata . keyword ) ; if ( qdata . dateAdded ) PlacesUtils . bookmarks . setItemDateAdded ( itemId , qdata . dateAdded ) ; if ( qdata . lastModified ) PlacesUtils . bookmarks . setItemLastModified ( itemId , qdata . lastModified ) ; } if ( qdata . isTag ) { PlacesUtils . tagging . tagURI ( uri ( qdata . uri ) , qdata . tagArray ) ; } if ( qdata . isDynContainer ) { PlacesUtils . bookmarks . createDynamicContainer ( qdata . parentFolder , qdata . title , qdata . contractId , qdata . index ) ; } if ( qdata . isSeparator ) PlacesUtils . bookmarks . insertSeparator ( qdata . parentFolder , qdata . index ) ; } catch ( ex ) { LOG ( "Problem with this URI: " + data . uri ) ; do_throw ( "Error creating database: " + ex + "\n" ) ; } } ) ; } } , null ) ; }
Generalized function to pull in an array of objects of data and push it into
 the database.
Generalized function to pull in an array of objects of data and push it into the database .
// make the data object into a query data object in order to create proper // default values for anything left unspecified // Then we should add a visit for this node // Set a fake visit_count, this is not a real count but can be used // to test sorting by visit_count. // Redirect sources added through the docshell are properly marked // as redirects and get hidden state, the API doesn't have that // power (And actually doesn't make much sense to add redirects // through the API). // This must be async cause otherwise the updateFrecency call // done by addVisits may randomly happen after it, overwriting the // value. // Then we add extraneous page details for testing // use the data object here in case instantiation of qdata failed // End of function and array