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

parent 77a76d86
...@@ -1128,7 +1128,7 @@ class QueryBuilder ...@@ -1128,7 +1128,7 @@ class QueryBuilder
// Loop through all FROM clauses // Loop through all FROM clauses
foreach ($this->sqlParts['from'] as $from) { foreach ($this->sqlParts['from'] as $from) {
if ($from['alias'] === null) { if ($from['alias'] === null || $from['alias'] === $from['table']) {
$tableSql = $from['table']; $tableSql = $from['table'];
$tableReference = $from['table']; $tableReference = $from['table'];
} else { } else {
...@@ -1187,7 +1187,14 @@ class QueryBuilder ...@@ -1187,7 +1187,14 @@ class QueryBuilder
*/ */
private function getSQLForUpdate() 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 return 'UPDATE ' . $table
. ' SET ' . implode(', ', $this->sqlParts['set']) . ' SET ' . implode(', ', $this->sqlParts['set'])
. ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : ''); . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '');
...@@ -1200,7 +1207,14 @@ class QueryBuilder ...@@ -1200,7 +1207,14 @@ class QueryBuilder
*/ */
private function getSQLForDelete() 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']) : ''); return 'DELETE FROM ' . $table . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '');
} }
......
...@@ -411,6 +411,16 @@ class QueryBuilderTest extends DbalTestCase ...@@ -411,6 +411,16 @@ class QueryBuilderTest extends DbalTestCase
self::assertEquals('UPDATE users SET foo = ?, bar = ?', (string) $qb); 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() public function testUpdateWhere()
{ {
$qb = new QueryBuilder($this->conn); $qb = new QueryBuilder($this->conn);
...@@ -448,6 +458,15 @@ class QueryBuilderTest extends DbalTestCase ...@@ -448,6 +458,15 @@ class QueryBuilderTest extends DbalTestCase
self::assertEquals('DELETE FROM users', (string) $qb); 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() public function testDeleteWhere()
{ {
$qb = new QueryBuilder($this->conn); $qb = new QueryBuilder($this->conn);
...@@ -776,6 +795,16 @@ class QueryBuilderTest extends DbalTestCase ...@@ -776,6 +795,16 @@ class QueryBuilderTest extends DbalTestCase
self::assertEquals('SELECT id FROM users', (string) $qb); 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() public function testSelectWithSimpleWhereWithoutTableAlias()
{ {
$qb = new QueryBuilder($this->conn); $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