Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically cast datetime bulk entries into DateTime objects #929

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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