3737use function array_shift ;
3838use function array_values ;
3939use function count ;
40+ use function crc32 ;
41+ use function dechex ;
4042use function implode ;
4143use function in_array ;
4244use function is_int ;
4345use function sprintf ;
4446use function strtolower ;
4547use function strtoupper ;
48+ use function substr ;
4649
4750/**
4851 * Object Representation of a table.
@@ -159,11 +162,7 @@ public function addUniqueConstraint(
159162 ?string $ indexName = null ,
160163 array $ flags = [],
161164 ): self {
162- $ indexName ??= $ this ->_generateIdentifierName (
163- array_merge ([$ this ->getName ()], $ columnNames ),
164- 'uniq ' ,
165- $ this ->maxIdentifierLength ,
166- );
165+ $ indexName ??= $ this ->generateNameFromStringColumnNames ('uniq ' , $ columnNames );
167166
168167 $ isClustered = in_array ('clustered ' , $ flags , true );
169168
@@ -181,11 +180,7 @@ public function addIndex(
181180 array $ flags = [],
182181 array $ options = [],
183182 ): self {
184- $ indexName ??= $ this ->_generateIdentifierName (
185- array_merge ([$ this ->getName ()], $ columnNames ),
186- 'idx ' ,
187- $ this ->maxIdentifierLength ,
188- );
183+ $ indexName ??= $ this ->generateNameFromStringColumnNames ('idx ' , $ columnNames );
189184
190185 return $ this ->_addIndex ($ this ->_createIndex ($ columnNames , $ indexName , false , $ flags , $ options ));
191186 }
@@ -218,11 +213,7 @@ public function dropIndex(string $name): void
218213 */
219214 public function addUniqueIndex (array $ columnNames , ?string $ indexName = null , array $ options = []): self
220215 {
221- $ indexName ??= $ this ->_generateIdentifierName (
222- array_merge ([$ this ->getName ()], $ columnNames ),
223- 'uniq ' ,
224- $ this ->maxIdentifierLength ,
225- );
216+ $ indexName ??= $ this ->generateNameFromStringColumnNames ('uniq ' , $ columnNames );
226217
227218 return $ this ->_addIndex ($ this ->_createIndex ($ columnNames , $ indexName , true , [], $ options ));
228219 }
@@ -258,7 +249,7 @@ public function renameIndex(string $oldName, ?string $newName = null): self
258249 $ name = $ this ->parseUnqualifiedName ($ newName );
259250 } else {
260251 $ name = UnqualifiedName::unquoted (
261- $ this ->generateName (
252+ $ this ->generateNameFromObjectColumnNames (
262253 $ index ->getType () === IndexType::UNIQUE ? 'uniq ' : 'idx ' ,
263254 array_map (
264255 static fn (IndexedColumn $ indexedColumn ): UnqualifiedName => $ indexedColumn ->getColumnName (),
@@ -429,7 +420,7 @@ public function addForeignKeyConstraint(
429420 );
430421 } else {
431422 $ editor ->setUnquotedName (
432- $ this ->generateName ('fk ' , $ referencingColumnNames ),
423+ $ this ->generateNameFromObjectColumnNames ('fk ' , $ referencingColumnNames ),
433424 );
434425 }
435426
@@ -790,7 +781,7 @@ protected function _addUniqueConstraint(UniqueConstraint $constraint): self
790781
791782 $ name = $ constraint ->getName () !== ''
792783 ? $ constraint ->getName ()
793- : $ this ->generateName ('fk ' , $ columnNames );
784+ : $ this ->generateNameFromObjectColumnNames ('fk ' , $ columnNames );
794785
795786 $ name = $ this ->normalizeIdentifier ($ name );
796787
@@ -799,7 +790,7 @@ protected function _addUniqueConstraint(UniqueConstraint $constraint): self
799790 // If there is already an index that fulfills this requirements drop the request. In the case of __construct
800791 // calling this method during hydration from schema-details all the explicitly added indexes lead to duplicates.
801792 // This creates computation overhead in this case, however no duplicate indexes are ever added (column based).
802- $ indexName = $ this ->generateName ('idx ' , $ columnNames );
793+ $ indexName = $ this ->generateNameFromObjectColumnNames ('idx ' , $ columnNames );
803794
804795 $ indexCandidate = Index::editor ()
805796 ->setName (UnqualifiedName::unquoted ($ indexName ))
@@ -822,7 +813,7 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint): s
822813 {
823814 $ name = $ constraint ->getName () !== ''
824815 ? $ constraint ->getName ()
825- : $ this ->generateName ('fk ' , $ constraint ->getReferencingColumnNames ());
816+ : $ this ->generateNameFromObjectColumnNames ('fk ' , $ constraint ->getReferencingColumnNames ());
826817
827818 $ name = $ this ->normalizeIdentifier ($ name );
828819
@@ -832,7 +823,7 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint): s
832823 // If there is already an index that fulfills this requirements drop the request. In the case of __construct
833824 // calling this method during hydration from schema-details all the explicitly added indexes lead to duplicates.
834825 // This creates computation overhead in this case, however no duplicate indexes are ever added (column based).
835- $ indexName = $ this ->generateName ('idx ' , $ constraint ->getReferencingColumnNames ());
826+ $ indexName = $ this ->generateNameFromObjectColumnNames ('idx ' , $ constraint ->getReferencingColumnNames ());
836827
837828 $ indexCandidate = Index::editor ()
838829 ->setName (UnqualifiedName::unquoted ($ indexName ))
@@ -1055,23 +1046,44 @@ private function parseIndexColumns(array $columnNames, array $lengths): array
10551046 }
10561047
10571048 /**
1058- * Generates a name from a prefix and a list of column names obeying the configured maximum identifier length.
1049+ * Generates a name from a prefix and a list of column names represented as objects obeying the configured maximum
1050+ * identifier length.
10591051 *
10601052 * @param non-empty-list<UnqualifiedName> $columnNames
10611053 *
10621054 * @return non-empty-string
10631055 */
1064- private function generateName (string $ prefix , array $ columnNames ): string
1056+ private function generateNameFromObjectColumnNames (string $ prefix , array $ columnNames ): string
10651057 {
1066- return $ this ->_generateIdentifierName (
1067- array_merge ([$ this ->getName ()], array_map (static function (UnqualifiedName $ columnName ): string {
1068- return $ columnName ->getIdentifier ()->getValue ();
1069- }, $ columnNames )),
1058+ return $ this ->generateNameFromStringColumnNames (
10701059 $ prefix ,
1071- $ this ->maxIdentifierLength ,
1060+ array_map (static function (UnqualifiedName $ columnName ): string {
1061+ return $ columnName ->getIdentifier ()->getValue ();
1062+ }, $ columnNames ),
10721063 );
10731064 }
10741065
1066+ /**
1067+ * Generates a name from a prefix and a list of column names represented as strings obeying the configured maximum
1068+ * identifier length.
1069+ *
1070+ * @param array<int, string> $columnNames
1071+ *
1072+ * @return non-empty-string
1073+ */
1074+ private function generateNameFromStringColumnNames (string $ prefix , array $ columnNames ): string
1075+ {
1076+ $ hash = implode ('' , array_map (static function (string $ columnName ): string {
1077+ return dechex (crc32 ($ columnName ));
1078+ }, array_merge ([
1079+ $ this ->getObjectName ()
1080+ ->getUnqualifiedName ()
1081+ ->getValue (),
1082+ ], $ columnNames )));
1083+
1084+ return strtoupper (substr ($ prefix . '_ ' . $ hash , 0 , $ this ->maxIdentifierLength ));
1085+ }
1086+
10751087 /** @param non-empty-string $newName */
10761088 private function renameColumnInIndexes (string $ oldName , string $ newName ): void
10771089 {
0 commit comments