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 class DatabaseTableIndex
18 use TDroppableDatabaseComponent
;
27 * is `true` if index name has been automatically generated
30 protected $generatedName = false;
39 * type of index (see `*_TYPE` constants)
44 const DEFAULT_TYPE
= null;
46 const PRIMARY_TYPE
= 'PRIMARY';
48 const UNIQUE_TYPE
= 'UNIQUE';
50 const FULLTEXT_TYPE
= 'FULLTEXT';
53 * Creates a new `DatabaseTableIndex` object.
55 * @param string $name column name
57 protected function __construct($name)
63 * Sets the indexed columns and returns the index.
65 * @param string[] $columns indexed columns
66 * @return $this this index
68 public function columns($columns)
70 $this->columns
= \array_values
($columns);
76 * Sets the automatically generated name of the index.
78 * @param string $name index name
79 * @return $this this index
81 public function generatedName($name)
84 $this->generatedName
= true;
90 * Returns the index columns.
94 public function getColumns()
96 if (!isset($this->columns
)) {
97 throw new \
BadMethodCallException(
98 "Before getting the columns, they must be set for index '{$this->getName()}'."
102 return $this->columns
;
106 * Returns the data used by `DatabaseEditor` to add the index to a table.
110 public function getData()
113 'columns' => \
implode(',', $this->getColumns()),
114 'type' => $this->getType(),
119 * Returns the name of the index.
123 public function getName()
129 * Returns the type of the index (see `*_TYPE` constants).
131 * @return null|string
133 public function getType()
139 * Returns `true` if the name of the index has been automatically generated.
143 public function hasGeneratedName()
145 return $this->generatedName
;
149 * Sets the name of the index.
151 * @param string $name index name
152 * @return $this this index
154 public function name($name)
162 * Sets the type of the index and returns the index
164 * @param null|string $type index type
165 * @return $this this index
166 * @throws \InvalidArgumentException if given type is invalid
168 public function type($type)
171 $type !== static::DEFAULT_TYPE
172 && $type !== static::PRIMARY_TYPE
173 && $type !== static::UNIQUE_TYPE
174 && $type !== static::FULLTEXT_TYPE
176 throw new \
InvalidArgumentException("Unknown index type '{$type}'.");
185 * Returns a `DatabaseTableIndex` object with the given name.
187 * @param string $name
190 public static function create($name = '')
192 return new static($name);
196 * Returns a `DatabaseTableIndex` object with the given name and data.
198 * @param string $name
199 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
202 public static function createFromData($name, array $data)
204 return static::create($name)
205 ->type($data['type'])
206 ->columns($data['columns']);