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

Optionally delegate metadata matching to persistence strategy #232

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions src/HasMetadataMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of prooph/pdo-event-store.
* (c) 2016-2021 Alexander Miertsch <[email protected]>
* (c) 2016-2021 Sascha-Oliver Prolic <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Prooph\EventStore\Pdo;

use Prooph\EventStore\Metadata\MetadataMatcher;

/**
* Additional interface to be implemented for persistence strategies
* to to provide their own logic to create where clauses
*/
interface HasMetadataMatcher
{
public function createWhereClause(?MetadataMatcher $metadataMatcher): array;
}
4 changes: 4 additions & 0 deletions src/MySqlEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ public function fetchCategoryNamesRegex(string $filter, int $limit = 20, int $of

private function createWhereClause(?MetadataMatcher $metadataMatcher): array
{
if ($this->persistenceStrategy instanceof HasMetadataMatcher) {
return $this->persistenceStrategy->createWhereClause($metadataMatcher);
}

$where = [];
$values = [];

Expand Down
85 changes: 85 additions & 0 deletions src/PersistenceStrategy/MySqlSingleStreamStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@

use Iterator;
use Prooph\Common\Messaging\MessageConverter;
use Prooph\EventStore\Metadata\FieldType;
use Prooph\EventStore\Metadata\MetadataMatcher;
use Prooph\EventStore\Metadata\Operator;
use Prooph\EventStore\Pdo\DefaultMessageConverter;
use Prooph\EventStore\Pdo\HasQueryHint;
use Prooph\EventStore\Pdo\Util\Json;
use Prooph\EventStore\StreamName;

final class MySqlSingleStreamStrategy implements MySqlPersistenceStrategy, HasQueryHint
{
private static $metadataFieldsToColumnMap = [
'_aggregate_version' => 'aggregate_version',
'_aggregate_id' => 'aggregate_id',
'_aggregate_type' => 'aggregate_type',
];

/**
* @var MessageConverter
*/
Expand Down Expand Up @@ -96,4 +105,80 @@ public function indexName(): string
{
return 'ix_query_aggregate';
}

public function createWhereClause(?MetadataMatcher $metadataMatcher): array
{
$where = [];
$values = [];

if (! $metadataMatcher) {
return [
$where,
$values,
];
}

foreach ($metadataMatcher->data() as $key => $match) {
/** @var FieldType $fieldType */
$fieldType = $match['fieldType'];
$field = $match['field'];
/** @var Operator $operator */
$operator = $match['operator'];
$value = $match['value'];
$parameters = [];

if (\is_array($value)) {
foreach ($value as $k => $v) {
$parameters[] = ':metadata_' . $key . '_' . $k;
}
} else {
$parameters = [':metadata_' . $key];
}

$parameterString = \implode(', ', $parameters);

$operatorStringEnd = '';

if ($operator->is(Operator::REGEX())) {
$operatorString = 'REGEXP';
} elseif ($operator->is(Operator::IN())) {
$operatorString = 'IN (';
$operatorStringEnd = ')';
} elseif ($operator->is(Operator::NOT_IN())) {
$operatorString = 'NOT IN (';
$operatorStringEnd = ')';
} else {
$operatorString = $operator->getValue();
}

if ($fieldType->is(FieldType::METADATA()) && !array_key_exists($field, self::$metadataFieldsToColumnMap)) {
if (\is_bool($value)) {
$where[] = "metadata->\"$.$field\" $operatorString " . \var_export($value, true) . ' '. $operatorStringEnd;
continue;
}

$where[] = "JSON_UNQUOTE(metadata->\"$.$field\") $operatorString $parameterString $operatorStringEnd";
} else {
if (array_key_exists($field, self::$metadataFieldsToColumnMap)) {
$field = self::$metadataFieldsToColumnMap[$field];
}
if (\is_bool($value)) {
$where[] = "$field $operatorString " . \var_export($value, true) . ' ' . $operatorStringEnd;
continue;
}

$where[] = "$field $operatorString $parameterString $operatorStringEnd";
}

$value = (array) $value;
foreach ($value as $k => $v) {
$values[$parameters[$k]] = $v;
}
}

return [
$where,
$values,
];
}
}