-
Notifications
You must be signed in to change notification settings - Fork 8
Added support for database access levels and new query methods with binds #49
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
Changes from 4 commits
00d196d
82c6d68
100d090
f183a76
158db75
f497090
9a3f1c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ global class AggregateQuery extends SOQL { | |||||||||
| private SOQL.GroupingDimension groupingDimension; | ||||||||||
| private List<AggregateField> aggregateFields; | ||||||||||
| private List<String> havingConditions; | ||||||||||
| private String countQuery; | ||||||||||
|
|
||||||||||
| global AggregateQuery(Schema.SObjectType sobjectType) { | ||||||||||
| super(sobjectType, false); | ||||||||||
|
|
@@ -79,19 +80,48 @@ global class AggregateQuery extends SOQL { | |||||||||
| return this.havingAggregate(aggregateFunction, new SOQL.QueryField(field), operator, value); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, Schema.SObjectField field, SOQL.Operator operator, Object value, String bindWithKey) { | ||||||||||
| return this.havingAggregate(aggregateFunction, new SOQL.QueryField(field), operator, value, bindWithKey); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.Operator operator, Object value) { | ||||||||||
| this.havingConditions.add(aggregateFunction.name() + '(' + queryField + ') ' + SOQL.getOperatorValue(operator) + ' ' + value); | ||||||||||
| return this.havingAggregate(aggregateFunction, queryField, operator, value, null); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery havingAggregate(SOQL.Aggregate aggregateFunction, SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindWithKey) { | ||||||||||
| this.havingConditions.add( | ||||||||||
| String.format( | ||||||||||
| '{0}({1}) {2} {3}', | ||||||||||
| new List<String> { | ||||||||||
| aggregateFunction.name(), | ||||||||||
| queryField.toString(), | ||||||||||
| SOQL.getOperatorValue(operator), | ||||||||||
| (String.isNotBlank(bindWithKey) ? ':' + bindWithKey : new QueryArgument(value).toString()) | ||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love how you've kept it backwards compatible with |
||||||||||
| } | ||||||||||
| ) | ||||||||||
| ); | ||||||||||
| if (String.isNotBlank(bindWithKey)) { | ||||||||||
| this.bindsMap.put(bindWithKey, value); | ||||||||||
| } | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value) { | ||||||||||
| return this.filterWhere(new SOQL.QueryField(field), operator, value); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value, String bindWithKey) { | ||||||||||
| return this.filterWhere(new SOQL.QueryField(field), operator, value, bindWithKey); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value) { | ||||||||||
| return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindWithKey) { | ||||||||||
| return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value, bindWithKey)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery filterWhere(SOQL.QueryFilter filter) { | ||||||||||
| return this.filterWhere(new List<SOQL.QueryFilter>{ filter }); | ||||||||||
| } | ||||||||||
|
|
@@ -106,6 +136,11 @@ global class AggregateQuery extends SOQL { | |||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery withAccessLevel(System.AccessLevel mode) { | ||||||||||
| super.doWithAccessLevel(mode); | ||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, I think the variable should be called
Suggested change
|
||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery orderByField(Schema.SObjectField field) { | ||||||||||
| return this.orderByField(field, null); | ||||||||||
| } | ||||||||||
|
|
@@ -166,6 +201,26 @@ global class AggregateQuery extends SOQL { | |||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery setBind(String key, Object value) { | ||||||||||
| super.doSetBind(key, value); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery setBinds(Map<String, Object> binds) { | ||||||||||
| super.doSetBinds(binds); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery removeBind(String key) { | ||||||||||
| super.doRemoveBind(key); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateQuery clearBinds() { | ||||||||||
| super.doClearBinds(); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // TODO decide if this should be global | ||||||||||
| public AggregateQuery cacheResults() { | ||||||||||
| super.doCacheResults(); | ||||||||||
|
|
@@ -206,10 +261,12 @@ global class AggregateQuery extends SOQL { | |||||||||
| return this.query; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // TODO consider renaming to getCountResult() | ||||||||||
| @SuppressWarnings('PMD.ApexSOQLInjection') | ||||||||||
| global Integer getResultCount() { | ||||||||||
| String countQuery = | ||||||||||
| global String getCountQuery() { | ||||||||||
| if (this.countQuery != null && !this.hasChanged) { | ||||||||||
| return this.countQuery; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| this.countQuery = | ||||||||||
| 'SELECT COUNT()' + | ||||||||||
| ' FROM ' + | ||||||||||
| this.sobjectType + | ||||||||||
|
|
@@ -220,7 +277,19 @@ global class AggregateQuery extends SOQL { | |||||||||
| super.doGetOrderByString() + | ||||||||||
| super.doGetLimitCountString() + | ||||||||||
| super.doGetOffetString(); | ||||||||||
| return Database.countQuery(countQuery); | ||||||||||
|
|
||||||||||
| System.debug(System.LoggingLevel.FINEST, this.countQuery); | ||||||||||
| return this.countQuery; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // TODO consider renaming to getCountResult() | ||||||||||
| @SuppressWarnings('PMD.ApexSOQLInjection') | ||||||||||
| global Integer getResultCount() { | ||||||||||
| return Database.countQueryWithBinds( | ||||||||||
| this.getCountQuery(), | ||||||||||
| this.doGetBindsMap(), | ||||||||||
| this.doGetAccessLevel() | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global AggregateResult getFirstResult() { | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>58.0</apiVersion> | ||
| <apiVersion>60.0</apiVersion> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Summer '24 release is now live in all orgs, we could probably bump this to |
||
| <status>Active</status> | ||
| </ApexClass> | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -199,10 +199,18 @@ global class Query extends SOQL { | |||||||||
| return this.filterWhere(new SOQL.QueryField(field), operator, value); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query filterWhere(Schema.SObjectField field, SOQL.Operator operator, Object value, String bindWithKey) { | ||||||||||
| return this.filterWhere(new SOQL.QueryField(field), operator, value, bindWithKey); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value) { | ||||||||||
| return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query filterWhere(SOQL.QueryField queryField, SOQL.Operator operator, Object value, String bindWithKey) { | ||||||||||
| return this.filterWhere(new SOQL.QueryFilter(queryField, operator, value, bindWithKey)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query filterWhere(SOQL.QueryFilter filter) { | ||||||||||
| return this.filterWhere(new List<SOQL.QueryFilter>{ filter }); | ||||||||||
| } | ||||||||||
|
|
@@ -239,6 +247,11 @@ global class Query extends SOQL { | |||||||||
| //return this.setHasChanged(); | ||||||||||
| //} | ||||||||||
|
|
||||||||||
| global Query withAccessLevel(System.AccessLevel mode) { | ||||||||||
| super.doWithAccessLevel(mode); | ||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here - for consistency, I think the variable should be called
Suggested change
|
||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query orderByField(Schema.SObjectField field) { | ||||||||||
| return this.orderByField(new SOQL.QueryField(field)); | ||||||||||
| } | ||||||||||
|
|
@@ -289,6 +302,26 @@ global class Query extends SOQL { | |||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query setBind(String key, Object value) { | ||||||||||
| super.doSetBind(key, value); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query setBinds(Map<String, Object> binds) { | ||||||||||
| super.doSetBinds(binds); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query removeBind(String key) { | ||||||||||
| super.doRemoveBind(key); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| global Query clearBinds() { | ||||||||||
| super.doClearBinds(); | ||||||||||
| return this.setHasChanged(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // TODO decide if this should be global | ||||||||||
| public Query cacheResults() { | ||||||||||
| super.doCacheResults(); | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>58.0</apiVersion> | ||
| <apiVersion>60.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -64,6 +64,11 @@ global class RecordSearch extends SOSL { | |||||
| return this.setHasChanged(); | ||||||
| } | ||||||
|
|
||||||
| global RecordSearch withAccessLevel(System.AccessLevel accessLevel) { | ||||||
| this.accessLevel = accessLevel; | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should call the super class's method right?
Suggested change
|
||||||
| return this.setHasChanged(); | ||||||
| } | ||||||
|
|
||||||
| global RecordSearch updateArticleReporting(SOSL.ArticleReporting articleReporting) { | ||||||
| this.articleReporting = articleReporting; | ||||||
| return this.setHasChanged(); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>58.0</apiVersion> | ||
| <apiVersion>60.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>58.0</apiVersion> | ||
| <apiVersion>60.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this new method overload signature makes sense, especially for trying to keep things backwards compatible with
QueryArgument. But my small complaint is:QueryArgumentSo I'm thinking there could be another method to enable automatically generating bind variable names. Something like this:
And then in this
havingAggregate()overload, auto-generate the bind var name when enabled:Example usage would look something like this:
I think the same idea would apply in the
Queryclass too. Let me know what you think.