From ee4b85931a5d9dadd80ee96f5a1565d03bfc0473 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Tue, 31 Oct 2023 08:35:53 +0700 Subject: [PATCH] Return and deprecate `getTypecastValue()` method and `$table` parameter --- src/QueryBuilder/AbstractDMLQueryBuilder.php | 31 +++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/QueryBuilder/AbstractDMLQueryBuilder.php b/src/QueryBuilder/AbstractDMLQueryBuilder.php index 884a5a1b2..1224e503f 100644 --- a/src/QueryBuilder/AbstractDMLQueryBuilder.php +++ b/src/QueryBuilder/AbstractDMLQueryBuilder.php @@ -13,6 +13,7 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Query\QueryInterface; +use Yiisoft\Db\Schema\ColumnSchemaInterface; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -55,7 +56,7 @@ public function batchInsert(string $table, array $columns, iterable $rows, array } $values = []; - $columns = $this->getNormalizeColumnNames($columns); + $columns = $this->getNormalizeColumnNames('', $columns); $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; foreach ($rows as $row) { @@ -208,7 +209,7 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu $names = []; $placeholders = []; - $columns = $this->normalizeColumnNames($columns); + $columns = $this->normalizeColumnNames('', $columns); $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; foreach ($columns as $name => $value) { @@ -241,7 +242,7 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu protected function prepareUpdateSets(string $table, array $columns, array $params = []): array { $sets = []; - $columns = $this->normalizeColumnNames($columns); + $columns = $this->normalizeColumnNames('', $columns); $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; foreach ($columns as $name => $value) { @@ -285,7 +286,7 @@ protected function prepareUpsertColumns( if ($insertColumns instanceof QueryInterface) { [$insertNames] = $this->prepareInsertSelectSubQuery($insertColumns); } else { - $insertNames = $this->getNormalizeColumnNames(array_keys($insertColumns)); + $insertNames = $this->getNormalizeColumnNames('', array_keys($insertColumns)); $insertNames = array_map( [$this->quoter, 'quoteColumnName'], @@ -386,20 +387,35 @@ static function (Constraint $constraint) use ($quoter, $columns, &$columnNames): return array_unique($columnNames); } + /** + * @return mixed The typecast value of the given column. + * + * @deprecated will be removed in version 2.0.0 + */ + protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnSchema = null): mixed + { + if ($columnSchema) { + return $columnSchema->dbTypecast($value); + } + + return $value; + } + /** * Normalizes the column names. * + * @param string $table Not used. Could be empty string. Will be removed in version 2.0.0. * @param array $columns The column data (name => value). * * @return array The normalized column names (name => value). * * @psalm-return array */ - protected function normalizeColumnNames(array $columns): array + protected function normalizeColumnNames(string $table, array $columns): array { /** @var string[] $columnNames */ $columnNames = array_keys($columns); - $normalizedNames = $this->getNormalizeColumnNames($columnNames); + $normalizedNames = $this->getNormalizeColumnNames('', $columnNames); return array_combine($normalizedNames, $columns); } @@ -407,11 +423,12 @@ protected function normalizeColumnNames(array $columns): array /** * Get normalized column names * + * @param string $table Not used. Could be empty string. Will be removed in version 2.0.0. * @param string[] $columns The column names. * * @return string[] Normalized column names. */ - protected function getNormalizeColumnNames(array $columns): array + protected function getNormalizeColumnNames(string $table, array $columns): array { $normalizedNames = [];