Skip to content

Commit 6767b65

Browse files
author
Joan He
committed
Merge remote-tracking branch 'origin/MAGETWO-37848-prefix-length' into develop
2 parents 5b1c86c + 785aca6 commit 6767b65

File tree

9 files changed

+280
-193
lines changed

9 files changed

+280
-193
lines changed

lib/internal/Magento/Framework/App/Resource.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function __construct(
8585
*
8686
* @param string $resourceName
8787
* @return \Magento\Framework\DB\Adapter\AdapterInterface|false
88+
* @codeCoverageIgnore
8889
*/
8990
public function getConnection($resourceName)
9091
{
@@ -152,12 +153,26 @@ public function getTableName($modelEntity, $connectionName = self::DEFAULT_READ_
152153
return $this->getConnection($connectionName)->getTableName($tableName);
153154
}
154155

156+
/**
157+
* Build a trigger name
158+
*
159+
* @param string $tableName The table that is the subject of the trigger
160+
* @param string $time Either "before" or "after"
161+
* @param string $event The DB level event which activates the trigger, i.e. "update" or "insert"
162+
* @return string
163+
*/
164+
public function getTriggerName($tableName, $time, $event)
165+
{
166+
return $this->getConnection(self::DEFAULT_READ_RESOURCE)->getTriggerName($tableName, $time, $event);
167+
}
168+
155169
/**
156170
* Set mapped table name
157171
*
158172
* @param string $tableName
159173
* @param string $mappedName
160174
* @return $this
175+
* @codeCoverageIgnore
161176
*/
162177
public function setMappedTableName($tableName, $mappedName)
163178
{
@@ -193,13 +208,12 @@ public function getIdxName(
193208
$fields,
194209
$indexType = \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX
195210
) {
196-
return $this->getConnection(
197-
self::DEFAULT_READ_RESOURCE
198-
)->getIndexName(
199-
$this->getTableName($tableName),
200-
$fields,
201-
$indexType
202-
);
211+
return $this->getConnection(self::DEFAULT_READ_RESOURCE)
212+
->getIndexName(
213+
$this->getTableName($tableName),
214+
$fields,
215+
$indexType
216+
);
203217
}
204218

205219
/**

lib/internal/Magento/Framework/App/Test/Unit/ResourceTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class ResourceTest extends \PHPUnit_Framework_TestCase
1515
{
1616
const RESOURCE_NAME = \Magento\Framework\App\Resource::DEFAULT_READ_RESOURCE;
17-
const CONNECTION_NAME = 'Connection Name';
17+
const CONNECTION_NAME = 'connection-name';
1818
const TABLE_PREFIX = 'prefix_';
1919

2020
/**
@@ -197,4 +197,21 @@ public function testGetFkName()
197197

198198
$this->assertEquals('fkName', $this->resource->getFkName($table, $columnName, $refTable, $refColumnName));
199199
}
200+
201+
public function testGetTriggerName()
202+
{
203+
$tableName = 'subject_table';
204+
$time = 'before';
205+
$event = 'insert';
206+
$triggerName = 'trg_subject_table_before_insert';
207+
208+
$this->_connectionFactory->expects($this->once())
209+
->method('create')
210+
->will($this->returnValue($this->connection));
211+
$this->connection->expects($this->once())
212+
->method('getTriggerName')
213+
->with($tableName, $time, $event)
214+
->willReturn($triggerName);
215+
$this->assertSame($triggerName, $this->resource->getTriggerName($tableName, $time, $event));
216+
}
200217
}

lib/internal/Magento/Framework/DB/Adapter/AdapterInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,17 @@ public function getDateExtractSql($date, $unit);
918918
*/
919919
public function getTableName($tableName);
920920

921+
922+
/**
923+
* Build a trigger name based on table name and trigger details
924+
*
925+
* @param string $tableName The table that is the subject of the trigger
926+
* @param string $time Either "before" or "after"
927+
* @param string $event The DB level event which activates the trigger, i.e. "update" or "insert"
928+
* @return string
929+
*/
930+
public function getTriggerName($tableName, $time, $event);
931+
921932
/**
922933
* Retrieve valid index name
923934
* Check index name length and allowed symbols

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Framework\DB\Select;
2020
use Magento\Framework\DB\Statement\Parameter;
2121
use Magento\Framework\Exception\LocalizedException;
22+
use Magento\Framework\Mview\View\Subscription;
2223
use Magento\Framework\Phrase;
2324
use Magento\Framework\Stdlib\DateTime;
2425
use Magento\Framework\Stdlib\String;
@@ -3115,47 +3116,31 @@ public function getDateExtractSql($date, $unit)
31153116
}
31163117

31173118
/**
3118-
* Minus superfluous characters from hash.
3119+
* Returns a compressed version of the table name if it is too long
31193120
*
3120-
* @param string $hash
3121-
* @param string $prefix
3122-
* @param int $maxCharacters
3121+
* @param string $tableName
31233122
* @return string
3123+
* @codeCoverageIgnore
31243124
*/
3125-
protected function _minusSuperfluous($hash, $prefix, $maxCharacters)
3125+
public function getTableName($tableName)
31263126
{
3127-
$diff = strlen($hash) + strlen($prefix) - $maxCharacters;
3128-
$superfluous = $diff / 2;
3129-
$odd = $diff % 2;
3130-
$hash = substr($hash, $superfluous, - ($superfluous + $odd));
3131-
return $hash;
3127+
return ExpressionConverter::shortenEntityName($tableName, 't_');
31323128
}
31333129

31343130
/**
3135-
* Retrieve valid table name
3136-
* Check table name length and allowed symbols
3131+
* Build a trigger name based on table name and trigger details
31373132
*
3138-
* @param string $tableName
3133+
* @param string $tableName The table which is the subject of the trigger
3134+
* @param string $time Either "before" or "after"
3135+
* @param string $event The DB level event which activates the trigger, i.e. "update" or "insert"
31393136
* @return string
3137+
* @codeCoverageIgnore
31403138
*/
3141-
public function getTableName($tableName)
3142-
{
3143-
$prefix = 't_';
3144-
if (strlen($tableName) > self::LENGTH_TABLE_NAME) {
3145-
$shortName = ExpressionConverter::shortName($tableName);
3146-
if (strlen($shortName) > self::LENGTH_TABLE_NAME) {
3147-
$hash = md5($tableName);
3148-
if (strlen($prefix . $hash) > self::LENGTH_TABLE_NAME) {
3149-
$tableName = $this->_minusSuperfluous($hash, $prefix, self::LENGTH_TABLE_NAME);
3150-
} else {
3151-
$tableName = $prefix . $hash;
3152-
}
3153-
} else {
3154-
$tableName = $shortName;
3155-
}
3156-
}
31573139

3158-
return $tableName;
3140+
public function getTriggerName($tableName, $time, $event)
3141+
{
3142+
$triggerName = 'trg_' . $tableName . '_' . $time . '_' . $event;
3143+
return ExpressionConverter::shortenEntityName($triggerName, 'trg_');
31593144
}
31603145

31613146
/**
@@ -3176,35 +3161,15 @@ public function getIndexName($tableName, $fields, $indexType = '')
31763161
switch (strtolower($indexType)) {
31773162
case AdapterInterface::INDEX_TYPE_UNIQUE:
31783163
$prefix = 'unq_';
3179-
$shortPrefix = 'u_';
31803164
break;
31813165
case AdapterInterface::INDEX_TYPE_FULLTEXT:
31823166
$prefix = 'fti_';
3183-
$shortPrefix = 'f_';
31843167
break;
31853168
case AdapterInterface::INDEX_TYPE_INDEX:
31863169
default:
31873170
$prefix = 'idx_';
3188-
$shortPrefix = 'i_';
3189-
}
3190-
3191-
$hash = $tableName . '_' . $fields;
3192-
3193-
if (strlen($hash) + strlen($prefix) > self::LENGTH_INDEX_NAME) {
3194-
$short = ExpressionConverter::shortName($prefix . $hash);
3195-
if (strlen($short) > self::LENGTH_INDEX_NAME) {
3196-
$hash = md5($hash);
3197-
if (strlen($hash) + strlen($shortPrefix) > self::LENGTH_INDEX_NAME) {
3198-
$hash = $this->_minusSuperfluous($hash, $shortPrefix, self::LENGTH_INDEX_NAME);
3199-
}
3200-
} else {
3201-
$hash = $short;
3202-
}
3203-
} else {
3204-
$hash = $prefix . $hash;
32053171
}
3206-
3207-
return strtoupper($hash);
3172+
return strtoupper(ExpressionConverter::shortenEntityName($tableName . '_' . $fields, $prefix));
32083173
}
32093174

32103175
/**
@@ -3216,28 +3181,12 @@ public function getIndexName($tableName, $fields, $indexType = '')
32163181
* @param string $refTableName
32173182
* @param string $refColumnName
32183183
* @return string
3184+
* @codeCoverageIgnore
32193185
*/
32203186
public function getForeignKeyName($priTableName, $priColumnName, $refTableName, $refColumnName)
32213187
{
3222-
$prefix = 'fk_';
3223-
$hash = sprintf('%s_%s_%s_%s', $priTableName, $priColumnName, $refTableName, $refColumnName);
3224-
if (strlen($prefix . $hash) > self::LENGTH_FOREIGN_NAME) {
3225-
$short = ExpressionConverter::shortName($prefix . $hash);
3226-
if (strlen($short) > self::LENGTH_FOREIGN_NAME) {
3227-
$hash = md5($hash);
3228-
if (strlen($prefix . $hash) > self::LENGTH_FOREIGN_NAME) {
3229-
$hash = $this->_minusSuperfluous($hash, $prefix, self::LENGTH_FOREIGN_NAME);
3230-
} else {
3231-
$hash = $prefix . $hash;
3232-
}
3233-
} else {
3234-
$hash = $short;
3235-
}
3236-
} else {
3237-
$hash = $prefix . $hash;
3238-
}
3239-
3240-
return strtoupper($hash);
3188+
$fkName = sprintf('%s_%s_%s_%s', $priTableName, $priColumnName, $refTableName, $refColumnName);
3189+
return strtoupper(ExpressionConverter::shortenEntityName($fkName, 'fk_'));
32413190
}
32423191

32433192
/**

lib/internal/Magento/Framework/DB/ExpressionConverter.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
class ExpressionConverter
1111
{
1212
/**
13-
* Dictionary for generate short name
13+
* Maximum length for many MySql identifiers, including database, table, trigger, and column names
14+
*/
15+
const MYSQL_IDENTIFIER_LEN = 64;
16+
17+
/**
18+
* Dictionary maps common words in identifiers to abbreviations
1419
*
1520
* @var array
1621
*/
@@ -62,7 +67,7 @@ class ExpressionConverter
6267
];
6368

6469
/**
65-
* Convert name using dictionary
70+
* Shorten name by abbreviating words
6671
*
6772
* @param string $name
6873
* @return string
@@ -73,7 +78,7 @@ public static function shortName($name)
7378
}
7479

7580
/**
76-
* Add or replace translate to dictionary
81+
* Add an abbreviation to the dictionary, or replace if it already exists
7782
*
7883
* @param string $from
7984
* @param string $to
@@ -83,4 +88,47 @@ public static function addTranslate($from, $to)
8388
{
8489
self::$_translateMap[$from] = $to;
8590
}
91+
92+
/**
93+
* Shorten the name of a MySql identifier, by abbreviating common words and hashing if necessary. Prepends the
94+
* given prefix to clarify what kind of entity the identifier represents, in case hashing is used.
95+
*
96+
* @param string $entityName
97+
* @param string $prefix
98+
* @return string
99+
*/
100+
public static function shortenEntityName($entityName, $prefix)
101+
{
102+
if (strlen($entityName) > self::MYSQL_IDENTIFIER_LEN) {
103+
$shortName = ExpressionConverter::shortName($entityName);
104+
if (strlen($shortName) > self::MYSQL_IDENTIFIER_LEN) {
105+
$hash = md5($entityName);
106+
if (strlen($prefix . $hash) > self::MYSQL_IDENTIFIER_LEN) {
107+
$entityName = self::trimHash($hash, $prefix, self::MYSQL_IDENTIFIER_LEN);
108+
} else {
109+
$entityName = $prefix . $hash;
110+
}
111+
} else {
112+
$entityName = $shortName;
113+
}
114+
}
115+
return $entityName;
116+
}
117+
118+
/**
119+
* Remove superfluous characters from hash
120+
*
121+
* @param string $hash
122+
* @param string $prefix
123+
* @param int $maxCharacters
124+
* @return string
125+
*/
126+
private static function trimHash($hash, $prefix, $maxCharacters)
127+
{
128+
$diff = strlen($hash) + strlen($prefix) - $maxCharacters;
129+
$superfluous = $diff / 2;
130+
$odd = $diff % 2;
131+
$hash = substr($hash, $superfluous, - ($superfluous + $odd));
132+
return $hash;
133+
}
86134
}

0 commit comments

Comments
 (0)