Commit f57250d1 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'DBAL-172' into 2.3

parents 22a30c9c 9d597cd7
...@@ -946,29 +946,37 @@ class QueryBuilder ...@@ -946,29 +946,37 @@ class QueryBuilder
$query = 'SELECT ' . implode(', ', $this->sqlParts['select']) . ' FROM '; $query = 'SELECT ' . implode(', ', $this->sqlParts['select']) . ' FROM ';
$fromClauses = array(); $fromClauses = array();
$joinsPending = true;
$joinAliases = array();
// Loop through all FROM clauses // Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) { foreach ($this->sqlParts['from'] as $from) {
$fromClause = $from['table'] . ' ' . $from['alias']; $fromClause = $from['table'] . ' ' . $from['alias'];
if (isset($this->sqlParts['join'][$from['alias']])) { if ($joinsPending && isset($this->sqlParts['join'][$from['alias']])) {
foreach ($this->sqlParts['join'][$from['alias']] as $join) { foreach ($this->sqlParts['join'] as $joins) {
$fromClause .= ' ' . strtoupper($join['joinType']) foreach ($joins as $join) {
. ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias'] $fromClause .= ' ' . strtoupper($join['joinType'])
. ' ON ' . ((string) $join['joinCondition']); . ' JOIN ' . $join['joinTable'] . ' ' . $join['joinAlias']
. ' ON ' . ((string) $join['joinCondition']);
$joinAliases[$join['joinAlias']] = true;
}
} }
$joinsPending = false;
} }
$fromClauses[$from['alias']] = $fromClause; $fromClauses[$from['alias']] = $fromClause;
} }
// loop through all JOIN clasues for validation purpose // loop through all JOIN clauses for validation purpose
$knownAliases = array_merge($fromClauses,$joinAliases);
foreach ($this->sqlParts['join'] as $fromAlias => $joins) { foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if ( ! isset($fromClauses[$fromAlias]) ) { if ( ! isset($knownAliases[$fromAlias]) ) {
throw QueryException::unknownFromAlias($fromAlias, array_keys($fromClauses)); throw QueryException::unknownAlias($fromAlias, array_keys($knownAliases));
} }
} }
$query .= implode(', ', $fromClauses) $query .= implode(', ', $fromClauses)
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '') . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '')
. ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '') . ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '')
......
...@@ -29,12 +29,10 @@ use Doctrine\DBAL\DBALException; ...@@ -29,12 +29,10 @@ use Doctrine\DBAL\DBALException;
*/ */
class QueryException extends DBALException 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 " . return new self("The given alias '" . $alias . "' is not part of " .
"any FROM clause table. The currently registered FROM-clause " . "any FROM or JOIN clause table. The currently registered " .
"aliases are: " . implode(", ", $registeredAliases) . ". Join clauses " . "aliases are: " . implode(", ", $registeredAliases) . ".");
"are bound to from clauses to provide support for mixing of multiple " .
"from and join clauses.");
} }
} }
...@@ -556,17 +556,32 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -556,17 +556,32 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
{ {
$qb = new QueryBuilder($this->conn); $qb = new QueryBuilder($this->conn);
$qb->select("l.id", "mdsh.xcode", "mdso.xcode") $qb->select('COUNT(DISTINCT news.id)')
->from("location_tree", "l") ->from('cb_newspages', 'news')
->join("l", "location_tree_pos", "p", "l.id = p.tree_id") ->innerJoin('news', 'nodeversion', 'nv', 'nv.refId = news.id AND nv.refEntityname=\'News\'')
->rightJoin("l", "hotel", "h", "h.location_id = l.id") ->innerJoin('invalid', 'nodetranslation', 'nt', 'nv.nodetranslation = nt.id')
->leftJoin("l", "offer_location", "ol", "l.id=ol.location_id") ->innerJoin('nt', 'node', 'n', 'nt.node = n.id')
->leftJoin("ol", "mds_offer", "mdso", "ol.offer_id = mdso.offer_id") ->where('nt.lang = :lang AND n.deleted != 1');
->leftJoin("h", "mds_hotel", "mdsh", "h.id = mdsh.hotel_id")
->where("p.parent_id IN (:ids)") $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.");
->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");
$this->assertEquals('', $qb->getSQL()); $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());
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment