Skip to content

Commit 5df052d

Browse files
committed
fix: updateBatch() Postgre type error
1 parent d56f851 commit 5df052d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

system/Database/Postgre/Builder.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,19 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
347347

348348
$sql .= "SET\n";
349349

350+
$fieldTypes = $this->getFieldTypes($table);
351+
350352
$sql .= implode(
351353
",\n",
352354
array_map(
353-
static fn ($key, $value) => $key . ($value instanceof RawSql ?
354-
' = ' . $value :
355-
' = ' . $alias . '.' . $value),
355+
static function ($key, $value) use ($alias, $fieldTypes) {
356+
$type = $fieldTypes[trim($key, '"')] ?? null;
357+
$cast = ($type === null) ? '' : '::' . $type;
358+
359+
return $key . ($value instanceof RawSql ?
360+
' = ' . $value :
361+
' = ' . $alias . '.' . $value . $cast);
362+
},
356363
array_keys($updateFields),
357364
$updateFields
358365
)
@@ -405,6 +412,20 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
405412
return str_replace('{:_table_:}', $data, $sql);
406413
}
407414

415+
/**
416+
* @return array<string, string> [name => type]
417+
*/
418+
private function getFieldTypes(string $table): array
419+
{
420+
$types = [];
421+
422+
foreach ($this->db->getFieldData($table) as $field) {
423+
$types[$field->name] = $field->type;
424+
}
425+
426+
return $types;
427+
}
428+
408429
/**
409430
* Generates a platform-specific upsertBatch string from the supplied data
410431
*

0 commit comments

Comments
 (0)