3 namespace wcf\system\database\table\index
;
5 use wcf\system\database\table\TDroppableDatabaseComponent
;
8 * Represents an index of a database table.
10 * @author Matthias Schmidt
11 * @copyright 2001-2019 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package WoltLabSuite\Core\System\Database\Table\Index
16 final class DatabaseTableIndex
18 use TDroppableDatabaseComponent
;
24 private array $columns;
27 * is `true` if index name has been automatically generated
29 private bool $generatedName = false;
37 * type of index (see `*_TYPE` constants)
39 private ?
string $type = null;
41 const DEFAULT_TYPE
= null;
43 const PRIMARY_TYPE
= 'PRIMARY';
45 const UNIQUE_TYPE
= 'UNIQUE';
47 const FULLTEXT_TYPE
= 'FULLTEXT';
50 * Creates a new `DatabaseTableIndex` object.
52 private function __construct(string $name)
58 * Sets the indexed columns and returns the index.
60 * @param string[] $columns indexed columns
63 public function columns(array $columns): static
65 $this->columns
= \array_values
($columns);
71 * Sets the automatically generated name of the index.
75 public function generatedName(string $name): static
78 $this->generatedName
= true;
84 * Returns the index columns.
88 public function getColumns(): array
90 if (!isset($this->columns
)) {
91 throw new \
BadMethodCallException(
92 "Before getting the columns, they must be set for index '{$this->getName()}'."
96 return $this->columns
;
100 * Returns the data used by `DatabaseEditor` to add the index to a table.
102 * @return array{columns: string, type: string}
104 public function getData(): array
107 'columns' => \
implode(',', $this->getColumns()),
108 'type' => $this->getType(),
113 * Returns the name of the index.
115 public function getName(): string
121 * Returns the type of the index (see `*_TYPE` constants).
123 public function getType(): ?
string
129 * Returns `true` if the name of the index has been automatically generated.
131 public function hasGeneratedName(): bool
133 return $this->generatedName
;
137 * Sets the name of the index.
141 public function name(string $name): static
149 * Sets the type of the index and returns the index
151 * @throws \InvalidArgumentException if given type is invalid
153 public function type(?
string $type): static
156 $type !== static::DEFAULT_TYPE
157 && $type !== static::PRIMARY_TYPE
158 && $type !== static::UNIQUE_TYPE
159 && $type !== static::FULLTEXT_TYPE
161 throw new \
InvalidArgumentException("Unknown index type '{$type}'.");
170 * Returns a `DatabaseTableIndex` object with the given name.
172 public static function create(string $name): static
174 return new static($name);
178 * Returns a `DatabaseTableIndex` object with the given name and data.
180 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
182 public static function createFromData(string $name, array $data): static
184 return self
::create($name)
185 ->type($data['type'])
186 ->columns($data['columns']);