SqlServerGrammar.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 a rename index command.
  251. *
  252. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  253. * @param \Illuminate\Support\Fluent $command
  254. * @return string
  255. */
  256. public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
  257. {
  258. return sprintf("sp_rename N'%s', %s, N'INDEX'",
  259. $this->wrap($blueprint->getTable().'.'.$command->from),
  260. $this->wrap($command->to)
  261. );
  262. }
  263. /**
  264. * Compile the command to enable foreign key constraints.
  265. *
  266. * @return string
  267. */
  268. public function compileEnableForeignKeyConstraints()
  269. {
  270. return 'EXEC sp_msforeachtable @command1="print \'?\'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";';
  271. }
  272. /**
  273. * Compile the command to disable foreign key constraints.
  274. *
  275. * @return string
  276. */
  277. public function compileDisableForeignKeyConstraints()
  278. {
  279. return 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";';
  280. }
  281. /**
  282. * Create the column definition for a char type.
  283. *
  284. * @param \Illuminate\Support\Fluent $column
  285. * @return string
  286. */
  287. protected function typeChar(Fluent $column)
  288. {
  289. return "nchar({$column->length})";
  290. }
  291. /**
  292. * Create the column definition for a string type.
  293. *
  294. * @param \Illuminate\Support\Fluent $column
  295. * @return string
  296. */
  297. protected function typeString(Fluent $column)
  298. {
  299. return "nvarchar({$column->length})";
  300. }
  301. /**
  302. * Create the column definition for a text type.
  303. *
  304. * @param \Illuminate\Support\Fluent $column
  305. * @return string
  306. */
  307. protected function typeText(Fluent $column)
  308. {
  309. return 'nvarchar(max)';
  310. }
  311. /**
  312. * Create the column definition for a medium text type.
  313. *
  314. * @param \Illuminate\Support\Fluent $column
  315. * @return string
  316. */
  317. protected function typeMediumText(Fluent $column)
  318. {
  319. return 'nvarchar(max)';
  320. }
  321. /**
  322. * Create the column definition for a long text type.
  323. *
  324. * @param \Illuminate\Support\Fluent $column
  325. * @return string
  326. */
  327. protected function typeLongText(Fluent $column)
  328. {
  329. return 'nvarchar(max)';
  330. }
  331. /**
  332. * Create the column definition for an integer type.
  333. *
  334. * @param \Illuminate\Support\Fluent $column
  335. * @return string
  336. */
  337. protected function typeInteger(Fluent $column)
  338. {
  339. return 'int';
  340. }
  341. /**
  342. * Create the column definition for a big integer type.
  343. *
  344. * @param \Illuminate\Support\Fluent $column
  345. * @return string
  346. */
  347. protected function typeBigInteger(Fluent $column)
  348. {
  349. return 'bigint';
  350. }
  351. /**
  352. * Create the column definition for a medium integer type.
  353. *
  354. * @param \Illuminate\Support\Fluent $column
  355. * @return string
  356. */
  357. protected function typeMediumInteger(Fluent $column)
  358. {
  359. return 'int';
  360. }
  361. /**
  362. * Create the column definition for a tiny integer type.
  363. *
  364. * @param \Illuminate\Support\Fluent $column
  365. * @return string
  366. */
  367. protected function typeTinyInteger(Fluent $column)
  368. {
  369. return 'tinyint';
  370. }
  371. /**
  372. * Create the column definition for a small integer type.
  373. *
  374. * @param \Illuminate\Support\Fluent $column
  375. * @return string
  376. */
  377. protected function typeSmallInteger(Fluent $column)
  378. {
  379. return 'smallint';
  380. }
  381. /**
  382. * Create the column definition for a float type.
  383. *
  384. * @param \Illuminate\Support\Fluent $column
  385. * @return string
  386. */
  387. protected function typeFloat(Fluent $column)
  388. {
  389. return 'float';
  390. }
  391. /**
  392. * Create the column definition for a double type.
  393. *
  394. * @param \Illuminate\Support\Fluent $column
  395. * @return string
  396. */
  397. protected function typeDouble(Fluent $column)
  398. {
  399. return 'float';
  400. }
  401. /**
  402. * Create the column definition for a decimal type.
  403. *
  404. * @param \Illuminate\Support\Fluent $column
  405. * @return string
  406. */
  407. protected function typeDecimal(Fluent $column)
  408. {
  409. return "decimal({$column->total}, {$column->places})";
  410. }
  411. /**
  412. * Create the column definition for a boolean type.
  413. *
  414. * @param \Illuminate\Support\Fluent $column
  415. * @return string
  416. */
  417. protected function typeBoolean(Fluent $column)
  418. {
  419. return 'bit';
  420. }
  421. /**
  422. * Create the column definition for an enumeration type.
  423. *
  424. * @param \Illuminate\Support\Fluent $column
  425. * @return string
  426. */
  427. protected function typeEnum(Fluent $column)
  428. {
  429. return sprintf(
  430. 'nvarchar(255) check ("%s" in (%s))',
  431. $column->name,
  432. $this->quoteString($column->allowed)
  433. );
  434. }
  435. /**
  436. * Create the column definition for a json type.
  437. *
  438. * @param \Illuminate\Support\Fluent $column
  439. * @return string
  440. */
  441. protected function typeJson(Fluent $column)
  442. {
  443. return 'nvarchar(max)';
  444. }
  445. /**
  446. * Create the column definition for a jsonb type.
  447. *
  448. * @param \Illuminate\Support\Fluent $column
  449. * @return string
  450. */
  451. protected function typeJsonb(Fluent $column)
  452. {
  453. return 'nvarchar(max)';
  454. }
  455. /**
  456. * Create the column definition for a date type.
  457. *
  458. * @param \Illuminate\Support\Fluent $column
  459. * @return string
  460. */
  461. protected function typeDate(Fluent $column)
  462. {
  463. return 'date';
  464. }
  465. /**
  466. * Create the column definition for a date-time type.
  467. *
  468. * @param \Illuminate\Support\Fluent $column
  469. * @return string
  470. */
  471. protected function typeDateTime(Fluent $column)
  472. {
  473. return $column->precision ? "datetime2($column->precision)" : 'datetime';
  474. }
  475. /**
  476. * Create the column definition for a date-time (with time zone) type.
  477. *
  478. * @param \Illuminate\Support\Fluent $column
  479. * @return string
  480. */
  481. protected function typeDateTimeTz(Fluent $column)
  482. {
  483. return $column->precision ? "datetimeoffset($column->precision)" : 'datetimeoffset';
  484. }
  485. /**
  486. * Create the column definition for a time type.
  487. *
  488. * @param \Illuminate\Support\Fluent $column
  489. * @return string
  490. */
  491. protected function typeTime(Fluent $column)
  492. {
  493. return $column->precision ? "time($column->precision)" : 'time';
  494. }
  495. /**
  496. * Create the column definition for a time (with time zone) type.
  497. *
  498. * @param \Illuminate\Support\Fluent $column
  499. * @return string
  500. */
  501. protected function typeTimeTz(Fluent $column)
  502. {
  503. return $this->typeTime($column);
  504. }
  505. /**
  506. * Create the column definition for a timestamp type.
  507. *
  508. * @param \Illuminate\Support\Fluent $column
  509. * @return string
  510. */
  511. protected function typeTimestamp(Fluent $column)
  512. {
  513. $columnType = $column->precision ? "datetime2($column->precision)" : 'datetime';
  514. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  515. }
  516. /**
  517. * Create the column definition for a timestamp (with time zone) type.
  518. *
  519. * @link https://msdn.microsoft.com/en-us/library/bb630289(v=sql.120).aspx
  520. *
  521. * @param \Illuminate\Support\Fluent $column
  522. * @return string
  523. */
  524. protected function typeTimestampTz(Fluent $column)
  525. {
  526. if ($column->useCurrent) {
  527. $columnType = $column->precision ? "datetimeoffset($column->precision)" : 'datetimeoffset';
  528. return "$columnType default CURRENT_TIMESTAMP";
  529. }
  530. return "datetimeoffset($column->precision)";
  531. }
  532. /**
  533. * Create the column definition for a year type.
  534. *
  535. * @param \Illuminate\Support\Fluent $column
  536. * @return string
  537. */
  538. protected function typeYear(Fluent $column)
  539. {
  540. return $this->typeInteger($column);
  541. }
  542. /**
  543. * Create the column definition for a binary type.
  544. *
  545. * @param \Illuminate\Support\Fluent $column
  546. * @return string
  547. */
  548. protected function typeBinary(Fluent $column)
  549. {
  550. return 'varbinary(max)';
  551. }
  552. /**
  553. * Create the column definition for a uuid type.
  554. *
  555. * @param \Illuminate\Support\Fluent $column
  556. * @return string
  557. */
  558. protected function typeUuid(Fluent $column)
  559. {
  560. return 'uniqueidentifier';
  561. }
  562. /**
  563. * Create the column definition for an IP address type.
  564. *
  565. * @param \Illuminate\Support\Fluent $column
  566. * @return string
  567. */
  568. protected function typeIpAddress(Fluent $column)
  569. {
  570. return 'nvarchar(45)';
  571. }
  572. /**
  573. * Create the column definition for a MAC address type.
  574. *
  575. * @param \Illuminate\Support\Fluent $column
  576. * @return string
  577. */
  578. protected function typeMacAddress(Fluent $column)
  579. {
  580. return 'nvarchar(17)';
  581. }
  582. /**
  583. * Create the column definition for a spatial Geometry type.
  584. *
  585. * @param \Illuminate\Support\Fluent $column
  586. * @return string
  587. */
  588. public function typeGeometry(Fluent $column)
  589. {
  590. return 'geography';
  591. }
  592. /**
  593. * Create the column definition for a spatial Point type.
  594. *
  595. * @param \Illuminate\Support\Fluent $column
  596. * @return string
  597. */
  598. public function typePoint(Fluent $column)
  599. {
  600. return 'geography';
  601. }
  602. /**
  603. * Create the column definition for a spatial LineString type.
  604. *
  605. * @param \Illuminate\Support\Fluent $column
  606. * @return string
  607. */
  608. public function typeLineString(Fluent $column)
  609. {
  610. return 'geography';
  611. }
  612. /**
  613. * Create the column definition for a spatial Polygon type.
  614. *
  615. * @param \Illuminate\Support\Fluent $column
  616. * @return string
  617. */
  618. public function typePolygon(Fluent $column)
  619. {
  620. return 'geography';
  621. }
  622. /**
  623. * Create the column definition for a spatial GeometryCollection type.
  624. *
  625. * @param \Illuminate\Support\Fluent $column
  626. * @return string
  627. */
  628. public function typeGeometryCollection(Fluent $column)
  629. {
  630. return 'geography';
  631. }
  632. /**
  633. * Create the column definition for a spatial MultiPoint type.
  634. *
  635. * @param \Illuminate\Support\Fluent $column
  636. * @return string
  637. */
  638. public function typeMultiPoint(Fluent $column)
  639. {
  640. return 'geography';
  641. }
  642. /**
  643. * Create the column definition for a spatial MultiLineString type.
  644. *
  645. * @param \Illuminate\Support\Fluent $column
  646. * @return string
  647. */
  648. public function typeMultiLineString(Fluent $column)
  649. {
  650. return 'geography';
  651. }
  652. /**
  653. * Create the column definition for a spatial MultiPolygon type.
  654. *
  655. * @param \Illuminate\Support\Fluent $column
  656. * @return string
  657. */
  658. public function typeMultiPolygon(Fluent $column)
  659. {
  660. return 'geography';
  661. }
  662. /**
  663. * Get the SQL for a collation column modifier.
  664. *
  665. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  666. * @param \Illuminate\Support\Fluent $column
  667. * @return string|null
  668. */
  669. protected function modifyCollate(Blueprint $blueprint, Fluent $column)
  670. {
  671. if (! is_null($column->collation)) {
  672. return ' collate '.$column->collation;
  673. }
  674. }
  675. /**
  676. * Get the SQL for a nullable column modifier.
  677. *
  678. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  679. * @param \Illuminate\Support\Fluent $column
  680. * @return string|null
  681. */
  682. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  683. {
  684. return $column->nullable ? ' null' : ' not null';
  685. }
  686. /**
  687. * Get the SQL for a default column modifier.
  688. *
  689. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  690. * @param \Illuminate\Support\Fluent $column
  691. * @return string|null
  692. */
  693. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  694. {
  695. if (! is_null($column->default)) {
  696. return ' default '.$this->getDefaultValue($column->default);
  697. }
  698. }
  699. /**
  700. * Get the SQL for an auto-increment column modifier.
  701. *
  702. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  703. * @param \Illuminate\Support\Fluent $column
  704. * @return string|null
  705. */
  706. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  707. {
  708. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  709. return ' identity primary key';
  710. }
  711. }
  712. /**
  713. * Wrap a table in keyword identifiers.
  714. *
  715. * @param \Illuminate\Database\Query\Expression|string $table
  716. * @return string
  717. */
  718. public function wrapTable($table)
  719. {
  720. if ($table instanceof Blueprint && $table->temporary) {
  721. $this->setTablePrefix('#');
  722. }
  723. return parent::wrapTable($table);
  724. }
  725. }