Spaces:
No application file
No application file
File size: 1,891 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php
declare(strict_types=1);
namespace Mautic\LeadBundle\Field\Helper;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\ORM\EntityManager;
use Mautic\LeadBundle\Entity\Lead;
/**
* Helper for getting and counting indexes on lead table.
*
* @see Lead
*/
class IndexHelper
{
public const MAX_COUNT_ALLOWED = 64;
/**
* @var bool|array<string>
*/
private $indexedColumns = false;
/**
* Can be different from indexed column count when using multiple indexes on same table.
*/
private int $indexCount = 0;
public function __construct(private EntityManager $entityManager)
{
}
/**
* @return array<string>|bool
*/
public function getIndexedColumnNames()
{
$this->getIndexes();
return $this->indexedColumns;
}
public function getIndexCount(): int
{
$this->getIndexes();
return $this->indexCount;
}
public function getMaxCount(): int
{
return self::MAX_COUNT_ALLOWED;
}
public function isNewIndexAllowed(): bool
{
return $this->getIndexCount() < $this->getMaxCount();
}
/**
* Get indexes created on `leads` table.
*
* @see Lead
*
* @throws DBALException
*/
private function getIndexes(): void
{
if (false !== $this->indexedColumns) {
// Query below performed
return;
}
$tableName = $this->entityManager->getClassMetadata(Lead::class)->getTableName();
$sql = "SHOW INDEXES FROM `$tableName`";
$stmt = $this->entityManager->getConnection()->prepare($sql);
$indexes = $stmt->executeQuery()->fetchAllAssociative();
$this->indexedColumns = array_map(
fn ($index) => $index['Column_name'],
$indexes
);
$this->indexCount = count($indexes);
}
}
|