Save all item data values as string, and convert old integers to strings

Before 5.0 we performed a regexp on new item data values to determine if
they were integers and saved them natively in SQLite if so. We no longer
do that, but setField() used strict equality when checking for changes,
so an item could be marked as changed when comparing to a new string
value (e.g., from a write response from the API, which always returns
strings). To avoid that, this converts all old values in the DB to
strings and saves all incoming values as strings automatically. (This
should also help with searching and some other things.)
This commit is contained in:
Dan Stillman 2016-05-18 16:07:00 -04:00
parent b9bbf007f0
commit d8abfa4f67
5 changed files with 55 additions and 51 deletions

View file

@ -33,6 +33,32 @@ describe("Zotero.Item", function () {
assert.isTrue(item.hasChanged());
})
it("should save an integer as a string", function* () {
var val = 1234;
var item = new Zotero.Item('book');
item.setField('numPages', val);
yield item.saveTx();
assert.strictEqual(item.getField('numPages'), "" + val);
// Setting again as string shouldn't register a change
assert.isFalse(item.setField('numPages', "" + val));
// Value should be TEXT in the DB
var sql = "SELECT TYPEOF(value) FROM itemData JOIN itemDataValues USING (valueID) "
+ "WHERE itemID=? AND fieldID=?";
var type = yield Zotero.DB.valueQueryAsync(sql, [item.id, Zotero.ItemFields.getID('numPages')]);
assert.equal(type, 'text');
});
it("should save integer 0 as a string", function* () {
var val = 0;
var item = new Zotero.Item('book');
item.setField('numPages', val);
yield item.saveTx();
assert.strictEqual(item.getField('numPages'), "" + val);
// Setting again as string shouldn't register a change
assert.isFalse(item.setField('numPages', "" + val));
});
it('should clear an existing field when ""/null/false is passed', function* () {
var field = 'title';
var val = 'foo';
@ -69,9 +95,9 @@ describe("Zotero.Item", function () {
assert.equal(item.getField(field), "");
})
it('should clear a field set to 0 when a ""/null/false is passed', function* () {
it('should clear a field set to '0' when a ""/null/false is passed', function* () {
var field = 'title';
var val = 0;
var val = "0";
var fieldID = Zotero.ItemFields.getID(field);
var item = new Zotero.Item('book');
item.setField(field, val);