fix: select() with first param as RawSql

This commit is contained in:
kenjis 2024-06-28 19:20:15 +09:00
parent 51f94576e6
commit fd5bf15ca5
No known key found for this signature in database
GPG Key ID: BD254878922AF198
2 changed files with 8 additions and 6 deletions

View File

@ -3060,15 +3060,17 @@ class BaseBuilder
if (empty($this->QBSelect)) {
$sql .= '*';
} elseif ($this->QBSelect[0] instanceof RawSql) {
$sql .= (string) $this->QBSelect[0];
} else {
// Cycle through the "select" portion of the query and prep each column name.
// The reason we protect identifiers here rather than in the select() function
// is because until the user calls the from() function we don't know if there are aliases
foreach ($this->QBSelect as $key => $val) {
$protect = $this->QBNoEscape[$key] ?? null;
$this->QBSelect[$key] = $this->db->protectIdentifiers($val, false, $protect);
if ($val instanceof RawSql) {
$this->QBSelect[$key] = (string) $this->QBSelect[0];
} else {
$protect = $this->QBNoEscape[$key] ?? null;
$this->QBSelect[$key] = $this->db->protectIdentifiers($val, false, $protect);
}
}
$sql .= implode(', ', $this->QBSelect);

View File

@ -72,12 +72,12 @@ final class SelectTest extends CIUnitTestCase
$builder = new BaseBuilder('employees', $this->db);
$builder->select([
'employee_id',
new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"),
'employee_id',
]);
$expected = <<<'SQL'
SELECT "employee_id", IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees"
SELECT IF(salary > 5000, 'High', 'Low') AS salary_level, "employee_id" FROM "employees"
SQL;
$this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect()));
}