PageRenderTime 79ms CodeModel.GetById 28ms RepoModel.GetById 7ms app.codeStats 0ms

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

https://gitlab.com/4gdevs/online-class-record-system
PHP | 459 lines | 221 code | 71 blank | 167 comment | 18 complexity | db23836697c415f85c4d80c20cf592a8 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Schema\Grammars;
  3. use RuntimeException;
  4. use Doctrine\DBAL\Types\Type;
  5. use Illuminate\Support\Fluent;
  6. use Doctrine\DBAL\Schema\Table;
  7. use Doctrine\DBAL\Schema\Column;
  8. use Doctrine\DBAL\Schema\TableDiff;
  9. use Illuminate\Database\Connection;
  10. use Doctrine\DBAL\Schema\Comparator;
  11. use Illuminate\Database\Query\Expression;
  12. use Illuminate\Database\Schema\Blueprint;
  13. use Illuminate\Database\Grammar as BaseGrammar;
  14. use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
  15. abstract class Grammar extends BaseGrammar
  16. {
  17. /**
  18. * Compile a rename column command.
  19. *
  20. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  21. * @param \Illuminate\Support\Fluent $command
  22. * @param \Illuminate\Database\Connection $connection
  23. * @return array
  24. */
  25. public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
  26. {
  27. $schema = $connection->getDoctrineSchemaManager();
  28. $table = $this->getTablePrefix().$blueprint->getTable();
  29. $column = $connection->getDoctrineColumn($table, $command->from);
  30. $tableDiff = $this->getRenamedDiff($blueprint, $command, $column, $schema);
  31. return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
  32. }
  33. /**
  34. * Get a new column instance with the new column name.
  35. *
  36. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  37. * @param \Illuminate\Support\Fluent $command
  38. * @param \Doctrine\DBAL\Schema\Column $column
  39. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
  40. * @return \Doctrine\DBAL\Schema\TableDiff
  41. */
  42. protected function getRenamedDiff(Blueprint $blueprint, Fluent $command, Column $column, SchemaManager $schema)
  43. {
  44. $tableDiff = $this->getDoctrineTableDiff($blueprint, $schema);
  45. return $this->setRenamedColumns($tableDiff, $command, $column);
  46. }
  47. /**
  48. * Set the renamed columns on the table diff.
  49. *
  50. * @param \Doctrine\DBAL\Schema\TableDiff $tableDiff
  51. * @param \Illuminate\Support\Fluent $command
  52. * @param \Doctrine\DBAL\Schema\Column $column
  53. * @return \Doctrine\DBAL\Schema\TableDiff
  54. */
  55. protected function setRenamedColumns(TableDiff $tableDiff, Fluent $command, Column $column)
  56. {
  57. $newColumn = new Column($command->to, $column->getType(), $column->toArray());
  58. $tableDiff->renamedColumns = [$command->from => $newColumn];
  59. return $tableDiff;
  60. }
  61. /**
  62. * Compile a foreign key command.
  63. *
  64. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  65. * @param \Illuminate\Support\Fluent $command
  66. * @return string
  67. */
  68. public function compileForeign(Blueprint $blueprint, Fluent $command)
  69. {
  70. $table = $this->wrapTable($blueprint);
  71. $on = $this->wrapTable($command->on);
  72. // We need to prepare several of the elements of the foreign key definition
  73. // before we can create the SQL, such as wrapping the tables and convert
  74. // an array of columns to comma-delimited strings for the SQL queries.
  75. $columns = $this->columnize($command->columns);
  76. $onColumns = $this->columnize((array) $command->references);
  77. $sql = "alter table {$table} add constraint {$command->index} ";
  78. $sql .= "foreign key ({$columns}) references {$on} ({$onColumns})";
  79. // Once we have the basic foreign key creation statement constructed we can
  80. // build out the syntax for what should happen on an update or delete of
  81. // the affected columns, which will get something like "cascade", etc.
  82. if (! is_null($command->onDelete)) {
  83. $sql .= " on delete {$command->onDelete}";
  84. }
  85. if (! is_null($command->onUpdate)) {
  86. $sql .= " on update {$command->onUpdate}";
  87. }
  88. return $sql;
  89. }
  90. /**
  91. * Compile the blueprint's column definitions.
  92. *
  93. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  94. * @return array
  95. */
  96. protected function getColumns(Blueprint $blueprint)
  97. {
  98. $columns = [];
  99. foreach ($blueprint->getAddedColumns() as $column) {
  100. // Each of the column types have their own compiler functions which are tasked
  101. // with turning the column definition into its SQL format for this platform
  102. // used by the connection. The column's modifiers are compiled and added.
  103. $sql = $this->wrap($column).' '.$this->getType($column);
  104. $columns[] = $this->addModifiers($sql, $blueprint, $column);
  105. }
  106. return $columns;
  107. }
  108. /**
  109. * Add the column modifiers to the definition.
  110. *
  111. * @param string $sql
  112. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  113. * @param \Illuminate\Support\Fluent $column
  114. * @return string
  115. */
  116. protected function addModifiers($sql, Blueprint $blueprint, Fluent $column)
  117. {
  118. foreach ($this->modifiers as $modifier) {
  119. if (method_exists($this, $method = "modify{$modifier}")) {
  120. $sql .= $this->{$method}($blueprint, $column);
  121. }
  122. }
  123. return $sql;
  124. }
  125. /**
  126. * Get the primary key command if it exists on the blueprint.
  127. *
  128. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  129. * @param string $name
  130. * @return \Illuminate\Support\Fluent|null
  131. */
  132. protected function getCommandByName(Blueprint $blueprint, $name)
  133. {
  134. $commands = $this->getCommandsByName($blueprint, $name);
  135. if (count($commands) > 0) {
  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. return $value->name == $name;
  150. });
  151. }
  152. /**
  153. * Get the SQL for the column data type.
  154. *
  155. * @param \Illuminate\Support\Fluent $column
  156. * @return string
  157. */
  158. protected function getType(Fluent $column)
  159. {
  160. return $this->{'type'.ucfirst($column->type)}($column);
  161. }
  162. /**
  163. * Add a prefix to an array of values.
  164. *
  165. * @param string $prefix
  166. * @param array $values
  167. * @return array
  168. */
  169. public function prefixArray($prefix, array $values)
  170. {
  171. return array_map(function ($value) use ($prefix) {
  172. return $prefix.' '.$value;
  173. }, $values);
  174. }
  175. /**
  176. * Wrap a table in keyword identifiers.
  177. *
  178. * @param mixed $table
  179. * @return string
  180. */
  181. public function wrapTable($table)
  182. {
  183. if ($table instanceof Blueprint) {
  184. $table = $table->getTable();
  185. }
  186. return parent::wrapTable($table);
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function wrap($value, $prefixAlias = false)
  192. {
  193. if ($value instanceof Fluent) {
  194. $value = $value->name;
  195. }
  196. return parent::wrap($value, $prefixAlias);
  197. }
  198. /**
  199. * Format a value so that it can be used in "default" clauses.
  200. *
  201. * @param mixed $value
  202. * @return string
  203. */
  204. protected function getDefaultValue($value)
  205. {
  206. if ($value instanceof Expression) {
  207. return $value;
  208. }
  209. if (is_bool($value)) {
  210. return "'".(int) $value."'";
  211. }
  212. return "'".strval($value)."'";
  213. }
  214. /**
  215. * Create an empty Doctrine DBAL TableDiff from the Blueprint.
  216. *
  217. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  218. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
  219. * @return \Doctrine\DBAL\Schema\TableDiff
  220. */
  221. protected function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema)
  222. {
  223. $table = $this->getTablePrefix().$blueprint->getTable();
  224. $tableDiff = new TableDiff($table);
  225. $tableDiff->fromTable = $schema->listTableDetails($table);
  226. return $tableDiff;
  227. }
  228. /**
  229. * Compile a change column command into a series of SQL statements.
  230. *
  231. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  232. * @param \Illuminate\Support\Fluent $command
  233. * @param \Illuminate\Database\Connection $connection
  234. * @return array
  235. */
  236. public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection)
  237. {
  238. if (! $connection->isDoctrineAvailable()) {
  239. throw new RuntimeException(sprintf(
  240. 'Changing columns for table "%s" requires Doctrine DBAL; install "doctrine/dbal".',
  241. $blueprint->getTable()
  242. ));
  243. }
  244. $schema = $connection->getDoctrineSchemaManager();
  245. $tableDiff = $this->getChangedDiff($blueprint, $schema);
  246. if ($tableDiff !== false) {
  247. return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
  248. }
  249. return [];
  250. }
  251. /**
  252. * Get the Doctrine table difference for the given changes.
  253. *
  254. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  255. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
  256. * @return \Doctrine\DBAL\Schema\TableDiff|bool
  257. */
  258. protected function getChangedDiff(Blueprint $blueprint, SchemaManager $schema)
  259. {
  260. $table = $schema->listTableDetails($this->getTablePrefix().$blueprint->getTable());
  261. return (new Comparator)->diffTable($table, $this->getTableWithColumnChanges($blueprint, $table));
  262. }
  263. /**
  264. * Get a copy of the given Doctrine table after making the column changes.
  265. *
  266. * @param \Illuminate\Database\Schema\Blueprint $blueprint
  267. * @param \Doctrine\DBAL\Schema\Table $table
  268. * @return \Doctrine\DBAL\Schema\TableDiff
  269. */
  270. protected function getTableWithColumnChanges(Blueprint $blueprint, Table $table)
  271. {
  272. $table = clone $table;
  273. foreach ($blueprint->getChangedColumns() as $fluent) {
  274. $column = $this->getDoctrineColumnForChange($table, $fluent);
  275. // Here we will spin through each fluent column definition and map it to the proper
  276. // Doctrine column definitions - which is necessary because Laravel and Doctrine
  277. // use some different terminology for various column attributes on the tables.
  278. foreach ($fluent->getAttributes() as $key => $value) {
  279. if (! is_null($option = $this->mapFluentOptionToDoctrine($key))) {
  280. if (method_exists($column, $method = 'set'.ucfirst($option))) {
  281. $column->{$method}($this->mapFluentValueToDoctrine($option, $value));
  282. }
  283. }
  284. }
  285. }
  286. return $table;
  287. }
  288. /**
  289. * Get the Doctrine column instance for a column change.
  290. *
  291. * @param \Doctrine\DBAL\Schema\Table $table
  292. * @param \Illuminate\Support\Fluent $fluent
  293. * @return \Doctrine\DBAL\Schema\Column
  294. */
  295. protected function getDoctrineColumnForChange(Table $table, Fluent $fluent)
  296. {
  297. return $table->changeColumn(
  298. $fluent['name'], $this->getDoctrineColumnChangeOptions($fluent)
  299. )->getColumn($fluent['name']);
  300. }
  301. /**
  302. * Get the Doctrine column change options.
  303. *
  304. * @param \Illuminate\Support\Fluent $fluent
  305. * @return array
  306. */
  307. protected function getDoctrineColumnChangeOptions(Fluent $fluent)
  308. {
  309. $options = ['type' => $this->getDoctrineColumnType($fluent['type'])];
  310. if (in_array($fluent['type'], ['text', 'mediumText', 'longText'])) {
  311. $options['length'] = $this->calculateDoctrineTextLength($fluent['type']);
  312. }
  313. return $options;
  314. }
  315. /**
  316. * Get the doctrine column type.
  317. *
  318. * @param string $type
  319. * @return \Doctrine\DBAL\Types\Type
  320. */
  321. protected function getDoctrineColumnType($type)
  322. {
  323. $type = strtolower($type);
  324. switch ($type) {
  325. case 'biginteger':
  326. $type = 'bigint';
  327. break;
  328. case 'smallinteger':
  329. $type = 'smallint';
  330. break;
  331. case 'mediumtext':
  332. case 'longtext':
  333. $type = 'text';
  334. break;
  335. }
  336. return Type::getType($type);
  337. }
  338. /**
  339. * Calculate the proper column length to force the Doctrine text type.
  340. *
  341. * @param string $type
  342. * @return int
  343. */
  344. protected function calculateDoctrineTextLength($type)
  345. {
  346. switch ($type) {
  347. case 'mediumText':
  348. return 65535 + 1;
  349. case 'longText':
  350. return 16777215 + 1;
  351. default:
  352. return 255 + 1;
  353. }
  354. }
  355. /**
  356. * Get the matching Doctrine option for a given Fluent attribute name.
  357. *
  358. * @param string $attribute
  359. * @return string|null
  360. */
  361. protected function mapFluentOptionToDoctrine($attribute)
  362. {
  363. switch ($attribute) {
  364. case 'type':
  365. case 'name':
  366. return;
  367. case 'nullable':
  368. return 'notnull';
  369. case 'total':
  370. return 'precision';
  371. case 'places':
  372. return 'scale';
  373. default:
  374. return $attribute;
  375. }
  376. }
  377. /**
  378. * Get the matching Doctrine value for a given Fluent attribute.
  379. *
  380. * @param string $option
  381. * @param mixed $value
  382. * @return mixed
  383. */
  384. protected function mapFluentValueToDoctrine($option, $value)
  385. {
  386. return $option == 'notnull' ? ! $value : $value;
  387. }
  388. }