From e5cfe9e8b36ad499b31649f10df95d685db76a6d Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 23 Jan 2024 11:25:12 +0900 Subject: [PATCH] refactor: change castValue(),getFieldTypes() to cast(),getFieldType() --- system/Database/Postgre/Builder.php | 45 +++++++++++++---------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index 7e0125c606ac..b597e2df517c 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -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 @@ -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 @@ -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; } /**