Scholar.DB.getNextID(table, column) -- get the lowest unused integer >0 in a DB column
This commit is contained in:
parent
a457cdb493
commit
1410433e73
1 changed files with 30 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue