MysqlSchema.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Schema;
  18. use Cake\Database\Exception;
  19. use Cake\Database\Schema\Table;
  20. /**
  21. * Schema generation/reflection features for MySQL
  22. *
  23. */
  24. class MysqlSchema extends BaseSchema {
  25. /**
  26. * {@inheritdoc}
  27. *
  28. */
  29. public function listTablesSql($config) {
  30. return ['SHOW TABLES FROM ' . $this->_driver->quoteIdentifier($config['database']), []];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. *
  35. */
  36. public function describeTableSql($name, $config) {
  37. return ['SHOW FULL COLUMNS FROM ' . $this->_driver->quoteIdentifier($name), []];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. */
  43. public function describeIndexSql($table, $config) {
  44. return ['SHOW INDEXES FROM ' . $this->_driver->quoteIdentifier($table), []];
  45. }
  46. /**
  47. * Convert a MySQL column type into an abstract type.
  48. *
  49. * The returned type will be a type that Cake\Database\Type can handle.
  50. *
  51. * @param string $column The column type + length
  52. * @return array Array of column information.
  53. * @throws Cake\Database\Exception When column type cannot be parsed.
  54. */
  55. protected function _convertColumn($column) {
  56. preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
  57. if (empty($matches)) {
  58. throw new Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
  59. }
  60. $col = strtolower($matches[1]);
  61. $length = $precision = null;
  62. if (isset($matches[2])) {
  63. $length = $matches[2];
  64. if (strpos($matches[2], ',') !== false) {
  65. list($length, $precision) = explode(',', $length);
  66. }
  67. $length = (int)$length;
  68. $precision = (int)$precision;
  69. }
  70. if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
  71. return ['type' => $col, 'length' => null];
  72. }
  73. if (($col === 'tinyint' && $length === 1) || $col === 'boolean') {
  74. return ['type' => 'boolean', 'length' => null];
  75. }
  76. if (strpos($col, 'bigint') !== false || $col === 'bigint') {
  77. return ['type' => 'biginteger', 'length' => $length];
  78. }
  79. if (strpos($col, 'int') !== false) {
  80. return ['type' => 'integer', 'length' => $length];
  81. }
  82. if ($col === 'char') {
  83. return ['type' => 'string', 'fixed' => true, 'length' => $length];
  84. }
  85. if (strpos($col, 'char') !== false || $col === 'tinytext') {
  86. return ['type' => 'string', 'length' => $length];
  87. }
  88. if (strpos($col, 'text') !== false) {
  89. return ['type' => 'text', 'length' => $length];
  90. }
  91. if (strpos($col, 'blob') !== false || $col === 'binary') {
  92. return ['type' => 'binary', 'length' => $length];
  93. }
  94. if (strpos($col, 'float') !== false || strpos($col, 'double') !== false) {
  95. return ['type' => 'float', 'length' => $length, 'precision' => $precision];
  96. }
  97. if (strpos($col, 'decimal') !== false) {
  98. return ['type' => 'decimal', 'length' => $length, 'precision' => $precision];
  99. }
  100. return ['type' => 'text', 'length' => null];
  101. }
  102. /**
  103. * {@inheritdoc}
  104. *
  105. */
  106. public function convertFieldDescription(Table $table, $row) {
  107. $field = $this->_convertColumn($row['Type']);
  108. $field += [
  109. 'null' => $row['Null'] === 'YES' ? true : false,
  110. 'default' => $row['Default'],
  111. 'collate' => $row['Collation'],
  112. 'comment' => $row['Comment'],
  113. ];
  114. $table->addColumn($row['Field'], $field);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. */
  120. public function convertIndexDescription(Table $table, $row) {
  121. $type = null;
  122. $columns = $length = [];
  123. $name = $row['Key_name'];
  124. if ($name === 'PRIMARY') {
  125. $name = $type = Table::CONSTRAINT_PRIMARY;
  126. }
  127. $columns[] = $row['Column_name'];
  128. if ($row['Index_type'] === 'FULLTEXT') {
  129. $type = Table::INDEX_FULLTEXT;
  130. } elseif ($row['Non_unique'] == 0 && $type !== 'primary') {
  131. $type = Table::CONSTRAINT_UNIQUE;
  132. } elseif ($type !== 'primary') {
  133. $type = Table::INDEX_INDEX;
  134. }
  135. if (!empty($row['Sub_part'])) {
  136. $length[$row['Column_name']] = $row['Sub_part'];
  137. }
  138. $isIndex = (
  139. $type === Table::INDEX_INDEX ||
  140. $type === Table::INDEX_FULLTEXT
  141. );
  142. if ($isIndex) {
  143. $existing = $table->index($name);
  144. } else {
  145. $existing = $table->constraint($name);
  146. }
  147. // MySQL multi column indexes come back as multiple rows.
  148. if (!empty($existing)) {
  149. $columns = array_merge($existing['columns'], $columns);
  150. $length = array_merge($existing['length'], $length);
  151. }
  152. if ($isIndex) {
  153. $table->addIndex($name, [
  154. 'type' => $type,
  155. 'columns' => $columns,
  156. 'length' => $length
  157. ]);
  158. } else {
  159. $table->addConstraint($name, [
  160. 'type' => $type,
  161. 'columns' => $columns,
  162. 'length' => $length
  163. ]);
  164. }
  165. }
  166. /**
  167. * {@inheritdoc}
  168. *
  169. */
  170. public function describeForeignKeySql($table, $config) {
  171. $sql = 'SELECT * FROM information_schema.key_column_usage AS kcu
  172. INNER JOIN information_schema.referential_constraints AS rc
  173. ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME)
  174. WHERE kcu.TABLE_SCHEMA = ? AND kcu.TABLE_NAME = ?';
  175. return [$sql, [$config['database'], $table]];
  176. }
  177. /**
  178. * {@inheritdoc}
  179. *
  180. */
  181. public function convertForeignKeyDescription(Table $table, $row) {
  182. $data = [
  183. 'type' => Table::CONSTRAINT_FOREIGN,
  184. 'columns' => [$row['COLUMN_NAME']],
  185. 'references' => [$row['REFERENCED_TABLE_NAME'], $row['REFERENCED_COLUMN_NAME']],
  186. 'update' => $this->_convertOnClause($row['UPDATE_RULE']),
  187. 'delete' => $this->_convertOnClause($row['DELETE_RULE']),
  188. ];
  189. $name = $row['CONSTRAINT_NAME'];
  190. $table->addConstraint($name, $data);
  191. }
  192. /**
  193. * {@inheritdoc}
  194. *
  195. */
  196. public function truncateTableSql(Table $table) {
  197. return [sprintf('TRUNCATE TABLE `%s`', $table->name())];
  198. }
  199. /**
  200. * {@inheritdoc}
  201. *
  202. */
  203. public function createTableSql(Table $table, $columns, $constraints, $indexes) {
  204. $content = implode(",\n", array_merge($columns, $constraints, $indexes));
  205. $content = sprintf("CREATE TABLE `%s` (\n%s\n)", $table->name(), $content);
  206. $options = $table->options();
  207. if (isset($options['engine'])) {
  208. $content .= sprintf(' ENGINE=%s', $options['engine']);
  209. }
  210. if (isset($options['charset'])) {
  211. $content .= sprintf(' DEFAULT CHARSET=%s', $options['charset']);
  212. }
  213. if (isset($options['collate'])) {
  214. $content .= sprintf(' COLLATE=%s', $options['collate']);
  215. }
  216. return [$content];
  217. }
  218. /**
  219. * {@inheritdoc}
  220. *
  221. */
  222. public function columnSql(Table $table, $name) {
  223. $data = $table->column($name);
  224. $out = $this->_driver->quoteIdentifier($name);
  225. $typeMap = [
  226. 'integer' => ' INTEGER',
  227. 'biginteger' => ' BIGINT',
  228. 'boolean' => ' BOOLEAN',
  229. 'binary' => ' BLOB',
  230. 'float' => ' FLOAT',
  231. 'decimal' => ' DECIMAL',
  232. 'text' => ' TEXT',
  233. 'date' => ' DATE',
  234. 'time' => ' TIME',
  235. 'datetime' => ' DATETIME',
  236. 'timestamp' => ' TIMESTAMP',
  237. ];
  238. $specialMap = [
  239. 'string' => true,
  240. ];
  241. if (isset($typeMap[$data['type']])) {
  242. $out .= $typeMap[$data['type']];
  243. }
  244. if (isset($specialMap[$data['type']])) {
  245. switch ($data['type']) {
  246. case 'string':
  247. $out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
  248. if (!isset($data['length'])) {
  249. $data['length'] = 255;
  250. }
  251. break;
  252. }
  253. }
  254. $hasLength = ['integer', 'string'];
  255. if (in_array($data['type'], $hasLength, true) && isset($data['length'])) {
  256. $out .= '(' . (int)$data['length'] . ')';
  257. }
  258. $hasPrecision = ['float', 'decimal'];
  259. if (
  260. in_array($data['type'], $hasPrecision, true) &&
  261. (isset($data['length']) || isset($data['precision']))
  262. ) {
  263. $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
  264. }
  265. if (isset($data['null']) && $data['null'] === false) {
  266. $out .= ' NOT NULL';
  267. }
  268. if (in_array($data['type'], ['integer', 'biginteger']) && in_array($name, (array)$table->primaryKey())) {
  269. $out .= ' AUTO_INCREMENT';
  270. }
  271. if (isset($data['null']) && $data['null'] === true) {
  272. $out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
  273. unset($data['default']);
  274. }
  275. if (isset($data['default']) && $data['type'] !== 'timestamp') {
  276. $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
  277. }
  278. if (
  279. isset($data['default']) &&
  280. $data['type'] === 'timestamp' &&
  281. strtolower($data['default']) === 'current_timestamp'
  282. ) {
  283. $out .= ' DEFAULT CURRENT_TIMESTAMP';
  284. }
  285. if (isset($data['comment'])) {
  286. $out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
  287. }
  288. return $out;
  289. }
  290. /**
  291. * {@inheritdoc}
  292. *
  293. */
  294. public function constraintSql(Table $table, $name) {
  295. $data = $table->constraint($name);
  296. if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
  297. $columns = array_map(
  298. [$this->_driver, 'quoteIdentifier'],
  299. $data['columns']
  300. );
  301. return sprintf('PRIMARY KEY (%s)', implode(', ', $columns));
  302. }
  303. if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
  304. $out = 'UNIQUE KEY ';
  305. }
  306. if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
  307. $out = 'CONSTRAINT ';
  308. }
  309. $out .= $this->_driver->quoteIdentifier($name);
  310. return $this->_keySql($out, $data);
  311. }
  312. /**
  313. * {@inheritdoc}
  314. *
  315. */
  316. public function indexSql(Table $table, $name) {
  317. $data = $table->index($name);
  318. if ($data['type'] === Table::INDEX_INDEX) {
  319. $out = 'KEY ';
  320. }
  321. if ($data['type'] === Table::INDEX_FULLTEXT) {
  322. $out = 'FULLTEXT KEY ';
  323. }
  324. $out .= $this->_driver->quoteIdentifier($name);
  325. return $this->_keySql($out, $data);
  326. }
  327. /**
  328. * Helper method for generating key SQL snippets.
  329. *
  330. * @param string $prefix The key prefix
  331. * @param array $data Key data.
  332. * @return string
  333. */
  334. protected function _keySql($prefix, $data) {
  335. $columns = array_map(
  336. [$this->_driver, 'quoteIdentifier'],
  337. $data['columns']
  338. );
  339. foreach ($data['columns'] as $i => $column) {
  340. if (isset($data['length'][$column])) {
  341. $columns[$i] .= sprintf('(%d)', $data['length'][$column]);
  342. }
  343. }
  344. if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
  345. return $prefix . sprintf(
  346. ' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
  347. implode(', ', $columns),
  348. $this->_driver->quoteIdentifier($data['references'][0]),
  349. $this->_driver->quoteIdentifier($data['references'][1]),
  350. $this->_foreignOnClause($data['update']),
  351. $this->_foreignOnClause($data['delete'])
  352. );
  353. }
  354. return $prefix . ' (' . implode(', ', $columns) . ')';
  355. }
  356. }