add filter for generic doc + fix issues in filter

This commit is contained in:
2023-05-30 12:46:05 +02:00
parent a3d3588b75
commit eb107f5a15
10 changed files with 109 additions and 25 deletions

View File

@@ -32,6 +32,7 @@ class Manager
}
/**
* @param list<string> $places
* @throws Exception
*/
public function countDocForAccompanyingPeriod(
@@ -39,16 +40,16 @@ class Manager
?\DateTimeImmutable $startDate = null,
?\DateTimeImmutable $endDate = null,
?string $content = null,
?string $origin = null
array $places = []
): int {
['sql' => $sql, 'params' => $params] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $origin);
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $places);
if ($sql === '') {
return 0;
}
$countSql = "SELECT count(*) AS c FROM ({$sql}) AS sq";
$result = $this->connection->executeQuery($countSql, $params);
$result = $this->connection->executeQuery($countSql, $params, $types);
$number = $result->fetchOne();
@@ -60,6 +61,7 @@ class Manager
}
/**
* @param list<string> $places places to search. When empty, search in all places
* @throws Exception
*/
public function findDocForAccompanyingPeriod(
@@ -69,15 +71,19 @@ class Manager
?\DateTimeImmutable $startDate = null,
?\DateTimeImmutable $endDate = null,
?string $content = null,
?string $origin = null
array $places = []
): iterable {
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $origin);
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $places);
if ($sql === '') {
return [];
}
$runSql = "{$sql} ORDER BY doc_date DESC LIMIT ? OFFSET ?";
$runParams = [...$params, ...[$limit, $offset]];
$runTypes = [...$types, ...[Types::INTEGER, Types::INTEGER]];
foreach($this->connection->iterateAssociative($runSql, $runParams, $runTypes) as $row) {
foreach ($this->connection->iterateAssociative($runSql, $runParams, $runTypes) as $row) {
yield new GenericDocDTO(
$row['key'],
json_decode($row['identifiers'], true, 512, JSON_THROW_ON_ERROR),
@@ -87,14 +93,34 @@ class Manager
}
}
public function placesForAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod): array
{
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod);
if ($sql === '') {
return [];
}
$runSql = "SELECT DISTINCT key FROM ({$sql}) AS sq";
$keys = [];
foreach ($this->connection->iterateAssociative($runSql, $params, $types) as $k) {
$keys[] = $k['key'];
}
return $keys;
}
/**
* @param list<string> $places places to search. When empty, search in all places
*/
private function buildUnionQuery(
AccompanyingPeriod|Person $linked,
?\DateTimeImmutable $startDate = null,
?\DateTimeImmutable $endDate = null,
?string $content = null,
?string $origin = null
array $places = [],
): array {
$sql = [];
$params = [];
@@ -105,17 +131,20 @@ class Manager
if (!$provider->isAllowedForAccompanyingPeriod($linked)) {
continue;
}
$query = $provider->buildFetchQueryForAccompanyingPeriod($linked, $startDate, $endDate, $content);
['sql' => $q, 'params' => $p, 'types' => $t ] = $this->builder
->toSql($provider->buildFetchQueryForAccompanyingPeriod($linked, $startDate, $endDate, $content, $origin));
$params = [...$params, ...$p];
$types = [...$types, ...$t];
if ([] !== $places and !in_array($query->getSelectKeyString(), $places, true)) {
continue;
}
['sql' => $q, 'params' => $p, 'types' => $t ] = $this->builder->toSql($query);
$sql[] = $q;
$params = [...$params, ...$p];
$types = [...$types, ...$t];
}
}
return ['sql' => implode(' UNION ', $sql), 'params' => $params, 'types' => $types];
}
}