<?php declare(strict_types = 1); namespace MailPoet\Util; if (!defined('ABSPATH')) exit; use MailPoetVendor\Doctrine\ORM\EntityManager; class DBCollationChecker { /** @var EntityManager */ private $entityManager; public function __construct( EntityManager $entityManager ) { $this->entityManager = $entityManager; } /** * If two columns have incompatible 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 * * In MySQL, if you have the same charset and collation in joined tables' columns it's perfect; * if you have different charsets, utf8 and utf8mb4, it works too; but if you have the same charset * with different collations, e.g. utf8mb4_unicode_ci and utf8mb4_unicode_520_ci, it will fail * with an 'Illegal mix of collations' error. */ 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 ''; } list($sourceCharset) = explode('_', $sourceCollation); list($targetCharset) = explode('_', $targetCollation); if ($sourceCharset === $targetCharset) { return "COLLATE $sourceCollation"; } return ''; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
DataInconsistency | Folder | 0755 |
|
|
License | Folder | 0755 |
|
|
Notices | Folder | 0755 |
|
|
pQuery | Folder | 0755 |
|
|
APIPermissionHelper.php | File | 1.26 KB | 0644 |
|
CdnAssetUrl.php | File | 576 B | 0644 |
|
ConflictResolver.php | File | 7.85 KB | 0644 |
|
Cookies.php | File | 1.1 KB | 0644 |
|
DBCollationChecker.php | File | 1.76 KB | 0644 |
|
DOM.php | File | 1.42 KB | 0644 |
|
DateConverter.php | File | 3.34 KB | 0644 |
|
FreeDomains.php | File | 3.14 KB | 0644 |
|
Headers.php | File | 734 B | 0644 |
|
Helpers.php | File | 4.16 KB | 0644 |
|
Installation.php | File | 836 B | 0644 |
|
LegacyDatabase.php | File | 3.78 KB | 0644 |
|
Request.php | File | 325 B | 0644 |
|
SecondLevelDomainNames.php | File | 356 B | 0644 |
|
Security.php | File | 2.81 KB | 0644 |
|
Url.php | File | 1.74 KB | 0644 |
|
index.php | File | 6 B | 0644 |
|