Created
February 24, 2023 15:48
-
-
Save vspruyt-sol/60a015be5f8a5de93e927e6783d7f520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Adapter class for b2b_QueryBuilder | |
public with sharing class QueryBuilder { | |
private b2b_QueryBuilder builder; | |
private Schema.SObjectType sobjectType; | |
private String conditionLogic; | |
private List<String> fields = new List<String>(); | |
private List<String> conditionList = new List<String>(); | |
private List<String> orderByList = new List<String>(); | |
public QueryBuilder(Schema.SObjectType sobjectType){ | |
this.builder = new b2b_QueryBuilder().withObjectName(sobjectType.getDescribe().getname()); | |
this.sobjectType = sobjectType; | |
} | |
public QueryBuilder addFields(List<String> fields){ | |
this.fields.addAll(fields); | |
this.builder = this.builder.addFields(fields); | |
return this; | |
} | |
public QueryBuilder addFields(Set<String> fields){ | |
return this.addFields(new List<String>(fields)); | |
} | |
public QueryBuilder setFields(Set<String> fields){ | |
this.fields = new List<String>(fields); | |
this.builder = this.builder.withFields(new Set<String>(fields)); | |
return this; | |
} | |
public QueryBuilder setFields(List<String> fields){ | |
return this.setFields(new Set<String>(fields)); | |
} | |
public QueryBuilder addCondition(String condition){ | |
this.conditionList.add(condition); | |
this.builder = this.builder.withConditions(getFormattedConditionsWithConditionLogicApplied()); | |
return this; | |
} | |
public QueryBuilder setConditionLogic(String conditionLogic){ | |
this.conditionLogic = conditionLogic; | |
this.builder = this.builder.withConditions(getFormattedConditionsWithConditionLogicApplied()); | |
return this; | |
} | |
public QueryBuilder setGroupBy(String groupBy){ | |
this.builder = this.builder.withGroupBy(groupBy); | |
return this; | |
} | |
public QueryBuilder addOrderBy(String orderBy, String orderDirection){ | |
this.orderByList.add(String.format('{0} {1}', new List<String>{orderBy, orderDirection})); | |
this.builder = this.builder.withOrders(this.orderByList); | |
return this; | |
} | |
public QueryBuilder setLimit(Integer lim){ | |
this.builder = this.builder.withRowLimit(lim); | |
return this; | |
} | |
public QueryBuilder setOffset(Integer offset){ | |
this.builder = this.builder.withOffset(offset); | |
return this; | |
} | |
public override String toString(){ | |
return this.builder.build(); | |
} | |
private String getFormattedConditionsWithConditionLogicApplied(){ | |
String formattedConditions; | |
if(conditionLogic != null){ | |
formattedConditions = String.format(conditionLogic, conditionList); | |
} | |
else{ | |
formattedConditions = String.join(conditionList, ' AND '); | |
} | |
return formattedConditions; | |
} | |
/**qb.addChildRelation(qb) to transform if needed | |
QueryBuilder qb = new QueryBuilder(Account.SObjectType); | |
String query = qb.setFields(new List<String>{'Id'}) | |
.addFields(new List<String>{'Name', 'Industry'}) | |
.addCondition('Name != \'test\'') | |
.addCondition('Name != \'test2\'') | |
.setConditionLogic('({0} OR {1})') | |
.addOrderBy('Name', 'ASC') | |
.addOrderBy('Industry', 'DESC') | |
.setLimit(5) | |
.setOffset(2) | |
.toString(); | |
System.debug(qb); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment