From 60f08cc40cfa4f849dc1d6b7a9d82cbf32653d2c Mon Sep 17 00:00:00 2001 From: "j.goderbauer" <j.goderbauer@adito.de> Date: Fri, 12 Apr 2019 14:33:04 +0200 Subject: [PATCH] in temporary Context_lib-function: added possibility of overwriting a columns table-alias --- process/Context_lib/process.js | 21 +++-- process/Sql_lib/process.js | 135 +++++++++++++++++++++++++++------ 2 files changed, 127 insertions(+), 29 deletions(-) diff --git a/process/Context_lib/process.js b/process/Context_lib/process.js index 82ea3eef6b..4d86570ae5 100644 --- a/process/Context_lib/process.js +++ b/process/Context_lib/process.js @@ -195,11 +195,22 @@ ContextSelector.create = function(pTableName, pIdField, pTitleExpression) return new ContextSelector(pTableName, pIdField, pTitleExpression); }; /** - * @return {String} full id field containing tablename and column + * @param {String} pField the fieldname that shall be returned as full string + * @return {String} full field containing tablename and the column; if the column itself is already a full qualified field that field is returned + */ +ContextSelector.prototype.getFullField = function(pField) +{ + if (SqlUtils.isFullFieldQualifier(pField)) + return pField; + else + return this.tableName + "." + pField; +}; +/** + * @return {String} full id field containing tablename and column; if the column itself is already a full qualified field that field is returned */ ContextSelector.prototype.getFullIdField = function() { - return this.tableName + "." + this.idField; + return this.getFullField(this.idField); }; /** * @return {String} full from-expression with tablename and join-part @@ -347,7 +358,7 @@ ContextUtils.getNameSubselectSql = function(pContextIdDbField, pRowIdDbField) var selectMap = ContextUtils.getSelectMap () for (let contextId in selectMap) { - select += "when '" + contextId + "' then (select " + selectMap[contextId].titleExpression + " from " + selectMap[contextId].getFullFromClause() + (pRowIdDbField ? " where " + selectMap[contextId].idField + " = " + pRowIdDbField : " ") + ") "; + select += "when '" + contextId + "' then (select " + selectMap[contextId].titleExpression + " from " + selectMap[contextId].getFullFromClause() + (pRowIdDbField ? " where " + selectMap[pContextId].getFullIdField() + " = " + pRowIdDbField : " ") + ") "; } select += "else 'Not defined in ContextUtils.getNameSql()!'"; @@ -382,7 +393,7 @@ ContextUtils.getContextDataSql = function(pContextId, pContactId, pWithDate, pAc var cond = SqlCondition.begin(); if (pContactId) { - cond.andPrepare(ownContextSelector.tableName + "." + ownContextSelector.contactIdField, pContactId) + cond.andPrepare(ownContextSelector.getFullField(ownContextSelector.contactIdField), pContactId) } if (pActive != undefined) @@ -408,7 +419,7 @@ ContextUtils.getContextDataSql = function(pContextId, pContactId, pWithDate, pAc pExcludedObjectIds.forEach(function(id) { - this.andPrepare(ownContextSelector.tableName + "." + ownContextSelector.idField, id, "# <> ?"); + this.andPrepare(ownContextSelector.getFullIdField(), id, "# <> ?"); }, exludedIdsCond) cond.andSqlCondition(exludedIdsCond); diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index ffeaa776d3..4c68f069fb 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -1033,42 +1033,129 @@ SqlMaskingUtils.prototype.yearFromDate = function(pField) return retSql; } +/** + * functions for various Sql-actions + * Do not create an instance of this! + * + * @class + * @static + */ function SqlUtils() {} -SqlUtils.getSingleColumnType = function(fieldOrTableName, columnName, alias) { - var tableName, fieldVarType; - if (columnName == undefined){ - fieldVarType = typeof(fieldOrTableName); - if (fieldVarType == "string") { - fieldOrTableName = text.split(fieldOrTableName, "\\."); - } else if (fieldVarType != "object") { - throw new TypeError(translate.text("${SQL_LIB_WRONG_FIELD_TYPE}")); +/** +* parses given name of table and name of column to clearly find out the tablename and columnanme +* +* @param {String|Array} pFieldOrTableName you've got several possibilites to pass here: +* <br/> 1. the name of the table if also a pColumnName is specified +* <br/> 2. the name of the table and columname as "tablename.columnname" (e.g. "ORGANISATION.NAME") if no pColumnName is specified +* <br/> 3. an array with 2 elements: [tablename, columnname] (e.g. ["ORGANISATION", "NAME"]) if no pColumnName is specified +* <br/> Everything else will raise an error +* +* @param {String} [pColumnName] depending on pFieldOrTableName this should be undefined/null or the name of a column +* +* @return {Object|TypeError} TypeError if something wrong has been passed or returns a object with these properties: +* 1. "table" which is the tablename +* 2. "column" which is the columnname +* e.g. {table: "ORGANISATION", column: "NAME"} +* +* +*/ +SqlUtils._parseFieldQualifier = function(pFieldOrTableName, pColumnName) +{ + var tableName, columnName; + if (pColumnName != undefined) + { + tableName = pFieldOrTableName; + columnName = pColumnName; + } + else + { + var fnName = "SqlUtils._parseFieldQualifier";//for return errors + var fieldVarType = typeof(pFieldOrTableName); + if (fieldVarType == "string") + { + pFieldOrTableName = text.split(pFieldOrTableName, "\\."); + } + else if (fieldVarType != "object") //check for object since there exists JavaArrays and JavaScript arrays which are both valid + { + return new TypeError(translate.withArguments("[%0]%1 has to be a string or array but it is %2", [fnName, "pFieldOrTableName", + fieldVarType])); } - if (fieldOrTableName.hasOwnProperty("length")) { - if (fieldOrTableName.length == 2) { + if (pFieldOrTableName.hasOwnProperty("length")) + { + if (pFieldOrTableName.length != 2) + return new TypeError(translate.withArguments("[%0]has now an incorrect length; estimated 2 elements but got %1", [ + fnName, pFieldOrTableName.length ])); - } else { - throw new TypeError(translate.text("${SQL_LIB_WRONG_FIELD_TYPE}")) - } - tableName = fieldOrTableName[0]; - columnName = fieldOrTableName[1]; + tableName = pFieldOrTableName[0]; + columnName = pFieldOrTableName[1]; } - else - throw new TypeError(translate.text("${SQL_LIB_WRONG_FIELD_TYPE}")); + else //check for object happens since there exists JavaArrays and JavaScript arrays which are both valid + return new TypeError(translate.withArguments("[%0]%1 is an object but seems not to be a valid array or array-like", [ + fnName, "pFieldOrTableName"])); } - else - tableName = fieldOrTableName; if (typeof(columnName) != "string") - throw new TypeError(translate.text("${SQL_LIB_WRONG_FIELD_TYPE}")); + return new TypeError(translate.withArguments("[%0]the columnName is not a string after interpreting", [fnName])); if (typeof(tableName) != "string") - throw new TypeError(translate.text("${SQL_LIB_WRONG_FIELD_TYPE}")); + return new TypeError(translate.withArguments("[%0]the tableName is not a string after interpreting", [fnName])); - if (alias == undefined) - alias = db.getCurrentAlias(); + return { + table: tableName, + column: columnName + }; +}; + + +/** +* determines if given values match a full field qualifier (name of table and name of column) +* +* @param {String|Array} pFieldOrTableName you've got several possibilites to pass here: +* <br/> 1. the name of the table if also a pColumnName is specified +* <br/> 2. the name of the table and columname as "tablename.columnname" (e.g. "ORGANISATION.NAME") if no pColumnName is specified +* <br/> 3. an array with 2 elements: [tablename, columnname] (e.g. ["ORGANISATION", "NAME"]) if no pColumnName is specified +* <br/> Everything else will raise an error +* +* @param {String} [pColumnName] depending on pFieldOrTableName this should be undefined/null or the name of a column +* +* @return {Boolean} returns true if it's a full qualifier or false if not +* +*/ +SqlUtils.isFullFieldQualifier = function(pFieldOrTableName, pColumnName) +{ + var parsed = SqlUtils._parseFieldQualifier(pFieldOrTableName, pColumnName); + if (parsed instanceof TypeError) + return false; + return true; +}; + +/** +* determines the type of a single database column in a table; if you want to get several columntypes at once use db.getColumnTypes instead +* +* @param {String|Array} pFieldOrTableName you've got several possibilites to pass here: +* <br/> 1. the name of the table if also a pColumnName is specified +* <br/> 2. the name of the table and columname as "tablename.columnname" (e.g. "ORGANISATION.NAME") if no pColumnName is specified +* <br/> 3. an array with 2 elements: [tablename, columnname] (e.g. ["ORGANISATION", "NAME"]) if no pColumnName is specified +* <br/> Everything else will raise an error +* +* @param {String} [pColumnName] depending on pFieldOrTableName this should be undefined/null or the name of a column +* @param {String} [pAlias=the current alias] Database-Aliasname, where the SQL-Statement shall be executed; default is the current dbalias +* +* @throws TypeError if a wrong format is passed as table/column-combination +* +* @return {Number} returns the corresponding SQLTYPES-value +* +*/ +SqlUtils.getSingleColumnType = function(pFieldOrTableName, pColumnName, pAlias) { + var fields = SqlUtils._parseFieldQualifier(pFieldOrTableName, pColumnName); + if (fields instanceof TypeError) + throw fields; + + if (pAlias == undefined) + pAlias = db.getCurrentAlias(); - return db.getColumnTypes(tableName, [columnName], alias)[0]; + return db.getColumnTypes(fields.table, [fields.column], pAlias)[0]; }; /** -- GitLab