Commit | Line | Data |
---|---|---|
11ade432 AE |
1 | <?php |
2 | namespace wcf\system\database\editor; | |
82a7de7f | 3 | use wcf\system\Regex; |
11ade432 AE |
4 | |
5 | /** | |
a17de04e MS |
6 | * Database editor implementation for MySQL4.1 or higher. |
7 | * | |
11ade432 | 8 | * @author Marcel Werk |
ca4ba303 | 9 | * @copyright 2001-2014 WoltLab GmbH |
11ade432 AE |
10 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
11 | * @package com.woltlab.wcf | |
12 | * @subpackage system.database.editor | |
9f959ced | 13 | * @category Community Framework |
11ade432 AE |
14 | */ |
15 | class MySQLDatabaseEditor extends DatabaseEditor { | |
16 | /** | |
0ad90fc3 | 17 | * @see \wcf\system\database\editor\DatabaseEditor::getTableNames() |
11ade432 AE |
18 | */ |
19 | public function getTableNames() { | |
20 | $existingTables = array(); | |
21 | $sql = "SHOW TABLES FROM `".$this->dbObj->getDatabaseName()."`"; | |
22 | $statement = $this->dbObj->prepareStatement($sql); | |
23 | $statement->execute(); | |
24 | while ($row = $statement->fetchArray(\PDO::FETCH_NUM)) { | |
25 | $existingTables[] = $row[0]; | |
26 | } | |
27 | return $existingTables; | |
28 | } | |
29 | ||
30 | /** | |
0ad90fc3 | 31 | * @see \wcf\system\database\editor\DatabaseEditor::getColumns() |
11ade432 AE |
32 | */ |
33 | public function getColumns($tableName) { | |
34 | $columns = array(); | |
14d48464 | 35 | $regex = new Regex('([a-z]+)\(([0-9]+)\)', Regex::CASE_INSENSITIVE); |
77bb36ca | 36 | |
11ade432 AE |
37 | $sql = "SHOW COLUMNS FROM ".$tableName; |
38 | $statement = $this->dbObj->prepareStatement($sql); | |
39 | $statement->execute(); | |
40 | while ($row = $statement->fetchArray()) { | |
14d48464 | 41 | $regex->match($row['Type']); |
42 | $typeMatches = $regex->getMatches(); | |
77bb36ca | 43 | |
ebe8e825 | 44 | $columns[] = array('name' => $row['Field'], 'data' => array( |
77bb36ca | 45 | 'type' => ((empty($typeMatches)) ? $row['Type'] : $typeMatches[1]), |
46 | 'length' => ((empty($typeMatches)) ? '' : $typeMatches[2]), | |
14d48464 | 47 | 'notNull' => (($row['Null'] == 'YES') ? false : true), |
5fe135db K |
48 | 'key' => (($row['Key'] == 'PRI') ? 'PRIMARY' : (($row['Key'] == 'UNI') ? 'UNIQUE' : '')), |
49 | 'default' => $row['Default'], | |
50 | 'autoIncrement' => ($row['Extra'] == 'auto_increment' ? true : false) | |
51 | )); | |
ebe8e825 | 52 | } |
bf27dc91 | 53 | |
9f959ced | 54 | return $columns; |
11ade432 AE |
55 | } |
56 | ||
57 | /** | |
0ad90fc3 | 58 | * @see \wcf\system\database\editor\DatabaseEditor::getIndices() |
11ade432 AE |
59 | */ |
60 | public function getIndices($tableName) { | |
61 | $indices = array(); | |
62 | $sql = "SHOW INDEX FROM ".$tableName; | |
63 | $statement = $this->dbObj->prepareStatement($sql); | |
64 | $statement->execute(); | |
65 | while ($row = $statement->fetchArray()) { | |
9f959ced MS |
66 | $indices[] = $row['Key_name']; |
67 | } | |
68 | ||
69 | return $indices; | |
11ade432 AE |
70 | } |
71 | ||
72 | /** | |
0ad90fc3 | 73 | * @see \wcf\system\database\editor\DatabaseEditor::createTable() |
11ade432 AE |
74 | */ |
75 | public function createTable($tableName, $columns, $indices = array()) { | |
76 | $columnDefinition = $indexDefinition = ''; | |
77 | ||
78 | // build column definition | |
79 | foreach ($columns as $column) { | |
80 | if (!empty($columnDefinition)) $columnDefinition .= ','; | |
81 | $columnDefinition .= $this->buildColumnDefinition($column['name'], $column['data']); | |
82 | } | |
83 | ||
84 | // build index definition | |
85 | $hasFulltextIndex = false; | |
86 | foreach ($indices as $index) { | |
87 | if (!empty($indexDefinition)) $indexDefinition .= ','; | |
88 | $indexDefinition .= $this->buildIndexDefinition($index['name'], $index['data']); | |
89 | if ($index['data']['type'] == 'FULLTEXT') $hasFulltextIndex = true; | |
90 | } | |
91 | ||
92 | // create table | |
93 | $sql = "CREATE TABLE ".$tableName." ( | |
94 | ".$columnDefinition." | |
95 | ".(!empty($indexDefinition) ? ',' : '')." | |
96 | ".$indexDefinition." | |
288fb832 | 97 | ) ENGINE=".($hasFulltextIndex ? 'MyISAM' : 'InnoDB')." DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; |
11ade432 AE |
98 | $statement = $this->dbObj->prepareStatement($sql); |
99 | $statement->execute(); | |
100 | } | |
101 | ||
102 | /** | |
0ad90fc3 | 103 | * @see \wcf\system\database\editor\DatabaseEditor::dropTable() |
11ade432 AE |
104 | */ |
105 | public function dropTable($tableName) { | |
106 | $sql = "DROP TABLE IF EXISTS ".$tableName; | |
107 | $statement = $this->dbObj->prepareStatement($sql); | |
108 | $statement->execute(); | |
109 | } | |
110 | ||
111 | /** | |
0ad90fc3 | 112 | * @see \wcf\system\database\editor\DatabaseEditor::addColumn() |
11ade432 AE |
113 | */ |
114 | public function addColumn($tableName, $columnName, $columnData) { | |
115 | $sql = "ALTER TABLE ".$tableName." ADD COLUMN ".$this->buildColumnDefinition($columnName, $columnData); | |
116 | $statement = $this->dbObj->prepareStatement($sql); | |
117 | $statement->execute(); | |
118 | } | |
119 | ||
120 | /** | |
0ad90fc3 | 121 | * @see \wcf\system\database\editor\DatabaseEditor::alterColumn() |
11ade432 AE |
122 | */ |
123 | public function alterColumn($tableName, $oldColumnName, $newColumnName, $newColumnData) { | |
124 | $sql = "ALTER TABLE ".$tableName." CHANGE COLUMN ".$oldColumnName." ".$this->buildColumnDefinition($newColumnName, $newColumnData); | |
125 | $statement = $this->dbObj->prepareStatement($sql); | |
126 | $statement->execute(); | |
127 | } | |
128 | ||
129 | /** | |
0ad90fc3 | 130 | * @see \wcf\system\database\editor\DatabaseEditor::dropColumn() |
11ade432 AE |
131 | */ |
132 | public function dropColumn($tableName, $columnName) { | |
133 | $sql = "ALTER TABLE ".$tableName." DROP COLUMN ".$columnName; | |
134 | $statement = $this->dbObj->prepareStatement($sql); | |
135 | $statement->execute(); | |
136 | } | |
137 | ||
138 | /** | |
0ad90fc3 | 139 | * @see \wcf\system\database\editor\DatabaseEditor::addIndex() |
11ade432 AE |
140 | */ |
141 | public function addIndex($tableName, $indexName, $indexData) { | |
142 | $sql = "ALTER TABLE ".$tableName." ADD ".$this->buildIndexDefinition($indexName, $indexData); | |
143 | $statement = $this->dbObj->prepareStatement($sql); | |
144 | $statement->execute(); | |
145 | } | |
146 | ||
147 | /** | |
0ad90fc3 | 148 | * @see \wcf\system\database\editor\DatabaseEditor::addIndex() |
11ade432 AE |
149 | */ |
150 | public function addForeignKey($tableName, $indexName, $indexData) { | |
151 | $sql = "ALTER TABLE ".$tableName." ADD"; | |
152 | ||
153 | // add index name | |
96017679 | 154 | if (!empty($indexName)) $sql .= " CONSTRAINT `".$indexName."`"; |
11ade432 AE |
155 | |
156 | // add columns | |
157 | $sql .= " FOREIGN KEY (".str_replace(',', ',', preg_replace('/\s+/', '', $indexData['columns'])).")"; | |
158 | ||
159 | // add referenced table name | |
160 | $sql .= " REFERENCES ".$indexData['referencedTable']; | |
161 | ||
162 | // add referenced columns | |
163 | $sql .= " (".str_replace(',', ',', preg_replace('/\s+/', '', $indexData['referencedColumns'])).")"; | |
164 | ||
165 | // add operation and action | |
166 | if (!empty($indexData['operation'])) $sql .= " ON ".$indexData['operation']." ".$indexData['action']; | |
167 | ||
168 | $statement = $this->dbObj->prepareStatement($sql); | |
169 | $statement->execute(); | |
170 | } | |
171 | ||
172 | /** | |
0ad90fc3 | 173 | * @see \wcf\system\database\editor\DatabaseEditor::dropIndex() |
11ade432 AE |
174 | */ |
175 | public function dropIndex($tableName, $indexName) { | |
176 | $sql = "ALTER TABLE ".$tableName." DROP INDEX ".$indexName; | |
177 | $statement = $this->dbObj->prepareStatement($sql); | |
178 | $statement->execute(); | |
179 | } | |
180 | ||
b6ae7d04 | 181 | /** |
0ad90fc3 | 182 | * @see \wcf\system\database\editor\DatabaseEditor::dropForeignKey() |
b6ae7d04 MW |
183 | */ |
184 | public function dropForeignKey($tableName, $indexName) { | |
96017679 | 185 | $sql = "ALTER TABLE ".$tableName." DROP FOREIGN KEY `".$indexName."`"; |
b6ae7d04 MW |
186 | $statement = $this->dbObj->prepareStatement($sql); |
187 | $statement->execute(); | |
188 | } | |
189 | ||
11ade432 AE |
190 | /** |
191 | * Builds a column definition for execution in a create table or alter table statement. | |
192 | * | |
193 | * @param string $columnName | |
194 | * @param array $columnData | |
195 | * @param string | |
196 | */ | |
197 | protected function buildColumnDefinition($columnName, $columnData) { | |
198 | // column name | |
199 | $definition = $columnName; | |
200 | // column type | |
201 | $definition .= " ".$columnData['type']; | |
202 | // column length and decimals | |
203 | if (!empty($columnData['length'])) { | |
204 | $definition .= "(".$columnData['length'].(!empty($columnData['decimals']) ? ",".$columnData['decimals'] : "").")"; | |
205 | } | |
206 | // enum / set | |
207 | if ($columnData['type'] == 'enum' && !empty($columnData['values'])) { | |
208 | $definition .= "(".$columnData['values'].")"; | |
209 | } | |
210 | // not null / null | |
211 | if (!empty($columnData['notNull'])) $definition .= " NOT NULL"; | |
212 | // default | |
213 | if (isset($columnData['default']) && $columnData['default'] !== '') $definition .= " DEFAULT ".$columnData['default']; | |
214 | // auto_increment | |
215 | if (!empty($columnData['autoIncrement'])) $definition .= " AUTO_INCREMENT"; | |
216 | // key | |
217 | if (!empty($columnData['key'])) $definition .= " ".$columnData['key']." KEY"; | |
218 | ||
219 | return $definition; | |
220 | } | |
221 | ||
222 | /** | |
223 | * Builds a index definition for execution in a create table or alter table statement. | |
224 | * | |
225 | * @param string $indexName | |
226 | * @param array $indexData | |
227 | * @param string | |
228 | */ | |
229 | protected function buildIndexDefinition($indexName, $indexData) { | |
230 | $definition = ""; | |
231 | // index type | |
232 | if ($indexData['type'] == 'PRIMARY') $definition = "PRIMARY KEY"; | |
233 | else if ($indexData['type'] == 'UNIQUE') $definition = "UNIQUE KEY"; | |
234 | else if ($indexData['type'] == 'FULLTEXT') $definition = "FULLTEXT KEY"; | |
235 | else $definition = "KEY"; | |
236 | ||
237 | // index name | |
238 | if (!empty($indexName)) $definition .= " ".$indexName.""; | |
239 | // columns | |
240 | $definition .= " (".str_replace(',', ',', preg_replace('/\s+/', '', $indexData['columns'])).")"; | |
241 | ||
242 | return $definition; | |
243 | } | |
244 | } |