Skip to content

Commit 8534e8f

Browse files
committed
fix: wrong variable and add tests
1 parent fd5bf15 commit 8534e8f

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

system/Database/BaseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3066,7 +3066,7 @@ protected function compileSelect($selectOverride = false): string
30663066
// is because until the user calls the from() function we don't know if there are aliases
30673067
foreach ($this->QBSelect as $key => $val) {
30683068
if ($val instanceof RawSql) {
3069-
$this->QBSelect[$key] = (string) $this->QBSelect[0];
3069+
$this->QBSelect[$key] = (string) $val;
30703070
} else {
30713071
$protect = $this->QBNoEscape[$key] ?? null;
30723072
$this->QBSelect[$key] = $this->db->protectIdentifiers($val, false, $protect);

tests/system/Database/Builder/SelectTest.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Database\SQLSRV\Builder as SQLSRVBuilder;
2020
use CodeIgniter\Test\CIUnitTestCase;
2121
use CodeIgniter\Test\Mock\MockConnection;
22+
use PHPUnit\Framework\Attributes\DataProvider;
2223
use PHPUnit\Framework\Attributes\Group;
2324

2425
/**
@@ -67,21 +68,62 @@ public function testSelectAcceptsArray(): void
6768
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
6869
}
6970

70-
public function testSelectAcceptsArrayWithRawSql(): void
71+
#[DataProvider('provideSelectAcceptsArrayWithRawSql')]
72+
public function testSelectAcceptsArrayWithRawSql(array $select, string $expected): void
7173
{
7274
$builder = new BaseBuilder('employees', $this->db);
7375

74-
$builder->select([
75-
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
76-
'employee_id',
77-
]);
76+
$builder->select($select);
7877

79-
$expected = <<<'SQL'
80-
SELECT IF(salary > 5000, 'High', 'Low') AS salary_level, "employee_id" FROM "employees"
81-
SQL;
8278
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
8379
}
8480

81+
/**
82+
* @return list<list<RawSql|string>|string>
83+
*/
84+
public static function provideSelectAcceptsArrayWithRawSql(): iterable
85+
{
86+
yield from [
87+
[
88+
[
89+
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
90+
'employee_id',
91+
],
92+
<<<'SQL'
93+
SELECT IF(salary > 5000, 'High', 'Low') AS salary_level, "employee_id" FROM "employees"
94+
SQL,
95+
],
96+
[
97+
[
98+
'employee_id',
99+
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
100+
],
101+
<<<'SQL'
102+
SELECT "employee_id", IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees"
103+
SQL,
104+
],
105+
[
106+
[
107+
new RawSql("CONCAT(first_name, ' ', last_name) AS full_name"),
108+
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
109+
],
110+
<<<'SQL'
111+
SELECT CONCAT(first_name, ' ', last_name) AS full_name, IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees"
112+
SQL,
113+
],
114+
[
115+
[
116+
new RawSql("CONCAT(first_name, ' ', last_name) AS full_name"),
117+
'employee_id',
118+
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
119+
],
120+
<<<'SQL'
121+
SELECT CONCAT(first_name, ' ', last_name) AS full_name, "employee_id", IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees"
122+
SQL,
123+
],
124+
];
125+
}
126+
85127
public function testSelectAcceptsMultipleColumns(): void
86128
{
87129
$builder = new BaseBuilder('users', $this->db);

0 commit comments

Comments
 (0)