‪TYPO3CMS  9.5
SchemaIndexDefinitionListener.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
20 use Doctrine\DBAL\Schema\Index;
22 
28 {
38  public function ‪onSchemaIndexDefinition(SchemaIndexDefinitionEventArgs $event)
39  {
40  if (strpos($event->getConnection()->getServerVersion(), 'MySQL') !== 0) {
41  return;
42  }
43 
44  $connection = $event->getConnection();
45  $indexName = $event->getTableIndex()['name'];
46  $sql = $event->getDatabasePlatform()->getListTableIndexesSQL(
47  $event->getTable(),
48  $event->getConnection()->getDatabase()
49  );
50 
51  // check whether ORDER BY is available in SQL
52  // and place the part 'AND INDEX_NAME = "SOME_INDEX_NAME"' before that
53  if (strpos($sql, 'ORDER BY') !== false) {
54  $posOfOrderBy = strpos($sql, 'ORDER BY');
55  $tmpSql = substr($sql, 0, $posOfOrderBy);
56  $tmpSql .= ' AND ' . $connection->quoteIdentifier('INDEX_NAME') . ' = ' . $connection->quote($indexName);
57  $tmpSql .= ' ' . substr($sql, $posOfOrderBy);
58  $sql = $tmpSql;
59  unset($tmpSql);
60  } else {
61  $sql .= ' AND ' . $connection->quoteIdentifier('INDEX_NAME') . ' = ' . $connection->quote($indexName);
62  }
63 
64  $tableIndexes = $event->getConnection()->fetchAll($sql);
65 
66  $subPartColumns = array_filter(
67  $tableIndexes,
68  function ($column) {
69  return $column['Sub_Part'];
70  }
71  );
72 
73  if (!empty($subPartColumns)) {
74  $event->setIndex($this->‪buildIndex($tableIndexes));
75  $event->preventDefault();
76  }
77  }
78 
87  protected function ‪buildIndex(array $tableIndexRows): Index
88  {
89  $data = null;
90  foreach ($tableIndexRows as $tableIndex) {
91  $tableIndex = array_change_key_case($tableIndex, CASE_LOWER);
92 
93  $tableIndex['primary'] = $tableIndex['key_name'] === 'PRIMARY';
94 
95  if (strpos($tableIndex['index_type'], 'FULLTEXT') !== false) {
96  $tableIndex['flags'] = ['FULLTEXT'];
97  } elseif (strpos($tableIndex['index_type'], 'SPATIAL') !== false) {
98  $tableIndex['flags'] = ['SPATIAL'];
99  }
100 
101  $indexName = $tableIndex['key_name'];
102  $columnName = $tableIndex['column_name'];
103 
104  if ($tableIndex['sub_part'] !== null) {
105  $columnName .= '(' . $tableIndex['sub_part'] . ')';
106  }
107 
108  if ($data === null) {
109  $data = [
110  'name' => $indexName,
111  'columns' => [$columnName],
112  'unique' => !$tableIndex['non_unique'],
113  'primary' => $tableIndex['primary'],
114  'flags' => $tableIndex['flags'] ?? [],
115  'options' => isset($tableIndex['where']) ? ['where' => $tableIndex['where']] : [],
116  ];
117  } else {
118  $data['columns'][] = $columnName;
119  }
120  }
121 
122  $index = GeneralUtility::makeInstance(
123  Index::class,
124  $data['name'],
125  $data['columns'],
126  $data['unique'],
127  $data['primary'],
128  $data['flags'],
129  $data['options']
130  );
131 
132  return $index;
133  }
134 }
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaIndexDefinitionListener
Definition: SchemaIndexDefinitionListener.php:28
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaIndexDefinitionListener\onSchemaIndexDefinition
‪onSchemaIndexDefinition(SchemaIndexDefinitionEventArgs $event)
Definition: SchemaIndexDefinitionListener.php:38
‪TYPO3\CMS\Core\Database\Schema\EventListener
Definition: SchemaAlterTableListener.php:4
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaIndexDefinitionListener\buildIndex
‪Doctrine DBAL Schema Index buildIndex(array $tableIndexRows)
Definition: SchemaIndexDefinitionListener.php:87
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45