B2G/gaia/apps/communications/dialer/js/call_log_db.js
/*
   * Prepare the database. This may include opening the database and upgrading
   * it to the latest schema version.
   *
   * param callback
   *        Function that takes an error and db argument. It is called when
   *        the database is ready to use or if an error occurs while preparing
   *        the database.
   *
   * return (via callback) a database ready for use.
   */
Prepare the database. This may include opening the database and upgrading
it to the latest schema version.

param callback
Function that takes an error and db argument. It is called when
the database is ready to use or if an error occurs while preparing
the database.

return (via callback) a database ready for use.
Prepare the database . This may include opening the database and upgrading it to the latest schema version . param callback Function that takes an error and db argument . It is called when the database is ready to use or if an error occurs while preparing the database . return ( via callback ) a database ready for use .
function ensureDB(callback) {
    if (this._db) {
      callback(null, this._db);
      return;
    }
    LazyLoader.load(['/dialer/js/utils.js',
                     '/dialer/js/contacts.js'], (function() {
      try {
        var indexedDB = window.indexedDB || window.webkitIndexedDB ||
                        window.mozIndexedDB || window.msIndexedDB;
        if (!indexedDB) {
          callback('NO_INDEXED_DB_AVAILABLE', null);
          return;
        }
        var self = this;
        var request = indexedDB.open(this._dbName, this._dbVersion);
        request.onsuccess = (function onsuccess(event) {
          this._db = event.target.result;
          callback(null, this._db);
        }).bind(this);
        request.onerror = function onerror(event) {
          callback(event.target.errorCode, null);
        };
        request.onblocked = function onblocked() {
          callback('DB_REQUEST_BLOCKED', null);
        };
        request.onupgradeneeded = function onupgradeneeded(event) {
          self._notifyObservers('upgradeneeded');
          var db = event.target.result;
          var txn = event.target.transaction;
          var currentVersion = event.oldVersion;
          function update(currentVersion) {
            var next = update.bind(self, currentVersion + 1);
            switch (currentVersion) {
              case 0:
                self._createSchema(db, next);
                break;
              case 1:
                self._upgradeSchemaVersion2(db, txn, next);
                break;
              case 2:
                self._upgradeSchemaVersion3(next);
                break;
              case 3:
                self._upgradeSchemaVersion4(db, txn, next);
                break;
              case 4:
                self._upgradeSchemaVersion5(txn, next);
                break;
              case 5:
                self._notifyObservers('upgradedone');
                break;
              default:
                event.target.transaction.abort();
                break;
            }
            currentVersion++;
          }
          update(currentVersion);
        };
      } catch (ex) {
        callback(ex.message, null);
      }
    }).bind(this));
  }
function ensureDB ( callback ) { if ( this . _db ) { callback ( null , this . _db ) ; return ; } LazyLoader . load ( [ '/dialer/js/utils.js' , '/dialer/js/contacts.js' ] , ( function ( ) { try { var indexedDB = window . indexedDB || window . webkitIndexedDB || window . mozIndexedDB || window . msIndexedDB ; if ( ! indexedDB ) { callback ( 'NO_INDEXED_DB_AVAILABLE' , null ) ; return ; } var self = this ; var request = indexedDB . open ( this . _dbName , this . _dbVersion ) ; request . onsuccess = ( function onsuccess ( event ) { this . _db = event . target . result ; callback ( null , this . _db ) ; } ) . bind ( this ) ; request . onerror = function onerror ( event ) { callback ( event . target . errorCode , null ) ; } ; request . onblocked = function onblocked ( ) { callback ( 'DB_REQUEST_BLOCKED' , null ) ; } ; request . onupgradeneeded = function onupgradeneeded ( event ) { self . _notifyObservers ( 'upgradeneeded' ) ; var db = event . target . result ; var txn = event . target . transaction ; var currentVersion = event . oldVersion ; function update ( currentVersion ) { var next = update . bind ( self , currentVersion + 1 ) ; switch ( currentVersion ) { case 0 : self . _createSchema ( db , next ) ; break ; case 1 : self . _upgradeSchemaVersion2 ( db , txn , next ) ; break ; case 2 : self . _upgradeSchemaVersion3 ( next ) ; break ; case 3 : self . _upgradeSchemaVersion4 ( db , txn , next ) ; break ; case 4 : self . _upgradeSchemaVersion5 ( txn , next ) ; break ; case 5 : self . _notifyObservers ( 'upgradedone' ) ; break ; default : event . target . transaction . abort ( ) ; break ; } currentVersion ++ ; } update ( currentVersion ) ; } ; } catch ( ex ) { callback ( ex . message , null ) ; } } ) . bind ( this ) ) ; }
Prepare the database.
Prepare the database .
// Notify the UI about the need to upgrade the database. // we have finished the upgrades. please keep this // in sync for future upgrades, since otherwise it // will call the default: and abort the transaction :(