SqlserverSchema.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Schema;
  16. /**
  17. * Schema management/reflection features for SQLServer.
  18. */
  19. class SqlserverSchema extends BaseSchema
  20. {
  21. const DEFAULT_SCHEMA_NAME = 'dbo';
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function listTablesSql($config)
  26. {
  27. $sql = "SELECT TABLE_NAME
  28. FROM INFORMATION_SCHEMA.TABLES
  29. WHERE TABLE_SCHEMA = ?
  30. AND (TABLE_TYPE = 'BASE TABLE' OR TABLE_TYPE = 'VIEW')
  31. ORDER BY TABLE_NAME";
  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. {
  40. $sql = 'SELECT DISTINCT
  41. AC.column_id AS [column_id],
  42. AC.name AS [name],
  43. TY.name AS [type],
  44. AC.max_length AS [char_length],
  45. AC.precision AS [precision],
  46. AC.scale AS [scale],
  47. AC.is_identity AS [autoincrement],
  48. AC.is_nullable AS [null],
  49. OBJECT_DEFINITION(AC.default_object_id) AS [default],
  50. AC.collation_name AS [collation_name]
  51. FROM sys.[objects] T
  52. INNER JOIN sys.[schemas] S ON S.[schema_id] = T.[schema_id]
  53. INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
  54. INNER JOIN sys.[types] TY ON TY.[user_type_id] = AC.[user_type_id]
  55. WHERE T.[name] = ? AND S.[name] = ?
  56. ORDER BY column_id';
  57. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  58. return [$sql, [$tableName, $schema]];
  59. }
  60. /**
  61. * Convert a column definition to the abstract types.
  62. *
  63. * The returned type will be a type that
  64. * Cake\Database\Type can handle.
  65. *
  66. * @param string $col The column type
  67. * @param int|null $length the column length
  68. * @param int|null $precision The column precision
  69. * @param int|null $scale The column scale
  70. * @return array Array of column information.
  71. * @link https://technet.microsoft.com/en-us/library/ms187752.aspx
  72. */
  73. protected function _convertColumn($col, $length = null, $precision = null, $scale = null)
  74. {
  75. $col = strtolower($col);
  76. $length = (int)$length;
  77. $precision = (int)$precision;
  78. $scale = (int)$scale;
  79. if (in_array($col, ['date', 'time'])) {
  80. return ['type' => $col, 'length' => null];
  81. }
  82. if (strpos($col, 'datetime') !== false) {
  83. return ['type' => TableSchema::TYPE_TIMESTAMP, 'length' => null];
  84. }
  85. if ($col === 'tinyint') {
  86. return ['type' => TableSchema::TYPE_TINYINTEGER, 'length' => $precision ?: 3];
  87. }
  88. if ($col === 'smallint') {
  89. return ['type' => TableSchema::TYPE_SMALLINTEGER, 'length' => $precision ?: 5];
  90. }
  91. if ($col === 'int' || $col === 'integer') {
  92. return ['type' => TableSchema::TYPE_INTEGER, 'length' => $precision ?: 10];
  93. }
  94. if ($col === 'bigint') {
  95. return ['type' => TableSchema::TYPE_BIGINTEGER, 'length' => $precision ?: 20];
  96. }
  97. if ($col === 'bit') {
  98. return ['type' => TableSchema::TYPE_BOOLEAN, 'length' => null];
  99. }
  100. if (strpos($col, 'numeric') !== false ||
  101. strpos($col, 'money') !== false ||
  102. strpos($col, 'decimal') !== false
  103. ) {
  104. return ['type' => TableSchema::TYPE_DECIMAL, 'length' => $precision, 'precision' => $scale];
  105. }
  106. if ($col === 'real' || $col === 'float') {
  107. return ['type' => TableSchema::TYPE_FLOAT, 'length' => null];
  108. }
  109. // SqlServer schema reflection returns double length for unicode
  110. // columns because internally it uses UTF16/UCS2
  111. if ($col === 'nvarchar' || $col === 'nchar' || $col === 'ntext') {
  112. $length /= 2;
  113. }
  114. if (strpos($col, 'varchar') !== false && $length < 0) {
  115. return ['type' => TableSchema::TYPE_TEXT, 'length' => null];
  116. }
  117. if (strpos($col, 'varchar') !== false) {
  118. return ['type' => TableSchema::TYPE_STRING, 'length' => $length ?: 255];
  119. }
  120. if (strpos($col, 'char') !== false) {
  121. return ['type' => TableSchema::TYPE_STRING, 'fixed' => true, 'length' => $length];
  122. }
  123. if (strpos($col, 'text') !== false) {
  124. return ['type' => TableSchema::TYPE_TEXT, 'length' => null];
  125. }
  126. if ($col === 'image' || strpos($col, 'binary') !== false) {
  127. return ['type' => TableSchema::TYPE_BINARY, 'length' => $length];
  128. }
  129. if ($col === 'uniqueidentifier') {
  130. return ['type' => TableSchema::TYPE_UUID];
  131. }
  132. return ['type' => TableSchema::TYPE_STRING, 'length' => null];
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public function convertColumnDescription(TableSchema $schema, $row)
  138. {
  139. $field = $this->_convertColumn(
  140. $row['type'],
  141. $row['char_length'],
  142. $row['precision'],
  143. $row['scale']
  144. );
  145. if (!empty($row['default'])) {
  146. $row['default'] = trim($row['default'], '()');
  147. }
  148. if (!empty($row['autoincrement'])) {
  149. $field['autoIncrement'] = true;
  150. }
  151. if ($field['type'] === TableSchema::TYPE_BOOLEAN) {
  152. $row['default'] = (int)$row['default'];
  153. }
  154. $field += [
  155. 'null' => $row['null'] === '1',
  156. 'default' => $this->_defaultValue($row['default']),
  157. 'collate' => $row['collation_name'],
  158. ];
  159. $schema->addColumn($row['name'], $field);
  160. }
  161. /**
  162. * Manipulate the default value.
  163. *
  164. * Sqlite includes quotes and bared NULLs in default values.
  165. * We need to remove those.
  166. *
  167. * @param string|null $default The default value.
  168. * @return string|null
  169. */
  170. protected function _defaultValue($default)
  171. {
  172. if ($default === 'NULL') {
  173. return null;
  174. }
  175. // Remove quotes
  176. if (preg_match("/^N?'(.*)'/", $default, $matches)) {
  177. return str_replace("''", "'", $matches[1]);
  178. }
  179. return $default;
  180. }
  181. /**
  182. * {@inheritDoc}
  183. */
  184. public function describeIndexSql($tableName, $config)
  185. {
  186. $sql = "SELECT
  187. I.[name] AS [index_name],
  188. IC.[index_column_id] AS [index_order],
  189. AC.[name] AS [column_name],
  190. I.[is_unique], I.[is_primary_key],
  191. I.[is_unique_constraint]
  192. FROM sys.[tables] AS T
  193. INNER JOIN sys.[schemas] S ON S.[schema_id] = T.[schema_id]
  194. INNER JOIN sys.[indexes] I ON T.[object_id] = I.[object_id]
  195. INNER JOIN sys.[index_columns] IC ON I.[object_id] = IC.[object_id] AND I.[index_id] = IC.[index_id]
  196. INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id] AND IC.[column_id] = AC.[column_id]
  197. WHERE T.[is_ms_shipped] = 0 AND I.[type_desc] <> 'HEAP' AND T.[name] = ? AND S.[name] = ?
  198. ORDER BY I.[index_id], IC.[index_column_id]";
  199. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  200. return [$sql, [$tableName, $schema]];
  201. }
  202. /**
  203. * {@inheritDoc}
  204. */
  205. public function convertIndexDescription(TableSchema $schema, $row)
  206. {
  207. $type = TableSchema::INDEX_INDEX;
  208. $name = $row['index_name'];
  209. if ($row['is_primary_key']) {
  210. $name = $type = TableSchema::CONSTRAINT_PRIMARY;
  211. }
  212. if ($row['is_unique_constraint'] && $type === TableSchema::INDEX_INDEX) {
  213. $type = TableSchema::CONSTRAINT_UNIQUE;
  214. }
  215. if ($type === TableSchema::INDEX_INDEX) {
  216. $existing = $schema->getIndex($name);
  217. } else {
  218. $existing = $schema->getConstraint($name);
  219. }
  220. $columns = [$row['column_name']];
  221. if (!empty($existing)) {
  222. $columns = array_merge($existing['columns'], $columns);
  223. }
  224. if ($type === TableSchema::CONSTRAINT_PRIMARY || $type === TableSchema::CONSTRAINT_UNIQUE) {
  225. $schema->addConstraint($name, [
  226. 'type' => $type,
  227. 'columns' => $columns
  228. ]);
  229. return;
  230. }
  231. $schema->addIndex($name, [
  232. 'type' => $type,
  233. 'columns' => $columns
  234. ]);
  235. }
  236. /**
  237. * {@inheritDoc}
  238. */
  239. public function describeForeignKeySql($tableName, $config)
  240. {
  241. $sql = 'SELECT FK.[name] AS [foreign_key_name], FK.[delete_referential_action_desc] AS [delete_type],
  242. FK.[update_referential_action_desc] AS [update_type], C.name AS [column], RT.name AS [reference_table],
  243. RC.name AS [reference_column]
  244. FROM sys.foreign_keys FK
  245. INNER JOIN sys.foreign_key_columns FKC ON FKC.constraint_object_id = FK.object_id
  246. INNER JOIN sys.tables T ON T.object_id = FKC.parent_object_id
  247. INNER JOIN sys.tables RT ON RT.object_id = FKC.referenced_object_id
  248. INNER JOIN sys.schemas S ON S.schema_id = T.schema_id AND S.schema_id = RT.schema_id
  249. INNER JOIN sys.columns C ON C.column_id = FKC.parent_column_id AND C.object_id = FKC.parent_object_id
  250. INNER JOIN sys.columns RC ON RC.column_id = FKC.referenced_column_id AND RC.object_id = FKC.referenced_object_id
  251. WHERE FK.is_ms_shipped = 0 AND T.name = ? AND S.name = ?';
  252. $schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
  253. return [$sql, [$tableName, $schema]];
  254. }
  255. /**
  256. * {@inheritDoc}
  257. */
  258. public function convertForeignKeyDescription(TableSchema $schema, $row)
  259. {
  260. $data = [
  261. 'type' => TableSchema::CONSTRAINT_FOREIGN,
  262. 'columns' => [$row['column']],
  263. 'references' => [$row['reference_table'], $row['reference_column']],
  264. 'update' => $this->_convertOnClause($row['update_type']),
  265. 'delete' => $this->_convertOnClause($row['delete_type']),
  266. ];
  267. $name = $row['foreign_key_name'];
  268. $schema->addConstraint($name, $data);
  269. }
  270. /**
  271. * {@inheritDoc}
  272. */
  273. protected function _foreignOnClause($on)
  274. {
  275. $parent = parent::_foreignOnClause($on);
  276. return $parent === 'RESTRICT' ? parent::_foreignOnClause(TableSchema::ACTION_SET_NULL) : $parent;
  277. }
  278. /**
  279. * {@inheritDoc}
  280. */
  281. protected function _convertOnClause($clause)
  282. {
  283. switch ($clause) {
  284. case 'NO_ACTION':
  285. return TableSchema::ACTION_NO_ACTION;
  286. case 'CASCADE':
  287. return TableSchema::ACTION_CASCADE;
  288. case 'SET_NULL':
  289. return TableSchema::ACTION_SET_NULL;
  290. case 'SET_DEFAULT':
  291. return TableSchema::ACTION_SET_DEFAULT;
  292. }
  293. return TableSchema::ACTION_SET_NULL;
  294. }
  295. /**
  296. * {@inheritDoc}
  297. */
  298. public function columnSql(TableSchema $schema, $name)
  299. {
  300. $data = $schema->getColumn($name);
  301. $out = $this->_driver->quoteIdentifier($name);
  302. $typeMap = [
  303. TableSchema::TYPE_TINYINTEGER => ' TINYINT',
  304. TableSchema::TYPE_SMALLINTEGER => ' SMALLINT',
  305. TableSchema::TYPE_INTEGER => ' INTEGER',
  306. TableSchema::TYPE_BIGINTEGER => ' BIGINT',
  307. TableSchema::TYPE_BINARY_UUID => ' UNIQUEIDENTIFIER',
  308. TableSchema::TYPE_BOOLEAN => ' BIT',
  309. TableSchema::TYPE_FLOAT => ' FLOAT',
  310. TableSchema::TYPE_DECIMAL => ' DECIMAL',
  311. TableSchema::TYPE_DATE => ' DATE',
  312. TableSchema::TYPE_TIME => ' TIME',
  313. TableSchema::TYPE_DATETIME => ' DATETIME',
  314. TableSchema::TYPE_TIMESTAMP => ' DATETIME',
  315. TableSchema::TYPE_UUID => ' UNIQUEIDENTIFIER',
  316. TableSchema::TYPE_JSON => ' NVARCHAR(MAX)',
  317. ];
  318. if (isset($typeMap[$data['type']])) {
  319. $out .= $typeMap[$data['type']];
  320. }
  321. if ($data['type'] === TableSchema::TYPE_INTEGER || $data['type'] === TableSchema::TYPE_BIGINTEGER) {
  322. if ([$name] === $schema->primaryKey() || $data['autoIncrement'] === true) {
  323. unset($data['null'], $data['default']);
  324. $out .= ' IDENTITY(1, 1)';
  325. }
  326. }
  327. if ($data['type'] === TableSchema::TYPE_TEXT && $data['length'] !== TableSchema::LENGTH_TINY) {
  328. $out .= ' NVARCHAR(MAX)';
  329. }
  330. if ($data['type'] === TableSchema::TYPE_BINARY) {
  331. if (!isset($data['length'])
  332. || in_array($data['length'], [TableSchema::LENGTH_MEDIUM, TableSchema::LENGTH_LONG], true)) {
  333. $data['length'] = 'MAX';
  334. }
  335. if ($data['length'] === 1) {
  336. $out .= ' BINARY(1)';
  337. } else {
  338. $out .= ' VARBINARY';
  339. $out .= sprintf('(%s)', $data['length']);
  340. }
  341. }
  342. if ($data['type'] === TableSchema::TYPE_STRING ||
  343. ($data['type'] === TableSchema::TYPE_TEXT && $data['length'] === TableSchema::LENGTH_TINY)
  344. ) {
  345. $type = ' NVARCHAR';
  346. if (!empty($data['fixed'])) {
  347. $type = ' NCHAR';
  348. }
  349. if (!isset($data['length'])) {
  350. $data['length'] = 255;
  351. }
  352. $out .= sprintf('%s(%d)', $type, $data['length']);
  353. }
  354. $hasCollate = [TableSchema::TYPE_TEXT, TableSchema::TYPE_STRING];
  355. if (in_array($data['type'], $hasCollate, true) && isset($data['collate']) && $data['collate'] !== '') {
  356. $out .= ' COLLATE ' . $data['collate'];
  357. }
  358. if ($data['type'] === TableSchema::TYPE_FLOAT && isset($data['precision'])) {
  359. $out .= '(' . (int)$data['precision'] . ')';
  360. }
  361. if ($data['type'] === TableSchema::TYPE_DECIMAL &&
  362. (isset($data['length']) || isset($data['precision']))
  363. ) {
  364. $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
  365. }
  366. if (isset($data['null']) && $data['null'] === false) {
  367. $out .= ' NOT NULL';
  368. }
  369. if (isset($data['default']) &&
  370. in_array($data['type'], [TableSchema::TYPE_TIMESTAMP, TableSchema::TYPE_DATETIME]) &&
  371. strtolower($data['default']) === 'current_timestamp'
  372. ) {
  373. $out .= ' DEFAULT CURRENT_TIMESTAMP';
  374. } elseif (isset($data['default'])) {
  375. $default = is_bool($data['default']) ? (int)$data['default'] : $this->_driver->schemaValue($data['default']);
  376. $out .= ' DEFAULT ' . $default;
  377. } elseif (isset($data['null']) && $data['null'] !== false) {
  378. $out .= ' DEFAULT NULL';
  379. }
  380. return $out;
  381. }
  382. /**
  383. * {@inheritDoc}
  384. */
  385. public function addConstraintSql(TableSchema $schema)
  386. {
  387. $sqlPattern = 'ALTER TABLE %s ADD %s;';
  388. $sql = [];
  389. foreach ($schema->constraints() as $name) {
  390. $constraint = $schema->getConstraint($name);
  391. if ($constraint['type'] === TableSchema::CONSTRAINT_FOREIGN) {
  392. $tableName = $this->_driver->quoteIdentifier($schema->name());
  393. $sql[] = sprintf($sqlPattern, $tableName, $this->constraintSql($schema, $name));
  394. }
  395. }
  396. return $sql;
  397. }
  398. /**
  399. * {@inheritDoc}
  400. */
  401. public function dropConstraintSql(TableSchema $schema)
  402. {
  403. $sqlPattern = 'ALTER TABLE %s DROP CONSTRAINT %s;';
  404. $sql = [];
  405. foreach ($schema->constraints() as $name) {
  406. $constraint = $schema->getConstraint($name);
  407. if ($constraint['type'] === TableSchema::CONSTRAINT_FOREIGN) {
  408. $tableName = $this->_driver->quoteIdentifier($schema->name());
  409. $constraintName = $this->_driver->quoteIdentifier($name);
  410. $sql[] = sprintf($sqlPattern, $tableName, $constraintName);
  411. }
  412. }
  413. return $sql;
  414. }
  415. /**
  416. * {@inheritDoc}
  417. */
  418. public function indexSql(TableSchema $schema, $name)
  419. {
  420. $data = $schema->getIndex($name);
  421. $columns = array_map(
  422. [$this->_driver, 'quoteIdentifier'],
  423. $data['columns']
  424. );
  425. return sprintf(
  426. 'CREATE INDEX %s ON %s (%s)',
  427. $this->_driver->quoteIdentifier($name),
  428. $this->_driver->quoteIdentifier($schema->name()),
  429. implode(', ', $columns)
  430. );
  431. }
  432. /**
  433. * {@inheritDoc}
  434. */
  435. public function constraintSql(TableSchema $schema, $name)
  436. {
  437. $data = $schema->getConstraint($name);
  438. $out = 'CONSTRAINT ' . $this->_driver->quoteIdentifier($name);
  439. if ($data['type'] === TableSchema::CONSTRAINT_PRIMARY) {
  440. $out = 'PRIMARY KEY';
  441. }
  442. if ($data['type'] === TableSchema::CONSTRAINT_UNIQUE) {
  443. $out .= ' UNIQUE';
  444. }
  445. return $this->_keySql($out, $data);
  446. }
  447. /**
  448. * Helper method for generating key SQL snippets.
  449. *
  450. * @param string $prefix The key prefix
  451. * @param array $data Key data.
  452. * @return string
  453. */
  454. protected function _keySql($prefix, $data)
  455. {
  456. $columns = array_map(
  457. [$this->_driver, 'quoteIdentifier'],
  458. $data['columns']
  459. );
  460. if ($data['type'] === TableSchema::CONSTRAINT_FOREIGN) {
  461. return $prefix . sprintf(
  462. ' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
  463. implode(', ', $columns),
  464. $this->_driver->quoteIdentifier($data['references'][0]),
  465. $this->_convertConstraintColumns($data['references'][1]),
  466. $this->_foreignOnClause($data['update']),
  467. $this->_foreignOnClause($data['delete'])
  468. );
  469. }
  470. return $prefix . ' (' . implode(', ', $columns) . ')';
  471. }
  472. /**
  473. * {@inheritDoc}
  474. */
  475. public function createTableSql(TableSchema $schema, $columns, $constraints, $indexes)
  476. {
  477. $content = array_merge($columns, $constraints);
  478. $content = implode(",\n", array_filter($content));
  479. $tableName = $this->_driver->quoteIdentifier($schema->name());
  480. $out = [];
  481. $out[] = sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content);
  482. foreach ($indexes as $index) {
  483. $out[] = $index;
  484. }
  485. return $out;
  486. }
  487. /**
  488. * {@inheritDoc}
  489. */
  490. public function truncateTableSql(TableSchema $schema)
  491. {
  492. $name = $this->_driver->quoteIdentifier($schema->name());
  493. $queries = [
  494. sprintf('DELETE FROM %s', $name)
  495. ];
  496. // Restart identity sequences
  497. $pk = $schema->primaryKey();
  498. if (count($pk) === 1) {
  499. $column = $schema->getColumn($pk[0]);
  500. if (in_array($column['type'], ['integer', 'biginteger'])) {
  501. $queries[] = sprintf(
  502. "DBCC CHECKIDENT('%s', RESEED, 0)",
  503. $schema->name()
  504. );
  505. }
  506. }
  507. return $queries;
  508. }
  509. }