From 1410433e73d6b0935783c81875be41bf24197884 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 26 Aug 2006 08:02:17 +0000 Subject: [PATCH] Scholar.DB.getNextID(table, column) -- get the lowest unused integer >0 in a DB column --- .../chromeFiles/content/scholar/xpcom/db.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/chrome/chromeFiles/content/scholar/xpcom/db.js b/chrome/chromeFiles/content/scholar/xpcom/db.js index a2517cb080..393f13ca7e 100644 --- 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