PostgresGrammar.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. * If this Grammar supports schema changes wrapped in a transaction.
  9. *
  10. * @var bool
  11. */
  12. protected $transactions = true;
  13. /**
  14. * The possible column modifiers.
  15. *
  16. * @var array
  17. */
  18. protected $modifiers = ['Collate', 'Increment', 'Nullable', 'Default'];
  19. /**
  20. * The columns available as serials.
  21. *
  22. * @var array
  23. */
  24. protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
  25. /**
  26. * The commands to be executed outside of create or alter command.
  27. *
  28. * @var array
  29. */
  30. protected $fluentCommands = ['Comment'];
  31. /**
  32. * Compile the query to determine if a table exists.
  33. *
  34. * @return string
  35. */
  36. public function compileTableExists()
  37. {
  38. return "select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'";
  39. }
  40. /**
  41. * Compile the query to determine the list of columns.
  42. *
  43. * @return string
  44. */
  45. public function compileColumnListing()
  46. {
  47. return 'select column_name from information_schema.columns where table_schema = ? and table_name = ?';
  48. }
  49. /**
  50. * Compile a create table command.
  51. *
  52. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  53. * @param \Illuminate\Support\Fluent $command
  54. * @return string
  55. */
  56. public function compileCreate(Blueprint $blueprint, Fluent $command)
  57. {
  58. return sprintf('%s table %s (%s)',
  59. $blueprint->temporary ? 'create temporary' : 'create',
  60. $this->wrapTable($blueprint),
  61. implode(', ', $this->getColumns($blueprint))
  62. );
  63. }
  64. /**
  65. * Compile a column addition command.
  66. *
  67. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  68. * @param \Illuminate\Support\Fluent $command
  69. * @return string
  70. */
  71. public function compileAdd(Blueprint $blueprint, Fluent $command)
  72. {
  73. return sprintf('alter table %s %s',
  74. $this->wrapTable($blueprint),
  75. implode(', ', $this->prefixArray('add column', $this->getColumns($blueprint)))
  76. );
  77. }
  78. /**
  79. * Compile a primary key command.
  80. *
  81. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  82. * @param \Illuminate\Support\Fluent $command
  83. * @return string
  84. */
  85. public function compilePrimary(Blueprint $blueprint, Fluent $command)
  86. {
  87. $columns = $this->columnize($command->columns);
  88. return 'alter table '.$this->wrapTable($blueprint)." add primary key ({$columns})";
  89. }
  90. /**
  91. * Compile a unique key command.
  92. *
  93. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  94. * @param \Illuminate\Support\Fluent $command
  95. * @return string
  96. */
  97. public function compileUnique(Blueprint $blueprint, Fluent $command)
  98. {
  99. return sprintf('alter table %s add constraint %s unique (%s)',
  100. $this->wrapTable($blueprint),
  101. $this->wrap($command->index),
  102. $this->columnize($command->columns)
  103. );
  104. }
  105. /**
  106. * Compile a plain index key command.
  107. *
  108. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  109. * @param \Illuminate\Support\Fluent $command
  110. * @return string
  111. */
  112. public function compileIndex(Blueprint $blueprint, Fluent $command)
  113. {
  114. return sprintf('create index %s on %s%s (%s)',
  115. $this->wrap($command->index),
  116. $this->wrapTable($blueprint),
  117. $command->algorithm ? ' using '.$command->algorithm : '',
  118. $this->columnize($command->columns)
  119. );
  120. }
  121. /**
  122. * Compile a spatial index key command.
  123. *
  124. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  125. * @param \Illuminate\Support\Fluent $command
  126. * @return string
  127. */
  128. public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
  129. {
  130. $command->algorithm = 'gist';
  131. return $this->compileIndex($blueprint, $command);
  132. }
  133. /**
  134. * Compile a foreign key command.
  135. *
  136. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  137. * @param \Illuminate\Support\Fluent $command
  138. * @return string
  139. */
  140. public function compileForeign(Blueprint $blueprint, Fluent $command)
  141. {
  142. $sql = parent::compileForeign($blueprint, $command);
  143. if (! is_null($command->deferrable)) {
  144. $sql .= $command->deferrable ? ' deferrable' : ' not deferrable';
  145. }
  146. if ($command->deferrable && ! is_null($command->initiallyImmediate)) {
  147. $sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred';
  148. }
  149. if (! is_null($command->notValid)) {
  150. $sql .= ' not valid';
  151. }
  152. return $sql;
  153. }
  154. /**
  155. * Compile a drop table command.
  156. *
  157. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  158. * @param \Illuminate\Support\Fluent $command
  159. * @return string
  160. */
  161. public function compileDrop(Blueprint $blueprint, Fluent $command)
  162. {
  163. return 'drop table '.$this->wrapTable($blueprint);
  164. }
  165. /**
  166. * Compile a drop table (if exists) command.
  167. *
  168. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  169. * @param \Illuminate\Support\Fluent $command
  170. * @return string
  171. */
  172. public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
  173. {
  174. return 'drop table if exists '.$this->wrapTable($blueprint);
  175. }
  176. /**
  177. * Compile the SQL needed to drop all tables.
  178. *
  179. * @param array $tables
  180. * @return string
  181. */
  182. public function compileDropAllTables($tables)
  183. {
  184. return 'drop table "'.implode('","', $tables).'" cascade';
  185. }
  186. /**
  187. * Compile the SQL needed to drop all views.
  188. *
  189. * @param array $views
  190. * @return string
  191. */
  192. public function compileDropAllViews($views)
  193. {
  194. return 'drop view "'.implode('","', $views).'" cascade';
  195. }
  196. /**
  197. * Compile the SQL needed to drop all types.
  198. *
  199. * @param array $types
  200. * @return string
  201. */
  202. public function compileDropAllTypes($types)
  203. {
  204. return 'drop type "'.implode('","', $types).'" cascade';
  205. }
  206. /**
  207. * Compile the SQL needed to retrieve all table names.
  208. *
  209. * @param string $schema
  210. * @return string
  211. */
  212. public function compileGetAllTables($schema)
  213. {
  214. return "select tablename from pg_catalog.pg_tables where schemaname = '{$schema}'";
  215. }
  216. /**
  217. * Compile the SQL needed to retrieve all view names.
  218. *
  219. * @param string $schema
  220. * @return string
  221. */
  222. public function compileGetAllViews($schema)
  223. {
  224. return "select viewname from pg_catalog.pg_views where schemaname = '{$schema}'";
  225. }
  226. /**
  227. * Compile the SQL needed to retrieve all type names.
  228. *
  229. * @return string
  230. */
  231. public function compileGetAllTypes()
  232. {
  233. return 'select distinct pg_type.typname from pg_type inner join pg_enum on pg_enum.enumtypid = pg_type.oid';
  234. }
  235. /**
  236. * Compile a drop column command.
  237. *
  238. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  239. * @param \Illuminate\Support\Fluent $command
  240. * @return string
  241. */
  242. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  243. {
  244. $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns));
  245. return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
  246. }
  247. /**
  248. * Compile a drop primary key command.
  249. *
  250. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  251. * @param \Illuminate\Support\Fluent $command
  252. * @return string
  253. */
  254. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  255. {
  256. $index = $this->wrap("{$blueprint->getTable()}_pkey");
  257. return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}";
  258. }
  259. /**
  260. * Compile a drop unique key command.
  261. *
  262. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  263. * @param \Illuminate\Support\Fluent $command
  264. * @return string
  265. */
  266. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  267. {
  268. $index = $this->wrap($command->index);
  269. return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
  270. }
  271. /**
  272. * Compile a drop index command.
  273. *
  274. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  275. * @param \Illuminate\Support\Fluent $command
  276. * @return string
  277. */
  278. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  279. {
  280. return "drop index {$this->wrap($command->index)}";
  281. }
  282. /**
  283. * Compile a drop spatial index command.
  284. *
  285. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  286. * @param \Illuminate\Support\Fluent $command
  287. * @return string
  288. */
  289. public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
  290. {
  291. return $this->compileDropIndex($blueprint, $command);
  292. }
  293. /**
  294. * Compile a drop foreign key command.
  295. *
  296. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  297. * @param \Illuminate\Support\Fluent $command
  298. * @return string
  299. */
  300. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  301. {
  302. $index = $this->wrap($command->index);
  303. return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
  304. }
  305. /**
  306. * Compile a rename table command.
  307. *
  308. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  309. * @param \Illuminate\Support\Fluent $command
  310. * @return string
  311. */
  312. public function compileRename(Blueprint $blueprint, Fluent $command)
  313. {
  314. $from = $this->wrapTable($blueprint);
  315. return "alter table {$from} rename to ".$this->wrapTable($command->to);
  316. }
  317. /**
  318. * Compile a rename index command.
  319. *
  320. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  321. * @param \Illuminate\Support\Fluent $command
  322. * @return string
  323. */
  324. public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
  325. {
  326. return sprintf('alter index %s rename to %s',
  327. $this->wrap($command->from),
  328. $this->wrap($command->to)
  329. );
  330. }
  331. /**
  332. * Compile the command to enable foreign key constraints.
  333. *
  334. * @return string
  335. */
  336. public function compileEnableForeignKeyConstraints()
  337. {
  338. return 'SET CONSTRAINTS ALL IMMEDIATE;';
  339. }
  340. /**
  341. * Compile the command to disable foreign key constraints.
  342. *
  343. * @return string
  344. */
  345. public function compileDisableForeignKeyConstraints()
  346. {
  347. return 'SET CONSTRAINTS ALL DEFERRED;';
  348. }
  349. /**
  350. * Compile a comment command.
  351. *
  352. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  353. * @param \Illuminate\Support\Fluent $command
  354. * @return string
  355. */
  356. public function compileComment(Blueprint $blueprint, Fluent $command)
  357. {
  358. return sprintf('comment on column %s.%s is %s',
  359. $this->wrapTable($blueprint),
  360. $this->wrap($command->column->name),
  361. "'".str_replace("'", "''", $command->value)."'"
  362. );
  363. }
  364. /**
  365. * Create the column definition for a char type.
  366. *
  367. * @param \Illuminate\Support\Fluent $column
  368. * @return string
  369. */
  370. protected function typeChar(Fluent $column)
  371. {
  372. return "char({$column->length})";
  373. }
  374. /**
  375. * Create the column definition for a string type.
  376. *
  377. * @param \Illuminate\Support\Fluent $column
  378. * @return string
  379. */
  380. protected function typeString(Fluent $column)
  381. {
  382. return "varchar({$column->length})";
  383. }
  384. /**
  385. * Create the column definition for a text type.
  386. *
  387. * @param \Illuminate\Support\Fluent $column
  388. * @return string
  389. */
  390. protected function typeText(Fluent $column)
  391. {
  392. return 'text';
  393. }
  394. /**
  395. * Create the column definition for a medium text type.
  396. *
  397. * @param \Illuminate\Support\Fluent $column
  398. * @return string
  399. */
  400. protected function typeMediumText(Fluent $column)
  401. {
  402. return 'text';
  403. }
  404. /**
  405. * Create the column definition for a long text type.
  406. *
  407. * @param \Illuminate\Support\Fluent $column
  408. * @return string
  409. */
  410. protected function typeLongText(Fluent $column)
  411. {
  412. return 'text';
  413. }
  414. /**
  415. * Create the column definition for an integer type.
  416. *
  417. * @param \Illuminate\Support\Fluent $column
  418. * @return string
  419. */
  420. protected function typeInteger(Fluent $column)
  421. {
  422. return $this->generatableColumn('integer', $column);
  423. }
  424. /**
  425. * Create the column definition for a big integer type.
  426. *
  427. * @param \Illuminate\Support\Fluent $column
  428. * @return string
  429. */
  430. protected function typeBigInteger(Fluent $column)
  431. {
  432. return $this->generatableColumn('bigint', $column);
  433. }
  434. /**
  435. * Create the column definition for a medium integer type.
  436. *
  437. * @param \Illuminate\Support\Fluent $column
  438. * @return string
  439. */
  440. protected function typeMediumInteger(Fluent $column)
  441. {
  442. return $this->generatableColumn('integer', $column);
  443. }
  444. /**
  445. * Create the column definition for a tiny integer type.
  446. *
  447. * @param \Illuminate\Support\Fluent $column
  448. * @return string
  449. */
  450. protected function typeTinyInteger(Fluent $column)
  451. {
  452. return $this->generatableColumn('smallint', $column);
  453. }
  454. /**
  455. * Create the column definition for a small integer type.
  456. *
  457. * @param \Illuminate\Support\Fluent $column
  458. * @return string
  459. */
  460. protected function typeSmallInteger(Fluent $column)
  461. {
  462. return $this->generatableColumn('smallint', $column);
  463. }
  464. /**
  465. * Create the column definition for a generatable column.
  466. *
  467. * @param string $type
  468. * @param \Illuminate\Support\Fluent $column
  469. * @return string
  470. */
  471. protected function generatableColumn($type, Fluent $column)
  472. {
  473. if (! $column->autoIncrement && is_null($column->generatedAs)) {
  474. return $type;
  475. }
  476. if ($column->autoIncrement && is_null($column->generatedAs)) {
  477. return with([
  478. 'integer' => 'serial',
  479. 'bigint' => 'bigserial',
  480. 'smallint' => 'smallserial',
  481. ])[$type];
  482. }
  483. $options = '';
  484. if (! is_bool($column->generatedAs) && ! empty($column->generatedAs)) {
  485. $options = sprintf(' (%s)', $column->generatedAs);
  486. }
  487. return sprintf(
  488. '%s generated %s as identity%s',
  489. $type,
  490. $column->always ? 'always' : 'by default',
  491. $options
  492. );
  493. }
  494. /**
  495. * Create the column definition for a float type.
  496. *
  497. * @param \Illuminate\Support\Fluent $column
  498. * @return string
  499. */
  500. protected function typeFloat(Fluent $column)
  501. {
  502. return $this->typeDouble($column);
  503. }
  504. /**
  505. * Create the column definition for a double type.
  506. *
  507. * @param \Illuminate\Support\Fluent $column
  508. * @return string
  509. */
  510. protected function typeDouble(Fluent $column)
  511. {
  512. return 'double precision';
  513. }
  514. /**
  515. * Create the column definition for a real type.
  516. *
  517. * @param \Illuminate\Support\Fluent $column
  518. * @return string
  519. */
  520. protected function typeReal(Fluent $column)
  521. {
  522. return 'real';
  523. }
  524. /**
  525. * Create the column definition for a decimal type.
  526. *
  527. * @param \Illuminate\Support\Fluent $column
  528. * @return string
  529. */
  530. protected function typeDecimal(Fluent $column)
  531. {
  532. return "decimal({$column->total}, {$column->places})";
  533. }
  534. /**
  535. * Create the column definition for a boolean type.
  536. *
  537. * @param \Illuminate\Support\Fluent $column
  538. * @return string
  539. */
  540. protected function typeBoolean(Fluent $column)
  541. {
  542. return 'boolean';
  543. }
  544. /**
  545. * Create the column definition for an enumeration type.
  546. *
  547. * @param \Illuminate\Support\Fluent $column
  548. * @return string
  549. */
  550. protected function typeEnum(Fluent $column)
  551. {
  552. return sprintf(
  553. 'varchar(255) check ("%s" in (%s))',
  554. $column->name,
  555. $this->quoteString($column->allowed)
  556. );
  557. }
  558. /**
  559. * Create the column definition for a json type.
  560. *
  561. * @param \Illuminate\Support\Fluent $column
  562. * @return string
  563. */
  564. protected function typeJson(Fluent $column)
  565. {
  566. return 'json';
  567. }
  568. /**
  569. * Create the column definition for a jsonb type.
  570. *
  571. * @param \Illuminate\Support\Fluent $column
  572. * @return string
  573. */
  574. protected function typeJsonb(Fluent $column)
  575. {
  576. return 'jsonb';
  577. }
  578. /**
  579. * Create the column definition for a date type.
  580. *
  581. * @param \Illuminate\Support\Fluent $column
  582. * @return string
  583. */
  584. protected function typeDate(Fluent $column)
  585. {
  586. return 'date';
  587. }
  588. /**
  589. * Create the column definition for a date-time type.
  590. *
  591. * @param \Illuminate\Support\Fluent $column
  592. * @return string
  593. */
  594. protected function typeDateTime(Fluent $column)
  595. {
  596. return $this->typeTimestamp($column);
  597. }
  598. /**
  599. * Create the column definition for a date-time (with time zone) type.
  600. *
  601. * @param \Illuminate\Support\Fluent $column
  602. * @return string
  603. */
  604. protected function typeDateTimeTz(Fluent $column)
  605. {
  606. return $this->typeTimestampTz($column);
  607. }
  608. /**
  609. * Create the column definition for a time type.
  610. *
  611. * @param \Illuminate\Support\Fluent $column
  612. * @return string
  613. */
  614. protected function typeTime(Fluent $column)
  615. {
  616. return "time($column->precision) without time zone";
  617. }
  618. /**
  619. * Create the column definition for a time (with time zone) type.
  620. *
  621. * @param \Illuminate\Support\Fluent $column
  622. * @return string
  623. */
  624. protected function typeTimeTz(Fluent $column)
  625. {
  626. return "time($column->precision) with time zone";
  627. }
  628. /**
  629. * Create the column definition for a timestamp type.
  630. *
  631. * @param \Illuminate\Support\Fluent $column
  632. * @return string
  633. */
  634. protected function typeTimestamp(Fluent $column)
  635. {
  636. $columnType = "timestamp($column->precision) without time zone";
  637. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  638. }
  639. /**
  640. * Create the column definition for a timestamp (with time zone) type.
  641. *
  642. * @param \Illuminate\Support\Fluent $column
  643. * @return string
  644. */
  645. protected function typeTimestampTz(Fluent $column)
  646. {
  647. $columnType = "timestamp($column->precision) with time zone";
  648. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  649. }
  650. /**
  651. * Create the column definition for a year type.
  652. *
  653. * @param \Illuminate\Support\Fluent $column
  654. * @return string
  655. */
  656. protected function typeYear(Fluent $column)
  657. {
  658. return $this->typeInteger($column);
  659. }
  660. /**
  661. * Create the column definition for a binary type.
  662. *
  663. * @param \Illuminate\Support\Fluent $column
  664. * @return string
  665. */
  666. protected function typeBinary(Fluent $column)
  667. {
  668. return 'bytea';
  669. }
  670. /**
  671. * Create the column definition for a uuid type.
  672. *
  673. * @param \Illuminate\Support\Fluent $column
  674. * @return string
  675. */
  676. protected function typeUuid(Fluent $column)
  677. {
  678. return 'uuid';
  679. }
  680. /**
  681. * Create the column definition for an IP address type.
  682. *
  683. * @param \Illuminate\Support\Fluent $column
  684. * @return string
  685. */
  686. protected function typeIpAddress(Fluent $column)
  687. {
  688. return 'inet';
  689. }
  690. /**
  691. * Create the column definition for a MAC address type.
  692. *
  693. * @param \Illuminate\Support\Fluent $column
  694. * @return string
  695. */
  696. protected function typeMacAddress(Fluent $column)
  697. {
  698. return 'macaddr';
  699. }
  700. /**
  701. * Create the column definition for a spatial Geometry type.
  702. *
  703. * @param \Illuminate\Support\Fluent $column
  704. * @return string
  705. */
  706. protected function typeGeometry(Fluent $column)
  707. {
  708. return $this->formatPostGisType('geometry');
  709. }
  710. /**
  711. * Create the column definition for a spatial Point type.
  712. *
  713. * @param \Illuminate\Support\Fluent $column
  714. * @return string
  715. */
  716. protected function typePoint(Fluent $column)
  717. {
  718. return $this->formatPostGisType('point');
  719. }
  720. /**
  721. * Create the column definition for a spatial LineString type.
  722. *
  723. * @param \Illuminate\Support\Fluent $column
  724. * @return string
  725. */
  726. protected function typeLineString(Fluent $column)
  727. {
  728. return $this->formatPostGisType('linestring');
  729. }
  730. /**
  731. * Create the column definition for a spatial Polygon type.
  732. *
  733. * @param \Illuminate\Support\Fluent $column
  734. * @return string
  735. */
  736. protected function typePolygon(Fluent $column)
  737. {
  738. return $this->formatPostGisType('polygon');
  739. }
  740. /**
  741. * Create the column definition for a spatial GeometryCollection type.
  742. *
  743. * @param \Illuminate\Support\Fluent $column
  744. * @return string
  745. */
  746. protected function typeGeometryCollection(Fluent $column)
  747. {
  748. return $this->formatPostGisType('geometrycollection');
  749. }
  750. /**
  751. * Create the column definition for a spatial MultiPoint type.
  752. *
  753. * @param \Illuminate\Support\Fluent $column
  754. * @return string
  755. */
  756. protected function typeMultiPoint(Fluent $column)
  757. {
  758. return $this->formatPostGisType('multipoint');
  759. }
  760. /**
  761. * Create the column definition for a spatial MultiLineString type.
  762. *
  763. * @param \Illuminate\Support\Fluent $column
  764. * @return string
  765. */
  766. public function typeMultiLineString(Fluent $column)
  767. {
  768. return $this->formatPostGisType('multilinestring');
  769. }
  770. /**
  771. * Create the column definition for a spatial MultiPolygon type.
  772. *
  773. * @param \Illuminate\Support\Fluent $column
  774. * @return string
  775. */
  776. protected function typeMultiPolygon(Fluent $column)
  777. {
  778. return $this->formatPostGisType('multipolygon');
  779. }
  780. /**
  781. * Format the column definition for a PostGIS spatial type.
  782. *
  783. * @param string $type
  784. * @return string
  785. */
  786. private function formatPostGisType(string $type)
  787. {
  788. return "geography($type, 4326)";
  789. }
  790. /**
  791. * Get the SQL for a collation column modifier.
  792. *
  793. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  794. * @param \Illuminate\Support\Fluent $column
  795. * @return string|null
  796. */
  797. protected function modifyCollate(Blueprint $blueprint, Fluent $column)
  798. {
  799. if (! is_null($column->collation)) {
  800. return ' collate '.$this->wrapValue($column->collation);
  801. }
  802. }
  803. /**
  804. * Get the SQL for a nullable column modifier.
  805. *
  806. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  807. * @param \Illuminate\Support\Fluent $column
  808. * @return string|null
  809. */
  810. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  811. {
  812. return $column->nullable ? ' null' : ' not null';
  813. }
  814. /**
  815. * Get the SQL for a default column modifier.
  816. *
  817. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  818. * @param \Illuminate\Support\Fluent $column
  819. * @return string|null
  820. */
  821. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  822. {
  823. if (! is_null($column->default)) {
  824. return ' default '.$this->getDefaultValue($column->default);
  825. }
  826. }
  827. /**
  828. * Get the SQL for an auto-increment column modifier.
  829. *
  830. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  831. * @param \Illuminate\Support\Fluent $column
  832. * @return string|null
  833. */
  834. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  835. {
  836. if ((in_array($column->type, $this->serials) || ($column->generatedAs !== null)) && $column->autoIncrement) {
  837. return ' primary key';
  838. }
  839. }
  840. }