commit cb56b9607d7af8649c37f234b3cb430d9a2a6e2a
parent b83bc40426e11609ae5e3203df5134964971d652
Author: Aurimas Vinckevicius <aurimas.dev@gmail.com>
Date: Fri, 14 Nov 2014 04:38:59 -0600
Move loadPrimaryData to Zotero.DataObject
Diffstat:
4 files changed, 66 insertions(+), 87 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js
@@ -128,38 +128,6 @@ Zotero.Collection.prototype.getName = function() {
/*
- * Build collection from database
- */
-Zotero.Collection.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 sql = this.ObjectsClass.getPrimaryDataSQL();
- if (id) {
- sql += " AND O.collectionID=?";
- var params = id;
- }
- else {
- sql += " AND O.libraryID=? AND O.key=?";
- var params = [libraryID, key];
- }
- var data = yield Zotero.DB.rowQueryAsync(sql, params);
-
- this._loaded.primaryData = true;
- this._clearChanged('primaryData');
-
- if (!data) {
- return;
- }
-
- this.loadFromRow(data);
-});
-
-
-/*
* Populate collection data from a database row
*/
Zotero.Collection.prototype.loadFromRow = function(row) {
diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js
@@ -324,6 +324,60 @@ Zotero.DataObject.prototype._getLinkedObject = Zotero.Promise.coroutine(function
return false;
});
+/*
+ * Build object from database
+ */
+Zotero.DataObject.prototype.loadPrimaryData = Zotero.Promise.coroutine(function* (reload, failOnMissing) {
+ if (this._loaded.primaryData && !reload) return;
+
+ var id = this.id;
+ var key = this.key;
+ var libraryID = this.libraryID;
+
+ if (!id && !key) {
+ throw new Error('ID or key not set in Zotero.' + this._ObjectType + '.loadPrimaryData()');
+ }
+
+ var columns = [], join = [], where = [];
+ var primaryFields = this.ObjectsClass.primaryFields;
+ var idField = this.ObjectsClass.idColumn;
+ for (let i=0; i<primaryFields.length; i++) {
+ let field = primaryFields[i];
+ // If field not already set
+ if (field == idField || this['_' + field] === null || reload) {
+ columns.push(this.ObjectsClass.getPrimaryDataSQLPart(field));
+ }
+ }
+ if (!columns.length) {
+ return;
+ }
+
+ // This should match Zotero.*.primaryDataSQL, but without
+ // necessarily including all columns
+ var sql = "SELECT " + columns.join(", ") + this.ObjectsClass.primaryDataSQLFrom;
+ if (id) {
+ sql += " AND O." + idField + "=? ";
+ var params = id;
+ }
+ else {
+ sql += " AND O.key=? AND O.libraryID=? ";
+ var params = [key, libraryID];
+ }
+ sql += (where.length ? ' AND ' + where.join(' AND ') : '');
+ var row = yield Zotero.DB.rowQueryAsync(sql, params);
+
+ if (!row) {
+ if (failOnMissing) {
+ throw new Error(this._ObjectType + " " + (id ? id : libraryID + "/" + key)
+ + " not found in Zotero." + this._ObjectType + ".loadPrimaryData()");
+ }
+ this._loaded.primaryData = true;
+ this._clearChanged('primaryData');
+ return;
+ }
+
+ this.loadFromRow(row, reload);
+});
/**
* Reloads loaded, changed data
diff --git a/chrome/content/zotero/xpcom/data/dataObjects.js b/chrome/content/zotero/xpcom/data/dataObjects.js
@@ -50,6 +50,10 @@ Zotero.DataObjects = function (object, objectPlural, id, table) {
this._ZDO_idOnly = false;
}
+ Zotero.defineProperty(this, 'idColumn', {
+ get: function() this._ZDO_id
+ });
+
this._objectCache = {};
this._objectKeys = {};
this._objectIDs = {};
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -302,61 +302,6 @@ Zotero.Item.prototype.getUsedFields = function(asNames) {
/*
- * Build object from database
- */
-Zotero.Item.prototype.loadPrimaryData = Zotero.Promise.coroutine(function* (reload, failOnMissing) {
- if (this._loaded.primaryData && !reload) return;
-
- var id = this._id;
- var key = this._key;
- var libraryID = this._libraryID;
-
- if (!id && !key) {
- throw new Error('ID or key not set in Zotero.Item.loadPrimaryData()');
- }
-
- var columns = [], join = [], where = [];
- var primaryFields = this.ObjectsClass.primaryFields;
- for (let i=0; i<primaryFields.length; i++) {
- let field = primaryFields[i];
- // If field not already set
- if (field == 'itemID' || this['_' + field] === null || reload) {
- columns.push(this.ObjectsClass.getPrimaryDataSQLPart(field));
- }
- }
- if (!columns.length) {
- return;
- }
- // This should match Zotero.Items.getPrimaryDataSQL(), but without
- // necessarily including all columns
- var sql = "SELECT " + columns.join(", ") + this.ObjectsClass.primaryDataSQLFrom;
- if (id) {
- sql += " AND O.itemID=? ";
- var params = id;
- }
- else {
- sql += " AND O.key=? AND O.libraryID=? ";
- var params = [key, libraryID];
- }
- sql += (where.length ? ' AND ' + where.join(' AND ') : '');
- var row = yield Zotero.DB.rowQueryAsync(sql, params);
-
- if (!row) {
- if (failOnMissing) {
- throw new Error("Item " + (id ? id : libraryID + "/" + key)
- + " not found in Zotero.Item.loadPrimaryData()");
- }
- this._loaded.primaryData = true;
- this._clearChanged('primaryData');
- return;
- }
-
- this.loadFromRow(row, reload);
- return;
-});
-
-
-/*
* Populate basic item data from a database row
*/
Zotero.Item.prototype.loadFromRow = function(row, reload) {
@@ -365,6 +310,11 @@ Zotero.Item.prototype.loadFromRow = function(row, reload) {
this.setType(row.itemTypeID, true);
}
+ this._parseRowData(row);
+ this._finalizeLoadFromRow(row);
+}
+
+Zotero.Item.prototype._parseRowData = function(row) {
if (false) {
var primaryFields = this.ObjectsClass.primaryFields;
for (let i=0; i<primaryFields.length; i++) {
@@ -498,6 +448,9 @@ Zotero.Item.prototype.loadFromRow = function(row, reload) {
}
}
}
+}
+
+Zotero.Item.prototype._finalizeLoadFromRow = function(row) {
this._loaded.primaryData = true;
this._clearChanged('primaryData');
this._identified = true;