commit 1410433e73d6b0935783c81875be41bf24197884
parent a457cdb493334fb75a804ed962ea19f8b9ad4e8c
Author: Dan Stillman <dstillman@zotero.org>
Date: Sat, 26 Aug 2006 08:02:17 +0000
Scholar.DB.getNextID(table, column) -- get the lowest unused integer >0 in a DB column
Diffstat:
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git a/chrome/chromeFiles/content/scholar/xpcom/db.js b/chrome/chromeFiles/content/scholar/xpcom/db.js
@@ -14,6 +14,7 @@ Scholar.DB = new function(){
this.statementQuery = statementQuery;
this.getColumns = getColumns;
this.getColumnHash = getColumnHash;
+ this.getNextID = getNextID;
this.beginTransaction = beginTransaction;
this.commitTransaction = commitTransaction;
this.rollbackTransaction = rollbackTransaction;
@@ -342,6 +343,35 @@ Scholar.DB = new function(){
}
+ /**
+ * Find the lowest unused integer >0 in a table column
+ *
+ * Note: This retrieves all the rows of the column, so it's not really
+ * meant for particularly large tables.
+ **/
+ function getNextID(table, column){
+ var sql = 'SELECT ' + column + ' FROM ' + table + ' ORDER BY ' + column;
+ var vals = Scholar.DB.columnQuery(sql);
+
+ if (!vals){
+ return 1;
+ }
+
+ if (vals[0] === '0'){
+ vals.shift();
+ }
+
+ for (var i=0, len=vals.length; i<len; i++){
+ if (vals[i] != i+1){
+ break;
+ }
+ }
+
+ return i+1;
+ }
+
+
+
/////////////////////////////////////////////////////////////////
//