Merge branch '3.1' into 5.2
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / database / table / index / DatabaseTableIndex.class.php
1 <?php
2 namespace wcf\system\database\table\index;
3 use wcf\system\database\table\TDroppableDatabaseComponent;
4
5 /**
6 * Represents an index of a database table.
7 *
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
12 * @since 5.2
13 */
14 class DatabaseTableIndex {
15 use TDroppableDatabaseComponent;
16
17 /**
18 * indexed columns
19 * @var string[]
20 */
21 protected $columns;
22
23 /**
24 * is `true` if index name has been automatically generated
25 * @var bool
26 */
27 protected $generatedName = false;
28
29 /**
30 * name of index
31 * @var string
32 */
33 protected $name;
34
35 /**
36 * type of index (see `*_TYPE` constants)
37 * @var null|string
38 */
39 protected $type;
40
41 const DEFAULT_TYPE = null;
42 const PRIMARY_TYPE = 'PRIMARY';
43 const UNIQUE_TYPE = 'UNIQUE';
44 const FULLTEXT_TYPE = 'FULLTEXT';
45
46 /**
47 * Creates a new `DatabaseTableIndex` object.
48 *
49 * @param string $name column name
50 */
51 protected function __construct($name) {
52 $this->name = $name;
53 }
54
55 /**
56 * Sets the indexed columns and returns the index.
57 *
58 * @param string[] $columns indexed columns
59 * @return $this this index
60 */
61 public function columns($columns) {
62 $this->columns = array_values($columns);
63
64 return $this;
65 }
66
67 /**
68 * Sets the automatically generated name of the index.
69 *
70 * @param string $name index name
71 * @return $this this index
72 */
73 public function generatedName($name) {
74 $this->name($name);
75 $this->generatedName = true;
76
77 return $this;
78 }
79
80 /**
81 * Returns the index columns.
82 *
83 * @return string[]
84 */
85 public function getColumns() {
86 return $this->columns;
87 }
88
89 /**
90 * Returns the data used by `DatabaseEditor` to add the index to a table.
91 *
92 * @return array
93 */
94 public function getData() {
95 return [
96 'columns' => implode(',', $this->columns),
97 'type' => $this->type
98 ];
99 }
100
101 /**
102 * Returns the name of the index.
103 *
104 * @return string
105 */
106 public function getName() {
107 return $this->name;
108 }
109
110 /**
111 * Returns the type of the index (see `*_TYPE` constants).
112 *
113 * @return null|string
114 */
115 public function getType() {
116 return $this->type;
117 }
118
119 /**
120 * Returns `true` if the name of the index has been automatically generated.
121 *
122 * @return bool
123 */
124 public function hasGeneratedName() {
125 return $this->generatedName;
126 }
127
128 /**
129 * Sets the name of the index.
130 *
131 * @param string $name index name
132 * @return $this this index
133 */
134 public function name($name) {
135 $this->name = $name;
136
137 return $this;
138 }
139
140 /**
141 * Sets the type of the index and returns the index
142 *
143 * @param null|string $type index type
144 * @return $this this index
145 * @throws \InvalidArgumentException if given type is invalid
146 */
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}'.");
150 }
151
152 $this->type = $type;
153
154 return $this;
155 }
156
157 /**
158 * Returns a `DatabaseTableIndex` object with the given name.
159 *
160 * @param string $name
161 * @return static
162 */
163 public static function create($name = '') {
164 return new static($name);
165 }
166
167 /**
168 * Returns a `DatabaseTableIndex` object with the given name and data.
169 *
170 * @param string $name
171 * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
172 * @return static
173 */
174 public static function createFromData($name, array $data) {
175 return static::create($name)
176 ->type($data['type'])
177 ->columns($data['columns']);
178 }
179 }