/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php

https://gitlab.com/daniruizcamacho/pfcascensores · PHP · 825 lines · 314 code · 91 blank · 420 comment · 11 complexity · 67c1eb07c15b1f66dad6f51962da38a7 MD5 · raw file

  1. <?php namespace Illuminate\Database\Schema;
  2. use Closure;
  3. use Illuminate\Support\Fluent;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Schema\Grammars\Grammar;
  6. class Blueprint {
  7. /**
  8. * The table the blueprint describes.
  9. *
  10. * @var string
  11. */
  12. protected $table;
  13. /**
  14. * The columns that should be added to the table.
  15. *
  16. * @var array
  17. */
  18. protected $columns = array();
  19. /**
  20. * The commands that should be run for the table.
  21. *
  22. * @var array
  23. */
  24. protected $commands = array();
  25. /**
  26. * The storage engine that should be used for the table.
  27. *
  28. * @var string
  29. */
  30. public $engine;
  31. /**
  32. * Create a new schema blueprint.
  33. *
  34. * @param string $table
  35. * @param Closure $callback
  36. * @return void
  37. */
  38. public function __construct($table, Closure $callback = null)
  39. {
  40. $this->table = $table;
  41. if ( ! is_null($callback)) $callback($this);
  42. }
  43. /**
  44. * Execute the blueprint against the database.
  45. *
  46. * @param \Illuminate\Database\Connection $connection
  47. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  48. * @return void
  49. */
  50. public function build(Connection $connection, Grammar $grammar)
  51. {
  52. foreach ($this->toSql($connection, $grammar) as $statement)
  53. {
  54. $connection->statement($statement);
  55. }
  56. }
  57. /**
  58. * Get the raw SQL statements for the blueprint.
  59. *
  60. * @param \Illuminate\Database\Connection $connection
  61. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  62. * @return array
  63. */
  64. public function toSql(Connection $connection, Grammar $grammar)
  65. {
  66. $this->addImpliedCommands();
  67. $statements = array();
  68. // Each type of command has a corresponding compiler function on the schema
  69. // grammar which is used to build the necessary SQL statements to build
  70. // the blueprint element, so we'll just call that compilers function.
  71. foreach ($this->commands as $command)
  72. {
  73. $method = 'compile'.ucfirst($command->name);
  74. if (method_exists($grammar, $method))
  75. {
  76. if ( ! is_null($sql = $grammar->$method($this, $command, $connection)))
  77. {
  78. $statements = array_merge($statements, (array) $sql);
  79. }
  80. }
  81. }
  82. return $statements;
  83. }
  84. /**
  85. * Add the commands that are implied by the blueprint.
  86. *
  87. * @return void
  88. */
  89. protected function addImpliedCommands()
  90. {
  91. if (count($this->columns) > 0 && ! $this->creating())
  92. {
  93. array_unshift($this->commands, $this->createCommand('add'));
  94. }
  95. $this->addFluentIndexes();
  96. }
  97. /**
  98. * Add the index commands fluently specified on columns.
  99. *
  100. * @return void
  101. */
  102. protected function addFluentIndexes()
  103. {
  104. foreach ($this->columns as $column)
  105. {
  106. foreach (array('primary', 'unique', 'index') as $index)
  107. {
  108. // If the index has been specified on the given column, but is simply
  109. // equal to "true" (boolean), no name has been specified for this
  110. // index, so we will simply call the index methods without one.
  111. if ($column->$index === true)
  112. {
  113. $this->$index($column->name);
  114. continue 2;
  115. }
  116. // If the index has been specified on the column and it is something
  117. // other than boolean true, we will assume a name was provided on
  118. // the index specification, and pass in the name to the method.
  119. elseif (isset($column->$index))
  120. {
  121. $this->$index($column->name, $column->$index);
  122. continue 2;
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * Determine if the blueprint has a create command.
  129. *
  130. * @return bool
  131. */
  132. protected function creating()
  133. {
  134. foreach ($this->commands as $command)
  135. {
  136. if ($command->name == 'create') return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Indicate that the table needs to be created.
  142. *
  143. * @return \Illuminate\Support\Fluent
  144. */
  145. public function create()
  146. {
  147. return $this->addCommand('create');
  148. }
  149. /**
  150. * Indicate that the table should be dropped.
  151. *
  152. * @return \Illuminate\Support\Fluent
  153. */
  154. public function drop()
  155. {
  156. return $this->addCommand('drop');
  157. }
  158. /**
  159. * Indicate that the table should be dropped if it exists.
  160. *
  161. * @return \Illuminate\Support\Fluent
  162. */
  163. public function dropIfExists()
  164. {
  165. return $this->addCommand('dropIfExists');
  166. }
  167. /**
  168. * Indicate that the given columns should be dropped.
  169. *
  170. * @param string|array $columns
  171. * @return \Illuminate\Support\Fluent
  172. */
  173. public function dropColumn($columns)
  174. {
  175. $columns = is_array($columns) ? $columns : (array) func_get_args();
  176. return $this->addCommand('dropColumn', compact('columns'));
  177. }
  178. /**
  179. * Indicate that the given columns should be renamed.
  180. *
  181. * @param string $from
  182. * @param string $to
  183. * @return \Illuminate\Support\Fluent
  184. */
  185. public function renameColumn($from, $to)
  186. {
  187. return $this->addCommand('renameColumn', compact('from', 'to'));
  188. }
  189. /**
  190. * Indicate that the given primary key should be dropped.
  191. *
  192. * @param string|array $index
  193. * @return \Illuminate\Support\Fluent
  194. */
  195. public function dropPrimary($index = null)
  196. {
  197. return $this->dropIndexCommand('dropPrimary', 'primary', $index);
  198. }
  199. /**
  200. * Indicate that the given unique key should be dropped.
  201. *
  202. * @param string|array $index
  203. * @return \Illuminate\Support\Fluent
  204. */
  205. public function dropUnique($index)
  206. {
  207. return $this->dropIndexCommand('dropUnique', 'unique', $index);
  208. }
  209. /**
  210. * Indicate that the given index should be dropped.
  211. *
  212. * @param string|array $index
  213. * @return \Illuminate\Support\Fluent
  214. */
  215. public function dropIndex($index)
  216. {
  217. return $this->dropIndexCommand('dropIndex', 'index', $index);
  218. }
  219. /**
  220. * Indicate that the given foreign key should be dropped.
  221. *
  222. * @param string $index
  223. * @return \Illuminate\Support\Fluent
  224. */
  225. public function dropForeign($index)
  226. {
  227. return $this->dropIndexCommand('dropForeign', 'foreign', $index);
  228. }
  229. /**
  230. * Indicate that the timestamp columns should be dropped.
  231. *
  232. * @return void
  233. */
  234. public function dropTimestamps()
  235. {
  236. $this->dropColumn('created_at', 'updated_at');
  237. }
  238. /**
  239. * Indicate that the soft delete column should be dropped.
  240. *
  241. * @return void
  242. */
  243. public function dropSoftDeletes()
  244. {
  245. $this->dropColumn('deleted_at');
  246. }
  247. /**
  248. * Rename the table to a given name.
  249. *
  250. * @param string $to
  251. * @return \Illuminate\Support\Fluent
  252. */
  253. public function rename($to)
  254. {
  255. return $this->addCommand('rename', compact('to'));
  256. }
  257. /**
  258. * Specify the primary key(s) for the table.
  259. *
  260. * @param string|array $columns
  261. * @param string $name
  262. * @return \Illuminate\Support\Fluent
  263. */
  264. public function primary($columns, $name = null)
  265. {
  266. return $this->indexCommand('primary', $columns, $name);
  267. }
  268. /**
  269. * Specify a unique index for the table.
  270. *
  271. * @param string|array $columns
  272. * @param string $name
  273. * @return \Illuminate\Support\Fluent
  274. */
  275. public function unique($columns, $name = null)
  276. {
  277. return $this->indexCommand('unique', $columns, $name);
  278. }
  279. /**
  280. * Specify an index for the table.
  281. *
  282. * @param string|array $columns
  283. * @param string $name
  284. * @return \Illuminate\Support\Fluent
  285. */
  286. public function index($columns, $name = null)
  287. {
  288. return $this->indexCommand('index', $columns, $name);
  289. }
  290. /**
  291. * Specify a foreign key for the table.
  292. *
  293. * @param string|array $columns
  294. * @param string $name
  295. * @return \Illuminate\Support\Fluent
  296. */
  297. public function foreign($columns, $name = null)
  298. {
  299. return $this->indexCommand('foreign', $columns, $name);
  300. }
  301. /**
  302. * Create a new auto-incrementing integer column on the table.
  303. *
  304. * @param string $column
  305. * @return \Illuminate\Support\Fluent
  306. */
  307. public function increments($column)
  308. {
  309. return $this->unsignedInteger($column, true);
  310. }
  311. /**
  312. * Create a new auto-incrementing big integer column on the table.
  313. *
  314. * @param string $column
  315. * @return \Illuminate\Support\Fluent
  316. */
  317. public function bigIncrements($column)
  318. {
  319. return $this->unsignedBigInteger($column, true);
  320. }
  321. /**
  322. * Create a new char column on the table.
  323. *
  324. * @param string $column
  325. * @param int $length
  326. * @return \Illuminate\Support\Fluent
  327. */
  328. public function char($column, $length = 255)
  329. {
  330. return $this->addColumn('char', $column, compact('length'));
  331. }
  332. /**
  333. * Create a new string column on the table.
  334. *
  335. * @param string $column
  336. * @param int $length
  337. * @return \Illuminate\Support\Fluent
  338. */
  339. public function string($column, $length = 255)
  340. {
  341. return $this->addColumn('string', $column, compact('length'));
  342. }
  343. /**
  344. * Create a new text column on the table.
  345. *
  346. * @param string $column
  347. * @return \Illuminate\Support\Fluent
  348. */
  349. public function text($column)
  350. {
  351. return $this->addColumn('text', $column);
  352. }
  353. /**
  354. * Create a new medium text column on the table.
  355. *
  356. * @param string $column
  357. * @return \Illuminate\Support\Fluent
  358. */
  359. public function mediumText($column)
  360. {
  361. return $this->addColumn('mediumText', $column);
  362. }
  363. /**
  364. * Create a new long text column on the table.
  365. *
  366. * @param string $column
  367. * @return \Illuminate\Support\Fluent
  368. */
  369. public function longText($column)
  370. {
  371. return $this->addColumn('longText', $column);
  372. }
  373. /**
  374. * Create a new integer column on the table.
  375. *
  376. * @param string $column
  377. * @param bool $autoIncrement
  378. * @param bool $unsigned
  379. * @return \Illuminate\Support\Fluent
  380. */
  381. public function integer($column, $autoIncrement = false, $unsigned = false)
  382. {
  383. return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
  384. }
  385. /**
  386. * Create a new big integer column on the table.
  387. *
  388. * @param string $column
  389. * @param bool $autoIncrement
  390. * @param bool $unsigned
  391. * @return \Illuminate\Support\Fluent
  392. */
  393. public function bigInteger($column, $autoIncrement = false, $unsigned = false)
  394. {
  395. return $this->addColumn('bigInteger', $column, compact('autoIncrement', 'unsigned'));
  396. }
  397. /**
  398. * Create a new medium integer column on the table.
  399. *
  400. * @param string $column
  401. * @param bool $autoIncrement
  402. * @param bool $unsigned
  403. * @return \Illuminate\Support\Fluent
  404. */
  405. public function mediumInteger($column, $autoIncrement = false, $unsigned = false)
  406. {
  407. return $this->addColumn('mediumInteger', $column, compact('autoIncrement', 'unsigned'));
  408. }
  409. /**
  410. * Create a new tiny integer column on the table.
  411. *
  412. * @param string $column
  413. * @param bool $autoIncrement
  414. * @param bool $unsigned
  415. * @return \Illuminate\Support\Fluent
  416. */
  417. public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
  418. {
  419. return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
  420. }
  421. /**
  422. * Create a new small integer column on the table.
  423. *
  424. * @param string $column
  425. * @param bool $autoIncrement
  426. * @param bool $unsigned
  427. * @return \Illuminate\Support\Fluent
  428. */
  429. public function smallInteger($column, $autoIncrement = false, $unsigned = false)
  430. {
  431. return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned'));
  432. }
  433. /**
  434. * Create a new unsigned integer column on the table.
  435. *
  436. * @param string $column
  437. * @param bool $autoIncrement
  438. * @return \Illuminate\Support\Fluent
  439. */
  440. public function unsignedInteger($column, $autoIncrement = false)
  441. {
  442. return $this->integer($column, $autoIncrement, true);
  443. }
  444. /**
  445. * Create a new unsigned big integer column on the table.
  446. *
  447. * @param string $column
  448. * @param bool $autoIncrement
  449. * @return \Illuminate\Support\Fluent
  450. */
  451. public function unsignedBigInteger($column, $autoIncrement = false)
  452. {
  453. return $this->bigInteger($column, $autoIncrement, true);
  454. }
  455. /**
  456. * Create a new float column on the table.
  457. *
  458. * @param string $column
  459. * @param int $total
  460. * @param int $places
  461. * @return \Illuminate\Support\Fluent
  462. */
  463. public function float($column, $total = 8, $places = 2)
  464. {
  465. return $this->addColumn('float', $column, compact('total', 'places'));
  466. }
  467. /**
  468. * Create a new double column on the table.
  469. *
  470. * @param string $column
  471. * @param int|null $total
  472. * @param int|null $places
  473. * @return \Illuminate\Support\Fluent
  474. *
  475. */
  476. public function double($column, $total = null, $places = null)
  477. {
  478. return $this->addColumn('double', $column, compact('total', 'places'));
  479. }
  480. /**
  481. * Create a new decimal column on the table.
  482. *
  483. * @param string $column
  484. * @param int $total
  485. * @param int $places
  486. * @return \Illuminate\Support\Fluent
  487. */
  488. public function decimal($column, $total = 8, $places = 2)
  489. {
  490. return $this->addColumn('decimal', $column, compact('total', 'places'));
  491. }
  492. /**
  493. * Create a new boolean column on the table.
  494. *
  495. * @param string $column
  496. * @return \Illuminate\Support\Fluent
  497. */
  498. public function boolean($column)
  499. {
  500. return $this->addColumn('boolean', $column);
  501. }
  502. /**
  503. * Create a new enum column on the table.
  504. *
  505. * @param string $column
  506. * @param array $allowed
  507. * @return \Illuminate\Support\Fluent
  508. */
  509. public function enum($column, array $allowed)
  510. {
  511. return $this->addColumn('enum', $column, compact('allowed'));
  512. }
  513. /**
  514. * Create a new date column on the table.
  515. *
  516. * @param string $column
  517. * @return \Illuminate\Support\Fluent
  518. */
  519. public function date($column)
  520. {
  521. return $this->addColumn('date', $column);
  522. }
  523. /**
  524. * Create a new date-time column on the table.
  525. *
  526. * @param string $column
  527. * @return \Illuminate\Support\Fluent
  528. */
  529. public function dateTime($column)
  530. {
  531. return $this->addColumn('dateTime', $column);
  532. }
  533. /**
  534. * Create a new time column on the table.
  535. *
  536. * @param string $column
  537. * @return \Illuminate\Support\Fluent
  538. */
  539. public function time($column)
  540. {
  541. return $this->addColumn('time', $column);
  542. }
  543. /**
  544. * Create a new timestamp column on the table.
  545. *
  546. * @param string $column
  547. * @return \Illuminate\Support\Fluent
  548. */
  549. public function timestamp($column)
  550. {
  551. return $this->addColumn('timestamp', $column);
  552. }
  553. /**
  554. * Add nullable creation and update timestamps to the table.
  555. *
  556. * @return void
  557. */
  558. public function nullableTimestamps()
  559. {
  560. $this->timestamp('created_at')->nullable();
  561. $this->timestamp('updated_at')->nullable();
  562. }
  563. /**
  564. * Add creation and update timestamps to the table.
  565. *
  566. * @return void
  567. */
  568. public function timestamps()
  569. {
  570. $this->timestamp('created_at');
  571. $this->timestamp('updated_at');
  572. }
  573. /**
  574. * Add a "deleted at" timestamp for the table.
  575. *
  576. * @return void
  577. */
  578. public function softDeletes()
  579. {
  580. $this->timestamp('deleted_at')->nullable();
  581. }
  582. /**
  583. * Create a new binary column on the table.
  584. *
  585. * @param string $column
  586. * @return \Illuminate\Support\Fluent
  587. */
  588. public function binary($column)
  589. {
  590. return $this->addColumn('binary', $column);
  591. }
  592. /**
  593. * Add the proper columns for a polymorphic table.
  594. *
  595. * @param string $name
  596. * @return void
  597. */
  598. public function morphs($name)
  599. {
  600. $this->unsignedInteger("{$name}_id");
  601. $this->string("{$name}_type");
  602. }
  603. /**
  604. * Create a new drop index command on the blueprint.
  605. *
  606. * @param string $command
  607. * @param string $type
  608. * @param string|array $index
  609. * @return \Illuminate\Support\Fluent
  610. */
  611. protected function dropIndexCommand($command, $type, $index)
  612. {
  613. $columns = array();
  614. // If the given "index" is actually an array of columns, the developer means
  615. // to drop an index merely by specifying the columns involved without the
  616. // conventional name, so we will built the index name from the columns.
  617. if (is_array($index))
  618. {
  619. $columns = $index;
  620. $index = $this->createIndexName($type, $columns);
  621. }
  622. return $this->indexCommand($command, $columns, $index);
  623. }
  624. /**
  625. * Add a new index command to the blueprint.
  626. *
  627. * @param string $type
  628. * @param string|array $columns
  629. * @param string $index
  630. * @return \Illuminate\Support\Fluent
  631. */
  632. protected function indexCommand($type, $columns, $index)
  633. {
  634. $columns = (array) $columns;
  635. // If no name was specified for this index, we will create one using a basic
  636. // convention of the table name, followed by the columns, followed by an
  637. // index type, such as primary or index, which makes the index unique.
  638. if (is_null($index))
  639. {
  640. $index = $this->createIndexName($type, $columns);
  641. }
  642. return $this->addCommand($type, compact('index', 'columns'));
  643. }
  644. /**
  645. * Create a default index name for the table.
  646. *
  647. * @param string $type
  648. * @param array $columns
  649. * @return string
  650. */
  651. protected function createIndexName($type, array $columns)
  652. {
  653. $index = strtolower($this->table.'_'.implode('_', $columns).'_'.$type);
  654. return str_replace(array('-', '.'), '_', $index);
  655. }
  656. /**
  657. * Add a new column to the blueprint.
  658. *
  659. * @param string $type
  660. * @param string $name
  661. * @param array $parameters
  662. * @return \Illuminate\Support\Fluent
  663. */
  664. protected function addColumn($type, $name, array $parameters = array())
  665. {
  666. $attributes = array_merge(compact('type', 'name'), $parameters);
  667. $this->columns[] = $column = new Fluent($attributes);
  668. return $column;
  669. }
  670. /**
  671. * Remove a column from the schema blueprint.
  672. *
  673. * @param string $name
  674. * @return \Illuminate\Database\Schema\Blueprint
  675. */
  676. public function removeColumn($name)
  677. {
  678. $this->columns = array_values(array_filter($this->columns, function($c) use ($name)
  679. {
  680. return $c['attributes']['name'] != $name;
  681. }));
  682. return $this;
  683. }
  684. /**
  685. * Add a new command to the blueprint.
  686. *
  687. * @param string $name
  688. * @param array $parameters
  689. * @return \Illuminate\Support\Fluent
  690. */
  691. protected function addCommand($name, array $parameters = array())
  692. {
  693. $this->commands[] = $command = $this->createCommand($name, $parameters);
  694. return $command;
  695. }
  696. /**
  697. * Create a new Fluent command.
  698. *
  699. * @param string $name
  700. * @param array $parameters
  701. * @return \Illuminate\Support\Fluent
  702. */
  703. protected function createCommand($name, array $parameters = array())
  704. {
  705. return new Fluent(array_merge(compact('name'), $parameters));
  706. }
  707. /**
  708. * Get the table the blueprint describes.
  709. *
  710. * @return string
  711. */
  712. public function getTable()
  713. {
  714. return $this->table;
  715. }
  716. /**
  717. * Get the columns that should be added.
  718. *
  719. * @return array
  720. */
  721. public function getColumns()
  722. {
  723. return $this->columns;
  724. }
  725. /**
  726. * Get the commands on the blueprint.
  727. *
  728. * @return array
  729. */
  730. public function getCommands()
  731. {
  732. return $this->commands;
  733. }
  734. }