Skip to content

Commit

Permalink
Implement group bys
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Nov 29, 2016
1 parent ed65e09 commit 73f961d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
24 changes: 1 addition & 23 deletions ModuleConfig.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,8 @@ component {
this.autoMapModels = true;
this.cfmapping = 'Quick';

variables.defaultSettings = {
defaultGrammar = 'OracleGrammar'
};

function configure() {
var settings = controller.getConfigSettings();

parseParentSettings(settings);

binder.map('Grammar@Quick')
.to('#moduleMapping#.models.Query.Grammars.#settings.quick.defaultGrammar#');
}

private void function parseParentSettings(required struct settings) {
if (! structKeyExists(settings, 'quick')) {
settings.quick = {};
}

var userSettings = controller.getSetting('ColdBoxConfig')
.getPropertyMixin('quick', 'variables', structNew());

structAppend(settings.quick, variables.defaultSettings);

structAppend(settings.quick, userSettings, true);
//
}

}
22 changes: 22 additions & 0 deletions models/Query/Builder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ component displayname="Builder" accessors="true" {
property name="from" type="string";
property name="joins" type="array";
property name="wheres" type="array";
property name="groups" type="array";

variables.operators = [
"=", "<", ">", "<=", ">=", "<>", "!=",
Expand Down Expand Up @@ -41,6 +42,7 @@ component displayname="Builder" accessors="true" {
variables.joins = [];
variables.from = "";
variables.wheres = [];
variables.groups = [];
}

// API
Expand Down Expand Up @@ -465,6 +467,26 @@ component displayname="Builder" accessors="true" {
return condition ? onTrue( this ) : onFalse( this );
}

// group by

public Builder function groupBy() {
// This block is necessary for ACF 10.
// It can't be extracted to a function because
// the arguments struct doesn't get passed correctly.
var args = {};
var count = structCount( arguments );
for ( var arg in arguments ) {
args[ count ] = arguments[ arg ];
count--;
}

var groupBys = normalizeToArray( argumentCollection = args );
groupBys.each( function( groupBy ) {
variables.groups.append( groupBy );
} );
return this;
}

public Builder function newQuery() {
return new Quick.models.Query.Builder( grammar = getGrammar() );
}
Expand Down
10 changes: 9 additions & 1 deletion models/Query/Grammars/Grammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ component displayname="Grammar" accessors="true" {
property name="tablePrefix" type="string" default="";

variables.selectComponents = [
"columns", "from", "joins", "wheres"
"columns", "from", "joins", "wheres", "groups"
];

public Grammar function init() {
Expand Down Expand Up @@ -159,6 +159,14 @@ component displayname="Grammar" accessors="true" {
return "#wrapColumn( where.column )# NOT IN (#compileSelect( where.query )#)";
}

private string function compileGroups( required Builder query, required array groups ) {
if ( groups.isEmpty() ) {
return "";
}

return "GROUP BY #groups.map( wrapColumn ).toList( ", " )#";
}

private string function concatenate( required array sql ) {
return arrayToList( arrayFilter( sql, function( item ) {
return item != "";
Expand Down

0 comments on commit 73f961d

Please sign in to comment.