forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RegEx to match if queryPart contains OR/AND (doctrine#8453)
This allows fixes cases of queries that contain line feeds or tabs but do not benefit from automatic wrapping of parenthesis.
- Loading branch information
Showing
2 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/Doctrine/Tests/ORM/Functional/QueryBuilderParenthesis.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional; | ||
|
||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class QueryBuilderParenthesis extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); | ||
$this->_schemaTool->createSchema( | ||
[ | ||
$this->_em->getClassMetadata(QueryBuilderParenthesisEntity::class), | ||
] | ||
); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
$this->_schemaTool->dropSchema( | ||
[ | ||
$this->_em->getClassMetadata(QueryBuilderParenthesisEntity::class), | ||
] | ||
); | ||
} | ||
|
||
public function testParenthesisOnSingleLine(): void | ||
{ | ||
$queryBuilder = $this->_em->createQueryBuilder(); | ||
$queryBuilder->select('o')->from(QueryBuilderParenthesisEntity::class, 'o'); | ||
$queryBuilder->andWhere('o.property3 = :value3')->setParameter('value3', 'x'); | ||
$queryBuilder->andWhere('o.property1 = :value1 OR o.property2 = :value2'); | ||
$queryBuilder->setParameter('value1', 'x'); | ||
$queryBuilder->setParameter('value2', 'x'); | ||
|
||
$query = $queryBuilder->getQuery(); | ||
$results = $query->getResult(); | ||
$this->assertCount(0, $results); | ||
|
||
$dql = $query->getDQL(); | ||
|
||
$this->assertSame( | ||
'SELECT o FROM ' . QueryBuilderParenthesisEntity::class . ' o WHERE o.property3 = :value3 AND (o.property1 = :value1 OR o.property2 = :value2)', | ||
$dql | ||
); | ||
} | ||
|
||
public function testParenthesisOnMultiLine(): void | ||
{ | ||
$queryBuilder = $this->_em->createQueryBuilder(); | ||
$queryBuilder->select('o')->from(QueryBuilderParenthesisEntity::class, 'o'); | ||
$queryBuilder->andWhere('o.property3 = :value3')->setParameter('value3', 'x'); | ||
|
||
$queryBuilder->andWhere( | ||
'o.property1 = :value1 | ||
OR o.property2 = :value2' | ||
); | ||
$queryBuilder->setParameter('value1', 'x'); | ||
$queryBuilder->setParameter('value2', 'x'); | ||
|
||
$query = $queryBuilder->getQuery(); | ||
$results = $query->getResult(); | ||
$this->assertCount(0, $results); | ||
|
||
$dql = $query->getDQL(); | ||
|
||
$this->assertSame( | ||
'SELECT o FROM ' . QueryBuilderParenthesisEntity::class . ' o WHERE o.property3 = :value3 AND (o.property1 = :value1 | ||
OR o.property2 = :value2)', | ||
$dql | ||
); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @Entity | ||
*/ | ||
class QueryBuilderParenthesisEntity | ||
{ | ||
/** | ||
* @var int|null | ||
* @Column(type="integer") | ||
* @Id | ||
* @GeneratedValue | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @var string|null | ||
* @Column() | ||
*/ | ||
public $property1; | ||
|
||
/** | ||
* @var string|null | ||
* @Column() | ||
*/ | ||
public $property2; | ||
|
||
/** | ||
* @var string|null | ||
* @Column() | ||
*/ | ||
public $property3; | ||
} |