1
0

change_collation_sql.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreScripts Scripts
  7. * @{
  8. */
  9. $aPathInfo = pathinfo(__FILE__);
  10. require_once ($aPathInfo['dirname'] . '/../inc/header.inc.php');
  11. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  12. /**
  13. * Command line interface for generating a list of service functions
  14. */
  15. class BxDolChangeCOllationSqlCmd
  16. {
  17. protected $_sTablesPrefix = '';
  18. protected $_aTextTypes = array('char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext');
  19. public function main()
  20. {
  21. $a = getopt('hp:');
  22. if (isset($a['h']) || !$a)
  23. $this->finish(0, $this->getHelp());
  24. if (isset($a['p']))
  25. $this->_sTablesPrefix = $a['p'];
  26. if (!$this->_sTablesPrefix)
  27. $this->finish(1, 'Tables prefix is mandatory');
  28. $this->process();
  29. }
  30. protected function process()
  31. {
  32. $oDb = BxDolDb::getInstance();
  33. $aTables = $oDb->getColumn("SHOW TABLES FROM `" . BX_DATABASE_NAME . "` LIKE ?", [$this->_sTablesPrefix . '%']);
  34. $s = '';
  35. foreach ($aTables as $sTable) {
  36. $s .= "-- TABLE: " . $sTable . "\n\n";
  37. $s .= "ALTER TABLE `$sTable` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\n";
  38. $aFields = $oDb->getAll("SHOW COLUMNS FROM `" . $sTable . "`");
  39. foreach ($aFields as $r) {
  40. if (!$this->isTextField($r['Type']))
  41. continue;
  42. $s .= "ALTER TABLE `$sTable` CHANGE `{$r['Field']}` `{$r['Field']}` {$r['Type']} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n";
  43. }
  44. $s .= "\n";
  45. $s .= "REPAIR TABLE `$sTable`;\n";
  46. $s .= "OPTIMIZE TABLE `$sTable`;\n\n\n";
  47. }
  48. $this->finish(0, $s);
  49. }
  50. protected function isTextField($sType)
  51. {
  52. foreach ($this->_aTextTypes as $s) {
  53. if (0 === strpos($sType, $s))
  54. return true;
  55. }
  56. return false;
  57. }
  58. protected function getHelp()
  59. {
  60. $n = 21;
  61. $s = "\nUsage: php change_collation_sql.php [options]\n";
  62. $s .= str_pad("\t -h", $n) . "Print this help\n";
  63. $s .= str_pad("\t -p <prefix>", $n) . "Tables prefix to prins SQL queries for\n\n";
  64. $s .= "\n";
  65. return $s;
  66. }
  67. protected function finish ($iCode, $sMsg = null, $bAddLn = true)
  68. {
  69. if (null !== $sMsg)
  70. echo $sMsg . ($bAddLn ? "\n" : '');
  71. exit($iCode);
  72. }
  73. }
  74. $o = new BxDolChangeCOllationSqlCmd();
  75. $o->main();
  76. /** @} */