2012-02-29 02:01:48 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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";
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ['ContactDB'];
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2012-08-23 01:34:57 +04:00
|
|
|
const DEBUG = false;
|
|
|
|
function debug(s) { dump("-*- ContactDB component: " + s + "\n"); }
|
2012-02-29 02:01:48 +04:00
|
|
|
|
2012-07-02 23:03:49 +04:00
|
|
|
const Cu = Components.utils;
|
2012-02-29 02:01:48 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-04-27 02:10:04 +04:00
|
|
|
Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
|
2012-02-29 02:01:48 +04:00
|
|
|
|
|
|
|
const DB_NAME = "contacts";
|
2012-08-16 03:42:08 +04:00
|
|
|
const DB_VERSION = 4;
|
2012-02-29 02:01:48 +04:00
|
|
|
const STORE_NAME = "contacts";
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.ContactDB = function ContactDB(aGlobal) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Constructor");
|
2012-03-14 03:12:25 +04:00
|
|
|
this._global = aGlobal;
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ContactDB.prototype = {
|
2012-04-27 02:10:04 +04:00
|
|
|
__proto__: IndexedDBHelper.prototype,
|
2012-05-11 08:51:48 +04:00
|
|
|
|
|
|
|
upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade schema from: " + aOldVersion + " to " + aNewVersion + " called!");
|
2012-05-11 08:51:48 +04:00
|
|
|
let db = aDb;
|
|
|
|
let objectStore;
|
|
|
|
for (let currVersion = aOldVersion; currVersion < aNewVersion; currVersion++) {
|
|
|
|
if (currVersion == 0) {
|
|
|
|
/**
|
|
|
|
* Create the initial database schema.
|
|
|
|
*
|
|
|
|
* The schema of records stored is as follows:
|
|
|
|
*
|
|
|
|
* {id: "...", // UUID
|
|
|
|
* published: Date(...), // First published date.
|
|
|
|
* updated: Date(...), // Last updated date.
|
|
|
|
* properties: {...} // Object holding the ContactProperties
|
|
|
|
* }
|
|
|
|
*/
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("create schema");
|
2012-05-11 08:51:48 +04:00
|
|
|
objectStore = db.createObjectStore(this.dbStoreName, {keyPath: "id"});
|
|
|
|
|
|
|
|
// Metadata indexes
|
|
|
|
objectStore.createIndex("published", "published", { unique: false });
|
|
|
|
objectStore.createIndex("updated", "updated", { unique: false });
|
|
|
|
|
|
|
|
// Properties indexes
|
|
|
|
objectStore.createIndex("nickname", "properties.nickname", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("name", "properties.name", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("familyName", "properties.familyName", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("givenName", "properties.givenName", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("tel", "properties.tel", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("email", "properties.email", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("note", "properties.note", { unique: false, multiEntry: true });
|
|
|
|
|
|
|
|
objectStore.createIndex("nicknameLowerCase", "search.nickname", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("nameLowerCase", "search.name", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("familyNameLowerCase", "search.familyName", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("givenNameLowerCase", "search.givenName", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("telLowerCase", "search.tel", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("emailLowerCase", "search.email", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("noteLowerCase", "search.note", { unique: false, multiEntry: true });
|
|
|
|
} else if (currVersion == 1) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade 1");
|
2012-05-11 08:51:48 +04:00
|
|
|
|
2012-07-02 23:03:49 +04:00
|
|
|
// Create a new scheme for the tel field. We move from an array of tel-numbers to an array of
|
2012-05-11 08:51:48 +04:00
|
|
|
// ContactTelephone.
|
|
|
|
if (!objectStore) {
|
|
|
|
objectStore = aTransaction.objectStore(STORE_NAME);
|
|
|
|
}
|
|
|
|
// Delete old tel index.
|
|
|
|
objectStore.deleteIndex("tel");
|
|
|
|
|
|
|
|
// Upgrade existing tel field in the DB.
|
2012-07-02 23:03:49 +04:00
|
|
|
objectStore.openCursor().onsuccess = function(event) {
|
2012-05-11 08:51:48 +04:00
|
|
|
let cursor = event.target.result;
|
|
|
|
if (cursor) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade tel1: " + JSON.stringify(cursor.value));
|
2012-05-11 08:51:48 +04:00
|
|
|
for (let number in cursor.value.properties.tel) {
|
|
|
|
cursor.value.properties.tel[number] = {number: number};
|
|
|
|
}
|
|
|
|
cursor.update(cursor.value);
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade tel2: " + JSON.stringify(cursor.value));
|
2012-05-11 08:51:48 +04:00
|
|
|
cursor.continue();
|
2012-07-02 23:03:49 +04:00
|
|
|
}
|
2012-05-11 08:51:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create new searchable indexes.
|
|
|
|
objectStore.createIndex("tel", "search.tel", { unique: false, multiEntry: true });
|
|
|
|
objectStore.createIndex("category", "properties.category", { unique: false, multiEntry: true });
|
2012-07-03 22:00:53 +04:00
|
|
|
} else if (currVersion == 2) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade 2");
|
2012-07-03 22:00:53 +04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
// Delete old email index.
|
|
|
|
objectStore.deleteIndex("email");
|
|
|
|
|
|
|
|
// Upgrade existing email field in the DB.
|
|
|
|
objectStore.openCursor().onsuccess = function(event) {
|
|
|
|
let cursor = event.target.result;
|
2012-08-31 18:59:42 +04:00
|
|
|
if (cursor && cursor.value.properties.email) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade email1: " + JSON.stringify(cursor.value));
|
2012-07-03 22:00:53 +04:00
|
|
|
cursor.value.properties.email =
|
|
|
|
cursor.value.properties.email.map(function(address) { return { address: address }; });
|
|
|
|
cursor.update(cursor.value);
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade email2: " + JSON.stringify(cursor.value));
|
2012-07-03 22:00:53 +04:00
|
|
|
cursor.continue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create new searchable indexes.
|
|
|
|
objectStore.createIndex("email", "search.email", { unique: false, multiEntry: true });
|
2012-08-16 03:42:08 +04:00
|
|
|
} else if (currVersion == 3) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade 3");
|
2012-08-16 04:49:47 +04:00
|
|
|
|
2012-08-16 03:42:08 +04:00
|
|
|
if (!objectStore) {
|
|
|
|
objectStore = aTransaction.objectStore(STORE_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade existing impp field in the DB.
|
|
|
|
objectStore.openCursor().onsuccess = function(event) {
|
|
|
|
let cursor = event.target.result;
|
2012-08-31 18:59:42 +04:00
|
|
|
if (cursor && cursor.value.properties.impp) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade impp1: " + JSON.stringify(cursor.value));
|
2012-08-16 03:42:08 +04:00
|
|
|
cursor.value.properties.impp =
|
|
|
|
cursor.value.properties.impp.map(function(value) { return { value: value }; });
|
|
|
|
cursor.update(cursor.value);
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
|
2012-08-16 03:42:08 +04:00
|
|
|
cursor.continue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Upgrade existing url field in the DB.
|
|
|
|
objectStore.openCursor().onsuccess = function(event) {
|
|
|
|
let cursor = event.target.result;
|
2012-08-31 18:59:42 +04:00
|
|
|
if (cursor && cursor.value.properties.url) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade url1: " + JSON.stringify(cursor.value));
|
2012-08-16 03:42:08 +04:00
|
|
|
cursor.value.properties.url =
|
|
|
|
cursor.value.properties.url.map(function(value) { return { value: value }; });
|
|
|
|
cursor.update(cursor.value);
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
|
2012-08-16 03:42:08 +04:00
|
|
|
cursor.continue();
|
|
|
|
}
|
|
|
|
};
|
2012-05-11 08:51:48 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-29 02:01:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
makeImport: function makeImport(aContact) {
|
|
|
|
let contact = {};
|
|
|
|
contact.properties = {
|
|
|
|
name: [],
|
|
|
|
honorificPrefix: [],
|
|
|
|
givenName: [],
|
|
|
|
additionalName: [],
|
|
|
|
familyName: [],
|
|
|
|
honorificSuffix: [],
|
|
|
|
nickname: [],
|
|
|
|
email: [],
|
|
|
|
photo: [],
|
|
|
|
url: [],
|
|
|
|
category: [],
|
|
|
|
adr: [],
|
|
|
|
tel: [],
|
|
|
|
org: [],
|
2012-05-11 08:51:48 +04:00
|
|
|
jobTitle: [],
|
2012-02-29 02:01:48 +04:00
|
|
|
bday: null,
|
|
|
|
note: [],
|
|
|
|
impp: [],
|
|
|
|
anniversary: null,
|
|
|
|
sex: null,
|
|
|
|
genderIdentity: null
|
|
|
|
};
|
|
|
|
|
2012-03-14 03:12:25 +04:00
|
|
|
contact.search = {
|
|
|
|
name: [],
|
|
|
|
honorificPrefix: [],
|
|
|
|
givenName: [],
|
|
|
|
additionalName: [],
|
|
|
|
familyName: [],
|
|
|
|
honorificSuffix: [],
|
|
|
|
nickname: [],
|
|
|
|
email: [],
|
|
|
|
category: [],
|
|
|
|
tel: [],
|
|
|
|
org: [],
|
2012-05-11 08:51:48 +04:00
|
|
|
jobTitle: [],
|
2012-03-14 03:12:25 +04:00
|
|
|
note: [],
|
|
|
|
impp: []
|
|
|
|
};
|
|
|
|
|
2012-02-29 02:01:48 +04:00
|
|
|
for (let field in aContact.properties) {
|
|
|
|
contact.properties[field] = aContact.properties[field];
|
2012-03-14 03:12:25 +04:00
|
|
|
// Add search fields
|
|
|
|
if (aContact.properties[field] && contact.search[field]) {
|
|
|
|
for (let i = 0; i <= aContact.properties[field].length; i++) {
|
2012-03-21 22:45:03 +04:00
|
|
|
if (aContact.properties[field][i]) {
|
|
|
|
if (field == "tel") {
|
2012-07-02 23:03:49 +04:00
|
|
|
// Special case telephone number.
|
2012-03-21 22:45:03 +04:00
|
|
|
// "+1-234-567" should also be found with 1234, 234-56, 23456
|
|
|
|
|
|
|
|
// Chop off the first characters
|
2012-08-16 03:42:08 +04:00
|
|
|
let number = aContact.properties[field][i].value;
|
|
|
|
if (number) {
|
2012-08-16 04:49:47 +04:00
|
|
|
for (let i = 0; i < number.length; i++) {
|
2012-08-16 03:42:08 +04:00
|
|
|
contact.search[field].push(number.substring(i, number.length));
|
|
|
|
}
|
|
|
|
// Store +1-234-567 as ["1234567", "234567"...]
|
|
|
|
let digits = number.match(/\d/g);
|
|
|
|
if (digits && number.length != digits.length) {
|
|
|
|
digits = digits.join('');
|
|
|
|
for(let i = 0; i < digits.length; i++) {
|
|
|
|
contact.search[field].push(digits.substring(i, digits.length));
|
|
|
|
}
|
2012-03-21 22:45:03 +04:00
|
|
|
}
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("lookup: " + JSON.stringify(contact.search[field]));
|
2012-08-16 03:42:08 +04:00
|
|
|
}
|
2012-07-03 22:00:53 +04:00
|
|
|
} else if (field == "email") {
|
2012-08-16 03:42:08 +04:00
|
|
|
let address = aContact.properties[field][i].value;
|
2012-07-03 22:00:53 +04:00
|
|
|
if (address && typeof address == "string") {
|
|
|
|
contact.search[field].push(address.toLowerCase());
|
|
|
|
}
|
2012-08-16 03:42:08 +04:00
|
|
|
} else if (field == "impp") {
|
|
|
|
let value = aContact.properties[field][i].value;
|
|
|
|
if (value && typeof value == "string") {
|
|
|
|
contact.search[field].push(value.toLowerCase());
|
|
|
|
}
|
2012-03-21 22:45:03 +04:00
|
|
|
} else {
|
2012-07-03 22:00:53 +04:00
|
|
|
let val = aContact.properties[field][i];
|
|
|
|
if (typeof val == "string") {
|
|
|
|
contact.search[field].push(val.toLowerCase());
|
|
|
|
}
|
2012-03-21 22:45:03 +04:00
|
|
|
}
|
|
|
|
}
|
2012-03-14 03:12:25 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("contact:" + JSON.stringify(contact));
|
2012-02-29 02:01:48 +04:00
|
|
|
|
|
|
|
contact.updated = aContact.updated;
|
|
|
|
contact.published = aContact.published;
|
|
|
|
contact.id = aContact.id;
|
|
|
|
|
|
|
|
return contact;
|
|
|
|
},
|
|
|
|
|
|
|
|
makeExport: function makeExport(aRecord) {
|
|
|
|
let contact = {};
|
|
|
|
contact.properties = aRecord.properties;
|
|
|
|
|
|
|
|
for (let field in aRecord.properties)
|
|
|
|
contact.properties[field] = aRecord.properties[field];
|
|
|
|
|
|
|
|
contact.updated = aRecord.updated;
|
|
|
|
contact.published = aRecord.published;
|
|
|
|
contact.id = aRecord.id;
|
|
|
|
return contact;
|
|
|
|
},
|
|
|
|
|
|
|
|
updateRecordMetadata: function updateRecordMetadata(record) {
|
|
|
|
if (!record.id) {
|
|
|
|
Cu.reportError("Contact without ID");
|
|
|
|
}
|
|
|
|
if (!record.published) {
|
|
|
|
record.published = new Date();
|
|
|
|
}
|
|
|
|
record.updated = new Date();
|
|
|
|
},
|
|
|
|
|
|
|
|
saveContact: function saveContact(aContact, successCb, errorCb) {
|
|
|
|
let contact = this.makeImport(aContact);
|
2012-03-14 00:25:05 +04:00
|
|
|
this.newTxn("readwrite", function (txn, store) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Going to update" + JSON.stringify(contact));
|
2012-02-29 02:01:48 +04:00
|
|
|
|
|
|
|
// Look up the existing record and compare the update timestamp.
|
|
|
|
// If no record exists, just add the new entry.
|
|
|
|
let newRequest = store.get(contact.id);
|
|
|
|
newRequest.onsuccess = function (event) {
|
|
|
|
if (!event.target.result) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("new record!")
|
2012-02-29 02:01:48 +04:00
|
|
|
this.updateRecordMetadata(contact);
|
|
|
|
store.put(contact);
|
|
|
|
} else {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("old record!")
|
2012-02-29 02:01:48 +04:00
|
|
|
if (new Date(typeof contact.updated === "undefined" ? 0 : contact.updated) < new Date(event.target.result.updated)) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("rev check fail!");
|
2012-02-29 02:01:48 +04:00
|
|
|
txn.abort();
|
|
|
|
return;
|
|
|
|
} else {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("rev check OK");
|
2012-02-29 02:01:48 +04:00
|
|
|
contact.published = event.target.result.published;
|
|
|
|
contact.updated = new Date();
|
|
|
|
store.put(contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
}.bind(this), successCb, errorCb);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeContact: function removeContact(aId, aSuccessCb, aErrorCb) {
|
2012-03-14 00:25:05 +04:00
|
|
|
this.newTxn("readwrite", function (txn, store) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Going to delete" + aId);
|
2012-02-29 02:01:48 +04:00
|
|
|
store.delete(aId);
|
|
|
|
}, aSuccessCb, aErrorCb);
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function clear(aSuccessCb, aErrorCb) {
|
2012-03-14 00:25:05 +04:00
|
|
|
this.newTxn("readwrite", function (txn, store) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Going to clear all!");
|
2012-02-29 02:01:48 +04:00
|
|
|
store.clear();
|
|
|
|
}, aSuccessCb, aErrorCb);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param successCb
|
|
|
|
* Callback function to invoke with result array.
|
|
|
|
* @param failureCb [optional]
|
|
|
|
* Callback function to invoke when there was an error.
|
|
|
|
* @param options [optional]
|
|
|
|
* Object specifying search options. Possible attributes:
|
|
|
|
* - filterBy
|
|
|
|
* - filterOp
|
|
|
|
* - filterValue
|
|
|
|
* - count
|
|
|
|
*/
|
|
|
|
find: function find(aSuccessCb, aFailureCb, aOptions) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("ContactDB:find val:" + aOptions.filterValue + " by: " + aOptions.filterBy + " op: " + aOptions.filterOp + "\n");
|
2012-02-29 02:01:48 +04:00
|
|
|
let self = this;
|
2012-03-14 00:25:05 +04:00
|
|
|
this.newTxn("readonly", function (txn, store) {
|
2012-03-14 03:12:25 +04:00
|
|
|
if (aOptions && (aOptions.filterOp == "equals" || aOptions.filterOp == "contains")) {
|
2012-02-29 02:01:48 +04:00
|
|
|
self._findWithIndex(txn, store, aOptions);
|
|
|
|
} else {
|
|
|
|
self._findAll(txn, store, aOptions);
|
|
|
|
}
|
|
|
|
}, aSuccessCb, aFailureCb);
|
|
|
|
},
|
|
|
|
|
|
|
|
_findWithIndex: function _findWithIndex(txn, store, options) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("_findWithIndex: " + options.filterValue +" " + options.filterOp + " " + options.filterBy + " ");
|
2012-02-29 02:01:48 +04:00
|
|
|
let fields = options.filterBy;
|
|
|
|
for (let key in fields) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("key: " + fields[key]);
|
2012-02-29 02:01:48 +04:00
|
|
|
if (!store.indexNames.contains(fields[key]) && !fields[key] == "id") {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Key not valid!" + fields[key] + ", " + store.indexNames);
|
2012-02-29 02:01:48 +04:00
|
|
|
txn.abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup for all keys
|
|
|
|
if (options.filterBy.length == 0) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("search in all fields!" + JSON.stringify(store.indexNames));
|
2012-02-29 02:01:48 +04:00
|
|
|
for(let myIndex = 0; myIndex < store.indexNames.length; myIndex++) {
|
|
|
|
fields = Array.concat(fields, store.indexNames[myIndex])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:39:57 +04:00
|
|
|
// Sorting functions takes care of limit if set.
|
|
|
|
let limit = options.sortBy === 'undefined' ? options.filterLimit : null;
|
|
|
|
|
2012-02-29 02:01:48 +04:00
|
|
|
let filter_keys = fields.slice();
|
|
|
|
for (let key = filter_keys.shift(); key; key = filter_keys.shift()) {
|
|
|
|
let request;
|
|
|
|
if (key == "id") {
|
|
|
|
// store.get would return an object and not an array
|
2012-07-02 23:03:49 +04:00
|
|
|
request = store.mozGetAll(options.filterValue);
|
2012-05-11 08:51:48 +04:00
|
|
|
} else if (key == "category") {
|
|
|
|
let index = store.index(key);
|
2012-07-02 23:03:49 +04:00
|
|
|
request = index.mozGetAll(options.filterValue, limit);
|
2012-03-14 03:12:25 +04:00
|
|
|
} else if (options.filterOp == "equals") {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Getting index: " + key);
|
2012-03-14 03:12:25 +04:00
|
|
|
// case sensitive
|
2012-02-29 02:01:48 +04:00
|
|
|
let index = store.index(key);
|
2012-07-02 23:03:49 +04:00
|
|
|
request = index.mozGetAll(options.filterValue, limit);
|
2012-03-14 03:12:25 +04:00
|
|
|
} else {
|
|
|
|
// not case sensitive
|
2012-07-03 22:00:53 +04:00
|
|
|
let tmp = typeof options.filterValue == "string"
|
|
|
|
? options.filterValue.toLowerCase()
|
|
|
|
: options.filterValue.toString().toLowerCase();
|
2012-03-14 03:12:25 +04:00
|
|
|
let range = this._global.IDBKeyRange.bound(tmp, tmp + "\uFFFF");
|
|
|
|
let index = store.index(key + "LowerCase");
|
2012-07-02 23:03:49 +04:00
|
|
|
request = index.mozGetAll(range, limit);
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
|
|
|
if (!txn.result)
|
|
|
|
txn.result = {};
|
|
|
|
|
|
|
|
request.onsuccess = function (event) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Request successful. Record count:" + event.target.result.length);
|
2012-02-29 02:01:48 +04:00
|
|
|
for (let i in event.target.result)
|
|
|
|
txn.result[event.target.result[i].id] = this.makeExport(event.target.result[i]);
|
|
|
|
}.bind(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_findAll: function _findAll(txn, store, options) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("ContactDB:_findAll: " + JSON.stringify(options));
|
2012-02-29 02:01:48 +04:00
|
|
|
if (!txn.result)
|
|
|
|
txn.result = {};
|
2012-04-03 03:39:57 +04:00
|
|
|
// Sorting functions takes care of limit if set.
|
|
|
|
let limit = options.sortBy === 'undefined' ? options.filterLimit : null;
|
2012-07-02 23:03:49 +04:00
|
|
|
store.mozGetAll(null, limit).onsuccess = function (event) {
|
2012-08-23 01:34:57 +04:00
|
|
|
if (DEBUG) debug("Request successful. Record count:", event.target.result.length);
|
2012-02-29 02:01:48 +04:00
|
|
|
for (let i in event.target.result)
|
|
|
|
txn.result[event.target.result[i].id] = this.makeExport(event.target.result[i]);
|
|
|
|
}.bind(this);
|
2012-04-27 02:10:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
init: function init(aGlobal) {
|
|
|
|
this.initDBHelper(DB_NAME, DB_VERSION, STORE_NAME, aGlobal);
|
2012-02-29 02:01:48 +04:00
|
|
|
}
|
|
|
|
};
|