Skip to content

Commit 9bb0e7c

Browse files
committed
Throw QueryException on improperly initialized from clause
1 parent ec4e1b9 commit 9bb0e7c

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

lib/Doctrine/DBAL/Query/QueryBuilder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,16 @@ private function getFromClauses()
11761176
return $fromClauses;
11771177
}
11781178

1179+
/**
1180+
* @throws QueryException
1181+
*/
1182+
private function assertHasSingleFromClause(): void
1183+
{
1184+
if (! array_key_exists('table', $this->sqlParts['from'])) {
1185+
throw QueryException::uninitializedTableName();
1186+
}
1187+
}
1188+
11791189
/**
11801190
* @param string[] $knownAliases
11811191
*
@@ -1202,9 +1212,13 @@ private function isLimitQuery()
12021212
* Converts this instance into an INSERT string in SQL.
12031213
*
12041214
* @return string
1215+
*
1216+
* @throws QueryException
12051217
*/
12061218
private function getSQLForInsert()
12071219
{
1220+
$this->assertHasSingleFromClause();
1221+
12081222
return 'INSERT INTO ' . $this->sqlParts['from']['table'] .
12091223
' (' . implode(', ', array_keys($this->sqlParts['values'])) . ')' .
12101224
' VALUES(' . implode(', ', $this->sqlParts['values']) . ')';
@@ -1214,9 +1228,13 @@ private function getSQLForInsert()
12141228
* Converts this instance into an UPDATE string in SQL.
12151229
*
12161230
* @return string
1231+
*
1232+
* @throws QueryException
12171233
*/
12181234
private function getSQLForUpdate()
12191235
{
1236+
$this->assertHasSingleFromClause();
1237+
12201238
$table = $this->sqlParts['from']['table']
12211239
. ($this->sqlParts['from']['alias'] ? ' ' . $this->sqlParts['from']['alias'] : '');
12221240

@@ -1229,9 +1247,13 @@ private function getSQLForUpdate()
12291247
* Converts this instance into a DELETE string in SQL.
12301248
*
12311249
* @return string
1250+
*
1251+
* @throws QueryException
12321252
*/
12331253
private function getSQLForDelete()
12341254
{
1255+
$this->assertHasSingleFromClause();
1256+
12351257
$table = $this->sqlParts['from']['table']
12361258
. ($this->sqlParts['from']['alias'] ? ' ' . $this->sqlParts['from']['alias'] : '');
12371259

lib/Doctrine/DBAL/Query/QueryException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public static function nonUniqueAlias($alias, $registeredAliases)
3636
'in FROM and JOIN clause table. The currently registered ' .
3737
'aliases are: ' . implode(', ', $registeredAliases) . '.');
3838
}
39+
40+
public static function uninitializedTableName(): self
41+
{
42+
return new self('There is no currently registered table name.');
43+
}
3944
}

tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,18 @@ public function testEmptyUpdate(): void
462462
self::assertSame($qb2, $qb);
463463
}
464464

465+
public function testUpdateImproperFromThrowsException(): void
466+
{
467+
$qb = new QueryBuilder($this->conn);
468+
$qb->update()
469+
->from('users');
470+
$this->expectException(QueryException::class);
471+
$this->expectExceptionMessage(
472+
'There is no currently registered table name.'
473+
);
474+
self::assertEquals('', $qb->getSQL());
475+
}
476+
465477
public function testDelete(): void
466478
{
467479
$qb = new QueryBuilder($this->conn);
@@ -489,6 +501,18 @@ public function testDeleteWhere(): void
489501
self::assertEquals('DELETE FROM users u WHERE u.foo = ?', (string) $qb);
490502
}
491503

504+
public function testDeleteImproperFromThrowsException(): void
505+
{
506+
$qb = new QueryBuilder($this->conn);
507+
$qb->delete()
508+
->from('users');
509+
$this->expectException(QueryException::class);
510+
$this->expectExceptionMessage(
511+
'There is no currently registered table name.'
512+
);
513+
self::assertEquals('', $qb->getSQL());
514+
}
515+
492516
public function testEmptyDelete(): void
493517
{
494518
$qb = new QueryBuilder($this->conn);
@@ -568,6 +592,18 @@ public function testEmptyInsert(): void
568592
self::assertSame($qb2, $qb);
569593
}
570594

595+
public function testInsertImproperFromThrowsException(): void
596+
{
597+
$qb = new QueryBuilder($this->conn);
598+
$qb->insert()
599+
->from('users');
600+
$this->expectException(QueryException::class);
601+
$this->expectExceptionMessage(
602+
'There is no currently registered table name.'
603+
);
604+
self::assertEquals('', $qb->getSQL());
605+
}
606+
571607
public function testGetConnection(): void
572608
{
573609
$qb = new QueryBuilder($this->conn);

0 commit comments

Comments
 (0)