SqlServerGrammar.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. <?php
  2. namespace Illuminate\Database\Schema\Grammars;
  3. use Illuminate\Support\Fluent;
  4. use Illuminate\Database\Schema\Blueprint;
  5. class SqlServerGrammar extends Grammar
  6. {
  7. /**
  8. * If this Grammar supports schema changes wrapped in a transaction.
  9. *
  10. * @var bool
  11. */
  12. protected $transactions = true;
  13. /**
  14. * The possible column modifiers.
  15. *
  16. * @var array
  17. */
  18. protected $modifiers = ['Increment', 'Collate', 'Nullable', 'Default'];
  19. /**
  20. * The columns available as serials.
  21. *
  22. * @var array
  23. */
  24. protected $serials = ['tinyInteger', 'smallInteger', 'mediumInteger', 'integer', 'bigInteger'];
  25. /**
  26. * Compile the query to determine if a table exists.
  27. *
  28. * @return string
  29. */
  30. public function compileTableExists()
  31. {
  32. return "select * from sysobjects where type = 'U' and name = ?";
  33. }
  34. /**
  35. * Compile the query to determine the list of columns.
  36. *
  37. * @param string $table
  38. * @return string
  39. */
  40. public function compileColumnListing($table)
  41. {
  42. return "select col.name from sys.columns as col
  43. join sys.objects as obj on col.object_id = obj.object_id
  44. where obj.type = 'U' and obj.name = '$table'";
  45. }
  46. /**
  47. * Compile a create table command.
  48. *
  49. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  50. * @param \Illuminate\Support\Fluent $command
  51. * @return string
  52. */
  53. public function compileCreate(Blueprint $blueprint, Fluent $command)
  54. {
  55. $columns = implode(', ', $this->getColumns($blueprint));
  56. return 'create table '.$this->wrapTable($blueprint)." ($columns)";
  57. }
  58. /**
  59. * Compile a column addition table command.
  60. *
  61. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  62. * @param \Illuminate\Support\Fluent $command
  63. * @return string
  64. */
  65. public function compileAdd(Blueprint $blueprint, Fluent $command)
  66. {
  67. return sprintf('alter table %s add %s',
  68. $this->wrapTable($blueprint),
  69. implode(', ', $this->getColumns($blueprint))
  70. );
  71. }
  72. /**
  73. * Compile a primary key command.
  74. *
  75. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  76. * @param \Illuminate\Support\Fluent $command
  77. * @return string
  78. */
  79. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  80. {
  81. return sprintf('alter table %s add constraint %s primary key (%s)',
  82. $this->wrapTable($blueprint),
  83. $this->wrap($command->index),
  84. $this->columnize($command->columns)
  85. );
  86. }
  87. /**
  88. * Compile a unique key command.
  89. *
  90. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  91. * @param \Illuminate\Support\Fluent $command
  92. * @return string
  93. */
  94. public function compileUnique(Blueprint $blueprint, Fluent $command)
  95. {
  96. return sprintf('create unique index %s on %s (%s)',
  97. $this->wrap($command->index),
  98. $this->wrapTable($blueprint),
  99. $this->columnize($command->columns)
  100. );
  101. }
  102. /**
  103. * Compile a plain index key command.
  104. *
  105. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  106. * @param \Illuminate\Support\Fluent $command
  107. * @return string
  108. */
  109. public function compileIndex(Blueprint $blueprint, Fluent $command)
  110. {
  111. return sprintf('create index %s on %s (%s)',
  112. $this->wrap($command->index),
  113. $this->wrapTable($blueprint),
  114. $this->columnize($command->columns)
  115. );
  116. }
  117. /**
  118. * Compile a spatial index key command.
  119. *
  120. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  121. * @param \Illuminate\Support\Fluent $command
  122. * @return string
  123. */
  124. public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
  125. {
  126. return sprintf('create spatial index %s on %s (%s)',
  127. $this->wrap($command->index),
  128. $this->wrapTable($blueprint),
  129. $this->columnize($command->columns)
  130. );
  131. }
  132. /**
  133. * Compile a drop table command.
  134. *
  135. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  136. * @param \Illuminate\Support\Fluent $command
  137. * @return string
  138. */
  139. public function compileDrop(Blueprint $blueprint, Fluent $command)
  140. {
  141. return 'drop table '.$this->wrapTable($blueprint);
  142. }
  143. /**
  144. * Compile a drop table (if exists) command.
  145. *
  146. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  147. * @param \Illuminate\Support\Fluent $command
  148. * @return string
  149. */
  150. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  151. {
  152. return sprintf('if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = %s) drop table %s',
  153. "'".str_replace("'", "''", $this->getTablePrefix().$blueprint->getTable())."'",
  154. $this->wrapTable($blueprint)
  155. );
  156. }
  157. /**
  158. * Compile the SQL needed to drop all tables.
  159. *
  160. * @return string
  161. */
  162. public function compileDropAllTables()
  163. {
  164. return "EXEC sp_msforeachtable 'DROP TABLE ?'";
  165. }
  166. /**
  167. * Compile a drop column command.
  168. *
  169. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  170. * @param \Illuminate\Support\Fluent $command
  171. * @return string
  172. */
  173. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  174. {
  175. $columns = $this->wrapArray($command->columns);
  176. return 'alter table '.$this->wrapTable($blueprint).' drop column '.implode(', ', $columns);
  177. }
  178. /**
  179. * Compile a drop primary key command.
  180. *
  181. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  182. * @param \Illuminate\Support\Fluent $command
  183. * @return string
  184. */
  185. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  186. {
  187. $index = $this->wrap($command->index);
  188. return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
  189. }
  190. /**
  191. * Compile a drop unique key command.
  192. *
  193. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  194. * @param \Illuminate\Support\Fluent $command
  195. * @return string
  196. */
  197. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  198. {
  199. $index = $this->wrap($command->index);
  200. return "drop index {$index} on {$this->wrapTable($blueprint)}";
  201. }
  202. /**
  203. * Compile a drop index command.
  204. *
  205. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  206. * @param \Illuminate\Support\Fluent $command
  207. * @return string
  208. */
  209. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  210. {
  211. $index = $this->wrap($command->index);
  212. return "drop index {$index} on {$this->wrapTable($blueprint)}";
  213. }
  214. /**
  215. * Compile a drop spatial index command.
  216. *
  217. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  218. * @param \Illuminate\Support\Fluent $command
  219. * @return string
  220. */
  221. public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
  222. {
  223. return $this->compileDropIndex($blueprint, $command);
  224. }
  225. /**
  226. * Compile a drop foreign key command.
  227. *
  228. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  229. * @param \Illuminate\Support\Fluent $command
  230. * @return string
  231. */
  232. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  233. {
  234. $index = $this->wrap($command->index);
  235. return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
  236. }
  237. /**
  238. * Compile a rename table command.
  239. *
  240. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  241. * @param \Illuminate\Support\Fluent $command
  242. * @return string
  243. */
  244. public function compileRename(Blueprint $blueprint, Fluent $command)
  245. {
  246. $from = $this->wrapTable($blueprint);
  247. return "sp_rename {$from}, ".$this->wrapTable($command->to);
  248. }
  249. /**
  250. * Compile the command to enable foreign key constraints.
  251. *
  252. * @return string
  253. */
  254. public function compileEnableForeignKeyConstraints()
  255. {
  256. return 'EXEC sp_msforeachtable @command1="print \'?\'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";';
  257. }
  258. /**
  259. * Compile the command to disable foreign key constraints.
  260. *
  261. * @return string
  262. */
  263. public function compileDisableForeignKeyConstraints()
  264. {
  265. return 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";';
  266. }
  267. /**
  268. * Create the column definition for a char type.
  269. *
  270. * @param \Illuminate\Support\Fluent $column
  271. * @return string
  272. */
  273. protected function typeChar(Fluent $column)
  274. {
  275. return "nchar({$column->length})";
  276. }
  277. /**
  278. * Create the column definition for a string type.
  279. *
  280. * @param \Illuminate\Support\Fluent $column
  281. * @return string
  282. */
  283. protected function typeString(Fluent $column)
  284. {
  285. return "nvarchar({$column->length})";
  286. }
  287. /**
  288. * Create the column definition for a text type.
  289. *
  290. * @param \Illuminate\Support\Fluent $column
  291. * @return string
  292. */
  293. protected function typeText(Fluent $column)
  294. {
  295. return 'nvarchar(max)';
  296. }
  297. /**
  298. * Create the column definition for a medium text type.
  299. *
  300. * @param \Illuminate\Support\Fluent $column
  301. * @return string
  302. */
  303. protected function typeMediumText(Fluent $column)
  304. {
  305. return 'nvarchar(max)';
  306. }
  307. /**
  308. * Create the column definition for a long text type.
  309. *
  310. * @param \Illuminate\Support\Fluent $column
  311. * @return string
  312. */
  313. protected function typeLongText(Fluent $column)
  314. {
  315. return 'nvarchar(max)';
  316. }
  317. /**
  318. * Create the column definition for an integer type.
  319. *
  320. * @param \Illuminate\Support\Fluent $column
  321. * @return string
  322. */
  323. protected function typeInteger(Fluent $column)
  324. {
  325. return 'int';
  326. }
  327. /**
  328. * Create the column definition for a big integer type.
  329. *
  330. * @param \Illuminate\Support\Fluent $column
  331. * @return string
  332. */
  333. protected function typeBigInteger(Fluent $column)
  334. {
  335. return 'bigint';
  336. }
  337. /**
  338. * Create the column definition for a medium integer type.
  339. *
  340. * @param \Illuminate\Support\Fluent $column
  341. * @return string
  342. */
  343. protected function typeMediumInteger(Fluent $column)
  344. {
  345. return 'int';
  346. }
  347. /**
  348. * Create the column definition for a tiny integer type.
  349. *
  350. * @param \Illuminate\Support\Fluent $column
  351. * @return string
  352. */
  353. protected function typeTinyInteger(Fluent $column)
  354. {
  355. return 'tinyint';
  356. }
  357. /**
  358. * Create the column definition for a small integer type.
  359. *
  360. * @param \Illuminate\Support\Fluent $column
  361. * @return string
  362. */
  363. protected function typeSmallInteger(Fluent $column)
  364. {
  365. return 'smallint';
  366. }
  367. /**
  368. * Create the column definition for a float type.
  369. *
  370. * @param \Illuminate\Support\Fluent $column
  371. * @return string
  372. */
  373. protected function typeFloat(Fluent $column)
  374. {
  375. return 'float';
  376. }
  377. /**
  378. * Create the column definition for a double type.
  379. *
  380. * @param \Illuminate\Support\Fluent $column
  381. * @return string
  382. */
  383. protected function typeDouble(Fluent $column)
  384. {
  385. return 'float';
  386. }
  387. /**
  388. * Create the column definition for a decimal type.
  389. *
  390. * @param \Illuminate\Support\Fluent $column
  391. * @return string
  392. */
  393. protected function typeDecimal(Fluent $column)
  394. {
  395. return "decimal({$column->total}, {$column->places})";
  396. }
  397. /**
  398. * Create the column definition for a boolean type.
  399. *
  400. * @param \Illuminate\Support\Fluent $column
  401. * @return string
  402. */
  403. protected function typeBoolean(Fluent $column)
  404. {
  405. return 'bit';
  406. }
  407. /**
  408. * Create the column definition for an enum type.
  409. *
  410. * @param \Illuminate\Support\Fluent $column
  411. * @return string
  412. */
  413. protected function typeEnum(Fluent $column)
  414. {
  415. return 'nvarchar(255)';
  416. }
  417. /**
  418. * Create the column definition for a json type.
  419. *
  420. * @param \Illuminate\Support\Fluent $column
  421. * @return string
  422. */
  423. protected function typeJson(Fluent $column)
  424. {
  425. return 'nvarchar(max)';
  426. }
  427. /**
  428. * Create the column definition for a jsonb type.
  429. *
  430. * @param \Illuminate\Support\Fluent $column
  431. * @return string
  432. */
  433. protected function typeJsonb(Fluent $column)
  434. {
  435. return 'nvarchar(max)';
  436. }
  437. /**
  438. * Create the column definition for a date type.
  439. *
  440. * @param \Illuminate\Support\Fluent $column
  441. * @return string
  442. */
  443. protected function typeDate(Fluent $column)
  444. {
  445. return 'date';
  446. }
  447. /**
  448. * Create the column definition for a date-time type.
  449. *
  450. * @param \Illuminate\Support\Fluent $column
  451. * @return string
  452. */
  453. protected function typeDateTime(Fluent $column)
  454. {
  455. return $column->precision ? "datetime2($column->precision)" : 'datetime';
  456. }
  457. /**
  458. * Create the column definition for a date-time (with time zone) type.
  459. *
  460. * @param \Illuminate\Support\Fluent $column
  461. * @return string
  462. */
  463. protected function typeDateTimeTz(Fluent $column)
  464. {
  465. return $column->precision ? "datetimeoffset($column->precision)" : 'datetimeoffset';
  466. }
  467. /**
  468. * Create the column definition for a time type.
  469. *
  470. * @param \Illuminate\Support\Fluent $column
  471. * @return string
  472. */
  473. protected function typeTime(Fluent $column)
  474. {
  475. return $column->precision ? "time($column->precision)" : 'time';
  476. }
  477. /**
  478. * Create the column definition for a time (with time zone) type.
  479. *
  480. * @param \Illuminate\Support\Fluent $column
  481. * @return string
  482. */
  483. protected function typeTimeTz(Fluent $column)
  484. {
  485. return $this->typeTime($column);
  486. }
  487. /**
  488. * Create the column definition for a timestamp type.
  489. *
  490. * @param \Illuminate\Support\Fluent $column
  491. * @return string
  492. */
  493. protected function typeTimestamp(Fluent $column)
  494. {
  495. $columnType = $column->precision ? "datetime2($column->precision)" : 'datetime';
  496. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  497. }
  498. /**
  499. * Create the column definition for a timestamp (with time zone) type.
  500. *
  501. * @link https://msdn.microsoft.com/en-us/library/bb630289(v=sql.120).aspx
  502. *
  503. * @param \Illuminate\Support\Fluent $column
  504. * @return string
  505. */
  506. protected function typeTimestampTz(Fluent $column)
  507. {
  508. if ($column->useCurrent) {
  509. $columnType = $column->precision ? "datetimeoffset($column->precision)" : 'datetimeoffset';
  510. return "$columnType default CURRENT_TIMESTAMP";
  511. }
  512. return "datetimeoffset($column->precision)";
  513. }
  514. /**
  515. * Create the column definition for a year type.
  516. *
  517. * @param \Illuminate\Support\Fluent $column
  518. * @return string
  519. */
  520. protected function typeYear(Fluent $column)
  521. {
  522. return $this->typeInteger($column);
  523. }
  524. /**
  525. * Create the column definition for a binary type.
  526. *
  527. * @param \Illuminate\Support\Fluent $column
  528. * @return string
  529. */
  530. protected function typeBinary(Fluent $column)
  531. {
  532. return 'varbinary(max)';
  533. }
  534. /**
  535. * Create the column definition for a uuid type.
  536. *
  537. * @param \Illuminate\Support\Fluent $column
  538. * @return string
  539. */
  540. protected function typeUuid(Fluent $column)
  541. {
  542. return 'uniqueidentifier';
  543. }
  544. /**
  545. * Create the column definition for an IP address type.
  546. *
  547. * @param \Illuminate\Support\Fluent $column
  548. * @return string
  549. */
  550. protected function typeIpAddress(Fluent $column)
  551. {
  552. return 'nvarchar(45)';
  553. }
  554. /**
  555. * Create the column definition for a MAC address type.
  556. *
  557. * @param \Illuminate\Support\Fluent $column
  558. * @return string
  559. */
  560. protected function typeMacAddress(Fluent $column)
  561. {
  562. return 'nvarchar(17)';
  563. }
  564. /**
  565. * Create the column definition for a spatial Geometry type.
  566. *
  567. * @param \Illuminate\Support\Fluent $column
  568. * @return string
  569. */
  570. public function typeGeometry(Fluent $column)
  571. {
  572. return 'geography';
  573. }
  574. /**
  575. * Create the column definition for a spatial Point type.
  576. *
  577. * @param \Illuminate\Support\Fluent $column
  578. * @return string
  579. */
  580. public function typePoint(Fluent $column)
  581. {
  582. return 'geography';
  583. }
  584. /**
  585. * Create the column definition for a spatial LineString type.
  586. *
  587. * @param \Illuminate\Support\Fluent $column
  588. * @return string
  589. */
  590. public function typeLineString(Fluent $column)
  591. {
  592. return 'geography';
  593. }
  594. /**
  595. * Create the column definition for a spatial Polygon type.
  596. *
  597. * @param \Illuminate\Support\Fluent $column
  598. * @return string
  599. */
  600. public function typePolygon(Fluent $column)
  601. {
  602. return 'geography';
  603. }
  604. /**
  605. * Create the column definition for a spatial GeometryCollection type.
  606. *
  607. * @param \Illuminate\Support\Fluent $column
  608. * @return string
  609. */
  610. public function typeGeometryCollection(Fluent $column)
  611. {
  612. return 'geography';
  613. }
  614. /**
  615. * Create the column definition for a spatial MultiPoint type.
  616. *
  617. * @param \Illuminate\Support\Fluent $column
  618. * @return string
  619. */
  620. public function typeMultiPoint(Fluent $column)
  621. {
  622. return 'geography';
  623. }
  624. /**
  625. * Create the column definition for a spatial MultiLineString type.
  626. *
  627. * @param \Illuminate\Support\Fluent $column
  628. * @return string
  629. */
  630. public function typeMultiLineString(Fluent $column)
  631. {
  632. return 'geography';
  633. }
  634. /**
  635. * Create the column definition for a spatial MultiPolygon type.
  636. *
  637. * @param \Illuminate\Support\Fluent $column
  638. * @return string
  639. */
  640. public function typeMultiPolygon(Fluent $column)
  641. {
  642. return 'geography';
  643. }
  644. /**
  645. * Get the SQL for a collation column modifier.
  646. *
  647. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  648. * @param \Illuminate\Support\Fluent $column
  649. * @return string|null
  650. */
  651. protected function modifyCollate(Blueprint $blueprint, Fluent $column)
  652. {
  653. if (! is_null($column->collation)) {
  654. return ' collate '.$column->collation;
  655. }
  656. }
  657. /**
  658. * Get the SQL for a nullable column modifier.
  659. *
  660. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  661. * @param \Illuminate\Support\Fluent $column
  662. * @return string|null
  663. */
  664. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  665. {
  666. return $column->nullable ? ' null' : ' not null';
  667. }
  668. /**
  669. * Get the SQL for a default column modifier.
  670. *
  671. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  672. * @param \Illuminate\Support\Fluent $column
  673. * @return string|null
  674. */
  675. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  676. {
  677. if (! is_null($column->default)) {
  678. return ' default '.$this->getDefaultValue($column->default);
  679. }
  680. }
  681. /**
  682. * Get the SQL for an auto-increment column modifier.
  683. *
  684. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  685. * @param \Illuminate\Support\Fluent $column
  686. * @return string|null
  687. */
  688. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  689. {
  690. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  691. return ' identity primary key';
  692. }
  693. }
  694. /**
  695. * Wrap a table in keyword identifiers.
  696. *
  697. * @param \Illuminate\Database\Query\Expression|string $table
  698. * @return string
  699. */
  700. public function wrapTable($table)
  701. {
  702. if ($table instanceof Blueprint && $table->temporary) {
  703. $this->setTablePrefix('#');
  704. }
  705. return parent::wrapTable($table);
  706. }
  707. }