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