Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fflib-apex-common | mateuszzw | add addCondition() method to QueryFactory #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sfdx-source/apex-common/main/classes/fflib_QueryFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
**/
Expand Down
24 changes: 24 additions & 0 deletions sfdx-source/apex-common/test/classes/fflib_QueryFactoryTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down