Skip to content

Commit

Permalink
Merge pull request #3045 from morozov/remove-redundant-alias
Browse files Browse the repository at this point in the history
When rendering SQL, only render the alias if it's different from the table name
  • Loading branch information
Ocramius authored Mar 15, 2018
2 parents 25412cb + 7582225 commit e6e2eea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ private function getFromClauses()

// Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) {
if ($from['alias'] === null) {
if ($from['alias'] === null || $from['alias'] === $from['table']) {
$tableSql = $from['table'];
$tableReference = $from['table'];
} else {
Expand Down Expand Up @@ -1179,7 +1179,14 @@ private function getSQLForInsert()
*/
private function getSQLForUpdate()
{
$table = $this->sqlParts['from']['table'] . ($this->sqlParts['from']['alias'] ? ' ' . $this->sqlParts['from']['alias'] : '');
$from = $this->sqlParts['from'];

if ($from['alias'] === null || $from['alias'] === $from['table']) {
$table = $from['table'];
} else {
$table = $from['table'] . ' ' . $from['alias'];
}

$query = 'UPDATE ' . $table
. ' SET ' . implode(", ", $this->sqlParts['set'])
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '');
Expand All @@ -1194,7 +1201,14 @@ private function getSQLForUpdate()
*/
private function getSQLForDelete()
{
$table = $this->sqlParts['from']['table'] . ($this->sqlParts['from']['alias'] ? ' ' . $this->sqlParts['from']['alias'] : '');
$from = $this->sqlParts['from'];

if ($from['alias'] === null || $from['alias'] === $from['table']) {
$table = $from['table'];
} else {
$table = $from['table'] . ' ' . $from['alias'];
}

$query = 'DELETE FROM ' . $table . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '');

return $query;
Expand Down
29 changes: 29 additions & 0 deletions tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ public function testUpdateWithoutAlias()
self::assertEquals('UPDATE users SET foo = ?, bar = ?', (string) $qb);
}

public function testUpdateWithMatchingAlias()
{
$qb = new QueryBuilder($this->conn);
$qb->update('users', 'users')
->set('foo', '?')
->set('bar', '?');

self::assertEquals('UPDATE users SET foo = ?, bar = ?', (string) $qb);
}

public function testUpdateWhere()
{
$qb = new QueryBuilder($this->conn);
Expand Down Expand Up @@ -447,6 +457,15 @@ public function testDeleteWithoutAlias()
self::assertEquals('DELETE FROM users', (string) $qb);
}

public function testDeleteWithMatchingAlias()
{
$qb = new QueryBuilder($this->conn);
$qb->delete('users', 'users');

self::assertEquals(QueryBuilder::DELETE, $qb->getType());
self::assertEquals('DELETE FROM users', (string) $qb);
}

public function testDeleteWhere()
{
$qb = new QueryBuilder($this->conn);
Expand Down Expand Up @@ -776,6 +795,16 @@ public function testSimpleSelectWithoutTableAlias()
self::assertEquals('SELECT id FROM users', (string) $qb);
}

public function testSimpleSelectWithMatchingTableAlias()
{
$qb = new QueryBuilder($this->conn);

$qb->select('id')
->from('users', 'users');

self::assertEquals('SELECT id FROM users', (string) $qb);
}

public function testSelectWithSimpleWhereWithoutTableAlias()
{
$qb = new QueryBuilder($this->conn);
Expand Down

0 comments on commit e6e2eea

Please sign in to comment.