Skip to content

Commit cf01198

Browse files
committed
Fix CR issues
1 parent cde6bd7 commit cf01198

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

framework/db/QueryBuilder.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ public function batchUpdate($table, $rows, $columns = [], $keys = [], $condition
540540
$columnSchemas = $tableSchema !== null ? $tableSchema->columns : [];
541541

542542
$keys = $this->resolveKeys($table, $keys);
543+
$rows = $this->prepareRows($rows);
543544
$rows = $this->resolveColumnNames($rows, $columns);
544545

545546
$isSingleKey = count($keys) === 1;
@@ -553,13 +554,6 @@ public function batchUpdate($table, $rows, $columns = [], $keys = [], $condition
553554
$rowKeyData = [];
554555

555556
foreach ($rows as $row) {
556-
if ($row instanceof \Traversable) {
557-
$row = iterator_to_array($row);
558-
}
559-
if (!is_array($row)) {
560-
throw new InvalidArgumentException('Each batch update row must be an array.');
561-
}
562-
563557
$keyData = [];
564558
$keyHashParts = [];
565559
foreach ($keys as $keyCol) {
@@ -705,15 +699,38 @@ protected function resolveKeys($table, $keys)
705699
);
706700
}
707701

702+
/**
703+
* Converts Traversable objects to arrays and validates that all rows are arrays.
704+
*
705+
* @param array|\Generator $rows the rows to prepare.
706+
* @return array[] prepared rows.
707+
* @since 2.0.55
708+
*/
709+
protected function prepareRows($rows)
710+
{
711+
$prepared = [];
712+
foreach ($rows as $row) {
713+
if ($row instanceof \Traversable) {
714+
$row = iterator_to_array($row);
715+
}
716+
if (!is_array($row)) {
717+
throw new InvalidArgumentException('Each batch update row must be an array.');
718+
}
719+
$prepared[] = $row;
720+
}
721+
722+
return $prepared;
723+
}
724+
708725
/**
709726
* Resolves batch update row column names.
710727
*
711728
* When `$columns` is specified, converts indexed arrays to associative arrays
712729
* using column names as keys (same convention as [[batchInsert()]]).
713730
*
714-
* @param array|\Generator $rows the rows to resolve.
731+
* @param array[] $rows the rows to resolve (already prepared by [[prepareRows()]]).
715732
* @param array $columns list of column names.
716-
* @return array resolved rows as associative arrays.
733+
* @return array[] resolved rows as associative arrays.
717734
* @since 2.0.55
718735
*/
719736
protected function resolveColumnNames($rows, $columns)
@@ -725,12 +742,6 @@ protected function resolveColumnNames($rows, $columns)
725742
$resolvedRows = [];
726743
$columnCount = count($columns);
727744
foreach ($rows as $row) {
728-
if ($row instanceof \Traversable) {
729-
$row = iterator_to_array($row);
730-
}
731-
if (!is_array($row)) {
732-
throw new InvalidArgumentException('Each batch update row must be an array.');
733-
}
734745
if (count($row) !== $columnCount) {
735746
throw new InvalidArgumentException(
736747
'Each batch update row must have exactly ' . $columnCount . ' values when $columns is specified.'
@@ -749,11 +760,10 @@ protected function resolveColumnNames($rows, $columns)
749760
*
750761
* @param string $table the table that data will be saved into.
751762
* @param array $columns the column data (name => value) to be saved into the table.
752-
* @param array $params the binding parameters that will be modified by this method.
753763
* @return array normalized columns.
754764
* @since 2.0.55
755765
*/
756-
protected function normalizeTableRowData($table, $columns, &$params)
766+
protected function normalizeTableRowData($table, $columns)
757767
{
758768
return $columns;
759769
}
@@ -767,30 +777,21 @@ protected function normalizeTableRowData($table, $columns, &$params)
767777
* @param string $table the table name.
768778
* @param array $rows the rows to normalize.
769779
* @param array $keys the key column names.
770-
* @param array $params the binding parameters that will be modified by this method.
771780
* @return array normalized rows.
772781
* @since 2.0.55
773782
*/
774-
protected function normalizeBatchUpdateRows($table, $rows, $keys, &$params)
783+
protected function normalizeBatchUpdateRows($table, $rows, $keys)
775784
{
776785
$normalizedRows = [];
777786
foreach ($rows as $row) {
778-
if ($row instanceof \Traversable) {
779-
$row = iterator_to_array($row);
780-
}
781-
if (!is_array($row)) {
782-
$normalizedRows[] = $row;
783-
continue;
784-
}
785-
786787
$keyValues = [];
787788
foreach ($keys as $keyCol) {
788789
if (array_key_exists($keyCol, $row)) {
789790
$keyValues[$keyCol] = $row[$keyCol];
790791
unset($row[$keyCol]);
791792
}
792793
}
793-
$row = $this->normalizeTableRowData($table, $row, $params);
794+
$row = $this->normalizeTableRowData($table, $row);
794795
foreach ($keyValues as $keyCol => $keyValue) {
795796
$row[$keyCol] = $keyValue;
796797
}

framework/db/mssql/QueryBuilder.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public function selectExists($rawSql)
464464
/**
465465
* {@inheritdoc}
466466
*/
467-
protected function normalizeTableRowData($table, $columns, &$params)
467+
protected function normalizeTableRowData($table, $columns)
468468
{
469469
if (($tableSchema = $this->db->getSchema()->getTableSchema($table)) !== null) {
470470
$columnSchemas = $tableSchema->columns;
@@ -488,7 +488,7 @@ protected function normalizeTableRowData($table, $columns, &$params)
488488
*/
489489
public function insert($table, $columns, &$params)
490490
{
491-
$columns = $this->normalizeTableRowData($table, $columns, $params);
491+
$columns = $this->normalizeTableRowData($table, $columns);
492492

493493
$version2005orLater = version_compare($this->db->getSchema()->getServerVersion(), '9', '>=');
494494

@@ -542,7 +542,7 @@ public function insert($table, $columns, &$params)
542542
*/
543543
public function upsert($table, $insertColumns, $updateColumns, &$params)
544544
{
545-
$insertColumns = $this->normalizeTableRowData($table, $insertColumns, $params);
545+
$insertColumns = $this->normalizeTableRowData($table, $insertColumns);
546546

547547
list($uniqueNames, $insertNames, $updateNames) = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns, $constraints);
548548
if (empty($uniqueNames)) {
@@ -602,7 +602,7 @@ public function upsert($table, $insertColumns, $updateColumns, &$params)
602602
$updateColumns[$name] = new Expression($quotedName);
603603
}
604604
}
605-
$updateColumns = $this->normalizeTableRowData($table, $updateColumns, $params);
605+
$updateColumns = $this->normalizeTableRowData($table, $updateColumns);
606606

607607
list($updates, $params) = $this->prepareUpdateSets($table, $updateColumns, $params);
608608
$updateSql = 'UPDATE SET ' . implode(', ', $updates);
@@ -614,18 +614,19 @@ public function upsert($table, $insertColumns, $updateColumns, &$params)
614614
*/
615615
public function update($table, $columns, $condition, &$params)
616616
{
617-
return parent::update($table, $this->normalizeTableRowData($table, $columns, $params), $condition, $params);
617+
return parent::update($table, $this->normalizeTableRowData($table, $columns), $condition, $params);
618618
}
619619

620620
/**
621621
* {@inheritdoc}
622622
*/
623623
public function batchUpdate($table, $rows, $columns = [], $keys = [], $condition = '', &$params = [])
624624
{
625-
$rows = $this->resolveColumnNames($rows, $columns);
626625
$keys = $this->resolveKeys($table, $keys);
626+
$rows = $this->prepareRows($rows);
627+
$rows = $this->resolveColumnNames($rows, $columns);
627628

628-
return parent::batchUpdate($table, $this->normalizeBatchUpdateRows($table, $rows, $keys, $params), [], $keys, $condition, $params);
629+
return parent::batchUpdate($table, $this->normalizeBatchUpdateRows($table, $rows, $keys), [], $keys, $condition, $params);
629630
}
630631

631632
/**

framework/db/pgsql/QueryBuilder.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public function alterColumn($table, $column, $type)
305305
*/
306306
public function insert($table, $columns, &$params)
307307
{
308-
return parent::insert($table, $this->normalizeTableRowData($table, $columns, $params), $params);
308+
return parent::insert($table, $this->normalizeTableRowData($table, $columns), $params);
309309
}
310310

311311
/**
@@ -315,9 +315,9 @@ public function insert($table, $columns, &$params)
315315
*/
316316
public function upsert($table, $insertColumns, $updateColumns, &$params)
317317
{
318-
$insertColumns = $this->normalizeTableRowData($table, $insertColumns, $params);
318+
$insertColumns = $this->normalizeTableRowData($table, $insertColumns);
319319
if (!is_bool($updateColumns)) {
320-
$updateColumns = $this->normalizeTableRowData($table, $updateColumns, $params);
320+
$updateColumns = $this->normalizeTableRowData($table, $updateColumns);
321321
}
322322
if (version_compare($this->db->getServerVersion(), '9.5', '<')) {
323323
return $this->oldUpsert($table, $insertColumns, $updateColumns, $params);
@@ -455,24 +455,25 @@ private function oldUpsert($table, $insertColumns, $updateColumns, &$params)
455455
*/
456456
public function update($table, $columns, $condition, &$params)
457457
{
458-
return parent::update($table, $this->normalizeTableRowData($table, $columns, $params), $condition, $params);
458+
return parent::update($table, $this->normalizeTableRowData($table, $columns), $condition, $params);
459459
}
460460

461461
/**
462462
* {@inheritdoc}
463463
*/
464464
public function batchUpdate($table, $rows, $columns = [], $keys = [], $condition = '', &$params = [])
465465
{
466-
$rows = $this->resolveColumnNames($rows, $columns);
467466
$keys = $this->resolveKeys($table, $keys);
467+
$rows = $this->prepareRows($rows);
468+
$rows = $this->resolveColumnNames($rows, $columns);
468469

469-
return parent::batchUpdate($table, $this->normalizeBatchUpdateRows($table, $rows, $keys, $params), [], $keys, $condition, $params);
470+
return parent::batchUpdate($table, $this->normalizeBatchUpdateRows($table, $rows, $keys), [], $keys, $condition, $params);
470471
}
471472

472473
/**
473474
* {@inheritdoc}
474475
*/
475-
protected function normalizeTableRowData($table, $columns, &$params)
476+
protected function normalizeTableRowData($table, $columns)
476477
{
477478
if ($columns instanceof Query) {
478479
return $columns;

0 commit comments

Comments
 (0)