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

Improve context identification reducing limits as much as possible but giving up backwards compat #244

70 changes: 41 additions & 29 deletions rolluptool/src/classes/LREngine.cls
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,12 @@ public class LREngine {

// #0 token : SOQL projection
String soqlProjection = ctx.lookupField.getName();
List<String> orderByFields = new List<String>();
orderByFields.add(ctx.lookupField.getName()); // ensure details records are ordered by parent record

// k: detail field name, v: master field name
Integer exprIdx = 0;
Boolean needsCurrency = false;
Boolean builtAggregateQuery = false;
Set<String> selectedFields = new Set<String>();
Set<String> orderByFieldsSet = new Set<String>();
Map<String, RollupSummaryField> rsfByAlais = new Map<String, RollupSummaryField>();
for (RollupSummaryField rsf : ctx.fieldsToRoll) {
if(rsf.operation == RollupOperation.Sum ||
Expand All @@ -161,14 +158,6 @@ public class LREngine {
soqlProjection += ', ' + selectField;
selectedFields.add(selectField);
}
// create order by projections
// i.e. Amount ASC NULLS FIRST
String orderByField =
rsf.detailOrderBy!=null ? rsf.detailOrderBy.getName() : rsf.detail.getName();
if(!orderByFieldsSet.contains(orderByField)) {
orderByFields.add(orderByField);
orderByFieldsSet.add(orderByField);
}
}
}

Expand All @@ -191,6 +180,10 @@ public class LREngine {
// #3 Group by field
String grpByFld = ctx.lookupField.getName();

// #4 Order by clause fields
// i.e. Amount ASC NULLS FIRST, Name DESC NULL LAST
String orderByClause = ctx.lookupField.getName() + (String.isBlank(ctx.detailOrderByClause) ? '' : (',' + ctx.detailOrderByClause));

// build approprite soql for this rollup context
String soql =
builtAggregateQuery ?
Expand All @@ -206,7 +199,7 @@ public class LREngine {
detailTblName,
ctx.lookupField.getName(),
whereClause,
String.join(orderByFields, ',')});
orderByClause});
System.debug('SOQL is ' + soql);

// validate only?
Expand Down Expand Up @@ -367,7 +360,6 @@ public class LREngine {
public class RollupSummaryField {
public Schema.Describefieldresult master;
public Schema.Describefieldresult detail;
public Schema.Describefieldresult detailOrderBy;
public RollupOperation operation;
public String concatenateDelimiter;

Expand All @@ -383,17 +375,15 @@ public class LREngine {

public RollupSummaryField(Schema.Describefieldresult m,
Schema.Describefieldresult d, RollupOperation op) {
this(m, d, null, op, null);
this(m, d, op, null);
}

public RollupSummaryField(Schema.Describefieldresult m,
Schema.Describefieldresult d,
Schema.Describefieldresult detailOrderBy,
Schema.Describefieldresult d,
RollupOperation op,
String concatenateDelimiter) {
this.master = m;
this.detail = d;
this.detailOrderBy = detailOrderBy;
this.operation = op;
this.concatenateDelimiter = concatenateDelimiter;
// caching these derived attrbutes for once
Expand Down Expand Up @@ -469,19 +459,11 @@ public class LREngine {
}

public boolean isAggregateBasedRollup() {
return operation == RollupOperation.Sum ||
operation == RollupOperation.Min ||
operation == RollupOperation.Max ||
operation == RollupOperation.Avg ||
operation == RollupOperation.Count ||
operation == RollupOperation.Count_Distinct;
return isAggregateBasedRollup(operation);
}

public boolean isQueryBasedRollup() {
return operation == RollupOperation.Concatenate ||
operation == RollupOperation.Concatenate_Distinct ||
operation == RollupOperation.First ||
operation == RollupOperation.Last;
return isQueryBasedRollup(operation);
}
}

Expand All @@ -490,6 +472,22 @@ public class LREngine {
System_x
}

public static boolean isAggregateBasedRollup(RollupOperation operation) {
return operation == RollupOperation.Sum ||
operation == RollupOperation.Min ||
operation == RollupOperation.Max ||
operation == RollupOperation.Avg ||
operation == RollupOperation.Count ||
operation == RollupOperation.Count_Distinct;
}

public static boolean isQueryBasedRollup(RollupOperation operation) {
return operation == RollupOperation.Concatenate ||
operation == RollupOperation.Concatenate_Distinct ||
operation == RollupOperation.First ||
operation == RollupOperation.Last;
}

/**
Context having all the information about the rollup to be done.
Please note : This class encapsulates many rollup summary fields with different operations.
Expand All @@ -511,23 +509,37 @@ public class LREngine {

// Where clause or filters to apply while aggregating detail records
public String detailWhereClause;


// Order By clause to apply while aggregating detail records
public String detailOrderByClause;

public Context(Schema.Sobjecttype m, Schema.Sobjecttype d,
Schema.Describefieldresult lf) {
this(m, d, lf, '');
}

public Context(Schema.Sobjecttype m, Schema.Sobjecttype d,
Schema.Describefieldresult lf, String detailWhereClause) {
this(m, d, lf, detailWhereClause, null);
this(m, d, lf, detailWhereClause, (SharingMode)null);
}

public Context(Schema.Sobjecttype m, Schema.Sobjecttype d,
Schema.Describefieldresult lf, String detailWhereClause, SharingMode sharingMode) {
this(m, d, lf, detailWhereClause, sharingMode, null);
}

public Context(Schema.Sobjecttype m, Schema.Sobjecttype d,
Schema.Describefieldresult lf, String detailWhereClause, String detailOrderByClause) {
this(m, d, lf, detailWhereClause, null, detailOrderByClause);
}

public Context(Schema.Sobjecttype m, Schema.Sobjecttype d,
Schema.Describefieldresult lf, String detailWhereClause, SharingMode sharingMode, String detailOrderByClause) {
this.master = m;
this.detail = d;
this.lookupField = lf;
this.detailWhereClause = detailWhereClause;
this.detailOrderByClause = detailOrderByClause;
this.fieldsToRoll = new List<RollupSummaryField>();
this.sharingMode = sharingMode;
}
Expand Down
Loading