2 namespace wcf\system\database\table\index
;
3 use wcf\system\database\table\TDroppableDatabaseComponent
;
6 * Represents an index of a database table.
8 * @author Matthias Schmidt
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System\Database\Table\Index
14 class DatabaseTableIndex
{
15 use TDroppableDatabaseComponent
;
24 * is `true` if index name has been automatically generated
27 protected $generatedName = false;
36 * type of index (see `*_TYPE` constants)
41 const DEFAULT_TYPE
= null;
42 const PRIMARY_TYPE
= 'PRIMARY';
43 const UNIQUE_TYPE
= 'UNIQUE';
44 const FULLTEXT_TYPE
= 'FULLTEXT';
47 * Creates a new `DatabaseTableIndex` object.
49 * @param string $name column name
51 protected function __construct($name) {
56 * Sets the indexed columns and returns the index.
58 * @param string[] $columns indexed columns
59 * @return $this this index
61 public function columns($columns) {
62 $this->columns
= array_values($columns);
68 * Sets the automatically generated name of the index.
70 * @param string $name index name
71 * @return $this this index
73 public function generatedName($name) {
75 $this->generatedName
= true;
81 * Returns the index columns.
85 public function getColumns() {
86 return $this->columns
;
90 * Returns the data used by `DatabaseEditor` to add the index to a table.
94 public function getData() {
96 'columns' => implode(',', $this->columns
),
102 * Returns the name of the index.
106 public function getName() {
111 * Returns the type of the index (see `*_TYPE` constants).
113 * @return null|string
115 public function getType() {
120 * Returns `true` if the name of the index has been automatically generated.
124 public function hasGeneratedName() {
125 return $this->generatedName
;
129 * Sets the name of the index.
131 * @param string $name index name
132 * @return $this this index
134 public function name($name) {
141 * Sets the type of the index and returns the index
143 * @param null|string $type index type
144 * @return $this this index
145 * @throws \InvalidArgumentException if given type is invalid
147 public function type($type) {
148 if ($type !== static::DEFAULT_TYPE
&& $type !== static::PRIMARY_TYPE
&& $type !== static::UNIQUE_TYPE
&& $type !== static::FULLTEXT_TYPE
) {
149 throw new \
InvalidArgumentException("Unknown index type '{$type}'.");
158 * Returns a `DatabaseTableIndex` object with the given name.
160 * @param string $name
163 public static function create($name = '') {
164 return new static($name);
168 * Returns a `DatabaseTableIndex` object with the given name and data.
170 * @param string $name
171 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
174 public static function createFromData($name, array $data) {
175 return static::create($name)
176 ->type($data['type'])
177 ->columns($data['columns']);