Skip to content

Commit

Permalink
Merge pull request #175 from wimvds/DBAL-172
Browse files Browse the repository at this point in the history
Fix for DBAL-172
  • Loading branch information
beberlei committed Dec 17, 2012
2 parents 74c7616 + edf2843 commit 6ace93c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
37 changes: 22 additions & 15 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Expand Up @@ -946,29 +946,36 @@ private function getSQLForSelect()
$query = 'SELECT ' . implode(', ', $this->sqlParts['select']) . ' FROM ';

$fromClauses = array();

$joinsPending = true;
$joinAliases = array();

// Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) {
$fromClause = $from['table'] . ' ' . $from['alias'];

if (isset($this->sqlParts['join'][$from['alias']])) {
foreach ($this->sqlParts['join'][$from['alias']] as $join) {
$fromClause .= ' ' . strtoupper($join['joinType'])
. ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
. ' ON ' . ((string) $join['joinCondition']);
}
}

if ($joinsPending && isset($this->sqlParts['join'][$from['alias']])) {
foreach ($this->sqlParts['join'] as $joins) {
foreach ($joins as $join) {
$fromClause .= ' ' . strtoupper($join['joinType'])
. ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
. ' ON ' . ((string) $join['joinCondition']);
$joinAliases[$join['joinAlias']] = true;
}
}
$joinsPending = false;
}

$fromClauses[$from['alias']] = $fromClause;
}

// loop through all JOIN clauses for validation purpose
foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if ( ! isset($fromClauses[$fromAlias]) ) {
throw QueryException::unknownFromAlias($fromAlias, array_keys($fromClauses));
}
}

$knownAliases = array_merge($fromClauses,$joinAliases);
foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if ( ! isset($knownAliases[$fromAlias]) ) {
throw QueryException::unknownAlias($fromAlias, array_keys($knownAliases));
}
}

$query .= implode(', ', $fromClauses)
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '')
. ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '')
Expand Down
8 changes: 3 additions & 5 deletions lib/Doctrine/DBAL/Query/QueryException.php
Expand Up @@ -29,12 +29,10 @@
*/
class QueryException extends DBALException
{
static public function unknownFromAlias($alias, $registeredAliases)
static public function unknownAlias($alias, $registeredAliases)
{
return new self("The given alias '" . $alias . "' is not part of " .
"any FROM clause table. The currently registered FROM-clause " .
"aliases are: " . implode(", ", $registeredAliases) . ". Join clauses " .
"are bound to from clauses to provide support for mixing of multiple " .
"from and join clauses.");
"any FROM or JOIN clause table. The currently registered " .
"aliases are: " . implode(", ", $registeredAliases) . ".");
}
}
37 changes: 26 additions & 11 deletions tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
Expand Up @@ -556,17 +556,32 @@ public function testReferenceJoinFromJoin()
{
$qb = new QueryBuilder($this->conn);

$qb->select("l.id", "mdsh.xcode", "mdso.xcode")
->from("location_tree", "l")
->join("l", "location_tree_pos", "p", "l.id = p.tree_id")
->rightJoin("l", "hotel", "h", "h.location_id = l.id")
->leftJoin("l", "offer_location", "ol", "l.id=ol.location_id")
->leftJoin("ol", "mds_offer", "mdso", "ol.offer_id = mdso.offer_id")
->leftJoin("h", "mds_hotel", "mdsh", "h.id = mdsh.hotel_id")
->where("p.parent_id IN (:ids)")
->andWhere("(mdso.xcode IS NOT NULL OR mdsh.xcode IS NOT NULL)");

$this->setExpectedException('Doctrine\DBAL\Query\QueryException', "The given alias 'ol' is not part of any FROM clause table. The currently registered FROM-clause aliases are: l");
$qb->select('COUNT(DISTINCT news.id)')
->from('cb_newspages', 'news')
->innerJoin('news', 'nodeversion', 'nv', 'nv.refId = news.id AND nv.refEntityname=\'News\'')
->innerJoin('invalid', 'nodetranslation', 'nt', 'nv.nodetranslation = nt.id')
->innerJoin('nt', 'node', 'n', 'nt.node = n.id')
->where('nt.lang = :lang AND n.deleted != 1');

$this->setExpectedException('Doctrine\DBAL\Query\QueryException', "The given alias 'invalid' is not part of any FROM or JOIN clause table. The currently registered aliases are: news, nv, nt, n.");
$this->assertEquals('', $qb->getSQL());
}

/**
* @group DBAL-172
*/
public function testSelectFromMasterWithWhereOnJoinedTables()
{
$qb = new QueryBuilder($this->conn);

$qb->select('COUNT(DISTINCT news.id)')
->from('newspages', 'news')
->innerJoin('news', 'nodeversion', 'nv', "nv.refId = news.id AND nv.refEntityname='Entity\\News'")
->innerJoin('nv', 'nodetranslation', 'nt', 'nv.nodetranslation = nt.id')
->innerJoin('nt', 'node', 'n', 'nt.node = n.id')
->where('nt.lang = ?')
->andWhere('n.deleted = 0');

$this->assertEquals("SELECT COUNT(DISTINCT news.id) FROM newspages news INNER JOIN nodeversion nv ON nv.refId = news.id AND nv.refEntityname='Entity\\News' INNER JOIN nodetranslation nt ON nv.nodetranslation = nt.id INNER JOIN node n ON nt.node = n.id WHERE (nt.lang = ?) AND (n.deleted = 0)", $qb->getSQL());
}
}

0 comments on commit 6ace93c

Please sign in to comment.