www

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

id.js (2225B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2016 Center for History and New Media
      5                      George Mason University, Fairfax, Virginia, USA
      6                      https://www.zotero.org
      7     
      8     This file is part of Zotero.
      9     
     10     Zotero is free software: you can redistribute it and/or modify
     11     it under the terms of the GNU Affero General Public License as published by
     12     the Free Software Foundation, either version 3 of the License, or
     13     (at your option) any later version.
     14     
     15     Zotero is distributed in the hope that it will be useful,
     16     but WITHOUT ANY WARRANTY; without even the implied warranty of
     17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18     GNU Affero General Public License for more details.
     19     
     20     You should have received a copy of the GNU Affero General Public License
     21     along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
     22     
     23     ***** END LICENSE BLOCK *****
     24 */
     25 
     26 Zotero.ID_Tracker = function () {
     27 	var _tables = [
     28 		'collections',
     29 		'creators',
     30 		'customFields',
     31 		'customItemTypes',
     32 		'itemDataValues',
     33 		'items',
     34 		'libraries',
     35 		'proxies',
     36 		'savedSearches',
     37 		'tags'
     38 	];
     39 	var _nextIDs = {};
     40 	
     41 	
     42 	this.init = Zotero.Promise.coroutine(function* () {
     43 		for (let table of _tables) {
     44 			_nextIDs[table] = yield _getNext(table);
     45 		}
     46 	});
     47 	
     48 	
     49 	/**
     50 	 * Gets an unused primary key id for a DB table
     51 	 */
     52 	this.get = function (table) {
     53 		if (!_nextIDs[table]) {
     54 			throw new Error("IDs not loaded for table '" + table + "'");
     55 		}
     56 		
     57 		return ++_nextIDs[table];
     58 	};
     59 	
     60 	
     61 	function _getTableColumn(table) {
     62 		switch (table) {
     63 			case 'libraries':
     64 				return 'libraryID';
     65 			
     66 			case 'itemDataValues':
     67 				return 'valueID';
     68 			
     69 			case 'savedSearches':
     70 				return 'savedSearchID';
     71 			
     72 			case 'creatorData':
     73 				return 'creatorDataID';
     74 			
     75 			case 'proxies':
     76 				return 'proxyID';
     77 			
     78 			default:
     79 				return table.substr(0, table.length - 1) + 'ID';
     80 		}
     81 	}
     82 	
     83 	
     84 	/**
     85 	 * Get MAX(id) + 1 from table
     86 	 *
     87 	 * @return {Promise<Integer>}
     88 	 */
     89 	function _getNext(table) {
     90 		var sql = 'SELECT COALESCE(MAX(' + _getTableColumn(table) + ') + 1, 1) FROM ' + table;
     91 		return Zotero.DB.valueQueryAsync(sql);
     92 	};
     93 }
     94 
     95 Zotero.ID = new Zotero.ID_Tracker;