Keine Beschreibung

DBCollationChecker.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php declare(strict_types = 1);
  2. namespace MailPoet\Util;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Doctrine\ORM\EntityManager;
  5. class DBCollationChecker {
  6. /** @var EntityManager */
  7. private $entityManager;
  8. public function __construct(
  9. EntityManager $entityManager
  10. ) {
  11. $this->entityManager = $entityManager;
  12. }
  13. /**
  14. * If two columns have a different collations returns MySQL's COLLATE command to be used with the target table column.
  15. * e.g. WHERE source_table.column = target_table.column COLLATE xyz
  16. */
  17. public function getCollateIfNeeded(string $sourceTable, string $sourceColumn, string $targetTable, string $targetColumn): string {
  18. $connection = $this->entityManager->getConnection();
  19. $sourceColumnData = $connection->executeQuery("SHOW FULL COLUMNS FROM $sourceTable WHERE Field = '$sourceColumn';")->fetchAllAssociative();
  20. $sourceCollation = $sourceColumnData[0]['Collation'] ?? '';
  21. $targetColumnData = $connection->executeQuery("SHOW FULL COLUMNS FROM $targetTable WHERE Field = '$targetColumn';")->fetchAllAssociative();
  22. $targetCollation = $targetColumnData[0]['Collation'] ?? '';
  23. if ($sourceCollation !== $targetCollation) {
  24. return "COLLATE $sourceCollation";
  25. }
  26. return '';
  27. }
  28. }