Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
- Chg #972: Change in query "distinct" flag type from `bool|null` to `bool` (@vjik)
- New #973: Add `upsertWithReturningPks()` method to `CommandInterface` and `DMLQueryBuilderInterface` (@Tigrov)
- New #968: Add `DateTimeColumn` column class (@Tigrov)
- Bug #978: Fix memory leaking in `Command::exists()` method (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder/AbstractDQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function getExpressionBuilder(ExpressionInterface $expression): object

public function selectExists(string $rawSql): string
{
return 'SELECT EXISTS(' . $rawSql . ')';
return 'SELECT EXISTS(' . $rawSql . ') AS ' . $this->quoter->quoteSimpleColumnName('0');
}

public function setConditionClasses(array $classes): void
Expand Down
17 changes: 10 additions & 7 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2129,17 +2129,20 @@ public function testResetSequenceTableNoExistException(): void
$qb->resetSequence('noExist', 1);
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::selectExist
*/
public function testSelectExists(string $sql, string $expected): void
public function testSelectExists(): void
{
$db = $this->getConnection();

$qb = $db->getQueryBuilder();
$sqlSelectExist = $qb->selectExists($sql);

$this->assertSame($expected, $sqlSelectExist);
$sql = DbHelper::replaceQuotes('SELECT 1 FROM [[customer]] WHERE [[id]] = 1', $db->getDriverName());
// Alias required to avoid memory leaking on MySQL. Other DBMS have the same alias for consistency.
// @link https://github.com/yiisoft/yii2/issues/20385
$expected = DbHelper::replaceQuotes(
'SELECT EXISTS(SELECT 1 FROM [[customer]] WHERE [[id]] = 1) AS [[0]]',
$db->getDriverName()
);

$this->assertSame($expected, $qb->selectExists($sql));
}

/**
Expand Down
20 changes: 0 additions & 20 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,26 +1141,6 @@ public static function insertWithReturningPks(): array
];
}

public static function selectExist(): array
{
return [
[
DbHelper::replaceQuotes(
<<<SQL
SELECT 1 FROM `table` WHERE `id` = 1
SQL,
static::$driverName,
),
DbHelper::replaceQuotes(
<<<SQL
SELECT EXISTS(SELECT 1 FROM `table` WHERE `id` = 1)
SQL,
static::$driverName,
),
],
];
}

public static function selectScalar(): array
{
return [
Expand Down