SqlserverSchemaDialect.php 23 KB

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