Skip to content

Commit

Permalink
PRESIDECMS-2974 fix for double join inclusion and errors with agg: fi…
Browse files Browse the repository at this point in the history
…elds

When including the agg formula field in selectFields AND also ordering
on that field (i.e. when using a data grid), this causes the query
to fail due to the double include of the agg join.

Ensure it is only every included the once.
  • Loading branch information
DominicWatson committed Nov 25, 2024
1 parent 7a7272a commit 9c4eb1a
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions system/services/presideObjects/PresideObjectService.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ component displayName="Preside Object Service" {

args.adapter = adapter;
args.objMeta = objMeta;
args.orderBy = arguments.recordCountOnly ? "" : _parseOrderBy( args.orderBy, args.objectName, args.adapter, args.filterParams, args.extraJoins );
args.orderBy = arguments.recordCountOnly ? "" : _parseOrderBy( args.orderBy, args.objectName, args.adapter, args.filterParams, args.extraJoins, args.selectFields );
args.groupBy = _autoPrefixBareProperty( args.objectName, args.groupBy, args.adapter );
if ( !Len( Trim( args.groupBy ) ) && args.autoGroupBy ) {
args.groupBy = _autoCalculateGroupBy( args.selectFields, args.objectName, args.adapter );
Expand Down Expand Up @@ -3512,10 +3512,22 @@ component displayName="Preside Object Service" {
, joinToTable = joinToTable
, joinToColumn = joinToColumn
, type = "left"
, aggJoinFor = alias
} );
}
}

private boolean function _aggJoinExists( joins, propertyName, dbAdapter ) {
var escapedPropName = arguments.dbAdapter.escapeEntity( arguments.propertyName );
for( var join in arguments.joins ) {
if ( StructKeyExists( join, "aggJoinFor" ) && ( join.aggJoinFor == escapedPropName || join.aggJoinFor == arguments.propertyName ) ) {
return true;
}
}

return false;
}

private string function _optimiseAggregateFunctions( required string formula ) {
var optimised = arguments.formula;

Expand Down Expand Up @@ -3784,8 +3796,8 @@ component displayName="Preside Object Service" {
return REReplaceNoCase( Trim( text ), '\bas\b\s+(\w+)(?!\s*[`\"\[])$', "as #dbAdapter.escapeEntity( "\1" )#" );
}

private string function _parseOrderBy( required string orderBy, required string objectName, required any dbAdapter, required struct filterParams, required array extraJoins ) {
var items = arguments.orderBy.listToArray();
private string function _parseOrderBy( required string orderBy, required string objectName, required any dbAdapter, required struct filterParams, required array extraJoins, required array selectFields ) {
var items = ListToArray( arguments.orderBy );
var rebuilt = [];
var aliased = "";
var propertyName = "";
Expand All @@ -3797,22 +3809,28 @@ component displayName="Preside Object Service" {
direction = ListLen( item, " " ) > 1 ? " " & ListRest( item, " ") : "";

if ( left( propertyName, 4 ) == "agg:" ) {
aggregateArgs = {
selectFields = [ "#propertyName# as _placeholder" ]
, objectName = arguments.objectName
, filterParams = arguments.filterParams
, extraJoins = arguments.extraJoins
};
_prepareAggregateFormulaFields( aggregateArgs );
var rawPropName = Trim( ListFirst( item, " " ) );

if ( _aggJoinExists( arguments.extraJoins, rawPropName, dbAdapter ) ) {
item = rawPropName & direction;
} else {
aggregateArgs = {
selectFields = [ "#propertyName# as _placeholder" ]
, objectName = arguments.objectName
, filterParams = arguments.filterParams
, extraJoins = arguments.extraJoins
};
_prepareAggregateFormulaFields( aggregateArgs );
propertyName = ReReplaceNoCase( aggregateArgs.selectFields[ 1 ], " as _placeholder$", "" );
item = propertyName & direction;
}

propertyName = ReReplaceNoCase( aggregateArgs.selectFields[ 1 ], " as _placeholder$", "" );
item = propertyName & direction;
} else {
aliased = _autoPrefixBareProperty( arguments.objectName, propertyName, arguments.dbAdapter );
item = aliased & direction;
}

rebuilt.append( Trim( item ) );
ArrayAppend( rebuilt, Trim( item ) );
}

return rebuilt.toList( ", " );
Expand Down

0 comments on commit 9c4eb1a

Please sign in to comment.