Skip to content

Commit

Permalink
Automatically cast datetime bulk entries into DateTime objects (#929)
Browse files Browse the repository at this point in the history
* Automatically cast datetime bulk entries into DateTime objects

* Satisfy psalm
  • Loading branch information
norberttech authored Jan 22, 2024
1 parent 26385b0 commit 26f0d80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/lib/doctrine-dbal-bulk/src/Flow/Doctrine/Bulk/BulkData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ final class BulkData
private array $rows;

/**
* @psalm-suppress DocblockTypeContradiction
*
* @param array<int, array<string, mixed>> $rows
*/
public function __construct(array $rows)
Expand All @@ -29,13 +27,15 @@ public function __construct(array $rows)

$firstRow = \reset($rows);

/** @psalm-suppress DocblockTypeContradiction */
if (!\is_array($firstRow)) {
throw new RuntimeException('Each row must be an array');
}

$columns = \array_keys($firstRow);

foreach ($rows as $row) {
/** @psalm-suppress DocblockTypeContradiction */
if (!\is_array($row)) {
throw new RuntimeException('Each row must be an array');
}
Expand Down Expand Up @@ -121,11 +121,20 @@ public function toSqlParameters(TableDefinition $table) : array
* @var mixed $entry
*/
foreach ($row as $column => $entry) {
if (\is_string($entry) && ($table->dbalColumn($column)->getType()->getName() === Types::JSON || $table->dbalColumn($column)->getType()->getName() === 'json_array')) {
$rows[$index][$column . '_' . $index] = \json_decode($entry, true, 512, JSON_THROW_ON_ERROR);
} else {
$rows[$index][$column . '_' . $index] = $entry;
}
$rows[$index][$column . '_' . $index] = match (\gettype($entry)) {
'string' => match ($table->dbalColumn($column)->getType()->getName()) {
Types::JSON, 'json_array' => \json_decode($entry, true, 512, JSON_THROW_ON_ERROR),
Types::DATETIME_IMMUTABLE,
Types::DATETIMETZ_IMMUTABLE,
Types::DATE_IMMUTABLE,
Types::TIME_IMMUTABLE => new \DateTimeImmutable($entry),
Types::DATE_MUTABLE,
Types::DATETIME_MUTABLE,
Types::DATETIMETZ_MUTABLE => new \DateTime($entry),
default => $entry
},
default => $entry
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public function test_inserts_multiple_rows_at_once() : void
->setPrimaryKey(['id'])
);

$date1 = new \DateTimeImmutable();

Bulk::create()->insert(
$this->databaseContext->connection(),
$table,
new BulkData([
['id' => $id1 = \bin2hex(\random_bytes(5)), 'age' => 20, 'name' => 'Name One', 'description' => 'Description One', 'active' => false, 'updated_at' => $date1 = new \DateTimeImmutable(), 'tags' => \json_encode(['a', 'b', 'c'])],
['id' => $id1 = \bin2hex(\random_bytes(5)), 'age' => 20, 'name' => 'Name One', 'description' => 'Description One', 'active' => false, 'updated_at' => $date1->format(\DateTimeInterface::ATOM), 'tags' => \json_encode(['a', 'b', 'c'])],
['id' => $id2 = \bin2hex(\random_bytes(5)), 'age' => 30, 'name' => 'Name Two', 'description' => null, 'active' => true, 'updated_at' => $date2 = new \DateTimeImmutable(), 'tags' => \json_encode(['a', 'b', 'c'])],
['id' => $id3 = \bin2hex(\random_bytes(5)), 'age' => 40, 'name' => 'Name Three', 'description' => 'Description Three', 'active' => false, 'updated_at' => $date3 = new \DateTimeImmutable(), 'tags' => \json_encode(['a', 'b', 'c'])],
])
Expand Down

0 comments on commit 26f0d80

Please sign in to comment.