MySqlPlatform.php 25 KB

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