Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PostgresGrammar.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 = ['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 = ?';
  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 string $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 string $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 retrieve all table names.
  198. *
  199. * @param string $schema
  200. * @return string
  201. */
  202. public function compileGetAllTables($schema)
  203. {
  204. return "select tablename from pg_catalog.pg_tables where schemaname = '{$schema}'";
  205. }
  206. /**
  207. * Compile the SQL needed to retrieve all view names.
  208. *
  209. * @param string $schema
  210. * @return string
  211. */
  212. public function compileGetAllViews($schema)
  213. {
  214. return "select viewname from pg_catalog.pg_views where schemaname = '{$schema}'";
  215. }
  216. /**
  217. * Compile a drop column command.
  218. *
  219. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  220. * @param \Illuminate\Support\Fluent $command
  221. * @return string
  222. */
  223. public function compileDropColumn(Blueprint $blueprint, Fluent $command)
  224. {
  225. $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns));
  226. return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
  227. }
  228. /**
  229. * Compile a drop primary key command.
  230. *
  231. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  232. * @param \Illuminate\Support\Fluent $command
  233. * @return string
  234. */
  235. public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
  236. {
  237. $index = $this->wrap("{$blueprint->getTable()}_pkey");
  238. return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}";
  239. }
  240. /**
  241. * Compile a drop unique key command.
  242. *
  243. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  244. * @param \Illuminate\Support\Fluent $command
  245. * @return string
  246. */
  247. public function compileDropUnique(Blueprint $blueprint, Fluent $command)
  248. {
  249. $index = $this->wrap($command->index);
  250. return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
  251. }
  252. /**
  253. * Compile a drop index command.
  254. *
  255. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  256. * @param \Illuminate\Support\Fluent $command
  257. * @return string
  258. */
  259. public function compileDropIndex(Blueprint $blueprint, Fluent $command)
  260. {
  261. return "drop index {$this->wrap($command->index)}";
  262. }
  263. /**
  264. * Compile a drop spatial index command.
  265. *
  266. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  267. * @param \Illuminate\Support\Fluent $command
  268. * @return string
  269. */
  270. public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
  271. {
  272. return $this->compileDropIndex($blueprint, $command);
  273. }
  274. /**
  275. * Compile a drop foreign key command.
  276. *
  277. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  278. * @param \Illuminate\Support\Fluent $command
  279. * @return string
  280. */
  281. public function compileDropForeign(Blueprint $blueprint, Fluent $command)
  282. {
  283. $index = $this->wrap($command->index);
  284. return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}";
  285. }
  286. /**
  287. * Compile a rename table command.
  288. *
  289. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  290. * @param \Illuminate\Support\Fluent $command
  291. * @return string
  292. */
  293. public function compileRename(Blueprint $blueprint, Fluent $command)
  294. {
  295. $from = $this->wrapTable($blueprint);
  296. return "alter table {$from} rename to ".$this->wrapTable($command->to);
  297. }
  298. /**
  299. * Compile a rename index command.
  300. *
  301. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  302. * @param \Illuminate\Support\Fluent $command
  303. * @return string
  304. */
  305. public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
  306. {
  307. return sprintf('alter index %s rename to %s',
  308. $this->wrap($command->from),
  309. $this->wrap($command->to)
  310. );
  311. }
  312. /**
  313. * Compile the command to enable foreign key constraints.
  314. *
  315. * @return string
  316. */
  317. public function compileEnableForeignKeyConstraints()
  318. {
  319. return 'SET CONSTRAINTS ALL IMMEDIATE;';
  320. }
  321. /**
  322. * Compile the command to disable foreign key constraints.
  323. *
  324. * @return string
  325. */
  326. public function compileDisableForeignKeyConstraints()
  327. {
  328. return 'SET CONSTRAINTS ALL DEFERRED;';
  329. }
  330. /**
  331. * Compile a comment command.
  332. *
  333. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  334. * @param \Illuminate\Support\Fluent $command
  335. * @return string
  336. */
  337. public function compileComment(Blueprint $blueprint, Fluent $command)
  338. {
  339. return sprintf('comment on column %s.%s is %s',
  340. $this->wrapTable($blueprint),
  341. $this->wrap($command->column->name),
  342. "'".str_replace("'", "''", $command->value)."'"
  343. );
  344. }
  345. /**
  346. * Create the column definition for a char type.
  347. *
  348. * @param \Illuminate\Support\Fluent $column
  349. * @return string
  350. */
  351. protected function typeChar(Fluent $column)
  352. {
  353. return "char({$column->length})";
  354. }
  355. /**
  356. * Create the column definition for a string type.
  357. *
  358. * @param \Illuminate\Support\Fluent $column
  359. * @return string
  360. */
  361. protected function typeString(Fluent $column)
  362. {
  363. return "varchar({$column->length})";
  364. }
  365. /**
  366. * Create the column definition for a text type.
  367. *
  368. * @param \Illuminate\Support\Fluent $column
  369. * @return string
  370. */
  371. protected function typeText(Fluent $column)
  372. {
  373. return 'text';
  374. }
  375. /**
  376. * Create the column definition for a medium text type.
  377. *
  378. * @param \Illuminate\Support\Fluent $column
  379. * @return string
  380. */
  381. protected function typeMediumText(Fluent $column)
  382. {
  383. return 'text';
  384. }
  385. /**
  386. * Create the column definition for a long text type.
  387. *
  388. * @param \Illuminate\Support\Fluent $column
  389. * @return string
  390. */
  391. protected function typeLongText(Fluent $column)
  392. {
  393. return 'text';
  394. }
  395. /**
  396. * Create the column definition for an integer type.
  397. *
  398. * @param \Illuminate\Support\Fluent $column
  399. * @return string
  400. */
  401. protected function typeInteger(Fluent $column)
  402. {
  403. return $this->generatableColumn('integer', $column);
  404. }
  405. /**
  406. * Create the column definition for a big integer type.
  407. *
  408. * @param \Illuminate\Support\Fluent $column
  409. * @return string
  410. */
  411. protected function typeBigInteger(Fluent $column)
  412. {
  413. return $this->generatableColumn('bigint', $column);
  414. }
  415. /**
  416. * Create the column definition for a medium integer type.
  417. *
  418. * @param \Illuminate\Support\Fluent $column
  419. * @return string
  420. */
  421. protected function typeMediumInteger(Fluent $column)
  422. {
  423. return $this->generatableColumn('integer', $column);
  424. }
  425. /**
  426. * Create the column definition for a tiny integer type.
  427. *
  428. * @param \Illuminate\Support\Fluent $column
  429. * @return string
  430. */
  431. protected function typeTinyInteger(Fluent $column)
  432. {
  433. return $this->generatableColumn('smallint', $column);
  434. }
  435. /**
  436. * Create the column definition for a small integer type.
  437. *
  438. * @param \Illuminate\Support\Fluent $column
  439. * @return string
  440. */
  441. protected function typeSmallInteger(Fluent $column)
  442. {
  443. return $this->generatableColumn('smallint', $column);
  444. }
  445. /**
  446. * Create the column definition for a generatable column.
  447. *
  448. * @param string $type
  449. * @param \Illuminate\Support\Fluent $column
  450. * @return string
  451. */
  452. protected function generatableColumn($type, Fluent $column)
  453. {
  454. if (! $column->autoIncrement && is_null($column->generatedAs)) {
  455. return $type;
  456. }
  457. if ($column->autoIncrement && is_null($column->generatedAs)) {
  458. return with([
  459. 'integer' => 'serial',
  460. 'bigint' => 'bigserial',
  461. 'smallint' => 'smallserial',
  462. ])[$type];
  463. }
  464. $options = '';
  465. if (! is_bool($column->generatedAs) && ! empty($column->generatedAs)) {
  466. $options = sprintf(' (%s)', $column->generatedAs);
  467. }
  468. return sprintf(
  469. '%s generated %s as identity%s',
  470. $type,
  471. $column->always ? 'always' : 'by default',
  472. $options
  473. );
  474. }
  475. /**
  476. * Create the column definition for a float type.
  477. *
  478. * @param \Illuminate\Support\Fluent $column
  479. * @return string
  480. */
  481. protected function typeFloat(Fluent $column)
  482. {
  483. return $this->typeDouble($column);
  484. }
  485. /**
  486. * Create the column definition for a double type.
  487. *
  488. * @param \Illuminate\Support\Fluent $column
  489. * @return string
  490. */
  491. protected function typeDouble(Fluent $column)
  492. {
  493. return 'double precision';
  494. }
  495. /**
  496. * Create the column definition for a real type.
  497. *
  498. * @param \Illuminate\Support\Fluent $column
  499. * @return string
  500. */
  501. protected function typeReal(Fluent $column)
  502. {
  503. return 'real';
  504. }
  505. /**
  506. * Create the column definition for a decimal type.
  507. *
  508. * @param \Illuminate\Support\Fluent $column
  509. * @return string
  510. */
  511. protected function typeDecimal(Fluent $column)
  512. {
  513. return "decimal({$column->total}, {$column->places})";
  514. }
  515. /**
  516. * Create the column definition for a boolean type.
  517. *
  518. * @param \Illuminate\Support\Fluent $column
  519. * @return string
  520. */
  521. protected function typeBoolean(Fluent $column)
  522. {
  523. return 'boolean';
  524. }
  525. /**
  526. * Create the column definition for an enumeration type.
  527. *
  528. * @param \Illuminate\Support\Fluent $column
  529. * @return string
  530. */
  531. protected function typeEnum(Fluent $column)
  532. {
  533. return sprintf(
  534. 'varchar(255) check ("%s" in (%s))',
  535. $column->name,
  536. $this->quoteString($column->allowed)
  537. );
  538. }
  539. /**
  540. * Create the column definition for a json type.
  541. *
  542. * @param \Illuminate\Support\Fluent $column
  543. * @return string
  544. */
  545. protected function typeJson(Fluent $column)
  546. {
  547. return 'json';
  548. }
  549. /**
  550. * Create the column definition for a jsonb type.
  551. *
  552. * @param \Illuminate\Support\Fluent $column
  553. * @return string
  554. */
  555. protected function typeJsonb(Fluent $column)
  556. {
  557. return 'jsonb';
  558. }
  559. /**
  560. * Create the column definition for a date type.
  561. *
  562. * @param \Illuminate\Support\Fluent $column
  563. * @return string
  564. */
  565. protected function typeDate(Fluent $column)
  566. {
  567. return 'date';
  568. }
  569. /**
  570. * Create the column definition for a date-time type.
  571. *
  572. * @param \Illuminate\Support\Fluent $column
  573. * @return string
  574. */
  575. protected function typeDateTime(Fluent $column)
  576. {
  577. return $this->typeTimestamp($column);
  578. }
  579. /**
  580. * Create the column definition for a date-time (with time zone) type.
  581. *
  582. * @param \Illuminate\Support\Fluent $column
  583. * @return string
  584. */
  585. protected function typeDateTimeTz(Fluent $column)
  586. {
  587. return $this->typeTimestampTz($column);
  588. }
  589. /**
  590. * Create the column definition for a time type.
  591. *
  592. * @param \Illuminate\Support\Fluent $column
  593. * @return string
  594. */
  595. protected function typeTime(Fluent $column)
  596. {
  597. return "time($column->precision) without time zone";
  598. }
  599. /**
  600. * Create the column definition for a time (with time zone) type.
  601. *
  602. * @param \Illuminate\Support\Fluent $column
  603. * @return string
  604. */
  605. protected function typeTimeTz(Fluent $column)
  606. {
  607. return "time($column->precision) with time zone";
  608. }
  609. /**
  610. * Create the column definition for a timestamp type.
  611. *
  612. * @param \Illuminate\Support\Fluent $column
  613. * @return string
  614. */
  615. protected function typeTimestamp(Fluent $column)
  616. {
  617. $columnType = "timestamp($column->precision) without time zone";
  618. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  619. }
  620. /**
  621. * Create the column definition for a timestamp (with time zone) type.
  622. *
  623. * @param \Illuminate\Support\Fluent $column
  624. * @return string
  625. */
  626. protected function typeTimestampTz(Fluent $column)
  627. {
  628. $columnType = "timestamp($column->precision) with time zone";
  629. return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
  630. }
  631. /**
  632. * Create the column definition for a year type.
  633. *
  634. * @param \Illuminate\Support\Fluent $column
  635. * @return string
  636. */
  637. protected function typeYear(Fluent $column)
  638. {
  639. return $this->typeInteger($column);
  640. }
  641. /**
  642. * Create the column definition for a binary type.
  643. *
  644. * @param \Illuminate\Support\Fluent $column
  645. * @return string
  646. */
  647. protected function typeBinary(Fluent $column)
  648. {
  649. return 'bytea';
  650. }
  651. /**
  652. * Create the column definition for a uuid type.
  653. *
  654. * @param \Illuminate\Support\Fluent $column
  655. * @return string
  656. */
  657. protected function typeUuid(Fluent $column)
  658. {
  659. return 'uuid';
  660. }
  661. /**
  662. * Create the column definition for an IP address type.
  663. *
  664. * @param \Illuminate\Support\Fluent $column
  665. * @return string
  666. */
  667. protected function typeIpAddress(Fluent $column)
  668. {
  669. return 'inet';
  670. }
  671. /**
  672. * Create the column definition for a MAC address type.
  673. *
  674. * @param \Illuminate\Support\Fluent $column
  675. * @return string
  676. */
  677. protected function typeMacAddress(Fluent $column)
  678. {
  679. return 'macaddr';
  680. }
  681. /**
  682. * Create the column definition for a spatial Geometry type.
  683. *
  684. * @param \Illuminate\Support\Fluent $column
  685. * @return string
  686. */
  687. protected function typeGeometry(Fluent $column)
  688. {
  689. return $this->formatPostGisType('geometry');
  690. }
  691. /**
  692. * Create the column definition for a spatial Point type.
  693. *
  694. * @param \Illuminate\Support\Fluent $column
  695. * @return string
  696. */
  697. protected function typePoint(Fluent $column)
  698. {
  699. return $this->formatPostGisType('point');
  700. }
  701. /**
  702. * Create the column definition for a spatial LineString type.
  703. *
  704. * @param \Illuminate\Support\Fluent $column
  705. * @return string
  706. */
  707. protected function typeLineString(Fluent $column)
  708. {
  709. return $this->formatPostGisType('linestring');
  710. }
  711. /**
  712. * Create the column definition for a spatial Polygon type.
  713. *
  714. * @param \Illuminate\Support\Fluent $column
  715. * @return string
  716. */
  717. protected function typePolygon(Fluent $column)
  718. {
  719. return $this->formatPostGisType('polygon');
  720. }
  721. /**
  722. * Create the column definition for a spatial GeometryCollection type.
  723. *
  724. * @param \Illuminate\Support\Fluent $column
  725. * @return string
  726. */
  727. protected function typeGeometryCollection(Fluent $column)
  728. {
  729. return $this->formatPostGisType('geometrycollection');
  730. }
  731. /**
  732. * Create the column definition for a spatial MultiPoint type.
  733. *
  734. * @param \Illuminate\Support\Fluent $column
  735. * @return string
  736. */
  737. protected function typeMultiPoint(Fluent $column)
  738. {
  739. return $this->formatPostGisType('multipoint');
  740. }
  741. /**
  742. * Create the column definition for a spatial MultiLineString type.
  743. *
  744. * @param \Illuminate\Support\Fluent $column
  745. * @return string
  746. */
  747. public function typeMultiLineString(Fluent $column)
  748. {
  749. return $this->formatPostGisType('multilinestring');
  750. }
  751. /**
  752. * Create the column definition for a spatial MultiPolygon type.
  753. *
  754. * @param \Illuminate\Support\Fluent $column
  755. * @return string
  756. */
  757. protected function typeMultiPolygon(Fluent $column)
  758. {
  759. return $this->formatPostGisType('multipolygon');
  760. }
  761. /**
  762. * Format the column definition for a PostGIS spatial type.
  763. *
  764. * @param string $type
  765. * @return string
  766. */
  767. private function formatPostGisType(string $type)
  768. {
  769. return "geography($type, 4326)";
  770. }
  771. /**
  772. * Get the SQL for a nullable column modifier.
  773. *
  774. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  775. * @param \Illuminate\Support\Fluent $column
  776. * @return string|null
  777. */
  778. protected function modifyNullable(Blueprint $blueprint, Fluent $column)
  779. {
  780. return $column->nullable ? ' null' : ' not null';
  781. }
  782. /**
  783. * Get the SQL for a default column modifier.
  784. *
  785. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  786. * @param \Illuminate\Support\Fluent $column
  787. * @return string|null
  788. */
  789. protected function modifyDefault(Blueprint $blueprint, Fluent $column)
  790. {
  791. if (! is_null($column->default)) {
  792. return ' default '.$this->getDefaultValue($column->default);
  793. }
  794. }
  795. /**
  796. * Get the SQL for an auto-increment column modifier.
  797. *
  798. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  799. * @param \Illuminate\Support\Fluent $column
  800. * @return string|null
  801. */
  802. protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
  803. {
  804. if ((in_array($column->type, $this->serials) || ($column->generatedAs !== null)) && $column->autoIncrement) {
  805. return ' primary key';
  806. }
  807. }
  808. }