SqlserverSchema.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Schema;
  16. use Cake\Database\Schema\Table;
  17. /**
  18. * Schema management/reflection features for SQLServer.
  19. */
  20. class SqlserverSchema extends BaseSchema {
  21. const DEFAULT_SCHEMA_NAME = 'dbo';
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function listTablesSql($config) {
  26. $sql = '
  27. SELECT TABLE_NAME
  28. FROM INFORMATION_SCHEMA.TABLES
  29. WHERE TABLE_SCHEMA = ?
  30. ORDER BY TABLE_NAME
  31. ';
  32. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  33. return [$sql, [$schema]];
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function describeColumnSql($tableName, $config) {
  39. $sql =
  40. "SELECT DISTINCT TABLE_SCHEMA AS [schema], COLUMN_NAME AS [name], DATA_TYPE AS [type],
  41. IS_NULLABLE AS [null], COLUMN_DEFAULT AS [default],
  42. CHARACTER_MAXIMUM_LENGTH AS [char_length],
  43. NUMERIC_PRECISION AS [precision],
  44. NUMERIC_SCALE AS [scale],
  45. '' AS [comment], ORDINAL_POSITION AS [ordinal_position]
  46. FROM INFORMATION_SCHEMA.COLUMNS
  47. WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?
  48. ORDER BY ordinal_position";
  49. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  50. return [$sql, [$tableName, $schema]];
  51. }
  52. /**
  53. * Convert a column definition to the abstract types.
  54. *
  55. * The returned type will be a type that
  56. * Cake\Database\Type can handle.
  57. *
  58. * @param string $col The column type
  59. * @param int|null $length the column length
  60. * @param int|null $precision The column precision
  61. * @param int|null $scale The column scale
  62. * @return array Array of column information.
  63. * @link http://technet.microsoft.com/en-us/library/ms187752.aspx
  64. */
  65. protected function _convertColumn($col, $length = null, $precision = null, $scale = null) {
  66. $col = strtolower($col);
  67. if (in_array($col, array('date', 'time'))) {
  68. return ['type' => $col, 'length' => null];
  69. }
  70. if (strpos($col, 'datetime') !== false) {
  71. return ['type' => 'timestamp', 'length' => null];
  72. }
  73. if ($col === 'int' || $col === 'integer') {
  74. return ['type' => 'integer', 'length' => $precision ?: 10];
  75. }
  76. if ($col === 'bigint') {
  77. return ['type' => 'biginteger', 'length' => $precision ?: 20];
  78. }
  79. if ($col === 'smallint') {
  80. return ['type' => 'integer', 'length' => $precision ?: 5];
  81. }
  82. if ($col === 'tinyint') {
  83. return ['type' => 'integer', 'length' => $precision ?: 3];
  84. }
  85. if ($col === 'bit') {
  86. return ['type' => 'boolean', 'length' => null];
  87. }
  88. if (
  89. strpos($col, 'numeric') !== false ||
  90. strpos($col, 'money') !== false ||
  91. strpos($col, 'decimal') !== false
  92. ) {
  93. return ['type' => 'decimal', 'length' => $precision, 'precision' => $scale];
  94. }
  95. if ($col === 'real' || $col === 'float') {
  96. return ['type' => 'float', 'length' => null];
  97. }
  98. if (strpos($col, 'varchar') !== false && $length < 0) {
  99. return ['type' => 'text', 'length' => null];
  100. }
  101. if (strpos($col, 'varchar') !== false) {
  102. return ['type' => 'string', 'length' => $length ?: 255];
  103. }
  104. if (strpos($col, 'char') !== false) {
  105. return ['type' => 'string', 'fixed' => true, 'length' => $length];
  106. }
  107. if (strpos($col, 'text') !== false) {
  108. return ['type' => 'text', 'length' => null];
  109. }
  110. if ($col === 'image' || strpos($col, 'binary')) {
  111. return ['type' => 'binary', 'length' => null];
  112. }
  113. if ($col === 'uniqueidentifier') {
  114. return ['type' => 'uuid'];
  115. }
  116. return ['type' => 'text', 'length' => null];
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function convertColumnDescription(Table $table, $row) {
  122. $field = $this->_convertColumn(
  123. $row['type'],
  124. $row['char_length'],
  125. $row['precision'],
  126. $row['scale']
  127. );
  128. if (!empty($row['default'])) {
  129. $row['default'] = trim($row['default'], '()');
  130. }
  131. if ($field['type'] === 'boolean') {
  132. $row['default'] = (int)$row['default'];
  133. }
  134. $field += [
  135. 'null' => $row['null'] === 'YES' ? true : false,
  136. 'default' => $row['default'],
  137. ];
  138. $table->addColumn($row['name'], $field);
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function describeIndexSql($tableName, $config) {
  144. $sql = "
  145. SELECT
  146. I.[name] AS [index_name],
  147. IC.[index_column_id] AS [index_order],
  148. AC.[name] AS [column_name],
  149. I.[is_unique], I.[is_primary_key],
  150. I.[is_unique_constraint]
  151. FROM sys.[tables] AS T
  152. INNER JOIN sys.[schemas] S ON S.[schema_id] = T.[schema_id]
  153. INNER JOIN sys.[indexes] I ON T.[object_id] = I.[object_id]
  154. INNER JOIN sys.[index_columns] IC ON I.[object_id] = IC.[object_id] AND I.[index_id] = IC.[index_id]
  155. INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id] AND IC.[column_id] = AC.[column_id]
  156. WHERE T.[is_ms_shipped] = 0 AND I.[type_desc] <> 'HEAP' AND T.[name] = ? AND S.[name] = ?
  157. ORDER BY I.[index_id], IC.[index_column_id]
  158. ";
  159. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  160. return [$sql, [$tableName, $schema]];
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public function convertIndexDescription(Table $table, $row) {
  166. $type = Table::INDEX_INDEX;
  167. $name = $row['index_name'];
  168. if ($row['is_primary_key']) {
  169. $name = $type = Table::CONSTRAINT_PRIMARY;
  170. }
  171. if ($row['is_unique_constraint'] && $type === Table::INDEX_INDEX) {
  172. $type = Table::CONSTRAINT_UNIQUE;
  173. }
  174. if ($type === Table::INDEX_INDEX) {
  175. $existing = $table->index($name);
  176. } else {
  177. $existing = $table->constraint($name);
  178. }
  179. $columns = [$row['column_name']];
  180. if (!empty($existing)) {
  181. $columns = array_merge($existing['columns'], $columns);
  182. }
  183. if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) {
  184. $table->addConstraint($name, [
  185. 'type' => $type,
  186. 'columns' => $columns
  187. ]);
  188. return;
  189. }
  190. $table->addIndex($name, [
  191. 'type' => $type,
  192. 'columns' => $columns
  193. ]);
  194. }
  195. /**
  196. * {@inheritDoc}
  197. */
  198. public function describeForeignKeySql($tableName, $config) {
  199. $sql = "
  200. SELECT FK.[name] AS [foreign_key_name], FK.[delete_referential_action_desc] AS [delete_type],
  201. FK.[update_referential_action_desc] AS [update_type], C.name AS [column], RT.name AS [reference_table],
  202. RC.name AS [reference_column]
  203. FROM sys.foreign_keys FK
  204. INNER JOIN sys.foreign_key_columns FKC ON FKC.constraint_object_id = FK.object_id
  205. INNER JOIN sys.tables T ON T.object_id = FKC.parent_object_id
  206. INNER JOIN sys.tables RT ON RT.object_id = FKC.referenced_object_id
  207. INNER JOIN sys.schemas S ON S.schema_id = T.schema_id AND S.schema_id = RT.schema_id
  208. INNER JOIN sys.columns C ON C.column_id = FKC.parent_column_id AND C.object_id = FKC.parent_object_id
  209. INNER JOIN sys.columns RC ON RC.column_id = FKC.referenced_column_id AND RC.object_id = FKC.referenced_object_id
  210. WHERE FK.is_ms_shipped = 0 AND T.name = ? AND S.name = ?
  211. ";
  212. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  213. return [$sql, [$tableName, $schema]];
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public function convertForeignKeyDescription(Table $table, $row) {
  219. $data = [
  220. 'type' => Table::CONSTRAINT_FOREIGN,
  221. 'columns' => [$row['column']],
  222. 'references' => [$row['reference_table'], $row['reference_column']],
  223. 'update' => $this->_convertOnClause($row['update_type']),
  224. 'delete' => $this->_convertOnClause($row['delete_type']),
  225. ];
  226. $name = $row['foreign_key_name'];
  227. $table->addConstraint($name, $data);
  228. }
  229. /**
  230. * {@inheritDoc}
  231. */
  232. protected function _foreignOnClause($on) {
  233. $parent = parent::_foreignOnClause($on);
  234. return $parent === 'RESTRICT' ? parent::_foreignOnClause(Table::ACTION_SET_NULL) : $parent;
  235. }
  236. /**
  237. * {@inheritDoc}
  238. */
  239. protected function _convertOnClause($clause) {
  240. switch ($clause) {
  241. case 'NO_ACTION':
  242. return Table::ACTION_NO_ACTION;
  243. case 'CASCADE':
  244. return Table::ACTION_CASCADE;
  245. case 'SET_NULL':
  246. return Table::ACTION_SET_NULL;
  247. case 'SET_DEFAULT':
  248. return Table::ACTION_SET_DEFAULT;
  249. }
  250. return Table::ACTION_SET_NULL;
  251. }
  252. /**
  253. * {@inheritDoc}
  254. */
  255. public function columnSql(Table $table, $name) {
  256. $data = $table->column($name);
  257. $out = $this->_driver->quoteIdentifier($name);
  258. $typeMap = [
  259. 'integer' => ' INTEGER',
  260. 'biginteger' => ' BIGINT',
  261. 'boolean' => ' BIT',
  262. 'binary' => ' VARBINARY(MAX)',
  263. 'float' => ' FLOAT',
  264. 'decimal' => ' DECIMAL',
  265. 'text' => ' NVARCHAR(MAX)',
  266. 'date' => ' DATE',
  267. 'time' => ' TIME',
  268. 'datetime' => ' DATETIME',
  269. 'timestamp' => ' DATETIME',
  270. 'uuid' => ' UNIQUEIDENTIFIER'
  271. ];
  272. if (isset($typeMap[$data['type']])) {
  273. $out .= $typeMap[$data['type']];
  274. }
  275. if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
  276. if ([$name] === $table->primaryKey() || $data['autoIncrement'] === true) {
  277. unset($data['null'], $data['default']);
  278. $out .= ' IDENTITY(1, 1)';
  279. }
  280. }
  281. if ($data['type'] === 'string') {
  282. $type = ' NVARCHAR';
  283. if (!empty($data['fixed'])) {
  284. $type = ' NCHAR';
  285. }
  286. if (!isset($data['length'])) {
  287. $data['length'] = 255;
  288. }
  289. $out .= sprintf('%s(%d)', $type, $data['length']);
  290. }
  291. if ($data['type'] === 'float' && isset($data['precision'])) {
  292. $out .= '(' . (int)$data['precision'] . ')';
  293. }
  294. if ($data['type'] === 'decimal' &&
  295. (isset($data['length']) || isset($data['precision']))
  296. ) {
  297. $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
  298. }
  299. if (isset($data['null']) && $data['null'] === false) {
  300. $out .= ' NOT NULL';
  301. }
  302. if (isset($data['null']) && $data['null'] === true) {
  303. $out .= ' DEFAULT NULL';
  304. unset($data['default']);
  305. }
  306. if (isset($data['default']) && $data['type'] !== 'datetime') {
  307. $default = is_bool($data['default']) ? (int)$data['default'] : $this->_driver->schemaValue($data['default']);
  308. $out .= ' DEFAULT ' . $default;
  309. }
  310. return $out;
  311. }
  312. /**
  313. * {@inheritDoc}
  314. */
  315. public function indexSql(Table $table, $name) {
  316. $data = $table->index($name);
  317. $columns = array_map(
  318. [$this->_driver, 'quoteIdentifier'],
  319. $data['columns']
  320. );
  321. return sprintf('CREATE INDEX %s ON %s (%s)',
  322. $this->_driver->quoteIdentifier($name),
  323. $this->_driver->quoteIdentifier($table->name()),
  324. implode(', ', $columns)
  325. );
  326. }
  327. /**
  328. * {@inheritDoc}
  329. */
  330. public function constraintSql(Table $table, $name) {
  331. $data = $table->constraint($name);
  332. $out = 'CONSTRAINT ' . $this->_driver->quoteIdentifier($name);
  333. if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
  334. $out = 'PRIMARY KEY';
  335. }
  336. if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
  337. $out .= ' UNIQUE';
  338. }
  339. return $this->_keySql($out, $data);
  340. }
  341. /**
  342. * Helper method for generating key SQL snippets.
  343. *
  344. * @param string $prefix The key prefix
  345. * @param array $data Key data.
  346. * @return string
  347. */
  348. protected function _keySql($prefix, $data) {
  349. $columns = array_map(
  350. [$this->_driver, 'quoteIdentifier'],
  351. $data['columns']
  352. );
  353. if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
  354. return $prefix . sprintf(
  355. ' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
  356. implode(', ', $columns),
  357. $this->_driver->quoteIdentifier($data['references'][0]),
  358. $this->_driver->quoteIdentifier($data['references'][1]),
  359. $this->_foreignOnClause($data['update']),
  360. $this->_foreignOnClause($data['delete'])
  361. );
  362. }
  363. return $prefix . ' (' . implode(', ', $columns) . ')';
  364. }
  365. /**
  366. * {@inheritDoc}
  367. */
  368. public function createTableSql(Table $table, $columns, $constraints, $indexes) {
  369. $content = array_merge($columns, $constraints);
  370. $content = implode(",\n", array_filter($content));
  371. $tableName = $this->_driver->quoteIdentifier($table->name());
  372. $out = [];
  373. $out[] = sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content);
  374. foreach ($indexes as $index) {
  375. $out[] = $index;
  376. }
  377. return $out;
  378. }
  379. /**
  380. * {@inheritDoc}
  381. */
  382. public function truncateTableSql(Table $table) {
  383. $name = $this->_driver->quoteIdentifier($table->name());
  384. $queries = [
  385. sprintf('DELETE FROM %s', $name)
  386. ];
  387. // Restart identity sequences
  388. $pk = $table->primaryKey();
  389. if (count($pk) === 1) {
  390. $column = $table->column($pk[0]);
  391. if (in_array($column['type'], ['integer', 'biginteger'])) {
  392. $queries[] = sprintf(
  393. 'DBCC CHECKIDENT(%s, RESEED, 0)',
  394. $name
  395. );
  396. }
  397. }
  398. return $queries;
  399. }
  400. }