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

https://bitbucket.org/larryg/powerhut · PHP · 435 lines · 159 code · 55 blank · 221 comment · 2 complexity · 236578ef261df6cdc2de6b3377c97752 MD5 · raw file

  1. <?php namespace Illuminate\Database\Schema\Grammars;
  2. use Illuminate\Support\Fluent;
  3. use Illuminate\Database\Schema\Blueprint;
  4. class SqlServerGrammar extends Grammar {
  5. /**
  6. * The keyword identifier wrapper format.
  7. *
  8. * @var string
  9. */
  10. protected $wrapper = '"%s"';
  11. /**
  12. * The possible column modifiers.
  13. *
  14. * @var array
  15. */
  16. protected $modifiers = array('Increment', 'Nullable', 'Default');
  17. /**
  18. * The columns available as serials.
  19. *
  20. * @var array
  21. */
  22. protected $serials = array('bigInteger', 'integer');
  23. /**
  24. * Compile the query to determine if a table exists.
  25. *
  26. * @return string
  27. */
  28. public function compileTableExists()
  29. {
  30. return "select * from sysobjects where type = 'U' and name = ?";
  31. }
  32. /**
  33. * Compile a create table command.
  34. *
  35. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  36. * @param \Illuminate\Support\Fluent $command
  37. * @return string
  38. */
  39. public function compileCreate(Blueprint $blueprint, Fluent $command)
  40. {
  41. $columns = implode(', ', $this->getColumns($blueprint));
  42. return 'create table '.$this->wrapTable($blueprint)." ($columns)";
  43. }
  44. /**
  45. * Compile a create table command.
  46. *
  47. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  48. * @param \Illuminate\Support\Fluent $command
  49. * @return string
  50. */
  51. public function compileAdd(Blueprint $blueprint, Fluent $command)
  52. {
  53. $table = $this->wrapTable($blueprint);
  54. $columns = $this->getColumns($blueprint);
  55. return 'alter table '.$table.' add '.implode(', ', $columns);
  56. }
  57. /**
  58. * Compile a primary key command.
  59. *
  60. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  61. * @param \Illuminate\Support\Fluent $command
  62. * @return string
  63. */
  64. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  65. {
  66. $columns = $this->columnize($command->columns);
  67. $table = $this->wrapTable($blueprint);
  68. return "alter table {$table} add constraint {$command->index} primary key ({$columns})";
  69. }
  70. /**
  71. * Compile a unique key command.
  72. *
  73. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  74. * @param \Illuminate\Support\Fluent $command
  75. * @return string
  76. */
  77. public function compileUnique(Blueprint $blueprint, Fluent $command)
  78. {
  79. $columns = $this->columnize($command->columns);
  80. $table = $this->wrapTable($blueprint);
  81. return "create unique index {$command->index} on {$table} ({$columns})";
  82. }
  83. /**
  84. * Compile a plain index key command.
  85. *
  86. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  87. * @param \Illuminate\Support\Fluent $command
  88. * @return string
  89. */
  90. public function compileIndex(Blueprint $blueprint, Fluent $command)
  91. {
  92. $columns = $this->columnize($command->columns);
  93. $table = $this->wrapTable($blueprint);
  94. return "create index {$command->index} on {$table} ({$columns})";
  95. }
  96. /**
  97. * Compile a drop table command.
  98. *
  99. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  100. * @param \Illuminate\Support\Fluent $command
  101. * @return string
  102. */
  103. public function compileDrop(Blueprint $blueprint, Fluent $command)
  104. {
  105. return 'drop table '.$this->wrapTable($blueprint);
  106. }
  107. /**
  108. * Compile a drop column command.
  109. *
  110. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  111. * @param \Illuminate\Support\Fluent $command
  112. * @return string
  113. */
  114. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  115. {
  116. $columns = $this->wrapArray($command->columns);
  117. $table = $this->wrapTable($blueprint);
  118. return 'alter table '.$table.' drop column '.implode(', ', $columns);
  119. }
  120. /**
  121. * Compile a drop primary key command.
  122. *
  123. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  124. * @param \Illuminate\Support\Fluent $command
  125. * @return string
  126. */
  127. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  128. {
  129. $table = $blueprint->getTable();
  130. $table = $this->wrapTable($blueprint);
  131. return "alter table {$table} drop constraint {$command->index}";
  132. }
  133. /**
  134. * Compile a drop unique key command.
  135. *
  136. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  137. * @param \Illuminate\Support\Fluent $command
  138. * @return string
  139. */
  140. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  141. {
  142. $table = $this->wrapTable($blueprint);
  143. return "drop index {$command->index} on {$table}";
  144. }
  145. /**
  146. * Compile a drop index command.
  147. *
  148. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  149. * @param \Illuminate\Support\Fluent $command
  150. * @return string
  151. */
  152. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  153. {
  154. $table = $this->wrapTable($blueprint);
  155. return "drop index {$command->index} on {$table}";
  156. }
  157. /**
  158. * Compile a drop foreign key command.
  159. *
  160. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  161. * @param \Illuminate\Support\Fluent $command
  162. * @return string
  163. */
  164. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  165. {
  166. $table = $this->wrapTable($blueprint);
  167. return "alter table {$table} drop constraint {$command->index}";
  168. }
  169. /**
  170. * Compile a rename table command.
  171. *
  172. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  173. * @param \Illuminate\Support\Fluent $command
  174. * @return string
  175. */
  176. public function compileRename(Blueprint $blueprint, Fluent $command)
  177. {
  178. $from = $this->wrapTable($blueprint);
  179. return "sp_rename {$from}, ".$this->wrapTable($command->to);
  180. }
  181. /**
  182. * Create the column definition for a string type.
  183. *
  184. * @param \Illuminate\Support\Fluent $column
  185. * @return string
  186. */
  187. protected function typeString(Fluent $column)
  188. {
  189. return "nvarchar({$column->length})";
  190. }
  191. /**
  192. * Create the column definition for a text type.
  193. *
  194. * @param \Illuminate\Support\Fluent $column
  195. * @return string
  196. */
  197. protected function typeText(Fluent $column)
  198. {
  199. return 'nvarchar(max)';
  200. }
  201. /**
  202. * Create the column definition for a integer type.
  203. *
  204. * @param \Illuminate\Support\Fluent $column
  205. * @return string
  206. */
  207. protected function typeInteger(Fluent $column)
  208. {
  209. return 'int';
  210. }
  211. /**
  212. * Create the column definition for a big integer type.
  213. *
  214. * @param \Illuminate\Support\Fluent $column
  215. * @return string
  216. */
  217. protected function typeBigInteger(Fluent $column)
  218. {
  219. return 'bigint';
  220. }
  221. /**
  222. * Create the column definition for a medium integer type.
  223. *
  224. * @param \Illuminate\Support\Fluent $column
  225. * @return string
  226. */
  227. protected function typeMediumInteger(Fluent $column)
  228. {
  229. return 'int';
  230. }
  231. /**
  232. * Create the column definition for a tiny integer type.
  233. *
  234. * @param \Illuminate\Support\Fluent $column
  235. * @return string
  236. */
  237. protected function typeTinyInteger(Fluent $column)
  238. {
  239. return 'tinyint';
  240. }
  241. /**
  242. * Create the column definition for a small integer type.
  243. *
  244. * @param \Illuminate\Support\Fluent $column
  245. * @return string
  246. */
  247. protected function typeSmallInteger(Fluent $column)
  248. {
  249. return 'smallint';
  250. }
  251. /**
  252. * Create the column definition for a float type.
  253. *
  254. * @param \Illuminate\Support\Fluent $column
  255. * @return string
  256. */
  257. protected function typeFloat(Fluent $column)
  258. {
  259. return 'float';
  260. }
  261. /**
  262. * Create the column definition for a decimal type.
  263. *
  264. * @param \Illuminate\Support\Fluent $column
  265. * @return string
  266. */
  267. protected function typeDecimal(Fluent $column)
  268. {
  269. return "decimal({$column->total}, {$column->places})";
  270. }
  271. /**
  272. * Create the column definition for a boolean type.
  273. *
  274. * @param \Illuminate\Support\Fluent $column
  275. * @return string
  276. */
  277. protected function typeBoolean(Fluent $column)
  278. {
  279. return 'tinyint';
  280. }
  281. /**
  282. * Create the column definition for a enum type.
  283. *
  284. * @param \Illuminate\Support\Fluent $column
  285. * @return string
  286. */
  287. protected function typeEnum(Fluent $column)
  288. {
  289. return 'nvarchar(255)';
  290. }
  291. /**
  292. * Create the column definition for a date type.
  293. *
  294. * @param \Illuminate\Support\Fluent $column
  295. * @return string
  296. */
  297. protected function typeDate(Fluent $column)
  298. {
  299. return 'date';
  300. }
  301. /**
  302. * Create the column definition for a date-time type.
  303. *
  304. * @param \Illuminate\Support\Fluent $column
  305. * @return string
  306. */
  307. protected function typeDateTime(Fluent $column)
  308. {
  309. return 'datetime';
  310. }
  311. /**
  312. * Create the column definition for a time type.
  313. *
  314. * @param \Illuminate\Support\Fluent $column
  315. * @return string
  316. */
  317. protected function typeTime(Fluent $column)
  318. {
  319. return 'time';
  320. }
  321. /**
  322. * Create the column definition for a timestamp type.
  323. *
  324. * @param \Illuminate\Support\Fluent $column
  325. * @return string
  326. */
  327. protected function typeTimestamp(Fluent $column)
  328. {
  329. return 'datetime';
  330. }
  331. /**
  332. * Create the column definition for a binary type.
  333. *
  334. * @param \Illuminate\Support\Fluent $column
  335. * @return string
  336. */
  337. protected function typeBinary(Fluent $column)
  338. {
  339. return 'varbinary(max)';
  340. }
  341. /**
  342. * Get the SQL for a nullable column modifier.
  343. *
  344. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  345. * @param \Illuminate\Support\Fluent $column
  346. * @return string|null
  347. */
  348. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  349. {
  350. return $column->nullable ? ' null' : ' not null';
  351. }
  352. /**
  353. * Get the SQL for a default column modifier.
  354. *
  355. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  356. * @param \Illuminate\Support\Fluent $column
  357. * @return string|null
  358. */
  359. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  360. {
  361. if ( ! is_null($column->default))
  362. {
  363. return " default ".$this->getDefaultValue($column->default);
  364. }
  365. }
  366. /**
  367. * Get the SQL for an auto-increment column modifier.
  368. *
  369. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  370. * @param \Illuminate\Support\Fluent $column
  371. * @return string|null
  372. */
  373. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  374. {
  375. if (in_array($column->type, $this->serials) and $column->autoIncrement)
  376. {
  377. return ' identity primary key';
  378. }
  379. }
  380. }