Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Added public method usingScope() that takes a QueryFilterScope enum
Browse files Browse the repository at this point in the history
* New enum, QueryFilterScope, with values EVERYTHING, MINE, TEAM
* IQueryBuilder now has the method IQueryBuilder usingScope(QueryFilterScope filterScope);
* Fixed all methods in QueryBuilder & IQueryBuilder to return IQueryBuilder instead of QueryBuilder
* Fixed NebulaSettings so it actually loads the custom settings
  • Loading branch information
jongpie authored Mar 15, 2017
2 parents 52f7c26 + f60cae0 commit b724297
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/classes/IQueryBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IQueryBuilder {

IQueryBuilder limitCount(Integer limitCount);
IQueryBuilder setAsUpdate();
IQueryBuilder usingScope(QueryFilterScope filterScope);

// Query execution methods
SObject getFirstQueryResult();
Expand Down
4 changes: 4 additions & 0 deletions src/classes/NebulaSettings.cls
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public without sharing class NebulaSettings {
public NebulaRepositorySettings__c repositorySettings;
public NebulaTriggerHandlerSettings__c triggerHandlerSettings;

public NebulaSettings() {
this.loadCustomSettings();
}

public void resetAllSettingsToDefaults() {
this.deleteExistingCustomSettings();
this.createCustomSettings();
Expand Down
27 changes: 21 additions & 6 deletions src/classes/QueryBuilder.cls
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public without sharing class QueryBuilder extends NebulaCore implements IQueryBu
private List<String> orderByList;
private Integer limitCount;
private Boolean forUpdate;
private QueryFilterScope filterScope;

public QueryBuilder(Schema.SObjectType sobjectType, Schema.FieldSet fieldSet, List<Schema.SObjectField> sobjectFieldList) {
this.currentModule = NebulaCore.Module.REPOSITORY;
Expand All @@ -32,22 +33,22 @@ public without sharing class QueryBuilder extends NebulaCore implements IQueryBu
this.addSObjectFields();
}

public QueryBuilder whereField(Schema.SObjectField field, QueryOperator operator, Object value) {
public IQueryBuilder whereField(Schema.SObjectField field, QueryOperator operator, Object value) {
String parsedValue = QueryUtils.toQueryString(value);

this.whereClauseList.add(field + ' ' + operator.getValue() + ' ' + parsedValue);
return this;
}

public QueryBuilder orderBy(Schema.SObjectField orderByField) {
public IQueryBuilder orderBy(Schema.SObjectField orderByField) {
return this.orderBy(orderByField, null, null);
}

public QueryBuilder orderBy(Schema.SObjectField orderByField, QuerySortOrder sortOrder) {
public IQueryBuilder orderBy(Schema.SObjectField orderByField, QuerySortOrder sortOrder) {
return orderBy(orderByField, sortOrder, null);
}

public QueryBuilder orderBy(Schema.SObjectField orderByField, QuerySortOrder sortOrder, QueryNullSortOrder nullSortOrder) {
public IQueryBuilder orderBy(Schema.SObjectField orderByField, QuerySortOrder sortOrder, QueryNullSortOrder nullSortOrder) {
String sortOrderSoql = '';
if(sortOrder == QuerySortOrder.ASCENDING) sortOrderSoql = ' ASC';
else if(sortOrder == QuerySortOrder.DESCENDING) sortOrderSoql = ' DESC';
Expand All @@ -59,16 +60,21 @@ public without sharing class QueryBuilder extends NebulaCore implements IQueryBu
return this;
}

public QueryBuilder limitCount(Integer limitCount) {
public IQueryBuilder limitCount(Integer limitCount) {
this.limitCount = limitCount;
return this;
}

public QueryBuilder setAsUpdate() {
public IQueryBuilder setAsUpdate() {
this.forUpdate = true;
return this;
}

public IQueryBuilder usingScope(QueryFilterScope filterScope) {
this.filterScope = filterScope;
return this;
}

// Query execution methods
public SObject getFirstQueryResult() {
return this.getQueryResults()[0];
Expand Down Expand Up @@ -124,9 +130,17 @@ public without sharing class QueryBuilder extends NebulaCore implements IQueryBu
return forUpdateString;
}

private String getUsingScopeString() {
String usingScopeString = '';
if(this.filterScope != null) usingScopeString = '\nUSING SCOPE ' + this.filterScope.name();
return usingScopeString;
// TODO figure out what to do for SOSL https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_syntax.htm
}

private String getQueryString() {
String query = 'SELECT ' + this.getQueryFieldString()
+ '\nFROM ' + this.sobjectType
+ this.getUsingScopeString()
+ this.getWhereClauseString()
+ this.getOrderByString()
+ this.getLimitCountString()
Expand All @@ -148,6 +162,7 @@ public without sharing class QueryBuilder extends NebulaCore implements IQueryBu
+ ')';

if(this.forUpdate) this.addLogEntry('SOSL Search Query method flagged as FOR UPDATE. SOSL cannot use FOR UPDATE, ignoring');
if(this.filterScope != null) this.addLogEntry('SOSL Search Query method flagged as USING SCOPE ' + this.filterScope + '. SOSL cannot use USING SCOPE, ignoring');

this.addLogEntry(query);

Expand Down
5 changes: 5 additions & 0 deletions src/classes/QueryFilterScope.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*************************************************************************************************
* This file is part of the Nebula Framework project, released under the MIT License. *
* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. *
*************************************************************************************************/
public enum QueryFilterScope {EVERYTHING, MINE, TEAM}
5 changes: 5 additions & 0 deletions src/classes/QueryFilterScope.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>38.0</apiVersion>
<status>Active</status>
</ApexClass>
8 changes: 4 additions & 4 deletions src/classes/QueryUtils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public without sharing class QueryUtils {
Datetime datetimeValue = (Datetime)value;
return datetimeValue.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'', 'Greenwich Mean Time');
}
else if(value instanceof Decimal) return String.valueOf((Decimal) value);
else if(value instanceof Double) return String.valueOf((Double) value);
else if(value instanceof Integer) return String.valueOf((Integer) value);
else if(value instanceof Long) return String.valueOf((Long) value);
else if(value instanceof Decimal) return String.valueOf((Decimal)value);
else if(value instanceof Double) return String.valueOf((Double)value);
else if(value instanceof Integer) return String.valueOf((Integer)value);
else if(value instanceof Long) return String.valueOf((Long)value);
else if(value instanceof SObject) {
SObject record = (SObject)value;
return wrapInSingleQuotes(record.Id);
Expand Down

0 comments on commit b724297

Please sign in to comment.