When rendering SQL, only render the alias if it's different from the table name

parent ee5e4a75
......@@ -1152,7 +1152,7 @@ class QueryBuilder
// 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 {
......@@ -1211,7 +1211,13 @@ class QueryBuilder
*/
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'];
}
return 'UPDATE ' . $table
. ' SET ' . implode(', ', $this->sqlParts['set'])
......@@ -1225,7 +1231,13 @@ class QueryBuilder
*/
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'];
}
return 'DELETE FROM ' . $table . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '');
}
......
......@@ -422,6 +422,16 @@ class QueryBuilderTest extends DbalTestCase
self::assertEquals('UPDATE users SET foo = ?, bar = ?', (string) $qb);
}
public function testUpdateWithMatchingAlias() : void
{
$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() : void
{
$qb = new QueryBuilder($this->conn);
......@@ -459,6 +469,15 @@ class QueryBuilderTest extends DbalTestCase
self::assertEquals('DELETE FROM users', (string) $qb);
}
public function testDeleteWithMatchingAlias() : void
{
$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() : void
{
$qb = new QueryBuilder($this->conn);
......@@ -787,6 +806,16 @@ class QueryBuilderTest extends DbalTestCase
self::assertEquals('SELECT id FROM users', (string) $qb);
}
public function testSimpleSelectWithMatchingTableAlias() : void
{
$qb = new QueryBuilder($this->conn);
$qb->select('id')
->from('users', 'users');
self::assertEquals('SELECT id FROM users', (string) $qb);
}
public function testSelectWithSimpleWhereWithoutTableAlias() : void
{
$qb = new QueryBuilder($this->conn);
......
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