Bug 836513 - Support multiple object stores in IndexedDBHelper.jsm. r=gwagner, a=leo+

This commit is contained in:
Reuben Morais 2013-02-28 15:58:28 -05:00
Родитель 99a764d232
Коммит 1169b9d876
6 изменённых файлов: 49 добавлений и 40 удалений

Просмотреть файл

@ -40,7 +40,7 @@ ActivitiesDb.prototype = {
let idbManager = Cc["@mozilla.org/dom/indexeddb/manager;1"]
.getService(Ci.nsIIndexedDatabaseManager);
idbManager.initWindowless(idbGlobal);
this.initDBHelper(DB_NAME, DB_VERSION, STORE_NAME, idbGlobal);
this.initDBHelper(DB_NAME, DB_VERSION, [STORE_NAME], idbGlobal);
},
/**
@ -88,7 +88,7 @@ ActivitiesDb.prototype = {
// Add all the activities carried in the |aObjects| array.
add: function actdb_add(aObjects, aSuccess, aError) {
this.newTxn("readwrite", function (txn, store) {
this.newTxn("readwrite", STORE_NAME, function (txn, store) {
aObjects.forEach(function (aObject) {
let object = {
manifest: aObject.manifest,
@ -105,7 +105,7 @@ ActivitiesDb.prototype = {
// Remove all the activities carried in the |aObjects| array.
remove: function actdb_remove(aObjects) {
this.newTxn("readwrite", function (txn, store) {
this.newTxn("readwrite", STORE_NAME, function (txn, store) {
aObjects.forEach(function (aObject) {
let object = {
manifest: aObject.manifest,
@ -120,7 +120,7 @@ ActivitiesDb.prototype = {
find: function actdb_find(aObject, aSuccess, aError, aMatch) {
debug("Looking for " + aObject.options.name);
this.newTxn("readonly", function (txn, store) {
this.newTxn("readonly", STORE_NAME, function (txn, store) {
let index = store.index("name");
let request = index.mozGetAll(aObject.options.name);
request.onsuccess = function findSuccess(aEvent) {

Просмотреть файл

@ -34,7 +34,7 @@ AlarmDB.prototype = {
init: function init(aGlobal) {
debug("init()");
this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, ALARMSTORE_NAME, aGlobal);
this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, [ALARMSTORE_NAME], aGlobal);
},
upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
@ -64,7 +64,8 @@ AlarmDB.prototype = {
debug("add()");
this.newTxn(
"readwrite",
"readwrite",
ALARMSTORE_NAME,
function txnCb(aTxn, aStore) {
debug("Going to add " + JSON.stringify(aAlarm));
aStore.put(aAlarm).onsuccess = function setTxnResult(aEvent) {
@ -72,7 +73,7 @@ AlarmDB.prototype = {
debug("Request successful. New record ID: " + aTxn.result);
};
},
aSuccessCb,
aSuccessCb,
aErrorCb
);
},
@ -93,7 +94,8 @@ AlarmDB.prototype = {
debug("remove()");
this.newTxn(
"readwrite",
"readwrite",
ALARMSTORE_NAME,
function txnCb(aTxn, aStore) {
debug("Going to remove " + aId);
@ -114,8 +116,8 @@ AlarmDB.prototype = {
aStore.delete(aId);
};
},
aSuccessCb,
},
aSuccessCb,
aErrorCb
);
},
@ -134,7 +136,8 @@ AlarmDB.prototype = {
debug("getAll()");
this.newTxn(
"readonly",
"readonly",
ALARMSTORE_NAME,
function txnCb(aTxn, aStore) {
if (!aTxn.result)
aTxn.result = [];
@ -147,8 +150,8 @@ AlarmDB.prototype = {
debug("Request successful. Record count: " + aTxn.result.length);
};
},
aSuccessCb,
},
aSuccessCb,
aErrorCb
);
}

Просмотреть файл

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict"
"use strict";
let DEBUG = 0;
let debug;
@ -12,7 +12,7 @@ if (DEBUG) {
debug = function (s) {}
}
const Cu = Components.utils;
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -24,7 +24,7 @@ Cu.import("resource://gre/modules/Services.jsm");
this.IndexedDBHelper = function IndexedDBHelper() {}
IndexedDBHelper.prototype = {
// Cache the database
_db: null,
@ -38,7 +38,7 @@ IndexedDBHelper.prototype = {
/**
* Open a new database.
* User has to provide upgradeSchema.
*
*
* @param successCb
* Success callback to call once database is open.
* @param failureCb
@ -49,7 +49,7 @@ IndexedDBHelper.prototype = {
if (DEBUG) debug("Try to open database:" + self.dbName + " " + self.dbVersion);
let req = this.dbGlobal.indexedDB.open(this.dbName, this.dbVersion);
req.onsuccess = function (event) {
if (DEBUG) debug("Opened database:" + self.dbName + " " + self.dbName);
if (DEBUG) debug("Opened database:" + self.dbName + " " + self.dbVersion);
self._db = event.target.result;
self._db.onversionchange = function(event) {
if (DEBUG) debug("WARNING: DB modified from a different window.");
@ -60,7 +60,7 @@ IndexedDBHelper.prototype = {
req.onupgradeneeded = function (aEvent) {
if (DEBUG) {
debug("Database needs upgrade:" + self.dbName + aEvent.oldVersion + aEvent.newVersion);
debug("Correct new database version:" + aEvent.newVersion == this.dbVersion);
debug("Correct new database version:" + (aEvent.newVersion == this.dbVersion));
}
let _db = aEvent.target.result;
@ -77,7 +77,7 @@ IndexedDBHelper.prototype = {
/**
* Use the cached DB or open a new one.
*
*
* @param successCb
* Success callback to call.
* @param failureCb
@ -94,35 +94,41 @@ IndexedDBHelper.prototype = {
/**
* Start a new transaction.
*
*
* @param txn_type
* Type of transaction (e.g. "readwrite")
* @param store_name
* The object store you want to be passed to the callback
* @param callback
* Function to call when the transaction is available. It will
* be invoked with the transaction and the 'aDBStoreName' object store.
* be invoked with the transaction and the `store_name' object store.
* @param successCb
* Success callback to call on a successful transaction commit.
* The result is stored in txn.result.
* @param failureCb
* Error callback to call when an error is encountered.
*/
newTxn: function newTxn(txn_type, callback, successCb, failureCb) {
newTxn: function newTxn(txn_type, store_name, callback, successCb, failureCb) {
this.ensureDB(function () {
if (DEBUG) debug("Starting new transaction" + txn_type);
let txn = this._db.transaction(this.dbName, txn_type);
let txn = this._db.transaction(this.dbStoreNames, txn_type);
if (DEBUG) debug("Retrieving object store", this.dbName);
let store = txn.objectStore(this.dbStoreName);
let store = txn.objectStore(store_name);
txn.oncomplete = function (event) {
if (DEBUG) debug("Transaction complete. Returning to callback.");
successCb(txn.result);
if (successCb) {
successCb(txn.result);
}
};
txn.onabort = function (event) {
if (DEBUG) debug("Caught error on transaction");
// FIXXMEE: this will work in the future. Bug 748630
// failureCb(event.target.error.name);
failureCb("UnknownError");
if (failureCb) {
failureCb("UnknownError");
}
};
callback(txn, store);
}.bind(this), failureCb);
@ -130,7 +136,7 @@ IndexedDBHelper.prototype = {
/**
* Initialize the DB. Does not call open.
*
*
* @param aDBName
* DB name for the open call.
* @param aDBVersion
@ -140,10 +146,10 @@ IndexedDBHelper.prototype = {
* @param aGlobal
* Global object that has indexedDB property.
*/
initDBHelper: function initDBHelper(aDBName, aDBVersion, aDBStoreName, aGlobal) {
initDBHelper: function initDBHelper(aDBName, aDBVersion, aDBStoreNames, aGlobal) {
this.dbName = aDBName;
this.dbVersion = aDBVersion;
this.dbStoreName = aDBStoreName;
this.dbStoreNames = aDBStoreNames;
this.dbGlobal = aGlobal;
}
}

Просмотреть файл

@ -47,7 +47,7 @@ ContactDB.prototype = {
* }
*/
if (DEBUG) debug("create schema");
objectStore = db.createObjectStore(this.dbStoreName, {keyPath: "id"});
objectStore = db.createObjectStore(STORE_NAME, {keyPath: "id"});
// Properties indexes
objectStore.createIndex("familyName", "properties.familyName", { multiEntry: true });
@ -89,7 +89,7 @@ ContactDB.prototype = {
objectStore.createIndex("category", "properties.category", { multiEntry: true });
} else if (currVersion == 2) {
if (DEBUG) debug("upgrade 2");
// Create a new scheme for the email field. We move from an array of emailaddresses to an array of
// Create a new scheme for the email field. We move from an array of emailaddresses to an array of
// ContactEmail.
if (!objectStore) {
objectStore = aTransaction.objectStore(STORE_NAME);
@ -384,7 +384,7 @@ ContactDB.prototype = {
saveContact: function saveContact(aContact, successCb, errorCb) {
let contact = this.makeImport(aContact);
this.newTxn("readwrite", function (txn, store) {
this.newTxn("readwrite", STORE_NAME, function (txn, store) {
if (DEBUG) debug("Going to update" + JSON.stringify(contact));
// Look up the existing record and compare the update timestamp.
@ -413,14 +413,14 @@ ContactDB.prototype = {
},
removeContact: function removeContact(aId, aSuccessCb, aErrorCb) {
this.newTxn("readwrite", function (txn, store) {
this.newTxn("readwrite", STORE_NAME, function (txn, store) {
if (DEBUG) debug("Going to delete" + aId);
store.delete(aId);
}, aSuccessCb, aErrorCb);
},
clear: function clear(aSuccessCb, aErrorCb) {
this.newTxn("readwrite", function (txn, store) {
this.newTxn("readwrite", STORE_NAME, function (txn, store) {
if (DEBUG) debug("Going to clear all!");
store.clear();
}, aSuccessCb, aErrorCb);
@ -441,7 +441,7 @@ ContactDB.prototype = {
find: function find(aSuccessCb, aFailureCb, aOptions) {
if (DEBUG) debug("ContactDB:find val:" + aOptions.filterValue + " by: " + aOptions.filterBy + " op: " + aOptions.filterOp + "\n");
let self = this;
this.newTxn("readonly", function (txn, store) {
this.newTxn("readonly", STORE_NAME, function (txn, store) {
if (aOptions && (aOptions.filterOp == "equals" || aOptions.filterOp == "contains")) {
self._findWithIndex(txn, store, aOptions);
} else {
@ -527,6 +527,6 @@ ContactDB.prototype = {
},
init: function init(aGlobal) {
this.initDBHelper(DB_NAME, DB_VERSION, STORE_NAME, aGlobal);
this.initDBHelper(DB_NAME, DB_VERSION, [STORE_NAME], aGlobal);
}
};

Просмотреть файл

@ -29,7 +29,7 @@ this.NetworkStatsDB = function NetworkStatsDB(aGlobal) {
if (DEBUG) {
debug("Constructor");
}
this.initDBHelper(DB_NAME, DB_VERSION, STORE_NAME, aGlobal);
this.initDBHelper(DB_NAME, DB_VERSION, [STORE_NAME], aGlobal);
}
NetworkStatsDB.prototype = {
@ -42,7 +42,7 @@ NetworkStatsDB.prototype = {
function errorCb(error) {
txnCb(error, null);
}
return this.newTxn(txn_type, callback, successCb, errorCb);
return this.newTxn(txn_type, STORE_NAME, callback, successCb, errorCb);
},
upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {

Просмотреть файл

@ -109,6 +109,6 @@ SettingsDB.prototype = {
init: function init(aGlobal) {
this.initDBHelper(SETTINGSDB_NAME, SETTINGSDB_VERSION,
SETTINGSSTORE_NAME, aGlobal);
[SETTINGSSTORE_NAME], aGlobal);
}
}