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

https://gitlab.com/ealexis.t/trends · PHP · 622 lines · 235 code · 76 blank · 311 comment · 5 complexity · e5ef9f51993092e3b77b649be934c92c MD5 · raw file

  1. <?php
  2. namespace Illuminate\Database\Schema\Grammars;
  3. use Illuminate\Support\Fluent;
  4. use Illuminate\Database\Schema\Blueprint;
  5. class PostgresGrammar 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', 'mediumInteger', 'smallInteger', 'tinyInteger'];
  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 information_schema.tables where table_schema = ? and table_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 column_name from information_schema.columns where table_name = '$table'";
  37. }
  38. /**
  39. * Compile a create table command.
  40. *
  41. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  42. * @param \Illuminate\Support\Fluent $command
  43. * @return string
  44. */
  45. public function compileCreate(Blueprint $blueprint, Fluent $command)
  46. {
  47. $columns = implode(', ', $this->getColumns($blueprint));
  48. $sql = $blueprint->temporary ? 'create temporary' : 'create';
  49. $sql .= ' table '.$this->wrapTable($blueprint)." ($columns)";
  50. return $sql;
  51. }
  52. /**
  53. * Compile a column addition 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->prefixArray('add column', $this->getColumns($blueprint));
  63. return 'alter table '.$table.' '.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. return 'alter table '.$this->wrapTable($blueprint)." add primary key ({$columns})";
  76. }
  77. /**
  78. * Compile a unique key command.
  79. *
  80. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  81. * @param \Illuminate\Support\Fluent $command
  82. * @return string
  83. */
  84. public function compileUnique(Blueprint $blueprint, Fluent $command)
  85. {
  86. $table = $this->wrapTable($blueprint);
  87. $index = $this->wrap($command->index);
  88. $columns = $this->columnize($command->columns);
  89. return "alter table $table add constraint {$index} unique ($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. $index = $this->wrap($command->index);
  102. return "create index {$index} on ".$this->wrapTable($blueprint)." ({$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 'drop table if exists '.$this->wrapTable($blueprint);
  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->prefixArray('drop column', $this->wrapArray($command->columns));
  136. $table = $this->wrapTable($blueprint);
  137. return 'alter table '.$table.' '.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 = $blueprint->getTable();
  149. $index = $this->wrap("{$table}_pkey");
  150. return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}";
  151. }
  152. /**
  153. * Compile a drop unique key command.
  154. *
  155. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  156. * @param \Illuminate\Support\Fluent $command
  157. * @return string
  158. */
  159. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  160. {
  161. $table = $this->wrapTable($blueprint);
  162. $index = $this->wrap($command->index);
  163. return "alter table {$table} drop constraint {$index}";
  164. }
  165. /**
  166. * Compile a drop index command.
  167. *
  168. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  169. * @param \Illuminate\Support\Fluent $command
  170. * @return string
  171. */
  172. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  173. {
  174. $index = $this->wrap($command->index);
  175. return "drop index {$index}";
  176. }
  177. /**
  178. * Compile a drop foreign key command.
  179. *
  180. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  181. * @param \Illuminate\Support\Fluent $command
  182. * @return string
  183. */
  184. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  185. {
  186. $table = $this->wrapTable($blueprint);
  187. $index = $this->wrap($command->index);
  188. return "alter table {$table} drop constraint {$index}";
  189. }
  190. /**
  191. * Compile the command to enable foreign key constraints.
  192. *
  193. * @return string
  194. */
  195. public function compileEnableForeignKeyConstraints()
  196. {
  197. return 'SET CONSTRAINTS ALL IMMEDIATE;';
  198. }
  199. /**
  200. * Compile the command to disable foreign key constraints.
  201. *
  202. * @return string
  203. */
  204. public function compileDisableForeignKeyConstraints()
  205. {
  206. return 'SET CONSTRAINTS ALL DEFERRED;';
  207. }
  208. /**
  209. * Compile a rename table command.
  210. *
  211. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  212. * @param \Illuminate\Support\Fluent $command
  213. * @return string
  214. */
  215. public function compileRename(Blueprint $blueprint, Fluent $command)
  216. {
  217. $from = $this->wrapTable($blueprint);
  218. return "alter table {$from} rename to ".$this->wrapTable($command->to);
  219. }
  220. /**
  221. * Create the column definition for a char type.
  222. *
  223. * @param \Illuminate\Support\Fluent $column
  224. * @return string
  225. */
  226. protected function typeChar(Fluent $column)
  227. {
  228. return "char({$column->length})";
  229. }
  230. /**
  231. * Create the column definition for a string type.
  232. *
  233. * @param \Illuminate\Support\Fluent $column
  234. * @return string
  235. */
  236. protected function typeString(Fluent $column)
  237. {
  238. return "varchar({$column->length})";
  239. }
  240. /**
  241. * Create the column definition for a text type.
  242. *
  243. * @param \Illuminate\Support\Fluent $column
  244. * @return string
  245. */
  246. protected function typeText(Fluent $column)
  247. {
  248. return 'text';
  249. }
  250. /**
  251. * Create the column definition for a medium text type.
  252. *
  253. * @param \Illuminate\Support\Fluent $column
  254. * @return string
  255. */
  256. protected function typeMediumText(Fluent $column)
  257. {
  258. return 'text';
  259. }
  260. /**
  261. * Create the column definition for a long text type.
  262. *
  263. * @param \Illuminate\Support\Fluent $column
  264. * @return string
  265. */
  266. protected function typeLongText(Fluent $column)
  267. {
  268. return 'text';
  269. }
  270. /**
  271. * Create the column definition for a integer type.
  272. *
  273. * @param \Illuminate\Support\Fluent $column
  274. * @return string
  275. */
  276. protected function typeInteger(Fluent $column)
  277. {
  278. return $column->autoIncrement ? 'serial' : 'integer';
  279. }
  280. /**
  281. * Create the column definition for a big integer type.
  282. *
  283. * @param \Illuminate\Support\Fluent $column
  284. * @return string
  285. */
  286. protected function typeBigInteger(Fluent $column)
  287. {
  288. return $column->autoIncrement ? 'bigserial' : 'bigint';
  289. }
  290. /**
  291. * Create the column definition for a medium integer type.
  292. *
  293. * @param \Illuminate\Support\Fluent $column
  294. * @return string
  295. */
  296. protected function typeMediumInteger(Fluent $column)
  297. {
  298. return $column->autoIncrement ? 'serial' : 'integer';
  299. }
  300. /**
  301. * Create the column definition for a tiny integer type.
  302. *
  303. * @param \Illuminate\Support\Fluent $column
  304. * @return string
  305. */
  306. protected function typeTinyInteger(Fluent $column)
  307. {
  308. return $column->autoIncrement ? 'smallserial' : 'smallint';
  309. }
  310. /**
  311. * Create the column definition for a small integer type.
  312. *
  313. * @param \Illuminate\Support\Fluent $column
  314. * @return string
  315. */
  316. protected function typeSmallInteger(Fluent $column)
  317. {
  318. return $column->autoIncrement ? 'smallserial' : 'smallint';
  319. }
  320. /**
  321. * Create the column definition for a float type.
  322. *
  323. * @param \Illuminate\Support\Fluent $column
  324. * @return string
  325. */
  326. protected function typeFloat(Fluent $column)
  327. {
  328. return $this->typeDouble($column);
  329. }
  330. /**
  331. * Create the column definition for a double type.
  332. *
  333. * @param \Illuminate\Support\Fluent $column
  334. * @return string
  335. */
  336. protected function typeDouble(Fluent $column)
  337. {
  338. return 'double precision';
  339. }
  340. /**
  341. * Create the column definition for a decimal type.
  342. *
  343. * @param \Illuminate\Support\Fluent $column
  344. * @return string
  345. */
  346. protected function typeDecimal(Fluent $column)
  347. {
  348. return "decimal({$column->total}, {$column->places})";
  349. }
  350. /**
  351. * Create the column definition for a boolean type.
  352. *
  353. * @param \Illuminate\Support\Fluent $column
  354. * @return string
  355. */
  356. protected function typeBoolean(Fluent $column)
  357. {
  358. return 'boolean';
  359. }
  360. /**
  361. * Create the column definition for an enum type.
  362. *
  363. * @param \Illuminate\Support\Fluent $column
  364. * @return string
  365. */
  366. protected function typeEnum(Fluent $column)
  367. {
  368. $allowed = array_map(function ($a) {
  369. return "'".$a."'";
  370. }, $column->allowed);
  371. return "varchar(255) check (\"{$column->name}\" in (".implode(', ', $allowed).'))';
  372. }
  373. /**
  374. * Create the column definition for a json type.
  375. *
  376. * @param \Illuminate\Support\Fluent $column
  377. * @return string
  378. */
  379. protected function typeJson(Fluent $column)
  380. {
  381. return 'json';
  382. }
  383. /**
  384. * Create the column definition for a jsonb type.
  385. *
  386. * @param \Illuminate\Support\Fluent $column
  387. * @return string
  388. */
  389. protected function typeJsonb(Fluent $column)
  390. {
  391. return 'jsonb';
  392. }
  393. /**
  394. * Create the column definition for a date type.
  395. *
  396. * @param \Illuminate\Support\Fluent $column
  397. * @return string
  398. */
  399. protected function typeDate(Fluent $column)
  400. {
  401. return 'date';
  402. }
  403. /**
  404. * Create the column definition for a date-time type.
  405. *
  406. * @param \Illuminate\Support\Fluent $column
  407. * @return string
  408. */
  409. protected function typeDateTime(Fluent $column)
  410. {
  411. return 'timestamp(0) without time zone';
  412. }
  413. /**
  414. * Create the column definition for a date-time type.
  415. *
  416. * @param \Illuminate\Support\Fluent $column
  417. * @return string
  418. */
  419. protected function typeDateTimeTz(Fluent $column)
  420. {
  421. return 'timestamp(0) with time zone';
  422. }
  423. /**
  424. * Create the column definition for a time type.
  425. *
  426. * @param \Illuminate\Support\Fluent $column
  427. * @return string
  428. */
  429. protected function typeTime(Fluent $column)
  430. {
  431. return 'time(0) without time zone';
  432. }
  433. /**
  434. * Create the column definition for a time type.
  435. *
  436. * @param \Illuminate\Support\Fluent $column
  437. * @return string
  438. */
  439. protected function typeTimeTz(Fluent $column)
  440. {
  441. return 'time(0) with time zone';
  442. }
  443. /**
  444. * Create the column definition for a timestamp type.
  445. *
  446. * @param \Illuminate\Support\Fluent $column
  447. * @return string
  448. */
  449. protected function typeTimestamp(Fluent $column)
  450. {
  451. if ($column->useCurrent) {
  452. return 'timestamp(0) without time zone default CURRENT_TIMESTAMP(0)';
  453. }
  454. return 'timestamp(0) without time zone';
  455. }
  456. /**
  457. * Create the column definition for a timestamp type.
  458. *
  459. * @param \Illuminate\Support\Fluent $column
  460. * @return string
  461. */
  462. protected function typeTimestampTz(Fluent $column)
  463. {
  464. if ($column->useCurrent) {
  465. return 'timestamp(0) with time zone default CURRENT_TIMESTAMP(0)';
  466. }
  467. return 'timestamp(0) with time zone';
  468. }
  469. /**
  470. * Create the column definition for a binary type.
  471. *
  472. * @param \Illuminate\Support\Fluent $column
  473. * @return string
  474. */
  475. protected function typeBinary(Fluent $column)
  476. {
  477. return 'bytea';
  478. }
  479. /**
  480. * Create the column definition for a uuid type.
  481. *
  482. * @param \Illuminate\Support\Fluent $column
  483. * @return string
  484. */
  485. protected function typeUuid(Fluent $column)
  486. {
  487. return 'uuid';
  488. }
  489. /**
  490. * Create the column definition for an IP address type.
  491. *
  492. * @param \Illuminate\Support\Fluent $column
  493. * @return string
  494. */
  495. protected function typeIpAddress(Fluent $column)
  496. {
  497. return 'inet';
  498. }
  499. /**
  500. * Create the column definition for a MAC address type.
  501. *
  502. * @param \Illuminate\Support\Fluent $column
  503. * @return string
  504. */
  505. protected function typeMacAddress(Fluent $column)
  506. {
  507. return 'macaddr';
  508. }
  509. /**
  510. * Get the SQL for a nullable column modifier.
  511. *
  512. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  513. * @param \Illuminate\Support\Fluent $column
  514. * @return string|null
  515. */
  516. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  517. {
  518. return $column->nullable ? ' null' : ' not null';
  519. }
  520. /**
  521. * Get the SQL for a default column modifier.
  522. *
  523. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  524. * @param \Illuminate\Support\Fluent $column
  525. * @return string|null
  526. */
  527. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  528. {
  529. if (! is_null($column->default)) {
  530. return ' default '.$this->getDefaultValue($column->default);
  531. }
  532. }
  533. /**
  534. * Get the SQL for an auto-increment column modifier.
  535. *
  536. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  537. * @param \Illuminate\Support\Fluent $column
  538. * @return string|null
  539. */
  540. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  541. {
  542. if (in_array($column->type, $this->serials) && $column->autoIncrement) {
  543. return ' primary key';
  544. }
  545. }
  546. }