Skip to content
Snippets Groups Projects
Commit d8111d5e authored by Johannes Hörmann's avatar Johannes Hörmann
Browse files

Merge origin/master

parents 40d4309c 14984de9
No related branches found
No related tags found
No related merge requests found
......@@ -770,130 +770,124 @@ SqlUtils.getSingleColumnType = function(fieldOrTableName, columnName, alias) {
return db.getColumnTypes(tableName, [columnName], alias)[0];
};
//scope for internal functions;
//it's extremly important that the semicolon of the function expression above is correctly set because otherwise the function is called immediately
(function(){
/**
* calls a given function for N blocks of sql-data as long as records are available or the paging-process is manually canceled
*
* @param {Object|String} sqlStatement the sql statement that shall be executed
* String: SQL-query in a simple text form
* Object: prepared-sql-query: [sqlStr, [[value1, type1], [valueN, typeN]]]
* @param {Number} blockSize Amount of records that shall be read per block. (you need to specify an ORDER BY in your SQL-query)
* "0" <=> all records
* @param {Object (function)} callbackFn a callback-function that is called for every block and has the following params:
* myCallback(myDataBlockAs2Darray, myLoopCountThatStartsWith1)
* If "false" is returned sqlPageData will abort the paging process and return false
* @param {String} [dbAlias=the current alias] Database-Aliasname, where the SQL-Statement shall be executed; default is the current dbalias
* @param {Number} [timeout=configured dbTimeout in Preferences] Timeout in milliseconds; When it's reached the SQL-Statement will abort; default is in PREFERENCES configured
* @param {Number} [startOffset=0] Position where to begin with the data-reading-process; default is 0
*
*
* @return {bool} returns whether the function read all available data or not:
* false if the callback-function returned false, otherwise true
*
* @example
* var varValues = [];//you've got access to variables declared with 'var'
* let letValues = [];//you've got access to variables declared with 'let'
* var count = 0;//you cannot overwrite a variable of 'sqlPageData' by accident
*
* var sql = "select ORGNAME from ORG";
* var blockSize = 5 * 1000;
*
* var allRows = +db.cell("select count(*) from ORG");
*
* sqlPageData(sql, blockSize, function (pData, pRunNo){
* var j = pData.length;//pData is the current block with data
* logging.log(pRunNo.toString() + "#" + j);//pRunNo is the amount how often the func. has been already called
* //you can calculate the progress easily by: progress = (blockSize* (pRunNo-1) + pData.length) / (allRows - startOffset)
* //example in per cent:
* var startOffset = 0;//we did not pass any startOffset to sqlPageData - this is equivalent to zero
* var progress = (blockSize* (pRunNo-1) + pData.length) / (allRows - startOffset);
* logging.log("progess: " + eMath.roundDec(progress * 100, 2, eMath.ROUND_CEILING) + "%");
*
* for (var i = 0; i < j; i++)
* {
* varValues.push(pData[i][0]);
* letValues.push(pData[i][0]);
* }
*
* count += pRunNo * 100;
* logging.log("count:" + count);//you cannot overwrite a variable of 'sqlPageData' by accident
* });
*
* logging.show(letValues);//contains orgnames
* logging.show(varValues);//contains orgnames
*/
SqlUtils.pageTableData = function(sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset) {
return _pageData(null, sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset);
};
/**
* calls a given function for N blocks of sql-data as long as records are available or the paging-process is manually canceled
*
* @param {Object|String} sqlStatement the sql statement that shall be executed
* String: SQL-query in a simple text form
* Object: prepared-sql-query: [sqlStr, [[value1, type1], [valueN, typeN]]]
* @param {Number} blockSize Amount of records that shall be read per block. (you need to specify an ORDER BY in your SQL-query)
* "0" <=> all records
* @param {Object (function)} callbackFn a callback-function that is called for every block and has the following params:
* myCallback(myColumnDataBlockAsArray, myLoopCountThatStartsWith1)
* If "false" is returned sqlPageData will abort the paging process and return false
* @param {String} [dbAlias=the current alias] Database-Aliasname, where the SQL-Statement shall be executed; default is the current dbalias
* @param {Number} [timeout=configured dbTimeout in Preferences] Timeout in milliseconds; When it's reached the SQL-Statement will abort; default is in PREFERENCES configured
* @param {Number} [startOffset=0] Position where to begin with the data-reading-process; default is 0
*
*
* @return {bool} returns whether the function read all available data or not:
* false if the callback-function returned false, otherwise true
*
* @example
* similar to sqlTablePageData -> take a look at the example there
*/
SqlUtils.pageColumnData = function(sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset) {
return _pageData(db.COLUMN, sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset);
};
//internal function for paging through data; for description take a look at sqlArrayPageData
function _pageData(sqlType ,sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset){
if (dbAlias == undefined)
dbAlias = db.getCurrentAlias();
if (startOffset == undefined)
startOffset = 0;
var count = 0;
while (startOffset > -1) {
var data;
if (sqlType == null) {
if (timeout == undefined)
data = db.tablePage(sqlStatement, dbAlias, startOffset, blockSize);
else
data = db.tablePage(sqlStatement, dbAlias, startOffset, blockSize, timeout);
}
else {
if (timeout == undefined)
data = db.arrayPage(sqlType, sqlStatement, dbAlias, startOffset, blockSize);
else
data = db.arrayPage(sqlType, sqlStatement, dbAlias, startOffset, blockSize, timeout);
}
startOffset += blockSize;
/**
* calls a given function for N blocks of sql-data as long as records are available or the paging-process is manually canceled
*
* @param {Object|String} sqlStatement the sql statement that shall be executed
* String: SQL-query in a simple text form
* Object: prepared-sql-query: [sqlStr, [[value1, type1], [valueN, typeN]]]
* @param {Number} blockSize Amount of records that shall be read per block. (you need to specify an ORDER BY in your SQL-query)
* "0" <=> all records
* @param {Object (function)} callbackFn a callback-function that is called for every block and has the following params:
* myCallback(myDataBlockAs2Darray, myLoopCountThatStartsWith1)
* If "false" is returned sqlPageData will abort the paging process and return false
* @param {String} [dbAlias=the current alias] Database-Aliasname, where the SQL-Statement shall be executed; default is the current dbalias
* @param {Number} [timeout=configured dbTimeout in Preferences] Timeout in milliseconds; When it's reached the SQL-Statement will abort; default is in PREFERENCES configured
* @param {Number} [startOffset=0] Position where to begin with the data-reading-process; default is 0
*
*
* @return {bool} returns whether the function read all available data or not:
* false if the callback-function returned false, otherwise true
*
* @example
* var varValues = [];//you've got access to variables declared with 'var'
* let letValues = [];//you've got access to variables declared with 'let'
* var count = 0;//you cannot overwrite a variable of 'sqlPageData' by accident
*
* var sql = "select ORGNAME from ORG";
* var blockSize = 5 * 1000;
*
* var allRows = +db.cell("select count(*) from ORG");
*
* sqlPageData(sql, blockSize, function (pData, pRunNo){
* var j = pData.length;//pData is the current block with data
* logging.log(pRunNo.toString() + "#" + j);//pRunNo is the amount how often the func. has been already called
* //you can calculate the progress easily by: progress = (blockSize* (pRunNo-1) + pData.length) / (allRows - startOffset)
* //example in per cent:
* var startOffset = 0;//we did not pass any startOffset to sqlPageData - this is equivalent to zero
* var progress = (blockSize* (pRunNo-1) + pData.length) / (allRows - startOffset);
* logging.log("progess: " + eMath.roundDec(progress * 100, 2, eMath.ROUND_CEILING) + "%");
*
* for (var i = 0; i < j; i++)
* {
* varValues.push(pData[i][0]);
* letValues.push(pData[i][0]);
* }
*
* count += pRunNo * 100;
* logging.log("count:" + count);//you cannot overwrite a variable of 'sqlPageData' by accident
* });
*
* logging.show(letValues);//contains orgnames
* logging.show(varValues);//contains orgnames
*/
SqlUtils.pageTableData = function(sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset) {
return SqlUtils._pageData(null, sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset);
};
//this happens when all-records % blockSize == 0
//we do not want to call the callback-fn
if (data.length == 0)
return true;
else if (data.length < blockSize || blockSize == 0)//blocksize 0 is everything
startOffset = -1;//call callback the last time
/**
* calls a given function for N blocks of sql-data as long as records are available or the paging-process is manually canceled
*
* @param {Object|String} sqlStatement the sql statement that shall be executed
* String: SQL-query in a simple text form
* Object: prepared-sql-query: [sqlStr, [[value1, type1], [valueN, typeN]]]
* @param {Number} blockSize Amount of records that shall be read per block. (you need to specify an ORDER BY in your SQL-query)
* "0" <=> all records
* @param {Object (function)} callbackFn a callback-function that is called for every block and has the following params:
* myCallback(myColumnDataBlockAsArray, myLoopCountThatStartsWith1)
* If "false" is returned sqlPageData will abort the paging process and return false
* @param {String} [dbAlias=the current alias] Database-Aliasname, where the SQL-Statement shall be executed; default is the current dbalias
* @param {Number} [timeout=configured dbTimeout in Preferences] Timeout in milliseconds; When it's reached the SQL-Statement will abort; default is in PREFERENCES configured
* @param {Number} [startOffset=0] Position where to begin with the data-reading-process; default is 0
*
*
* @return {bool} returns whether the function read all available data or not:
* false if the callback-function returned false, otherwise true
*
* @example
* similar to sqlTablePageData -> take a look at the example there
*/
SqlUtils.pageColumnData = function(sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset) {
return SqlUtils._pageData(db.COLUMN, sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset);
};
if (callbackFn.call(this, data, ++count) === false)
return false;//callback can return false to manually stop the paging-process
//internal function for paging through data; for description take a look at sqlArrayPageData
SqlUtils._pageData = function(sqlType ,sqlStatement, blockSize, callbackFn, dbAlias, timeout, startOffset) {
if (dbAlias == undefined)
dbAlias = db.getCurrentAlias();
if (startOffset == undefined)
startOffset = 0;
var count = 0;
while (startOffset > -1) {
var data;
if (sqlType == null) {
if (timeout == undefined)
data = db.tablePage(sqlStatement, dbAlias, startOffset, blockSize);
else
data = db.tablePage(sqlStatement, dbAlias, startOffset, blockSize, timeout);
}
else {
if (timeout == undefined)
data = db.arrayPage(sqlType, sqlStatement, dbAlias, startOffset, blockSize);
else
data = db.arrayPage(sqlType, sqlStatement, dbAlias, startOffset, blockSize, timeout);
}
return true;
}
})();
startOffset += blockSize;
//this happens when all-records % blockSize == 0
//we do not want to call the callback-fn
if (data.length == 0)
return true;
else if (data.length < blockSize || blockSize == 0)//blocksize 0 is everything
startOffset = -1;//call callback the last time
if (callbackFn.call(this, data, ++count) === false)
return false;//callback can return false to manually stop the paging-process
}
return true;
}
/**
* Builds a SQL IN condition, while accounting for the 1000 elements maximum
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment