From c8b0d3daeda4d728f3668b980dbaea5e8892ba3e Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 18 May 2006 11:25:10 +0000 Subject: [PATCH] Renamed 'objects' to 'items' in DB and code -- all remaining uses refer to actual JS objects (with the exception of sidebar.js, in which only the calls to data access methods were changed) --- .../content/scholar/data_access.js | 384 +++++++++--------- chrome/chromeFiles/content/scholar/db.js | 4 +- chrome/chromeFiles/content/scholar/scholar.js | 2 +- chrome/chromeFiles/content/scholar/sidebar.js | 8 +- schema.sql | 171 ++++---- 5 files changed, 285 insertions(+), 284 deletions(-) diff --git a/chrome/chromeFiles/content/scholar/data_access.js b/chrome/chromeFiles/content/scholar/data_access.js index fb1d3fe3f7..cd8e9d748a 100644 --- a/chrome/chromeFiles/content/scholar/data_access.js +++ b/chrome/chromeFiles/content/scholar/data_access.js @@ -1,56 +1,56 @@ /* - * Constructor for Object object + * Constructor for Item object * - * Generally should be called through Scholar.Objects rather than directly + * Generally should be called through Scholar.Items rather than directly */ -Scholar.Object = function(){ +Scholar.Item = function(){ this._init(); - // Accept objectTypeID, folderID and orderIndex in constructor + // Accept itemTypeID, folderID and orderIndex in constructor if (arguments.length){ this.setType(arguments[0]); this.setPosition(arguments[1],arguments[2]); } } -Scholar.Object.prototype._init = function(){ +Scholar.Item.prototype._init = function(){ // // Public members for access by public methods -- do not access directly // this._data = new Array(); this._creators = new Scholar.Hash(); - this._objectData = new Array(); + this._itemData = new Array(); this._creatorsLoaded = false; - this._objectDataLoaded = false; + this._itemDataLoaded = false; this._changed = new Scholar.Hash(); this._changedCreators = new Scholar.Hash(); - this._changedObjectData = new Scholar.Hash(); + this._changedItemData = new Scholar.Hash(); } ////////////////////////////////////////////////////////////////////////////// // -// Public Scholar.Object methods +// Public Scholar.Item methods // ////////////////////////////////////////////////////////////////////////////// /* - * Check if the specified field is a primary field from the objects table + * Check if the specified field is a primary field from the items table */ -Scholar.Object.prototype.isPrimaryField = function(field){ - if (!Scholar.Object.primaryFields){ - Scholar.Object.primaryFields = Scholar.DB.getColumnHash('objects'); - Scholar.Object.primaryFields['firstCreator'] = true; - Scholar.Object.primaryFields['parentFolderID'] = true; - Scholar.Object.primaryFields['orderIndex'] = true; +Scholar.Item.prototype.isPrimaryField = function(field){ + if (!Scholar.Item.primaryFields){ + Scholar.Item.primaryFields = Scholar.DB.getColumnHash('items'); + Scholar.Item.primaryFields['firstCreator'] = true; + Scholar.Item.primaryFields['parentFolderID'] = true; + Scholar.Item.primaryFields['orderIndex'] = true; } - return !!Scholar.Object.primaryFields[field]; + return !!Scholar.Item.primaryFields[field]; } -Scholar.Object.editableFields = { +Scholar.Item.editableFields = { title: true, source: true, rights: true @@ -59,31 +59,31 @@ Scholar.Object.editableFields = { /* * Check if the specified primary field can be changed with setField() */ -Scholar.Object.prototype.isEditableField = function(field){ - return !!Scholar.Object.editableFields[field]; +Scholar.Item.prototype.isEditableField = function(field){ + return !!Scholar.Item.editableFields[field]; } /* * Build object from database */ -Scholar.Object.prototype.loadFromID = function(id){ - var sql = 'SELECT O.*, lastName AS firstCreator, TS.parentFolderID, ' +Scholar.Item.prototype.loadFromID = function(id){ + var sql = 'SELECT I.*, lastName AS firstCreator, TS.parentFolderID, ' + 'TS.orderIndex ' - + 'FROM objects O ' - + 'LEFT JOIN treeStructure TS ON (O.objectID=TS.id AND isFolder=0) ' - + 'LEFT JOIN objectCreators OC ON (O.objectID=OC.objectID) ' - + 'LEFT JOIN creators C ON (OC.creatorID=C.creatorID) ' - + 'WHERE objectID=' + id + ' AND OC.orderIndex=0'; + + 'FROM items I ' + + 'LEFT JOIN treeStructure TS ON (I.itemID=TS.id AND isFolder=0) ' + + 'LEFT JOIN itemCreators IC ON (I.itemID=IC.itemID) ' + + 'LEFT JOIN creators C ON (IC.creatorID=C.creatorID) ' + + 'WHERE itemID=' + id + ' AND IC.orderIndex=0'; var row = Scholar.DB.rowQuery(sql); this.loadFromRow(row); } /* - * Populate basic object data from a database row + * Populate basic item data from a database row */ -Scholar.Object.prototype.loadFromRow = function(row){ +Scholar.Item.prototype.loadFromRow = function(row){ this._init(); for (col in row){ if (this.isPrimaryField(col) || col=='firstCreator'){ @@ -97,41 +97,41 @@ Scholar.Object.prototype.loadFromRow = function(row){ /* * Check if any data fields have changed since last save */ -Scholar.Object.prototype.hasChanged = function(){ +Scholar.Item.prototype.hasChanged = function(){ return (this._changed.length || this._changedCreators.length || - this._changedObjectData.length); + this._changedItemData.length); } -Scholar.Object.prototype.getID = function(){ - return this._data['objectID'] ? this._data['objectID'] : false; +Scholar.Item.prototype.getID = function(){ + return this._data['itemID'] ? this._data['itemID'] : false; } -Scholar.Object.prototype.getType = function(){ - return this._data['objectTypeID'] ? this._data['objectTypeID'] : false; +Scholar.Item.prototype.getType = function(){ + return this._data['itemTypeID'] ? this._data['itemTypeID'] : false; } -Scholar.Object.prototype.getParent = function(){ +Scholar.Item.prototype.getParent = function(){ return this._data['parentFolderID'] ? this._data['parentFolderID'] : false; } /* - * Set or change the object's type + * Set or change the item's type */ -Scholar.Object.prototype.setType = function(objectTypeID){ - if (objectTypeID==this.getType()){ +Scholar.Item.prototype.setType = function(itemTypeID){ + if (itemTypeID==this.getType()){ return true; } // If existing type, clear fields from old type that aren't in new one if (this.getType()){ - var sql = 'SELECT fieldID FROM objectTypeFields ' - + 'WHERE objectTypeID=' + this.getType() + ' AND fieldID NOT IN ' - + '(SELECT fieldID FROM objectTypeFields WHERE objectTypeID=' - + objectTypeID + ')'; + var sql = 'SELECT fieldID FROM itemTypeFields ' + + 'WHERE itemTypeID=' + this.getType() + ' AND fieldID NOT IN ' + + '(SELECT fieldID FROM itemTypeFields WHERE itemTypeID=' + + itemTypeID + ')'; var obsoleteFields = Scholar.DB.columnQuery(sql); if (obsoleteFields){ @@ -141,16 +141,16 @@ Scholar.Object.prototype.setType = function(objectTypeID){ } } - this._data['objectTypeID'] = objectTypeID; - this._changed.set('objectTypeID'); + this._data['itemTypeID'] = itemTypeID; + this._changed.set('itemTypeID'); return true; } /* - * Returns the number of creators for this object + * Returns the number of creators for this item */ -Scholar.Object.prototype.numCreators = function(){ +Scholar.Item.prototype.numCreators = function(){ if (this.getID() && !this._creatorsLoaded){ this._loadCreators(); } @@ -160,7 +160,7 @@ Scholar.Object.prototype.numCreators = function(){ /* * Returns an array of the creator data at the given position, or false if none */ -Scholar.Object.prototype.getCreator = function(pos){ +Scholar.Item.prototype.getCreator = function(pos){ if (this.getID() && !this._creatorsLoaded){ this._loadCreators(); } @@ -175,7 +175,7 @@ Scholar.Object.prototype.getCreator = function(pos){ /* * Set or update the creator at the specified position */ -Scholar.Object.prototype.setCreator = function(orderIndex, firstName, lastName, creatorTypeID){ +Scholar.Item.prototype.setCreator = function(orderIndex, firstName, lastName, creatorTypeID){ if (this.getID() && !this._creatorsLoaded){ this._loadCreators(); } @@ -209,7 +209,7 @@ Scholar.Object.prototype.setCreator = function(orderIndex, firstName, lastName, /* * Remove a creator and shift others down */ -Scholar.Object.prototype.removeCreator = function(orderIndex){ +Scholar.Item.prototype.removeCreator = function(orderIndex){ if (this.getID() && !this._creatorsLoaded){ this._loadCreators(); } @@ -230,33 +230,33 @@ Scholar.Object.prototype.removeCreator = function(orderIndex){ /* - * Retrieves (and loads from DB, if necessary) an objectData field value + * Retrieves (and loads from DB, if necessary) an itemData field value * * Field can be passed as fieldID or fieldName */ -Scholar.Object.prototype.getField = function(field){ - //Scholar.debug('Requesting field ' + field + ' for object ' + this.getID(), 4); +Scholar.Item.prototype.getField = function(field){ + //Scholar.debug('Requesting field ' + field + ' for item ' + this.getID(), 4); if (this.isPrimaryField(field)){ return this._data[field] ? this._data[field] : ''; } else { - if (this.getID() && !this._objectDataLoaded){ - this._loadObjectData(); + if (this.getID() && !this._itemDataLoaded){ + this._loadItemData(); } - var fieldID = Scholar.ObjectFields.getID(field); + var fieldID = Scholar.ItemFields.getID(field); - return this._objectData[fieldID] ? this._objectData[fieldID] : ''; + return this._itemData[fieldID] ? this._itemData[fieldID] : ''; } } /* - * Set a field value, loading existing objectData first if necessary + * Set a field value, loading existing itemData first if necessary * * Field can be passed as fieldID or fieldName */ -Scholar.Object.prototype.setField = function(field, value, loadIn){ +Scholar.Item.prototype.setField = function(field, value, loadIn){ // Primary field if (this.isPrimaryField(field)){ if (!this.isEditableField(field)){ @@ -274,32 +274,32 @@ Scholar.Object.prototype.setField = function(field, value, loadIn){ // Type-specific field else { if (!this.getType()){ - throw ('Object type must be set before setting field data.'); + throw ('Item type must be set before setting field data.'); } - // If existing object, load field data first unless we're already in + // If existing item, load field data first unless we're already in // the middle of a load - if (this.getID() && !loadIn && !this._objectDataLoaded){ - this._loadObjectData(); + if (this.getID() && !loadIn && !this._itemDataLoaded){ + this._loadItemData(); } - var fieldID = Scholar.ObjectFields.getID(field); + var fieldID = Scholar.ItemFields.getID(field); if (!fieldID){ - throw (field + ' is not a valid objectData field.'); + throw (field + ' is not a valid itemData field.'); } - if (!Scholar.ObjectFields.isValidForType(fieldID, this.getType())){ + if (!Scholar.ItemFields.isValidForType(fieldID, this.getType())){ throw (field + ' is not a valid field for this type.'); } // If existing value, make sure it's actually changing - if (this._objectData[fieldID] && this._objectData[fieldID]==value){ + if (this._itemData[fieldID] && this._itemData[fieldID]==value){ return false; } - this._objectData[fieldID] = value; + this._itemData[fieldID] = value; if (!loadIn){ - this._changedObjectData.set(fieldID); + this._changedItemData.set(fieldID); } return true; } @@ -307,15 +307,15 @@ Scholar.Object.prototype.setField = function(field, value, loadIn){ /* - * Move object to new position and shift surrounding objects + * Move item to new position and shift surrounding items * * N.B. Unless isNew is set, this function updates the DB immediately and - * reloads all cached objects -- a save() is not required + * reloads all cached items -- a save() is not required * * If isNew is true, a transaction is not started or committed, so it * should only be run from an existing transaction within save() */ -Scholar.Object.prototype.setPosition = function(newFolder, newPos, isNew){ +Scholar.Item.prototype.setPosition = function(newFolder, newPos, isNew){ var oldFolder = this.getField('parentFolderID'); var oldPos = this.getField('orderIndex'); @@ -347,7 +347,7 @@ Scholar.Object.prototype.setPosition = function(newFolder, newPos, isNew){ Scholar.DB.query(sql); } - // If a new object, insert + // If a new item, insert if (isNew){ sql = 'INSERT INTO treeStructure SET id=' + this.getID() + ', ' + 'isFolder=0, orderIndex=' + newPos + ', ' + @@ -370,7 +370,7 @@ Scholar.Object.prototype.setPosition = function(newFolder, newPos, isNew){ this._data['orderIndex'] = newPos; if (!isNew){ - Scholar.Objects.reloadAll(); + Scholar.Items.reloadAll(); } return true; } @@ -379,19 +379,19 @@ Scholar.Object.prototype.setPosition = function(newFolder, newPos, isNew){ /* * Save changes back to database */ -Scholar.Object.prototype.save = function(){ +Scholar.Item.prototype.save = function(){ if (!this.hasChanged()){ - Scholar.debug('Object ' + this.getID() + ' has not changed', 4); + Scholar.debug('Item ' + this.getID() + ' has not changed', 4); return !!this.getID(); } // - // Existing object, update + // Existing item, update // if (this.getID()){ - Scholar.debug('Updating database with new object data', 4); + Scholar.debug('Updating database with new item data', 4); - var objectID = this.getID(); + var itemID = this.getID(); try { Scholar.DB.beginTransaction(); @@ -399,11 +399,11 @@ Scholar.Object.prototype.save = function(){ // // Primary fields // - var sql = "UPDATE objects SET "; + var sql = "UPDATE items SET "; var sql2; - if (this._changed.has('objectTypeID')){ - sql += "objectTypeID='" + this.getField('objectTypeID') + "', "; + if (this._changed.has('itemTypeID')){ + sql += "itemTypeID='" + this.getField('itemTypeID') + "', "; } if (this._changed.has('title')){ sql += "title='" + this.getField('title') + "', "; @@ -417,7 +417,7 @@ Scholar.Object.prototype.save = function(){ // Always update modified time sql += "dateModified=CURRENT_TIMESTAMP "; - sql += "WHERE objectID=" + this.getID() + ";\n"; + sql += "WHERE itemID=" + this.getID() + ";\n"; // @@ -434,8 +434,8 @@ Scholar.Object.prototype.save = function(){ // We have to do this immediately so old entries are // cleared before other ones are shifted down if (!creator['firstName'] && !creator['lastName']){ - sql2 = 'DELETE FROM objectCreators ' - + ' WHERE objectID=' + this.getID() + sql2 = 'DELETE FROM itemCreators ' + + ' WHERE itemID=' + this.getID() + ' AND orderIndex=' + orderIndex; Scholar.DB.query(sql2); continue; @@ -458,18 +458,18 @@ Scholar.Object.prototype.save = function(){ } - sql2 = 'SELECT COUNT(*) FROM objectCreators' - + ' WHERE objectID=' + this.getID() + sql2 = 'SELECT COUNT(*) FROM itemCreators' + + ' WHERE itemID=' + this.getID() + ' AND orderIndex=' + orderIndex; if (Scholar.DB.valueQuery(sql2)){ - sql += 'UPDATE objectCreators SET creatorID=' - + creatorID + ' WHERE objectID=' + this.getID() + sql += 'UPDATE itemCreators SET creatorID=' + + creatorID + ' WHERE itemID=' + this.getID() + ' AND orderIndex=' + orderIndex + ";\n"; } else { - sql += 'INSERT INTO objectCreators VALUES (' - + creatorID + ',' + objectID + ',' + orderIndex + sql += 'INSERT INTO itemCreators VALUES (' + + creatorID + ',' + itemID + ',' + orderIndex + ");\n"; } } @@ -480,34 +480,34 @@ Scholar.Object.prototype.save = function(){ // - // ObjectData + // ItemData // - if (this._changedObjectData.length){ + if (this._changedItemData.length){ var del = new Array(); - for (fieldID in this._changedObjectData.items){ + for (fieldID in this._changedItemData.items){ if (this.getField(fieldID)){ // Oh, for an INSERT...ON DUPLICATE KEY UPDATE - sql2 = 'SELECT COUNT(*) FROM objectData ' - + 'WHERE objectID=' + this.getID() + sql2 = 'SELECT COUNT(*) FROM itemData ' + + 'WHERE itemID=' + this.getID() + ' AND fieldID=' + fieldID; if (Scholar.DB.valueQuery(sql2)){ - sql += "UPDATE objectData SET value="; + sql += "UPDATE itemData SET value="; // Take advantage of SQLite's manifest typing - if (Scholar.ObjectFields.isInteger(fieldID)){ + if (Scholar.ItemFields.isInteger(fieldID)){ sql += this.getField(fieldID); } else { sql += "'" + this.getField(fieldID) + "'"; } - sql += " WHERE objectID=" + this.getID() + sql += " WHERE itemID=" + this.getID() + ' AND fieldID=' + fieldID + ";\n"; } else { - sql += 'INSERT INTO objectData VALUES (' + sql += 'INSERT INTO itemData VALUES (' + this.getID() + ',' + fieldID + ','; - if (Scholar.ObjectFields.isInteger(fieldID)){ + if (Scholar.ItemFields.isInteger(fieldID)){ sql += this.getField(fieldID); } else { @@ -524,8 +524,8 @@ Scholar.Object.prototype.save = function(){ // Delete blank fields if (del.length){ - sql += 'DELETE from objectData ' - + 'WHERE objectID=' + this.getID() + ' ' + sql += 'DELETE from itemData ' + + 'WHERE itemID=' + this.getID() + ' ' + 'AND fieldID IN (' + del.join() + ");\n"; } } @@ -541,10 +541,10 @@ Scholar.Object.prototype.save = function(){ } // - // New object, insert and return id + // New item, insert and return id // else { - Scholar.debug('Saving data for new object to database'); + Scholar.debug('Saving data for new item to database'); var isNew = true; var sqlColumns = new Array(); @@ -553,8 +553,8 @@ Scholar.Object.prototype.save = function(){ // // Primary fields // - sqlColumns.push('objectTypeID'); - sqlValues.push({'int':this.getField('objectTypeID')}); + sqlColumns.push('itemTypeID'); + sqlValues.push({'int':this.getField('itemTypeID')}); if (this._changed.has('title')){ sqlColumns.push('title'); @@ -601,17 +601,17 @@ Scholar.Object.prototype.save = function(){ ); } - sql += 'INSERT INTO objectCreators VALUES (' - + creatorID + ',' + objectID + ',' + orderIndex + sql += 'INSERT INTO itemCreators VALUES (' + + creatorID + ',' + itemID + ',' + orderIndex + ");\n"; } } // - // objectData fields + // itemData fields // - var sql = "INSERT INTO objects (" + sqlColumns.join() + ')' + var sql = "INSERT INTO items (" + sqlColumns.join() + ')' + ' VALUES ('; // Insert placeholders for bind parameters for (var i=0; i