diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 5c4b1876b6ee3a1765e04cc6855235c047d7fc86..3b2f720defaf774b4b1d06cb5203fcdcbb77bf44 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -3061,11 +3061,12 @@ SqlBuilder.prototype.translate = function(pAlias) /** *provides functions for masking sql functions * - * @param {String} [alias=currentAlias] database alias, you can specify null if you have no alias available and you can manually set the dbType property + * @param {String} [pAlias=currentAlias] database alias, you can specify null if you have no alias available and you can manually set the dbType property * * @class */ -function SqlMaskingUtils(alias) { +function SqlMaskingUtils (pAlias) +{ this.alias = null; Object.defineProperty(this, "alias", { set: function(v){ @@ -3089,10 +3090,10 @@ function SqlMaskingUtils(alias) { } }); - if (alias === undefined) + if (pAlias === undefined) this.alias = vars.getString("$sys.dbalias"); else - this.alias = alias; + this.alias = pAlias; } /** @@ -3101,7 +3102,7 @@ function SqlMaskingUtils(alias) { */ SqlMaskingUtils.prototype.getConcatSymbol = function() { - switch(Number(this.dbType)) + switch(this.dbType) { case db.DBTYPE_SQLSERVER2000: return " + "; @@ -3129,10 +3130,10 @@ SqlMaskingUtils.prototype.getConcatSymbol = function() * @return {String} <p> * Returns the trimmed string.<br> */ -SqlMaskingUtils.prototype.trim = function(pField) +SqlMaskingUtils.prototype.trim = function (pField) { if (this.dbType == db.DBTYPE_SQLSERVER2000) - return "ltrim(rtrim(" + pField + "))"; + return "ltrim(rtrim(" + pField + "))"; return "trim(" + pField + ")"; } @@ -3140,26 +3141,26 @@ SqlMaskingUtils.prototype.trim = function(pField) * returns the max-value sql expressions depending on the database behind the given alias * note that this function does not verifiy if the field (and type) usage is valid at all * -* @param {String} field expression +* @param {String} pField expression * * @return {String} sql-part that can be used in a select */ -SqlMaskingUtils.prototype.max = function(field) +SqlMaskingUtils.prototype.max = function (pField) { - return "max(" + field + ")"; + return "max(" + pField + ")"; } /** * returns the min-value sql expressions depending on the database behind the given alias * note that this function does not verifiy if the field (and type) usage is valid at all * -* @param {String} field expression +* @param {String} pField expression * * @return {String} sql-part that can be used in a select */ -SqlMaskingUtils.prototype.min = function(field) +SqlMaskingUtils.prototype.min = function (pField) { - return "min(" + field + ")"; + return "min(" + pField + ")"; } /** @@ -3169,25 +3170,25 @@ SqlMaskingUtils.prototype.min = function(field) * Problems: * Derby has problems with casting to CHAR({> 254}) https://db.apache.org/derby/docs/10.14/ref/rrefsqlj13733.html * - * @param {String} field name of the database field that shall be castet - * @param {String} [targetDatatype] a SQLTYPES-value of the following: SQLTYPES.CHAR, SQLTYPES.VARCHAR, SQLTYPES.INTEGER, + * @param {String} pField name of the database field that shall be castet + * @param {String} [pTargetDatatype] a SQLTYPES-value of the following: SQLTYPES.CHAR, SQLTYPES.VARCHAR, SQLTYPES.INTEGER, * SQLTYPES.DECIMAL, SQLTYPES.DATE - * @param {int|int[]} targetLength specifies the length of the target data type; + * @param {int|int[]} pTargetLength specifies the length of the target data type; * <br/>- char/varchar: length * <br/>- decimal: [length, decimals] * * @return {String} sql part to be included in sql-statements */ -SqlMaskingUtils.prototype.cast = function(field, targetDatatype, targetLength) { +SqlMaskingUtils.prototype.cast = function (pField, pTargetDatatype, pTargetLength) +{ /* Some informations if you want to add supported databaseTypes or dataTypes: * You should consider using the _mapDefaults function-expression (details in the functions doc) * However you shouldn't use the function in a "default"-Block of a switch-case because of the following behaviour: * If a datatype is not supported you just have to NOT specify "sqlDataType" (leave it "undefined") -> an error is then raised * Therefore you should explicitly define which Data-type is supported and which is not */ - var dbType, functionName, sqlPart, sqlDataType, _mapDefaults; - dbType = this.dbType; - functionName = "cast";//overwrite this in the "switch (dbType)" if needed with your DBMS + var sqlDataType; + var functionName = "cast";//overwrite this in the "switch (dbType)" if needed with your DBMS /** * handles default-scenarios for mapping input-targetDatatype to a string for a sql-statement @@ -3195,39 +3196,37 @@ SqlMaskingUtils.prototype.cast = function(field, targetDatatype, targetLength) { * @param {Number} dataType input as a value of "SQLTYPES." that will be mapped to a string * @return {String} the mapped dataType for using in a sql-statement */ - _mapDefaults = function(dataType) { - var res; - switch(dataType) { + var _mapDefaults = function (dataType) + { + switch (dataType) + { case SQLTYPES.CHAR: - res = "char"; - break; + return "char"; case SQLTYPES.VARCHAR: - res = "char"; - break; + return "char"; case SQLTYPES.INTEGER: - res = "int"; - break; + return "int"; case SQLTYPES.DECIMAL: - res = "decimal"; - break; + return "decimal"; case SQLTYPES.DATE: - res = "date"; - break; + return "date"; } - return res; + return null; } - switch (dbType) { + switch (this.dbType) + { case db.DBTYPE_DERBY10: - switch(targetDatatype) { + switch(pTargetDatatype) + { case SQLTYPES.VARCHAR: // Because of a Derby bug, you can't cast INTEGER into VARCHAR // Therefor first cast to char then to varchar // https://issues.apache.org/jira/browse/DERBY-2072 // This cast to char is only done if the length is not bigger than 254, // otherwise the additional cast would result in a different error - if (targetLength <= 254) - field = "rtrim(" + this.cast(field, SQLTYPES.CHAR, targetLength) + ")"; + if (pTargetLength <= 254) + pField = "rtrim(" + this.cast(pField, SQLTYPES.CHAR, pTargetLength) + ")"; sqlDataType = "varchar"; break; case SQLTYPES.CHAR: @@ -3236,50 +3235,51 @@ SqlMaskingUtils.prototype.cast = function(field, targetDatatype, targetLength) { case SQLTYPES.DECIMAL: case SQLTYPES.INTEGER: case SQLTYPES.DATE: - sqlDataType = _mapDefaults(targetDatatype); + sqlDataType = _mapDefaults(pTargetDatatype); break; } break; case db.DBTYPE_MARIADB10: case db.DBTYPE_MYSQL4: - switch(targetDatatype) { + switch(pTargetDatatype) + { case SQLTYPES.VARCHAR: case SQLTYPES.CHAR: case SQLTYPES.INTEGER: case SQLTYPES.DECIMAL: case SQLTYPES.DATE: - sqlDataType = _mapDefaults(targetDatatype); + sqlDataType = _mapDefaults(pTargetDatatype); break; } break; case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: case db.DBTYPE_ORACLE10_OCI: - switch(targetDatatype) + switch(pTargetDatatype) { case SQLTYPES.VARCHAR: sqlDataType = "varchar2"; break; case SQLTYPES.INTEGER: sqlDataType = "number"; - targetLength = "10" + pTargetLength = "10" break; case SQLTYPES.CHAR: case SQLTYPES.DECIMAL: case SQLTYPES.DATE: - sqlDataType = _mapDefaults(targetDatatype); + sqlDataType = _mapDefaults(pTargetDatatype); break; } break; case db.DBTYPE_POSTGRESQL8: - switch(targetDatatype) + switch(pTargetDatatype) { case SQLTYPES.DATE: case SQLTYPES.DECIMAL: case SQLTYPES.INTEGER: case SQLTYPES.CHAR: case SQLTYPES.VARCHAR: - sqlDataType = _mapDefaults(targetDatatype); + sqlDataType = _mapDefaults(pTargetDatatype); break; } break; @@ -3289,64 +3289,60 @@ SqlMaskingUtils.prototype.cast = function(field, targetDatatype, targetLength) { case SQLTYPES.INTEGER: case SQLTYPES.CHAR: case SQLTYPES.VARCHAR: - sqlDataType = _mapDefaults(targetDatatype); + sqlDataType = _mapDefaults(pTargetDatatype); break; //TODO: firebird support? } - if (sqlDataType == undefined) { + if (sqlDataType == undefined) throw new Error(translate.withArguments("${SQL_LIB_UNSUPPORTED_DBTYPE} function: %0", ["SqlMaskingUtils.prototype.cast._mapDefaults"])); - } - if(targetLength == undefined) - targetLength = ""; - else if(targetLength != "") + if (pTargetLength == undefined) + pTargetLength = ""; + else if (pTargetLength != "") { - if(Array.isArray(targetLength)) - targetLength = "(" + targetLength.join(", ") + ")"; + if (Array.isArray(pTargetLength)) + pTargetLength = "(" + pTargetLength.join(", ") + ")"; else - targetLength = "(" + targetLength + ")"; + pTargetLength = "(" + pTargetLength + ")"; } - sqlPart = functionName + "(" + field + " as " + sqlDataType + targetLength + ")"; - return sqlPart; + return functionName + "(" + pField + " as " + sqlDataType + pTargetLength + ")"; } /** * masks the cast function for lob datatypes(clob, blob) into varchar or similar * - * @param {String} field expression that shall be casted - * @param {Integer|Interger[]} targetLength dessired length of the datatype + * @param {String} pField expression that shall be casted + * @param {Number|Number[]} pTargetLength desired length of the datatype * decimal: [length, decimals] * * @return {String} part of sql-expression that can be used */ -SqlMaskingUtils.prototype.castLob = function(field, targetLength) { - var res; - switch(this.dbType) { +SqlMaskingUtils.prototype.castLob = function (pField, pTargetLength) +{ + switch (this.dbType) + { case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: case db.DBTYPE_ORACLE10_OCI: - res = "DBMS_LOB.SUBSTR(" + field + ", " + targetLength + ", 1)"; - break; + return "DBMS_LOB.SUBSTR(" + pField + ", " + pTargetLength + ", 1)"; default: - res = this.cast(field, SQLTYPES.VARCHAR, targetLength); - break; + return this.cast(pField, SQLTYPES.VARCHAR, pTargetLength); } - return res; } /** * returns the function which determines the length of binary data * - * @param {String} fieldName name of the checked field + * @param {String} pField name of the checked field * * @return {String} */ -SqlMaskingUtils.prototype.binDataLength = function(fieldName) { - var res; - - switch(this.dbType) { +SqlMaskingUtils.prototype.binDataLength = function (pField) +{ + switch (this.dbType) + { case db.DBTYPE_MARIADB10: case db.DBTYPE_MYSQL4: case db.DBTYPE_ORACLE10_CLUSTER: @@ -3354,28 +3350,27 @@ SqlMaskingUtils.prototype.binDataLength = function(fieldName) { case db.DBTYPE_ORACLE10_OCI: case db.DBTYPE_POSTGRESQL8: case db.DBTYPE_DERBY10: - res = "LENGTH(" + fieldName + ")"; - break; + return "length(" + pField + ")"; case db.DBTYPE_SQLSERVER2000: - res = "DATALENGTH(" + fieldName + ")"; - break; + return "datalength(" + pField + ")"; + default: + throw new Error(translate.withArguments("${SQL_LIB_UNSUPPORTED_DBTYPE} function: %0", ["SqlMaskingUtils.prototype.binDataLength"])); } - return res; } /** * masks the sql function substring * - * @param {String } field the expression that shall be substringed - * @param {Number} start posistion where the substring starts - * @param {Number} length amount of characters of the expression will be returned by the sql function + * @param {String } pField the expression that shall be substringed + * @param {Number} pStartPos posistion where the substring starts + * @param {Number} pLength amount of characters of the expression will be returned by the sql function * * @return {String} part of sql-expression that can be used for substringing */ -SqlMaskingUtils.prototype.substring = function(field, start, length) { +SqlMaskingUtils.prototype.substring = function (pField, pStartPos, pLength) +{ var sqlFnName; - - switch(this.dbType) + switch (this.dbType) { case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: @@ -3398,8 +3393,7 @@ SqlMaskingUtils.prototype.substring = function(field, start, length) { default: throw new Error(translate.withArguments("${SQL_LIB_UNSUPPORTED_DBTYPE} function: %0", ["SqlMaskingUtils.prototype.substring"])); } - - return sqlFnName + "(" + field + ", " + start + ", " + length + ")"; + return sqlFnName + "(" + pField + ", " + pStartPos + ", " + pLength + ")"; } @@ -3414,7 +3408,7 @@ SqlMaskingUtils.prototype.substring = function(field, start, length) { * * @return {String} part of SQL-querey */ -SqlMaskingUtils.prototype.concat = function(pFields, pSeparator, pAutoTrimFields) +SqlMaskingUtils.prototype.concat = function (pFields, pSeparator, pAutoTrimFields) { if (pFields.length === 0) return "''"; @@ -3438,7 +3432,7 @@ SqlMaskingUtils.prototype.concat = function(pFields, pSeparator, pAutoTrimFields case db.DBTYPE_POSTGRESQL8: if (pAutoTrimFields) pFields = pFields.map(this.trim, this); - return " concat_ws( " + pSeparator + ", " + pFields.join(", ") + ")"; + return " concat_ws(" + pSeparator + ", " + pFields.join(", ") + ")"; case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: case db.DBTYPE_ORACLE10_OCI: @@ -3499,34 +3493,30 @@ SqlMaskingUtils.prototype.concat = function(pFields, pSeparator, pAutoTrimFields /** * returns the function for replacing a null value * - * @param {String} field expression that shall be checked for a null value - * @param {String} [replaceWith=empty string] expression that shall be used if the field contains null + * @param {String} pField expression that shall be checked for a null value + * @param {String} [pReplacement=empty string] expression that shall be used if the field contains null * * @return {string} */ -SqlMaskingUtils.prototype.isNull = function(field, replaceWith) { - var retSql; - - if (replaceWith == undefined) - replaceWith = "''"; - switch(this.dbType) { +SqlMaskingUtils.prototype.isNull = function (pField, pReplacement) +{ + if (pReplacement == undefined) + pReplacement = "''"; + switch (this.dbType) + { case db.DBTYPE_SQLSERVER2000: - retSql = "isnull(" + field + ", " + replaceWith + ")"; - break; + return "isnull(" + pField + ", " + pReplacement + ")"; case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_OCI: case db.DBTYPE_ORACLE10_THIN : - retSql = "nvl(" + field + ", " + replaceWith + ")"; - break; + return "nvl(" + pField + ", " + pReplacement + ")"; case db.DBTYPE_POSTGRESQL8: case db.DBTYPE_DERBY10: case db.DBTYPE_MYSQL4: case db.DBTYPE_MARIADB10: default: - retSql = "coalesce(" + field + ", " + replaceWith + ")"; - break; + return "coalesce(" + pField + ", " + pReplacement + ")"; } - return retSql; } /** @@ -3536,28 +3526,22 @@ SqlMaskingUtils.prototype.isNull = function(field, replaceWith) { * * @return {String} sql expression that extracts the day from a timestamp */ -SqlMaskingUtils.prototype.dayFromDate = function(pField) +SqlMaskingUtils.prototype.dayFromDate = function (pField) { - var retSql = ""; - switch (this.dbType) { case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: case db.DBTYPE_ORACLE10_OCI: - retSql = "to_char(" + pField + ",'dd')"; - break; + return "to_char(" + pField + ",'dd')"; case db.DBTYPE_DERBY10: case db.DBTYPE_SQLSERVER2000: case db.DBTYPE_MYSQL4: case db.DBTYPE_MARIADB10: - retSql = "DAY(" + pField + ")"; - break; + return "day(" + pField + ")"; case db.DBTYPE_POSTGRESQL8: - retSql = "EXTRACT (DAY from " + pField + ")"; - break; + return "extract (day from " + pField + ")"; } - return retSql; } /** @@ -3567,28 +3551,22 @@ SqlMaskingUtils.prototype.dayFromDate = function(pField) * * @return {String} sql expression that extracts the month from a timestamp */ -SqlMaskingUtils.prototype.monthFromDate = function(pField) +SqlMaskingUtils.prototype.monthFromDate = function (pField) { - var retSql = ""; - switch (this.dbType) { case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: case db.DBTYPE_ORACLE10_OCI: - retSql = "to_char(" + pField + ",'MM')"; - break; + return "to_char(" + pField + ",'MM')"; case db.DBTYPE_DERBY10: case db.DBTYPE_SQLSERVER2000: case db.DBTYPE_MYSQL4: case db.DBTYPE_MARIADB10: - retSql = "MONTH(" + pField + ")"; - break; + return "month(" + pField + ")"; case db.DBTYPE_POSTGRESQL8: - retSql = "EXTRACT (MONTH FROM " + pField + ")"; - break; + return "extract (month from " + pField + ")"; } - return retSql; } /** @@ -3600,26 +3578,20 @@ SqlMaskingUtils.prototype.monthFromDate = function(pField) */ SqlMaskingUtils.prototype.yearFromDate = function(pField) { - var retSql = ""; - switch (this.dbType) { case db.DBTYPE_ORACLE10_CLUSTER: case db.DBTYPE_ORACLE10_THIN: case db.DBTYPE_ORACLE10_OCI: - retSql = "to_char(" + pField + ",'yyyy')"; - break; + return "to_char(" + pField + ",'yyyy')"; case db.DBTYPE_DERBY10: case db.DBTYPE_SQLSERVER2000: case db.DBTYPE_MYSQL4: case db.DBTYPE_MARIADB10: - retSql = "YEAR(" + pField + ")"; - break; + return "YEAR(" + pField + ")"; case db.DBTYPE_POSTGRESQL8: - retSql = "EXTRACT (YEAR FROM " + pField + ")"; - break; + return "EXTRACT (YEAR FROM " + pField + ")"; } - return retSql; } /**