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 return $this->columns
;
100 * Returns the data used by `DatabaseEditor` to add the index to a table.
104 public function getData()
107 'columns' => \
implode(',', $this->columns
),
108 'type' => $this->type
,
113 * Returns the name of the index.
117 public function getName()
123 * Returns the type of the index (see `*_TYPE` constants).
125 * @return null|string
127 public function getType()
133 * Returns `true` if the name of the index has been automatically generated.
137 public function hasGeneratedName()
139 return $this->generatedName
;
143 * Sets the name of the index.
145 * @param string $name index name
146 * @return $this this index
148 public function name($name)
156 * Sets the type of the index and returns the index
158 * @param null|string $type index type
159 * @return $this this index
160 * @throws \InvalidArgumentException if given type is invalid
162 public function type($type)
165 $type !== static::DEFAULT_TYPE
166 && $type !== static::PRIMARY_TYPE
167 && $type !== static::UNIQUE_TYPE
168 && $type !== static::FULLTEXT_TYPE
170 throw new \
InvalidArgumentException("Unknown index type '{$type}'.");
179 * Returns a `DatabaseTableIndex` object with the given name.
181 * @param string $name
184 public static function create($name = '')
186 return new static($name);
190 * Returns a `DatabaseTableIndex` object with the given name and data.
192 * @param string $name
193 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
196 public static function createFromData($name, array $data)
198 return static::create($name)
199 ->type($data['type'])
200 ->columns($data['columns']);