Exception
2 years ago
Visitor
9 months ago
AbstractAsset.php
2 years ago
AbstractSchemaManager.php
9 months ago
Column.php
2 years ago
ColumnDiff.php
2 years ago
Comparator.php
2 years ago
Constraint.php
2 years ago
DB2SchemaManager.php
2 years ago
DefaultSchemaManagerFactory.php
2 years ago
ForeignKeyConstraint.php
9 months ago
Identifier.php
2 years ago
Index.php
2 years ago
LegacySchemaManagerFactory.php
2 years ago
MySQLSchemaManager.php
9 months ago
OracleSchemaManager.php
2 years ago
PostgreSQLSchemaManager.php
9 months ago
SQLServerSchemaManager.php
9 months ago
Schema.php
2 years ago
SchemaConfig.php
2 years ago
SchemaDiff.php
2 years ago
SchemaException.php
2 years ago
SchemaManagerFactory.php
2 years ago
Sequence.php
2 years ago
SqliteSchemaManager.php
8 months ago
Table.php
2 years ago
TableDiff.php
2 years ago
UniqueConstraint.php
2 years ago
View.php
2 years ago
Index.php
179 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL\Schema; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\DBAL\Platforms\AbstractPlatform; |
| 5 | use InvalidArgumentException; |
| 6 | use function array_filter; |
| 7 | use function array_keys; |
| 8 | use function array_map; |
| 9 | use function array_search; |
| 10 | use function array_shift; |
| 11 | use function count; |
| 12 | use function strtolower; |
| 13 | class Index extends AbstractAsset implements Constraint |
| 14 | { |
| 15 | protected $_columns = []; |
| 16 | protected $_isUnique = \false; |
| 17 | protected $_isPrimary = \false; |
| 18 | protected $_flags = []; |
| 19 | private array $options = []; |
| 20 | public function __construct($name, array $columns, $isUnique = \false, $isPrimary = \false, array $flags = [], array $options = []) |
| 21 | { |
| 22 | $isUnique = $isUnique || $isPrimary; |
| 23 | $this->_setName($name); |
| 24 | $this->_isUnique = $isUnique; |
| 25 | $this->_isPrimary = $isPrimary; |
| 26 | $this->options = $options; |
| 27 | foreach ($columns as $column) { |
| 28 | $this->_addColumn($column); |
| 29 | } |
| 30 | foreach ($flags as $flag) { |
| 31 | $this->addFlag($flag); |
| 32 | } |
| 33 | } |
| 34 | protected function _addColumn(string $column) : void |
| 35 | { |
| 36 | $this->_columns[$column] = new Identifier($column); |
| 37 | } |
| 38 | public function getColumns() |
| 39 | { |
| 40 | return array_keys($this->_columns); |
| 41 | } |
| 42 | public function getQuotedColumns(AbstractPlatform $platform) |
| 43 | { |
| 44 | $subParts = $platform->supportsColumnLengthIndexes() && $this->hasOption('lengths') ? $this->getOption('lengths') : []; |
| 45 | $columns = []; |
| 46 | foreach ($this->_columns as $column) { |
| 47 | $length = array_shift($subParts); |
| 48 | $quotedColumn = $column->getQuotedName($platform); |
| 49 | if ($length !== null) { |
| 50 | $quotedColumn .= '(' . $length . ')'; |
| 51 | } |
| 52 | $columns[] = $quotedColumn; |
| 53 | } |
| 54 | return $columns; |
| 55 | } |
| 56 | public function getUnquotedColumns() |
| 57 | { |
| 58 | return array_map([$this, 'trimQuotes'], $this->getColumns()); |
| 59 | } |
| 60 | public function isSimpleIndex() |
| 61 | { |
| 62 | return !$this->_isPrimary && !$this->_isUnique; |
| 63 | } |
| 64 | public function isUnique() |
| 65 | { |
| 66 | return $this->_isUnique; |
| 67 | } |
| 68 | public function isPrimary() |
| 69 | { |
| 70 | return $this->_isPrimary; |
| 71 | } |
| 72 | public function hasColumnAtPosition($name, $pos = 0) |
| 73 | { |
| 74 | $name = $this->trimQuotes(strtolower($name)); |
| 75 | $indexColumns = array_map('strtolower', $this->getUnquotedColumns()); |
| 76 | return array_search($name, $indexColumns, \true) === $pos; |
| 77 | } |
| 78 | public function spansColumns(array $columnNames) |
| 79 | { |
| 80 | $columns = $this->getColumns(); |
| 81 | $numberOfColumns = count($columns); |
| 82 | $sameColumns = \true; |
| 83 | for ($i = 0; $i < $numberOfColumns; $i++) { |
| 84 | if (isset($columnNames[$i]) && $this->trimQuotes(strtolower($columns[$i])) === $this->trimQuotes(strtolower($columnNames[$i]))) { |
| 85 | continue; |
| 86 | } |
| 87 | $sameColumns = \false; |
| 88 | } |
| 89 | return $sameColumns; |
| 90 | } |
| 91 | public function isFullfilledBy(Index $other) |
| 92 | { |
| 93 | return $this->isFulfilledBy($other); |
| 94 | } |
| 95 | public function isFulfilledBy(Index $other) : bool |
| 96 | { |
| 97 | // allow the other index to be equally large only. It being larger is an option |
| 98 | // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo) |
| 99 | if (count($other->getColumns()) !== count($this->getColumns())) { |
| 100 | return \false; |
| 101 | } |
| 102 | // Check if columns are the same, and even in the same order |
| 103 | $sameColumns = $this->spansColumns($other->getColumns()); |
| 104 | if ($sameColumns) { |
| 105 | if (!$this->samePartialIndex($other)) { |
| 106 | return \false; |
| 107 | } |
| 108 | if (!$this->hasSameColumnLengths($other)) { |
| 109 | return \false; |
| 110 | } |
| 111 | if (!$this->isUnique() && !$this->isPrimary()) { |
| 112 | // this is a special case: If the current key is neither primary or unique, any unique or |
| 113 | // primary key will always have the same effect for the index and there cannot be any constraint |
| 114 | // overlaps. This means a primary or unique index can always fulfill the requirements of just an |
| 115 | // index that has no constraints. |
| 116 | return \true; |
| 117 | } |
| 118 | if ($other->isPrimary() !== $this->isPrimary()) { |
| 119 | return \false; |
| 120 | } |
| 121 | return $other->isUnique() === $this->isUnique(); |
| 122 | } |
| 123 | return \false; |
| 124 | } |
| 125 | public function overrules(Index $other) |
| 126 | { |
| 127 | if ($other->isPrimary()) { |
| 128 | return \false; |
| 129 | } |
| 130 | if ($this->isSimpleIndex() && $other->isUnique()) { |
| 131 | return \false; |
| 132 | } |
| 133 | return $this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique()) && $this->samePartialIndex($other); |
| 134 | } |
| 135 | public function getFlags() |
| 136 | { |
| 137 | return array_keys($this->_flags); |
| 138 | } |
| 139 | public function addFlag($flag) |
| 140 | { |
| 141 | $this->_flags[strtolower($flag)] = \true; |
| 142 | return $this; |
| 143 | } |
| 144 | public function hasFlag($flag) |
| 145 | { |
| 146 | return isset($this->_flags[strtolower($flag)]); |
| 147 | } |
| 148 | public function removeFlag($flag) |
| 149 | { |
| 150 | unset($this->_flags[strtolower($flag)]); |
| 151 | } |
| 152 | public function hasOption($name) |
| 153 | { |
| 154 | return isset($this->options[strtolower($name)]); |
| 155 | } |
| 156 | public function getOption($name) |
| 157 | { |
| 158 | return $this->options[strtolower($name)]; |
| 159 | } |
| 160 | public function getOptions() |
| 161 | { |
| 162 | return $this->options; |
| 163 | } |
| 164 | private function samePartialIndex(Index $other) : bool |
| 165 | { |
| 166 | if ($this->hasOption('where') && $other->hasOption('where') && $this->getOption('where') === $other->getOption('where')) { |
| 167 | return \true; |
| 168 | } |
| 169 | return !$this->hasOption('where') && !$other->hasOption('where'); |
| 170 | } |
| 171 | private function hasSameColumnLengths(self $other) : bool |
| 172 | { |
| 173 | $filter = static function (?int $length) : bool { |
| 174 | return $length !== null; |
| 175 | }; |
| 176 | return array_filter($this->options['lengths'] ?? [], $filter) === array_filter($other->options['lengths'] ?? [], $filter); |
| 177 | } |
| 178 | } |
| 179 |