Skip to content

Commit

Permalink
accept predicate only join clause
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Apr 20, 2016
1 parent 856bc7a commit d4e6b64
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
27 changes: 17 additions & 10 deletions src/Assembler/Segments/ClauseAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,24 @@ public function assemble()

public function assembleJoinClause(JoinClause $clause)
{
return $clause->getAction() . ' '
. $this->assembleSegment($clause->getTable()) . ' ON '
. $this->assembleSegment($clause->getSource()) . ' = '
. $this->assembleSegment($clause->getDestination())
. ($clause->hasPredicates() ?
' AND ' .
implode(
$clause->getGlue(),
$segments = [];
$source = $clause->getSource();
$destination = $clause->getDestination();
if($source && $destination)
{
$segments[] = $this->assembleSegment($source)
. ' = ' . $this->assembleSegment($destination);
}
if($clause->hasPredicates())
{
$segments = array_merge(
$segments,
$this->assembleSegments($clause->getPredicates())
)
: '');
);
}
return $clause->getAction() . ' '
. $this->assembleSegment($clause->getTable())
. ' ON ' . implode($clause->getGlue(), $segments);
}

public function assembleInsertClause(InsertClause $clause)
Expand Down
15 changes: 15 additions & 0 deletions src/Builder/Traits/JoinTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ public function join($table, $sourceField, $destField = null, $where = null)
$this->addClause($join);
return $this;
}

public function joinWithPredicates($table, ...$predicates)
{
if(empty($predicates))
{
throw new \RuntimeException('No predicates specified for join');
}

$join = new JoinClause();
$join->setTableName($table);
$join->setPredicates(WhereClause::buildPredicates($predicates, $table));

$this->addClause($join);
return $this;
}
}
16 changes: 12 additions & 4 deletions src/Clause/JoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class JoinClause extends AbstractPredicateClause
{
protected $_table;
protected $_src = [null, null];
protected $_dest = [null, null];
protected $_src = null;
protected $_dest = null;

public function setTableName($table)
{
Expand Down Expand Up @@ -39,12 +39,20 @@ public function setDestinationField($table, $field)

public function getSource()
{
return FieldExpression::createWithTable($this->_src[1], $this->_src[0]);
if(is_array($this->_src))
{
return FieldExpression::createWithTable($this->_src[1], $this->_src[0]);
}
return $this->_src;
}

public function getDestination()
{
return FieldExpression::createWithTable($this->_dest[1], $this->_dest[0]);
if(is_array($this->_dest))
{
return FieldExpression::createWithTable($this->_dest[1], $this->_dest[0]);
}
return $this->_dest;
}

/**
Expand Down
35 changes: 31 additions & 4 deletions tests/Builder/Traits/JoinTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use Packaged\QueryBuilder\Assembler\QueryAssembler;
use Packaged\QueryBuilder\Builder\Traits\JoinTrait;
use Packaged\QueryBuilder\Clause\FromClause;
use Packaged\QueryBuilder\Expression\FieldExpression;
use Packaged\QueryBuilder\Expression\TableExpression;
use Packaged\QueryBuilder\Predicate\EqualPredicate;
use Packaged\QueryBuilder\Statement\AbstractStatement;

class JoinTraitTest extends \PHPUnit_Framework_TestCase
Expand All @@ -24,15 +26,40 @@ public function testCreate()
. 'JOIN tbl3 ON tbl.user = tbl3.user_id',
QueryAssembler::stringify($class)
);
$class->joinWithPredicates(
'tbl4',
EqualPredicate::create(
FieldExpression::createWithTable('email', 'tbl2'),
FieldExpression::createWithTable('email', 'tbl4')
)
);
$this->assertEquals(
'FROM tbl JOIN tbl2 ON tbl.email = tbl2.email '
. 'JOIN tbl3 ON tbl.user = tbl3.user_id '
. 'JOIN tbl4 ON tbl2.email = tbl4.email',
QueryAssembler::stringify($class)
);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage No predicates specified for join
*/
public function testNoPredicates()
{
$class = new FinalJoinTrait();
$class->addClause((new FromClause())->setTable('tbl'));
$class->joinWithPredicates('tbl2');
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to join on a statement without a from
* clause being specified
*/
public function testException()
{
$class = new FinalJoinTrait();
$this->setExpectedException(
'RuntimeException',
"Unable to join on a statement without a from clause being specified"
);
$class->join('tbl', 'email');
}
}
Expand Down

0 comments on commit d4e6b64

Please sign in to comment.