finish(0, $this->getHelp()); if (isset($a['p'])) $this->_sTablesPrefix = $a['p']; if (!$this->_sTablesPrefix) $this->finish(1, 'Tables prefix is mandatory'); $this->process(); } protected function process() { $oDb = BxDolDb::getInstance(); $aTables = $oDb->getColumn("SHOW TABLES FROM `" . BX_DATABASE_NAME . "` LIKE ?", [$this->_sTablesPrefix . '%']); $s = ''; foreach ($aTables as $sTable) { $s .= "-- TABLE: " . $sTable . "\n\n"; $s .= "ALTER TABLE `$sTable` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\n"; $aFields = $oDb->getAll("SHOW COLUMNS FROM `" . $sTable . "`"); foreach ($aFields as $r) { if (!$this->isTextField($r['Type'])) continue; $s .= "ALTER TABLE `$sTable` CHANGE `{$r['Field']}` `{$r['Field']}` {$r['Type']} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n"; } $s .= "\n\n"; } $this->finish(0, $s); } protected function isTextField($sType) { foreach ($this->_aTextTypes as $s) { if (0 === strpos($sType, $s)) return true; } return false; } protected function getHelp() { $n = 21; $s = "\nUsage: php change_collation_sql.php [options]\n"; $s .= str_pad("\t -h", $n) . "Print this help\n"; $s .= str_pad("\t -p ", $n) . "Tables prefix to prins SQL queries for\n\n"; $s .= "\n"; return $s; } protected function finish ($iCode, $sMsg = null, $bAddLn = true) { if (null !== $sMsg) echo $sMsg . ($bAddLn ? "\n" : ''); exit($iCode); } } $o = new BxDolChangeCOllationSqlCmd(); $o->main(); /** @} */