Skip to content

Commit fd5bf15

Browse files
committed
fix: select() with first param as RawSql
1 parent 51f9457 commit fd5bf15

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

system/Database/BaseBuilder.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,15 +3060,17 @@ protected function compileSelect($selectOverride = false): string
30603060

30613061
if (empty($this->QBSelect)) {
30623062
$sql .= '*';
3063-
} elseif ($this->QBSelect[0] instanceof RawSql) {
3064-
$sql .= (string) $this->QBSelect[0];
30653063
} else {
30663064
// Cycle through the "select" portion of the query and prep each column name.
30673065
// The reason we protect identifiers here rather than in the select() function
30683066
// is because until the user calls the from() function we don't know if there are aliases
30693067
foreach ($this->QBSelect as $key => $val) {
3070-
$protect = $this->QBNoEscape[$key] ?? null;
3071-
$this->QBSelect[$key] = $this->db->protectIdentifiers($val, false, $protect);
3068+
if ($val instanceof RawSql) {
3069+
$this->QBSelect[$key] = (string) $this->QBSelect[0];
3070+
} else {
3071+
$protect = $this->QBNoEscape[$key] ?? null;
3072+
$this->QBSelect[$key] = $this->db->protectIdentifiers($val, false, $protect);
3073+
}
30723074
}
30733075

30743076
$sql .= implode(', ', $this->QBSelect);

tests/system/Database/Builder/SelectTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public function testSelectAcceptsArrayWithRawSql(): void
7272
$builder = new BaseBuilder('employees', $this->db);
7373

7474
$builder->select([
75-
'employee_id',
7675
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
76+
'employee_id',
7777
]);
7878

7979
$expected = <<<'SQL'
80-
SELECT "employee_id", IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees"
80+
SELECT IF(salary > 5000, 'High', 'Low') AS salary_level, "employee_id" FROM "employees"
8181
SQL;
8282
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
8383
}

0 commit comments

Comments
 (0)