Skip to content

Commit 6d4ce25

Browse files
committed
refactor: change castValue() API
1 parent 64cd791 commit 6d4ce25

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

system/Database/Postgre/Builder.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use CodeIgniter\Database\RawSql;
1717
use InvalidArgumentException;
18-
use LogicException;
1918

2019
/**
2120
* Builder for Postgre
@@ -348,18 +347,16 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
348347

349348
$sql .= "SET\n";
350349

351-
$this->getFieldTypes($table);
352-
353350
$that = $this;
354351
$sql .= implode(
355352
",\n",
356353
array_map(
357-
static function ($key, $value) use ($alias, $that) {
354+
static function ($key, $value) use ($table, $alias, $that) {
358355
$fieldName = trim($key, '"');
359356

360357
return $key . ($value instanceof RawSql ?
361358
' = ' . $value :
362-
' = ' . $alias . '.' . $that->castValue($fieldName, $value));
359+
' = ' . $alias . '.' . $that->castValue($table, $key, $value));
363360
},
364361
array_keys($updateFields),
365362
$updateFields
@@ -382,9 +379,7 @@ static function ($key, $value) use ($table, $alias, $that) {
382379
return $value;
383380
}
384381

385-
$fieldName = trim($value, '"');
386-
387-
return $table . '.' . $value . ' = ' . $alias . '.' . $that->castValue($fieldName, $value);
382+
return $table . '.' . $value . ' = ' . $alias . '.' . $that->castValue($table, $value, $value);
388383
},
389384
array_keys($constraints),
390385
$constraints
@@ -416,23 +411,27 @@ static function ($key, $value) use ($table, $alias, $that) {
416411
/**
417412
* Returns cast value.
418413
*
419-
* @param float|int|string $value Escaped value
414+
* @param string $table Protected Table name.
415+
* @param string $fieldName Field name. May be protected.
416+
* @param float|int|string $value Escaped value
420417
*/
421-
private function castValue(string $fieldName, $value): string
418+
private function castValue(string $table, string $fieldName, $value): string
422419
{
423-
if (! isset($this->QBOptions['fieldTypes'])) {
424-
throw new LogicException(
425-
'You must call getFieldTypes() before calling castValue().'
426-
);
420+
$fieldName = trim($fieldName, $this->db->escapeChar);
421+
422+
if (! isset($this->QBOptions['fieldTypes'][$table])) {
423+
$this->getFieldTypes($table);
427424
}
428425

429-
$type = $this->QBOptions['fieldTypes'][$fieldName] ?? null;
426+
$type = $this->QBOptions['fieldTypes'][$table][$fieldName] ?? null;
430427

431428
return ($type === null) ? $value : $value . '::' . strtoupper($type);
432429
}
433430

434431
/**
435432
* Gets filed types from database meta data.
433+
*
434+
* @param string $table Protected Table name.
436435
*/
437436
private function getFieldTypes(string $table): void
438437
{
@@ -442,7 +441,7 @@ private function getFieldTypes(string $table): void
442441
$types[$field->name] = $field->type;
443442
}
444443

445-
$this->QBOptions['fieldTypes'] = $types;
444+
$this->QBOptions['fieldTypes'][$table] = $types;
446445
}
447446

448447
/**

0 commit comments

Comments
 (0)