Skip to content

Commit 92cc84e

Browse files
committed
refactor: change castValue(),getFieldTypes() to cast(),getFieldType()
1 parent cd644df commit 92cc84e

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

system/Database/Postgre/Builder.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,9 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
352352
",\n",
353353
array_map(
354354
static function ($key, $value) use ($table, $alias, $that) {
355-
$fieldName = trim($key, '"');
356-
357355
return $key . ($value instanceof RawSql ?
358356
' = ' . $value :
359-
' = ' . $alias . '.' . $that->castValue($table, $key, $value));
357+
' = ' . $alias . '.' . $that->cast($value, $that->getFieldType($table, $key)));
360358
},
361359
array_keys($updateFields),
362360
$updateFields
@@ -379,7 +377,7 @@ static function ($key, $value) use ($table, $alias, $that) {
379377
return $value;
380378
}
381379

382-
return $table . '.' . $value . ' = ' . $alias . '.' . $that->castValue($table, $value, $value);
380+
return $table . '.' . $value . ' = ' . $alias . '.' . $that->cast($value, $that->getFieldType($table, $value));
383381
},
384382
array_keys($constraints),
385383
$constraints
@@ -409,39 +407,36 @@ static function ($key, $value) use ($table, $alias, $that) {
409407
}
410408

411409
/**
412-
* Returns cast value.
410+
* Returns cast expression.
411+
*
412+
* @TODO move this to BaseBuilder in 4.5.0
413413
*
414-
* @param string $table Protected Table name.
415-
* @param string $fieldName Field name. May be protected.
416-
* @param float|int|string $value Escaped value
414+
* @param float|int|string $expression
417415
*/
418-
private function castValue(string $table, string $fieldName, $value): string
416+
private function cast($expression, ?string $type): string
419417
{
420-
$fieldName = trim($fieldName, $this->db->escapeChar);
421-
422-
if (! isset($this->QBOptions['fieldTypes'][$table])) {
423-
$this->getFieldTypes($table);
424-
}
425-
426-
$type = $this->QBOptions['fieldTypes'][$table][$fieldName] ?? null;
427-
428-
return ($type === null) ? $value : $value . '::' . strtoupper($type);
418+
return ($type === null) ? $expression : $expression . '::' . strtoupper($type);
429419
}
430420

431421
/**
432-
* Gets filed types from database meta data.
422+
* Returns the filed type from database meta data.
433423
*
434-
* @param string $table Protected Table name.
424+
* @param string $table Protected table name.
425+
* @param string $fieldName Field name. May be protected.
435426
*/
436-
private function getFieldTypes(string $table): void
427+
private function getFieldType(string $table, string $fieldName): ?string
437428
{
438-
$types = [];
429+
$fieldName = trim($fieldName, $this->db->escapeChar);
439430

440-
foreach ($this->db->getFieldData($table) as $field) {
441-
$types[$field->name] = $field->type;
431+
if (! isset($this->QBOptions['fieldTypes'][$table])) {
432+
$this->QBOptions['fieldTypes'][$table] = [];
433+
434+
foreach ($this->db->getFieldData($table) as $field) {
435+
$this->QBOptions['fieldTypes'][$table][$field->name] = $field->type;
436+
}
442437
}
443438

444-
$this->QBOptions['fieldTypes'][$table] = $types;
439+
return $this->QBOptions['fieldTypes'][$table][$fieldName] ?? null;
445440
}
446441

447442
/**

0 commit comments

Comments
 (0)