Skip to content

Commit 647af58

Browse files
authored
Add alias in DQLQueryBuilder::selectExists() method for consistency (#363)
1 parent 9eb7202 commit 647af58

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- New #358: Use `DateTimeColumn` class for datetime column types (@Tigrov)
4444
- New #361: Implement `DMLQueryBuilder::upsertWithReturningPks()` method (@Tigrov)
4545
- Enh #361: Refactor `DMLQueryBuilder::insertWithReturningPks()` method (@Tigrov)
46+
- Chg #363: Add alias in `DQLQueryBuilder::selectExists()` method for consistency with other DBMS (@Tigrov)
4647

4748
## 1.2.0 March 21, 2024
4849

src/DQLQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function buildOrderByAndLimit(
3939

4040
public function selectExists(string $rawSql): string
4141
{
42-
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END';
42+
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END AS [0]';
4343
}
4444

4545
protected function defaultExpressionBuilders(): array

tests/Provider/QueryBuilderProvider.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,6 @@ public static function insertWithReturningPks(): array
395395
];
396396
}
397397

398-
public static function selectExist(): array
399-
{
400-
$selectExist = parent::selectExist();
401-
402-
$selectExist[0][1] = <<<SQL
403-
SELECT CASE WHEN EXISTS(SELECT 1 FROM `table` WHERE `id` = 1) THEN 1 ELSE 0 END
404-
SQL;
405-
406-
return $selectExist;
407-
}
408-
409398
public static function upsert(): array
410399
{
411400
$concreteData = [

tests/QueryBuilderTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,16 @@ public function testResetSequence(): void
413413
);
414414
}
415415
416-
#[DataProviderExternal(QueryBuilderProvider::class, 'selectExist')]
417-
public function testSelectExists(string $sql, string $expected): void
416+
public function testSelectExists(): void
418417
{
419-
parent::testSelectExists($sql, $expected);
418+
$db = $this->getConnection();
419+
$qb = $db->getQueryBuilder();
420+
421+
$sql = 'SELECT 1 FROM [customer] WHERE [id] = 1';
422+
// Alias is not required in MSSQL, but it is added for consistency with other DBMS.
423+
$expected = 'SELECT CASE WHEN EXISTS(SELECT 1 FROM [customer] WHERE [id] = 1) THEN 1 ELSE 0 END AS [0]';
424+
425+
$this->assertSame($expected, $qb->selectExists($sql));
420426
}
421427
422428
#[DataProviderExternal(QueryBuilderProvider::class, 'update')]

0 commit comments

Comments
 (0)