Files
piratepoet/lib/Util/DBCollationChecker.php
Rostislav Wolny 8df940e02c Add strict types to newly added classes
[MAILPOET-3762]
2021-09-07 10:07:08 +02:00

34 lines
1.2 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\Util;
use MailPoetVendor\Doctrine\ORM\EntityManager;
class DBCollationChecker {
/** @var EntityManager */
private $entityManager;
public function __construct(
EntityManager $entityManager
) {
$this->entityManager = $entityManager;
}
/**
* If two columns have a different collations returns MySQL's COLLATE command to be used with the target table column.
* e.g. WHERE source_table.column = target_table.column COLLATE xyz
*/
public function getCollateIfNeeded(string $sourceTable, string $sourceColumn, string $targetTable, string $targetColumn): string {
$connection = $this->entityManager->getConnection();
$sourceColumnData = $connection->executeQuery("SHOW FULL COLUMNS FROM $sourceTable WHERE Field = '$sourceColumn';")->fetchAllAssociative();
$sourceCollation = $sourceColumnData[0]['Collation'] ?? '';
$targetColumnData = $connection->executeQuery("SHOW FULL COLUMNS FROM $targetTable WHERE Field = '$targetColumn';")->fetchAllAssociative();
$targetCollation = $targetColumnData[0]['Collation'] ?? '';
if ($sourceCollation !== $targetCollation) {
return "COLLATE $sourceCollation";
}
return '';
}
}