PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/xolotsoft/pumasruiz
PHP | 270 lines | 118 code | 45 blank | 107 comment | 9 complexity | b3cab91e62af463a7a3e241281fc28b9 MD5 | raw file
  1. <?php namespace Illuminate\Database\Schema\Grammars;
  2. use Illuminate\Support\Fluent;
  3. use Doctrine\DBAL\Schema\Column;
  4. use Doctrine\DBAL\Schema\TableDiff;
  5. use Illuminate\Database\Connection;
  6. use Illuminate\Database\Query\Expression;
  7. use Illuminate\Database\Schema\Blueprint;
  8. use Illuminate\Database\Grammar as BaseGrammar;
  9. use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
  10. abstract class Grammar extends BaseGrammar {
  11. /**
  12. * Compile a rename column command.
  13. *
  14. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  15. * @param \Illuminate\Support\Fluent $command
  16. * @param \Illuminate\Database\Connection $connection
  17. * @return array
  18. */
  19. public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
  20. {
  21. $schema = $connection->getDoctrineSchemaManager();
  22. $table = $this->getTablePrefix().$blueprint->getTable();
  23. $column = $connection->getDoctrineColumn($table, $command->from);
  24. $tableDiff = $this->getRenamedDiff($blueprint, $command, $column, $schema);
  25. return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
  26. }
  27. /**
  28. * Get a new column instance with the new column name.
  29. *
  30. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  31. * @param \Illuminate\Support\Fluent $command
  32. * @param \Doctrine\DBAL\Schema\Column $column
  33. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
  34. * @return \Doctrine\DBAL\Schema\TableDiff
  35. */
  36. protected function getRenamedDiff(Blueprint $blueprint, Fluent $command, Column $column, SchemaManager $schema)
  37. {
  38. $tableDiff = $this->getDoctrineTableDiff($blueprint, $schema);
  39. return $this->setRenamedColumns($tableDiff, $command, $column);
  40. }
  41. /**
  42. * Set the renamed columns on the table diff.
  43. *
  44. * @param \Doctrine\DBAL\Schema\TableDiff $tableDiff
  45. * @param \Illuminate\Support\Fluent $command
  46. * @param \Doctrine\DBAL\Schema\Column $column
  47. * @return \Doctrine\DBAL\Schema\TableDiff
  48. */
  49. protected function setRenamedColumns(TableDiff $tableDiff, Fluent $command, Column $column)
  50. {
  51. $newColumn = new Column($command->to, $column->getType(), $column->toArray());
  52. $tableDiff->renamedColumns = array($command->from => $newColumn);
  53. return $tableDiff;
  54. }
  55. /**
  56. * Compile a foreign key command.
  57. *
  58. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  59. * @param \Illuminate\Support\Fluent $command
  60. * @return string
  61. */
  62. public function compileForeign(Blueprint $blueprint, Fluent $command)
  63. {
  64. $table = $this->wrapTable($blueprint);
  65. $on = $this->wrapTable($command->on);
  66. // We need to prepare several of the elements of the foreign key definition
  67. // before we can create the SQL, such as wrapping the tables and convert
  68. // an array of columns to comma-delimited strings for the SQL queries.
  69. $columns = $this->columnize($command->columns);
  70. $onColumns = $this->columnize((array) $command->references);
  71. $sql = "alter table {$table} add constraint {$command->index} ";
  72. $sql .= "foreign key ({$columns}) references {$on} ({$onColumns})";
  73. // Once we have the basic foreign key creation statement constructed we can
  74. // build out the syntax for what should happen on an update or delete of
  75. // the affected columns, which will get something like "cascade", etc.
  76. if ( ! is_null($command->onDelete))
  77. {
  78. $sql .= " on delete {$command->onDelete}";
  79. }
  80. if ( ! is_null($command->onUpdate))
  81. {
  82. $sql .= " on update {$command->onUpdate}";
  83. }
  84. return $sql;
  85. }
  86. /**
  87. * Compile the blueprint's column definitions.
  88. *
  89. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  90. * @return array
  91. */
  92. protected function getColumns(Blueprint $blueprint)
  93. {
  94. $columns = array();
  95. foreach ($blueprint->getColumns() as $column)
  96. {
  97. // Each of the column types have their own compiler functions which are tasked
  98. // with turning the column definition into its SQL format for this platform
  99. // used by the connection. The column's modifiers are compiled and added.
  100. $sql = $this->wrap($column).' '.$this->getType($column);
  101. $columns[] = $this->addModifiers($sql, $blueprint, $column);
  102. }
  103. return $columns;
  104. }
  105. /**
  106. * Add the column modifiers to the definition.
  107. *
  108. * @param string $sql
  109. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  110. * @param \Illuminate\Support\Fluent $column
  111. * @return string
  112. */
  113. protected function addModifiers($sql, Blueprint $blueprint, Fluent $column)
  114. {
  115. foreach ($this->modifiers as $modifier)
  116. {
  117. if (method_exists($this, $method = "modify{$modifier}"))
  118. {
  119. $sql .= $this->{$method}($blueprint, $column);
  120. }
  121. }
  122. return $sql;
  123. }
  124. /**
  125. * Get the primary key command if it exists on the blueprint.
  126. *
  127. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  128. * @param string $name
  129. * @return \Illuminate\Support\Fluent|null
  130. */
  131. protected function getCommandByName(Blueprint $blueprint, $name)
  132. {
  133. $commands = $this->getCommandsByName($blueprint, $name);
  134. if (count($commands) > 0)
  135. {
  136. return reset($commands);
  137. }
  138. }
  139. /**
  140. * Get all of the commands with a given name.
  141. *
  142. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  143. * @param string $name
  144. * @return array
  145. */
  146. protected function getCommandsByName(Blueprint $blueprint, $name)
  147. {
  148. return array_filter($blueprint->getCommands(), function($value) use ($name)
  149. {
  150. return $value->name == $name;
  151. });
  152. }
  153. /**
  154. * Get the SQL for the column data type.
  155. *
  156. * @param \Illuminate\Support\Fluent $column
  157. * @return string
  158. */
  159. protected function getType(Fluent $column)
  160. {
  161. return $this->{"type".ucfirst($column->type)}($column);
  162. }
  163. /**
  164. * Add a prefix to an array of values.
  165. *
  166. * @param string $prefix
  167. * @param array $values
  168. * @return array
  169. */
  170. public function prefixArray($prefix, array $values)
  171. {
  172. return array_map(function($value) use ($prefix)
  173. {
  174. return $prefix.' '.$value;
  175. }, $values);
  176. }
  177. /**
  178. * Wrap a table in keyword identifiers.
  179. *
  180. * @param mixed $table
  181. * @return string
  182. */
  183. public function wrapTable($table)
  184. {
  185. if ($table instanceof Blueprint) $table = $table->getTable();
  186. return parent::wrapTable($table);
  187. }
  188. /**
  189. * Wrap a value in keyword identifiers.
  190. *
  191. * @param string $value
  192. * @return string
  193. */
  194. public function wrap($value)
  195. {
  196. if ($value instanceof Fluent) $value = $value->name;
  197. return parent::wrap($value);
  198. }
  199. /**
  200. * Format a value so that it can be used in "default" clauses.
  201. *
  202. * @param mixed $value
  203. * @return string
  204. */
  205. protected function getDefaultValue($value)
  206. {
  207. if ($value instanceof Expression) return $value;
  208. if (is_bool($value)) return "'".(int) $value."'";
  209. return "'".strval($value)."'";
  210. }
  211. /**
  212. * Create an empty Doctrine DBAL TableDiff from the Blueprint.
  213. *
  214. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  215. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
  216. * @return \Doctrine\DBAL\Schema\TableDiff
  217. */
  218. protected function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema)
  219. {
  220. $table = $this->getTablePrefix().$blueprint->getTable();
  221. $tableDiff = new TableDiff($table);
  222. $tableDiff->fromTable = $schema->listTableDetails($table);
  223. return $tableDiff;
  224. }
  225. }