/vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php

https://gitlab.com/ealexis.t/trends · PHP · 626 lines · 236 code · 77 blank · 313 comment · 5 complexity · c8e475b1114299cf9298456f71cc3016 MD5 · raw file

  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. * The possible column modifiers.
  9. *
  10. * @var array
  11. */
  12. protected $modifiers = ['Increment', 'Nullable', 'Default'];
  13. /**
  14. * The columns available as serials.
  15. *
  16. * @var array
  17. */
  18. protected $serials = ['tinyInteger', 'smallInteger', 'mediumInteger', 'integer', 'bigInteger'];
  19. /**
  20. * Compile the query to determine if a table exists.
  21. *
  22. * @return string
  23. */
  24. public function compileTableExists()
  25. {
  26. return "select * from sysobjects where type = 'U' and name = ?";
  27. }
  28. /**
  29. * Compile the query to determine the list of columns.
  30. *
  31. * @param string $table
  32. * @return string
  33. */
  34. public function compileColumnExists($table)
  35. {
  36. return "select col.name from sys.columns as col
  37. join sys.objects as obj on col.object_id = obj.object_id
  38. where obj.type = 'U' and obj.name = '$table'";
  39. }
  40. /**
  41. * Compile a create table command.
  42. *
  43. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  44. * @param \Illuminate\Support\Fluent $command
  45. * @return string
  46. */
  47. public function compileCreate(Blueprint $blueprint, Fluent $command)
  48. {
  49. $columns = implode(', ', $this->getColumns($blueprint));
  50. return 'create table '.$this->wrapTable($blueprint)." ($columns)";
  51. }
  52. /**
  53. * Compile a column addition table command.
  54. *
  55. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  56. * @param \Illuminate\Support\Fluent $command
  57. * @return string
  58. */
  59. public function compileAdd(Blueprint $blueprint, Fluent $command)
  60. {
  61. $table = $this->wrapTable($blueprint);
  62. $columns = $this->getColumns($blueprint);
  63. return 'alter table '.$table.' add '.implode(', ', $columns);
  64. }
  65. /**
  66. * Compile a primary key command.
  67. *
  68. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  69. * @param \Illuminate\Support\Fluent $command
  70. * @return string
  71. */
  72. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  73. {
  74. $columns = $this->columnize($command->columns);
  75. $table = $this->wrapTable($blueprint);
  76. $index = $this->wrap($command->index);
  77. return "alter table {$table} add constraint {$index} primary key ({$columns})";
  78. }
  79. /**
  80. * Compile a unique key command.
  81. *
  82. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  83. * @param \Illuminate\Support\Fluent $command
  84. * @return string
  85. */
  86. public function compileUnique(Blueprint $blueprint, Fluent $command)
  87. {
  88. $columns = $this->columnize($command->columns);
  89. $table = $this->wrapTable($blueprint);
  90. $index = $this->wrap($command->index);
  91. return "create unique index {$index} on {$table} ({$columns})";
  92. }
  93. /**
  94. * Compile a plain index key command.
  95. *
  96. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  97. * @param \Illuminate\Support\Fluent $command
  98. * @return string
  99. */
  100. public function compileIndex(Blueprint $blueprint, Fluent $command)
  101. {
  102. $columns = $this->columnize($command->columns);
  103. $table = $this->wrapTable($blueprint);
  104. $index = $this->wrap($command->index);
  105. return "create index {$index} on {$table} ({$columns})";
  106. }
  107. /**
  108. * Compile a drop table command.
  109. *
  110. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  111. * @param \Illuminate\Support\Fluent $command
  112. * @return string
  113. */
  114. public function compileDrop(Blueprint $blueprint, Fluent $command)
  115. {
  116. return 'drop table '.$this->wrapTable($blueprint);
  117. }
  118. /**
  119. * Compile a drop table (if exists) command.
  120. *
  121. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  122. * @param \Illuminate\Support\Fluent $command
  123. * @return string
  124. */
  125. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  126. {
  127. return 'if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \''.$blueprint->getTable().'\') drop table ['.$blueprint->getTable().']';
  128. }
  129. /**
  130. * Compile a drop column command.
  131. *
  132. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  133. * @param \Illuminate\Support\Fluent $command
  134. * @return string
  135. */
  136. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  137. {
  138. $columns = $this->wrapArray($command->columns);
  139. $table = $this->wrapTable($blueprint);
  140. return 'alter table '.$table.' drop column '.implode(', ', $columns);
  141. }
  142. /**
  143. * Compile a drop primary key command.
  144. *
  145. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  146. * @param \Illuminate\Support\Fluent $command
  147. * @return string
  148. */
  149. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  150. {
  151. $table = $this->wrapTable($blueprint);
  152. $index = $this->wrap($command->index);
  153. return "alter table {$table} drop constraint {$index}";
  154. }
  155. /**
  156. * Compile a drop unique key command.
  157. *
  158. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  159. * @param \Illuminate\Support\Fluent $command
  160. * @return string
  161. */
  162. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  163. {
  164. $table = $this->wrapTable($blueprint);
  165. $index = $this->wrap($command->index);
  166. return "drop index {$index} on {$table}";
  167. }
  168. /**
  169. * Compile a drop index command.
  170. *
  171. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  172. * @param \Illuminate\Support\Fluent $command
  173. * @return string
  174. */
  175. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  176. {
  177. $table = $this->wrapTable($blueprint);
  178. $index = $this->wrap($command->index);
  179. return "drop index {$index} on {$table}";
  180. }
  181. /**
  182. * Compile a drop foreign key command.
  183. *
  184. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  185. * @param \Illuminate\Support\Fluent $command
  186. * @return string
  187. */
  188. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  189. {
  190. $table = $this->wrapTable($blueprint);
  191. $index = $this->wrap($command->index);
  192. return "alter table {$table} drop constraint {$index}";
  193. }
  194. /**
  195. * Compile a rename table command.
  196. *
  197. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  198. * @param \Illuminate\Support\Fluent $command
  199. * @return string
  200. */
  201. public function compileRename(Blueprint $blueprint, Fluent $command)
  202. {
  203. $from = $this->wrapTable($blueprint);
  204. return "sp_rename {$from}, ".$this->wrapTable($command->to);
  205. }
  206. /**
  207. * Compile the command to enable foreign key constraints.
  208. *
  209. * @return string
  210. */
  211. public function compileEnableForeignKeyConstraints()
  212. {
  213. return 'EXEC sp_msforeachtable @command1="print \'?\'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";';
  214. }
  215. /**
  216. * Compile the command to disable foreign key constraints.
  217. *
  218. * @return string
  219. */
  220. public function compileDisableForeignKeyConstraints()
  221. {
  222. return 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";';
  223. }
  224. /**
  225. * Create the column definition for a char type.
  226. *
  227. * @param \Illuminate\Support\Fluent $column
  228. * @return string
  229. */
  230. protected function typeChar(Fluent $column)
  231. {
  232. return "nchar({$column->length})";
  233. }
  234. /**
  235. * Create the column definition for a string type.
  236. *
  237. * @param \Illuminate\Support\Fluent $column
  238. * @return string
  239. */
  240. protected function typeString(Fluent $column)
  241. {
  242. return "nvarchar({$column->length})";
  243. }
  244. /**
  245. * Create the column definition for a text type.
  246. *
  247. * @param \Illuminate\Support\Fluent $column
  248. * @return string
  249. */
  250. protected function typeText(Fluent $column)
  251. {
  252. return 'nvarchar(max)';
  253. }
  254. /**
  255. * Create the column definition for a medium text type.
  256. *
  257. * @param \Illuminate\Support\Fluent $column
  258. * @return string
  259. */
  260. protected function typeMediumText(Fluent $column)
  261. {
  262. return 'nvarchar(max)';
  263. }
  264. /**
  265. * Create the column definition for a long text type.
  266. *
  267. * @param \Illuminate\Support\Fluent $column
  268. * @return string
  269. */
  270. protected function typeLongText(Fluent $column)
  271. {
  272. return 'nvarchar(max)';
  273. }
  274. /**
  275. * Create the column definition for a integer type.
  276. *
  277. * @param \Illuminate\Support\Fluent $column
  278. * @return string
  279. */
  280. protected function typeInteger(Fluent $column)
  281. {
  282. return 'int';
  283. }
  284. /**
  285. * Create the column definition for a big integer type.
  286. *
  287. * @param \Illuminate\Support\Fluent $column
  288. * @return string
  289. */
  290. protected function typeBigInteger(Fluent $column)
  291. {
  292. return 'bigint';
  293. }
  294. /**
  295. * Create the column definition for a medium integer type.
  296. *
  297. * @param \Illuminate\Support\Fluent $column
  298. * @return string
  299. */
  300. protected function typeMediumInteger(Fluent $column)
  301. {
  302. return 'int';
  303. }
  304. /**
  305. * Create the column definition for a tiny integer type.
  306. *
  307. * @param \Illuminate\Support\Fluent $column
  308. * @return string
  309. */
  310. protected function typeTinyInteger(Fluent $column)
  311. {
  312. return 'tinyint';
  313. }
  314. /**
  315. * Create the column definition for a small integer type.
  316. *
  317. * @param \Illuminate\Support\Fluent $column
  318. * @return string
  319. */
  320. protected function typeSmallInteger(Fluent $column)
  321. {
  322. return 'smallint';
  323. }
  324. /**
  325. * Create the column definition for a float type.
  326. *
  327. * @param \Illuminate\Support\Fluent $column
  328. * @return string
  329. */
  330. protected function typeFloat(Fluent $column)
  331. {
  332. return 'float';
  333. }
  334. /**
  335. * Create the column definition for a double type.
  336. *
  337. * @param \Illuminate\Support\Fluent $column
  338. * @return string
  339. */
  340. protected function typeDouble(Fluent $column)
  341. {
  342. return 'float';
  343. }
  344. /**
  345. * Create the column definition for a decimal type.
  346. *
  347. * @param \Illuminate\Support\Fluent $column
  348. * @return string
  349. */
  350. protected function typeDecimal(Fluent $column)
  351. {
  352. return "decimal({$column->total}, {$column->places})";
  353. }
  354. /**
  355. * Create the column definition for a boolean type.
  356. *
  357. * @param \Illuminate\Support\Fluent $column
  358. * @return string
  359. */
  360. protected function typeBoolean(Fluent $column)
  361. {
  362. return 'bit';
  363. }
  364. /**
  365. * Create the column definition for an enum type.
  366. *
  367. * @param \Illuminate\Support\Fluent $column
  368. * @return string
  369. */
  370. protected function typeEnum(Fluent $column)
  371. {
  372. return 'nvarchar(255)';
  373. }
  374. /**
  375. * Create the column definition for a json type.
  376. *
  377. * @param \Illuminate\Support\Fluent $column
  378. * @return string
  379. */
  380. protected function typeJson(Fluent $column)
  381. {
  382. return 'nvarchar(max)';
  383. }
  384. /**
  385. * Create the column definition for a jsonb type.
  386. *
  387. * @param \Illuminate\Support\Fluent $column
  388. * @return string
  389. */
  390. protected function typeJsonb(Fluent $column)
  391. {
  392. return 'nvarchar(max)';
  393. }
  394. /**
  395. * Create the column definition for a date type.
  396. *
  397. * @param \Illuminate\Support\Fluent $column
  398. * @return string
  399. */
  400. protected function typeDate(Fluent $column)
  401. {
  402. return 'date';
  403. }
  404. /**
  405. * Create the column definition for a date-time type.
  406. *
  407. * @param \Illuminate\Support\Fluent $column
  408. * @return string
  409. */
  410. protected function typeDateTime(Fluent $column)
  411. {
  412. return 'datetime';
  413. }
  414. /**
  415. * Create the column definition for a date-time type.
  416. *
  417. * @param \Illuminate\Support\Fluent $column
  418. * @return string
  419. */
  420. protected function typeDateTimeTz(Fluent $column)
  421. {
  422. return 'datetimeoffset(0)';
  423. }
  424. /**
  425. * Create the column definition for a time type.
  426. *
  427. * @param \Illuminate\Support\Fluent $column
  428. * @return string
  429. */
  430. protected function typeTime(Fluent $column)
  431. {
  432. return 'time';
  433. }
  434. /**
  435. * Create the column definition for a time type.
  436. *
  437. * @param \Illuminate\Support\Fluent $column
  438. * @return string
  439. */
  440. protected function typeTimeTz(Fluent $column)
  441. {
  442. return 'time';
  443. }
  444. /**
  445. * Create the column definition for a timestamp type.
  446. *
  447. * @param \Illuminate\Support\Fluent $column
  448. * @return string
  449. */
  450. protected function typeTimestamp(Fluent $column)
  451. {
  452. if ($column->useCurrent) {
  453. return 'datetime default CURRENT_TIMESTAMP';
  454. }
  455. return 'datetime';
  456. }
  457. /**
  458. * Create the column definition for a timestamp type.
  459. *
  460. * @link https://msdn.microsoft.com/en-us/library/bb630289(v=sql.120).aspx
  461. *
  462. * @param \Illuminate\Support\Fluent $column
  463. * @return string
  464. */
  465. protected function typeTimestampTz(Fluent $column)
  466. {
  467. if ($column->useCurrent) {
  468. return 'datetimeoffset(0) default CURRENT_TIMESTAMP';
  469. }
  470. return 'datetimeoffset(0)';
  471. }
  472. /**
  473. * Create the column definition for a binary type.
  474. *
  475. * @param \Illuminate\Support\Fluent $column
  476. * @return string
  477. */
  478. protected function typeBinary(Fluent $column)
  479. {
  480. return 'varbinary(max)';
  481. }
  482. /**
  483. * Create the column definition for a uuid type.
  484. *
  485. * @param \Illuminate\Support\Fluent $column
  486. * @return string
  487. */
  488. protected function typeUuid(Fluent $column)
  489. {
  490. return 'uniqueidentifier';
  491. }
  492. /**
  493. * Create the column definition for an IP address type.
  494. *
  495. * @param \Illuminate\Support\Fluent $column
  496. * @return string
  497. */
  498. protected function typeIpAddress(Fluent $column)
  499. {
  500. return 'nvarchar(45)';
  501. }
  502. /**
  503. * Create the column definition for a MAC address type.
  504. *
  505. * @param \Illuminate\Support\Fluent $column
  506. * @return string
  507. */
  508. protected function typeMacAddress(Fluent $column)
  509. {
  510. return 'nvarchar(17)';
  511. }
  512. /**
  513. * Get the SQL for a nullable column modifier.
  514. *
  515. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  516. * @param \Illuminate\Support\Fluent $column
  517. * @return string|null
  518. */
  519. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  520. {
  521. return $column->nullable ? ' null' : ' not null';
  522. }
  523. /**
  524. * Get the SQL for a default column modifier.
  525. *
  526. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  527. * @param \Illuminate\Support\Fluent $column
  528. * @return string|null
  529. */
  530. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  531. {
  532. if (! is_null($column->default)) {
  533. return ' default '.$this->getDefaultValue($column->default);
  534. }
  535. }
  536. /**
  537. * Get the SQL for an auto-increment column modifier.
  538. *
  539. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  540. * @param \Illuminate\Support\Fluent $column
  541. * @return string|null
  542. */
  543. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  544. {
  545. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  546. return ' identity primary key';
  547. }
  548. }
  549. }