1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Platforms; |
---|
21 | |
---|
22 | use Doctrine\DBAL\DBALException, |
---|
23 | Doctrine\DBAL\Schema\TableDiff, |
---|
24 | Doctrine\DBAL\Schema\Index, |
---|
25 | Doctrine\DBAL\Schema\Table; |
---|
26 | |
---|
27 | /** |
---|
28 | * The MySqlPlatform provides the behavior, features and SQL dialect of the |
---|
29 | * MySQL database platform. This platform represents a MySQL 5.0 or greater platform that |
---|
30 | * uses the InnoDB storage engine. |
---|
31 | * |
---|
32 | * @since 2.0 |
---|
33 | * @author Roman Borschel <roman@code-factory.org> |
---|
34 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
35 | * @todo Rename: MySQLPlatform |
---|
36 | */ |
---|
37 | class MySqlPlatform extends AbstractPlatform |
---|
38 | { |
---|
39 | /** |
---|
40 | * Gets the character used for identifier quoting. |
---|
41 | * |
---|
42 | * @return string |
---|
43 | * @override |
---|
44 | */ |
---|
45 | public function getIdentifierQuoteCharacter() |
---|
46 | { |
---|
47 | return '`'; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Returns the regular expression operator. |
---|
52 | * |
---|
53 | * @return string |
---|
54 | * @override |
---|
55 | */ |
---|
56 | public function getRegexpExpression() |
---|
57 | { |
---|
58 | return 'RLIKE'; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Returns global unique identifier |
---|
63 | * |
---|
64 | * @return string to get global unique identifier |
---|
65 | * @override |
---|
66 | */ |
---|
67 | public function getGuidExpression() |
---|
68 | { |
---|
69 | return 'UUID()'; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * returns the position of the first occurrence of substring $substr in string $str |
---|
74 | * |
---|
75 | * @param string $substr literal string to find |
---|
76 | * @param string $str literal string |
---|
77 | * @param int $pos position to start at, beginning of string by default |
---|
78 | * @return integer |
---|
79 | */ |
---|
80 | public function getLocateExpression($str, $substr, $startPos = false) |
---|
81 | { |
---|
82 | if ($startPos == false) { |
---|
83 | return 'LOCATE(' . $substr . ', ' . $str . ')'; |
---|
84 | } else { |
---|
85 | return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')'; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Returns a series of strings concatinated |
---|
91 | * |
---|
92 | * concat() accepts an arbitrary number of parameters. Each parameter |
---|
93 | * must contain an expression or an array with expressions. |
---|
94 | * |
---|
95 | * @param string|array(string) strings that will be concatinated. |
---|
96 | * @override |
---|
97 | */ |
---|
98 | public function getConcatExpression() |
---|
99 | { |
---|
100 | $args = func_get_args(); |
---|
101 | return 'CONCAT(' . join(', ', (array) $args) . ')'; |
---|
102 | } |
---|
103 | |
---|
104 | public function getDateDiffExpression($date1, $date2) |
---|
105 | { |
---|
106 | return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; |
---|
107 | } |
---|
108 | |
---|
109 | public function getDateAddDaysExpression($date, $days) |
---|
110 | { |
---|
111 | return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)'; |
---|
112 | } |
---|
113 | |
---|
114 | public function getDateSubDaysExpression($date, $days) |
---|
115 | { |
---|
116 | return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)'; |
---|
117 | } |
---|
118 | |
---|
119 | public function getDateAddMonthExpression($date, $months) |
---|
120 | { |
---|
121 | return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)'; |
---|
122 | } |
---|
123 | |
---|
124 | public function getDateSubMonthExpression($date, $months) |
---|
125 | { |
---|
126 | return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)'; |
---|
127 | } |
---|
128 | |
---|
129 | public function getListDatabasesSQL() |
---|
130 | { |
---|
131 | return 'SHOW DATABASES'; |
---|
132 | } |
---|
133 | |
---|
134 | public function getListTableConstraintsSQL($table) |
---|
135 | { |
---|
136 | return 'SHOW INDEX FROM ' . $table; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Two approaches to listing the table indexes. The information_schema is |
---|
141 | * prefered, because it doesn't cause problems with SQL keywords such as "order" or "table". |
---|
142 | * |
---|
143 | * @param string $table |
---|
144 | * @param string $currentDatabase |
---|
145 | * @return string |
---|
146 | */ |
---|
147 | public function getListTableIndexesSQL($table, $currentDatabase = null) |
---|
148 | { |
---|
149 | if ($currentDatabase) { |
---|
150 | return "SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ". |
---|
151 | "SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ". |
---|
152 | "CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, " . |
---|
153 | "NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment " . |
---|
154 | "FROM information_schema.STATISTICS WHERE TABLE_NAME = '" . $table . "' AND TABLE_SCHEMA = '" . $currentDatabase . "'"; |
---|
155 | } else { |
---|
156 | return 'SHOW INDEX FROM ' . $table; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | public function getListViewsSQL($database) |
---|
161 | { |
---|
162 | return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'"; |
---|
163 | } |
---|
164 | |
---|
165 | public function getListTableForeignKeysSQL($table, $database = null) |
---|
166 | { |
---|
167 | $sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ". |
---|
168 | "k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ". |
---|
169 | "FROM information_schema.key_column_usage k /*!50116 ". |
---|
170 | "INNER JOIN information_schema.referential_constraints c ON ". |
---|
171 | " c.constraint_name = k.constraint_name AND ". |
---|
172 | " c.table_name = '$table' */ WHERE k.table_name = '$table'"; |
---|
173 | |
---|
174 | if ($database) { |
---|
175 | $sql .= " AND k.table_schema = '$database' /*!50116 AND c.constraint_schema = '$database' */"; |
---|
176 | } |
---|
177 | |
---|
178 | $sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL"; |
---|
179 | |
---|
180 | return $sql; |
---|
181 | } |
---|
182 | |
---|
183 | public function getCreateViewSQL($name, $sql) |
---|
184 | { |
---|
185 | return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
---|
186 | } |
---|
187 | |
---|
188 | public function getDropViewSQL($name) |
---|
189 | { |
---|
190 | return 'DROP VIEW '. $name; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Gets the SQL snippet used to declare a VARCHAR column on the MySql platform. |
---|
195 | * |
---|
196 | * @params array $field |
---|
197 | */ |
---|
198 | protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
---|
199 | { |
---|
200 | return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') |
---|
201 | : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)'); |
---|
202 | } |
---|
203 | |
---|
204 | /** @override */ |
---|
205 | public function getClobTypeDeclarationSQL(array $field) |
---|
206 | { |
---|
207 | if ( ! empty($field['length']) && is_numeric($field['length'])) { |
---|
208 | $length = $field['length']; |
---|
209 | if ($length <= 255) { |
---|
210 | return 'TINYTEXT'; |
---|
211 | } else if ($length <= 65532) { |
---|
212 | return 'TEXT'; |
---|
213 | } else if ($length <= 16777215) { |
---|
214 | return 'MEDIUMTEXT'; |
---|
215 | } |
---|
216 | } |
---|
217 | return 'LONGTEXT'; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * @override |
---|
222 | */ |
---|
223 | public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
---|
224 | { |
---|
225 | if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) { |
---|
226 | return 'TIMESTAMP'; |
---|
227 | } else { |
---|
228 | return 'DATETIME'; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * @override |
---|
234 | */ |
---|
235 | public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
---|
236 | { |
---|
237 | return 'DATE'; |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * @override |
---|
242 | */ |
---|
243 | public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
---|
244 | { |
---|
245 | return 'TIME'; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * @override |
---|
250 | */ |
---|
251 | public function getBooleanTypeDeclarationSQL(array $field) |
---|
252 | { |
---|
253 | return 'TINYINT(1)'; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * Obtain DBMS specific SQL code portion needed to set the COLLATION |
---|
258 | * of a field declaration to be used in statements like CREATE TABLE. |
---|
259 | * |
---|
260 | * @param string $collation name of the collation |
---|
261 | * @return string DBMS specific SQL code portion needed to set the COLLATION |
---|
262 | * of a field declaration. |
---|
263 | */ |
---|
264 | public function getCollationFieldDeclaration($collation) |
---|
265 | { |
---|
266 | return 'COLLATE ' . $collation; |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Whether the platform prefers identity columns for ID generation. |
---|
271 | * MySql prefers "autoincrement" identity columns since sequences can only |
---|
272 | * be emulated with a table. |
---|
273 | * |
---|
274 | * @return boolean |
---|
275 | * @override |
---|
276 | */ |
---|
277 | public function prefersIdentityColumns() |
---|
278 | { |
---|
279 | return true; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * Whether the platform supports identity columns. |
---|
284 | * MySql supports this through AUTO_INCREMENT columns. |
---|
285 | * |
---|
286 | * @return boolean |
---|
287 | * @override |
---|
288 | */ |
---|
289 | public function supportsIdentityColumns() |
---|
290 | { |
---|
291 | return true; |
---|
292 | } |
---|
293 | |
---|
294 | public function supportsInlineColumnComments() |
---|
295 | { |
---|
296 | return true; |
---|
297 | } |
---|
298 | |
---|
299 | public function getShowDatabasesSQL() |
---|
300 | { |
---|
301 | return 'SHOW DATABASES'; |
---|
302 | } |
---|
303 | |
---|
304 | public function getListTablesSQL() |
---|
305 | { |
---|
306 | return "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"; |
---|
307 | } |
---|
308 | |
---|
309 | public function getListTableColumnsSQL($table, $database = null) |
---|
310 | { |
---|
311 | if ($database) { |
---|
312 | return "SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, ". |
---|
313 | "COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, " . |
---|
314 | "CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS CollactionName ". |
---|
315 | "FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . $database . "' AND TABLE_NAME = '" . $table . "'"; |
---|
316 | } else { |
---|
317 | return 'DESCRIBE ' . $table; |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * create a new database |
---|
323 | * |
---|
324 | * @param string $name name of the database that should be created |
---|
325 | * @return string |
---|
326 | * @override |
---|
327 | */ |
---|
328 | public function getCreateDatabaseSQL($name) |
---|
329 | { |
---|
330 | return 'CREATE DATABASE ' . $name; |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * drop an existing database |
---|
335 | * |
---|
336 | * @param string $name name of the database that should be dropped |
---|
337 | * @return string |
---|
338 | * @override |
---|
339 | */ |
---|
340 | public function getDropDatabaseSQL($name) |
---|
341 | { |
---|
342 | return 'DROP DATABASE ' . $name; |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * create a new table |
---|
347 | * |
---|
348 | * @param string $tableName Name of the database that should be created |
---|
349 | * @param array $columns Associative array that contains the definition of each field of the new table |
---|
350 | * The indexes of the array entries are the names of the fields of the table an |
---|
351 | * the array entry values are associative arrays like those that are meant to be |
---|
352 | * passed with the field definitions to get[Type]Declaration() functions. |
---|
353 | * array( |
---|
354 | * 'id' => array( |
---|
355 | * 'type' => 'integer', |
---|
356 | * 'unsigned' => 1 |
---|
357 | * 'notnull' => 1 |
---|
358 | * 'default' => 0 |
---|
359 | * ), |
---|
360 | * 'name' => array( |
---|
361 | * 'type' => 'text', |
---|
362 | * 'length' => 12 |
---|
363 | * ), |
---|
364 | * 'password' => array( |
---|
365 | * 'type' => 'text', |
---|
366 | * 'length' => 12 |
---|
367 | * ) |
---|
368 | * ); |
---|
369 | * @param array $options An associative array of table options: |
---|
370 | * array( |
---|
371 | * 'comment' => 'Foo', |
---|
372 | * 'charset' => 'utf8', |
---|
373 | * 'collate' => 'utf8_unicode_ci', |
---|
374 | * 'engine' => 'innodb', |
---|
375 | * 'foreignKeys' => array( |
---|
376 | * new ForeignKeyConstraint(), |
---|
377 | * new ForeignKeyConstraint(), |
---|
378 | * new ForeignKeyConstraint(), |
---|
379 | * // etc |
---|
380 | * ) |
---|
381 | * ); |
---|
382 | * |
---|
383 | * @return void |
---|
384 | * @override |
---|
385 | */ |
---|
386 | protected function _getCreateTableSQL($tableName, array $columns, array $options = array()) |
---|
387 | { |
---|
388 | $queryFields = $this->getColumnDeclarationListSQL($columns); |
---|
389 | |
---|
390 | if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { |
---|
391 | foreach ($options['uniqueConstraints'] as $index => $definition) { |
---|
392 | $queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | // add all indexes |
---|
397 | if (isset($options['indexes']) && ! empty($options['indexes'])) { |
---|
398 | foreach($options['indexes'] as $index => $definition) { |
---|
399 | $queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition); |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | // attach all primary keys |
---|
404 | if (isset($options['primary']) && ! empty($options['primary'])) { |
---|
405 | $keyColumns = array_unique(array_values($options['primary'])); |
---|
406 | $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')'; |
---|
407 | } |
---|
408 | |
---|
409 | $query = 'CREATE '; |
---|
410 | if (!empty($options['temporary'])) { |
---|
411 | $query .= 'TEMPORARY '; |
---|
412 | } |
---|
413 | $query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')'; |
---|
414 | |
---|
415 | $optionStrings = array(); |
---|
416 | |
---|
417 | if (isset($options['comment'])) { |
---|
418 | $optionStrings['comment'] = 'COMMENT = ' . $options['comment']; |
---|
419 | } |
---|
420 | if (isset($options['charset'])) { |
---|
421 | $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset']; |
---|
422 | if (isset($options['collate'])) { |
---|
423 | $optionStrings['charset'] .= ' COLLATE ' . $options['collate']; |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | // get the type of the table |
---|
428 | if (isset($options['engine'])) { |
---|
429 | $optionStrings[] = 'ENGINE = ' . $options['engine']; |
---|
430 | } else { |
---|
431 | // default to innodb |
---|
432 | $optionStrings[] = 'ENGINE = InnoDB'; |
---|
433 | } |
---|
434 | |
---|
435 | if ( ! empty($optionStrings)) { |
---|
436 | $query.= ' '.implode(' ', $optionStrings); |
---|
437 | } |
---|
438 | $sql[] = $query; |
---|
439 | |
---|
440 | if (isset($options['foreignKeys'])) { |
---|
441 | foreach ((array) $options['foreignKeys'] as $definition) { |
---|
442 | $sql[] = $this->getCreateForeignKeySQL($definition, $tableName); |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | return $sql; |
---|
447 | } |
---|
448 | |
---|
449 | /** |
---|
450 | * Gets the SQL to alter an existing table. |
---|
451 | * |
---|
452 | * @param TableDiff $diff |
---|
453 | * @return array |
---|
454 | */ |
---|
455 | public function getAlterTableSQL(TableDiff $diff) |
---|
456 | { |
---|
457 | $columnSql = array(); |
---|
458 | $queryParts = array(); |
---|
459 | if ($diff->newName !== false) { |
---|
460 | $queryParts[] = 'RENAME TO ' . $diff->newName; |
---|
461 | } |
---|
462 | |
---|
463 | foreach ($diff->addedColumns AS $fieldName => $column) { |
---|
464 | if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
---|
465 | continue; |
---|
466 | } |
---|
467 | |
---|
468 | $columnArray = $column->toArray(); |
---|
469 | $columnArray['comment'] = $this->getColumnComment($column); |
---|
470 | $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
---|
471 | } |
---|
472 | |
---|
473 | foreach ($diff->removedColumns AS $column) { |
---|
474 | if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
---|
475 | continue; |
---|
476 | } |
---|
477 | |
---|
478 | $queryParts[] = 'DROP ' . $column->getQuotedName($this); |
---|
479 | } |
---|
480 | |
---|
481 | foreach ($diff->changedColumns AS $columnDiff) { |
---|
482 | if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
---|
483 | continue; |
---|
484 | } |
---|
485 | |
---|
486 | /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ |
---|
487 | $column = $columnDiff->column; |
---|
488 | $columnArray = $column->toArray(); |
---|
489 | $columnArray['comment'] = $this->getColumnComment($column); |
---|
490 | $queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' ' |
---|
491 | . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
---|
492 | } |
---|
493 | |
---|
494 | foreach ($diff->renamedColumns AS $oldColumnName => $column) { |
---|
495 | if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
---|
496 | continue; |
---|
497 | } |
---|
498 | |
---|
499 | $columnArray = $column->toArray(); |
---|
500 | $columnArray['comment'] = $this->getColumnComment($column); |
---|
501 | $queryParts[] = 'CHANGE ' . $oldColumnName . ' ' |
---|
502 | . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
---|
503 | } |
---|
504 | |
---|
505 | $sql = array(); |
---|
506 | $tableSql = array(); |
---|
507 | |
---|
508 | if (!$this->onSchemaAlterTable($diff, $tableSql)) { |
---|
509 | if (count($queryParts) > 0) { |
---|
510 | $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts); |
---|
511 | } |
---|
512 | $sql = array_merge( |
---|
513 | $this->getPreAlterTableIndexForeignKeySQL($diff), |
---|
514 | $sql, |
---|
515 | $this->getPostAlterTableIndexForeignKeySQL($diff) |
---|
516 | ); |
---|
517 | } |
---|
518 | |
---|
519 | return array_merge($sql, $tableSql, $columnSql); |
---|
520 | } |
---|
521 | |
---|
522 | /** |
---|
523 | * Fix for DROP/CREATE index after foreign key change from OneToOne to ManyToOne |
---|
524 | * |
---|
525 | * @param TableDiff $diff |
---|
526 | * @return array |
---|
527 | */ |
---|
528 | protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff) |
---|
529 | { |
---|
530 | $sql = array(); |
---|
531 | $table = $diff->name; |
---|
532 | |
---|
533 | foreach ($diff->removedIndexes AS $remKey => $remIndex) { |
---|
534 | |
---|
535 | foreach ($diff->addedIndexes as $addKey => $addIndex) { |
---|
536 | if ($remIndex->getColumns() == $addIndex->getColumns()) { |
---|
537 | |
---|
538 | $columns = $addIndex->getColumns(); |
---|
539 | $type = ''; |
---|
540 | if ($addIndex->isUnique()) { |
---|
541 | $type = 'UNIQUE '; |
---|
542 | } |
---|
543 | |
---|
544 | $query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', '; |
---|
545 | $query .= 'ADD ' . $type . 'INDEX ' . $addIndex->getName(); |
---|
546 | $query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')'; |
---|
547 | |
---|
548 | $sql[] = $query; |
---|
549 | |
---|
550 | unset($diff->removedIndexes[$remKey]); |
---|
551 | unset($diff->addedIndexes[$addKey]); |
---|
552 | |
---|
553 | break; |
---|
554 | } |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | $sql = array_merge($sql, parent::getPreAlterTableIndexForeignKeySQL($diff)); |
---|
559 | |
---|
560 | return $sql; |
---|
561 | } |
---|
562 | |
---|
563 | /** |
---|
564 | * Obtain DBMS specific SQL code portion needed to declare an integer type |
---|
565 | * field to be used in statements like CREATE TABLE. |
---|
566 | * |
---|
567 | * @param string $name name the field to be declared. |
---|
568 | * @param string $field associative array with the name of the properties |
---|
569 | * of the field being declared as array indexes. |
---|
570 | * Currently, the types of supported field |
---|
571 | * properties are as follows: |
---|
572 | * |
---|
573 | * unsigned |
---|
574 | * Boolean flag that indicates whether the field |
---|
575 | * should be declared as unsigned integer if |
---|
576 | * possible. |
---|
577 | * |
---|
578 | * default |
---|
579 | * Integer value to be used as default for this |
---|
580 | * field. |
---|
581 | * |
---|
582 | * notnull |
---|
583 | * Boolean flag that indicates whether this field is |
---|
584 | * constrained to not be set to null. |
---|
585 | * @return string DBMS specific SQL code portion that should be used to |
---|
586 | * declare the specified field. |
---|
587 | * @override |
---|
588 | */ |
---|
589 | public function getIntegerTypeDeclarationSQL(array $field) |
---|
590 | { |
---|
591 | return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
---|
592 | } |
---|
593 | |
---|
594 | /** @override */ |
---|
595 | public function getBigIntTypeDeclarationSQL(array $field) |
---|
596 | { |
---|
597 | return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
---|
598 | } |
---|
599 | |
---|
600 | /** @override */ |
---|
601 | public function getSmallIntTypeDeclarationSQL(array $field) |
---|
602 | { |
---|
603 | return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
---|
604 | } |
---|
605 | |
---|
606 | /** @override */ |
---|
607 | protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
---|
608 | { |
---|
609 | $autoinc = ''; |
---|
610 | if ( ! empty($columnDef['autoincrement'])) { |
---|
611 | $autoinc = ' AUTO_INCREMENT'; |
---|
612 | } |
---|
613 | $unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : ''; |
---|
614 | |
---|
615 | return $unsigned . $autoinc; |
---|
616 | } |
---|
617 | |
---|
618 | /** |
---|
619 | * Return the FOREIGN KEY query section dealing with non-standard options |
---|
620 | * as MATCH, INITIALLY DEFERRED, ON UPDATE, ... |
---|
621 | * |
---|
622 | * @param ForeignKeyConstraint $foreignKey |
---|
623 | * @return string |
---|
624 | * @override |
---|
625 | */ |
---|
626 | public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey) |
---|
627 | { |
---|
628 | $query = ''; |
---|
629 | if ($foreignKey->hasOption('match')) { |
---|
630 | $query .= ' MATCH ' . $foreignKey->getOption('match'); |
---|
631 | } |
---|
632 | $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey); |
---|
633 | return $query; |
---|
634 | } |
---|
635 | |
---|
636 | /** |
---|
637 | * Gets the SQL to drop an index of a table. |
---|
638 | * |
---|
639 | * @param Index $index name of the index to be dropped |
---|
640 | * @param string|Table $table name of table that should be used in method |
---|
641 | * @override |
---|
642 | */ |
---|
643 | public function getDropIndexSQL($index, $table=null) |
---|
644 | { |
---|
645 | if($index instanceof Index) { |
---|
646 | $indexName = $index->getQuotedName($this); |
---|
647 | } else if(is_string($index)) { |
---|
648 | $indexName = $index; |
---|
649 | } else { |
---|
650 | throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.'); |
---|
651 | } |
---|
652 | |
---|
653 | if($table instanceof Table) { |
---|
654 | $table = $table->getQuotedName($this); |
---|
655 | } else if(!is_string($table)) { |
---|
656 | throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
---|
657 | } |
---|
658 | |
---|
659 | if ($index instanceof Index && $index->isPrimary()) { |
---|
660 | // mysql primary keys are always named "PRIMARY", |
---|
661 | // so we cannot use them in statements because of them being keyword. |
---|
662 | return $this->getDropPrimaryKeySQL($table); |
---|
663 | } |
---|
664 | |
---|
665 | return 'DROP INDEX ' . $indexName . ' ON ' . $table; |
---|
666 | } |
---|
667 | |
---|
668 | /** |
---|
669 | * @param Index $index |
---|
670 | * @param Table $table |
---|
671 | */ |
---|
672 | protected function getDropPrimaryKeySQL($table) |
---|
673 | { |
---|
674 | return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY'; |
---|
675 | } |
---|
676 | |
---|
677 | public function getSetTransactionIsolationSQL($level) |
---|
678 | { |
---|
679 | return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
---|
680 | } |
---|
681 | |
---|
682 | /** |
---|
683 | * Get the platform name for this instance. |
---|
684 | * |
---|
685 | * @return string |
---|
686 | */ |
---|
687 | public function getName() |
---|
688 | { |
---|
689 | return 'mysql'; |
---|
690 | } |
---|
691 | |
---|
692 | public function getReadLockSQL() |
---|
693 | { |
---|
694 | return 'LOCK IN SHARE MODE'; |
---|
695 | } |
---|
696 | |
---|
697 | protected function initializeDoctrineTypeMappings() |
---|
698 | { |
---|
699 | $this->doctrineTypeMapping = array( |
---|
700 | 'tinyint' => 'boolean', |
---|
701 | 'smallint' => 'smallint', |
---|
702 | 'mediumint' => 'integer', |
---|
703 | 'int' => 'integer', |
---|
704 | 'integer' => 'integer', |
---|
705 | 'bigint' => 'bigint', |
---|
706 | 'tinytext' => 'text', |
---|
707 | 'mediumtext' => 'text', |
---|
708 | 'longtext' => 'text', |
---|
709 | 'text' => 'text', |
---|
710 | 'varchar' => 'string', |
---|
711 | 'string' => 'string', |
---|
712 | 'char' => 'string', |
---|
713 | 'date' => 'date', |
---|
714 | 'datetime' => 'datetime', |
---|
715 | 'timestamp' => 'datetime', |
---|
716 | 'time' => 'time', |
---|
717 | 'float' => 'float', |
---|
718 | 'double' => 'float', |
---|
719 | 'real' => 'float', |
---|
720 | 'decimal' => 'decimal', |
---|
721 | 'numeric' => 'decimal', |
---|
722 | 'year' => 'date', |
---|
723 | 'longblob' => 'blob', |
---|
724 | 'blob' => 'blob', |
---|
725 | 'mediumblob' => 'blob', |
---|
726 | 'tinyblob' => 'blob', |
---|
727 | ); |
---|
728 | } |
---|
729 | |
---|
730 | public function getVarcharMaxLength() |
---|
731 | { |
---|
732 | return 65535; |
---|
733 | } |
---|
734 | |
---|
735 | protected function getReservedKeywordsClass() |
---|
736 | { |
---|
737 | return 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords'; |
---|
738 | } |
---|
739 | |
---|
740 | /** |
---|
741 | * Get SQL to safely drop a temporary table WITHOUT implicitly committing an open transaction. |
---|
742 | * |
---|
743 | * MySQL commits a transaction implicitly when DROP TABLE is executed, however not |
---|
744 | * if DROP TEMPORARY TABLE is executed. |
---|
745 | * |
---|
746 | * @throws \InvalidArgumentException |
---|
747 | * @param $table |
---|
748 | * @return string |
---|
749 | */ |
---|
750 | public function getDropTemporaryTableSQL($table) |
---|
751 | { |
---|
752 | if ($table instanceof \Doctrine\DBAL\Schema\Table) { |
---|
753 | $table = $table->getQuotedName($this); |
---|
754 | } else if(!is_string($table)) { |
---|
755 | throw new \InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
---|
756 | } |
---|
757 | |
---|
758 | return 'DROP TEMPORARY TABLE ' . $table; |
---|
759 | } |
---|
760 | |
---|
761 | /** |
---|
762 | * Gets the SQL Snippet used to declare a BLOB column type. |
---|
763 | */ |
---|
764 | public function getBlobTypeDeclarationSQL(array $field) |
---|
765 | { |
---|
766 | return 'LONGBLOB'; |
---|
767 | } |
---|
768 | } |
---|