Skip to content

Commit 58d5248

Browse files
committed
fix: cast OCI8 batch subquery values so updateBatch() and deleteBatch() accept mixed PHP types
1 parent ef96d2b commit 58d5248

4 files changed

Lines changed: 56 additions & 15 deletions

File tree

‎system/Database/OCI8/Builder.php‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,15 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
285285
$data = implode(
286286
" UNION ALL\n",
287287
array_map(
288-
static fn ($value): string => 'SELECT ' . implode(', ', array_map(
289-
static fn ($key, $index): string => $index . ' ' . $key,
288+
fn (array $value): string => sprintf('SELECT %s FROM DUAL', implode(', ', array_map(
289+
fn (string $key, float|int|string $index): string => sprintf(
290+
'%s %s',
291+
$this->cast((string) $index, $this->getFieldType($table, $key)),
292+
$key,
293+
),
290294
$keys,
291295
$value,
292-
)) . ' FROM DUAL',
296+
))),
293297
$values,
294298
),
295299
) . "\n";
@@ -466,14 +470,24 @@ protected function _deleteBatch(string $table, array $keys, array $values): stri
466470
if (isset($this->QBOptions['setQueryAsData'])) {
467471
$data = $this->QBOptions['setQueryAsData'];
468472
} else {
473+
$constraints = $this->QBOptions['constraints'] ?? [];
474+
469475
$data = implode(
470476
" FROM DUAL UNION ALL\n",
471477
array_map(
472-
static fn ($value): string => 'SELECT ' . implode(', ', array_map(
473-
static fn ($key, $index): string => $index . ' ' . $key,
478+
fn (array $value): string => sprintf('SELECT %s', implode(', ', array_map(
479+
function (string $key, float|int|string $index) use ($table, $constraints): string {
480+
$column = array_search($key, $constraints, true);
481+
482+
return sprintf(
483+
'%s %s',
484+
$this->cast((string) $index, $this->getFieldType($table, is_string($column) ? $column : $key)),
485+
$key,
486+
);
487+
},
474488
$keys,
475489
$value,
476-
)),
490+
))),
477491
$values,
478492
),
479493
) . " FROM DUAL\n";
@@ -489,4 +503,39 @@ protected function fieldsFromQuery(string $sql): array
489503
{
490504
return $this->db->query('SELECT * FROM (' . $sql . ') "_u_" WHERE ROWNUM = 1')->getFieldNames();
491505
}
506+
507+
private function cast(string $expression, ?string $type): string
508+
{
509+
return $type === null ? $expression : sprintf('CAST(%s AS %s)', $expression, $type);
510+
}
511+
512+
/**
513+
* Returns the type to cast a literal to for the field, or null when the field is unknown or a LOB.
514+
*
515+
* @param string $table Protected table name.
516+
* @param string $fieldName Field name. May be protected.
517+
*/
518+
private function getFieldType(string $table, string $fieldName): ?string
519+
{
520+
$fieldName = trim($fieldName, $this->db->escapeChar);
521+
522+
if (! isset($this->QBOptions['fieldTypes'][$table])) {
523+
$this->QBOptions['fieldTypes'][$table] = [];
524+
525+
foreach ($this->db->getFieldData($table) as $field) {
526+
$this->QBOptions['fieldTypes'][$table][$field->name] = $this->castType($field->type, $field->max_length);
527+
}
528+
}
529+
530+
return $this->QBOptions['fieldTypes'][$table][$fieldName] ?? null;
531+
}
532+
533+
private function castType(string $type, int|string|null $maxLength): ?string
534+
{
535+
return match ($type) {
536+
'CHAR', 'NCHAR', 'VARCHAR2', 'NVARCHAR2', 'RAW' => sprintf('%s(%d)', $type, $maxLength),
537+
'CLOB', 'NCLOB', 'BLOB', 'BFILE', 'LONG', 'LONG RAW' => null,
538+
default => $type,
539+
};
540+
}
492541
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ public function testDeleteBatchWithMixedConstraintValueTypesInTextColumn(): void
125125
$this->markTestSkipped('SQL Server cannot compare `text` columns with `=`.');
126126
}
127127

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-
132128
$table = 'type_test';
133129

134130
$builder = $this->db->table($table);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,6 @@ public function testUpdateBatchWithMixedValueTypesInTextColumn(): void
284284
$this->markTestSkipped('SQL Server cannot compare `text` columns with `=`.');
285285
}
286286

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-
291287
$table = 'type_test';
292288

293289
$builder = $this->db->table($table);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +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.
57+
- **Database:** Fixed a bug where ``updateBatch()`` and ``deleteBatch()`` failed on PostgreSQL and Oracle 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.
5858
- **Debug:** Fixed a bug where ``Timer::start()`` treated ``0.0`` as an empty value and substituted the current time.
5959
- **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()``.
6060
- **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)