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

https://bitbucket.org/helfreire/tccsite · PHP · 474 lines · 172 code · 56 blank · 246 comment · 2 complexity · 19c390bdbb9481d8b26cb2b6149bcffd MD5 · raw file

  1. <?php namespace Illuminate\Database\Schema\Grammars;
  2. use Illuminate\Support\Fluent;
  3. use Illuminate\Database\Schema\Blueprint;
  4. class PostgresGrammar 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 information_schema.tables where table_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->prefixArray('add column', $this->getColumns($blueprint));
  55. return 'alter table '.$table.' '.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. return 'alter table '.$this->wrapTable($blueprint)." add primary key ({$columns})";
  68. }
  69. /**
  70. * Compile a unique key command.
  71. *
  72. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  73. * @param \Illuminate\Support\Fluent $command
  74. * @return string
  75. */
  76. public function compileUnique(Blueprint $blueprint, Fluent $command)
  77. {
  78. $table = $this->wrapTable($blueprint);
  79. $columns = $this->columnize($command->columns);
  80. return "alter table $table add constraint {$command->index} unique ($columns)";
  81. }
  82. /**
  83. * Compile a plain index key command.
  84. *
  85. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  86. * @param \Illuminate\Support\Fluent $command
  87. * @return string
  88. */
  89. public function compileIndex(Blueprint $blueprint, Fluent $command)
  90. {
  91. $columns = $this->columnize($command->columns);
  92. return "create index {$command->index} on ".$this->wrapTable($blueprint)." ({$columns})";
  93. }
  94. /**
  95. * Compile a drop table command.
  96. *
  97. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  98. * @param \Illuminate\Support\Fluent $command
  99. * @return string
  100. */
  101. public function compileDrop(Blueprint $blueprint, Fluent $command)
  102. {
  103. return 'drop table '.$this->wrapTable($blueprint);
  104. }
  105. /**
  106. * Compile a drop table (if exists) command.
  107. *
  108. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  109. * @param \Illuminate\Support\Fluent $command
  110. * @return string
  111. */
  112. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  113. {
  114. return 'drop table if exists '.$this->wrapTable($blueprint);
  115. }
  116. /**
  117. * Compile a drop column command.
  118. *
  119. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  120. * @param \Illuminate\Support\Fluent $command
  121. * @return string
  122. */
  123. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  124. {
  125. $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns));
  126. $table = $this->wrapTable($blueprint);
  127. return 'alter table '.$table.' '.implode(', ', $columns);
  128. }
  129. /**
  130. * Compile a drop primary key command.
  131. *
  132. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  133. * @param \Illuminate\Support\Fluent $command
  134. * @return string
  135. */
  136. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  137. {
  138. $table = $blueprint->getTable();
  139. return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$table}_pkey";
  140. }
  141. /**
  142. * Compile a drop unique key command.
  143. *
  144. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  145. * @param \Illuminate\Support\Fluent $command
  146. * @return string
  147. */
  148. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  149. {
  150. $table = $this->wrapTable($blueprint);
  151. return "alter table {$table} drop constraint {$command->index}";
  152. }
  153. /**
  154. * Compile a drop index command.
  155. *
  156. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  157. * @param \Illuminate\Support\Fluent $command
  158. * @return string
  159. */
  160. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  161. {
  162. return "drop index {$command->index}";
  163. }
  164. /**
  165. * Compile a drop foreign key command.
  166. *
  167. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  168. * @param \Illuminate\Support\Fluent $command
  169. * @return string
  170. */
  171. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  172. {
  173. $table = $this->wrapTable($blueprint);
  174. return "alter table {$table} drop constraint {$command->index}";
  175. }
  176. /**
  177. * Compile a rename table command.
  178. *
  179. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  180. * @param \Illuminate\Support\Fluent $command
  181. * @return string
  182. */
  183. public function compileRename(Blueprint $blueprint, Fluent $command)
  184. {
  185. $from = $this->wrapTable($blueprint);
  186. return "alter table {$from} rename to ".$this->wrapTable($command->to);
  187. }
  188. /**
  189. * Create the column definition for a string type.
  190. *
  191. * @param \Illuminate\Support\Fluent $column
  192. * @return string
  193. */
  194. protected function typeString(Fluent $column)
  195. {
  196. return "varchar({$column->length})";
  197. }
  198. /**
  199. * Create the column definition for a text type.
  200. *
  201. * @param \Illuminate\Support\Fluent $column
  202. * @return string
  203. */
  204. protected function typeText(Fluent $column)
  205. {
  206. return 'text';
  207. }
  208. /**
  209. * Create the column definition for a medium text type.
  210. *
  211. * @param \Illuminate\Support\Fluent $column
  212. * @return string
  213. */
  214. protected function typeMediumText(Fluent $column)
  215. {
  216. return 'text';
  217. }
  218. /**
  219. * Create the column definition for a long text type.
  220. *
  221. * @param \Illuminate\Support\Fluent $column
  222. * @return string
  223. */
  224. protected function typeLongText(Fluent $column)
  225. {
  226. return 'text';
  227. }
  228. /**
  229. * Create the column definition for a integer type.
  230. *
  231. * @param \Illuminate\Support\Fluent $column
  232. * @return string
  233. */
  234. protected function typeInteger(Fluent $column)
  235. {
  236. return $column->autoIncrement ? 'serial' : 'integer';
  237. }
  238. /**
  239. * Create the column definition for a big integer type.
  240. *
  241. * @param \Illuminate\Support\Fluent $column
  242. * @return string
  243. */
  244. protected function typeBigInteger(Fluent $column)
  245. {
  246. return $column->autoIncrement ? 'bigserial' : 'bigint';
  247. }
  248. /**
  249. * Create the column definition for a medium integer type.
  250. *
  251. * @param \Illuminate\Support\Fluent $column
  252. * @return string
  253. */
  254. protected function typeMediumInteger(Fluent $column)
  255. {
  256. return 'integer';
  257. }
  258. /**
  259. * Create the column definition for a tiny integer type.
  260. *
  261. * @param \Illuminate\Support\Fluent $column
  262. * @return string
  263. */
  264. protected function typeTinyInteger(Fluent $column)
  265. {
  266. return 'smallint';
  267. }
  268. /**
  269. * Create the column definition for a small integer type.
  270. *
  271. * @param \Illuminate\Support\Fluent $column
  272. * @return string
  273. */
  274. protected function typeSmallInteger(Fluent $column)
  275. {
  276. return 'smallint';
  277. }
  278. /**
  279. * Create the column definition for a float type.
  280. *
  281. * @param \Illuminate\Support\Fluent $column
  282. * @return string
  283. */
  284. protected function typeFloat(Fluent $column)
  285. {
  286. return 'real';
  287. }
  288. /**
  289. * Create the column definition for a double type.
  290. *
  291. * @param \Illuminate\Support\Fluent $column
  292. * @return string
  293. */
  294. protected function typeDouble(Fluent $column)
  295. {
  296. return 'double precision';
  297. }
  298. /**
  299. * Create the column definition for a decimal type.
  300. *
  301. * @param \Illuminate\Support\Fluent $column
  302. * @return string
  303. */
  304. protected function typeDecimal(Fluent $column)
  305. {
  306. return "decimal({$column->total}, {$column->places})";
  307. }
  308. /**
  309. * Create the column definition for a boolean type.
  310. *
  311. * @param \Illuminate\Support\Fluent $column
  312. * @return string
  313. */
  314. protected function typeBoolean(Fluent $column)
  315. {
  316. return 'boolean';
  317. }
  318. /**
  319. * Create the column definition for an enum type.
  320. *
  321. * @param \Illuminate\Support\Fluent $column
  322. * @return string
  323. */
  324. protected function typeEnum(Fluent $column)
  325. {
  326. $allowed = array_map(function($a) { return "'".$a."'"; }, $column->allowed);
  327. return "varchar(255) check ({$column->name} in (".implode(', ', $allowed)."))";
  328. }
  329. /**
  330. * Create the column definition for a date type.
  331. *
  332. * @param \Illuminate\Support\Fluent $column
  333. * @return string
  334. */
  335. protected function typeDate(Fluent $column)
  336. {
  337. return 'date';
  338. }
  339. /**
  340. * Create the column definition for a date-time type.
  341. *
  342. * @param \Illuminate\Support\Fluent $column
  343. * @return string
  344. */
  345. protected function typeDateTime(Fluent $column)
  346. {
  347. return 'timestamp';
  348. }
  349. /**
  350. * Create the column definition for a time type.
  351. *
  352. * @param \Illuminate\Support\Fluent $column
  353. * @return string
  354. */
  355. protected function typeTime(Fluent $column)
  356. {
  357. return 'time';
  358. }
  359. /**
  360. * Create the column definition for a timestamp type.
  361. *
  362. * @param \Illuminate\Support\Fluent $column
  363. * @return string
  364. */
  365. protected function typeTimestamp(Fluent $column)
  366. {
  367. return 'timestamp';
  368. }
  369. /**
  370. * Create the column definition for a binary type.
  371. *
  372. * @param \Illuminate\Support\Fluent $column
  373. * @return string
  374. */
  375. protected function typeBinary(Fluent $column)
  376. {
  377. return 'bytea';
  378. }
  379. /**
  380. * Get the SQL for a nullable column modifier.
  381. *
  382. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  383. * @param \Illuminate\Support\Fluent $column
  384. * @return string|null
  385. */
  386. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  387. {
  388. return $column->nullable ? ' null' : ' not null';
  389. }
  390. /**
  391. * Get the SQL for a default column modifier.
  392. *
  393. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  394. * @param \Illuminate\Support\Fluent $column
  395. * @return string|null
  396. */
  397. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  398. {
  399. if ( ! is_null($column->default))
  400. {
  401. return " default ".$this->getDefaultValue($column->default);
  402. }
  403. }
  404. /**
  405. * Get the SQL for an auto-increment column modifier.
  406. *
  407. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  408. * @param \Illuminate\Support\Fluent $column
  409. * @return string|null
  410. */
  411. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  412. {
  413. if (in_array($column->type, $this->serials) and $column->autoIncrement)
  414. {
  415. return ' primary key';
  416. }
  417. }
  418. }