MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
MySqlPlatform.php
Ir para a documentação deste ficheiro.
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
21
24
36{
44 {
45 return '`';
46 }
47
54 public function getRegexpExpression()
55 {
56 return 'RLIKE';
57 }
58
65 public function getGuidExpression()
66 {
67 return 'UUID()';
68 }
69
78 public function getLocateExpression($str, $substr, $startPos = false)
79 {
80 if ($startPos == false) {
81 return 'LOCATE(' . $substr . ', ' . $str . ')';
82 } else {
83 return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
84 }
85 }
86
96 public function getConcatExpression()
97 {
98 $args = func_get_args();
99 return 'CONCAT(' . join(', ', (array) $args) . ')';
100 }
101
102 public function getListDatabasesSQL()
103 {
104 return 'SHOW DATABASES';
105 }
106
107 public function getListTableConstraintsSQL($table)
108 {
109 return 'SHOW INDEX FROM ' . $table;
110 }
111
112 public function getListTableIndexesSQL($table)
113 {
114 return 'SHOW INDEX FROM ' . $table;
115 }
116
117 public function getListViewsSQL($database)
118 {
119 return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'";
120 }
121
122 public function getListTableForeignKeysSQL($table, $database = null)
123 {
124 $sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ".
125 "k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ".
126 "FROM information_schema.key_column_usage k /*!50116 ".
127 "INNER JOIN information_schema.referential_constraints c ON ".
128 " c.constraint_name = k.constraint_name AND ".
129 " c.table_name = '$table' */ WHERE k.table_name = '$table'";
130
131 if ($database) {
132 $sql .= " AND k.table_schema = '$database' /*!50116 AND c.constraint_schema = '$database' */";
133 }
134
135 $sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL";
136
137 return $sql;
138 }
139
140 public function getCreateViewSQL($name, $sql)
141 {
142 return 'CREATE VIEW ' . $name . ' AS ' . $sql;
143 }
144
145 public function getDropViewSQL($name)
146 {
147 return 'DROP VIEW '. $name;
148 }
149
155 public function getVarcharTypeDeclarationSQL(array $field)
156 {
157 if ( ! isset($field['length'])) {
158 if (array_key_exists('default', $field)) {
159 $field['length'] = $this->getVarcharDefaultLength();
160 } else {
161 $field['length'] = false;
162 }
163 }
164
165 $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
166 $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
167
168 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
169 : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
170 }
171
173 public function getClobTypeDeclarationSQL(array $field)
174 {
175 if ( ! empty($field['length']) && is_numeric($field['length'])) {
176 $length = $field['length'];
177 if ($length <= 255) {
178 return 'TINYTEXT';
179 } else if ($length <= 65532) {
180 return 'TEXT';
181 } else if ($length <= 16777215) {
182 return 'MEDIUMTEXT';
183 }
184 }
185 return 'LONGTEXT';
186 }
187
191 public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
192 {
193 if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
194 return 'TIMESTAMP';
195 } else {
196 return 'DATETIME';
197 }
198 }
199
203 public function getDateTypeDeclarationSQL(array $fieldDeclaration)
204 {
205 return 'DATE';
206 }
207
211 public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
212 {
213 return 'TIME';
214 }
215
219 public function getBooleanTypeDeclarationSQL(array $field)
220 {
221 return 'TINYINT(1)';
222 }
223
232 public function getCollationFieldDeclaration($collation)
233 {
234 return 'COLLATE ' . $collation;
235 }
236
245 public function prefersIdentityColumns()
246 {
247 return true;
248 }
249
257 public function supportsIdentityColumns()
258 {
259 return true;
260 }
261
262 public function getShowDatabasesSQL()
263 {
264 return 'SHOW DATABASES';
265 }
266
267 public function getListTablesSQL()
268 {
269 return 'SHOW FULL TABLES WHERE Table_type = "BASE TABLE"';
270 }
271
272 public function getListTableColumnsSQL($table)
273 {
274 return 'DESCRIBE ' . $table;
275 }
276
284 public function getCreateDatabaseSQL($name)
285 {
286 return 'CREATE DATABASE ' . $name;
287 }
288
296 public function getDropDatabaseSQL($name)
297 {
298 return 'DROP DATABASE ' . $name;
299 }
300
336 protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
337 {
338 $queryFields = $this->getColumnDeclarationListSQL($columns);
339
340 if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
341 foreach ($options['uniqueConstraints'] as $index => $definition) {
342 $queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition);
343 }
344 }
345
346 // add all indexes
347 if (isset($options['indexes']) && ! empty($options['indexes'])) {
348 foreach($options['indexes'] as $index => $definition) {
349 $queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
350 }
351 }
352
353 // attach all primary keys
354 if (isset($options['primary']) && ! empty($options['primary'])) {
355 $keyColumns = array_unique(array_values($options['primary']));
356 $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
357 }
358
359 $query = 'CREATE ';
360 if (!empty($options['temporary'])) {
361 $query .= 'TEMPORARY ';
362 }
363 $query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')';
364
365 $optionStrings = array();
366
367 if (isset($options['comment'])) {
368 $optionStrings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
369 }
370 if (isset($options['charset'])) {
371 $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
372 if (isset($options['collate'])) {
373 $optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
374 }
375 }
376
377 // get the type of the table
378 if (isset($options['engine'])) {
379 $optionStrings[] = 'ENGINE = ' . $options['engine'];
380 } else {
381 // default to innodb
382 $optionStrings[] = 'ENGINE = InnoDB';
383 }
384
385 if ( ! empty($optionStrings)) {
386 $query.= ' '.implode(' ', $optionStrings);
387 }
388 $sql[] = $query;
389
390 if (isset($options['foreignKeys'])) {
391 foreach ((array) $options['foreignKeys'] as $definition) {
392 $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
393 }
394 }
395
396 return $sql;
397 }
398
405 public function getAlterTableSQL(TableDiff $diff)
406 {
407 $queryParts = array();
408 if ($diff->newName !== false) {
409 $queryParts[] = 'RENAME TO ' . $diff->newName;
410 }
411
412 foreach ($diff->addedColumns AS $fieldName => $column) {
413 $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
414 }
415
416 foreach ($diff->removedColumns AS $column) {
417 $queryParts[] = 'DROP ' . $column->getQuotedName($this);
418 }
419
420 foreach ($diff->changedColumns AS $columnDiff) {
421 /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
422 $column = $columnDiff->column;
423 $queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' '
424 . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
425 }
426
427 foreach ($diff->renamedColumns AS $oldColumnName => $column) {
428 $queryParts[] = 'CHANGE ' . $oldColumnName . ' '
429 . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
430 }
431
432 $sql = array();
433 if (count($queryParts) > 0) {
434 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
435 }
436 $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
437 return $sql;
438 }
439
466 public function getIntegerTypeDeclarationSQL(array $field)
467 {
468 return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
469 }
470
472 public function getBigIntTypeDeclarationSQL(array $field)
473 {
474 return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
475 }
476
478 public function getSmallIntTypeDeclarationSQL(array $field)
479 {
480 return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
481 }
482
484 protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
485 {
486 $autoinc = '';
487 if ( ! empty($columnDef['autoincrement'])) {
488 $autoinc = ' AUTO_INCREMENT';
489 }
490 $unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : '';
491
492 return $unsigned . $autoinc;
493 }
494
503 public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
504 {
505 $query = '';
506 if ($foreignKey->hasOption('match')) {
507 $query .= ' MATCH ' . $foreignKey->getOption('match');
508 }
509 $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
510 return $query;
511 }
512
520 public function getDropIndexSQL($index, $table=null)
521 {
522 if($index instanceof \Doctrine\DBAL\Schema\Index) {
523 $index = $index->getQuotedName($this);
524 } else if(!is_string($index)) {
525 throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
526 }
527
528 if($table instanceof \Doctrine\DBAL\Schema\Table) {
529 $table = $table->getQuotedName($this);
530 } else if(!is_string($table)) {
531 throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
532 }
533
534 return 'DROP INDEX ' . $index . ' ON ' . $table;
535 }
536
543 public function getDropTableSQL($table)
544 {
545 if ($table instanceof \Doctrine\DBAL\Schema\Table) {
546 $table = $table->getQuotedName($this);
547 } else if(!is_string($table)) {
548 throw new \InvalidArgumentException('MysqlPlatform::getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
549 }
550
551 return 'DROP TABLE ' . $table;
552 }
553
554 public function getSetTransactionIsolationSQL($level)
555 {
556 return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
557 }
558
564 public function getName()
565 {
566 return 'mysql';
567 }
568
569 public function getReadLockSQL()
570 {
571 return 'LOCK IN SHARE MODE';
572 }
573
575 {
576 $this->doctrineTypeMapping = array(
577 'tinyint' => 'boolean',
578 'smallint' => 'smallint',
579 'mediumint' => 'integer',
580 'int' => 'integer',
581 'integer' => 'integer',
582 'bigint' => 'bigint',
583 'tinytext' => 'text',
584 'mediumtext' => 'text',
585 'longtext' => 'text',
586 'text' => 'text',
587 'varchar' => 'string',
588 'string' => 'string',
589 'char' => 'string',
590 'date' => 'date',
591 'datetime' => 'datetime',
592 'timestamp' => 'datetime',
593 'time' => 'time',
594 'float' => 'float',
595 'double' => 'float',
596 'real' => 'float',
597 'decimal' => 'decimal',
598 'numeric' => 'decimal',
599 'year' => 'date',
600 );
601 }
602}
getUniqueConstraintDeclarationSQL($name, Index $index)
getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
getTimeTypeDeclarationSQL(array $fieldDeclaration)
getLocateExpression($str, $substr, $startPos=false)
_getCommonIntegerTypeDeclarationSQL(array $columnDef)
getListTableForeignKeysSQL($table, $database=null)
getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
getDateTypeDeclarationSQL(array $fieldDeclaration)
_getCreateTableSQL($tableName, array $columns, array $options=array())