Bug 836513 - Support multiple object stores in IndexedDBHelper.jsm. r=gwagner

This commit is contained in:
Reuben Morais 2013-01-30 18:03:46 -08:00
Родитель 53d4ff720b
Коммит 24db28157f
6 изменённых файлов: 40 добавлений и 35 удалений

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

@ -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
);
}

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

@ -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
@ -77,7 +77,7 @@ IndexedDBHelper.prototype = {
/**
* Use the cached DB or open a new one.
*
*
* @param successCb
* Success callback to call.
* @param failureCb
@ -94,24 +94,26 @@ 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' 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.");
@ -135,7 +137,7 @@ IndexedDBHelper.prototype = {
/**
* Initialize the DB. Does not call open.
*
*
* @param aDBName
* DB name for the open call.
* @param aDBVersion
@ -145,10 +147,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);
}
}