www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit aa512f0f8db6d968476cef2a8398bca22923e319
parent 53e40bb0f4d934e9bd7e8daef9c4e9f35fa3adc5
Author: Dan Stillman <dstillman@zotero.org>
Date:   Mon,  4 May 2015 03:19:58 -0400

Miscellaneous data layer fixes and tweaks

Diffstat:
Mchrome/content/zotero/xpcom/data/collection.js | 13+++++++++----
Mchrome/content/zotero/xpcom/data/creators.js | 2+-
Mchrome/content/zotero/xpcom/data/item.js | 16+++++++++-------
Mchrome/content/zotero/xpcom/search.js | 76++++++++++++++++++++++++++++------------------------------------------------
4 files changed, 47 insertions(+), 60 deletions(-)

diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js @@ -258,7 +258,7 @@ Zotero.Collection.prototype._initSave = Zotero.Promise.coroutine(function* (env) // Verify parent if (this._parentKey) { - let newParent = this.ObjectsClass.getByLibraryAndKey( + let newParent = yield this.ObjectsClass.getByLibraryAndKeyAsync( this.libraryID, this._parentKey ); @@ -285,6 +285,7 @@ Zotero.Collection.prototype._initSave = Zotero.Promise.coroutine(function* (env) Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) { var isNew = env.isNew; + var options = env.options; var collectionID = env.id = this._id = this.id ? this.id : yield Zotero.ID.get('collections'); var libraryID = env.libraryID = this.libraryID || Zotero.Libraries.userLibraryID; @@ -311,10 +312,14 @@ Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) this.version ? this.version : 0, this.synced ? 1 : 0 ]; + if (isNew || !options.skipClientDateModified) { + columns.push('clientDateModified'); + sqlValues.push(Zotero.DB.transactionDateTime); + } + if (isNew) { - var placeholders = columns.map(function () '?').join(); - - var sql = "REPLACE INTO collections (" + columns.join(', ') + ") " + let placeholders = columns.map(function () '?').join(); + let sql = "INSERT INTO collections (" + columns.join(', ') + ") " + "VALUES (" + placeholders + ")"; var insertID = yield Zotero.DB.queryAsync(sql, sqlValues); if (!collectionID) { diff --git a/chrome/content/zotero/xpcom/data/creators.js b/chrome/content/zotero/xpcom/data/creators.js @@ -169,7 +169,7 @@ Zotero.Creators = new function() { firstName: '', lastName: '' }; - for (i=0; i<this.fields.length; i++) { + for (let i=0; i<this.fields.length; i++) { let field = this.fields[i]; let val = data[field]; switch (field) { diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js @@ -36,7 +36,7 @@ Zotero.Item = function(itemTypeOrID) { this._disabled = false; - // loadPrimaryData (additional properties in dataObjet.js) + // loadPrimaryData (additional properties in dataObject.js) this._itemTypeID = null; this._firstCreator = null; this._sortCreator = null; @@ -290,7 +290,7 @@ Zotero.Item.prototype.getField = function(field, unformatted, includeBaseMapped) /** * @param {Boolean} asNames - * @return {Integer{}|String[]} + * @return {Integer[]|String[]} */ Zotero.Item.prototype.getUsedFields = function(asNames) { this._requireData('itemData'); @@ -711,7 +711,7 @@ Zotero.Item.prototype.setField = function(field, value, loadIn) { this._requireData('primaryData'); if (loadIn) { - throw('Cannot set primary field ' + field + ' in loadIn mode in Zotero.Item.setField()'); + throw new Error('Cannot set primary field ' + field + ' in loadIn mode in Zotero.Item.setField()'); } switch (field) { @@ -736,7 +736,7 @@ Zotero.Item.prototype.setField = function(field, value, loadIn) { break; default: - throw ('Primary field ' + field + ' cannot be changed in Zotero.Item.setField()'); + throw new Error('Primary field ' + field + ' cannot be changed in Zotero.Item.setField()'); } @@ -1534,7 +1534,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) { } } - if (this.isAttachment() || this._changed.attachmentData) { + if (this._changed.attachmentData) { let sql = "REPLACE INTO itemAttachments (itemID, parentItemID, linkMode, " + "contentType, charsetID, path, syncState) VALUES (?,?,?,?,?,?,?)"; let linkMode = this.attachmentLinkMode; @@ -3803,7 +3803,9 @@ Zotero.Item.prototype.multiDiff = Zotero.Promise.coroutine(function* (otherItems /** - * Returns an unsaved copy of the item + * Returns an unsaved copy of the item without an itemID or key + * + * This is used to duplicate items and copy them between libraries. * * @param {Number} [libraryID] - libraryID of the new item, or the same as original if omitted * @param {Boolean} [skipTags=false] - Skip tags @@ -3997,7 +3999,7 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { var setFields = {}; // Primary data - for (var field in json) { + for (let field in json) { let val = json[field]; switch (field) { diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js @@ -120,48 +120,11 @@ Zotero.Search.prototype._set = function (field, value) { } -/* - * Load a saved search from the DB - */ -Zotero.Search.prototype.loadPrimaryData = Zotero.Promise.coroutine(function* (reload) { - if (this._loaded.primaryData && !reload) return; - - var id = this._id; - var key = this._key; - var libraryID = this._libraryID; - var desc = id ? id : libraryID + "/" + key; - - if (!id && !key) { - throw new Error('ID or key not set'); - } - - var sql = Zotero.Searches.getPrimaryDataSQL() - if (id) { - sql += " AND savedSearchID=?"; - var params = id; - } - else { - sql += " AND key=? AND libraryID=?"; - var params = [key, libraryID]; - } - var data = yield Zotero.DB.rowQueryAsync(sql, params); - - this._loaded.primaryData = true; - this._clearChanged('primaryData'); - - if (!data) { - return; - } - - this.loadFromRow(data); -}); - - Zotero.Search.prototype.loadFromRow = function (row) { this._id = row.savedSearchID; this._libraryID = row.libraryID; this._key = row.key; - this._name = row.savedSearchName; + this._name = row.name; this._version = row.version; this._synced = !!row.synced; @@ -171,15 +134,18 @@ Zotero.Search.prototype.loadFromRow = function (row) { } Zotero.Search.prototype._initSave = Zotero.Promise.coroutine(function* (env) { + if (!this.libraryID) { + throw new Error('libraryID must be set before saving search'); + } if (!this.name) { throw new Error('Name not provided for saved search'); } - return Zotero.Search._super.prototype._initSave.apply(this, arguments); }); Zotero.Search.prototype._saveData = Zotero.Promise.coroutine(function* (env) { var isNew = env.isNew; + var options = env.options; var searchID = env.id = this._id = this.id ? this.id : yield Zotero.ID.get('savedSearches'); var libraryID = env.libraryID = this.libraryID || Zotero.Libraries.userLibraryID; @@ -189,7 +155,6 @@ Zotero.Search.prototype._saveData = Zotero.Promise.coroutine(function* (env) { var columns = [ 'savedSearchID', 'savedSearchName', - 'clientDateModified', 'libraryID', 'key', 'version', @@ -204,12 +169,27 @@ Zotero.Search.prototype._saveData = Zotero.Promise.coroutine(function* (env) { this.version ? this.version : 0, this.synced ? 1 : 0 ]; + if (isNew || !options.skipClientDateModified) { + columns.push('clientDateModified'); + sqlValues.push(Zotero.DB.transactionDateTime); + } - var sql = "REPLACE INTO savedSearches (" + columns.join(', ') + ") " - + "VALUES (" + placeholders + ")"; - var insertID = yield Zotero.DB.queryAsync(sql, sqlValues); - if (!searchID) { - searchID = env.id = insertID; + if (isNew) { + let placeholders = columns.map(function () '?').join(); + let sql = "INSERT INTO savedSearches (" + columns.join(', ') + ") " + + "VALUES (" + placeholders + ")"; + var insertID = yield Zotero.DB.queryAsync(sql, sqlValues); + if (!searchID) { + searchID = env.id = insertID; + } + } + else { + columns.shift(); + sqlValues.push(sqlValues.shift()); + let sql = 'UPDATE savedSearches SET ' + + columns.map(function (x) x + '=?').join(', ') + + ' WHERE savedSearchID=?'; + yield Zotero.DB.queryAsync(sql, sqlValues); } if (!isNew) { @@ -1674,18 +1654,19 @@ Zotero.Searches = function() { this.constructor = null; this._ZDO_object = 'search'; - this._ZDO_id = 'savedSearch'; + this._ZDO_id = 'savedSearchID'; this._ZDO_table = 'savedSearches'; this._primaryDataSQLParts = { savedSearchID: "O.savedSearchID", - name: "O.savedSearchName", + name: "O.savedSearchName AS name", libraryID: "O.libraryID", key: "O.key", version: "O.version", synced: "O.synced" } + this._primaryDataSQLFrom = "FROM savedSearches O"; this.init = Zotero.Promise.coroutine(function* () { yield Zotero.DataObjects.prototype.init.apply(this); @@ -1715,7 +1696,6 @@ Zotero.Searches = function() { var searches = []; for (var i=0; i<rows.length; i++) { let search = new Zotero.Search; - search.libraryID = libraryID; search.id = rows[i].id; yield search.loadPrimaryData(); searches.push(search);