1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreBundle\Doctrine\Helper; |
15
|
|
|
|
16
|
|
|
use Doctrine\DBAL\Connection; |
17
|
|
|
use Doctrine\DBAL\Driver\AbstractMySQLDriver; |
18
|
|
|
|
19
|
|
|
class CharsetRecodeHelper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Connection |
23
|
|
|
*/ |
24
|
|
|
private $conn; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
Connection $connection |
28
|
|
|
) { |
29
|
|
|
$this->conn = $connection; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function isRequired(): bool |
33
|
|
|
{ |
34
|
|
|
$driver = $this->conn->getDriver(); |
35
|
|
|
|
36
|
|
|
// recoding utf8 to utf8mb4 is only required for mysql |
37
|
|
|
return $driver instanceof AbstractMySQLDriver; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getCommands(): array |
41
|
|
|
{ |
42
|
|
|
if (!$this->isRequired()) { |
43
|
|
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// the following is based on |
47
|
|
|
// https://dba.stackexchange.com/questions/8239/how-to-easily-convert-utf8-tables-to-utf8mb4-in-mysql-5-5#answer-104866 |
48
|
|
|
$dbName = $this->conn->getDatabase(); |
49
|
|
|
$tableFilter = 'table_schema LIKE "' . $dbName . '"'; |
50
|
|
|
$commands = []; |
51
|
|
|
|
52
|
|
|
$this->conn->executeQuery('use information_schema;'); |
53
|
|
|
|
54
|
|
|
// database level |
55
|
|
|
$rows = $this->retrieveCommands(' |
56
|
|
|
SELECT CONCAT("ALTER DATABASE `",table_schema,"` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;") AS _sql |
57
|
|
|
FROM `TABLES` |
58
|
|
|
WHERE ' . $tableFilter . ' |
59
|
|
|
GROUP BY table_schema; |
60
|
|
|
'); |
61
|
|
|
$commands = array_merge($commands, $rows); |
62
|
|
|
|
63
|
|
|
// table level |
64
|
|
|
$rows = $this->retrieveCommands(' |
65
|
|
|
SELECT CONCAT("ALTER TABLE `",table_schema,"`.`",table_name,"` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") AS _sql |
66
|
|
|
FROM `TABLES` |
67
|
|
|
WHERE ' . $tableFilter . ' |
68
|
|
|
GROUP BY table_schema, table_name; |
69
|
|
|
'); |
70
|
|
|
$commands = array_merge($commands, $rows); |
71
|
|
|
|
72
|
|
|
// column level |
73
|
|
|
$rows = $this->retrieveCommands(' |
74
|
|
|
SELECT CONCAT("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",IF(is_nullable="YES"," NULL"," NOT NULL"),";") AS _sql |
75
|
|
|
FROM `COLUMNS` |
76
|
|
|
WHERE ' . $tableFilter . ' |
77
|
|
|
AND data_type IN (\'varchar\', \'char\'); |
78
|
|
|
'); |
79
|
|
|
$commands = array_merge($commands, $rows); |
80
|
|
|
|
81
|
|
|
$rows = $this->retrieveCommands(' |
82
|
|
|
SELECT CONCAT("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",IF(is_nullable="YES"," NULL"," NOT NULL"),";") AS _sql |
83
|
|
|
FROM `COLUMNS` |
84
|
|
|
WHERE ' . $tableFilter . ' |
85
|
|
|
AND data_type IN (\'text\', \'tinytext\', \'mediumtext\', \'longtext\'); |
86
|
|
|
'); |
87
|
|
|
$commands = array_merge($commands, $rows); |
88
|
|
|
|
89
|
|
|
$this->conn->executeQuery('use ' . $dbName . ';'); |
90
|
|
|
|
91
|
|
|
return $commands; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function retrieveCommands(string $sql): array |
95
|
|
|
{ |
96
|
|
|
$result = []; |
97
|
|
|
$stmt = $this->conn->executeQuery($sql); |
98
|
|
|
while ($row = $stmt->fetch()) { |
99
|
|
|
$result[] = $row['_sql']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|