PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/madwanz64/laravel
PHP | 286 lines | 123 code | 31 blank | 132 comment | 6 complexity | d0f4ccbd3105bcf4c8e374878d2c1270 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Schema\Grammars;
  3. use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
  4. use Doctrine\DBAL\Schema\TableDiff;
  5. use Illuminate\Database\Connection;
  6. use Illuminate\Database\Grammar as BaseGrammar;
  7. use Illuminate\Database\Query\Expression;
  8. use Illuminate\Database\Schema\Blueprint;
  9. use Illuminate\Support\Fluent;
  10. use RuntimeException;
  11. abstract class Grammar extends BaseGrammar
  12. {
  13. /**
  14. * If this Grammar supports schema changes wrapped in a transaction.
  15. *
  16. * @var bool
  17. */
  18. protected $transactions = false;
  19. /**
  20. * The commands to be executed outside of create or alter command.
  21. *
  22. * @var array
  23. */
  24. protected $fluentCommands = [];
  25. /**
  26. * Compile a rename column command.
  27. *
  28. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  29. * @param \Illuminate\Support\Fluent $command
  30. * @param \Illuminate\Database\Connection $connection
  31. * @return array
  32. */
  33. public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
  34. {
  35. return RenameColumn::compile($this, $blueprint, $command, $connection);
  36. }
  37. /**
  38. * Compile a change column command into a series of SQL statements.
  39. *
  40. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  41. * @param \Illuminate\Support\Fluent $command
  42. * @param \Illuminate\Database\Connection $connection
  43. * @return array
  44. *
  45. * @throws \RuntimeException
  46. */
  47. public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection)
  48. {
  49. return ChangeColumn::compile($this, $blueprint, $command, $connection);
  50. }
  51. /**
  52. * Compile a foreign key command.
  53. *
  54. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  55. * @param \Illuminate\Support\Fluent $command
  56. * @return string
  57. */
  58. public function compileForeign(Blueprint $blueprint, Fluent $command)
  59. {
  60. // We need to prepare several of the elements of the foreign key definition
  61. // before we can create the SQL, such as wrapping the tables and convert
  62. // an array of columns to comma-delimited strings for the SQL queries.
  63. $sql = sprintf('alter table %s add constraint %s ',
  64. $this->wrapTable($blueprint),
  65. $this->wrap($command->index)
  66. );
  67. // Once we have the initial portion of the SQL statement we will add on the
  68. // key name, table name, and referenced columns. These will complete the
  69. // main portion of the SQL statement and this SQL will almost be done.
  70. $sql .= sprintf('foreign key (%s) references %s (%s)',
  71. $this->columnize($command->columns),
  72. $this->wrapTable($command->on),
  73. $this->columnize((array) $command->references)
  74. );
  75. // Once we have the basic foreign key creation statement constructed we can
  76. // build out the syntax for what should happen on an update or delete of
  77. // the affected columns, which will get something like "cascade", etc.
  78. if (! is_null($command->onDelete)) {
  79. $sql .= " on delete {$command->onDelete}";
  80. }
  81. if (! is_null($command->onUpdate)) {
  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 = [];
  95. foreach ($blueprint->getAddedColumns() as $column) {
  96. // Each of the column types have their own compiler functions which are tasked
  97. // with turning the column definition into its SQL format for this platform
  98. // used by the connection. The column's modifiers are compiled and added.
  99. $sql = $this->wrap($column).' '.$this->getType($column);
  100. $columns[] = $this->addModifiers($sql, $blueprint, $column);
  101. }
  102. return $columns;
  103. }
  104. /**
  105. * Get the SQL for the column data type.
  106. *
  107. * @param \Illuminate\Support\Fluent $column
  108. * @return string
  109. */
  110. protected function getType(Fluent $column)
  111. {
  112. return $this->{'type'.ucfirst($column->type)}($column);
  113. }
  114. /**
  115. * Create the column definition for a generated, computed column type.
  116. *
  117. * @param \Illuminate\Support\Fluent $column
  118. * @return void
  119. *
  120. * @throws \RuntimeException
  121. */
  122. protected function typeComputed(Fluent $column)
  123. {
  124. throw new RuntimeException('This database driver does not support the computed type.');
  125. }
  126. /**
  127. * Add the column modifiers to the definition.
  128. *
  129. * @param string $sql
  130. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  131. * @param \Illuminate\Support\Fluent $column
  132. * @return string
  133. */
  134. protected function addModifiers($sql, Blueprint $blueprint, Fluent $column)
  135. {
  136. foreach ($this->modifiers as $modifier) {
  137. if (method_exists($this, $method = "modify{$modifier}")) {
  138. $sql .= $this->{$method}($blueprint, $column);
  139. }
  140. }
  141. return $sql;
  142. }
  143. /**
  144. * Get the primary key command if it exists on the blueprint.
  145. *
  146. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  147. * @param string $name
  148. * @return \Illuminate\Support\Fluent|null
  149. */
  150. protected function getCommandByName(Blueprint $blueprint, $name)
  151. {
  152. $commands = $this->getCommandsByName($blueprint, $name);
  153. if (count($commands) > 0) {
  154. return reset($commands);
  155. }
  156. }
  157. /**
  158. * Get all of the commands with a given name.
  159. *
  160. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  161. * @param string $name
  162. * @return array
  163. */
  164. protected function getCommandsByName(Blueprint $blueprint, $name)
  165. {
  166. return array_filter($blueprint->getCommands(), function ($value) use ($name) {
  167. return $value->name == $name;
  168. });
  169. }
  170. /**
  171. * Add a prefix to an array of values.
  172. *
  173. * @param string $prefix
  174. * @param array $values
  175. * @return array
  176. */
  177. public function prefixArray($prefix, array $values)
  178. {
  179. return array_map(function ($value) use ($prefix) {
  180. return $prefix.' '.$value;
  181. }, $values);
  182. }
  183. /**
  184. * Wrap a table in keyword identifiers.
  185. *
  186. * @param mixed $table
  187. * @return string
  188. */
  189. public function wrapTable($table)
  190. {
  191. return parent::wrapTable(
  192. $table instanceof Blueprint ? $table->getTable() : $table
  193. );
  194. }
  195. /**
  196. * Wrap a value in keyword identifiers.
  197. *
  198. * @param \Illuminate\Database\Query\Expression|string $value
  199. * @param bool $prefixAlias
  200. * @return string
  201. */
  202. public function wrap($value, $prefixAlias = false)
  203. {
  204. return parent::wrap(
  205. $value instanceof Fluent ? $value->name : $value, $prefixAlias
  206. );
  207. }
  208. /**
  209. * Format a value so that it can be used in "default" clauses.
  210. *
  211. * @param mixed $value
  212. * @return string
  213. */
  214. protected function getDefaultValue($value)
  215. {
  216. if ($value instanceof Expression) {
  217. return $value;
  218. }
  219. return is_bool($value)
  220. ? "'".(int) $value."'"
  221. : "'".(string) $value."'";
  222. }
  223. /**
  224. * Create an empty Doctrine DBAL TableDiff from the Blueprint.
  225. *
  226. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  227. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
  228. * @return \Doctrine\DBAL\Schema\TableDiff
  229. */
  230. public function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema)
  231. {
  232. $table = $this->getTablePrefix().$blueprint->getTable();
  233. return tap(new TableDiff($table), function ($tableDiff) use ($schema, $table) {
  234. $tableDiff->fromTable = $schema->listTableDetails($table);
  235. });
  236. }
  237. /**
  238. * Get the fluent commands for the grammar.
  239. *
  240. * @return array
  241. */
  242. public function getFluentCommands()
  243. {
  244. return $this->fluentCommands;
  245. }
  246. /**
  247. * Check if this Grammar supports schema changes wrapped in a transaction.
  248. *
  249. * @return bool
  250. */
  251. public function supportsSchemaTransactions()
  252. {
  253. return $this->transactions;
  254. }
  255. }