From c21887e57a4c309b80d3a519d27860b94ed67828 Mon Sep 17 00:00:00 2001 From: "S.Listl" <s.listl@adito.de> Date: Mon, 24 Aug 2020 16:57:59 +0200 Subject: [PATCH] Refactoring: SqlBuilder.NORESULT_CONDITION() instead of 1=2 --- .../recordcontainers/db/conditionProcess.js | 11 +++++----- .../recordcontainers/db/conditionProcess.js | 2 +- .../objecttrees/filterConditionProcess.js | 3 ++- .../recordcontainers/db/conditionProcess.js | 10 ++------- .../objecttrees/filterConditionProcess.js | 3 ++- .../recordcontainers/db/conditionProcess.js | 15 +++---------- .../recordcontainers/db/conditionProcess.js | 2 +- process/Campaign_lib/process.js | 6 ++--- process/Context_lib/process.js | 2 +- process/Offer_lib/process.js | 2 +- process/Order_lib/process.js | 2 +- process/Sql_lib/process.js | 22 ++++++++++++++++++- 12 files changed, 43 insertions(+), 37 deletions(-) diff --git a/entity/Activity_entity/recordcontainers/db/conditionProcess.js b/entity/Activity_entity/recordcontainers/db/conditionProcess.js index ee923b83f2..e090677518 100644 --- a/entity/Activity_entity/recordcontainers/db/conditionProcess.js +++ b/entity/Activity_entity/recordcontainers/db/conditionProcess.js @@ -23,7 +23,7 @@ if (vars.exists("$param.RowId_param") && vars.get("$param.RowId_param") && vars. .arrayColumn(); if (allPersonRelatedContactIds.length === 0) - condition.and("1=2"); + condition.noResult(); else { activityLinkSubselect.and("ACTIVITYLINK.OBJECT_ROWID", allPersonRelatedContactIds, SqlBuilder.IN()); @@ -40,11 +40,10 @@ if (vars.exists("$param.RowId_param") && vars.get("$param.RowId_param") && vars. if (vars.exists("$param.ActivityIDs_param") && vars.get("$param.ActivityIDs_param")) { var acticityIds = JSON.parse(vars.get("$param.ActivityIDs_param")); - if (acticityIds.length == 0) { - condition.and("ACTIVITY.ACTIVITYID", ""); // Bedingung um kein Ergebnis zu bekommen - } else { + if (acticityIds.length == 0) + condition.noResult(); + else condition.and("ACTIVITY.ACTIVITYID", acticityIds, SqlBuilder.IN()); - } } if (vars.getString("$param.OnlyInnate_param") == "true") @@ -53,7 +52,7 @@ if (vars.getString("$param.OnlyInnate_param") == "true") if (ownContactId) condition.and("ACTIVITY.RESPONSIBLE", ownContactId); else - condition.clearWhere().and("1=2"); + condition.noResult(); } //TODO: use a preparedCondition (.build instead of .toString) when available #1030812 #1034026 diff --git a/entity/KeywordEntry_entity/recordcontainers/db/conditionProcess.js b/entity/KeywordEntry_entity/recordcontainers/db/conditionProcess.js index 68666406b4..6fcb81de35 100644 --- a/entity/KeywordEntry_entity/recordcontainers/db/conditionProcess.js +++ b/entity/KeywordEntry_entity/recordcontainers/db/conditionProcess.js @@ -20,7 +20,7 @@ if (vars.getString("$param.WhitelistIds_param")) if (whitelist.length > 0) cond.and("AB_KEYWORD_ENTRY.KEYID", JSON.parse(vars.getString("$param.WhitelistIds_param")), SqlBuilder.IN()); else - cond.and("1=2"); // force empty result if whitelist is empty + cond.noResult(); // force empty result if whitelist is empty } //TODO: use a preparedCondition (.build instead of .toString) when available #1030812 #1034026 diff --git a/entity/Organisation_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js b/entity/Organisation_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js index f20cd50003..0f8a38ee79 100644 --- a/entity/Organisation_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js +++ b/entity/Organisation_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js @@ -1,3 +1,4 @@ +import("Sql_lib"); import("Context_lib"); import("system.result"); import("ObjectRelation_lib"); @@ -7,4 +8,4 @@ import("ObjectRelation_lib"); var mapping = {}; mapping[ContextUtils.getCurrentContextId()] = "CONTACT.CONTACTID"; -result.string(ObjectRelationUtils.getFilterCondition(mapping).toString("1=2")); \ No newline at end of file +result.string(ObjectRelationUtils.getFilterCondition(mapping).toString(SqlBuilder.NORESULT_CONDITION())); \ No newline at end of file diff --git a/entity/Person_entity/recordcontainers/db/conditionProcess.js b/entity/Person_entity/recordcontainers/db/conditionProcess.js index 96a93d2dde..105cede403 100644 --- a/entity/Person_entity/recordcontainers/db/conditionProcess.js +++ b/entity/Person_entity/recordcontainers/db/conditionProcess.js @@ -24,15 +24,9 @@ if (vars.exists("$param.ExcludedContactIds_param") && vars.get("$param.ExcludedC var onlyShowContactIds = JSON.parse(vars.get("$param.OnlyShowContactIds_param")); -if(onlyShowContactIds != null && onlyShowContactIds.length > 0) +if (onlyShowContactIds != null && onlyShowContactIds.length > 0) { - let additionalCondition = newWhere(); - onlyShowContactIds.forEach(function(pContactId) - { - additionalCondition.or("CONTACT.CONTACTID", pContactId); - }); - cond.and(additionalCondition); - alternativeCondition = "1 = 2"; + cond.and("CONTACT.CONTACTID", onlyShowContactIds, SqlBuilder.IN()); } if (vars.exists("$param.OnlyOwnSupervised_param") && vars.get("$param.OnlyOwnSupervised_param") == "true") diff --git a/entity/Person_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js b/entity/Person_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js index 60242965b3..4516eb7b9b 100644 --- a/entity/Person_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js +++ b/entity/Person_entity/recordcontainers/db/recordfieldmappings/objecttrees/filterConditionProcess.js @@ -1,3 +1,4 @@ +import("Sql_lib"); import("Context_lib"); import("system.result"); import("ObjectRelation_lib"); @@ -8,4 +9,4 @@ var mapping = {}; mapping[ContextUtils.getCurrentContextId()] = "CONTACT.CONTACTID"; mapping["PrivatePerson"] = "CONTACT.PERSON_ID"; -result.string(ObjectRelationUtils.getFilterCondition(mapping).toString("1=2")); \ No newline at end of file +result.string(ObjectRelationUtils.getFilterCondition(mapping).toString(SqlBuilder.NORESULT_CONDITION())); \ No newline at end of file diff --git a/entity/SupportTicket_entity/recordcontainers/db/conditionProcess.js b/entity/SupportTicket_entity/recordcontainers/db/conditionProcess.js index 05d2e05eca..a3fb601aa0 100644 --- a/entity/SupportTicket_entity/recordcontainers/db/conditionProcess.js +++ b/entity/SupportTicket_entity/recordcontainers/db/conditionProcess.js @@ -9,15 +9,13 @@ var cond = newWhere() .and("TASK.KIND", $KeywordRegistry.taskType$ticket()) .andIfSet("TICKET.TICKETTYPE", "$param.TicketType_param") -var loadNothing = false; - if (vars.getString("$param.FilterOnlyOwnTicket_param") == "true") { var ownContactId = EmployeeUtils.getCurrentContactId(); if (ownContactId) cond.andIfSet("TASK.EDITOR_CONTACT_ID", ownContactId); else - loadNothing = true; + cond.noResult(); } if (vars.get("$param.RowId_param") && vars.get("$param.ObjectId_param")) @@ -30,12 +28,5 @@ if (vars.get("$param.RowId_param") && vars.get("$param.ObjectId_param")) , SqlBuilder.IN()); } -if (loadNothing) -{ - result.string("1=2"); -} -else -{ - //TODO: use a preparedCondition (.build instead of .toString) when available #1030812 #1034026 - result.string(cond.toString()); -} \ No newline at end of file +//TODO: use a preparedCondition (.build instead of .toString) when available #1030812 #1034026 +result.string(cond.toString()); \ No newline at end of file diff --git a/entity/Task_entity/recordcontainers/db/conditionProcess.js b/entity/Task_entity/recordcontainers/db/conditionProcess.js index bf89d69810..aeaf998bec 100644 --- a/entity/Task_entity/recordcontainers/db/conditionProcess.js +++ b/entity/Task_entity/recordcontainers/db/conditionProcess.js @@ -29,7 +29,7 @@ if (vars.exists("$param.RowId_param") && vars.get("$param.RowId_param") && vars. if (loadNothing) { - result.string("1=2"); + result.string(SqlBuilder.NORESULT_CONDITION()); } else { diff --git a/process/Campaign_lib/process.js b/process/Campaign_lib/process.js index b38ab24f96..2c9252dbe4 100644 --- a/process/Campaign_lib/process.js +++ b/process/Campaign_lib/process.js @@ -453,7 +453,7 @@ CampaignUtils.GetParticipantsCountByCondition = function(pWhereCondition) // If pWhereCondition would be used directly, // .cell() would return number of all participants and // .cell(true) would return an empty string "", not 0 - .where(pWhereCondition.build("1=2")) + .where(pWhereCondition.build(SqlBuilder.NORESULT_CONDITION())) .cell(); } @@ -484,7 +484,7 @@ CampaignUtils.GetParticipantsAlreadyAddedCountByCondition = function(pWhereCondi // If pWhereCondition would be used directly, // .cell() would return number of all participants and // .cell(true) would return an empty string "", not 0 - .where(pWhereCondition.build("1=2")) + .where(pWhereCondition.build(SqlBuilder.NORESULT_CONDITION())) return select.cell(); } @@ -649,7 +649,7 @@ CampaignUtils.copyCampaignSteps = function(pSourceCampaignId, pTargetCampaignId) { var InputMapping = { "CAMPAIGNSTEP": { - condition: newWhereIfSet("CAMPAIGN.CAMPAIGN_ID", pSourceCampaignId).orderBy("SORTING").toString("1=2", true), + condition: newWhereIfSet("CAMPAIGN.CAMPAIGN_ID", pSourceCampaignId).orderBy("SORTING").toString(SqlBuilder.NORESULT_CONDITION(), true), ValueMapping: { "CAMPAIGN_ID" : pTargetCampaignId } diff --git a/process/Context_lib/process.js b/process/Context_lib/process.js index 59bed65186..953330e3b3 100644 --- a/process/Context_lib/process.js +++ b/process/Context_lib/process.js @@ -632,7 +632,7 @@ ContextUtils.getNameSql = function(pContextId, pRowId) return newSelect(selectMap[pContextId].titleExpression) .from(selectMap[pContextId].getFullFromClause()) .whereIfSet(selectMap[pContextId].getFullIdField(), pRowId) - .build("1=2"); + .build(SqlBuilder.NORESULT_CONDITION()); } else return "select 1 from person where 1=2"; diff --git a/process/Offer_lib/process.js b/process/Offer_lib/process.js index 0c256379c1..3ba48d7e1d 100644 --- a/process/Offer_lib/process.js +++ b/process/Offer_lib/process.js @@ -356,7 +356,7 @@ OfferUtils.copyOfferItems = function (pSourceOfferId, pTargetOfferId) { var InputMapping = { "OFFERITEM": { - condition: newWhereIfSet("OFFERITEM.OFFER_ID", pSourceOfferId).orderBy("ITEMSORT").toString("1=2", true), + condition: newWhereIfSet("OFFERITEM.OFFER_ID", pSourceOfferId).orderBy("ITEMSORT").toString(SqlBuilder.NORESULT_CONDITION(), true), ValueMapping: { "OFFER_ID" : pTargetOfferId } diff --git a/process/Order_lib/process.js b/process/Order_lib/process.js index 22b0f95f8f..d6938458c5 100644 --- a/process/Order_lib/process.js +++ b/process/Order_lib/process.js @@ -156,7 +156,7 @@ OrderUtils.copyOfferItemsToOrder = function (pSourceOfferId, pOrderId) "INFO" : "INFO", "VAT" : "VAT" }, - condition: newWhereIfSet("OFFERITEM.OFFER_ID", pSourceOfferId).orderBy("ITEMSORT").toString("1=2", true), + condition: newWhereIfSet("OFFERITEM.OFFER_ID", pSourceOfferId).orderBy("ITEMSORT").toString(SqlBuilder.NORESULT_CONDITION(), true), ValueMapping: { "OFFER_ID" : pOrderId } diff --git a/process/Sql_lib/process.js b/process/Sql_lib/process.js index 3039c1cf1c..93cf3bee05 100644 --- a/process/Sql_lib/process.js +++ b/process/Sql_lib/process.js @@ -1825,13 +1825,23 @@ SqlBuilder.GREATER_OR_EQUAL = function () /** * Constant-like function which provides a value for pCondition if you need a "# <= ?" statement. * - * @return {String} + * @return {String} */ SqlBuilder.LESS_OR_EQUAL = function () { return "# <= ?"; } +/** + * Constant-like function which returns an impossible condition ("1 = 2"). + * + * @return {String} + */ +SqlBuilder.NORESULT_CONDITION = function () +{ + return "1 = 2"; +} + /** * Object providing constant-like functions for sql-any-conditions. */ @@ -2870,6 +2880,16 @@ SqlBuilder.prototype.forEachPage = function (pCallBackFn, pExecuteOnlyIfConditio run = pCallBackFn.call(null, this.nextTablePage(pExecuteOnlyIfConditionExists, pTimeout)) != false; } +/** + * Sets an impossible where-condition, so that the query won't return any rows. + * + * @return {SqlBuilder} current object + */ +SqlBuilder.prototype.noResult = function () +{ + return this.clearWhere().where(SqlBuilder.NORESULT_CONDITION()); +} + /** * checks if an update /delete statement should be called or not * @return {Boolean} -- GitLab