Bug 827321 - Fixing ContactsDB tel search indexes

This commit is contained in:
Etienne Segonzac 2013-01-09 13:45:48 +01:00
Родитель 5bdf23a61f
Коммит 61718d7e7a
2 изменённых файлов: 101 добавлений и 4 удалений

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

@ -18,7 +18,7 @@ Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
const DB_NAME = "contacts";
const DB_VERSION = 5;
const DB_VERSION = 6;
const STORE_NAME = "contacts";
this.ContactDB = function ContactDB(aGlobal) {
@ -58,7 +58,6 @@ ContactDB.prototype = {
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 });
@ -78,7 +77,9 @@ ContactDB.prototype = {
objectStore = aTransaction.objectStore(STORE_NAME);
}
// Delete old tel index.
if (objectStore.indexNames.contains("tel")) {
objectStore.deleteIndex("tel");
}
// Upgrade existing tel field in the DB.
objectStore.openCursor().onsuccess = function(event) {
@ -192,6 +193,44 @@ ContactDB.prototype = {
cursor.continue();
}
};
} else if (currVersion == 5) {
if (DEBUG) debug("Add index for equals tel searches");
if (!objectStore) {
objectStore = aTransaction.objectStore(STORE_NAME);
}
// Delete old tel index (not on the right field).
if (objectStore.indexNames.contains("tel")) {
objectStore.deleteIndex("tel");
}
// Create new index for "equals" searches
objectStore.createIndex("tel", "search.exactTel", { unique: false, multiEntry: true });
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
if (cursor.value.properties.tel) {
if (DEBUG) debug("upgrade : " + JSON.stringify(cursor.value));
cursor.value.properties.tel.forEach(
function(duple) {
let number = duple.value.toString();
let parsedNumber = PhoneNumberUtils.parse(number);
cursor.value.search.exactTel = [number];
if (parsedNumber &&
parsedNumber.internationalNumber &&
number !== parsedNumber.internationalNumber) {
cursor.value.search.exactTel.push(parsedNumber.internationalNumber);
}
}
)
cursor.update(cursor.value);
}
if (DEBUG) debug("upgrade : " + JSON.stringify(cursor.value));
cursor.continue();
}
};
}
}
},
@ -233,6 +272,7 @@ ContactDB.prototype = {
email: [],
category: [],
tel: [],
exactTel: [],
org: [],
jobTitle: [],
note: [],
@ -251,6 +291,7 @@ ContactDB.prototype = {
// Chop off the first characters
let number = aContact.properties[field][i].value;
contact.search.exactTel.push(number);
let search = {};
if (number) {
for (let i = 0; i < number.length; i++) {
@ -273,6 +314,7 @@ ContactDB.prototype = {
debug("NationalFormat: " + parsedNumber.nationalFormat);
if (parsedNumber.internationalNumber &&
number.toString() !== parsedNumber.internationalNumber) {
contact.search.exactTel.push(parsedNumber.internationalNumber);
let digits = parsedNumber.internationalNumber.match(/\d/g);
if (digits) {
digits = digits.join('');

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

@ -52,6 +52,12 @@ var properties1 = {
tel: [{type: ["work"], value: number1.local, carrier: "testCarrier"} , {type: ["home", "fax"], value: number2.local}],
};
var shortNumber = "888";
var properties2 = {
name: "Testname2",
tel: [{type: ["work"], value: shortNumber, carrier: "testCarrier"}]
};
var req;
var index = 0;
var createResult1;
@ -82,6 +88,17 @@ var steps = [
};
req.onerror = onFailure;
},
function () {
ok(true, "Adding a new contact2");
var createResult2 = new mozContact();
createResult2.init(properties2);
req = navigator.mozContacts.save(createResult2);
req.onsuccess = function () {
ok(createResult2.id, "The contact now has an ID.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
@ -110,6 +127,44 @@ var steps = [
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for a short number matching the prefix");
var shortNumber = number1.local.substring(0, 3);
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: shortNumber};
req = mozContacts.find(options);
req.onsuccess = function() {
ok(req.result.length == 0, "The prefix short number should not match any contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for a short number matching the suffix");
var shortNumber = number1.local.substring(number1.local.length - 3);
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: shortNumber};
req = mozContacts.find(options);
req.onsuccess = function() {
ok(req.result.length == 0, "The suffix short number should not match any contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for a short number matching a contact");
var options = {filterBy: ["tel"],
filterOp: "equals",
filterValue: shortNumber};
req = mozContacts.find(options);
req.onsuccess = function() {
ok(req.result.length == 1, "Found the contact equally matching the shortNumber.");
next();
};
req.onerror = onFailure;
},
function() {
ok(true, "Modifying number");
findResult1.tel[0].value = number2.local;