diff --git a/src/Store/DoctrineDbalStore.php b/src/Store/DoctrineDbalStore.php index c700da87b..82f9241a6 100644 --- a/src/Store/DoctrineDbalStore.php +++ b/src/Store/DoctrineDbalStore.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; use Patchlevel\EventSourcing\EventBus\HeaderNotFound; @@ -30,7 +31,6 @@ public function __construct( private readonly EventSerializer $serializer, private readonly AggregateRootRegistry $aggregateRootRegistry, private readonly string $storeTableName = 'eventstore', - private readonly int $batch = 1000, ) { } @@ -118,7 +118,14 @@ public function save(Message ...$messages): void { $this->connection->transactional( function (Connection $connection) use ($messages): void { + $booleanType = Type::getType(Types::BOOLEAN); + $jsonType = Type::getType(Types::JSON); + $dateTimeType = Type::getType(Types::DATETIMETZ_IMMUTABLE); + + $parameters = []; $placeholders = []; + + /** @var array $types */ $types = []; $columns = [ @@ -134,7 +141,6 @@ function (Connection $connection) use ($messages): void { ]; $columnsLength = count($columns); - $placeholder = implode(', ', array_fill(0, count($columns), '?')); foreach ($messages as $position => $message) { @@ -154,12 +160,12 @@ function (Connection $connection) use ($messages): void { throw new MissingDataForStorage($e->name, $e); } - $offset = $position * $columnsLength; + $offset = (int)$position * $columnsLength; - $types[$offset + 5] = Types::DATETIMETZ_IMMUTABLE; - $types[$offset + 6] = Types::BOOLEAN; - $types[$offset + 7] = Types::BOOLEAN; - $types[$offset + 8] = Types::JSON; + $types[$offset + 5] = $dateTimeType; + $types[$offset + 6] = $booleanType; + $types[$offset + 7] = $booleanType; + $types[$offset + 8] = $jsonType; $placeholders[] = $placeholder; } @@ -180,22 +186,6 @@ function (Connection $connection) use ($messages): void { ); } - /** - * @param string[] $fields - * - * @return string[] - */ - private function placeholder(array $fields): array - { - $placeholders = []; - - foreach ($fields as $field) { - $placeholders[] = ':' . $field; - } - - return $placeholders; - } - /** * @param Closure():ClosureReturn $function * diff --git a/tests/Unit/Store/DoctrineDbalStoreTest.php b/tests/Unit/Store/DoctrineDbalStoreTest.php index 6daa54eda..162cf7018 100644 --- a/tests/Unit/Store/DoctrineDbalStoreTest.php +++ b/tests/Unit/Store/DoctrineDbalStoreTest.php @@ -12,6 +12,7 @@ use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Result; use Doctrine\DBAL\Statement; +use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use EmptyIterator; use Patchlevel\EventSourcing\EventBus\Message; @@ -191,27 +192,14 @@ public function testSaveWithOneEvent(): void $innerMockedConnection = $this->prophesize(Connection::class); - $platform = $this->prophesize(AbstractPlatform::class); - $innerMockedConnection->getDatabasePlatform()->willReturn($platform->reveal()); - - $innerMockedConnection->insert( - 'eventstore', - [ - 'aggregate' => 'profile', - 'aggregate_id' => '1', - 'playhead' => 1, - 'event' => 'profile_created', - 'payload' => '', - 'recorded_on' => $recordedOn, - 'new_stream_start' => false, - 'archived' => false, - 'custom_headers' => [], - ], + $innerMockedConnection->executeStatement( + "INSERT INTO eventstore (aggregate, aggregate_id, playhead, event, payload, recorded_on, new_stream_start, archived, custom_headers) VALUES\n(?, ?, ?, ?, ?, ?, ?, ?, ?)", + ['profile', '1', 1, 'profile_created', '', $recordedOn, false, false, []], [ - 'recorded_on' => 'datetimetz_immutable', - 'custom_headers' => 'json', - 'archived' => Types::BOOLEAN, - 'new_stream_start' => Types::BOOLEAN, + 5 => Type::getType(Types::DATETIMETZ_IMMUTABLE), + 6 => Type::getType(Types::BOOLEAN), + 7 => Type::getType(Types::BOOLEAN), + 8 => Type::getType(Types::JSON), ], )->shouldBeCalledOnce();