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