From 736121b5482112b62e678386931ca9278d2b1267 Mon Sep 17 00:00:00 2001 From: Mateusz Zwierzynski Date: Tue, 16 Aug 2022 19:39:39 +0100 Subject: [PATCH] fflib-apex-common | mateuszzw | add addCondition() method to QueryFactory --- .../main/classes/fflib_QueryFactory.cls | 30 +++++++++++++++++++ .../test/classes/fflib_QueryFactoryTest.cls | 24 +++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/sfdx-source/apex-common/main/classes/fflib_QueryFactory.cls b/sfdx-source/apex-common/main/classes/fflib_QueryFactory.cls index 3cd29f06f22..fb57601e2ef 100644 --- a/sfdx-source/apex-common/main/classes/fflib_QueryFactory.cls +++ b/sfdx-source/apex-common/main/classes/fflib_QueryFactory.cls @@ -54,6 +54,7 @@ **/ public class fflib_QueryFactory { //No explicit sharing declaration - inherit from caller public enum SortOrder {ASCENDING, DESCENDING} + public enum ConditionJoiner {AND_CONDITION, OR_CONDITION} /** * This property is read-only and may not be set after instantiation. @@ -334,6 +335,35 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr public String getCondition(){ return this.conditionExpression; } + + /******************************************************************************************************************* + * @description a method to add additional condition to the where clause of the query + * @param conditionExpression (String): where clause condition to add + * @param joiner (ConditionJoiner): enum value that indicates whether the condition should be included + * with AND or OR joiner. + * @return fflib_QueryFactory + *******************************************************************************************************************/ + public fflib_QueryFactory addCondition(String conditionExpression, ConditionJoiner joiner) { + if(conditionExpression == null || joiner == null) { + return this; + } + + String joinCondition = joiner == ConditionJoiner.AND_CONDITION + ? 'AND' + : 'OR'; + + String addedCondition = conditionExpression.startsWith(' ') + ? conditionExpression + : ' ' + conditionExpression; + + this.conditionExpression = String.isNotBlank(this.conditionExpression) + ? this.conditionExpression + ' ' + joinCondition + addedCondition + : addedCondition; + this.conditionExpression = this.conditionExpression.trim(); + + return this; + } + /** * @param limitCount if not null causes a LIMIT clause to be added to the resulting query. **/ diff --git a/sfdx-source/apex-common/test/classes/fflib_QueryFactoryTest.cls b/sfdx-source/apex-common/test/classes/fflib_QueryFactoryTest.cls index 3eec7a8237e..f0f6cdd1c2e 100644 --- a/sfdx-source/apex-common/test/classes/fflib_QueryFactoryTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_QueryFactoryTest.cls @@ -71,6 +71,30 @@ private class fflib_QueryFactoryTest { System.assert(query.endsWith('WHERE name = \'test\''),'Query should have ended with a filter on name, got: '+query); } + @IsTest + static void testAddFieldConditionSuccess() { + String whereClause = 'Name = \'test\' AND Account.Name = \'test\''; + fflib_QueryFactory qf = new fflib_QueryFactory(Contact.SObjectType); + qf.selectField('name'); + qf.selectField('email'); + System.assertEquals(null, qf.getCondition()); + qf.addCondition('Name = \'test\'', fflib_QueryFactory.ConditionJoiner.AND_CONDITION); + qf.addCondition('Account.Name = \'test\'', fflib_QueryFactory.ConditionJoiner.AND_CONDITION); + System.assertEquals(whereClause, qf.getCondition()); + String query = qf.toSOQL(); + System.assert(query.endsWith('WHERE Name = \'test\' AND Account.Name = \'test\''),'Query should have ended with a filter on name, got: '+query); + } + + @IsTest + static void testAddFieldConditionFailure() { + String whereClause = 'Name = \'test\' AND Account.Name = \'test\''; + fflib_QueryFactory qf = new fflib_QueryFactory(Contact.SObjectType); + qf.selectField('name'); + qf.selectField('email'); + qf.addCondition(null, fflib_QueryFactory.ConditionJoiner.AND_CONDITION); + System.assertEquals(null, qf.getCondition()); + } + @isTest static void duplicateFieldSelection() { fflib_QueryFactory qf = new fflib_QueryFactory(Contact.SObjectType);