Skip to content

Commit

Permalink
Fixed #597, fixed broken joinType comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
stevleibelt authored and Marc J. Schmidt committed Apr 16, 2014
1 parent 3a2467d commit 2475d76
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/Propel/Runtime/ActiveQuery/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function addCondition($left, $right, $operator = self::EQUAL)
* @param array $lefts The left columns of the join condition
* @param array $rights The right columns of the join condition
* @param array $operators The comparison operators of the join condition, default Join::EQUAL
* @throws \Propel\Runtime\Exception\LogicException
*/
public function addConditions($lefts, $rights, $operators = array())
{
Expand Down Expand Up @@ -208,6 +209,7 @@ public function addOperator($operator = null)
}

/**
* @param int $index
* @return string[] the comparison operator for the join condition
*/
public function getOperator($index = 0)
Expand Down Expand Up @@ -306,40 +308,63 @@ public function getLeftColumns()
return $columns;
}

/**
* @param string $leftTableName
* @return $this
*/
public function setLeftTableName($leftTableName)
{
$this->leftTableName = $leftTableName;

return $this;
}

/**
* @return null|string
*/
public function getLeftTableName()
{
return $this->leftTableName;
}

/**
* @param string $leftTableAlias
* @return $this
*/
public function setLeftTableAlias($leftTableAlias)
{
$this->leftTableAlias = $leftTableAlias;

return $this;
}

/**
* @return null|string
*/
public function getLeftTableAlias()
{
return $this->leftTableAlias;
}

/**
* @return bool
*/
public function hasLeftTableAlias()
{
return null !== $this->leftTableAlias;
}

/**
* @return null|string
*/
public function getLeftTableAliasOrName()
{
return $this->leftTableAlias ? $this->leftTableAlias : $this->leftTableName;
}

/**
* @return string
*/
public function getLeftTableWithAlias()
{
return $this->leftTableAlias ? $this->leftTableName . ' ' . $this->leftTableAlias : $this->leftTableName;
Expand Down Expand Up @@ -407,40 +432,63 @@ public function getRightColumns()
return $columns;
}

/**
* @param string $rightTableName
* @return $this
*/
public function setRightTableName($rightTableName)
{
$this->rightTableName = $rightTableName;

return $this;
}

/**
* @return null|string
*/
public function getRightTableName()
{
return $this->rightTableName;
}

/**
* @param string $rightTableAlias
* @return $this
*/
public function setRightTableAlias($rightTableAlias)
{
$this->rightTableAlias = $rightTableAlias;

return $this;
}

/**
* @return null|string
*/
public function getRightTableAlias()
{
return $this->rightTableAlias;
}

/**
* @return bool
*/
public function hasRightTableAlias()
{
return null !== $this->rightTableAlias;
}

/**
* @return null|string
*/
public function getRightTableAliasOrName()
{
return $this->rightTableAlias ? $this->rightTableAlias : $this->rightTableName;
}

/**
* @return string
*/
public function getRightTableWithAlias()
{
return $this->rightTableAlias ? $this->rightTableName . ' ' . $this->rightTableAlias : $this->rightTableName;
Expand Down Expand Up @@ -553,12 +601,20 @@ public function getClause(&$params)
);
}

/**
* @param null|Join $join
* @return bool
*/
public function equals($join)
{
$parametersOfThisClauses = array();
$parametersOfJoinClauses = array();

return null !== $join
&& $join instanceof Join
&& $this->joinType === $join->getJoinType()
&& $this->getJoinType() === $join->getJoinType()
&& $this->getConditions() == $join->getConditions()
&& $this->getClause($parametersOfThisClauses) == $join->getClause($parametersOfJoinClauses)
;
}

Expand All @@ -574,6 +630,9 @@ public function toString()
return $this->getClause($params);
}

/**
* @return string
*/
public function __toString()
{
return $this->toString();
Expand Down
5 changes: 5 additions & 0 deletions src/Propel/Runtime/ActiveQuery/PropelQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
class PropelQuery
{
/**
* @param $queryClassAndAlias
* @return ModelCriteria
* @throws \Propel\Runtime\Exception\ClassNotFoundException
*/
public static function from($queryClassAndAlias)
{
list($class, $alias) = ModelCriteria::getClassAndAlias($queryClassAndAlias);
Expand Down
20 changes: 20 additions & 0 deletions tests/Propel/Tests/Runtime/ActiveQuery/JoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,24 @@ public function testCountConditions()
$this->assertEquals(2, $j->countConditions());
}

public function testEquality()
{
$j1 = new Join('foo', 'bar', 'INNER JOIN');
$this->assertFalse($j1->equals(null), 'Join and null is not equal');

$j2 = new Join('foo', 'bar', 'LEFT JOIN');
$this->assertFalse($j1->equals($j2), 'INNER JOIN and LEFT JOIN are not equal');

$j3 = new Join('foo', 'bar', 'INNER JOIN');
$j3->addCondition('baz.foo', 'baz.bar');
$this->assertFalse($j1->equals($j3), 'Joins with differend conditionsare not equal');

$j4 = new Join('foo', 'bar', 'INNER JOIN');
$j4->addExplicitCondition('book', 'AUTHOR_ID', null, 'author', 'ID', 'a', Join::EQUAL);
$this->assertFalse($j1->equals($j4), 'Joins with differend clauses not equal');

$j5 = new Join('foo', 'bar');
$j6 = new Join('foo', 'bar');
$this->assertTrue($j5->equals($j6), 'Joins without specified join type should be equal as they fallback to default join type');
}
}

0 comments on commit 2475d76

Please sign in to comment.