Skip to content

Commit 22146f8

Browse files
committed
fix: cast Postgre batch subquery values so updateBatch() and deleteBatch() accept mixed PHP types
1 parent 94e9a16 commit 22146f8

4 files changed

Lines changed: 100 additions & 6 deletions

File tree

‎system/Database/Postgre/Builder.php‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,15 @@ static function ($key, $value) use ($table, $alias, $that): RawSql|string {
398398
$data = implode(
399399
" UNION ALL\n",
400400
array_map(
401-
static fn ($value): string => 'SELECT ' . implode(', ', array_map(
402-
static fn ($key, $index): string => $index . ' ' . $key,
401+
fn (array $value): string => sprintf('SELECT %s', implode(', ', array_map(
402+
fn (string $key, float|int|string $index): string => sprintf(
403+
'%s %s',
404+
$this->cast((string) $index, $this->getFieldType($table, $key)),
405+
$key,
406+
),
403407
$keys,
404408
$value,
405-
)),
409+
))),
406410
$values,
407411
),
408412
) . "\n";
@@ -613,14 +617,24 @@ static function ($key, $value) use ($table, $alias, $that): RawSql|string {
613617
if (isset($this->QBOptions['setQueryAsData'])) {
614618
$data = $this->QBOptions['setQueryAsData'];
615619
} else {
620+
$constraints = $this->QBOptions['constraints'] ?? [];
621+
616622
$data = implode(
617623
" UNION ALL\n",
618624
array_map(
619-
static fn ($value): string => 'SELECT ' . implode(', ', array_map(
620-
static fn ($key, $index): string => $index . ' ' . $key,
625+
fn (array $value): string => sprintf('SELECT %s', implode(', ', array_map(
626+
function (string $key, float|int|string $index) use ($table, $constraints): string {
627+
$column = array_search($key, $constraints, true);
628+
629+
return sprintf(
630+
'%s %s',
631+
$this->cast((string) $index, $this->getFieldType($table, is_string($column) ? $column : $key)),
632+
$key,
633+
);
634+
},
621635
$keys,
622636
$value,
623-
)),
637+
))),
624638
$values,
625639
),
626640
) . "\n";

‎tests/system/Database/Live/DeleteTest.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,46 @@ public function testDeleteBatchPreventsSQLInjectionInWhere(): void
119119
$this->seeInDatabase('user', ['email' => 'derek@world.com', 'name' => 'Derek Jones']);
120120
}
121121

122+
public function testDeleteBatchWithMixedConstraintValueTypesInTextColumn(): void
123+
{
124+
if ($this->db->DBDriver === 'SQLSRV') {
125+
$this->markTestSkipped('SQL Server cannot compare `text` columns with `=`.');
126+
}
127+
128+
if ($this->db->DBDriver === 'OCI8') {
129+
$this->markTestSkipped('TODO: the OCI8 builder does not cast mixed `UNION ALL` values yet. Remove this skip once it does.');
130+
}
131+
132+
$table = 'type_test';
133+
134+
$builder = $this->db->table($table);
135+
$builder->truncate();
136+
137+
foreach (['Example', '587', 'kept'] as $i => $text) {
138+
$builder->insert([
139+
'type_varchar' => 'test' . $i,
140+
'type_char' => 'char',
141+
'type_text' => $text,
142+
'type_smallint' => 32767,
143+
'type_integer' => 2_147_483_647,
144+
'type_bigint' => 9_223_372_036_854_775_807,
145+
'type_float' => 10.1,
146+
'type_numeric' => 123.23,
147+
'type_date' => '2023-12-0' . ($i + 1),
148+
'type_datetime' => '2023-12-21 12:00:00',
149+
]);
150+
}
151+
152+
$this->db->table($table)
153+
->setData([['text' => 'Example'], ['text' => 587]], null, 'data')
154+
->onConstraint(['type_text' => 'text'])
155+
->deleteBatch();
156+
157+
$this->dontSeeInDatabase($table, ['type_varchar' => 'test0']);
158+
$this->dontSeeInDatabase($table, ['type_varchar' => 'test1']);
159+
$this->seeInDatabase($table, ['type_varchar' => 'test2']);
160+
}
161+
122162
public function testDeleteBatchConstraintsDate(): void
123163
{
124164
$table = 'type_test';

‎tests/system/Database/Live/UpdateTest.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,45 @@ public static function provideUpdateBatch(): iterable
278278
];
279279
}
280280

281+
public function testUpdateBatchWithMixedValueTypesInTextColumn(): void
282+
{
283+
if ($this->db->DBDriver === 'SQLSRV') {
284+
$this->markTestSkipped('SQL Server cannot compare `text` columns with `=`.');
285+
}
286+
287+
if ($this->db->DBDriver === 'OCI8') {
288+
$this->markTestSkipped('TODO: the OCI8 builder does not cast mixed `UNION ALL` values yet. Remove this skip once it does.');
289+
}
290+
291+
$table = 'type_test';
292+
293+
$builder = $this->db->table($table);
294+
$builder->truncate();
295+
296+
for ($i = 1; $i < 3; $i++) {
297+
$builder->insert([
298+
'type_varchar' => 'test' . $i,
299+
'type_char' => 'char' . $i,
300+
'type_text' => 'text',
301+
'type_smallint' => 32767,
302+
'type_integer' => 2_147_483_647,
303+
'type_bigint' => 9_223_372_036_854_775_807,
304+
'type_float' => 10.1,
305+
'type_numeric' => 123.23,
306+
'type_date' => '2023-12-0' . $i,
307+
'type_datetime' => '2023-12-21 12:00:00',
308+
]);
309+
}
310+
311+
$this->db->table($table)->updateBatch([
312+
['type_varchar' => 'test1', 'type_text' => 'Example'],
313+
['type_varchar' => 'test2', 'type_text' => 587],
314+
], 'type_varchar');
315+
316+
$this->seeInDatabase($table, ['type_varchar' => 'test1', 'type_text' => 'Example']);
317+
$this->seeInDatabase($table, ['type_varchar' => 'test2', 'type_text' => '587']);
318+
}
319+
281320
public function testUpdateWithWhereSameColumn(): void
282321
{
283322
$this->db->table('user')->update(['country' => 'CA'], ['country' => 'US']);

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Bugs Fixed
5454
- **Cookie:** Fixed a bug where ``Cookie`` instances allowed invalid characters in path, domain, and prefix attributes rejected by ``setcookie()`` and ``setrawcookie()``.
5555
- **Database:** Fixed a bug where rebuilding a SQLite3 table (e.g., ``Forge::dropColumn()``, ``Forge::modifyColumn()``, ``Forge::dropForeignKey()`` and ``Forge::dropPrimaryKey()``) corrupted the table names referenced by its foreign keys when ``DBPrefix`` was set.
5656
- **Database:** Fixed a bug where Postgre query failures were silently ignored when ``DBDebug`` was enabled and PHP warnings were disabled. A ``DatabaseException`` is now thrown.
57+
- **Database:** Fixed a bug where ``updateBatch()`` and ``deleteBatch()`` failed on PostgreSQL when the rows supplied different PHP types (e.g., a string and an integer) for the same column, because the ``UNION ALL`` subquery values were not cast to the column type.
5758
- **Debug:** Fixed a bug where ``Timer::start()`` treated ``0.0`` as an empty value and substituted the current time.
5859
- **Files:** Fixed a bug where ``File::move()`` and ``UploadedFile::move()`` set executable and overly permissive file permissions (``0777 & ~umask()`` instead of ``0666 & ~umask()``), and ``UploadedFile::move()`` targeted the parent directory instead of the destination file for ``chmod()``.
5960
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.

0 commit comments

Comments
 (0)