mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
fix: select() with first param as RawSql
This commit is contained in:
parent
51f94576e6
commit
fd5bf15ca5
@ -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);
|
||||
|
@ -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()));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user