Skip to content

Commit

Permalink
refactor: change castValue(),getFieldTypes() to cast(),getFieldType()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jan 23, 2024
1 parent f098b84 commit e5cfe9e
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions system/Database/Postgre/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,9 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
",\n",
array_map(
static function ($key, $value) use ($table, $alias, $that) {
$fieldName = trim($key, '"');

return $key . ($value instanceof RawSql ?
' = ' . $value :
' = ' . $alias . '.' . $that->castValue($table, $key, $value));
' = ' . $alias . '.' . $that->cast($value, $that->getFieldType($table, $key)));
},
array_keys($updateFields),
$updateFields
Expand All @@ -379,7 +377,7 @@ static function ($key, $value) use ($table, $alias, $that) {
return $value;
}

return $table . '.' . $value . ' = ' . $alias . '.' . $that->castValue($table, $value, $value);
return $table . '.' . $value . ' = ' . $alias . '.' . $that->cast($value, $that->getFieldType($table, $value));
},
array_keys($constraints),
$constraints
Expand Down Expand Up @@ -409,39 +407,36 @@ static function ($key, $value) use ($table, $alias, $that) {
}

/**
* Returns cast value.
* Returns cast expression.
*
* @TODO move this to BaseBuilder in 4.5.0
*
* @param string $table Protected Table name.
* @param string $fieldName Field name. May be protected.
* @param float|int|string $value Escaped value
* @param float|int|string $expression
*/
private function castValue(string $table, string $fieldName, $value): string
private function cast($expression, ?string $type): string
{
$fieldName = trim($fieldName, $this->db->escapeChar);

if (! isset($this->QBOptions['fieldTypes'][$table])) {
$this->getFieldTypes($table);
}

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

return ($type === null) ? $value : $value . '::' . strtoupper($type);
return ($type === null) ? $expression : $expression . '::' . strtoupper($type);
}

/**
* Gets filed types from database meta data.
* Returns the filed type from database meta data.
*
* @param string $table Protected Table name.
* @param string $table Protected table name.
* @param string $fieldName Field name. May be protected.
*/
private function getFieldTypes(string $table): void
private function getFieldType(string $table, string $fieldName): ?string
{
$types = [];
$fieldName = trim($fieldName, $this->db->escapeChar);

foreach ($this->db->getFieldData($table) as $field) {
$types[$field->name] = $field->type;
if (! isset($this->QBOptions['fieldTypes'][$table])) {
$this->QBOptions['fieldTypes'][$table] = [];

foreach ($this->db->getFieldData($table) as $field) {
$this->QBOptions['fieldTypes'][$table][$field->name] = $field->type;
}
}

$this->QBOptions['fieldTypes'][$table] = $types;
return $this->QBOptions['fieldTypes'][$table][$fieldName] ?? null;
}

/**
Expand Down

0 comments on commit e5cfe9e

Please sign in to comment.