ExpressionConverter.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * DB expression converter
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\DB;
  9. class ExpressionConverter
  10. {
  11. /**
  12. * Maximum length for many MySql identifiers, including database, table, trigger, and column names
  13. */
  14. const MYSQL_IDENTIFIER_LEN = 64;
  15. /**
  16. * Dictionary maps common words in identifiers to abbreviations
  17. *
  18. * @var array
  19. */
  20. protected static $_translateMap = [
  21. 'address' => 'addr',
  22. 'admin' => 'adm',
  23. 'attribute' => 'attr',
  24. 'enterprise' => 'ent',
  25. 'catalog' => 'cat',
  26. 'category' => 'ctgr',
  27. 'customer' => 'cstr',
  28. 'notification' => 'ntfc',
  29. 'product' => 'prd',
  30. 'session' => 'sess',
  31. 'user' => 'usr',
  32. 'entity' => 'entt',
  33. 'datetime' => 'dtime',
  34. 'decimal' => 'dec',
  35. 'varchar' => 'vchr',
  36. 'index' => 'idx',
  37. 'compare' => 'cmp',
  38. 'bundle' => 'bndl',
  39. 'option' => 'opt',
  40. 'gallery' => 'glr',
  41. 'media' => 'mda',
  42. 'value' => 'val',
  43. 'link' => 'lnk',
  44. 'title' => 'ttl',
  45. 'super' => 'spr',
  46. 'label' => 'lbl',
  47. 'website' => 'ws',
  48. 'aggregat' => 'aggr',
  49. 'minimal' => 'min',
  50. 'inventory' => 'inv',
  51. 'status' => 'sts',
  52. 'agreement' => 'agrt',
  53. 'layout' => 'lyt',
  54. 'resource' => 'res',
  55. 'directory' => 'dir',
  56. 'downloadable' => 'dl',
  57. 'element' => 'elm',
  58. 'fieldset' => 'fset',
  59. 'checkout' => 'chkt',
  60. 'newsletter' => 'nlttr',
  61. 'shipping' => 'shpp',
  62. 'calculation' => 'calc',
  63. 'search' => 'srch',
  64. 'query' => 'qr',
  65. ];
  66. /**
  67. * Shorten name by abbreviating words
  68. *
  69. * @param string $name
  70. * @return string
  71. */
  72. public static function shortName($name)
  73. {
  74. return strtr($name, self::$_translateMap);
  75. }
  76. /**
  77. * Add an abbreviation to the dictionary, or replace if it already exists
  78. *
  79. * @param string $from
  80. * @param string $to
  81. * @return void
  82. */
  83. public static function addTranslate($from, $to)
  84. {
  85. self::$_translateMap[$from] = $to;
  86. }
  87. /**
  88. * Shorten the name of a MySql identifier, by abbreviating common words and hashing if necessary. Prepends the
  89. * given prefix to clarify what kind of entity the identifier represents, in case hashing is used.
  90. *
  91. * @param string $entityName
  92. * @param string $prefix
  93. * @return string
  94. */
  95. public static function shortenEntityName($entityName, $prefix)
  96. {
  97. if (strlen($entityName) > self::MYSQL_IDENTIFIER_LEN) {
  98. $shortName = ExpressionConverter::shortName($entityName);
  99. if (strlen($shortName) > self::MYSQL_IDENTIFIER_LEN) {
  100. $hash = md5($entityName);
  101. if (strlen($prefix . $hash) > self::MYSQL_IDENTIFIER_LEN) {
  102. $entityName = self::trimHash($hash, $prefix, self::MYSQL_IDENTIFIER_LEN);
  103. } else {
  104. $entityName = $prefix . $hash;
  105. }
  106. } else {
  107. $entityName = $shortName;
  108. }
  109. }
  110. return $entityName;
  111. }
  112. /**
  113. * Remove superfluous characters from hash
  114. *
  115. * @param string $hash
  116. * @param string $prefix
  117. * @param int $maxCharacters
  118. * @return string
  119. */
  120. private static function trimHash($hash, $prefix, $maxCharacters)
  121. {
  122. $diff = strlen($hash) + strlen($prefix) - $maxCharacters;
  123. $superfluous = $diff / 2;
  124. $odd = $diff % 2;
  125. $hash = substr($hash, $superfluous, - ($superfluous + $odd));
  126. return $hash;
  127. }
  128. }