MySqlSchemaManager.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Platforms\MySqlPlatform;
  21. use Doctrine\DBAL\Types\Type;
  22. /**
  23. * Schema manager for the MySql RDBMS.
  24. *
  25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  26. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  27. * @author Roman Borschel <roman@code-factory.org>
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @since 2.0
  30. */
  31. class MySqlSchemaManager extends AbstractSchemaManager
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function _getPortableViewDefinition($view)
  37. {
  38. return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function _getPortableTableDefinition($table)
  44. {
  45. return array_shift($table);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function _getPortableUserDefinition($user)
  51. {
  52. return array(
  53. 'user' => $user['User'],
  54. 'password' => $user['Password'],
  55. );
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  61. {
  62. foreach ($tableIndexes as $k => $v) {
  63. $v = array_change_key_case($v, CASE_LOWER);
  64. if ($v['key_name'] == 'PRIMARY') {
  65. $v['primary'] = true;
  66. } else {
  67. $v['primary'] = false;
  68. }
  69. if (strpos($v['index_type'], 'FULLTEXT') !== false) {
  70. $v['flags'] = array('FULLTEXT');
  71. } elseif (strpos($v['index_type'], 'SPATIAL') !== false) {
  72. $v['flags'] = array('SPATIAL');
  73. }
  74. $tableIndexes[$k] = $v;
  75. }
  76. return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function _getPortableSequenceDefinition($sequence)
  82. {
  83. return end($sequence);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function _getPortableDatabaseDefinition($database)
  89. {
  90. return $database['Database'];
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function _getPortableTableColumnDefinition($tableColumn)
  96. {
  97. $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
  98. $dbType = strtolower($tableColumn['type']);
  99. $dbType = strtok($dbType, '(), ');
  100. if (isset($tableColumn['length'])) {
  101. $length = $tableColumn['length'];
  102. } else {
  103. $length = strtok('(), ');
  104. }
  105. $fixed = null;
  106. if ( ! isset($tableColumn['name'])) {
  107. $tableColumn['name'] = '';
  108. }
  109. $scale = null;
  110. $precision = null;
  111. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  112. // In cases where not connected to a database DESCRIBE $table does not return 'Comment'
  113. if (isset($tableColumn['comment'])) {
  114. $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
  115. $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
  116. }
  117. switch ($dbType) {
  118. case 'char':
  119. case 'binary':
  120. $fixed = true;
  121. break;
  122. case 'float':
  123. case 'double':
  124. case 'real':
  125. case 'numeric':
  126. case 'decimal':
  127. if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
  128. $precision = $match[1];
  129. $scale = $match[2];
  130. $length = null;
  131. }
  132. break;
  133. case 'tinytext':
  134. $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT;
  135. break;
  136. case 'text':
  137. $length = MySqlPlatform::LENGTH_LIMIT_TEXT;
  138. break;
  139. case 'mediumtext':
  140. $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT;
  141. break;
  142. case 'tinyblob':
  143. $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB;
  144. break;
  145. case 'blob':
  146. $length = MySqlPlatform::LENGTH_LIMIT_BLOB;
  147. break;
  148. case 'mediumblob':
  149. $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB;
  150. break;
  151. case 'tinyint':
  152. case 'smallint':
  153. case 'mediumint':
  154. case 'int':
  155. case 'integer':
  156. case 'bigint':
  157. case 'year':
  158. $length = null;
  159. break;
  160. }
  161. $length = ((int) $length == 0) ? null : (int) $length;
  162. $options = array(
  163. 'length' => $length,
  164. 'unsigned' => (bool) (strpos($tableColumn['type'], 'unsigned') !== false),
  165. 'fixed' => (bool) $fixed,
  166. 'default' => isset($tableColumn['default']) ? $tableColumn['default'] : null,
  167. 'notnull' => (bool) ($tableColumn['null'] != 'YES'),
  168. 'scale' => null,
  169. 'precision' => null,
  170. 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
  171. 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
  172. ? $tableColumn['comment']
  173. : null,
  174. );
  175. if ($scale !== null && $precision !== null) {
  176. $options['scale'] = $scale;
  177. $options['precision'] = $precision;
  178. }
  179. $column = new Column($tableColumn['field'], Type::getType($type), $options);
  180. if (isset($tableColumn['collation'])) {
  181. $column->setPlatformOption('collation', $tableColumn['collation']);
  182. }
  183. return $column;
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  189. {
  190. $list = array();
  191. foreach ($tableForeignKeys as $value) {
  192. $value = array_change_key_case($value, CASE_LOWER);
  193. if (!isset($list[$value['constraint_name']])) {
  194. if (!isset($value['delete_rule']) || $value['delete_rule'] == "RESTRICT") {
  195. $value['delete_rule'] = null;
  196. }
  197. if (!isset($value['update_rule']) || $value['update_rule'] == "RESTRICT") {
  198. $value['update_rule'] = null;
  199. }
  200. $list[$value['constraint_name']] = array(
  201. 'name' => $value['constraint_name'],
  202. 'local' => array(),
  203. 'foreign' => array(),
  204. 'foreignTable' => $value['referenced_table_name'],
  205. 'onDelete' => $value['delete_rule'],
  206. 'onUpdate' => $value['update_rule'],
  207. );
  208. }
  209. $list[$value['constraint_name']]['local'][] = $value['column_name'];
  210. $list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name'];
  211. }
  212. $result = array();
  213. foreach ($list as $constraint) {
  214. $result[] = new ForeignKeyConstraint(
  215. array_values($constraint['local']), $constraint['foreignTable'],
  216. array_values($constraint['foreign']), $constraint['name'],
  217. array(
  218. 'onDelete' => $constraint['onDelete'],
  219. 'onUpdate' => $constraint['onUpdate'],
  220. )
  221. );
  222. }
  223. return $result;
  224. }
  225. }