|
33 | 33 | use Doctrine\DBAL\SQL\Parser; |
34 | 34 | use Doctrine\DBAL\Types\Type; |
35 | 35 | use Doctrine\Deprecations\Deprecation; |
| 36 | +use Exception; |
36 | 37 | use InvalidArgumentException; |
37 | 38 | use SensitiveParameter; |
38 | 39 | use Throwable; |
@@ -523,44 +524,52 @@ public function insert(string $table, array $data, array $types = []): int|strin |
523 | 524 | * Inserts multiple table rows with specified data. |
524 | 525 | * |
525 | 526 | * Table expression and columns are not escaped and are not safe for user-input. |
526 | | - * Each array within data should have the same keys. |
| 527 | + * Each row should have the same keys |
527 | 528 | * |
528 | | - * @param array<array<string, mixed>> $data |
529 | | - * @param array<int<0,max>, string|ParameterType|Type>|array<string, string|ParameterType|Type> $types |
| 529 | + * @param array<array<string, mixed>> $rows |
| 530 | + * @param array<string, string|ParameterType|Type> $types |
530 | 531 | * |
531 | 532 | * @return int|numeric-string The number of affected rows. |
532 | 533 | * |
533 | 534 | * @throws Exception |
534 | 535 | */ |
535 | | - public function insertMany(string $table, array $data, array $types = []): int|string |
| 536 | + public function insertMany(string $table, array $rows, array $types = []): int|string |
536 | 537 | { |
537 | | - $numRows = count($data); |
| 538 | + $numRows = count($rows); |
538 | 539 | if ($numRows === 0) { |
539 | | - return $this->executeStatement('INSERT INTO ' . $table . ' () VALUES ()'); |
| 540 | + return 0; |
540 | 541 | } |
541 | 542 |
|
542 | 543 | $columns = []; |
543 | 544 | $values = []; |
544 | 545 | $set = []; |
545 | 546 |
|
546 | | - $first = true; |
547 | | - foreach ($data as $row) { |
| 547 | + $first = true; |
| 548 | + $columnCount = 0; |
| 549 | + foreach ($rows as $row) { |
548 | 550 | if ($first) { |
549 | | - $first = false; |
550 | | - $columns = array_keys($row); |
551 | | - $set = array_fill(0, count($columns), '?'); |
| 551 | + $first = false; |
| 552 | + $columns = array_keys($row); |
| 553 | + $columnCount = count($columns); |
| 554 | + $set = array_fill(0, count($columns), '?'); |
| 555 | + } |
| 556 | + |
| 557 | + if ($columnCount !== count($row)) { |
| 558 | + // TODO: add a custom exception |
| 559 | + throw new Exception('Column count mismatch'); |
552 | 560 | } |
553 | 561 |
|
554 | 562 | foreach ($columns as $column) { |
555 | | - $values[] = $row[$column]; |
| 563 | + // TODO: add a custom exception |
| 564 | + $values[] = $row[$column] ?? throw new Exception('Column ' . $column . ' does not exist'); |
556 | 565 | } |
557 | 566 | } |
558 | 567 |
|
559 | 568 | $setParams = '(' . implode(',', $set) . ')'; |
560 | 569 |
|
561 | | - $calculatedTypes = is_string(key($types)) |
| 570 | + $calculatedTypes = $types |
562 | 571 | ? $this->extractTypeValues($columns, $types) |
563 | | - : array_merge($types, array_fill(0, count($columns) - count($types), ParameterType::STRING)); |
| 572 | + : []; |
564 | 573 |
|
565 | 574 | return $this->executeStatement( |
566 | 575 | 'INSERT INTO ' . $table . ' (' . implode(', ', $columns) . ') VALUES ' |
|
0 commit comments