|
0
|
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 |
$queryParts = array(); |
|
|
458 |
if ($diff->newName !== false) { |
|
|
459 |
$queryParts[] = 'RENAME TO ' . $diff->newName; |
|
|
460 |
} |
|
|
461 |
|
|
|
462 |
foreach ($diff->addedColumns AS $fieldName => $column) { |
|
|
463 |
$columnArray = $column->toArray(); |
|
|
464 |
$columnArray['comment'] = $this->getColumnComment($column); |
|
|
465 |
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
|
|
466 |
} |
|
|
467 |
|
|
|
468 |
foreach ($diff->removedColumns AS $column) { |
|
|
469 |
$queryParts[] = 'DROP ' . $column->getQuotedName($this); |
|
|
470 |
} |
|
|
471 |
|
|
|
472 |
foreach ($diff->changedColumns AS $columnDiff) { |
|
|
473 |
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ |
|
|
474 |
$column = $columnDiff->column; |
|
|
475 |
$columnArray = $column->toArray(); |
|
|
476 |
$columnArray['comment'] = $this->getColumnComment($column); |
|
|
477 |
$queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' ' |
|
|
478 |
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
|
|
479 |
} |
|
|
480 |
|
|
|
481 |
foreach ($diff->renamedColumns AS $oldColumnName => $column) { |
|
|
482 |
$columnArray = $column->toArray(); |
|
|
483 |
$columnArray['comment'] = $this->getColumnComment($column); |
|
|
484 |
$queryParts[] = 'CHANGE ' . $oldColumnName . ' ' |
|
|
485 |
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray); |
|
|
486 |
} |
|
|
487 |
|
|
|
488 |
$sql = array(); |
|
|
489 |
if (count($queryParts) > 0) { |
|
|
490 |
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts); |
|
|
491 |
} |
|
|
492 |
$sql = array_merge( |
|
|
493 |
$this->getPreAlterTableIndexForeignKeySQL($diff), |
|
|
494 |
$sql, |
|
|
495 |
$this->getPostAlterTableIndexForeignKeySQL($diff) |
|
|
496 |
); |
|
|
497 |
return $sql; |
|
|
498 |
} |
|
|
499 |
|
|
|
500 |
/** |
|
|
501 |
* Obtain DBMS specific SQL code portion needed to declare an integer type |
|
|
502 |
* field to be used in statements like CREATE TABLE. |
|
|
503 |
* |
|
|
504 |
* @param string $name name the field to be declared. |
|
|
505 |
* @param string $field associative array with the name of the properties |
|
|
506 |
* of the field being declared as array indexes. |
|
|
507 |
* Currently, the types of supported field |
|
|
508 |
* properties are as follows: |
|
|
509 |
* |
|
|
510 |
* unsigned |
|
|
511 |
* Boolean flag that indicates whether the field |
|
|
512 |
* should be declared as unsigned integer if |
|
|
513 |
* possible. |
|
|
514 |
* |
|
|
515 |
* default |
|
|
516 |
* Integer value to be used as default for this |
|
|
517 |
* field. |
|
|
518 |
* |
|
|
519 |
* notnull |
|
|
520 |
* Boolean flag that indicates whether this field is |
|
|
521 |
* constrained to not be set to null. |
|
|
522 |
* @return string DBMS specific SQL code portion that should be used to |
|
|
523 |
* declare the specified field. |
|
|
524 |
* @override |
|
|
525 |
*/ |
|
|
526 |
public function getIntegerTypeDeclarationSQL(array $field) |
|
|
527 |
{ |
|
|
528 |
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
|
529 |
} |
|
|
530 |
|
|
|
531 |
/** @override */ |
|
|
532 |
public function getBigIntTypeDeclarationSQL(array $field) |
|
|
533 |
{ |
|
|
534 |
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
|
535 |
} |
|
|
536 |
|
|
|
537 |
/** @override */ |
|
|
538 |
public function getSmallIntTypeDeclarationSQL(array $field) |
|
|
539 |
{ |
|
|
540 |
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
|
541 |
} |
|
|
542 |
|
|
|
543 |
/** @override */ |
|
|
544 |
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
|
|
545 |
{ |
|
|
546 |
$autoinc = ''; |
|
|
547 |
if ( ! empty($columnDef['autoincrement'])) { |
|
|
548 |
$autoinc = ' AUTO_INCREMENT'; |
|
|
549 |
} |
|
|
550 |
$unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : ''; |
|
|
551 |
|
|
|
552 |
return $unsigned . $autoinc; |
|
|
553 |
} |
|
|
554 |
|
|
|
555 |
/** |
|
|
556 |
* Return the FOREIGN KEY query section dealing with non-standard options |
|
|
557 |
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ... |
|
|
558 |
* |
|
|
559 |
* @param ForeignKeyConstraint $foreignKey |
|
|
560 |
* @return string |
|
|
561 |
* @override |
|
|
562 |
*/ |
|
|
563 |
public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey) |
|
|
564 |
{ |
|
|
565 |
$query = ''; |
|
|
566 |
if ($foreignKey->hasOption('match')) { |
|
|
567 |
$query .= ' MATCH ' . $foreignKey->getOption('match'); |
|
|
568 |
} |
|
|
569 |
$query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey); |
|
|
570 |
return $query; |
|
|
571 |
} |
|
|
572 |
|
|
|
573 |
/** |
|
|
574 |
* Gets the SQL to drop an index of a table. |
|
|
575 |
* |
|
|
576 |
* @param Index $index name of the index to be dropped |
|
|
577 |
* @param string|Table $table name of table that should be used in method |
|
|
578 |
* @override |
|
|
579 |
*/ |
|
|
580 |
public function getDropIndexSQL($index, $table=null) |
|
|
581 |
{ |
|
|
582 |
if($index instanceof Index) { |
|
|
583 |
$indexName = $index->getQuotedName($this); |
|
|
584 |
} else if(is_string($index)) { |
|
|
585 |
$indexName = $index; |
|
|
586 |
} else { |
|
|
587 |
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.'); |
|
|
588 |
} |
|
|
589 |
|
|
|
590 |
if($table instanceof Table) { |
|
|
591 |
$table = $table->getQuotedName($this); |
|
|
592 |
} else if(!is_string($table)) { |
|
|
593 |
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
|
|
594 |
} |
|
|
595 |
|
|
|
596 |
if ($index instanceof Index && $index->isPrimary()) { |
|
|
597 |
// mysql primary keys are always named "PRIMARY", |
|
|
598 |
// so we cannot use them in statements because of them being keyword. |
|
|
599 |
return $this->getDropPrimaryKeySQL($table); |
|
|
600 |
} |
|
|
601 |
|
|
|
602 |
return 'DROP INDEX ' . $indexName . ' ON ' . $table; |
|
|
603 |
} |
|
|
604 |
|
|
|
605 |
/** |
|
|
606 |
* @param Index $index |
|
|
607 |
* @param Table $table |
|
|
608 |
*/ |
|
|
609 |
protected function getDropPrimaryKeySQL($table) |
|
|
610 |
{ |
|
|
611 |
return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY'; |
|
|
612 |
} |
|
|
613 |
|
|
|
614 |
/** |
|
|
615 |
* Gets the SQL to drop a table. |
|
|
616 |
* |
|
|
617 |
* @param string $table The name of table to drop. |
|
|
618 |
* @override |
|
|
619 |
*/ |
|
|
620 |
public function getDropTableSQL($table) |
|
|
621 |
{ |
|
|
622 |
if ($table instanceof \Doctrine\DBAL\Schema\Table) { |
|
|
623 |
$table = $table->getQuotedName($this); |
|
|
624 |
} else if(!is_string($table)) { |
|
|
625 |
throw new \InvalidArgumentException('MysqlPlatform::getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.'); |
|
|
626 |
} |
|
|
627 |
|
|
|
628 |
return 'DROP TABLE ' . $table; |
|
|
629 |
} |
|
|
630 |
|
|
|
631 |
public function getSetTransactionIsolationSQL($level) |
|
|
632 |
{ |
|
|
633 |
return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
|
|
634 |
} |
|
|
635 |
|
|
|
636 |
/** |
|
|
637 |
* Get the platform name for this instance. |
|
|
638 |
* |
|
|
639 |
* @return string |
|
|
640 |
*/ |
|
|
641 |
public function getName() |
|
|
642 |
{ |
|
|
643 |
return 'mysql'; |
|
|
644 |
} |
|
|
645 |
|
|
|
646 |
public function getReadLockSQL() |
|
|
647 |
{ |
|
|
648 |
return 'LOCK IN SHARE MODE'; |
|
|
649 |
} |
|
|
650 |
|
|
|
651 |
protected function initializeDoctrineTypeMappings() |
|
|
652 |
{ |
|
|
653 |
$this->doctrineTypeMapping = array( |
|
|
654 |
'tinyint' => 'boolean', |
|
|
655 |
'smallint' => 'smallint', |
|
|
656 |
'mediumint' => 'integer', |
|
|
657 |
'int' => 'integer', |
|
|
658 |
'integer' => 'integer', |
|
|
659 |
'bigint' => 'bigint', |
|
|
660 |
'tinytext' => 'text', |
|
|
661 |
'mediumtext' => 'text', |
|
|
662 |
'longtext' => 'text', |
|
|
663 |
'text' => 'text', |
|
|
664 |
'varchar' => 'string', |
|
|
665 |
'string' => 'string', |
|
|
666 |
'char' => 'string', |
|
|
667 |
'date' => 'date', |
|
|
668 |
'datetime' => 'datetime', |
|
|
669 |
'timestamp' => 'datetime', |
|
|
670 |
'time' => 'time', |
|
|
671 |
'float' => 'float', |
|
|
672 |
'double' => 'float', |
|
|
673 |
'real' => 'float', |
|
|
674 |
'decimal' => 'decimal', |
|
|
675 |
'numeric' => 'decimal', |
|
|
676 |
'year' => 'date', |
|
|
677 |
); |
|
|
678 |
} |
|
|
679 |
|
|
|
680 |
public function getVarcharMaxLength() |
|
|
681 |
{ |
|
|
682 |
return 65535; |
|
|
683 |
} |
|
|
684 |
|
|
|
685 |
protected function getReservedKeywordsClass() |
|
|
686 |
{ |
|
|
687 |
return 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords'; |
|
|
688 |
} |
|
|
689 |
} |