PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/vincetang/mtweb
PHP | 551 lines | 203 code | 63 blank | 285 comment | 3 complexity | 08eb9301e8e89855af64f87393ca27f4 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 = ['bigInteger', 'integer'];
  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 create 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. return "alter table {$table} add constraint {$command->index} primary key ({$columns})";
  77. }
  78. /**
  79. * Compile a unique key command.
  80. *
  81. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  82. * @param \Illuminate\Support\Fluent $command
  83. * @return string
  84. */
  85. public function compileUnique(Blueprint $blueprint, Fluent $command)
  86. {
  87. $columns = $this->columnize($command->columns);
  88. $table = $this->wrapTable($blueprint);
  89. return "create unique index {$command->index} on {$table} ({$columns})";
  90. }
  91. /**
  92. * Compile a plain index key command.
  93. *
  94. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  95. * @param \Illuminate\Support\Fluent $command
  96. * @return string
  97. */
  98. public function compileIndex(Blueprint $blueprint, Fluent $command)
  99. {
  100. $columns = $this->columnize($command->columns);
  101. $table = $this->wrapTable($blueprint);
  102. return "create index {$command->index} on {$table} ({$columns})";
  103. }
  104. /**
  105. * Compile a drop table command.
  106. *
  107. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  108. * @param \Illuminate\Support\Fluent $command
  109. * @return string
  110. */
  111. public function compileDrop(Blueprint $blueprint, Fluent $command)
  112. {
  113. return 'drop table '.$this->wrapTable($blueprint);
  114. }
  115. /**
  116. * Compile a drop table (if exists) command.
  117. *
  118. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  119. * @param \Illuminate\Support\Fluent $command
  120. * @return string
  121. */
  122. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  123. {
  124. return 'if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = \''.$blueprint->getTable().'\') drop table '.$blueprint->getTable();
  125. }
  126. /**
  127. * Compile a drop column command.
  128. *
  129. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  130. * @param \Illuminate\Support\Fluent $command
  131. * @return string
  132. */
  133. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  134. {
  135. $columns = $this->wrapArray($command->columns);
  136. $table = $this->wrapTable($blueprint);
  137. return 'alter table '.$table.' drop column '.implode(', ', $columns);
  138. }
  139. /**
  140. * Compile a drop primary key command.
  141. *
  142. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  143. * @param \Illuminate\Support\Fluent $command
  144. * @return string
  145. */
  146. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  147. {
  148. $table = $this->wrapTable($blueprint);
  149. return "alter table {$table} drop constraint {$command->index}";
  150. }
  151. /**
  152. * Compile a drop unique key command.
  153. *
  154. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  155. * @param \Illuminate\Support\Fluent $command
  156. * @return string
  157. */
  158. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  159. {
  160. $table = $this->wrapTable($blueprint);
  161. return "drop index {$command->index} on {$table}";
  162. }
  163. /**
  164. * Compile a drop index command.
  165. *
  166. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  167. * @param \Illuminate\Support\Fluent $command
  168. * @return string
  169. */
  170. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  171. {
  172. $table = $this->wrapTable($blueprint);
  173. return "drop index {$command->index} on {$table}";
  174. }
  175. /**
  176. * Compile a drop foreign key command.
  177. *
  178. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  179. * @param \Illuminate\Support\Fluent $command
  180. * @return string
  181. */
  182. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  183. {
  184. $table = $this->wrapTable($blueprint);
  185. return "alter table {$table} drop constraint {$command->index}";
  186. }
  187. /**
  188. * Compile a rename table command.
  189. *
  190. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  191. * @param \Illuminate\Support\Fluent $command
  192. * @return string
  193. */
  194. public function compileRename(Blueprint $blueprint, Fluent $command)
  195. {
  196. $from = $this->wrapTable($blueprint);
  197. return "sp_rename {$from}, ".$this->wrapTable($command->to);
  198. }
  199. /**
  200. * Create the column definition for a char type.
  201. *
  202. * @param \Illuminate\Support\Fluent $column
  203. * @return string
  204. */
  205. protected function typeChar(Fluent $column)
  206. {
  207. return "nchar({$column->length})";
  208. }
  209. /**
  210. * Create the column definition for a string type.
  211. *
  212. * @param \Illuminate\Support\Fluent $column
  213. * @return string
  214. */
  215. protected function typeString(Fluent $column)
  216. {
  217. return "nvarchar({$column->length})";
  218. }
  219. /**
  220. * Create the column definition for a text type.
  221. *
  222. * @param \Illuminate\Support\Fluent $column
  223. * @return string
  224. */
  225. protected function typeText(Fluent $column)
  226. {
  227. return 'nvarchar(max)';
  228. }
  229. /**
  230. * Create the column definition for a medium text type.
  231. *
  232. * @param \Illuminate\Support\Fluent $column
  233. * @return string
  234. */
  235. protected function typeMediumText(Fluent $column)
  236. {
  237. return 'nvarchar(max)';
  238. }
  239. /**
  240. * Create the column definition for a long text type.
  241. *
  242. * @param \Illuminate\Support\Fluent $column
  243. * @return string
  244. */
  245. protected function typeLongText(Fluent $column)
  246. {
  247. return 'nvarchar(max)';
  248. }
  249. /**
  250. * Create the column definition for a integer type.
  251. *
  252. * @param \Illuminate\Support\Fluent $column
  253. * @return string
  254. */
  255. protected function typeInteger(Fluent $column)
  256. {
  257. return 'int';
  258. }
  259. /**
  260. * Create the column definition for a big integer type.
  261. *
  262. * @param \Illuminate\Support\Fluent $column
  263. * @return string
  264. */
  265. protected function typeBigInteger(Fluent $column)
  266. {
  267. return 'bigint';
  268. }
  269. /**
  270. * Create the column definition for a medium integer type.
  271. *
  272. * @param \Illuminate\Support\Fluent $column
  273. * @return string
  274. */
  275. protected function typeMediumInteger(Fluent $column)
  276. {
  277. return 'int';
  278. }
  279. /**
  280. * Create the column definition for a tiny integer type.
  281. *
  282. * @param \Illuminate\Support\Fluent $column
  283. * @return string
  284. */
  285. protected function typeTinyInteger(Fluent $column)
  286. {
  287. return 'tinyint';
  288. }
  289. /**
  290. * Create the column definition for a small integer type.
  291. *
  292. * @param \Illuminate\Support\Fluent $column
  293. * @return string
  294. */
  295. protected function typeSmallInteger(Fluent $column)
  296. {
  297. return 'smallint';
  298. }
  299. /**
  300. * Create the column definition for a float type.
  301. *
  302. * @param \Illuminate\Support\Fluent $column
  303. * @return string
  304. */
  305. protected function typeFloat(Fluent $column)
  306. {
  307. return 'float';
  308. }
  309. /**
  310. * Create the column definition for a double type.
  311. *
  312. * @param \Illuminate\Support\Fluent $column
  313. * @return string
  314. */
  315. protected function typeDouble(Fluent $column)
  316. {
  317. return 'float';
  318. }
  319. /**
  320. * Create the column definition for a decimal type.
  321. *
  322. * @param \Illuminate\Support\Fluent $column
  323. * @return string
  324. */
  325. protected function typeDecimal(Fluent $column)
  326. {
  327. return "decimal({$column->total}, {$column->places})";
  328. }
  329. /**
  330. * Create the column definition for a boolean type.
  331. *
  332. * @param \Illuminate\Support\Fluent $column
  333. * @return string
  334. */
  335. protected function typeBoolean(Fluent $column)
  336. {
  337. return 'bit';
  338. }
  339. /**
  340. * Create the column definition for an enum type.
  341. *
  342. * @param \Illuminate\Support\Fluent $column
  343. * @return string
  344. */
  345. protected function typeEnum(Fluent $column)
  346. {
  347. return 'nvarchar(255)';
  348. }
  349. /**
  350. * Create the column definition for a json type.
  351. *
  352. * @param \Illuminate\Support\Fluent $column
  353. * @return string
  354. */
  355. protected function typeJson(Fluent $column)
  356. {
  357. return 'nvarchar(max)';
  358. }
  359. /**
  360. * Create the column definition for a jsonb type.
  361. *
  362. * @param \Illuminate\Support\Fluent $column
  363. * @return string
  364. */
  365. protected function typeJsonb(Fluent $column)
  366. {
  367. return 'nvarchar(max)';
  368. }
  369. /**
  370. * Create the column definition for a date type.
  371. *
  372. * @param \Illuminate\Support\Fluent $column
  373. * @return string
  374. */
  375. protected function typeDate(Fluent $column)
  376. {
  377. return 'date';
  378. }
  379. /**
  380. * Create the column definition for a date-time type.
  381. *
  382. * @param \Illuminate\Support\Fluent $column
  383. * @return string
  384. */
  385. protected function typeDateTime(Fluent $column)
  386. {
  387. return 'datetime';
  388. }
  389. /**
  390. * Create the column definition for a date-time type.
  391. *
  392. * @param \Illuminate\Support\Fluent $column
  393. * @return string
  394. */
  395. protected function typeDateTimeTz(Fluent $column)
  396. {
  397. return 'datetimeoffset(0)';
  398. }
  399. /**
  400. * Create the column definition for a time type.
  401. *
  402. * @param \Illuminate\Support\Fluent $column
  403. * @return string
  404. */
  405. protected function typeTime(Fluent $column)
  406. {
  407. return 'time';
  408. }
  409. /**
  410. * Create the column definition for a time type.
  411. *
  412. * @param \Illuminate\Support\Fluent $column
  413. * @return string
  414. */
  415. protected function typeTimeTz(Fluent $column)
  416. {
  417. return 'time';
  418. }
  419. /**
  420. * Create the column definition for a timestamp type.
  421. *
  422. * @param \Illuminate\Support\Fluent $column
  423. * @return string
  424. */
  425. protected function typeTimestamp(Fluent $column)
  426. {
  427. return 'datetime';
  428. }
  429. /**
  430. * Create the column definition for a timestamp type.
  431. *
  432. * @link https://msdn.microsoft.com/en-us/library/bb630289(v=sql.120).aspx
  433. *
  434. * @param \Illuminate\Support\Fluent $column
  435. * @return string
  436. */
  437. protected function typeTimestampTz(Fluent $column)
  438. {
  439. return 'datetimeoffset(0)';
  440. }
  441. /**
  442. * Create the column definition for a binary type.
  443. *
  444. * @param \Illuminate\Support\Fluent $column
  445. * @return string
  446. */
  447. protected function typeBinary(Fluent $column)
  448. {
  449. return 'varbinary(max)';
  450. }
  451. /**
  452. * Get the SQL for a nullable column modifier.
  453. *
  454. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  455. * @param \Illuminate\Support\Fluent $column
  456. * @return string|null
  457. */
  458. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  459. {
  460. return $column->nullable ? ' null' : ' not null';
  461. }
  462. /**
  463. * Get the SQL for a default column modifier.
  464. *
  465. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  466. * @param \Illuminate\Support\Fluent $column
  467. * @return string|null
  468. */
  469. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  470. {
  471. if (!is_null($column->default)) {
  472. return ' default '.$this->getDefaultValue($column->default);
  473. }
  474. }
  475. /**
  476. * Get the SQL for an auto-increment column modifier.
  477. *
  478. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  479. * @param \Illuminate\Support\Fluent $column
  480. * @return string|null
  481. */
  482. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  483. {
  484. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  485. return ' identity primary key';
  486. }
  487. }
  488. }