From def86507cc055bffbd24a11f8ef2826701914ffa Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Mon, 17 Sep 2018 16:11:17 +0200
Subject: [PATCH] =?UTF-8?q?temporary=20changes=20for=20the=20sql-library?=
 =?UTF-8?q?=20[Projekt:=20Entwicklung=20-=20Neon][TicketNr.:=201023706][Be?=
 =?UTF-8?q?arbeitung=20-=20Auswahl=20der=20Verkn=C3=BCpfungen=20durch=20Lo?=
 =?UTF-8?q?okup-Komponente]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 entity/History_entity/conditionProcess.js |   2 +-
 entity/Pers_entity/conditionProcess.js    |   2 +-
 process/Sql_lib/process.js                | 277 +++++++++-------------
 3 files changed, 117 insertions(+), 164 deletions(-)

diff --git a/entity/History_entity/conditionProcess.js b/entity/History_entity/conditionProcess.js
index 9a59cbc1a0..495433af6a 100644
--- a/entity/History_entity/conditionProcess.js
+++ b/entity/History_entity/conditionProcess.js
@@ -8,7 +8,7 @@ var rowId, conditionStr, preparedValues
     ,sqlHelper;
 conditionStr = "";
 preparedValues = [];
-sqlHelper = new SqlUtils();
+sqlHelper = new LegacySqlUtils();
 
 if ((rowId = vars.getString("$param.RowId_param"))){
     conditionStr += " and HISTORYLINK.ROW_ID = ? ";
diff --git a/entity/Pers_entity/conditionProcess.js b/entity/Pers_entity/conditionProcess.js
index 0459862542..f2d2356e39 100644
--- a/entity/Pers_entity/conditionProcess.js
+++ b/entity/Pers_entity/conditionProcess.js
@@ -7,7 +7,7 @@ var orgId, conditionStr, preparedValues
     ,sqlHelper;
 conditionStr = "";
 preparedValues = [];
-sqlHelper = new SqlUtils();
+sqlHelper = new LegacySqlUtils();
 
 if (vars.exists("$param.OrgId_param") && (orgId = vars.getString("$param.OrgId_param"))){
     conditionStr += " and RELATION.ORG_ID = ? ";
diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js
index a29b85a638..24a50f2516 100644
--- a/process/Sql_lib/process.js
+++ b/process/Sql_lib/process.js
@@ -5,214 +5,167 @@ import("system.datetime");
 import("system.tools");
 import("system.SQLTYPES");
 import("Util_lib")
-/**
- *Class containing utilities for SQL
- *
- *@class
- */
-function SqlUtils()
-{
-    var that = this;
+
+function SqlMaskingUtils(){
+    //TODO: use callbacks for different handling?
     /**
-    * masks the function cast.
+    * masks the function cast of standard sql
     *
-    * @param {String} pField req
-    * @param {String} pDatatype req the datatypes, which should be casted to
-    *                               (varchar, integer, char, decimal or the SQLTYPES-Constants
-    *                                sowie SQLTYPES.DATE)
-    * @param {Integer|Interger[]} pLength req the length of the data
-    *                                         decimal: [length, decimals]
-    * @param {String} pAlias req the database alias
+    * @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, 
+    *                                   SQLTYPES.DECIMAL, SQLTYPES.DATE
+    * @param {int|int[]} targetLength specifies the length of the target data type;
+    *                                   <br/>- char/varchar: length
+    *                                   <br/>- decimal: [length, decimals]
+    * @param {String} [alias=the current alias] the alias where the statement shall be executed (this is needed to determine the database-type)
     *
-    * @return {String}
+    * @return {String} sql part to be included in sql-statements
     */
-    this.cast = function(pField, pDatatype, pLength, pAlias)
-    {
-        if (pAlias == undefined) 
-            pAlias = vars.getString("$sys.dbalias");
-        var dbtype = db.getDatabaseType(pAlias);
-        var func = "";
-        var datatype = "";
-
-        switch(Number(dbtype))
-        {
-            case db.DBTYPE_ORACLE10_CLUSTER:
-            case db.DBTYPE_ORACLE10_THIN:
-            case db.DBTYPE_ORACLE10_OCI:
-                func = "cast";
-                switch(pDatatype)
-                {
-                    case "char":
-                    case SQLTYPES.CHAR:
-                        datatype = "char";
-                        break;
-                    case "varchar":
+    this.cast = function (field, targetDatatype, targetLength, alias){
+        /* 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;
+        if (alias == undefined) 
+            alias = vars.getString("$sys.dbalias");
+        dbType = db.getDatabaseType(alias);
+        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
+         * e.g. SQLTYPES.INTEGER --> int
+         * @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)
+            {
+                case SQLTYPES.CHAR:
+                    res = "char";
+                    break;
+                case SQLTYPES.VARCHAR:
+                    res = "char";
+                    break;
+                case SQLTYPES.INTEGER:
+                    res = "int";
+                    break;
+                case SQLTYPES.DECIMAL:
+                    res = "decimal";
+                    break;
+                case SQLTYPES.DATE:
+                    res = "date";
+                    break;
+            }
+            return res;
+        }
+        switch (dbType) {
+            case db.DBTYPE_DERBY10:
+                switch(targetDatatype) {
                     case SQLTYPES.VARCHAR:
-                        datatype = "varchar2";
-                        break;
-                    case "integer":
-                    case SQLTYPES.INTEGER:
-                        datatype = "number(22,0)";
-                        break;
-                    case "decimal":
-                    case SQLTYPES.DECIMAL:
-                        datatype = "decimal";
-                        break;
-                    case SQLTYPES.DATE:
-                        datatype = "date";
+                        // 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
+                        field = "rtrim(" + cast(field, SQLTYPES.CHAR, targetLength, alias) + ")";
+                        sqlDataType = "varchar";
                         break;
-                }
-                break;
-            case db.DBTYPE_POSTGRESQL8:
-                func = "cast";
-                switch(pDatatype)
-                {
-                    case "char":
                     case SQLTYPES.CHAR:
-                        datatype = "char";
-                        break;
-                    case "varchar":
-                    case SQLTYPES.VARCHAR:
-                        datatype = "varchar";
+                        //TODO: throw error if(targetLength > 254)? https://db.apache.org/derby/docs/10.14/ref/rrefsqlj13733.html
+                        sqlDataType = "char";
                         break;
-                    case "integer":
-                    case SQLTYPES.INTEGER:
-                        datatype = "numeric(22,0)";
-                        break;
-                    case "decimal":
                     case SQLTYPES.DECIMAL:
-                        datatype = "numeric";
-                        break;
-                    case SQLTYPES.DATE:
-                        return pField + "::date";
-                }
-                break;
-            case db.DBTYPE_SQLSERVER2000:
-                func = "cast";
-                switch(pDatatype)
-                {
-                    case "char":
-                    case SQLTYPES.CHAR:
-                        datatype = "char";
-                        break;
-                    case "varchar":
-                    case SQLTYPES.VARCHAR:
-                        datatype = "varchar";
-                        break;
-                    case "integer":
                     case SQLTYPES.INTEGER:
-                        datatype = "int";
-                        break;
-                    case "decimal":
-                    case SQLTYPES.DECIMAL:
-                        datatype = "decimal";
-                        break;
                     case SQLTYPES.DATE:
-                        datatype = "date";
+                        sqlDataType = _mapDefaults(dataType);
                         break;
                 }
                 break;
-            case db.DBTYPE_MYSQL4:
             case db.DBTYPE_MARIADB10:
-                func = "cast";
-                switch(pDatatype)
-                {
-                    case "char":
-                    case SQLTYPES.CHAR:
-                        datatype = "char";
-                        break;
-                    case "varchar":
+            case db.DBTYPE_MYSQL4:
+                switch(targetDatatype) {
                     case SQLTYPES.VARCHAR:
-                        datatype = "char";
-                        break;
-                    case "integer":
+                    case SQLTYPES.CHAR:
                     case SQLTYPES.INTEGER:
-                        datatype = "integer";
-                        break;
-                    case "decimal":
                     case SQLTYPES.DECIMAL:
-                        datatype = "decimal";
-                        break;
                     case SQLTYPES.DATE:
-                        datatype = "date";
+                        sqlDataType = _mapDefaults(targetDatatype);
                         break;
                 }
                 break;
-            case db.DBTYPE_DERBY10:
-                func = "cast";
-                if(pLength > 254) 
-                    pLength = 254;
+            case db.DBTYPE_ORACLE10_CLUSTER:
+            case db.DBTYPE_ORACLE10_THIN:
+            case db.DBTYPE_ORACLE10_OCI:
                 switch(pDatatype)
                 {
-
-                    case "varchar":
                     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
-                        func = "rtrim(" + func;
-                        pField = " cast( " + pField + " as char("+pLength+")) ";
-                        datatype = "varchar";
-                        pLength += ")";
-                        break;
-
-                    case "char":
-                    case SQLTYPES.CHAR:
-                        datatype = "char";
+                        datatype = "varchar2";
                         break;
-                    case "integer":
                     case SQLTYPES.INTEGER:
-                        datatype = "int";
+                        datatype = "number";
+                        targetLength = "10"
                         break;
-                    case "decimal":
+                    case SQLTYPES.CHAR:
                     case SQLTYPES.DECIMAL:
-                        datatype = "numeric";
-                        break;
                     case SQLTYPES.DATE:
-                        datatype = "date";
+                        sqlDataType = _mapDefaults(targetDatatype);
                         break;
                 }
                 break;
-            case db.DBTYPE_FIREBIRD250:
-                func = "cast";
+            case db.DBTYPE_POSTGRESQL8:
                 switch(pDatatype)
                 {
-                    case "char":
+                    case SQLTYPES.DATE:
+                    case SQLTYPES.DECIMAL:
+                    case SQLTYPES.INTEGER:
                     case SQLTYPES.CHAR:
-                        datatype = "char";
-                        break;
-                    case "varchar":
                     case SQLTYPES.VARCHAR:
-                        datatype = "varchar";
-                        break;
-                    case "integer":
-                    case SQLTYPES.INTEGER:
-                        datatype = "numeric(22,0)";
-                        break;
-                    case "decimal":
-                    case SQLTYPES.DECIMAL:
-                        datatype = "numeric";
+                        sqlDataType = _mapDefaults(targetDatatype);
                         break;
+                }
+                break;
+            case db.DBTYPE_SQLSERVER2000:
                     case SQLTYPES.DATE:
-                        datatype = "date";
+                    case SQLTYPES.DECIMAL:
+                    case SQLTYPES.INTEGER:
+                    case SQLTYPES.CHAR:
+                    case SQLTYPES.VARCHAR:
+                        sqlDataType = _mapDefaults(targetDatatype);
                         break;
-                }
+            case db.DBTYPE_FIREBIRD250:
+                //TODO: firebird support?
                 break;
         }
-
-        if(pLength == undefined)
-            pLength = "";
-        else if(pLength != "")
+        
+        if (sqlDataType == undefined) {
+            throw new Error("sqlDataType");//TODO: add usefull message
+        }
+        
+        if(targetLength == undefined)
+            targetLength = "";
+        else if(targetLength != "")
         {
-            if(typeof(pLength == "object") && (pLength instanceof Array))
-                pLength = "(" + pLength.join(", ") + ")";
+            if(typeof(targetLength == "object") && (targetLength instanceof Array))
+                targetLength = "(" + targetLength.join(", ") + ")";
             else
-                pLength = "(" + pLength + ")";
+                targetLength = "(" + targetLength + ")";
         }
+        
+        sqlPart = functionName + "(" + field + " as " + sqlDataType + targetLength + ")";
+        return sqlPart;
+    }
+}
 
-        return func + "(" + pField + " as " + datatype + pLength + ")";
-    }   
-    
+/**
+ *Class containing utilities for SQL
+ *@deprecated use SqlMaskingUtils
+ *@class
+ */
+function LegacySqlUtils()
+{
+    var that = this;
     /**
     * masks the cast function for lob datatypes(clob, blob)
     *
-- 
GitLab