-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are now 3 builder classes (SobjectQueryBuilder, AggregateQueryBuilder & SearchBuilder) and 2 abstract classes (Soql & Sosl). * Upgraded to API v43.0 * Converted Soql.cls to an abstract class with shared logic + enums + subclasses * The old (but improved) AggregateResultQueryBuilder is back as AggregateQueryBuilder * (Re-)created SobjectQueryBuilder.cls * Polymorphic fields now have the sobject type added to the query Ex: addField(Lead.OwnerId) --> "Owner.Name, Owner.Type, OwnerId" are added to the query * Renamed includeChildrenRecords & getChildrenRecordsQuery methods * Added SearchBuilder.getSobjectTypes() * Simplified all aggregate methods into a few addAggregateField methods * Added support for rollup & dimesion groupings in aggregate queries * Renamed method useGroupingDimension to usingGroupingDimension * Added support for 'HAVING' keyword in aggregates * Added Soql.Operator enum to finally replace String operator * Added support for OR filters in the Soql builder classes * Replaced strings needed for Soql.DateLiteral with some enums * Added getFormattedValue() to Soql.QueryFilter * Added support for date functions (including convertTimeZone) in QueryField * Added AggregateQueryBuilder.getResultCount()
- Loading branch information
Showing
18 changed files
with
1,605 additions
and
732 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,104 @@ | ||
# Nebula Query & Search | ||
A lightweight Apex library for easily building dynamic SOQL queries & SOSL searches<br /><br /> | ||
[![Travis CI](https://img.shields.io/travis/jongpie/NebulaLogger/master.svg)](https://travis-ci.org/jongpie/NebulaLogger) | ||
# Nebula Query & Search for Salesforce Apex | ||
[![Travis CI](https://img.shields.io/travis/jongpie/NebulaQueryAndSearch/master.svg)](https://travis-ci.org/jongpie/NebulaQueryAndSearch) | ||
|
||
<a href="https://githubsfdeploy.herokuapp.com" target="_blank"> | ||
<img alt="Deploy to Salesforce" src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png"> | ||
</a> | ||
|
||
A dynamic SOQL query & SOSL search library for for Salesforce Apex<br /><br /> | ||
|
||
## Features | ||
* Easily add a field if the field meets the category specified, using the Soql.FieldCategory enum | ||
* Easily add any fields that are accessible, updateable, standard or custom, using the Soql.FieldCategory enum | ||
* Provides chainable builder methods for dyanmically building queries & searches in APex | ||
* Easily add fields to a query based on field level security | ||
* Easily add fields from a field set | ||
* Automatically adds the parent name field for any lookup/master-detail fields | ||
* Adds translations for picklist fields & record types by calling includeLabels() | ||
* Adds localized formatting for number, date, datetime, time, or currency fields by calling includeFormattedValues() | ||
* Leverage query scope to filter results | ||
* Enable query & search caching by simple calling cacheResults() | ||
* Reuse your dynamic SOQL queries to quickly build dynamic SOSL searches | ||
|
||
## SOQL Query Examples | ||
## Overview | ||
There are 3 main builder classes | ||
|
||
| SobjectQueryBuilder | AggregateQueryBuilder | SearchBuilder | ||
------- | --------------------|-----------------------|-------------- | ||
Super Class | Soql.cls (Queries) | Soql.cls (Queries) | Sosl.cls (Searches) | - | ||
Action | Queries an Sobject | Queries an Sobject | Searches 1 or more Sobjects | ||
Returns | `Sobject` or `List<Sobject>` | `AggregateResult` or `List<AggregateResult>` | `Sobject`, `List<Sobject>` or `List<List<Sobject>>` | ||
|
||
## SOQL Sobject Query Examples | ||
**Basic Usage:** Query an object & return the object's ID and display name field (typically the 'Name' field, but some objects use other fields, like Task.Subject and Case.CaseNumber). Since no filters have been added, this query would also return all accounts. | ||
|
||
``` | ||
List<Account> accounts = new Soql(Schema.Account.SobjectType).getQueryResults(); | ||
List<Account> accounts = new SobjectQueryBuilder(Schema.Account.SobjectType).getResults(); | ||
``` | ||
|
||
**Advanced Usage:** Query an object & leverage the query builder methods. The order of the builder methods does not matter - you can arrange the calls to these methods in any order that you prefer. | ||
|
||
``` | ||
Soql accountQuery = new Soql(Schema.Account) // Query the account object | ||
.addField(Schema.Account.ParentId) // Include the ParentId field, using SObjectField. The current user must have at least read access to the field | ||
.addField(Schema.Account.Type, Soql.FieldCategory.UPDATEABLE) // Include the Type field if the current user has access to update it | ||
.addFields(Soql.FieldCategory.CUSTOM) // Include all custom fields - Soql.cls only includes fields that are accessible to the user | ||
.addFields(myAccountFieldSet) // Include all fields in a field set that are accessible to the user | ||
.removeField(Schema.Account.My_custom_Field__c) // remove a custom field | ||
.usingScope(Soql.Scope.MINE) // Set the query scope | ||
.filterWhere(Schema.Account.CreatedDate, '=', new Soql.DateLiteral('LAST_WEEK')) // Filter on the created date, using a date literal | ||
.orderBy(Schema.Account.Type) // Order by a field API name - sort order/nulls defaults to 'Type ASC NULLS FIRST' | ||
.orderBy(Account.Name, Soql.SortOrder.ASCENDING) // Order by, using SObjectField & sort order | ||
.orderBy(Account.AnnualRevenue, Soql.SortOrder.DESCENDING, false) // Order by, using SObjectField, sort order and nulls sort order | ||
.limitCount(100) // Limit the results to 100 records | ||
.includeLabels() // Include labels/translations for any picklist fields or record types. These are aliased using the convention 'FieldName__c_Label' | ||
.includeFormattedValues() // Include formatted values for any number, date, time, or currency fields | ||
.cacheResults() // When enabled, the query results are internally cached - any subsequent calls for getQueryResults() will returned cached results instead of executing the query again | ||
.offset(25); // Skip the first 25 results | ||
SobjectQueryBuilder accountQuery = new SobjectQueryBuilder(Schema.Account.SobjectType) // Query the account object | ||
.addField(Schema.Account.ParentId) // Include the ParentId field, using SObjectField. The current user must have at least read access to the field | ||
.addField(Schema.Account.Type, Soql.FieldCategory.UPDATEABLE) // Include the Type field if the current user has access to update it | ||
.addFields(Soql.FieldCategory.CUSTOM) // Include all custom fields - only fields that are accessible to the user are included | ||
.addFieldSet(Schema.Account.MyFieldSet) // Include all fields in a field set that are accessible to the user | ||
.removeField(Schema.Account.My_Custom_Field__c) // remove a custom field | ||
.usingScope(Soql.Scope.MINE) // Set the query scope | ||
.filterWhere(Schema.Account.CreatedDate, '=', new Soql.DateLiteral('LAST_WEEK')) // Filter on the created date, using a date literal | ||
.orderBy(Schema.Account.Type) // Order by a field API name - sort order/nulls defaults to 'Type ASC NULLS FIRST' | ||
.orderBy(Account.Name, Soql.SortOrder.ASCENDING) // Order by, using SObjectField & sort order | ||
.orderBy(Account.AnnualRevenue, Soql.SortOrder.DESCENDING, false) // Order by, using SObjectField, sort order and nulls sort order | ||
.limitTo(100) // Limit the results to 100 records | ||
.includeLabels() // Include labels/translations for any picklist fields or record types. These are aliased using the convention 'FieldName__c_Label' | ||
.includeFormattedValues() // Include formatted values for any number, date, time, or currency fields | ||
.cacheResults() // When enabled, the query results are internally cached - any subsequent calls for getResults() will returned cached results instead of executing the query again | ||
.offsetBy(25); // Skip the first 25 results | ||
// Execute the query and store the results in the 'accounts' variable | ||
List<Account> accounts = accountQuery.getQueryResults(); | ||
List<Account> accounts = accountQuery.getResults(); | ||
/****** Resulting output ******* | ||
SELECT Id, MyCustomDateField__c, MyCustomPicklistField__c, Name, | ||
format(MyCustomDateField__c) MyCustomDateField__c__Formatted, | ||
toLabel(MyCustomPicklistField__c) MyCustomPicklistField__c__Label | ||
FROM Account | ||
USING SCOPE MINE | ||
WHERE CreatedDate = LAST_WEEK | ||
ORDER BY Type ASC NULLS FIRST, Name ASC NULLS FIRST, AnnualRevenue DESC NULLS LAST LIMIT 100 OFFSET 25 | ||
*******************************/ | ||
System.debug(accountQuery.getQuery()); | ||
``` | ||
|
||
## SOSL Search Examples | ||
**Basic Usage:** Search a single object | ||
|
||
``` | ||
Soql userQuery = new Soql(Schema.User.SobjectType); // Create an instance of Soql for an Sobject - you can include additional fields, filters, etc | ||
Sosl userSearch = new Sosl('my search term', userQuery); // Create a new Sosl instance with a search term & instance of Soql | ||
List<User> userSearchResults = userSearch.getFirstSearchResults(); // Sosl returns a list of lists of sobjects - getFirstSearchResults() returns the first list | ||
SobjectQueryBuilder userQuery = new SobjectQueryBuilder(Schema.User.SobjectType); // Create an instance of SobjectQueryBuilder for an Sobject - you can include additional fields, filters, etc | ||
SearchBuilder userSearch = new SearchBuilder('my search term', userQuery); // Create a new SearchBuilder instance with a search term & instance of SobjectQueryBuilder | ||
List<User> userSearchResults = userSearch.getFirstResults(); // SearchBuilder returns a list of lists of sobjects - getFirstResults() returns the first list | ||
/****** Resulting output ******* | ||
FIND 'my search term' IN ALL FIELDS RETURNING User(Id, Name) | ||
*******************************/ | ||
System.debug(userSearch.getSearch()); | ||
``` | ||
|
||
**Advanced Usage:** Search several objects | ||
|
||
``` | ||
Soql accountQuery = new Soql(Schema.Account.SobjectType); // Create an instance of Soql for the Account object | ||
Soql contactQuery = new Soql(Schema.Contact.SobjectType); // Create an instance of Soql for the Contact object | ||
Soql leadQuery = new Soql(Schema.Lead.SobjectType); // Create an instance of Soql for the Lead object | ||
List<Soql> queries = new List<Soql>{accountQuery, contactQuery, leadQuery}; // Add the Soql queries to a list | ||
SobjectQueryBuilder accountQuery = new SobjectQueryBuilder(Schema.Account.SobjectType); // Create an instance of SobjectQueryBuilder for the Account object | ||
SobjectQueryBuilder contactQuery = new SobjectQueryBuilder(Schema.Contact.SobjectType); // Create an instance of SobjectQueryBuilder for the Contact object | ||
SobjectQueryBuilder leadQuery = new SobjectQueryBuilder(Schema.Lead.SobjectType); // Create an instance of SobjectQueryBuilder for the Lead object | ||
List<SobjectQueryBuilder> queries = new List<SobjectQueryBuilder>{contactQuery, accountQuery, leadQuery}; // Add the SobjectQueryBuilder queries to a list | ||
SearchBuilder mySearch = new SearchBuilder('my search term', queries); // Create a new SearchBuilder instance with a search term & the list of SobjectQueryBuilder queries | ||
List<List<Sobject>> searchResults = mySearch.getResults(); // Returns all search results | ||
/****** Resulting output ******* | ||
FIND 'my search term' IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, Name), Lead(Id, Name) | ||
*******************************/ | ||
Sosl mySearch = new Sosl('my search term', queries); // Create a new Sosl instance with a search term & the list of Soql queries | ||
List<List<Sobject>> searchResults = mySearch.getSearchResults(); // Returns all search results | ||
System.debug(mySearch.getSearch()); | ||
``` |
Oops, something went wrong.