Skip to content

Commit

Permalink
Allow the shortcut where statement in joins.
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed May 6, 2016
1 parent ad911fa commit 28fc828
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion models/Query/JoinClause.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ component displayname='JoinClause' {
return on(argumentCollection = arguments);
}

public JoinClause function where(first, operator, second, combinator = 'and') {
public JoinClause function where(first, operator, second, combinator) {
arguments.where = true;

// If we only receive the first two arguments, this is the shortcut where statement
if (! structKeyExists(arguments, 'second')) {
arguments.second = arguments.operator;
arguments.operator = '=';
}

return on(argumentCollection = arguments);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/specs/Query/Builder/JoinClauseSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ component extends='testbox.system.BaseSpec' {
expect(clause.where).toBe(true, 'The where flag should be true');
});

it('can use the shortcut where statement when the operator is equals (=)', function() {
join.where('second.locale', 'en-US');

var clauses = join.getClauses();
expect(arrayLen(clauses)).toBe(1, 'Only one clause should exist in the join statement');

var clause = clauses[1];
expect(clause.first).toBe('second.locale', 'First column should be [second.locale]');
expect(clause.operator).toBe('=', 'Operator should be [>=]');
expect(clause.second).toBe('?', 'First column should be a binding placeholder [?]');
expect(clause.combinator).toBe('and');
expect(clause.where).toBe(true, 'The where flag should be true');
});

it('adds the where value to the bindings', function() {
join.where('second.locale', '=', 'en-US');

Expand Down

0 comments on commit 28fc828

Please sign in to comment.