From 3b5ec02949fca3e1c787619c84aad75384a7ed70 Mon Sep 17 00:00:00 2001 From: Sebastian Listl <s.listl@adito.de> Date: Thu, 28 Oct 2021 12:08:59 +0200 Subject: [PATCH] 1086863 SqlBuilder fix --- process/Sql_lib/process.js | 46 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 851b094fcf..c9189834b5 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -826,17 +826,34 @@ SqlBuilder.prototype._addWhereSubquery = function(pSubquery, pMandatory, pCondit */ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCondition, pFieldType, pAddPreparedConditionCallback) { - pCondition = this._verifyConditionFormat(pCondition); - if (!this._where._whereWasCalled) throw SqlBuilder._ERROR_WHERE_NOT_FIRST(); - + //skip if no values are provided and mandatory is false if (!pMandatory && pFieldOrCond === undefined && pValue === undefined && pCondition === undefined && pFieldType === undefined) return this; if (pFieldOrCond === undefined && pValue === undefined && pCondition === undefined && pFieldType === undefined) throw SqlBuilder._ERROR_NO_PARAMETER_PROVIDED(); + + //In a special case, pCondition can be a function. It will be called with the alias as argument and + //must return an array of the condition string and (optionally) the required sql field type. + //alternatively the function may return a string only to make the usage more bulletproof and convenient, so both SqlBuilder.EQUAL() + //and SqlBuilder.EQUAL work equally + if (typeof pCondition === "function") + { + var resCond = pCondition(this.alias); + if (Array.isArray(resCond)) + { + pCondition = resCond[0]; + pFieldType = pFieldType || resCond[1]; + } + else if(Utils.isString(pCondition)) + { + pCondition = resCond; + } + } + this._verifyConditionFormat(pCondition); // Special case: if only pFieldOrCond is set and we can identify it as a valid field-string (e.g. "Table.Field") we assume that it is not just a condition string. // --> we can check pValue for undefined and also allow simple string-conditions @@ -992,32 +1009,13 @@ SqlBuilder.prototype._addWhere = function(pFieldOrCond, pValue, pMandatory, pCon * helper function that checks if the format of a condition is valid * * @param {String} pCondition condition - * @return {String} the given condition, if the format is correct * @throws when the format is invalid */ SqlBuilder.prototype._verifyConditionFormat = function (pCondition) { if (!pCondition) { - return pCondition; - } - - //In a special case, pCondition can be a function. It will be called with the alias as argument and - //must return an array of the condition string and (optionally) the required sql field type. - //alternatively the function may return a string only to make the usage more bulletproof and convenient, so both SqlBuilder.EQUAL() - //and SqlBuilder.EQUAL work equally - if (typeof pCondition === "function") - { - var resCond = pCondition(this.alias); - if (Array.isArray(resCond)) - { - pCondition = resCond[0]; - pFieldType = pFieldType || resCond[1]; - } - else if(Utils.isString(pCondition)) - { - pCondition = resCond; - } + return; } // replace by {@NUMBERSIGN@} / {@QUESTIONSIGN@} as the js-regexp cannot do lookbehind (needed by the regexp used in replaceConditionTemplate to check escapes) @@ -1037,8 +1035,6 @@ SqlBuilder.prototype._verifyConditionFormat = function (pCondition) { throw SqlBuilder._ERROR_CONDITION_WRONG_FORMAT(); } - - return pCondition; } /** -- GitLab