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

[PD-106] Change SQL statements to query builder syntax #21

Merged
merged 3 commits into from
Feb 2, 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
64 changes: 60 additions & 4 deletions src/Repository/IndexQueueRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Pimcore\Bundle\GenericDataIndexBundle\Repository;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder as DBALQueryBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
Expand All @@ -29,6 +30,10 @@ final class IndexQueueRepository
{
use LoggerAwareTrait;

public const AND_OPERATOR = 'and';

public const OR_OPERATOR = 'or';

public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly TimeServiceInterface $timeService,
Expand Down Expand Up @@ -131,10 +136,34 @@ public function denormalizeDatabaseEntry(array $entry): IndexQueue
return $this->denormalizer->denormalize($entry, IndexQueue::class);
}

public function generateSelectQuery(
string $tableName,
array $fields,
string $idField = 'id',
array $params = [],
array $whereParameters = []
): DBALQueryBuilder {
$fields = $this->quoteParameters($fields);
array_unshift($fields, $idField);

$qb = $this->connection->createQueryBuilder()
->select($fields)
->from($tableName);

$this->addWhereStatements($qb, $whereParameters);

if (!empty($params)) {
$params = $this->quoteParameters($params);
$qb->setParameters($params);
}

return $qb;
}

/**
* @throws \Doctrine\DBAL\Exception
*/
public function enqueueBySelectQuery(string $selectQuery, array $params = []): void
public function enqueueBySelectQuery(DBALQueryBuilder $queryBuilder): void
{
$sql = <<<SQL
INSERT INTO
Expand All @@ -147,9 +176,8 @@ public function enqueueBySelectQuery(string $selectQuery, array $params = []): v
dispatched = VALUES(dispatched)
SQL;

$sql = sprintf($sql, IndexQueue::TABLE, $selectQuery);

$this->connection->executeQuery($sql, $params);
$sql = sprintf($sql, IndexQueue::TABLE, $queryBuilder->getSQL());
$this->connection->executeQuery($sql, $queryBuilder->getParameters());
}

/**
Expand Down Expand Up @@ -177,4 +205,32 @@ private function createQueryBuilder(string $alias): QueryBuilder
return $this->entityManager->getRepository(IndexQueue::class)
->createQueryBuilder($alias);
}

private function quoteParameters(array $parameters): array
{
return array_map(
function ($parameter) {
if (is_string($parameter)) {
return $this->connection->quote($parameter);
}

return $parameter;
},
$parameters
);
}

private function addWhereStatements(DBALQueryBuilder $queryBuilder, array $whereParameters): DBALQueryBuilder
{
foreach ($whereParameters as $operator => $parameter) {
$predicate = $parameter . ' = :' . $parameter;
match (true) {
$operator === self::AND_OPERATOR => $queryBuilder->andWhere($predicate),
$operator === self::OR_OPERATOR => $queryBuilder->orWhere($predicate),
default => $queryBuilder->where($predicate),
};
}

return $queryBuilder;
}
}
62 changes: 41 additions & 21 deletions src/Service/SearchIndex/IndexQueue/EnqueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,44 @@ public function __construct(
*/
public function enqueueByTag(Tag $tag): EnqueueService
{
$tagCondition = "WHERE id IN (SELECT cid FROM tags_assignment WHERE ctype='%s' AND tagid = %s)";
$tagCondition = $this->indexQueueRepository->generateSelectQuery(
'tags_assignment',
[],
'cid',
[],
['ctype', IndexQueueRepository::AND_OPERATOR => 'tagid']
);

//assets
$this->indexQueueRepository->enqueueBySelectQuery(
sprintf("SELECT id, '%s', '%s', '%s', '%s', 0 FROM assets " . $tagCondition,
$assetQuery = $this->indexQueueRepository->generateSelectQuery(
'assets',
[
ElementType::ASSET->value,
IndexName::ASSET->value,
IndexQueueOperation::UPDATE->value,
$this->timeService->getCurrentMillisecondTimestamp(),
$tag->getId(),
'asset'
)
0,
],
'id',
['ctype' => ElementType::ASSET->value, 'tagid' => $tag->getId()],
);
$assetQuery->where($assetQuery->expr()->in('id', $tagCondition->getSQL()));
$this->indexQueueRepository->enqueueBySelectQuery($assetQuery);

//data objects
$this->indexQueueRepository->enqueueBySelectQuery(
sprintf("SELECT id, className, '%s', '%s', '%s', 0 FROM objects " . $tagCondition,
$dataObjectQuery = $this->indexQueueRepository->generateSelectQuery(
'objects',
[
ElementType::DATA_OBJECT->value,
IndexQueueOperation::UPDATE->value,
$this->timeService->getCurrentMillisecondTimestamp(),
$tag->getId(),
'object'
)
0,
],
'id, className',
['ctype' => ElementType::DATA_OBJECT->value, 'tagid' => $tag->getId()],
);
$dataObjectQuery->where($dataObjectQuery->expr()->in('id', $tagCondition->getSQL()));
$this->indexQueueRepository->enqueueBySelectQuery($dataObjectQuery);

return $this;
}
Expand All @@ -78,15 +92,19 @@ public function enqueueByTag(Tag $tag): EnqueueService
public function enqueueByClassDefinition(ClassDefinition $classDefinition): EnqueueService
{
$dataObjectTableName = 'object_' . $classDefinition->getId();

$this->indexQueueRepository->enqueueBySelectQuery(
sprintf("SELECT oo_id, '%s', '%s', '%s', '%s', 0 FROM %s",
$selectQuery = $this->indexQueueRepository->generateSelectQuery(
$dataObjectTableName,
[
ElementType::DATA_OBJECT->value,
$classDefinition->getName(),
IndexQueueOperation::UPDATE->value,
$this->timeService->getCurrentMillisecondTimestamp(),
$dataObjectTableName
)
0,
],
'oo_id'
);
$this->indexQueueRepository->enqueueBySelectQuery(
$selectQuery
);

return $this;
Expand All @@ -98,15 +116,17 @@ public function enqueueByClassDefinition(ClassDefinition $classDefinition): Enqu
public function enqueueAssets(): EnqueueService
{
try {
$this->indexQueueRepository->enqueueBySelectQuery(
sprintf("SELECT id, '%s', '%s', '%s', '%s', 0 FROM %s",
$selectQuery = $this->indexQueueRepository->generateSelectQuery(
'assets',
[
ElementType::ASSET->value,
IndexName::ASSET->value,
IndexQueueOperation::UPDATE->value,
$this->timeService->getCurrentMillisecondTimestamp(),
'assets'
)
0,
]
);
$this->indexQueueRepository->enqueueBySelectQuery($selectQuery);
} catch (Exception $e) {
throw new EnqueueAssetsException(
$e->getMessage()
Expand All @@ -133,7 +153,7 @@ public function enqueueRelatedItemsOnUpdate(
);

if ($subQuery) {
$this->indexQueueRepository->enqueueBySelectQuery($subQuery->getSQL(), $subQuery->getParameters());
$this->indexQueueRepository->enqueueBySelectQuery($subQuery);
}
}

Expand Down
Loading