Commit d686e5d9 authored by Marco Pivetta's avatar Marco Pivetta

Merge pull request #646 from JeroenDeDauw/noalias

Make the $alias parameter in the `from` method optional
parents 663135d5 d56cafa0
...@@ -584,11 +584,11 @@ class QueryBuilder ...@@ -584,11 +584,11 @@ class QueryBuilder
* </code> * </code>
* *
* @param string $from The table. * @param string $from The table.
* @param string $alias The alias of the table. * @param string|null $alias The alias of the table.
* *
* @return \Doctrine\DBAL\Query\QueryBuilder This QueryBuilder instance. * @return QueryBuilder This QueryBuilder instance.
*/ */
public function from($from, $alias) public function from($from, $alias = null)
{ {
return $this->add('from', array( return $this->add('from', array(
'table' => $from, 'table' => $from,
...@@ -1087,33 +1087,71 @@ class QueryBuilder ...@@ -1087,33 +1087,71 @@ class QueryBuilder
{ {
$query = 'SELECT ' . implode(', ', $this->sqlParts['select']) . ' FROM '; $query = 'SELECT ' . implode(', ', $this->sqlParts['select']) . ' FROM ';
$query .= implode(', ', $this->getFromClauses())
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '')
. ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '')
. ($this->sqlParts['having'] !== null ? ' HAVING ' . ((string) $this->sqlParts['having']) : '')
. ($this->sqlParts['orderBy'] ? ' ORDER BY ' . implode(', ', $this->sqlParts['orderBy']) : '');
if ($this->isLimitQuery()) {
return $this->connection->getDatabasePlatform()->modifyLimitQuery(
$query,
$this->maxResults,
$this->firstResult
);
}
return $query;
}
/**
* @return string[]
*/
private function getFromClauses()
{
$fromClauses = array(); $fromClauses = array();
$knownAliases = array(); $knownAliases = array();
// Loop through all FROM clauses // Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) { foreach ($this->sqlParts['from'] as $from) {
$knownAliases[$from['alias']] = true; if ($from['alias'] === null) {
$fromClause = $from['table'] . ' ' . $from['alias'] $tableSql = $from['table'];
. $this->getSQLForJoins($from['alias'], $knownAliases); $tableReference = $from['table'];
} else {
$tableSql = $from['table'] . ' ' . $from['alias'];
$tableReference = $from['alias'];
}
$fromClauses[$from['alias']] = $fromClause; $knownAliases[$tableReference] = true;
$fromClauses[$tableReference] = $tableSql . $this->getSQLForJoins($tableReference, $knownAliases);
} }
$this->verifyAllAliasesAreKnown($knownAliases);
return $fromClauses;
}
/**
* @param array $knownAliases
*
* @throws QueryException
*/
private function verifyAllAliasesAreKnown(array $knownAliases)
{
foreach ($this->sqlParts['join'] as $fromAlias => $joins) { foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if ( ! isset($knownAliases[$fromAlias])) { if ( ! isset($knownAliases[$fromAlias])) {
throw QueryException::unknownAlias($fromAlias, array_keys($knownAliases)); throw QueryException::unknownAlias($fromAlias, array_keys($knownAliases));
} }
} }
}
$query .= implode(', ', $fromClauses) /**
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '') * @return bool
. ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '') */
. ($this->sqlParts['having'] !== null ? ' HAVING ' . ((string) $this->sqlParts['having']) : '') private function isLimitQuery()
. ($this->sqlParts['orderBy'] ? ' ORDER BY ' . implode(', ', $this->sqlParts['orderBy']) : ''); {
return $this->maxResults !== null || $this->firstResult !== null;
return ($this->maxResults === null && $this->firstResult == null)
? $query
: $this->connection->getDatabasePlatform()->modifyLimitQuery($query, $this->maxResults, $this->firstResult);
} }
/** /**
......
...@@ -694,4 +694,53 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -694,4 +694,53 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertFalse($qb->getQueryParts() === $qb_clone->getQueryParts()); $this->assertFalse($qb->getQueryParts() === $qb_clone->getQueryParts());
$this->assertFalse($qb->getParameters() === $qb_clone->getParameters()); $this->assertFalse($qb->getParameters() === $qb_clone->getParameters());
} }
public function testSimpleSelectWithoutTableAlias()
{
$qb = new QueryBuilder($this->conn);
$qb->select('id')
->from('users');
$this->assertEquals('SELECT id FROM users', (string) $qb);
}
public function testSelectWithSimpleWhereWithoutTableAlias()
{
$qb = new QueryBuilder($this->conn);
$qb->select('id', 'name')
->from('users')
->where('awesome=9001');
$this->assertEquals("SELECT id, name FROM users WHERE awesome=9001", (string) $qb);
}
public function testComplexSelectWithoutTableAliases()
{
$qb = new QueryBuilder($this->conn);
$qb->select('DISTINCT users.id')
->from('users')
->from('articles')
->innerJoin('users', 'permissions', 'p', 'p.user_id = users.id')
->innerJoin('articles', 'comments', 'c', 'c.article_id = articles.id')
->where('users.id = articles.user_id')
->andWhere('p.read = 1');
$this->assertEquals('SELECT DISTINCT users.id FROM users INNER JOIN permissions p ON p.user_id = users.id, articles INNER JOIN comments c ON c.article_id = articles.id WHERE (users.id = articles.user_id) AND (p.read = 1)', $qb->getSQL());
}
public function testComplexSelectWithSomeTableAliases()
{
$qb = new QueryBuilder($this->conn);
$qb->select('u.id')
->from('users', 'u')
->from('articles')
->innerJoin('u', 'permissions', 'p', 'p.user_id = u.id')
->innerJoin('articles', 'comments', 'c', 'c.article_id = articles.id');
$this->assertEquals('SELECT u.id FROM users u INNER JOIN permissions p ON p.user_id = u.id, articles INNER JOIN comments c ON c.article_id = articles.id', $qb->getSQL());
}
} }
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